Change options structure

This commit is contained in:
Sebastián Katzer 2019-02-07 16:42:11 +01:00
parent 13e6d4d941
commit f29b519b90
12 changed files with 147 additions and 114 deletions

View File

@ -76,8 +76,8 @@
<header-file src="src/ios/APPPrinterRenderer.h" /> <header-file src="src/ios/APPPrinterRenderer.h" />
<source-file src="src/ios/APPPrinterRenderer.m" /> <source-file src="src/ios/APPPrinterRenderer.m" />
<header-file src="src/ios/APPPrinterStyle.h" /> <header-file src="src/ios/APPPrinterFont.h" />
<source-file src="src/ios/APPPrinterStyle.m" /> <source-file src="src/ios/APPPrinterFont.m" />
<header-file src="src/ios/APPPrinterUnit.h" /> <header-file src="src/ios/APPPrinterUnit.h" />
<source-file src="src/ios/APPPrinterUnit.m" /> <source-file src="src/ios/APPPrinterUnit.m" />

View File

@ -301,7 +301,7 @@ class PrintManager {
boolean jsEnabled = settings.optBoolean("javascript", false); boolean jsEnabled = settings.optBoolean("javascript", false);
WebView view = new WebView(context); WebView view = new WebView(context);
WebSettings spec = view.getSettings(); WebSettings spec = view.getSettings();
JSONObject style = settings.optJSONObject("style"); JSONObject font = settings.optJSONObject("font");
spec.setDatabaseEnabled(true); spec.setDatabaseEnabled(true);
spec.setGeolocationEnabled(true); spec.setGeolocationEnabled(true);
@ -309,9 +309,9 @@ class PrintManager {
spec.setUseWideViewPort(true); spec.setUseWideViewPort(true);
spec.setJavaScriptEnabled(jsEnabled); 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) if (SDK_INT >= 21)

View File

@ -20,7 +20,7 @@
under the License. under the License.
*/ */
@interface APPPrinterStyle : NSObject @interface APPPrinterFont : NSObject
- (instancetype) initWithDictionary:(NSDictionary *)spec; - (instancetype) initWithDictionary:(NSDictionary *)spec;
@ -28,7 +28,7 @@
- (UIColor *)color; - (UIColor *)color;
- (NSTextAlignment) textAlignment; - (NSTextAlignment) alignment;
- (NSDictionary<NSAttributedStringKey, id> *)attributes; - (NSDictionary<NSAttributedStringKey, id> *)attributes;

View File

@ -20,15 +20,15 @@
under the License. under the License.
*/ */
#include "APPPrinterStyle.h" #include "APPPrinterFont.h"
@interface APPPrinterStyle () @interface APPPrinterFont ()
@property (nonatomic, retain) NSDictionary *settings; @property (nonatomic, retain) NSDictionary *settings;
@end @end
@implementation APPPrinterStyle @implementation APPPrinterFont
#pragma mark - #pragma mark -
#pragma mark Public #pragma mark Public
@ -47,7 +47,7 @@
NSMutableParagraphStyle *style = [[NSParagraphStyle defaultParagraphStyle] NSMutableParagraphStyle *style = [[NSParagraphStyle defaultParagraphStyle]
mutableCopy]; mutableCopy];
style.alignment = self.textAlignment; style.alignment = self.alignment;
return @{ NSFontAttributeName: self.font, return @{ NSFontAttributeName: self.font,
NSParagraphStyleAttributeName: style, NSParagraphStyleAttributeName: style,
@ -63,7 +63,7 @@
size = UIFont.smallSystemFontSize; size = UIFont.smallSystemFontSize;
} }
UIFont *font = [UIFont fontWithName:_settings[@"font"] size:size]; UIFont *font = [UIFont fontWithName:_settings[@"name"] size:size];
if (!font) if (!font)
{ {
@ -115,7 +115,7 @@
return color; return color;
} }
- (NSTextAlignment) textAlignment - (NSTextAlignment) alignment
{ {
NSString *align = _settings[@"align"]; NSString *align = _settings[@"align"];

View File

@ -21,13 +21,12 @@
@interface APPPrinterLayout : NSObject @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; @property(nonatomic) UIEdgeInsets contentInsets;
// The maximum height of the content area.
@property(nonatomic) CGFloat maximumContentHeight; @property(nonatomic) CGFloat maximumContentHeight;
// The maximum width of the content area.
@property(nonatomic) CGFloat maximumContentWidth; @property(nonatomic) CGFloat maximumContentWidth;
@end @end

View File

@ -20,7 +20,7 @@
*/ */
#include "APPPrinterLayout.h" #include "APPPrinterLayout.h"
#include "APPPrinterStyle.h" #include "APPPrinterFont.h"
#include "APPPrinterUnit.h" #include "APPPrinterUnit.h"
@implementation APPPrinterLayout @implementation APPPrinterLayout
@ -34,19 +34,16 @@
if (!spec) return self; if (!spec) return self;
NSDictionary* insests = spec[@"padding"]; NSDictionary *margin = spec[@"margin"];
double maxWidth = [spec[@"maxWidth"] doubleValue];
double maxHeight = [spec[@"maxHeight"] doubleValue];
double dots = [APPPrinterUnit convert:spec[@"unit"]];
_contentInsets = UIEdgeInsetsMake(dots * [insests[@"top"] doubleValue], _contentInsets = UIEdgeInsetsMake([APPPrinterUnit convert:margin[@"top"]],
dots * [insests[@"left"] doubleValue], [APPPrinterUnit convert:margin[@"left"]],
dots * [insests[@"bottom"] doubleValue], [APPPrinterUnit convert:margin[@"bottom"]],
dots * [insests[@"right"] doubleValue]); [APPPrinterUnit convert:margin[@"right"]]);
_maximumContentWidth = dots * maxWidth; _maximumContentWidth = [APPPrinterUnit convert:spec[@"maxWidth"]];
_maximumContentHeight = dots * maxHeight; _maximumContentHeight = [APPPrinterUnit convert:spec[@"maxHeight"]];
return self; return self;
} }
@ -55,17 +52,16 @@
#pragma mark Public #pragma mark Public
+ (UIPrintFormatter *) configureFormatter:(UIPrintFormatter *)formatter + (UIPrintFormatter *) configureFormatter:(UIPrintFormatter *)formatter
withLayoutFromDictionary:(NSDictionary *)layoutSpec withSettings:(NSDictionary *)settings
withStyleFromDictionary:(nullable NSDictionary *)styleSpec
{ {
id layout = [[self alloc] initWithDictionary:layoutSpec]; id layout = [[self alloc] initWithDictionary:settings];
[layout configureFormatter:formatter]; [layout configureFormatter:formatter];
if (styleSpec && ![formatter isKindOfClass:UIMarkupTextPrintFormatter.class]) if (settings && ![formatter isKindOfClass:UIMarkupTextPrintFormatter.class])
{ {
[layout configureTextFormatter:(UISimpleTextPrintFormatter *)formatter [layout configureTextFormatter:(UISimpleTextPrintFormatter *)formatter
withStyleFromDictionary:styleSpec]; withSettings:settings];
} }
return formatter; return formatter;
@ -87,14 +83,14 @@
} }
- (void) configureTextFormatter:(UISimpleTextPrintFormatter *)formatter - (void) configureTextFormatter:(UISimpleTextPrintFormatter *)formatter
withStyleFromDictionary:(NSDictionary *)spec withSettings:(NSDictionary *)settings
{ {
APPPrinterStyle *style = [[APPPrinterStyle alloc] APPPrinterFont *font = [[APPPrinterFont alloc]
initWithDictionary:spec]; initWithDictionary:settings[@"font"]];
formatter.font = style.font; formatter.font = font.font;
formatter.color = style.color; formatter.color = font.color;
formatter.textAlignment = style.textAlignment; formatter.textAlignment = font.alignment;
} }
@end @end

View File

@ -25,9 +25,8 @@
- (UIPrintPaper*) bestPaperFromArray:(NSArray<UIPrintPaper *> *)list; - (UIPrintPaper*) bestPaperFromArray:(NSArray<UIPrintPaper *> *)list;
// The length to use when cutting the page if using roll printers.
@property(nonatomic) CGFloat length; @property(nonatomic) CGFloat length;
// The size of the printed page.
@property(nonatomic) CGSize size; @property(nonatomic) CGSize size;
@end @end

View File

@ -31,13 +31,10 @@
{ {
self = [self init]; self = [self init];
double dots = [APPPrinterUnit convert:spec[@"unit"]]; _size = CGSizeMake([APPPrinterUnit convert:spec[@"width"]],
double length = [spec[@"length"] doubleValue]; [APPPrinterUnit convert:spec[@"height"]]);
double height = [spec[@"height"] doubleValue];
double width = [spec[@"width"] doubleValue];
_size = CGSizeMake(dots * width, dots * height); _length = [APPPrinterUnit convert:spec[@"length"]];
_length = dots * length;
return self; return self;
} }

View File

@ -21,7 +21,7 @@
#include "APPPrinterLayout.h" #include "APPPrinterLayout.h"
#include "APPPrinterRenderer.h" #include "APPPrinterRenderer.h"
#include "APPPrinterStyle.h" #include "APPPrinterFont.h"
#include "APPPrinterUnit.h" #include "APPPrinterUnit.h"
@interface APPPrinterRenderer () @interface APPPrinterRenderer ()
@ -39,26 +39,22 @@
{ {
NSDictionary* header = spec[@"header"]; NSDictionary* header = spec[@"header"];
NSDictionary* footer = spec[@"footer"]; NSDictionary* footer = spec[@"footer"];
double dots = 0;
self = [self init]; self = [self init];
[APPPrinterLayout configureFormatter:formatter [APPPrinterLayout configureFormatter:formatter
withLayoutFromDictionary:spec[@"layout"] withSettings:spec];
withStyleFromDictionary:spec[@"style"]];
[self addPrintFormatter:formatter startingAtPageAtIndex:0]; [self addPrintFormatter:formatter startingAtPageAtIndex:0];
if (header) if (header)
{ {
dots = [APPPrinterUnit convert:header[@"unit"]]; self.headerHeight = [APPPrinterUnit convert:header[@"height"]];
self.headerHeight = dots * [header[@"height"] floatValue];
} }
if (footer) if (footer)
{ {
dots = [APPPrinterUnit convert:footer[@"unit"]]; self.footerHeight = [APPPrinterUnit convert:footer[@"height"]];
self.footerHeight = dots * [footer[@"height"] floatValue];
} }
_settings = spec; _settings = spec;
@ -139,15 +135,14 @@
if ([self strIsNullOrEmpty:label]) if ([self strIsNullOrEmpty:label])
return; return;
NSDictionary *position = spec[@"position"]; APPPrinterFont *font = [[APPPrinterFont alloc]
NSDictionary *style = spec[@"style"]; initWithDictionary:spec[@"font"]];
NSDictionary *attributes = [[APPPrinterStyle alloc] NSDictionary *attributes = font.attributes;
initWithDictionary:style].attributes;
if (position) if (spec[@"top"] || spec[@"left"] || spec[@"right"] || spec[@"bottom"])
{ {
[label drawAtPoint:[self pointFromPositionAsDictionary:position [label drawAtPoint:[self pointFromPositionAsDictionary:spec
forLabel:label forLabel:label
withAttributes:attributes withAttributes:attributes
inRect:rect] inRect:rect]
@ -164,11 +159,10 @@
withAttributes:(NSDictionary *)attributes withAttributes:(NSDictionary *)attributes
inRect:(CGRect)rect inRect:(CGRect)rect
{ {
double dots = [APPPrinterUnit convert:spec[@"unit"]]; id top = spec[@"top"];
id top = spec[@"top"]; id left = spec[@"left"];
id left = spec[@"left"]; id right = spec[@"right"];
id right = spec[@"right"]; id bottom = spec[@"bottom"];
id bottom = spec[@"bottom"];
double x = rect.origin.x, y = rect.origin.y; double x = rect.origin.x, y = rect.origin.y;
CGSize size; CGSize size;
@ -180,20 +174,20 @@
if (top) if (top)
{ {
y = rect.origin.y + dots * [top doubleValue]; y = rect.origin.y + [APPPrinterUnit convert:top];
} }
else if (bottom) else if (bottom)
{ {
y = rect.origin.y - size.height - dots * [bottom doubleValue]; y = rect.origin.y - size.height - [APPPrinterUnit convert:bottom];
} }
if (left) if (left)
{ {
x = rect.origin.x + dots * [left doubleValue]; x = rect.origin.x + [APPPrinterUnit convert:left];
} }
else if (right) 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); return CGPointMake(x, y);

View File

@ -21,6 +21,6 @@
@interface APPPrinterUnit : NSObject @interface APPPrinterUnit : NSObject
+ (double) convert:(nullable NSString *)unit; + (double) convert:(nullable id)unit;
@end @end

View File

@ -23,26 +23,65 @@
@implementation APPPrinterUnit @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]]) double value = 0;
return 1.0;
if ([unit isEqualToString:@"in"]) @try
return 72.0; {
if (!unit || [unit isEqual:[NSNull null]])
if ([unit isEqualToString:@"mm"]) {
return 72.0 / 25.4; value = 0;
}
if ([unit isEqualToString:@"cm"]) else if ([unit isKindOfClass:NSNumber.class])
return 72.0 / 2.54; {
value = [unit longValue];
if (![unit isEqualToString:@"pp"]) }
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); 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 @end

View File

@ -26,46 +26,59 @@ var exec = require('cordova/exec'),
// Defaults // Defaults
exports._defaults = { exports._defaults = {
// name: 'unknown', // name: 'unknown',
// duplex: 'none', // duplex: 'none',
// landscape: false, // landscape: false,
// monochrome: false, // monochrome: false,
// border: true,
// copies: 1,
ui: { // copies: 1,
hideNumberOfCopies: false, // maxPages: 10,
hidePaperFormat: false,
bounds: [40, 30, 0, 0]
},
// paper: { // border: true,
// unit: 'cm', // maxHeight: '10cm',
// height: 0, // maxWidth: '10cm',
// width: 0,
// length: 0 // font: {
// name: 'Helvetica',
// align: 'left',
// italic: false,
// bold: false,
// color: '#FF0000'
// }, // },
// layout: { // margin: {
// unit: 'cm', // top: 0,
// maxHeight: 0, // left: 0,
// maxWidth: 0, // bottom: 0,
// padding: { top: 0, left: 0, right: 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: { // header: {
// unit: 'cm', // height: '1cm',
// height: 1,
// labels: [{ // labels: [{
// text: 'Awesome Printer Plug-in', // text: 'Awesome Printer Plug-in',
// style: { // font: {
// align: 'center', // align: 'center',
// italic: true, // italic: true,
// color: '#FF0000' // color: '#FF0000'
// } // }
// },{ // },{
// showPageIndex: true, // showPageIndex: true,
// style: { // font: {
// align: 'right', // align: 'right',
// bold: true // bold: true
// } // }
@ -73,17 +86,13 @@ exports._defaults = {
// }, // },
// footer: { // footer: {
// unit: 'mm', // height: '3mm',
// height: 3,
// label: { // label: {
// text: 'Copyright (c) 2013-2019 Sebastián Katzer', // text: 'Copyright (c) 2013-2019 Sebastián Katzer',
// style: { size: 9 }, // font: { size: 9 },
// position: { // top: '1.5mm',
// unit: 'mm', // right: '5mm'
// top: 1.5,
// right: 5
// }
// } // }
// } // }
}; };