From f29b519b90beb7fe0ab49e6704ac8bc1aed2e916 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebasti=C3=A1n=20Katzer?= Date: Thu, 7 Feb 2019 16:42:11 +0100 Subject: [PATCH] Change options structure --- plugin.xml | 4 +- src/android/PrintManager.java | 6 +- .../{APPPrinterStyle.h => APPPrinterFont.h} | 4 +- .../{APPPrinterStyle.m => APPPrinterFont.m} | 12 ++-- src/ios/APPPrinterLayout.h | 7 +- src/ios/APPPrinterLayout.m | 40 +++++------ src/ios/APPPrinterPaper.h | 3 +- src/ios/APPPrinterPaper.m | 9 +-- src/ios/APPPrinterRenderer.m | 40 +++++------ src/ios/APPPrinterUnit.h | 2 +- src/ios/APPPrinterUnit.m | 67 +++++++++++++++---- www/printer.js | 67 +++++++++++-------- 12 files changed, 147 insertions(+), 114 deletions(-) rename src/ios/{APPPrinterStyle.h => APPPrinterFont.h} (93%) rename src/ios/{APPPrinterStyle.m => APPPrinterFont.m} (93%) diff --git a/plugin.xml b/plugin.xml index f9a19d6..47b8392 100644 --- a/plugin.xml +++ b/plugin.xml @@ -76,8 +76,8 @@ - - + + diff --git a/src/android/PrintManager.java b/src/android/PrintManager.java index 483cabe..7416f53 100644 --- a/src/android/PrintManager.java +++ b/src/android/PrintManager.java @@ -301,7 +301,7 @@ class PrintManager { boolean jsEnabled = settings.optBoolean("javascript", false); WebView view = new WebView(context); WebSettings spec = view.getSettings(); - JSONObject style = settings.optJSONObject("style"); + JSONObject font = settings.optJSONObject("font"); spec.setDatabaseEnabled(true); spec.setGeolocationEnabled(true); @@ -309,9 +309,9 @@ class PrintManager { spec.setUseWideViewPort(true); spec.setJavaScriptEnabled(jsEnabled); - if (style != null && style.has("size")) + if (font != null && font.has("size")) { - spec.setDefaultFixedFontSize(style.optInt("size", 16)); + spec.setDefaultFixedFontSize(font.optInt("size", 16)); } if (SDK_INT >= 21) diff --git a/src/ios/APPPrinterStyle.h b/src/ios/APPPrinterFont.h similarity index 93% rename from src/ios/APPPrinterStyle.h rename to src/ios/APPPrinterFont.h index b71283b..22cf2e2 100644 --- a/src/ios/APPPrinterStyle.h +++ b/src/ios/APPPrinterFont.h @@ -20,7 +20,7 @@ under the License. */ -@interface APPPrinterStyle : NSObject +@interface APPPrinterFont : NSObject - (instancetype) initWithDictionary:(NSDictionary *)spec; @@ -28,7 +28,7 @@ - (UIColor *)color; -- (NSTextAlignment) textAlignment; +- (NSTextAlignment) alignment; - (NSDictionary *)attributes; diff --git a/src/ios/APPPrinterStyle.m b/src/ios/APPPrinterFont.m similarity index 93% rename from src/ios/APPPrinterStyle.m rename to src/ios/APPPrinterFont.m index 91fd3fa..f1c81d5 100644 --- a/src/ios/APPPrinterStyle.m +++ b/src/ios/APPPrinterFont.m @@ -20,15 +20,15 @@ under the License. */ -#include "APPPrinterStyle.h" +#include "APPPrinterFont.h" -@interface APPPrinterStyle () +@interface APPPrinterFont () @property (nonatomic, retain) NSDictionary *settings; @end -@implementation APPPrinterStyle +@implementation APPPrinterFont #pragma mark - #pragma mark Public @@ -47,7 +47,7 @@ NSMutableParagraphStyle *style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy]; - style.alignment = self.textAlignment; + style.alignment = self.alignment; return @{ NSFontAttributeName: self.font, NSParagraphStyleAttributeName: style, @@ -63,7 +63,7 @@ size = UIFont.smallSystemFontSize; } - UIFont *font = [UIFont fontWithName:_settings[@"font"] size:size]; + UIFont *font = [UIFont fontWithName:_settings[@"name"] size:size]; if (!font) { @@ -115,7 +115,7 @@ return color; } -- (NSTextAlignment) textAlignment +- (NSTextAlignment) alignment { NSString *align = _settings[@"align"]; diff --git a/src/ios/APPPrinterLayout.h b/src/ios/APPPrinterLayout.h index 73a783f..1099741 100644 --- a/src/ios/APPPrinterLayout.h +++ b/src/ios/APPPrinterLayout.h @@ -21,13 +21,12 @@ @interface APPPrinterLayout : NSObject -+ (UIPrintFormatter *) configureFormatter:(UIPrintFormatter *)formatter withLayoutFromDictionary:(nullable NSDictionary *)layoutSpec withStyleFromDictionary:(nullable NSDictionary *)styleSpec; ++ (UIPrintFormatter *) configureFormatter:(UIPrintFormatter *)formatter withSettings:(nullable NSDictionary *)settings; -// The margins for each printed page. @property(nonatomic) UIEdgeInsets contentInsets; -// The maximum height of the content area. + @property(nonatomic) CGFloat maximumContentHeight; -// The maximum width of the content area. + @property(nonatomic) CGFloat maximumContentWidth; @end diff --git a/src/ios/APPPrinterLayout.m b/src/ios/APPPrinterLayout.m index 75367b7..6fc2214 100644 --- a/src/ios/APPPrinterLayout.m +++ b/src/ios/APPPrinterLayout.m @@ -20,7 +20,7 @@ */ #include "APPPrinterLayout.h" -#include "APPPrinterStyle.h" +#include "APPPrinterFont.h" #include "APPPrinterUnit.h" @implementation APPPrinterLayout @@ -34,19 +34,16 @@ if (!spec) return self; - NSDictionary* insests = spec[@"padding"]; - double maxWidth = [spec[@"maxWidth"] doubleValue]; - double maxHeight = [spec[@"maxHeight"] doubleValue]; - double dots = [APPPrinterUnit convert:spec[@"unit"]]; + NSDictionary *margin = spec[@"margin"]; - _contentInsets = UIEdgeInsetsMake(dots * [insests[@"top"] doubleValue], - dots * [insests[@"left"] doubleValue], - dots * [insests[@"bottom"] doubleValue], - dots * [insests[@"right"] doubleValue]); + _contentInsets = UIEdgeInsetsMake([APPPrinterUnit convert:margin[@"top"]], + [APPPrinterUnit convert:margin[@"left"]], + [APPPrinterUnit convert:margin[@"bottom"]], + [APPPrinterUnit convert:margin[@"right"]]); - _maximumContentWidth = dots * maxWidth; + _maximumContentWidth = [APPPrinterUnit convert:spec[@"maxWidth"]]; - _maximumContentHeight = dots * maxHeight; + _maximumContentHeight = [APPPrinterUnit convert:spec[@"maxHeight"]]; return self; } @@ -55,17 +52,16 @@ #pragma mark Public + (UIPrintFormatter *) configureFormatter:(UIPrintFormatter *)formatter - withLayoutFromDictionary:(NSDictionary *)layoutSpec - withStyleFromDictionary:(nullable NSDictionary *)styleSpec + withSettings:(NSDictionary *)settings { - id layout = [[self alloc] initWithDictionary:layoutSpec]; + id layout = [[self alloc] initWithDictionary:settings]; [layout configureFormatter:formatter]; - if (styleSpec && ![formatter isKindOfClass:UIMarkupTextPrintFormatter.class]) + if (settings && ![formatter isKindOfClass:UIMarkupTextPrintFormatter.class]) { [layout configureTextFormatter:(UISimpleTextPrintFormatter *)formatter - withStyleFromDictionary:styleSpec]; + withSettings:settings]; } return formatter; @@ -87,14 +83,14 @@ } - (void) configureTextFormatter:(UISimpleTextPrintFormatter *)formatter - withStyleFromDictionary:(NSDictionary *)spec + withSettings:(NSDictionary *)settings { - APPPrinterStyle *style = [[APPPrinterStyle alloc] - initWithDictionary:spec]; + APPPrinterFont *font = [[APPPrinterFont alloc] + initWithDictionary:settings[@"font"]]; - formatter.font = style.font; - formatter.color = style.color; - formatter.textAlignment = style.textAlignment; + formatter.font = font.font; + formatter.color = font.color; + formatter.textAlignment = font.alignment; } @end diff --git a/src/ios/APPPrinterPaper.h b/src/ios/APPPrinterPaper.h index a84c3f7..2b0a158 100644 --- a/src/ios/APPPrinterPaper.h +++ b/src/ios/APPPrinterPaper.h @@ -25,9 +25,8 @@ - (UIPrintPaper*) bestPaperFromArray:(NSArray *)list; -// The length to use when cutting the page if using roll printers. @property(nonatomic) CGFloat length; -// The size of the printed page. + @property(nonatomic) CGSize size; @end diff --git a/src/ios/APPPrinterPaper.m b/src/ios/APPPrinterPaper.m index 2758e16..540ff54 100644 --- a/src/ios/APPPrinterPaper.m +++ b/src/ios/APPPrinterPaper.m @@ -31,13 +31,10 @@ { self = [self init]; - double dots = [APPPrinterUnit convert:spec[@"unit"]]; - double length = [spec[@"length"] doubleValue]; - double height = [spec[@"height"] doubleValue]; - double width = [spec[@"width"] doubleValue]; + _size = CGSizeMake([APPPrinterUnit convert:spec[@"width"]], + [APPPrinterUnit convert:spec[@"height"]]); - _size = CGSizeMake(dots * width, dots * height); - _length = dots * length; + _length = [APPPrinterUnit convert:spec[@"length"]]; return self; } diff --git a/src/ios/APPPrinterRenderer.m b/src/ios/APPPrinterRenderer.m index 6e0d392..8310154 100644 --- a/src/ios/APPPrinterRenderer.m +++ b/src/ios/APPPrinterRenderer.m @@ -21,7 +21,7 @@ #include "APPPrinterLayout.h" #include "APPPrinterRenderer.h" -#include "APPPrinterStyle.h" +#include "APPPrinterFont.h" #include "APPPrinterUnit.h" @interface APPPrinterRenderer () @@ -39,26 +39,22 @@ { NSDictionary* header = spec[@"header"]; NSDictionary* footer = spec[@"footer"]; - double dots = 0; self = [self init]; [APPPrinterLayout configureFormatter:formatter - withLayoutFromDictionary:spec[@"layout"] - withStyleFromDictionary:spec[@"style"]]; + withSettings:spec]; [self addPrintFormatter:formatter startingAtPageAtIndex:0]; if (header) { - dots = [APPPrinterUnit convert:header[@"unit"]]; - self.headerHeight = dots * [header[@"height"] floatValue]; + self.headerHeight = [APPPrinterUnit convert:header[@"height"]]; } if (footer) { - dots = [APPPrinterUnit convert:footer[@"unit"]]; - self.footerHeight = dots * [footer[@"height"] floatValue]; + self.footerHeight = [APPPrinterUnit convert:footer[@"height"]]; } _settings = spec; @@ -139,15 +135,14 @@ if ([self strIsNullOrEmpty:label]) return; - NSDictionary *position = spec[@"position"]; - NSDictionary *style = spec[@"style"]; + APPPrinterFont *font = [[APPPrinterFont alloc] + initWithDictionary:spec[@"font"]]; - NSDictionary *attributes = [[APPPrinterStyle alloc] - initWithDictionary:style].attributes; + NSDictionary *attributes = font.attributes; - if (position) + if (spec[@"top"] || spec[@"left"] || spec[@"right"] || spec[@"bottom"]) { - [label drawAtPoint:[self pointFromPositionAsDictionary:position + [label drawAtPoint:[self pointFromPositionAsDictionary:spec forLabel:label withAttributes:attributes inRect:rect] @@ -164,11 +159,10 @@ withAttributes:(NSDictionary *)attributes inRect:(CGRect)rect { - double dots = [APPPrinterUnit convert:spec[@"unit"]]; - id top = spec[@"top"]; - id left = spec[@"left"]; - id right = spec[@"right"]; - id bottom = spec[@"bottom"]; + id top = spec[@"top"]; + id left = spec[@"left"]; + id right = spec[@"right"]; + id bottom = spec[@"bottom"]; double x = rect.origin.x, y = rect.origin.y; CGSize size; @@ -180,20 +174,20 @@ if (top) { - y = rect.origin.y + dots * [top doubleValue]; + y = rect.origin.y + [APPPrinterUnit convert:top]; } else if (bottom) { - y = rect.origin.y - size.height - dots * [bottom doubleValue]; + y = rect.origin.y - size.height - [APPPrinterUnit convert:bottom]; } if (left) { - x = rect.origin.x + dots * [left doubleValue]; + x = rect.origin.x + [APPPrinterUnit convert:left]; } else if (right) { - x = rect.size.width - size.width - dots * [right doubleValue]; + x = rect.size.width - size.width - [APPPrinterUnit convert:right]; } return CGPointMake(x, y); diff --git a/src/ios/APPPrinterUnit.h b/src/ios/APPPrinterUnit.h index f6e160f..1681749 100644 --- a/src/ios/APPPrinterUnit.h +++ b/src/ios/APPPrinterUnit.h @@ -21,6 +21,6 @@ @interface APPPrinterUnit : NSObject -+ (double) convert:(nullable NSString *)unit; ++ (double) convert:(nullable id)unit; @end diff --git a/src/ios/APPPrinterUnit.m b/src/ios/APPPrinterUnit.m index 34834fd..514f972 100644 --- a/src/ios/APPPrinterUnit.m +++ b/src/ios/APPPrinterUnit.m @@ -23,26 +23,65 @@ @implementation APPPrinterUnit -+ (double) convert:(nullable NSString *)unit +/** + * Converts any unit value to poings. + * + * @param [ id ] unit A number, a number as string or a string with a unit. + * + * @return [ double ] The converted unit into points. + */ ++ (double) convert:(nullable id)unit { - if (!unit || [unit isEqual:[NSNull null]]) - return 1.0; + double value = 0; - 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"]) + @try + { + if (!unit || [unit isEqual:[NSNull null]]) + { + value = 0; + } + else if ([unit isKindOfClass:NSNumber.class]) + { + value = [unit longValue]; + } + else if ([unit hasSuffix:@"pt"]) + { + value = [[self stringWithoutUnit:unit] doubleValue]; + } + else if ([unit hasSuffix:@"in"]) + { + value = [[self stringWithoutUnit:unit] doubleValue] * 72.0; + } + else if ([unit hasSuffix:@"mm"]) + { + value = [[self stringWithoutUnit:unit] doubleValue] * 72.0 / 25.4; + } + else if ([unit hasSuffix:@"cm"]) { + value = [[self stringWithoutUnit:unit] doubleValue] * 72.0 / 2.54; + } + else + { + value = [unit doubleValue]; + } + } + @catch (NSException *e) { NSLog(@"[cordova-plugin-printer] unit not recognized: %@", unit); } - return 1.0; + return value; +} + +/** + * Cuts the last 2 characters from the string. + * + * @param str A string like @"2cm" + * + * @return [ NSString ] + */ ++ (NSString *) stringWithoutUnit:(NSString *)str +{ + return [str substringToIndex:[str length] - 2]; } @end diff --git a/www/printer.js b/www/printer.js index 323b40e..eff352e 100755 --- a/www/printer.js +++ b/www/printer.js @@ -26,46 +26,59 @@ var exec = require('cordova/exec'), // Defaults exports._defaults = { // name: 'unknown', + // duplex: 'none', // landscape: false, // monochrome: false, - // border: true, - // copies: 1, - ui: { - hideNumberOfCopies: false, - hidePaperFormat: false, - bounds: [40, 30, 0, 0] - }, + // copies: 1, + // maxPages: 10, - // paper: { - // unit: 'cm', - // height: 0, - // width: 0, - // length: 0 + // border: true, + // maxHeight: '10cm', + // maxWidth: '10cm', + + // font: { + // name: 'Helvetica', + // align: 'left', + // italic: false, + // bold: false, + // color: '#FF0000' // }, - // layout: { - // unit: 'cm', - // maxHeight: 0, - // maxWidth: 0, - // padding: { top: 0, left: 0, right: 0, bottom: 0 } + // margin: { + // top: 0, + // left: 0, + // bottom: 0, + // right: 0 + // }, + + // ui: { + // hideNumberOfCopies: false, + // hidePaperFormat: false, + // bounds: [40, 30, 0, 0] + // }, + + // paper: { + // height: 0, + // width: 0, + // length: 0, + // name: 'A4' // }, // header: { - // unit: 'cm', - // height: 1, + // height: '1cm', // labels: [{ // text: 'Awesome Printer Plug-in', - // style: { + // font: { // align: 'center', // italic: true, // color: '#FF0000' // } // },{ // showPageIndex: true, - // style: { + // font: { // align: 'right', // bold: true // } @@ -73,17 +86,13 @@ exports._defaults = { // }, // footer: { - // unit: 'mm', - // height: 3, + // height: '3mm', // label: { // text: 'Copyright (c) 2013-2019 Sebastián Katzer', - // style: { size: 9 }, - // position: { - // unit: 'mm', - // top: 1.5, - // right: 5 - // } + // font: { size: 9 }, + // top: '1.5mm', + // right: '5mm' // } // } };