forked from PSPDFKit/react-native
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRNConfigurationHelper.swift
More file actions
177 lines (144 loc) · 6.54 KB
/
RNConfigurationHelper.swift
File metadata and controls
177 lines (144 loc) · 6.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
//
// Copyright © 2018-2023 PSPDFKit GmbH. All rights reserved.
//
// THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY INTERNATIONAL COPYRIGHT LAW
// AND MAY NOT BE RESOLD OR REDISTRIBUTED. USAGE IS BOUND TO THE PSPDFKIT LICENSE AGREEMENT.
// UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES.
// This notice may not be removed from this file.
//
import Foundation
import PSPDFKit
@objc public class Customization: NSObject {
@objc public static func changeLineStyle() {
let styleManager = PSPDFKit.SDK.shared.styleManager
let key = Annotation.ToolVariantID(tool: .line, variant: .distanceMeasurement)
styleManager.setLastUsedValue([5,3,1,3] as NSArray, forProperty: #keyPath(LineAnnotation.__dashArray), forKey: key)
styleManager.setLastUsedValue(AbstractLineAnnotation.EndType.openArrow.rawValue, forProperty: #keyPath(LineAnnotation.lineEnd1), forKey: key)
styleManager.setLastUsedValue(AbstractLineAnnotation.EndType.openArrow.rawValue, forProperty: #keyPath(LineAnnotation.lineEnd2), forKey: key)
}
}
struct RNConfigurationHelper {
var configuration: [String: Any]
public init(_ configurationDictionary: [String : Any]) {
self.configuration = configurationDictionary
}
public func parseConfiguration() -> PDFNewPageConfiguration {
let configBuilder = NewPageConfigurationBuilder()
configBuilder.pageSize = parseSize()
configBuilder.pageRotation = parseRotation()
let pageTemplate = PageTemplate(pageType: .tiledPatternPage, identifier: parsePageTemplate())
return PDFNewPageConfiguration(pageTemplate: pageTemplate, builderBlock: { builder in
builder.pageSize = self.parseSize()
builder.pageRotation = self.parseRotation()
builder.pageMargins = self.parseMargins()
builder.backgroundColor = self.parseBackgroundColor()
})
}
public func newPageFromImage(_ configuration: [String: Any]) -> PDFNewPageConfiguration? {
guard
let imageUri = configuration["imageUri"] as? String,
let imagePath = URL(string: imageUri),
let image = UIImage(contentsOfFile: imagePath.path) else {
return nil
}
let pageTemplate = PageTemplate(pageType: .emptyPage, identifier: nil)
return PDFNewPageConfiguration(pageTemplate: pageTemplate) { builder in
builder.item = ProcessorItem(image: image, jpegCompressionQuality: 0.7, builderBlock: nil)
builder.pageSize = self.parseSize()
builder.pageRotation = self.parseRotation()
builder.pageMargins = self.parseMargins()
builder.backgroundColor = self.parseBackgroundColor()
}
}
public func newPageFromDocument(_ configuration: [String: Any]) -> PDFNewPageConfiguration? {
guard let documentUri = configuration["documentPath"] as? String,
let pageIndex = configuration["pageIndex"] as? UInt else {
return nil
}
let documentPath = URL(fileURLWithPath: documentUri)
let document = Document(url: documentPath, loadCheckpointIfAvailable: false)
let validRange = 0...document.pageCount-1
if !(validRange ~= pageIndex) {
return nil
}
let pageTemplate = PageTemplate(document: document, sourcePageIndex: pageIndex)
return PDFNewPageConfiguration(pageTemplate: pageTemplate) { builder in
builder.pageSize = self.parseSize()
builder.pageRotation = self.parseRotation()
builder.pageMargins = self.parseMargins()
builder.backgroundColor = self.parseBackgroundColor()
}
}
private func parsePageTemplate() -> PageTemplate.Identifier {
let templates: [String: PageTemplate.Identifier] = [
"blank": .blank,
"dot5mm": .dot5mm,
"grid5mm": .grid5mm,
"lines5mm": .lines5mm,
"lines7mm": .lines7mm
]
guard let templateID = configuration["template"] as? String else {
return .blank
}
return templates[templateID] ?? .blank
}
private func parseSize() -> CGSize {
let defaultSize = CGSize(width: 595, height: 842)
if let width = configuration["width"], let height = configuration["height"] {
return CGSize(
width: width as? CGFloat ?? defaultSize.width,
height: height as? CGFloat ?? defaultSize.height
)
}
guard let size = configuration["pageSize"] as? [String: Any] else {
return defaultSize
}
return CGSize(
width: size["width"] as? CGFloat ?? defaultSize.width,
height:size["height"] as? CGFloat ?? defaultSize.height
)
}
private func parseRotation() -> Rotation {
var rotationAngle = 0
let availableAngles: [Int] = [0, 90, 180, 270]
guard let rotation = configuration["rotation"] as? Int else {
return Rotation(rawValue: 0)!
}
if availableAngles.contains(rotation) {
rotationAngle = rotation
}
return Rotation(rawValue: UInt(rotationAngle))!
}
private func parseMargins() -> UIEdgeInsets {
let defaultMargins = UIEdgeInsets(top: 30, left: 15, bottom: 30, right: 15)
guard let margins = configuration["pageMargins"] as? [String: Any] else {
return defaultMargins
}
return UIEdgeInsets(
top: margins["top"] as? CGFloat ?? defaultMargins.top,
left: margins["left"] as? CGFloat ?? defaultMargins.left,
bottom: margins["bottom"] as? CGFloat ?? defaultMargins.bottom,
right: margins["right"] as? CGFloat ?? defaultMargins.right
)
}
// Can be in format of hex value (#ff0000), rgb(0,0,0), rgba(0,0,0,0) or color name (eg. lightgray, red...)
private func parseBackgroundColor() -> UIColor {
guard let color = configuration["backgroundColor"] else {
return .white
}
if let colorValue = color as? String {
if colorValue.contains("#"), let uiColor = UIColor(hexString: colorValue) {
return uiColor
}
if colorValue.contains("rgb") {
if let uiColor = UIColor.rgb(colorValue) {
return uiColor
}
}
if let uiColor = UIColor.colorFromName(colorValue) {
return uiColor
}
}
return .white
}
}