@quran.ws/qvp-react-native 0.7.0 → 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +86 -32
- package/android/build.gradle +16 -8
- package/android/src/main/java/ws/quran/qvp/rn/Codes.kt +34 -0
- package/android/src/main/java/ws/quran/qvp/rn/Marshal.kt +78 -30
- package/android/src/main/java/ws/quran/qvp/rn/QvpModule.kt +105 -50
- package/android/src/main/java/ws/quran/qvp/rn/QvpPageViewManager.kt +1 -0
- package/android/src/main/java/ws/quran/qvp/rn/QvpRnPageView.kt +55 -14
- package/android/src/main/java/ws/quran/qvp/rn/QvpRnWords.kt +26 -0
- package/ios/QvpModule.m +167 -0
- package/ios/QvpModule.swift +508 -0
- package/ios/QvpRnBytes.swift +66 -0
- package/ios/QvpRnCodes.swift +33 -0
- package/ios/QvpRnMarshal.swift +360 -0
- package/ios/QvpRnPageView.swift +467 -0
- package/ios/QvpRnPassageView.swift +160 -0
- package/ios/vendor/QvpEngine.json +4 -0
- package/ios/vendor/QvpKit/QvpDress.swift +390 -0
- package/ios/vendor/QvpKit/QvpEngine.swift +55 -0
- package/ios/vendor/QvpKit/QvpPage.swift +469 -0
- package/ios/vendor/QvpKit/QvpPageCache.swift +176 -0
- package/ios/vendor/QvpKit/QvpPageCanvas.swift +1031 -0
- package/ios/vendor/QvpKit/QvpPageView.swift +711 -0
- package/ios/vendor/QvpKit/QvpPassage.swift +240 -0
- package/ios/vendor/QvpKit/QvpViewPolicy.swift +26 -0
- package/ios/vendor/QvpKit/QvpZoom.swift +200 -0
- package/ios/vendor/QvpKit/QvpZoomSpring.swift +22 -0
- package/ios/vendor/QvpKit/Types.swift +440 -0
- package/ios/vendor-qvpkit.sh +36 -0
- package/package.json +10 -3
- package/qvp-react-native.podspec +97 -0
- package/src/codes.ts +33 -0
- package/src/index.tsx +171 -55
|
@@ -0,0 +1,390 @@
|
|
|
1
|
+
// Dress: another mushaf's ornaments on a page, over the C ABI in qvp.h. The engine places
|
|
2
|
+
// every medallion, band and border and hands back a display list. This file only marshals
|
|
3
|
+
// it. Mirrors `QvpOrnaments` and `QvpPage.dress` in web/qvp.js. See docs/API.md, "Dress".
|
|
4
|
+
//
|
|
5
|
+
// The binding calls qvp_ornaments_load, qvp_ornaments_free, qvp_ornament_count,
|
|
6
|
+
// qvp_ornament_info, qvp_ornament_find, qvp_ornament_part, qvp_dress, qvp_undress,
|
|
7
|
+
// qvp_dress_info, qvp_dress_geometry, qvp_dress_clamp and qvp_dress_view.
|
|
8
|
+
import Foundation
|
|
9
|
+
import CoreGraphics
|
|
10
|
+
import QvpFFI
|
|
11
|
+
|
|
12
|
+
/// What an ornament replaces (QVP_ORNAMENT_*). `name` is the web wrapper's string for it.
|
|
13
|
+
public enum OrnamentKind: Int, CaseIterable, Hashable {
|
|
14
|
+
case ayahMark = 0, surahHeader, pageFrame
|
|
15
|
+
public var name: String { ["ayah_mark", "surah_header", "page_frame"][rawValue] }
|
|
16
|
+
/// The QVP_LINE_ART_* bit of this kind.
|
|
17
|
+
var lineArtBit: UInt32 { 1 << UInt32(truncatingIfNeeded: rawValue) }
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/// Where an ornament's artwork came from (QVP_LINEAGE_*).
|
|
21
|
+
public enum OrnamentLineage: Int, Hashable {
|
|
22
|
+
case scan = 0, font
|
|
23
|
+
public var name: String { self == .scan ? "scan" : "font" }
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/// What an ornament may be published under. Read it before you publish a page wearing it.
|
|
27
|
+
public struct OrnamentLicence: Equatable {
|
|
28
|
+
public let id: String, status: String, redistributable: Bool, attribution: String
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/// One printed colour of an ornament. A part with alpha 0 is a window the design leaves open.
|
|
32
|
+
public struct OrnamentPart: Equatable {
|
|
33
|
+
public let index: Int, color: UInt32, stroke: Bool, name: String
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/// One ornament of a set: a medallion, a band or a border.
|
|
37
|
+
public struct Ornament: Equatable {
|
|
38
|
+
public let index: Int, kind: OrnamentKind, lineage: OrnamentLineage
|
|
39
|
+
public let style: String, riwayah: String, hasSlices: Bool
|
|
40
|
+
public let licence: OrnamentLicence
|
|
41
|
+
public let parts: [OrnamentPart]
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/// Every style in a set, with the ornament it has of each kind, or nil.
|
|
45
|
+
public struct OrnamentStyle: Equatable {
|
|
46
|
+
public let style: String, riwayah: String, lineage: OrnamentLineage
|
|
47
|
+
public var ayahMark: Ornament?, surahHeader: Ornament?, pageFrame: Ornament?
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/// The ornaments of other mushafs and fonts, from `ornaments.qvo`. None of these outlines is
|
|
51
|
+
/// part of a QVP page. Each ornament says what it may be redistributed under.
|
|
52
|
+
public final class QvpOrnaments {
|
|
53
|
+
private var h: OpaquePointer?
|
|
54
|
+
/// How many ornaments the set holds.
|
|
55
|
+
public let count: Int
|
|
56
|
+
/// Every ornament, in the set's order. `list[i].index == i`.
|
|
57
|
+
public let list: [Ornament]
|
|
58
|
+
|
|
59
|
+
/// Load an ornament set. The engine copies the bytes.
|
|
60
|
+
public init(bytes: Data) throws {
|
|
61
|
+
h = bytes.withUnsafeBytes { qvp_ornaments_load($0.bindMemory(to: UInt8.self).baseAddress, $0.count) }
|
|
62
|
+
guard let h else { throw QvpError.badOrnaments }
|
|
63
|
+
count = Int(qvp_ornament_count(h))
|
|
64
|
+
var out: [Ornament] = []
|
|
65
|
+
for i in 0..<count {
|
|
66
|
+
var o = QvpOrnament()
|
|
67
|
+
guard qvp_ornament_info(h, UInt32(truncatingIfNeeded: i), &o) != 0 else { continue }
|
|
68
|
+
// the strings point into the set and live as long as it: copy them now
|
|
69
|
+
let parts: [OrnamentPart] = (0..<Int(o.n_parts)).compactMap { k in
|
|
70
|
+
var q = QvpOrnamentPart()
|
|
71
|
+
guard qvp_ornament_part(h, UInt32(truncatingIfNeeded: i), UInt32(truncatingIfNeeded: k), &q) != 0 else { return nil }
|
|
72
|
+
return OrnamentPart(index: k, color: q.color, stroke: q.stroke != 0, name: q.name.string)
|
|
73
|
+
}
|
|
74
|
+
out.append(Ornament(index: i, kind: OrnamentKind(rawValue: Int(o.kind)) ?? .ayahMark, lineage: OrnamentLineage(rawValue: Int(o.lineage)) ?? .scan,
|
|
75
|
+
style: o.style.string, riwayah: o.riwayah.string, hasSlices: o.has_slices != 0,
|
|
76
|
+
licence: OrnamentLicence(id: o.license_id.string, status: o.license_status.string, redistributable: o.redistributable != 0, attribution: o.attribution.string),
|
|
77
|
+
parts: parts))
|
|
78
|
+
}
|
|
79
|
+
list = out
|
|
80
|
+
}
|
|
81
|
+
/// Load an ornament set from a file.
|
|
82
|
+
public convenience init(path: String) throws { try self.init(bytes: try Data(contentsOf: URL(fileURLWithPath: path))) }
|
|
83
|
+
deinit { close() }
|
|
84
|
+
/// Free the native set. A second call does nothing. A page dressed from it keeps its dress.
|
|
85
|
+
public func close() { if let s = h { qvp_ornaments_free(s); h = nil } }
|
|
86
|
+
/// False once the set is closed.
|
|
87
|
+
public var isOpen: Bool { h != nil }
|
|
88
|
+
/// The engine handle. Internal: the wrapper marshals through it, no app ever sees it.
|
|
89
|
+
/// A call on a closed set stops the program with the fatal error `QvpOrnaments is closed`.
|
|
90
|
+
var p: OpaquePointer { guard let h else { fatalError("QvpOrnaments is closed") }; return h }
|
|
91
|
+
|
|
92
|
+
/// The ornament of `kind` in `style`, or nil.
|
|
93
|
+
public func find(_ kind: OrnamentKind, _ style: String) -> Ornament? {
|
|
94
|
+
let s = p
|
|
95
|
+
let i = withBytes(style) { sp, sn in qvp_ornament_find(s, UInt32(truncatingIfNeeded: kind.rawValue), sp, sn) }
|
|
96
|
+
return i < 0 ? nil : list.first { $0.index == Int(i) }
|
|
97
|
+
}
|
|
98
|
+
/// The ornaments of one kind.
|
|
99
|
+
public func ofKind(_ kind: OrnamentKind) -> [Ornament] { list.filter { $0.kind == kind } }
|
|
100
|
+
/// Every style in the set, in the set's order, with the ornament it has of each kind.
|
|
101
|
+
public func styles() -> [OrnamentStyle] {
|
|
102
|
+
var out: [OrnamentStyle] = []
|
|
103
|
+
for o in list {
|
|
104
|
+
var k = out.firstIndex { $0.style == o.style }
|
|
105
|
+
if k == nil { out.append(OrnamentStyle(style: o.style, riwayah: o.riwayah, lineage: o.lineage)); k = out.count - 1 }
|
|
106
|
+
switch o.kind {
|
|
107
|
+
case .ayahMark: out[k!].ayahMark = o
|
|
108
|
+
case .surahHeader: out[k!].surahHeader = o
|
|
109
|
+
case .pageFrame: out[k!].pageFrame = o
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
return out
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/// Which ornament a dress puts on one kind: an ornament of the set, its index, a style name
|
|
117
|
+
/// (the ornament of that kind in that style), or `.absent` for none.
|
|
118
|
+
public enum OrnamentChoice: Equatable, ExpressibleByStringLiteral, ExpressibleByIntegerLiteral {
|
|
119
|
+
case ornament(Ornament), index(Int), style(String), absent
|
|
120
|
+
public init(stringLiteral value: String) { self = .style(value) }
|
|
121
|
+
public init(integerLiteral value: Int) { self = .index(value) }
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/// A repaint of one part of one ornament. `color` is 0xRRGGBBAA.
|
|
125
|
+
public struct OrnamentColor: Equatable {
|
|
126
|
+
public var ornament: Int, part: Int, color: UInt32
|
|
127
|
+
public init(ornament: Int, part: Int, color: UInt32) { self.ornament = ornament; self.part = part; self.color = color }
|
|
128
|
+
public init(_ ornament: Ornament, part: Int, color: UInt32) { self.init(ornament: ornament.index, part: part, color: color) }
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/// What to dress a page in. Mirrors the web wrapper's dress options.
|
|
132
|
+
public struct DressSpec: Equatable {
|
|
133
|
+
/// A mushaf name: its medallion, band and border together. A kind set below overrides it.
|
|
134
|
+
public var style: String?
|
|
135
|
+
/// One ornament per kind. nil takes the kind from `style`; `.absent` leaves it out.
|
|
136
|
+
public var ayahMark: OrnamentChoice?, surahHeader: OrnamentChoice?, pageFrame: OrnamentChoice?
|
|
137
|
+
/// The air between the text and the border, and between a band and the border, in page units.
|
|
138
|
+
public var gap: Float, bandGap: Float
|
|
139
|
+
/// The medallion's size against the printed ring. 0 takes the design's own:
|
|
140
|
+
/// `QvpDefaults.AYAH_MARK_SCALE_SCAN` for a scanned mushaf, 1 for a font marker.
|
|
141
|
+
public var ayahMarkScale: Float
|
|
142
|
+
/// The border's size against the design's own. The pattern scales as a whole.
|
|
143
|
+
public var pageFrameScale: Float
|
|
144
|
+
/// The kinds drawn as line art, their constant-width strokes alone. `Set(OrnamentKind.allCases)` for all.
|
|
145
|
+
public var lineArt: Set<OrnamentKind>
|
|
146
|
+
/// Repaints. Every part left out keeps the design's own colour.
|
|
147
|
+
public var colors: [OrnamentColor]
|
|
148
|
+
public init(style: String? = nil, ayahMark: OrnamentChoice? = nil, surahHeader: OrnamentChoice? = nil, pageFrame: OrnamentChoice? = nil,
|
|
149
|
+
gap: Float = 5, bandGap: Float = 5, ayahMarkScale: Float = 0, pageFrameScale: Float = 1,
|
|
150
|
+
lineArt: Set<OrnamentKind> = [], colors: [OrnamentColor] = []) {
|
|
151
|
+
self.style = style; self.ayahMark = ayahMark; self.surahHeader = surahHeader; self.pageFrame = pageFrame
|
|
152
|
+
self.gap = gap; self.bandGap = bandGap; self.ayahMarkScale = ayahMarkScale; self.pageFrameScale = pageFrameScale
|
|
153
|
+
self.lineArt = lineArt; self.colors = colors
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/// The readout of a dressed page.
|
|
158
|
+
public struct DressInfo: Equatable {
|
|
159
|
+
/// The ornament dressed on each kind, or nil.
|
|
160
|
+
public let ayahMark: Int?, surahHeader: Int?, pageFrame: Int?
|
|
161
|
+
/// How many medallions and bands were placed, and how the border was built.
|
|
162
|
+
public let ayahMarks: Int, surahHeaders: Int, frameRepeats: Int, frameStretched: Bool
|
|
163
|
+
/// How many draws the display list holds.
|
|
164
|
+
public let draws: Int
|
|
165
|
+
/// Counts up on every rebuild: a new dress or a new layout. Cache the ornament raster on it.
|
|
166
|
+
public let revision: Int
|
|
167
|
+
/// x, y, w, h in page units. A border grows it past the page.
|
|
168
|
+
public let viewBox: [Float]
|
|
169
|
+
/// left, top, right, bottom in layout px: how far the border reaches past the laid-out
|
|
170
|
+
/// content. The layout's fit (`QvpLayout.fitScale`) already holds it, so a host that draws
|
|
171
|
+
/// at the fit shows the whole border.
|
|
172
|
+
public let overflow: [Float]
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/// One draw of the ornament display list, in page units. Paint it behind the page ink.
|
|
176
|
+
public struct OrnamentDraw {
|
|
177
|
+
/// The outline, in page units.
|
|
178
|
+
public let path: CGPath
|
|
179
|
+
/// The colour, 0xRRGGBBAA.
|
|
180
|
+
public let color: UInt32
|
|
181
|
+
/// The kind of ornament the draw belongs to.
|
|
182
|
+
public let kind: OrnamentKind
|
|
183
|
+
/// Fill with the even-odd rule.
|
|
184
|
+
public let evenOdd: Bool
|
|
185
|
+
/// Stroke with `strokeWidth` (page units), round caps and joins, instead of filling.
|
|
186
|
+
public let stroke: Bool
|
|
187
|
+
/// The width of a stroke, in page units.
|
|
188
|
+
public let strokeWidth: Float
|
|
189
|
+
/// The part of the ornament's design the draw paints.
|
|
190
|
+
public let part: Int
|
|
191
|
+
/// The index into `QvpPage.layoutPlacements()` the draw goes under, as a page draw does.
|
|
192
|
+
/// nil for the border, which is drawn in layout space with the identity placement.
|
|
193
|
+
public let placement: Int?
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
extension QvpPage {
|
|
197
|
+
/// Put other ornaments on the page: up to one per kind. Returns the readout, or nil when
|
|
198
|
+
/// the engine refused the spec (an index out of range, an ornament of another kind, or
|
|
199
|
+
/// nothing to dress). Replaces any previous dress. A closed set is a fatal error,
|
|
200
|
+
/// `QvpOrnaments is closed`.
|
|
201
|
+
@discardableResult
|
|
202
|
+
public func dress(_ set: QvpOrnaments, _ spec: DressSpec = DressSpec()) -> DressInfo? {
|
|
203
|
+
let ornaments = set.p
|
|
204
|
+
// Resolve every name before the C spec is filled.
|
|
205
|
+
func index(_ c: OrnamentChoice?, _ kind: OrnamentKind) -> UInt32 {
|
|
206
|
+
let choice = c ?? spec.style.map { OrnamentChoice.style($0) } ?? .absent
|
|
207
|
+
switch choice {
|
|
208
|
+
case .ornament(let o): return UInt32(truncatingIfNeeded: o.index)
|
|
209
|
+
case .index(let i): return i < 0 ? QVP_NONE : UInt32(truncatingIfNeeded: i)
|
|
210
|
+
case .style(let s): return set.find(kind, s).map { UInt32(truncatingIfNeeded: $0.index) } ?? QVP_NONE
|
|
211
|
+
case .absent: return QVP_NONE
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
let ayah = index(spec.ayahMark, .ayahMark), header = index(spec.surahHeader, .surahHeader), frame = index(spec.pageFrame, .pageFrame)
|
|
215
|
+
let lineArt = spec.lineArt.reduce(UInt32(0)) { $0 | $1.lineArtBit }
|
|
216
|
+
let triples: [UInt32] = spec.colors.flatMap { [UInt32(truncatingIfNeeded: $0.ornament), UInt32(truncatingIfNeeded: $0.part), $0.color] }
|
|
217
|
+
dressDraws = nil
|
|
218
|
+
// the triples stay alive across the call
|
|
219
|
+
let ok = triples.withUnsafeBufferPointer { tp in
|
|
220
|
+
var s = QvpDressSpec(ayah_mark: ayah, surah_header: header, page_frame: frame, gap: spec.gap, band_gap: spec.bandGap,
|
|
221
|
+
ayah_mark_scale: spec.ayahMarkScale, page_frame_scale: spec.pageFrameScale, line_art: lineArt,
|
|
222
|
+
colors: triples.isEmpty ? nil : tp.baseAddress, n_colors: UInt32(truncatingIfNeeded: triples.count / 3))
|
|
223
|
+
return qvp_dress(p, ornaments, &s)
|
|
224
|
+
}
|
|
225
|
+
// the dress changed the layout's fit: read it again
|
|
226
|
+
readLayout()
|
|
227
|
+
return ok != 0 ? dressInfo() : nil
|
|
228
|
+
}
|
|
229
|
+
/// Take the ornaments off: the printed rings and bands come back.
|
|
230
|
+
public func undress() { qvp_undress(p); dressDraws = nil; readLayout() }
|
|
231
|
+
/// The readout, or nil when the page is not dressed.
|
|
232
|
+
public func dressInfo() -> DressInfo? {
|
|
233
|
+
var d = QvpDressInfo()
|
|
234
|
+
guard qvp_dress_info(p, &d) != 0 else { return nil }
|
|
235
|
+
let id = { (v: UInt32) -> Int? in v == QVP_NONE ? nil : Int(v) }
|
|
236
|
+
return DressInfo(ayahMark: id(d.ayah_mark), surahHeader: id(d.surah_header), pageFrame: id(d.page_frame),
|
|
237
|
+
ayahMarks: Int(d.n_ayah_marks), surahHeaders: Int(d.n_surah_headers), frameRepeats: Int(d.n_frame_repeats),
|
|
238
|
+
frameStretched: d.frame_stretched != 0, draws: Int(d.n_draws), revision: Int(d.revision),
|
|
239
|
+
viewBox: [d.view_box.0, d.view_box.1, d.view_box.2, d.view_box.3],
|
|
240
|
+
overflow: [d.overflow.0, d.overflow.1, d.overflow.2, d.overflow.3])
|
|
241
|
+
}
|
|
242
|
+
/// Counts up whenever the ornament layer is rebuilt. 0 when undressed.
|
|
243
|
+
public func dressRevision() -> Int { dressInfo()?.revision ?? 0 }
|
|
244
|
+
/// The ornament display list, in page units, in drawing order. Built once per revision.
|
|
245
|
+
public func dressGeometry() -> [OrnamentDraw] {
|
|
246
|
+
let rev = dressRevision()
|
|
247
|
+
if let c = dressDraws, c.revision == rev { return c.draws }
|
|
248
|
+
var g = QvpDressGeometry()
|
|
249
|
+
qvp_dress_geometry(p, &g)
|
|
250
|
+
let n = Int(g.n_draws)
|
|
251
|
+
guard n > 0, let ops = g.ops, let pts = g.pts, let table = g.table else { dressDraws = (rev, []); return [] }
|
|
252
|
+
// the engine's buffers last until the next dress, undress or layout: read them now
|
|
253
|
+
let t = UnsafeBufferPointer(start: table, count: n * 9)
|
|
254
|
+
var out: [OrnamentDraw] = []; out.reserveCapacity(n)
|
|
255
|
+
for i in 0..<n {
|
|
256
|
+
let m = CGMutablePath()
|
|
257
|
+
var o = Int(t[i * 9]); let oe = o + Int(t[i * 9 + 1]); var k = Int(t[i * 9 + 2])
|
|
258
|
+
func pt(_ j: Int) -> CGPoint { CGPoint(x: CGFloat(pts[k + j]), y: CGFloat(pts[k + j + 1])) }
|
|
259
|
+
while o < oe {
|
|
260
|
+
switch ops[o] {
|
|
261
|
+
case 0: m.move(to: pt(0)); k += 2
|
|
262
|
+
case 1: m.addLine(to: pt(0)); k += 2
|
|
263
|
+
case 2: m.addQuadCurve(to: pt(2), control: pt(0)); k += 4
|
|
264
|
+
case 3: m.addCurve(to: pt(4), control1: pt(0), control2: pt(2)); k += 6
|
|
265
|
+
case 4: m.closeSubpath()
|
|
266
|
+
default: break
|
|
267
|
+
}
|
|
268
|
+
o += 1
|
|
269
|
+
}
|
|
270
|
+
let flags = t[i * 9 + 5], placement = t[i * 9 + 8]
|
|
271
|
+
out.append(OrnamentDraw(path: m, color: t[i * 9 + 4], kind: OrnamentKind(rawValue: Int(flags & 0xff)) ?? .ayahMark,
|
|
272
|
+
evenOdd: flags & UInt32(truncatingIfNeeded: QVP_DRAW_EVENODD) != 0, stroke: flags & UInt32(truncatingIfNeeded: QVP_DRAW_STROKE) != 0,
|
|
273
|
+
strokeWidth: Float(bitPattern: t[i * 9 + 6]), part: Int(t[i * 9 + 7]),
|
|
274
|
+
placement: placement == QVP_NONE ? nil : Int(placement)))
|
|
275
|
+
}
|
|
276
|
+
dressDraws = (rev, out)
|
|
277
|
+
return out
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
/// `QvpPage.dressView`: the view for a host that scrolls the page, and the dressed box's size
|
|
282
|
+
/// at that view's scale, in layout px.
|
|
283
|
+
public struct QvpDressView: Equatable {
|
|
284
|
+
public let view: QvpView
|
|
285
|
+
public let width: Float, height: Float
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
// ── what both renderers share: the raster and the overflow of a dressed page ──
|
|
289
|
+
|
|
290
|
+
/// How far a dress's border reaches past the laid-out content, in layout px.
|
|
291
|
+
struct DressOverflow: Equatable {
|
|
292
|
+
let left: CGFloat, top: CGFloat, right: CGFloat, bottom: CGFloat
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
extension QvpPage {
|
|
296
|
+
/// The dress overflow of the layout in hand, or nil when the page is not dressed.
|
|
297
|
+
var dressOverflow: DressOverflow? {
|
|
298
|
+
guard isOpen, let o = dressInfo()?.overflow, o.count == 4 else { return nil }
|
|
299
|
+
return DressOverflow(left: CGFloat(o[0]), top: CGFloat(o[1]), right: CGFloat(o[2]), bottom: CGFloat(o[3]))
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
/// `viewClamp` over the content plus the dress overflow: a drag reaches the border, never
|
|
303
|
+
/// past it. An undressed page gets the plain clamp. The engine answers, so a scroll never
|
|
304
|
+
/// pushes the border's top or bottom off the screen.
|
|
305
|
+
public func dressClamp(_ view: QvpView, viewportW: Float, viewportH: Float) -> QvpView {
|
|
306
|
+
var v = view.c, out = QvpFFI.QvpView()
|
|
307
|
+
qvp_dress_clamp(p, &v, viewportW, viewportH, &out)
|
|
308
|
+
return QvpView(out)
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
/// For a host that scrolls the page itself: the view that shows the dressed box at `scale`,
|
|
312
|
+
/// centred across `viewportW` (at x = 0 when wider) with its top at 0, and the box's size at
|
|
313
|
+
/// that scale, which is the size to give the canvas. An undressed page gets its content
|
|
314
|
+
/// centred at the top and the content's size.
|
|
315
|
+
public func dressView(_ scale: Float, viewportW: Float) -> QvpDressView {
|
|
316
|
+
var out = QvpFFI.QvpView(), size: (Float, Float) = (0, 0)
|
|
317
|
+
withUnsafeMutablePointer(to: &size) { s in
|
|
318
|
+
s.withMemoryRebound(to: Float.self, capacity: 2) { qvp_dress_view(p, scale, viewportW, &out, $0) }
|
|
319
|
+
}
|
|
320
|
+
return QvpDressView(view: QvpView(out), width: size.0, height: size.1)
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
/// The paper behind the page: the content grown by the dress overflow, so the border sits
|
|
324
|
+
/// on paper. `ox` and `oy` are where the content's origin stands on screen. Sizing only:
|
|
325
|
+
/// the fit and the clamp are the engine's.
|
|
326
|
+
func paperRect(_ l: QvpLayout, scale s: CGFloat, ox: CGFloat, oy: CGFloat) -> CGRect {
|
|
327
|
+
let o = dressOverflow ?? DressOverflow(left: 0, top: 0, right: 0, bottom: 0)
|
|
328
|
+
return CGRect(x: ox - o.left * s, y: oy - o.top * s,
|
|
329
|
+
width: (CGFloat(l.contentW) + o.left + o.right) * s, height: (CGFloat(l.contentH) + o.top + o.bottom) * s)
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
/// The ornament layer as a bitmap of `width` × `height` pixels, y-down like the ink bitmap.
|
|
333
|
+
/// `transform` is the renderer's own page-to-band transform for one placement: each draw
|
|
334
|
+
/// goes under the placement its page ink goes under, and the border goes under the
|
|
335
|
+
/// identity placement. Strokes take their width with round caps and joins. Fills take the
|
|
336
|
+
/// even-odd rule when flagged.
|
|
337
|
+
func rasterizeOrnaments(width W: Int, height H: Int, scale: CGFloat,
|
|
338
|
+
transform: (QvpPlacement) -> CGAffineTransform) -> CGImage? {
|
|
339
|
+
let draws = dressGeometry()
|
|
340
|
+
guard W > 0, H > 0, !draws.isEmpty,
|
|
341
|
+
let oc = CGContext(data: nil, width: W, height: H, bitsPerComponent: 8, bytesPerRow: 0, space: CGColorSpaceCreateDeviceRGB(),
|
|
342
|
+
bitmapInfo: CGImageAlphaInfo.premultipliedFirst.rawValue | CGBitmapInfo.byteOrder32Little.rawValue)
|
|
343
|
+
else { return nil }
|
|
344
|
+
// flip to y-down page space so `makeImage()` comes out upright
|
|
345
|
+
oc.translateBy(x: 0, y: CGFloat(H)); oc.scaleBy(x: scale, y: -scale)
|
|
346
|
+
oc.setAllowsAntialiasing(true); oc.setShouldAntialias(true)
|
|
347
|
+
oc.setLineCap(.round); oc.setLineJoin(.round)
|
|
348
|
+
let places = layoutPlacements()
|
|
349
|
+
var cur: Int?? = .none
|
|
350
|
+
for d in draws {
|
|
351
|
+
if cur != .some(d.placement) {
|
|
352
|
+
if cur != nil { oc.restoreGState() }
|
|
353
|
+
oc.saveGState()
|
|
354
|
+
oc.concatenate(transform(d.placement.map { $0 < places.count ? places[$0] : .identity } ?? .identity))
|
|
355
|
+
cur = .some(d.placement)
|
|
356
|
+
}
|
|
357
|
+
oc.addPath(d.path)
|
|
358
|
+
if d.stroke {
|
|
359
|
+
oc.setStrokeColor(QvpColor.cgColor(d.color)); oc.setLineWidth(CGFloat(d.strokeWidth)); oc.strokePath()
|
|
360
|
+
} else {
|
|
361
|
+
oc.setFillColor(QvpColor.cgColor(d.color)); oc.fillPath(using: d.evenOdd ? .evenOdd : .winding)
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
if cur != nil { oc.restoreGState() }
|
|
365
|
+
return oc.makeImage()
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
/// The ornament layer of one page, rasterised once per dress revision, view transform and
|
|
370
|
+
/// band. A tiled border is hundreds of outlines and nothing in it moves while a highlight
|
|
371
|
+
/// animates, so a frame that draws the ink again takes this image as it is. One image per
|
|
372
|
+
/// slot: a page drawn as a stack of canvases keeps one per canvas.
|
|
373
|
+
final class QvpOrnamentRaster {
|
|
374
|
+
private var slots: [Int: (key: String, image: CGImage)] = [:]
|
|
375
|
+
/// The last image this cache handed out, for tests and a HUD.
|
|
376
|
+
private(set) var last: CGImage?
|
|
377
|
+
func clear() { slots.removeAll(); last = nil }
|
|
378
|
+
/// The layer of `p` for one band, or nil when the page is not dressed. `key` says where the
|
|
379
|
+
/// band is and at what view transform. The page, its dress revision and the pixel size
|
|
380
|
+
/// are added here.
|
|
381
|
+
func image(_ p: QvpPage, slot: Int = 0, key: String, width W: Int, height H: Int, scale: CGFloat,
|
|
382
|
+
transform: (QvpPlacement) -> CGAffineTransform) -> CGImage? {
|
|
383
|
+
guard p.isOpen, let dress = p.dressInfo() else { slots[slot] = nil; last = nil; return nil }
|
|
384
|
+
let k = "\(ObjectIdentifier(p))|\(dress.revision)|\(key)|\(W)x\(H)|\(scale)"
|
|
385
|
+
if let e = slots[slot], e.key == k { last = e.image; return e.image }
|
|
386
|
+
guard let image = p.rasterizeOrnaments(width: W, height: H, scale: scale, transform: transform) else { slots[slot] = nil; return nil }
|
|
387
|
+
slots[slot] = (k, image); last = image
|
|
388
|
+
return image
|
|
389
|
+
}
|
|
390
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
// Engine-wide helpers (names, Arabic text tools, layout maths). Mirrors `object QvpEngine` in Kotlin.
|
|
2
|
+
import Foundation
|
|
3
|
+
import QvpFFI
|
|
4
|
+
|
|
5
|
+
/// The engine-wide calls: names, the Arabic text tools, and the versions.
|
|
6
|
+
public enum QvpEngine {
|
|
7
|
+
/// The engine version, e.g. `0.2.0`.
|
|
8
|
+
public static func version() -> String { var s = QvpStr(); qvp_version(&s); return s.string }
|
|
9
|
+
/// The page format version the engine reads.
|
|
10
|
+
public static func formatVersion() -> Int { Int(qvp_format_version()) }
|
|
11
|
+
/// The engine's name, `qvp`.
|
|
12
|
+
public static func engineName() -> String { String(cString: qvp_engine_name()) }
|
|
13
|
+
/// A mark's name by its id.
|
|
14
|
+
public static func markName(_ m: Int) -> String { var s = QvpStr(); qvp_mark_name(UInt8(truncatingIfNeeded: m), &s); return s.string }
|
|
15
|
+
/// A mark family's name by its id.
|
|
16
|
+
public static func familyName(_ f: Int) -> String { var s = QvpStr(); qvp_family_name(UInt8(truncatingIfNeeded: f), &s); return s.string }
|
|
17
|
+
/// A path kind's name by its id.
|
|
18
|
+
public static func kindName(_ k: Int) -> String { var s = QvpStr(); qvp_kind_name(UInt8(truncatingIfNeeded: k), &s); return s.string }
|
|
19
|
+
/// A mark category's name by its id.
|
|
20
|
+
public static func categoryName(_ c: Int) -> String { var s = QvpStr(); qvp_category_name(UInt8(truncatingIfNeeded: c), &s); return s.string }
|
|
21
|
+
/// A mark's id by its name; 255 when the engine has no such mark.
|
|
22
|
+
public static func markFromName(_ name: String) -> Int { withBytes(name) { p, n in Int(qvp_mark_from_name(p, n)) } }
|
|
23
|
+
/// The category a mark belongs to.
|
|
24
|
+
public static func markCategory(_ m: Int) -> Int { Int(qvp_mark_category(UInt8(truncatingIfNeeded: m))) }
|
|
25
|
+
/// The engine's name tables (QVP_NAMES_*): no wrapper carries a table of its own.
|
|
26
|
+
public enum Names {
|
|
27
|
+
/// The table ids, by name.
|
|
28
|
+
public static let mark = 0, kind = 1, family = 2, category = 3, decoration = 4, division = 5, place = 6
|
|
29
|
+
}
|
|
30
|
+
/// How many ids a name table has.
|
|
31
|
+
public static func nameCount(_ table: Int) -> Int { Int(qvp_name_count(UInt8(truncatingIfNeeded: table))) }
|
|
32
|
+
/// The name of an id in a table; empty outside the table.
|
|
33
|
+
public static func name(_ table: Int, _ id: Int) -> String { var s = QvpStr(); qvp_name(UInt8(truncatingIfNeeded: table), UInt8(truncatingIfNeeded: id), &s); return s.string }
|
|
34
|
+
/// 255 when the table has no such name.
|
|
35
|
+
public static func nameId(_ table: Int, _ name: String) -> Int { withBytes(name) { p, n in Int(qvp_name_id(UInt8(truncatingIfNeeded: table), p, n)) } }
|
|
36
|
+
/// Every name of a table, index = id.
|
|
37
|
+
public static func names(_ table: Int) -> [String] { (0..<nameCount(table)).map { name(table, $0) } }
|
|
38
|
+
/// A decoration kind's name by its id.
|
|
39
|
+
public static func decorationName(_ k: Int) -> String { name(Names.decoration, k) }
|
|
40
|
+
/// A division's name by its id.
|
|
41
|
+
public static func divisionName(_ d: Int) -> String { name(Names.division, d) }
|
|
42
|
+
/// A place of revelation's name by its id.
|
|
43
|
+
public static func placeName(_ p: Int) -> String { name(Names.place, p) }
|
|
44
|
+
/// Remove every Arabic mark (harakah, tatweel, waqf signs and the like) and keep the letters.
|
|
45
|
+
public static func strip(_ s: String) -> String { arabic(0, s) }
|
|
46
|
+
/// Fold the letter forms that search treats as one: أإآٱ to ا, ىئ to ي, ؤ to و, ة to ه,
|
|
47
|
+
/// and Arabic-Indic digits to ASCII.
|
|
48
|
+
public static func fold(_ s: String) -> String { arabic(1, s) }
|
|
49
|
+
/// Strip, fold and collapse the spaces, as search does to both the query and the page.
|
|
50
|
+
public static func normalize(_ s: String) -> String { arabic(2, s) }
|
|
51
|
+
/// Normalise, then drop bare alif and hamzah: the key of the loose second search pass, so a
|
|
52
|
+
/// typed `الرحمان` finds `الرحمن`.
|
|
53
|
+
public static func looseKey(_ s: String) -> String { arabic(3, s) }
|
|
54
|
+
private static func arabic(_ op: Int, _ s: String) -> String { withBytes(s) { p, n in var o = QvpStr(); qvp_arabic(UInt8(truncatingIfNeeded: op), p, n, &o); return o.string } }
|
|
55
|
+
}
|