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

#import "IFXPDFRoundedRectanglesSample.h"

@implementation IFXPDFRoundedRectanglesSample

+ (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* darkGreenPen = [IFXPDFPen penWithColor:[IFXPDFRgbColor darkGreenColor] andWidth:1];
    
    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:@"Rounded rectangles" withFont:titleFont brush:blackBrush atX:20 y:50];
    
    [page.graphics drawLineWithPen:blackPen fromX1:20 y1:150 toX2:300 y2:150];
    [page.graphics drawLineWithPen:blackPen fromX1:80 y1:70 toX2:80 y2:350];
    [page.graphics drawRoundRectangleWithPen:darkGreenPen atX:80 y:150
                                   withWidth:180 height:100 ellipseWidth:100 ellipseHeight:60];
    
    [page.graphics drawLineWithPen:blackPen fromX1:320 y1:150 toX2:600 y2:150];
    [page.graphics drawLineWithPen:blackPen fromX1:380 y1:70 toX2:380 y2:350];
    [page.graphics drawRoundRectangleWithPen:darkGreenPen atX:380 y:150
                                   withWidth:180 height:100 ellipseWidth:100 ellipseHeight:60 rotation:30];
    
    [page.graphics drawText:@"Random rounded rectangles" withFont:sectionFont brush:blackBrush atX:20 y:385];
    IFXPDFPath* clipPath = [[IFXPDFPath alloc] init];
    [clipPath rectangleAtX:20 y:400 withWidth:570 height:300 rotation:0];
    
    [page.graphics saveGraphicsState];
    [page.graphics setClippingPath:clipPath];
    
    IFXPDFRgbColor* randomPenColor = [[IFXPDFRgbColor alloc] init];
    IFXPDFPen* randomPen = [IFXPDFPen penWithColor:randomPenColor andWidth:1];
    IFXPDFRgbColor* randomBrushColor = [[IFXPDFRgbColor alloc] init];
    IFXPDFBrush* randomBrush = [IFXPDFBrush brushWithColor:randomBrushColor];
    srand(time(NULL));
    for (int i = 0; i < 200; i++) {
        randomPenColor.red = (rand() % 256) / 255.0;
        randomPenColor.green = (rand() % 256) / 255.0;
        randomPenColor.blue = (rand() % 256) / 255.0;
        randomBrushColor.red = (rand() % 256) / 255.0;
        randomBrushColor.green = (rand() % 256) / 255.0;
        randomBrushColor.blue = (rand() % 256) / 255.0;
        
        double x = 20 + rand() % 570;
        double y = 400 + rand() % 300;
        double width = rand() % 570;
        double height = rand() % 300;
        double rotation = rand() % 360;
        
        int mode = rand() % 3;
        
        switch (mode) {
            case 0:
                [page.graphics drawRoundRectangleWithPen:randomPen atX:x y:y
                                               withWidth:width height:height ellipseWidth:width / 2 ellipseHeight:height / 2
                                                rotation:rotation];
                break;
            case 1:
                [page.graphics drawRoundRectangleWithBrush:randomBrush atX:x y:y
                                                 withWidth:width height:height ellipseWidth:width / 2 ellipseHeight:height / 2
                                                  rotation:rotation];
                break;
            case 2:
                [page.graphics drawRoundRectangleWithPen:randomPen andBrush:randomBrush
                                                     atX:x y:y
                                               withWidth:width height:height ellipseWidth:width / 2 ellipseHeight:height / 2
                                                rotation:rotation];
                break;
        }
    }
    
    [page.graphics restoreGraphicsState];
    
    return pdfDocument;
}

@end