//
//  IFXPDFTextBoxesSample.m
//  IFXPDFFactory-iOS-SamplesBrowser
//
//  Created by Sorin Nistor on 11/12/13.
//  Copyright (c) 2013 IFXFactory. All rights reserved.
//

#import "IFXPDFTextBoxesSample.h"

@implementation IFXPDFTextBoxesSample

+ (IFXPDFDocument*) run
{
    IFXPDFDocument* pdfDocument = [[IFXPDFDocument alloc] init];
    IFXPDFPage* page = [IFXPDFPage emptyPage];
    [pdfDocument.pages addPage:page];
    
    IFXPDFBrush* blackBrush = [IFXPDFBrush brushWithColor:[IFXPDFRgbColor blackColor]];
    IFXPDFPen* redPen = [IFXPDFPen penWithColor:[IFXPDFRgbColor redColor] andWidth:1];
    
    IFXPDFFont* titleFont = [[IFXPDFFont alloc] init];
    titleFont.fontFace = IFXPDFHelveticaBoldFontFace;
    titleFont.size = 24;
    IFXPDFFont* helvetica = [[IFXPDFFont alloc] init];
    helvetica.fontFace = IFXPDFHelveticaFontFace;
    helvetica.size = 10;
    
    [page.graphics drawText:@"Text Boxes" withFont:titleFont brush:blackBrush atX:20 y:50];
    
    NSString* text = @"Lorem ipsum dolor sit amet, sed mattis justo et diam rhoncus, imperdiet imperdiet sapien lobortis. "
                      "Duis ultricies orci ut libero auctor laoreet. Sed bibendum, dui id hendrerit tristique, neque mi "
                      "sollicitudin libero, in rutrum neque risus ac nulla. In eget risus at sapien gravida porta. Aliquam "
                      "accumsan mi vitae augue faucibus tempor. Proin consectetur mauris quis tortor gravida, vel malesuada "
                      "velit iaculis. Suspendisse vel congue mi. Cras quis urna sit amet tellus auctor ultricies non non nulla. "
                      "Pellentesque eu ullamcorper massa. Fusce venenatis ipsum ultrices accumsan vehicula. "
                      "Sed interdum justo arcu. In ut auctor augue, sed sodales libero. Nulla ullamcorper lectus non felis "
                      "scelerisque feugiat. Suspendisse elit lacus, mattis vel nibh vitae, consequat placerat massa. "
                      "Mauris ac massa viverra, pellentesque lectus ut, pellentesque lorem. Nullam ante turpis, placerat "
                      "sed risus non, consectetur pellentesque turpis.";
    
    // Draw box border
    [page.graphics drawRectangleWithPen:redPen atX:20 y:80 withWidth:200 height:145];
    // When the textbox height is 0 the text is wrapped at the specified width and takes all the height it requires.
    [page.graphics drawTextBox:text withFont:helvetica brush:blackBrush
                           atX:20 y:80 width:200 height:0 rotation:0
               horizontalAlign:IFXPDFTextJustifiedHorizontalAlign verticalAlign:IFXPDFTextTopVerticalAlign];
    
    // Draw box border
    [page.graphics drawRectangleWithPen:redPen atX:300 y:80 withWidth:200 height:145];
    // When the textbox height is greater than 0 the text is wrapped at the specified width and only the text that fits the box is displayed.
    [page.graphics drawTextBox:text withFont:helvetica brush:blackBrush
                           atX:300 y:80 width:200 height:145 rotation:0
               horizontalAlign:IFXPDFTextJustifiedHorizontalAlign verticalAlign:IFXPDFTextTopVerticalAlign];

    titleFont.size = 16;
    [page.graphics drawText:@"Rotation" withFont:titleFont brush:blackBrush atX:20 y:320];
    
    // A textbox is always rotated around top left corner no matter how text is aligned inside the box.
    [page.graphics drawLineWithPen:redPen fromX1:20 y1:350 toX2:20 y2:630];
    [page.graphics drawLineWithPen:redPen fromX1:20 y1:480 toX2:300 y2:480];
    // Draw box border
    [page.graphics drawRectangleWithPen:redPen atX:20 y:480 withWidth:200 height:150 rotation:30];
    // Draw rotated text box
    [page.graphics drawTextBox:text withFont:helvetica brush:blackBrush
                           atX:20 y:480 width:200 height:150 rotation:30
               horizontalAlign:IFXPDFTextJustifiedHorizontalAlign verticalAlign:IFXPDFTextTopVerticalAlign];

    // Rotating a textbox around the center can be done using affine transforms.
    [page.graphics drawLineWithPen:redPen fromX1:330 y1:480 toX2:600 y2:480];
    [page.graphics drawLineWithPen:redPen fromX1:460 y1:330 toX2:460 y2:630];
    
    [page.graphics saveGraphicsState];
    // Translate the coordinate system in the center of the textbox.
    [page.graphics translateTransformWithTx:460 andTy:480];
    // Rotate the coordinate system not the textbox.
    [page.graphics rotateTransformWithAngle:30];

    // Draw box border
    [page.graphics drawRectangleWithPen:redPen atX:-100 y:-75 withWidth:200 height:150];
    // Draw rotated text box
    [page.graphics drawTextBox:text withFont:helvetica brush:blackBrush
                           atX:-100 y:-75 width:200 height:150 rotation:0
               horizontalAlign:IFXPDFTextJustifiedHorizontalAlign verticalAlign:IFXPDFTextTopVerticalAlign];
    
    [page.graphics restoreGraphicsState];
    return pdfDocument;
}
@end