//
//  IFXPDFLinesSamples.m
//  IFXPDFFactory-iOS-SamplesBrowser
//
//  Created by Sorin Nistor on 10/1/13.
//  Copyright (c) 2013 IFXFactory. All rights reserved.
//

#import "IFXPDFLinesSample.h"

@implementation IFXPDFLinesSample

+ (IFXPDFDocument*) run
{
    IFXPDFDocument* pdfDocument = [[IFXPDFDocument alloc] init];
    IFXPDFPage* page = [IFXPDFPage emptyPage];
    [pdfDocument.pages addPage:page];
    
    IFXPDFBrush* blackBrush = [IFXPDFBrush brushWithColor:[IFXPDFRgbColor blackColor]];
    IFXPDFPen* blackPen = [IFXPDFPen penWithColor:[IFXPDFRgbColor blackColor] andWidth:1];
    IFXPDFPen* lightGreenPen = [IFXPDFPen penWithColor:[IFXPDFRgbColor lightGreenColor] andWidth:16];

    IFXPDFFont* titleFont = [[IFXPDFFont alloc] init];
    titleFont.fontFace = IFXPDFHelveticaFontFace;
    titleFont.size = 16;
    IFXPDFFont* sectionFont = [[IFXPDFFont alloc] init];
    sectionFont.fontFace = IFXPDFHelveticaFontFace;
    sectionFont.size = 10;
    
    [page.graphics drawText:@"Lines" withFont:titleFont brush:blackBrush atX:20 y:50];
    [page.graphics drawText:@"Line styles:" withFont:sectionFont brush:blackBrush atX:20 y:70];
    [page.graphics drawText:@"Solid" withFont:sectionFont brush:blackBrush atX:20 y:90];
    [page.graphics drawLineWithPen:blackPen fromX1:100 y1:95 toX2:400 y2:95];
    [page.graphics drawText:@"Dashed" withFont:sectionFont brush:blackBrush atX:20 y:110];
    blackPen.dashPattern = @"3 3";
    [page.graphics drawLineWithPen:blackPen fromX1:100 y1:115 toX2:400 y2:115];
    blackPen.dashPattern = nil;
    
    [page.graphics drawText:@"Line cap styles:" withFont:sectionFont brush:blackBrush atX:20 y:150];
    [page.graphics drawText:@"Flat" withFont:sectionFont brush:blackBrush atX:20 y:175];
    lightGreenPen.lineCap = IFXPDFFlatLineCap; // This is the default value.
    [page.graphics drawLineWithPen:lightGreenPen fromX1:100 y1:180 toX2:400 y2:180];
    [page.graphics drawLineWithPen:blackPen fromX1:100 y1:180 toX2:400 y2:180];
    [page.graphics drawText:@"Square" withFont:sectionFont brush:blackBrush atX:20 y:195];
    lightGreenPen.lineCap = IFXPDFSquareLineCap;
    [page.graphics drawLineWithPen:lightGreenPen fromX1:100 y1:200 toX2:400 y2:200];
    [page.graphics drawLineWithPen:blackPen fromX1:100 y1:200 toX2:400 y2:200];
    [page.graphics drawText:@"Round" withFont:sectionFont brush:blackBrush atX:20 y:215];
    lightGreenPen.lineCap = IFXPDFRoundLineCap;
    [page.graphics drawLineWithPen:lightGreenPen fromX1:100 y1:220 toX2:400 y2:220];
    [page.graphics drawLineWithPen:blackPen fromX1:100 y1:220 toX2:400 y2:220];

    [page.graphics drawText:@"Line join styles:" withFont:sectionFont brush:blackBrush atX:20 y:250];
    // Miter line join
    [page.graphics drawText:@"Miter:" withFont:sectionFont brush:blackBrush atX:20 y:280];
    IFXPDFPath* miterPath = [[IFXPDFPath alloc] init];
    [miterPath moveToX:150 y:320];
    [miterPath lineToX:250 y:260];
    [miterPath lineToX:350 y:320];
    lightGreenPen.lineCap = IFXPDFFlatLineCap;
    lightGreenPen.lineJoin = IFXPDFMiterLineJoin;
    [page.graphics drawPath:miterPath withPen:lightGreenPen];
    // Bevel line join
    [page.graphics drawText:@"Bevel:" withFont:sectionFont brush:blackBrush atX:20 y:360];
    IFXPDFPath* bevelPath = [[IFXPDFPath alloc] init];
    [bevelPath moveToX:150 y:400];
    [bevelPath lineToX:250 y:340];
    [bevelPath lineToX:350 y:400];
    lightGreenPen.lineCap = IFXPDFFlatLineCap;
    lightGreenPen.lineJoin = IFXPDFBevelLineJoin;
    [page.graphics drawPath:bevelPath withPen:lightGreenPen];
    // Round line join
    [page.graphics drawText:@"Round:" withFont:sectionFont brush:blackBrush atX:20 y:440];
    IFXPDFPath* roundPath = [[IFXPDFPath alloc] init];
    [roundPath moveToX:150 y:480];
    [roundPath lineToX:250 y:420];
    [roundPath lineToX:350 y:480];
    lightGreenPen.lineCap = IFXPDFFlatLineCap;
    lightGreenPen.lineJoin = IFXPDFRoundLineJoin;
    [page.graphics drawPath:roundPath withPen:lightGreenPen];

    [page.graphics drawText:@"Random lines" withFont:sectionFont brush:blackBrush atX:20 y:520];
    IFXPDFPath* clipPath = [[IFXPDFPath alloc] init];
    [clipPath rectangleAtX:20 y:550 withWidth:570 height:230 rotation:0];
    [page.graphics saveGraphicsState];
    [page.graphics setClippingPath:clipPath];

    IFXPDFRgbColor* randomColor = [[IFXPDFRgbColor alloc] init];
    IFXPDFPen* randomPen = [IFXPDFPen penWithColor:randomColor andWidth:1];
    srand(time(NULL));
    for (int i = 0; i < 200; i++) {
        randomColor.red = (rand() % 256) / 255.0;
        randomColor.green = (rand() % 256) / 255.0;
        randomColor.blue = (rand() % 256) / 255.0;
        
        [page.graphics drawLineWithPen:randomPen fromX1:20 + rand() % 570 y1:550 + rand() % 230
                                  toX2:20 + rand() % 570 y2:550 + rand() % 230];
    }
    
    [page.graphics restoreGraphicsState];
    return pdfDocument;
}

@end