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" />
<source-file src="src/ios/APPPrinterRenderer.m" />
<header-file src="src/ios/APPPrinterStyle.h" />
<source-file src="src/ios/APPPrinterStyle.m" />
<header-file src="src/ios/APPPrinterFont.h" />
<source-file src="src/ios/APPPrinterFont.m" />
<header-file src="src/ios/APPPrinterUnit.h" />
<source-file src="src/ios/APPPrinterUnit.m" />

View File

@ -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)

View File

@ -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<NSAttributedStringKey, id> *)attributes;

View File

@ -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"];

View File

@ -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

View File

@ -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

View File

@ -25,9 +25,8 @@
- (UIPrintPaper*) bestPaperFromArray:(NSArray<UIPrintPaper *> *)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

View File

@ -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;
}

View File

@ -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);

View File

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

View File

@ -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

View File

@ -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'
// }
// }
};