From 578342516a54a24f25ef3b75277062868fe60efc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebasti=C3=A1n=20Katzer?= Date: Mon, 21 Jan 2019 17:52:28 +0100 Subject: [PATCH] Initial support for header and footer --- plugin.xml | 3 + src/ios/APPPrinter.m | 9 ++- src/ios/APPPrinterRenderer.h | 26 ++++++++ src/ios/APPPrinterRenderer.m | 116 +++++++++++++++++++++++++++++++++++ www/printer.js | 10 +++ 5 files changed, 159 insertions(+), 5 deletions(-) create mode 100644 src/ios/APPPrinterRenderer.h create mode 100644 src/ios/APPPrinterRenderer.m diff --git a/plugin.xml b/plugin.xml index 3eae0df..2a481b6 100644 --- a/plugin.xml +++ b/plugin.xml @@ -75,6 +75,9 @@ + + + diff --git a/src/ios/APPPrinter.m b/src/ios/APPPrinter.m index 6f2f8f7..426728b 100755 --- a/src/ios/APPPrinter.m +++ b/src/ios/APPPrinter.m @@ -22,8 +22,8 @@ #import "APPPrinter.h" #import "APPPrinterItem.h" #import "APPPrinterPaper.h" -#import "APPPrinterLayout.h" -#import "UIPrintInteractionController+APPPrinter.h" +#include "APPPrinterRenderer.h" +#include "UIPrintInteractionController+APPPrinter.h" @interface APPPrinter () @@ -217,9 +217,8 @@ if ([item isKindOfClass:UIPrintFormatter.class]) { - ctrl.printFormatter = - [APPPrinterLayout configureFormatter:item - withLayoutFromDictionary:settings[@"layout"]]; + ctrl.printPageRenderer = + [[APPPrinterRenderer alloc] initWithDictionary:settings formatter:item]; } else { diff --git a/src/ios/APPPrinterRenderer.h b/src/ios/APPPrinterRenderer.h new file mode 100644 index 0000000..c904d45 --- /dev/null +++ b/src/ios/APPPrinterRenderer.h @@ -0,0 +1,26 @@ +/* + Copyright 2013 appPlant GmbH + + Licensed to the Apache Software Foundation (ASF) under one + or more contributor license agreements. See the NOTICE file + distributed with this work for additional information + regarding copyright ownership. The ASF licenses this file + to you under the Apache License, Version 2.0 (the + "License"); you may not use this file except in compliance + with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, + software distributed under the License is distributed on an + "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + KIND, either express or implied. See the License for the + specific language governing permissions and limitations + under the License. + */ + +@interface APPPrinterRenderer : UIPrintPageRenderer + +- (instancetype) initWithDictionary:(NSDictionary *)spec formatter:(UIPrintFormatter *)formatter; + +@end diff --git a/src/ios/APPPrinterRenderer.m b/src/ios/APPPrinterRenderer.m new file mode 100644 index 0000000..c67c065 --- /dev/null +++ b/src/ios/APPPrinterRenderer.m @@ -0,0 +1,116 @@ +/* + Copyright 2013 appPlant GmbH + + Licensed to the Apache Software Foundation (ASF) under one + or more contributor license agreements. See the NOTICE file + distributed with this work for additional information + regarding copyright ownership. The ASF licenses this file + to you under the Apache License, Version 2.0 (the + "License"); you may not use this file except in compliance + with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, + software distributed under the License is distributed on an + "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + KIND, either express or implied. See the License for the + specific language governing permissions and limitations + under the License. + */ + +#include "APPPrinterLayout.h" +#include "APPPrinterRenderer.h" + +@implementation APPPrinterRenderer + +#pragma mark - +#pragma mark Public + +- (instancetype) initWithDictionary:(NSDictionary *)spec formatter:(UIPrintFormatter *)formatter +{ + NSDictionary* layout = spec[@"layout"]; + NSDictionary* header = spec[@"header"]; + NSDictionary* footer = spec[@"footer"]; + double dots = 0; + + self = [self init]; + + if (layout) + { + [APPPrinterLayout configureFormatter:formatter + withLayoutFromDictionary:layout]; + } + + [self addPrintFormatter:formatter startingAtPageAtIndex:0]; + + if (header) + { + dots = [self pointsPerUnit:header[@"unit"]]; + self.headerHeight = dots * [header[@"height"] doubleValue]; + } + + if (footer) + { + dots = [self pointsPerUnit:header[@"unit"]]; + self.footerHeight = dots * [footer[@"height"] doubleValue]; + } + + return self; +} + +#pragma mark - +#pragma mark Private + +- (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]; +} + +- (void) drawFooterForPageAtIndex:(NSInteger)index inRect:(CGRect)rect +{ + NSString *footer = @"Sample Footer"; + NSMutableParagraphStyle *style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy]; + + style.alignment = NSTextAlignmentCenter; + + UIFont *font = [UIFont fontWithName:@"Helvetica" size:UIFont.buttonFontSize]; + + NSDictionary *attrs = @{ NSFontAttributeName: font, + NSParagraphStyleAttributeName: style, + NSForegroundColorAttributeName: [UIColor blueColor]}; + + [footer drawInRect:rect withAttributes:attrs]; +} + +- (double) pointsPerUnit:(NSString*)unit +{ + if ([unit isEqualToString:@"in"]) + return 72.0; + + if ([unit isEqualToString:@"mm"]) + return 72.0 / 25.4; + + if ([unit isEqualToString:@"cm"]) + return 72.0 / 2.54; + + if (![unit isEqualToString:@"pp"]) + { + NSLog(@"[cordova-plugin-printer] unit not recogniced: %@", unit); + } + + return 1.0; +} + +@end diff --git a/www/printer.js b/www/printer.js index e7f6035..a374468 100755 --- a/www/printer.js +++ b/www/printer.js @@ -53,6 +53,16 @@ exports.getDefaults = function () { maxHeight: 0, maxWidth: 0, padding: { top: 0, left: 0, right: 0, bottom: 0 } + }, + + header: { + unit: 'cm', + height: 0, + }, + + footer: { + unit: 'cm', + height: 0 } }; };