From 69541fb13e61444ea158abf6aea39ebb220f6030 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebasti=C3=A1n=20Katzer?= Date: Tue, 22 Jan 2019 15:27:31 +0100 Subject: [PATCH] Finished implementation for header/footer for iOS --- src/ios/APPPrinterRenderer.m | 177 ++++++++++++++++++++++++++++++----- www/printer.js | 16 +++- 2 files changed, 166 insertions(+), 27 deletions(-) diff --git a/src/ios/APPPrinterRenderer.m b/src/ios/APPPrinterRenderer.m index cf71a59..5fd9af4 100644 --- a/src/ios/APPPrinterRenderer.m +++ b/src/ios/APPPrinterRenderer.m @@ -23,6 +23,12 @@ #include "APPPrinterRenderer.h" #include "APPPrinterUnit.h" +@interface APPPrinterRenderer () + +@property (nonatomic, retain) NSDictionary *settings; + +@end + @implementation APPPrinterRenderer #pragma mark - @@ -48,51 +54,178 @@ if (header) { dots = [APPPrinterUnit convert:header[@"unit"]]; - self.headerHeight = dots * [header[@"height"] doubleValue]; + self.headerHeight = dots * [header[@"height"] floatValue]; } if (footer) { - dots = [APPPrinterUnit convert:header[@"unit"]]; - self.footerHeight = dots * [footer[@"height"] doubleValue]; + dots = [APPPrinterUnit convert:footer[@"unit"]]; + self.footerHeight = dots * [footer[@"height"] floatValue]; } + _settings = spec; + return self; } #pragma mark - -#pragma mark Private +#pragma mark Callbacks - (void) drawHeaderForPageAtIndex:(NSInteger)index inRect:(CGRect)rect { - NSString *header = [NSString stringWithFormat:@"Sample header at page %ld", index + 1]; - NSMutableParagraphStyle *style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy]; - - style.alignment = NSTextAlignmentRight; - - UIFont *font = [UIFont fontWithName:@"Courier" size:UIFont.labelFontSize]; - - NSDictionary *attrs = @{ NSFontAttributeName: font, - NSParagraphStyleAttributeName: style, - NSForegroundColorAttributeName: [UIColor redColor]}; - - [header drawInRect:rect withAttributes:attrs]; + [self drawLabelsFromDictionary:_settings[@"header"] + forPageAtIndex:index + inRect:rect]; } - (void) drawFooterForPageAtIndex:(NSInteger)index inRect:(CGRect)rect { - NSString *footer = @"Sample Footer"; - NSMutableParagraphStyle *style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy]; + [self drawLabelsFromDictionary:_settings[@"footer"] + forPageAtIndex:index + inRect:rect]; +} - style.alignment = NSTextAlignmentCenter; +#pragma mark - +#pragma mark Private - UIFont *font = [UIFont fontWithName:@"Helvetica" size:UIFont.buttonFontSize]; +- (void) drawLabelsFromDictionary:(NSDictionary *)spec + forPageAtIndex:(NSInteger)index + inRect:(CGRect)rect +{ + NSDictionary* label = spec[@"label"]; + NSArray *labels = label ? @[label] : spec[@"labels"]; + + if (!labels) + { + NSString *text = spec[@"text"]; + + if (![self strIsNullOrEmpty:text]) + { + labels = @[@{ @"text":text }]; + } + } + + if (!labels) return; + + for (label in labels) { + [self drawLabelFromDictionary:label forPageAtIndex:index inRect:rect]; + } +} + +- (void) drawLabelFromDictionary:(NSDictionary *)spec + forPageAtIndex:(NSInteger)index + inRect:(CGRect)rect +{ + NSString *label = spec[@"text"]; + NSString *align = spec[@"align"]; + BOOL showIndex = [spec[@"showPageIndex"] boolValue]; + + if (showIndex) + { + label = [self strIsNullOrEmpty:label] ? @"%ld" : label; + label = [NSString stringWithFormat:label, index + 1]; + } + + if ([self strIsNullOrEmpty:label]) + return; + + NSMutableParagraphStyle *style = [[NSParagraphStyle defaultParagraphStyle] + mutableCopy]; + + if ([align isEqualToString:@"left"]) + { + style.alignment = NSTextAlignmentLeft; + } + else if ([align isEqualToString:@"right"]) + { + style.alignment = NSTextAlignmentRight; + } + else if ([align isEqualToString:@"center"]) + { + style.alignment = NSTextAlignmentCenter; + } + else if ([align isEqualToString:@"justified"]) + { + style.alignment = NSTextAlignmentJustified; + } + + NSString *hex = spec[@"color"]; + UIColor *color; + + if ([self strIsNullOrEmpty:hex]) + { + color = UIColor.darkTextColor; + } + else + { + if ([hex characterAtIndex:0] == '#') + { + hex = [hex substringFromIndex:1]; + } + + color = [self colorFromHexString:hex]; + } + + if (!color) + { + color = UIColor.darkTextColor; + } + + double size = [spec[@"size"] doubleValue]; + + if (size <= 0.0) + { + size = UIFont.smallSystemFontSize; + } + + UIFont *font = [UIFont fontWithName:spec[@"font"] size:size]; + + if (!font) + { + font = [UIFont systemFontOfSize:size]; + } + + UIFontDescriptorSymbolicTraits traits = 0; + + if ([spec[@"bold"] boolValue]) + { + traits = traits | UIFontDescriptorTraitBold; + } + + if ([spec[@"italic"] boolValue]) + { + traits = traits | UIFontDescriptorTraitItalic; + } + + font = [UIFont fontWithDescriptor:[font.fontDescriptor fontDescriptorWithSymbolicTraits:traits] + size:size]; NSDictionary *attrs = @{ NSFontAttributeName: font, NSParagraphStyleAttributeName: style, - NSForegroundColorAttributeName: [UIColor blueColor]}; + NSForegroundColorAttributeName: color }; - [footer drawInRect:rect withAttributes:attrs]; + [label drawInRect:rect withAttributes:attrs]; +} + +- (BOOL) strIsNullOrEmpty:(NSString *)string +{ + return [string isEqual:[NSNull null]] || string.length == 0; +} + +- (UIColor *)colorFromHexString:(NSString *)hex +{ + unsigned rgb = 0; + + NSScanner *scanner = [NSScanner scannerWithString:hex]; + + [scanner setCharactersToBeSkipped: + [NSCharacterSet characterSetWithCharactersInString:@"#"]]; + + [scanner scanHexInt:&rgb]; + + return [UIColor colorWithRed:((rgb & 0xFF0000) >> 16)/255.0 + green:((rgb & 0xFF00) >> 8)/255.0 + blue:(rgb & 0xFF)/255.0 alpha:1.0]; } @end diff --git a/www/printer.js b/www/printer.js index a374468..b18e4c4 100755 --- a/www/printer.js +++ b/www/printer.js @@ -57,12 +57,18 @@ exports.getDefaults = function () { header: { unit: 'cm', - height: 0, - }, + height: 1, - footer: { - unit: 'cm', - height: 0 + labels: [{ + text: 'Awesome Printer Plug-in', + align: 'center', + italic: true, + color: '#FF0000' + },{ + showPageIndex: true, + align: 'right', + bold: true + }] } }; };