Add APPPrinterUnit to avoid code duplication

This commit is contained in:
Sebastián Katzer 2019-01-21 19:17:30 +01:00
parent 578342516a
commit ee88021da0
10 changed files with 89 additions and 75 deletions

View File

@ -78,6 +78,9 @@
<header-file src="src/ios/APPPrinterRenderer.h" />
<source-file src="src/ios/APPPrinterRenderer.m" />
<header-file src="src/ios/APPPrinterUnit.h" />
<source-file src="src/ios/APPPrinterUnit.m" />
<header-file src="src/ios/UIPrintInteractionController+APPPrinter.h" />
<source-file src="src/ios/UIPrintInteractionController+APPPrinter.m" />

View File

@ -19,9 +19,9 @@
under the License.
*/
#import "APPPrinter.h"
#import "APPPrinterItem.h"
#import "APPPrinterPaper.h"
#include "APPPrinter.h"
#include "APPPrinterItem.h"
#include "APPPrinterPaper.h"
#include "APPPrinterRenderer.h"
#include "UIPrintInteractionController+APPPrinter.h"

View File

@ -19,7 +19,7 @@
under the License.
*/
#import "APPPrinterInfo.h"
#include "APPPrinterInfo.h"
@implementation APPPrinterInfo

View File

@ -19,7 +19,7 @@
under the License.
*/
#import "APPPrinterItem.h"
#include "APPPrinterItem.h"
@implementation APPPrinterItem

View File

@ -19,7 +19,8 @@
under the License.
*/
#import "APPPrinterLayout.h"
#include "APPPrinterLayout.h"
#include "APPPrinterUnit.h"
@implementation APPPrinterLayout
@ -33,7 +34,7 @@
NSDictionary* insests = spec[@"padding"];
double maxWidth = [spec[@"maxWidth"] doubleValue];
double maxHeight = [spec[@"maxHeight"] doubleValue];
double dots = [self pointsPerUnit:spec[@"unit"]];
double dots = [APPPrinterUnit convert:spec[@"unit"]];
_contentInsets = UIEdgeInsetsMake(dots * [insests[@"top"] doubleValue],
dots * [insests[@"left"] doubleValue],
@ -76,26 +77,4 @@
formatter.perPageContentInsets = _contentInsets;
}
#pragma mark -
#pragma mark Private
- (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

View File

@ -19,7 +19,8 @@
under the License.
*/
#import "APPPrinterPaper.h"
#include "APPPrinterPaper.h"
#include "APPPrinterUnit.h"
@implementation APPPrinterPaper
@ -30,7 +31,7 @@
{
self = [self init];
double dots = [self pointsPerUnit:spec[@"unit"]];
double dots = [APPPrinterUnit convert:spec[@"unit"]];
double length = [spec[@"length"] doubleValue];
double height = [spec[@"height"] doubleValue];
double width = [spec[@"width"] doubleValue];
@ -57,26 +58,4 @@
return paper;
}
#pragma mark -
#pragma mark Private
- (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

View File

@ -21,6 +21,7 @@
#include "APPPrinterLayout.h"
#include "APPPrinterRenderer.h"
#include "APPPrinterUnit.h"
@implementation APPPrinterRenderer
@ -46,13 +47,13 @@
if (header)
{
dots = [self pointsPerUnit:header[@"unit"]];
dots = [APPPrinterUnit convert:header[@"unit"]];
self.headerHeight = dots * [header[@"height"] doubleValue];
}
if (footer)
{
dots = [self pointsPerUnit:header[@"unit"]];
dots = [APPPrinterUnit convert:header[@"unit"]];
self.footerHeight = dots * [footer[@"height"] doubleValue];
}
@ -94,23 +95,4 @@
[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

26
src/ios/APPPrinterUnit.h Normal file
View File

@ -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 APPPrinterUnit : NSObject
+ (double) convert:(NSString *)unit;
@end

45
src/ios/APPPrinterUnit.m Normal file
View File

@ -0,0 +1,45 @@
/*
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 "APPPrinterUnit.h"
@implementation APPPrinterUnit
+ (double) convert:(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

View File

@ -22,7 +22,7 @@
#include "APPPrinterInfo.h"
#include "UIPrintInteractionController+APPPrinter.h"
#import <objc/runtime.h>
#include <objc/runtime.h>
@implementation UIPrintInteractionController (APPPrinter)