@quran.ws/qvp-react-native 0.8.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 +78 -35
- package/android/build.gradle +16 -8
- package/android/src/main/java/ws/quran/qvp/rn/QvpModule.kt +1 -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/index.tsx +86 -10
|
@@ -0,0 +1,469 @@
|
|
|
1
|
+
// One loaded page and the cross-page atlas, over the C ABI in qvp.h.
|
|
2
|
+
// Geometry is copied out of the engine once; everything that decides (hit-testing, layout,
|
|
3
|
+
// styles, highlights, masks, search) stays inside the engine. Mirrors web/qvp.js and the
|
|
4
|
+
// Kotlin QvpPage — see docs/API.md.
|
|
5
|
+
import Foundation
|
|
6
|
+
import CoreGraphics
|
|
7
|
+
import QvpFFI
|
|
8
|
+
|
|
9
|
+
public enum QvpError: Error { case badPage, badAtlas, badPassage, badOrnaments }
|
|
10
|
+
|
|
11
|
+
public final class QvpPage {
|
|
12
|
+
private var h: OpaquePointer?
|
|
13
|
+
|
|
14
|
+
public let width: Float, height: Float, pageNo: Int
|
|
15
|
+
public let nLines: Int, nAyahs: Int, nWords: Int, nPaths: Int, nDecorations: Int
|
|
16
|
+
/// path ops: 0 MoveTo 1 LineTo 2 QuadTo 3 CubicTo 4 Close
|
|
17
|
+
public let ops: [UInt8]
|
|
18
|
+
/// x,y pairs consumed in order by the ops
|
|
19
|
+
public let pts: [Float]
|
|
20
|
+
/// stride 8 per path: opStart, opCount, ptStart, ptCount, flags, word, line, extra
|
|
21
|
+
public let table: [UInt32]
|
|
22
|
+
public let words: [QvpWord], ayahs: [QvpAyah], lines: [QvpLine], decorations: [QvpDecoration]
|
|
23
|
+
public let lineSpacing: Float
|
|
24
|
+
public private(set) var currentLayout: QvpLayout?
|
|
25
|
+
public private(set) var defaultInk: UInt32 = QvpDefaults.INK
|
|
26
|
+
private var paths: [CGPath]?
|
|
27
|
+
/// The ornament display list and the dress revision it was read at.
|
|
28
|
+
var dressDraws: (revision: Int, draws: [OrnamentDraw])?
|
|
29
|
+
|
|
30
|
+
/// Load a `NNN.qvp` page; bytes are copied by the engine.
|
|
31
|
+
public init(bytes: Data) throws {
|
|
32
|
+
h = bytes.withUnsafeBytes { qvp_page_load($0.bindMemory(to: UInt8.self).baseAddress, $0.count) }
|
|
33
|
+
guard let h else { throw QvpError.badPage }
|
|
34
|
+
var info = QvpPageInfo(); qvp_page_info(h, &info)
|
|
35
|
+
width = info.width; height = info.height; pageNo = Int(info.page)
|
|
36
|
+
nLines = Int(info.n_lines); nAyahs = Int(info.n_ayahs); nWords = Int(info.n_words); nPaths = Int(info.n_paths); nDecorations = Int(info.n_decorations)
|
|
37
|
+
var g = QvpGeometry(); qvp_geometry(h, &g)
|
|
38
|
+
ops = Array(UnsafeBufferPointer(start: g.ops, count: Int(g.ops_len)))
|
|
39
|
+
pts = Array(UnsafeBufferPointer(start: g.pts, count: Int(g.pts_len)))
|
|
40
|
+
table = Array(UnsafeBufferPointer(start: g.table, count: Int(g.n_paths) * 8))
|
|
41
|
+
words = (0..<Int(info.n_words)).map { k in
|
|
42
|
+
var w = QvpWordInfo(); _ = qvp_word_info(h, UInt32(truncatingIfNeeded: k), &w)
|
|
43
|
+
return QvpWord(index: k, surah: Int(w.surah), ayah: Int(w.ayah), word: Int(w.word), line: Int(w.line_number), ayahIndex: Int(w.ayah_index), lineIndex: Int(w.line_index),
|
|
44
|
+
x0: w.x0, y0: w.y0, x1: w.x1, y1: w.y1, text: w.text.string, firstPath: Int(w.first_path), nPaths: Int(w.n_paths))
|
|
45
|
+
}
|
|
46
|
+
ayahs = (0..<Int(info.n_ayahs)).map { k in
|
|
47
|
+
var a = QvpAyahInfo(); _ = qvp_ayah_info(h, UInt32(truncatingIfNeeded: k), &a)
|
|
48
|
+
return QvpAyah(index: k, surah: Int(a.surah), ayah: Int(a.ayah), fragment: Int(a.fragment), fragments: Int(a.fragments), flags: Int(a.flags), rubuAlHizb: Int(a.rubu_al_hizb), firstWord: Int(a.first_word), nWords: Int(a.n_words),
|
|
49
|
+
ayahMarkDecoration: index(a.ayah_mark_decoration), x0: a.x0, y0: a.y0, x1: a.x1, y1: a.y1)
|
|
50
|
+
}
|
|
51
|
+
lines = (0..<Int(info.n_lines)).map { k in
|
|
52
|
+
var l = QvpLineInfo(); _ = qvp_line_info(h, UInt32(truncatingIfNeeded: k), &l)
|
|
53
|
+
return QvpLine(index: k, lineNumber: Int(l.line_number), isHeader: l.is_header != 0, firstWord: Int(l.first_word), nWords: Int(l.n_words),
|
|
54
|
+
x0: l.x0, y0: l.y0, x1: l.x1, y1: l.y1, bandY0: l.band_y0, bandY1: l.band_y1, centre: l.centre)
|
|
55
|
+
}
|
|
56
|
+
decorations = (0..<Int(info.n_decorations)).map { k in
|
|
57
|
+
var d = QvpDecorationInfo(); _ = qvp_decoration_info(h, UInt32(truncatingIfNeeded: k), &d)
|
|
58
|
+
return QvpDecoration(index: k, decoration: Int(d.decoration), surah: Int(d.surah), ayah: Int(d.ayah), line: Int(d.line), x0: d.x0, y0: d.y0, x1: d.x1, y1: d.y1,
|
|
59
|
+
text: d.text.string, firstPath: Int(d.first_path), nPaths: Int(d.n_paths))
|
|
60
|
+
}
|
|
61
|
+
lineSpacing = qvp_page_line_spacing(h)
|
|
62
|
+
}
|
|
63
|
+
deinit { close() }
|
|
64
|
+
/// Free the native page. A second call does nothing.
|
|
65
|
+
public func close() { if let p = h { qvp_page_free(p); h = nil } }
|
|
66
|
+
/// False once `close()` has run. A renderer racing a host's page teardown
|
|
67
|
+
/// must check this before calling into the engine: every engine call on
|
|
68
|
+
/// a closed page stops the program with the fatal error `QvpPage is closed`.
|
|
69
|
+
public var isOpen: Bool { h != nil }
|
|
70
|
+
/// The engine handle. Internal: the wrapper marshals through it, no app ever sees it.
|
|
71
|
+
/// A closed page has none. A call on it is a programming error, as an index out of range
|
|
72
|
+
/// is, so this stops the program with a message that names the page, in every build.
|
|
73
|
+
var p: OpaquePointer { guard let h else { fatalError("QvpPage is closed") }; return h }
|
|
74
|
+
|
|
75
|
+
// ── geometry ──
|
|
76
|
+
public func pathOpStart(_ i: Int) -> Int { Int(table[i * 8]) }
|
|
77
|
+
public func pathOpCount(_ i: Int) -> Int { Int(table[i * 8 + 1]) }
|
|
78
|
+
public func pathPtStart(_ i: Int) -> Int { Int(table[i * 8 + 2]) }
|
|
79
|
+
public func pathPtCount(_ i: Int) -> Int { Int(table[i * 8 + 3]) }
|
|
80
|
+
public func pathFlags(_ i: Int) -> UInt32 { table[i * 8 + 4] }
|
|
81
|
+
public func pathKind(_ i: Int) -> Int { Int(pathFlags(i) & 0xff) }
|
|
82
|
+
public func pathMark(_ i: Int) -> Int { Int((pathFlags(i) >> 8) & 0xff) }
|
|
83
|
+
public func pathFamily(_ i: Int) -> Int { Int((pathFlags(i) >> 16) & 0xff) }
|
|
84
|
+
public func pathEvenOdd(_ i: Int) -> Bool { (pathFlags(i) >> 24) & 1 == 1 }
|
|
85
|
+
public func pathWord(_ i: Int) -> Int { index(table[i * 8 + 5]) }
|
|
86
|
+
public func pathLine(_ i: Int) -> Int { Int(table[i * 8 + 6]) }
|
|
87
|
+
public func pathCategory(_ i: Int) -> Int { Int(table[i * 8 + 7] & 0xff) }
|
|
88
|
+
public func pathNthInWord(_ i: Int) -> Int { Int((table[i * 8 + 7] >> 8) & 0xff) }
|
|
89
|
+
public func pathNthMark(_ i: Int) -> Int { let n = Int((table[i * 8 + 7] >> 16) & 0xff); return n == 0xff ? -1 : n }
|
|
90
|
+
/// One CGPath per engine path, in page units, built once. Fill with `pathEvenOdd(i)` as the rule.
|
|
91
|
+
public func buildPaths() -> [CGPath] {
|
|
92
|
+
if let paths { return paths }
|
|
93
|
+
var out: [CGPath] = []; out.reserveCapacity(nPaths)
|
|
94
|
+
for i in 0..<nPaths {
|
|
95
|
+
let m = CGMutablePath()
|
|
96
|
+
var o = pathOpStart(i); let oe = o + pathOpCount(i); var k = pathPtStart(i)
|
|
97
|
+
while o < oe {
|
|
98
|
+
switch ops[o] {
|
|
99
|
+
case 0: m.move(to: CGPoint(x: CGFloat(pts[k]), y: CGFloat(pts[k + 1]))); k += 2
|
|
100
|
+
case 1: m.addLine(to: CGPoint(x: CGFloat(pts[k]), y: CGFloat(pts[k + 1]))); k += 2
|
|
101
|
+
case 2: m.addQuadCurve(to: CGPoint(x: CGFloat(pts[k + 2]), y: CGFloat(pts[k + 3])), control: CGPoint(x: CGFloat(pts[k]), y: CGFloat(pts[k + 1]))); k += 4
|
|
102
|
+
case 3: m.addCurve(to: CGPoint(x: CGFloat(pts[k + 4]), y: CGFloat(pts[k + 5])), control1: CGPoint(x: CGFloat(pts[k]), y: CGFloat(pts[k + 1])), control2: CGPoint(x: CGFloat(pts[k + 2]), y: CGFloat(pts[k + 3]))); k += 6
|
|
103
|
+
case 4: m.closeSubpath()
|
|
104
|
+
default: break
|
|
105
|
+
}
|
|
106
|
+
o += 1
|
|
107
|
+
}
|
|
108
|
+
out.append(m)
|
|
109
|
+
}
|
|
110
|
+
paths = out; return out
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// ── words / text ──
|
|
114
|
+
public func wordKey(_ i: Int) -> String { words[i].wordKey }
|
|
115
|
+
/// The text of word `i` in `form`, or "" when the page has no such word or form.
|
|
116
|
+
public func wordForm(_ i: Int, _ form: Form = .rasmUthmani) -> String { var s = QvpStr(); return qvp_word_form(p, UInt32(truncatingIfNeeded: i), UInt8(truncatingIfNeeded: form.rawValue), &s) != 0 ? s.string : "" }
|
|
117
|
+
/// The index of the word `surah:ayah:word` on this page, or -1.
|
|
118
|
+
public func findWord(_ surah: Int, _ ayah: Int, _ word: Int) -> Int { Int(qvp_find_word(p, UInt16(truncatingIfNeeded: surah), UInt16(truncatingIfNeeded: ayah), UInt16(truncatingIfNeeded: word))) }
|
|
119
|
+
public func target(_ s: String) -> Target { Target.parse(s, page: self) }
|
|
120
|
+
public func targetWords(_ t: Target) -> [Int] { t.withC { tp in collect(512) { o, c in qvp_target_words(p, tp, o, c) } }.map { Int($0) } }
|
|
121
|
+
public func targetWords(_ s: String) -> [Int] { targetWords(target(s)) }
|
|
122
|
+
public func targetWords(_ ws: [Int]) -> [Int] { targetWords(Target.words(ws)) }
|
|
123
|
+
public func text(_ t: Target, form: Form = .rasmUthmani, wordSep: String = " ", lineSep: String = "\n") -> String {
|
|
124
|
+
withBytes(wordSep) { wp, wn in withBytes(lineSep) { lp, ln in t.withC { tp in var s = QvpStr(); qvp_text(p, tp, UInt8(truncatingIfNeeded: form.rawValue), wp, wn, lp, ln, &s); return s.string } } }
|
|
125
|
+
}
|
|
126
|
+
public func text(_ s: String = "page", form: Form = .rasmUthmani, wordSep: String = " ", lineSep: String = "\n") -> String { text(target(s), form: form, wordSep: wordSep, lineSep: lineSep) }
|
|
127
|
+
public func search(_ query: String, form: Form = .search, mode: SearchMode = .includes, normalize: Bool = true, looseMatch: Bool = true, limit: Int = 0) -> [QvpMatch] {
|
|
128
|
+
let v: [QvpFFI.QvpMatch] = withBytes(query) { qp, qn in collect(256) { o, c in qvp_search(p, qp, qn, UInt8(truncatingIfNeeded: form.rawValue), UInt8(truncatingIfNeeded: mode.rawValue), normalize ? 1 : 0, looseMatch ? 1 : 0, UInt32(truncatingIfNeeded: limit), o, c) } }
|
|
129
|
+
return v.map { m in let w = Int(m.word); return QvpMatch(word: w, index: Int(m.index), isLooseMatch: m.is_loose_match != 0, wordKey: wordKey(w), text: words[w].text) }
|
|
130
|
+
}
|
|
131
|
+
/// A citation such as `2:255-257, 3:1` for a word list.
|
|
132
|
+
public func citation(_ ws: [Int]) -> String { ws.map { UInt32(truncatingIfNeeded: $0) }.withUnsafeBufferPointer { b in var s = QvpStr(); qvp_citation(p, b.baseAddress, UInt32(truncatingIfNeeded: b.count), &s); return s.string } }
|
|
133
|
+
/// Attach a words sidecar ({"s:a:w": {"rasm_imlai","qpc","rasm","search"}}); returns words updated, -1 on bad JSON.
|
|
134
|
+
public func attachWords(_ json: Data) -> Int { json.withUnsafeBytes { Int(qvp_attach_words(p, $0.bindMemory(to: UInt8.self).baseAddress, UInt32(truncatingIfNeeded: $0.count))) } }
|
|
135
|
+
public func attachWords(_ json: String) -> Int { attachWords(Data(json.utf8)) }
|
|
136
|
+
/// True when the page has the text of `form` for its words.
|
|
137
|
+
public func hasForm(_ form: Form) -> Bool { qvp_has_form(p, UInt8(truncatingIfNeeded: form.rawValue)) != 0 }
|
|
138
|
+
|
|
139
|
+
// ── metadata ──
|
|
140
|
+
/// Every surah, in order.
|
|
141
|
+
public func surahs() -> [QvpSurah] {
|
|
142
|
+
(0..<Int(qvp_surah_count(p))).map { i in
|
|
143
|
+
var s = QvpFFI.QvpSurah(); _ = qvp_surah_at(p, UInt32(truncatingIfNeeded: i), &s)
|
|
144
|
+
return QvpSurah(number: Int(s.number), ayahCount: Int(s.ayah_count), hasBanner: s.has_banner != 0, hasBasmalah: s.has_basmalah != 0, place: place(s.place), bannerDecoration: index(s.banner_decoration), arabic: s.arabic.string, latin: s.latin.string, english: s.english.string)
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
private func header(_ h: QvpFFI.QvpSurahHeader) -> QvpSurahHeader {
|
|
148
|
+
QvpSurahHeader(decoration: index(h.decoration), surah: Int(h.surah), line: Int(h.line), x0: h.x0, y0: h.y0, x1: h.x1, y1: h.y1, titleX0: h.title_x0, titleY0: h.title_y0, titleX1: h.title_x1, titleY1: h.title_y1)
|
|
149
|
+
}
|
|
150
|
+
/// Each heading's two boxes in page units. Both leave a native frame out, so they hold
|
|
151
|
+
/// whether the layout draws one or not.
|
|
152
|
+
public func surahHeaders() -> [QvpSurahHeader] { collect(32) { o, c in qvp_surah_headers(p, o, c) }.map(header) }
|
|
153
|
+
/// The same boxes in viewport px through the current layout — where a reflowed page put them.
|
|
154
|
+
public func surahHeadersView() -> [QvpSurahHeader] { collect(32) { o, c in qvp_surah_headers_view(p, o, c) }.map(header) }
|
|
155
|
+
public func divisions() -> [QvpDivision] { collect(64) { o, c in qvp_divisions(p, o, c) }.map { (d: QvpFFI.QvpDivision) in QvpDivision(division: Division(rawValue: Int(d.division)) ?? .juz, number: Int(d.number), surah: Int(d.surah), ayah: Int(d.ayah), line: Int(d.line), ayahIndex: Int(d.ayah_index)) } }
|
|
156
|
+
public func ayahMarks() -> [QvpAyahMark] { collect(128) { o, c in qvp_ayah_marks(p, o, c) }.map { (m: QvpFFI.QvpAyahMark) in QvpAyahMark(decoration: index(m.decoration), surah: Int(m.surah), ayah: Int(m.ayah), line: Int(m.line), cx: m.cx, cy: m.cy, r: m.r, ornamentPath: index(m.ornament_path), numeralPath: index(m.numeral_path)) } }
|
|
157
|
+
public func ayahMarkOf(_ surah: Int, _ ayah: Int) -> QvpAyahMark? { ayahMarks().first { $0.surah == surah && $0.ayah == ayah } }
|
|
158
|
+
/// The same marks in viewport px through the current layout — where a reflowed page put them.
|
|
159
|
+
public func ayahMarksView() -> [QvpAyahMark] { collect(128) { o, c in qvp_ayah_marks_view(p, o, c) }.map { (m: QvpFFI.QvpAyahMark) in QvpAyahMark(decoration: index(m.decoration), surah: Int(m.surah), ayah: Int(m.ayah), line: Int(m.line), cx: m.cx, cy: m.cy, r: m.r, ornamentPath: index(m.ornament_path), numeralPath: index(m.numeral_path)) } }
|
|
160
|
+
public func rosettes() -> [QvpRosette] { collect(32) { o, c in qvp_rosettes(p, o, c) }.map { (r: QvpFFI.QvpRosette) in QvpRosette(decoration: index(r.decoration), surah: Int(r.surah), ayah: Int(r.ayah), juz: Int(r.juz), hizb: Int(r.hizb), nisf: Int(r.nisf), rubuAlHizb: Int(r.rubu_al_hizb), rubuAlHizbInHizb: Int(r.rubu_al_hizb_in_hizb)) } }
|
|
161
|
+
public func sajdahs() -> [QvpSajdah] { collect(16) { o, c in qvp_sajdahs(p, o, c) }.map { (s: QvpFFI.QvpSajdah) in QvpSajdah(decoration: index(s.decoration), surah: Int(s.surah), ayah: Int(s.ayah), signPath: index(s.sign_path)) } }
|
|
162
|
+
public func ayahKeys() -> [(Int, Int)] { collect(256) { o, c in qvp_ayah_keys(p, o, c) }.map { (k: UInt32) in (Int(k >> 16), Int(k & 0xffff)) } }
|
|
163
|
+
/// (count on this page, whole ayah is here)
|
|
164
|
+
public func ayahWordCount(_ surah: Int, _ ayah: Int) -> (count: Int, isComplete: Bool) { var c: UInt8 = 0; let n = qvp_ayah_word_count(p, UInt16(truncatingIfNeeded: surah), UInt16(truncatingIfNeeded: ayah), &c); return (Int(n), c != 0) }
|
|
165
|
+
/// Words for n recitation segments, or nil when the counts disagree (follow the ayah whole).
|
|
166
|
+
public func reciteMap(_ surah: Int, _ ayah: Int, nSegments: Int) -> [Int]? {
|
|
167
|
+
// The engine says how many it wrote; the buffer grows below when that is more. The C ABI
|
|
168
|
+
// reads nSegments as unsigned, so a negative or huge count is the engine's to answer.
|
|
169
|
+
var buf = [UInt32](repeating: 0, count: 64)
|
|
170
|
+
let n = buf.withUnsafeMutableBufferPointer { qvp_recite_map(p, UInt16(truncatingIfNeeded: surah), UInt16(truncatingIfNeeded: ayah), UInt32(truncatingIfNeeded: nSegments), $0.baseAddress, UInt32(truncatingIfNeeded: $0.count)) }
|
|
171
|
+
if n < 0 { return nil }
|
|
172
|
+
if Int(n) > buf.count { buf = [UInt32](repeating: 0, count: Int(n)); _ = buf.withUnsafeMutableBufferPointer { qvp_recite_map(p, UInt16(truncatingIfNeeded: surah), UInt16(truncatingIfNeeded: ayah), UInt32(truncatingIfNeeded: nSegments), $0.baseAddress, UInt32(truncatingIfNeeded: $0.count)) } }
|
|
173
|
+
return buf.prefix(Int(n)).map { Int($0) }
|
|
174
|
+
}
|
|
175
|
+
/// An accessibility label for word `i`.
|
|
176
|
+
public func wordLabel(_ i: Int) -> String { var s = QvpStr(); qvp_word_label(p, UInt32(truncatingIfNeeded: i), &s); return s.string }
|
|
177
|
+
/// An accessibility label for the ayah fragment at index `i` on this page.
|
|
178
|
+
public func ayahLabel(_ i: Int) -> String { var s = QvpStr(); qvp_ayah_label(p, UInt32(truncatingIfNeeded: i), &s); return s.string }
|
|
179
|
+
|
|
180
|
+
// ── hit testing ──
|
|
181
|
+
private func hit(_ ok: Int32, _ v: QvpFFI.QvpHit) -> QvpHit? { ok != 0 ? QvpHit(word: index(v.word), path: index(v.path), decoration: index(v.decoration), line: index(v.line), distance: v.distance, isExact: v.is_exact != 0) : nil }
|
|
182
|
+
/// Exact hit, page units: the outline under the point, else the box of a word or a decoration.
|
|
183
|
+
public func hitTestExact(_ x: Float, _ y: Float) -> QvpHit? { var v = QvpFFI.QvpHit(); return hit(qvp_hit_test_exact(p, x, y, &v), v) }
|
|
184
|
+
/// Exact hit, viewport px through the current layout.
|
|
185
|
+
public func hitTestExactView(_ viewX: Float, _ viewY: Float) -> QvpHit? { var v = QvpFFI.QvpHit(); return hit(qvp_hit_test_exact_view(p, viewX, viewY, &v), v) }
|
|
186
|
+
/// Gap-aware: every point on a printed line resolves to the word the reader meant.
|
|
187
|
+
public func hitTest(_ x: Float, _ y: Float, _ o: QvpHitOptions = QvpHitOptions()) -> QvpHit? {
|
|
188
|
+
var opt = QvpFFI.QvpHitOptions(max_distance: o.maxDistance, gap_bias: o.gapBias, prefer_exact: o.preferExact ? 1 : 0); var v = QvpFFI.QvpHit()
|
|
189
|
+
return hit(qvp_hit_test(p, x, y, &opt, &v), v)
|
|
190
|
+
}
|
|
191
|
+
public func hitTestView(_ viewX: Float, _ viewY: Float, _ o: QvpHitOptions = QvpHitOptions()) -> QvpHit? {
|
|
192
|
+
var opt = QvpFFI.QvpHitOptions(max_distance: o.maxDistance, gap_bias: o.gapBias, prefer_exact: o.preferExact ? 1 : 0); var v = QvpFFI.QvpHit()
|
|
193
|
+
return hit(qvp_hit_test_view(p, viewX, viewY, &opt, &v), v)
|
|
194
|
+
}
|
|
195
|
+
public func lineBands() -> [QvpLineBand] { collect(64) { o, c in qvp_line_bands(p, o, c) }.map { (b: QvpFFI.QvpLineBand) in QvpLineBand(line: Int(b.line), lineNumber: Int(b.line_number), y0: b.y0, y1: b.y1, mid: b.mid, inkY0: b.ink_y0, inkY1: b.ink_y1) } }
|
|
196
|
+
public func hitAreas(gapBias: Float = QvpDefaults.GAP_BIAS) -> [QvpHitArea] { collect(512) { o, c in qvp_hit_areas(p, gapBias, o, c) }.map { (b: QvpFFI.QvpHitArea) in QvpHitArea(word: Int(b.word), line: Int(b.line), x0: b.x0, y0: b.y0, x1: b.x1, y1: b.y1, inkX0: b.ink_x0, inkY0: b.ink_y0, inkX1: b.ink_x1, inkY1: b.ink_y1) } }
|
|
197
|
+
/// The same partition in viewport px through the current layout. Not `hitAreas` transformed:
|
|
198
|
+
/// a reflowed row's words have other neighbours than a printed line's, so it is computed again.
|
|
199
|
+
public func hitAreasView(gapBias: Float = QvpDefaults.GAP_BIAS) -> [QvpHitArea] { collect(512) { o, c in qvp_hit_areas_view(p, gapBias, o, c) }.map { (b: QvpFFI.QvpHitArea) in QvpHitArea(word: Int(b.word), line: Int(b.line), x0: b.x0, y0: b.y0, x1: b.x1, y1: b.y1, inkX0: b.ink_x0, inkY0: b.ink_y0, inkX1: b.ink_x1, inkY1: b.ink_y1) } }
|
|
200
|
+
|
|
201
|
+
// ── layout ──
|
|
202
|
+
@discardableResult
|
|
203
|
+
public func layout(_ spec: QvpLayoutSpec) -> QvpLayout {
|
|
204
|
+
var s = spec.c
|
|
205
|
+
var l = QvpFFI.QvpLayout(); qvp_layout(p, &s, &l)
|
|
206
|
+
let n = Int(l.n_lines); let f = Array(UnsafeBufferPointer(start: l.lines, count: n * 3))
|
|
207
|
+
let out = QvpLayout(scale: l.scale, offsetX: l.offset_x, offsetY: l.offset_y, contentW: l.content_w, contentH: l.content_h, lineSpacing: l.line_spacing,
|
|
208
|
+
lineDy: (0..<n).map { f[$0 * 3] }, slotTop: (0..<n).map { f[$0 * 3 + 1] }, slotBottom: (0..<n).map { f[$0 * 3 + 2] },
|
|
209
|
+
fitScale: l.fit_scale, fitX: l.fit_x, fitY: l.fit_y,
|
|
210
|
+
reflowed: l.reflowed != 0, rows: Int(l.n_rows))
|
|
211
|
+
currentLayout = out; return out
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/// Read back the layout the page already has, without computing one: what to call after the
|
|
215
|
+
/// engine laid the page out itself, as the zoom control does.
|
|
216
|
+
@discardableResult
|
|
217
|
+
public func readLayout() -> QvpLayout? {
|
|
218
|
+
var l = QvpFFI.QvpLayout()
|
|
219
|
+
guard qvp_layout_current(p, &l) != 0 else { return nil }
|
|
220
|
+
let n = Int(l.n_lines); let f = Array(UnsafeBufferPointer(start: l.lines, count: n * 3))
|
|
221
|
+
let out = QvpLayout(scale: l.scale, offsetX: l.offset_x, offsetY: l.offset_y, contentW: l.content_w, contentH: l.content_h, lineSpacing: l.line_spacing,
|
|
222
|
+
lineDy: (0..<n).map { f[$0 * 3] }, slotTop: (0..<n).map { f[$0 * 3 + 1] }, slotBottom: (0..<n).map { f[$0 * 3 + 2] },
|
|
223
|
+
fitScale: l.fit_scale, fitX: l.fit_x, fitY: l.fit_y,
|
|
224
|
+
reflowed: l.reflowed != 0, rows: Int(l.n_rows))
|
|
225
|
+
currentLayout = out; return out
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
// ── a reflowed page: where the ink went ──
|
|
229
|
+
/// Where each group of paths is placed. Without reflow there is one group per printed line,
|
|
230
|
+
/// holding that line's shift; with reflow the groups are the page's words and decorations.
|
|
231
|
+
public func layoutGroups() -> [QvpPlacement] {
|
|
232
|
+
let f: [Float] = collect(1024) { o, c in qvp_layout_groups(p, o, c / 4) * 4 }
|
|
233
|
+
return stride(from: 0, to: f.count, by: 4).map { QvpPlacement(dx: f[$0], dy: f[$0 + 1], kx: f[$0 + 2], ky: f[$0 + 3]) }
|
|
234
|
+
}
|
|
235
|
+
/// The group of every path. Empty without reflow, where a path's group is its printed line.
|
|
236
|
+
public func layoutPathGroups() -> [Int] { collect(4096) { (o: UnsafeMutablePointer<UInt32>, c) in qvp_layout_path_groups(p, o, c) }.map(Int.init) }
|
|
237
|
+
/// Paths this layout does not draw: sheet furniture and a native surah frame in reflow.
|
|
238
|
+
public func layoutOmittedPaths() -> [Int] { collect(64) { (o: UnsafeMutablePointer<UInt32>, c) in qvp_layout_omitted_paths(p, o, c) }.map(Int.init) }
|
|
239
|
+
/// Everything the current layout draws, in drawing order: each path once under the
|
|
240
|
+
/// placement it belongs to, and again for every row a decoration is repeated over. A path
|
|
241
|
+
/// this layout leaves out never appears, so one loop draws any page, printed or reflowed.
|
|
242
|
+
/// `band` holds it to a band of the laid-out page, in viewport px, as `QvpLayout.slotTop`
|
|
243
|
+
/// and every `…View` answer are: what keeps a page taller than the screen smooth under a
|
|
244
|
+
/// finger. `nil` draws the whole page.
|
|
245
|
+
public func layoutDrawList(band: (top: Float, bottom: Float)? = nil) -> [QvpDraw] {
|
|
246
|
+
let (t, b) = band ?? (0, 0)
|
|
247
|
+
let f: [UInt32] = collect(4096) { o, c in qvp_layout_draw_list(p, t, b, o, c / 2) * 2 }
|
|
248
|
+
return stride(from: 0, to: f.count, by: 2).map { QvpDraw(path: Int(f[$0]), placement: Int(f[$0 + 1])) }
|
|
249
|
+
}
|
|
250
|
+
/// What a draw list's `placement` indexes: every group, then every repeat.
|
|
251
|
+
public func layoutPlacements() -> [QvpPlacement] {
|
|
252
|
+
let f: [Float] = collect(1024) { o, c in qvp_layout_placements(p, o, c / 4) * 4 }
|
|
253
|
+
return stride(from: 0, to: f.count, by: 4).map { QvpPlacement(dx: f[$0], dy: f[$0 + 1], kx: f[$0 + 2], ky: f[$0 + 3]) }
|
|
254
|
+
}
|
|
255
|
+
/// Paths drawn again elsewhere: a sajdah line over the two rows its words landed on.
|
|
256
|
+
public func layoutRepeats() -> [QvpRepeat] {
|
|
257
|
+
let f: [Float] = collect(64) { o, c in qvp_layout_repeats(p, o, c / 6) * 6 }
|
|
258
|
+
return stride(from: 0, to: f.count, by: 6).map {
|
|
259
|
+
QvpRepeat(firstPath: Int(f[$0]), nPaths: Int(f[$0 + 1]), placement: QvpPlacement(dx: f[$0 + 2], dy: f[$0 + 3], kx: f[$0 + 4], ky: f[$0 + 5]))
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
/// The words of a reflowed row, in reading order.
|
|
263
|
+
public func rowWords(_ row: Int) -> [Int] { collect(64) { (o: UnsafeMutablePointer<UInt32>, c) in qvp_layout_row_words(p, UInt32(truncatingIfNeeded: row), o, c) }.map(Int.init) }
|
|
264
|
+
/// The largest reflow zoom at which every word of this page still fits a row.
|
|
265
|
+
public func reflowMaxZoom(_ spec: QvpLayoutSpec) -> Float { var s = spec.c; return qvp_reflow_max_zoom(p, &s) }
|
|
266
|
+
/// The zoom steps this page ships with, lowest first. Zoom 1, the printed page, is the step
|
|
267
|
+
/// before them, so a control has one more position than this has entries.
|
|
268
|
+
public func zoomSteps(_ spec: QvpLayoutSpec) -> [Float] {
|
|
269
|
+
var s = spec.c
|
|
270
|
+
var out = [Float](repeating: 0, count: 8)
|
|
271
|
+
let n = out.withUnsafeMutableBufferPointer { qvp_zoom_steps(p, &s, $0.baseAddress!, 8) }
|
|
272
|
+
return Array(out.prefix(Int(n)))
|
|
273
|
+
}
|
|
274
|
+
/// The `lineSpacing` multiplier that makes this page fill the padded viewport of `spec`; `max` 0 = unlimited.
|
|
275
|
+
public func layoutLineSpacingToFill(_ spec: QvpLayoutSpec, max: Float = 0) -> Float { var s = spec.c; return qvp_layout_line_spacing_to_fill(p, &s, max) }
|
|
276
|
+
/// The share of the padded viewport of `spec` left empty when the page is fitted to width.
|
|
277
|
+
public func layoutWastedFraction(_ spec: QvpLayoutSpec) -> Float { var s = spec.c; return qvp_layout_wasted_fraction(p, &s) }
|
|
278
|
+
/// The page's height laid out for `spec` at the printed pitch, in viewport px: `contentH` before
|
|
279
|
+
/// fill-height adds any leading, padding included. What a host asks before it sizes the canvas.
|
|
280
|
+
/// On a page dressed in a border it is the whole page, border included: the dressed box fitted
|
|
281
|
+
/// to the viewport width, as `dressView` at scale 0 fits it.
|
|
282
|
+
public func layoutPrintedHeight(_ spec: QvpLayoutSpec) -> Float { var s = spec.c; return qvp_layout_printed_height(p, &s) }
|
|
283
|
+
/// The grid this page is laid out inside: the mushaf's line count and the printed line spacing.
|
|
284
|
+
public var grid: QvpGrid { var g = QvpFFI.QvpGrid(); qvp_page_grid(p, &g); return QvpGrid(lines: Int(g.lines), lineSpacing: g.line_spacing) }
|
|
285
|
+
/// A word's box in viewport px through the current layout (x0, y0, x1, y1).
|
|
286
|
+
public func wordBoundsView(_ i: Int) -> (x0: Float, y0: Float, x1: Float, y1: Float)? {
|
|
287
|
+
var b: (Float, Float, Float, Float) = (0, 0, 0, 0)
|
|
288
|
+
let ok = withUnsafeMutablePointer(to: &b) { $0.withMemoryRebound(to: Float.self, capacity: 4) { qvp_word_bounds_view(p, UInt32(truncatingIfNeeded: i), $0) } }
|
|
289
|
+
return ok != 0 ? (b.0, b.1, b.2, b.3) : nil
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
// ── styles (handles undo exactly) ──
|
|
293
|
+
/// Colour the paths `sel` selects on `layer`. The handle undoes it.
|
|
294
|
+
@discardableResult public func style(_ sel: Selector, _ rgba: UInt32, transitionMs: Int = 0, layer: Int = QvpLayer.BASE) -> Int { var s = sel.c; return Int(qvp_style_add(p, Int32(truncatingIfNeeded: layer), &s, rgba, UInt32(truncatingIfNeeded: transitionMs))) }
|
|
295
|
+
/// Colour the words a target resolves to on `layer`. The handle undoes it.
|
|
296
|
+
@discardableResult public func styleTarget(_ t: Target, _ rgba: UInt32, transitionMs: Int = 0, layer: Int = QvpLayer.BASE) -> Int { t.withC { Int(qvp_style_add_target(p, Int32(truncatingIfNeeded: layer), $0, rgba, UInt32(truncatingIfNeeded: transitionMs))) } }
|
|
297
|
+
/// Colour the words a target resolves to on `layer`. The handle undoes it.
|
|
298
|
+
@discardableResult public func styleTarget(_ s: String, _ rgba: UInt32, transitionMs: Int = 0, layer: Int = QvpLayer.BASE) -> Int { styleTarget(target(s), rgba, transitionMs: transitionMs, layer: layer) }
|
|
299
|
+
/// Undo a style rule by its handle.
|
|
300
|
+
@discardableResult public func removeStyle(_ handle: Int) -> Int { Int(qvp_style_remove(p, UInt32(truncatingIfNeeded: handle))) }
|
|
301
|
+
/// Give a style rule another colour, fading over `transitionMs`.
|
|
302
|
+
@discardableResult public func recolorStyle(_ handle: Int, _ rgba: UInt32, transitionMs: Int = 0) -> Int { Int(qvp_style_recolor(p, UInt32(truncatingIfNeeded: handle), rgba, UInt32(truncatingIfNeeded: transitionMs))) }
|
|
303
|
+
@discardableResult public func hide(_ sel: Selector) -> Int { var s = sel.c; return Int(qvp_style_hide(p, &s)) }
|
|
304
|
+
public func clearStyles() { qvp_style_clear(p) }
|
|
305
|
+
/// Remove every style rule on `layer`.
|
|
306
|
+
public func clearLayer(_ layer: Int) { qvp_style_clear_layer(p, Int32(truncatingIfNeeded: layer)) }
|
|
307
|
+
public func setDefaultColor(_ rgba: UInt32) { defaultInk = rgba; qvp_style_default_color(p, rgba) }
|
|
308
|
+
@discardableResult public func theme(_ t: QvpTheme) -> Int {
|
|
309
|
+
let pairs: [UInt32] = t.marks.flatMap { [UInt32(truncatingIfNeeded: markId($0.key)), $0.value] }
|
|
310
|
+
return pairs.withUnsafeBufferPointer { mp in
|
|
311
|
+
var c = QvpFFI.QvpTheme(ink: t.ink ?? 0, diacritics: t.diacritics ?? 0, dots: t.dots ?? 0, waqf: t.waqf ?? 0, sifr: t.sifr ?? 0, ayah_mark: t.ayahMark ?? 0, numeral: t.numeral ?? 0, headers: t.headers ?? 0, transition_ms: UInt32(truncatingIfNeeded: t.transitionMs), marks: pairs.isEmpty ? nil : mp.baseAddress, n_marks: UInt32(truncatingIfNeeded: pairs.count / 2))
|
|
312
|
+
return Int(qvp_theme(p, &c))
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
public func styleHandles() -> [Int] { collect(256) { o, c in qvp_style_handles(p, o, c) }.map { (h: UInt32) in Int(h) } }
|
|
316
|
+
|
|
317
|
+
// ── clock & display list ──
|
|
318
|
+
/// Advance animations; true while something is still moving (keep drawing frames).
|
|
319
|
+
public func tick(_ nowMs: Double) -> Bool { qvp_tick(p, nowMs) != 0 }
|
|
320
|
+
/// A colour per path (current, mid-transition).
|
|
321
|
+
public func colors() -> [UInt32] { Array(UnsafeBufferPointer(start: qvp_colors(p), count: nPaths)) }
|
|
322
|
+
/// (path, colour) pairs for paths ≠ default ink.
|
|
323
|
+
public func styledPaths() -> [(path: Int, color: UInt32)] {
|
|
324
|
+
let v: [UInt32] = collect(512) { o, c in qvp_styled_paths(p, o, c / 2) * 2 }
|
|
325
|
+
return stride(from: 0, to: v.count - 1, by: 2).map { (Int(v[$0]), v[$0 + 1]) }
|
|
326
|
+
}
|
|
327
|
+
/// One path's current colour, 0xRRGGBBAA.
|
|
328
|
+
public func colorOf(_ path: Int) -> UInt32 { qvp_color_of(p, UInt32(truncatingIfNeeded: path)) }
|
|
329
|
+
|
|
330
|
+
// ── highlights ──
|
|
331
|
+
@discardableResult public func highlight(_ t: Target, _ style: QvpHighlightStyle = QvpHighlightStyle()) -> Int { var s = style.c; return t.withC { Int(qvp_highlight_add(p, $0, &s)) } }
|
|
332
|
+
@discardableResult public func highlight(_ s: String, _ style: QvpHighlightStyle = QvpHighlightStyle()) -> Int { highlight(target(s), style) }
|
|
333
|
+
/// Move a highlight to another target; the band slides.
|
|
334
|
+
@discardableResult public func moveHighlight(_ handle: Int, _ t: Target) -> Bool { t.withC { qvp_highlight_move(p, UInt32(truncatingIfNeeded: handle), $0) != 0 } }
|
|
335
|
+
/// Move a highlight to another target; the band slides.
|
|
336
|
+
@discardableResult public func moveHighlight(_ handle: Int, _ s: String) -> Bool { moveHighlight(handle, target(s)) }
|
|
337
|
+
/// Give a highlight another style.
|
|
338
|
+
@discardableResult public func restyleHighlight(_ handle: Int, _ style: QvpHighlightStyle) -> Bool { var s = style.c; return qvp_highlight_restyle(p, UInt32(truncatingIfNeeded: handle), &s) != 0 }
|
|
339
|
+
/// Remove a highlight by its handle; it fades out.
|
|
340
|
+
@discardableResult public func removeHighlight(_ handle: Int) -> Bool { qvp_highlight_remove(p, UInt32(truncatingIfNeeded: handle)) != 0 }
|
|
341
|
+
public func clearHighlights() { qvp_highlight_clear(p) }
|
|
342
|
+
public func highlightHandles() -> [Int] { collect(64) { o, c in qvp_highlight_handles(p, o, c) }.map { (h: UInt32) in Int(h) } }
|
|
343
|
+
/// The words a highlight covers.
|
|
344
|
+
public func highlightWords(_ handle: Int) -> [Int] { collect(512) { o, c in qvp_highlight_words(p, UInt32(truncatingIfNeeded: handle), o, c) }.map { (w: UInt32) in Int(w) } }
|
|
345
|
+
private func boxes(_ v: [QvpFFI.QvpBox]) -> [QvpBox] { v.map { QvpBox(id: Int($0.id), line: Int($0.line), x0: $0.x0, y0: $0.y0, x1: $0.x1, y1: $0.y1, color: $0.color, radius: $0.radius) } }
|
|
346
|
+
/// Animated band boxes in viewport px; draw each id as one nonzero path behind the ink.
|
|
347
|
+
public func highlightBoxesView() -> [QvpBox] { boxes(collect(128) { o, c in qvp_highlight_boxes_view(p, o, c) }) }
|
|
348
|
+
public func wordBands(_ ws: [Int], height: BandHeight = .lineSpacing, padX: Float = QvpDefaults.HIGHLIGHT_PAD_X, padY: Float = QvpDefaults.HIGHLIGHT_PAD_Y) -> [QvpBox] {
|
|
349
|
+
ws.map { UInt32(truncatingIfNeeded: $0) }.withUnsafeBufferPointer { wb in boxes(collect(64) { o, c in qvp_word_bands(p, wb.baseAddress, UInt32(truncatingIfNeeded: wb.count), UInt8(truncatingIfNeeded: height.rawValue), padX, padY, o, c) }) }
|
|
350
|
+
}
|
|
351
|
+
/// The same bands in viewport px through the current layout: one box per row the words
|
|
352
|
+
/// occupy, where a reflowed page has moved them. A host drawing its own ayah marks or
|
|
353
|
+
/// underlines reads these and never places ink from the printed page.
|
|
354
|
+
public func wordBandsView(_ ws: [Int], height: BandHeight = .lineSpacing, padX: Float = QvpDefaults.HIGHLIGHT_PAD_X, padY: Float = QvpDefaults.HIGHLIGHT_PAD_Y) -> [QvpBox] {
|
|
355
|
+
ws.map { UInt32(truncatingIfNeeded: $0) }.withUnsafeBufferPointer { wb in boxes(collect(64) { o, c in qvp_word_bands_view(p, wb.baseAddress, UInt32(truncatingIfNeeded: wb.count), UInt8(truncatingIfNeeded: height.rawValue), padX, padY, o, c) }) }
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
// ── selection ──
|
|
359
|
+
/// Select the words from `anchor` to `focus`, whole words. A negative index clears that end.
|
|
360
|
+
public func select(_ anchor: Int, _ focus: Int? = nil) { qvp_select(p, anchor < 0 ? QVP_NONE : UInt32(truncatingIfNeeded: anchor), (focus ?? anchor) < 0 ? QVP_NONE : UInt32(truncatingIfNeeded: focus ?? anchor)) }
|
|
361
|
+
public func clearSelection() { qvp_select(p, QVP_NONE, QVP_NONE) }
|
|
362
|
+
public func selection() -> [Int] { collect(512) { o, c in qvp_selection(p, o, c) }.map { (w: UInt32) in Int(w) } }
|
|
363
|
+
/// The text of the selection in `form`, with its citation when asked.
|
|
364
|
+
public func selectionText(_ form: Form = .rasmUthmani, includeCitation: Bool = false) -> String { var s = QvpStr(); qvp_selection_text(p, UInt8(truncatingIfNeeded: form.rawValue), includeCitation ? 1 : 0, &s); return s.string }
|
|
365
|
+
|
|
366
|
+
// ── memorisation ──
|
|
367
|
+
/// Mask the words a target resolves to.
|
|
368
|
+
public func mask(_ t: Target, _ mode: MaskMode = .hide) { t.withC { qvp_mask(p, $0, UInt8(truncatingIfNeeded: mode.rawValue)) } }
|
|
369
|
+
/// Mask the words a target resolves to.
|
|
370
|
+
public func mask(_ s: String, _ mode: MaskMode = .hide) { mask(target(s), mode) }
|
|
371
|
+
/// Mask every word from `wordIndex` to the end of the page.
|
|
372
|
+
public func maskFrom(_ wordIndex: Int, _ mode: MaskMode = .hide) { qvp_mask_from(p, UInt32(truncatingIfNeeded: wordIndex), UInt8(truncatingIfNeeded: mode.rawValue)) }
|
|
373
|
+
public func maskOptions(blockColor: UInt32 = QvpDefaults.MASK_BLOCK, padX: Float = QvpDefaults.MASK_PAD, padY: Float = QvpDefaults.MASK_PAD, radius: Float = QvpDefaults.MASK_RADIUS, reverse: Bool = false) { qvp_mask_options(p, blockColor, padX, padY, radius, reverse ? 1 : 0) }
|
|
374
|
+
/// Fade `.hide` words in and out over `ms` on the engine clock (0 = instant). Reset by `unmask()`.
|
|
375
|
+
public func maskTransition(_ ms: Int) { qvp_mask_transition(p, UInt32(truncatingIfNeeded: max(ms, 0))) }
|
|
376
|
+
/// Unhide the next `n` hidden words, in mask order; returns how many it unhid.
|
|
377
|
+
@discardableResult public func unmaskNext(_ n: Int = 1) -> Int { Int(qvp_unmask_next(p, UInt32(truncatingIfNeeded: n))) }
|
|
378
|
+
/// Hide again the last `n` shown words of the mask, walking back from its end; returns how many it hid.
|
|
379
|
+
@discardableResult public func maskBack(_ n: Int = 1) -> Int { Int(qvp_mask_back(p, UInt32(truncatingIfNeeded: n))) }
|
|
380
|
+
/// Unhide one word.
|
|
381
|
+
@discardableResult public func unmaskWord(_ wordIndex: Int) -> Bool { qvp_unmask_word(p, UInt32(truncatingIfNeeded: wordIndex)) != 0 }
|
|
382
|
+
/// Hide one word again.
|
|
383
|
+
@discardableResult public func maskWord(_ wordIndex: Int) -> Bool { qvp_mask_word(p, UInt32(truncatingIfNeeded: wordIndex)) != 0 }
|
|
384
|
+
public func unmaskAll() { qvp_unmask_all(p) }
|
|
385
|
+
public func maskAll() { qvp_mask_all(p) }
|
|
386
|
+
public func unmask() { qvp_unmask(p) }
|
|
387
|
+
public func maskHidden() -> [Int] { collect(512) { o, c in qvp_mask_hidden(p, o, c) }.map { (w: UInt32) in Int(w) } }
|
|
388
|
+
public func maskWords() -> [Int] { collect(512) { o, c in qvp_mask_words(p, o, c) }.map { (w: UInt32) in Int(w) } }
|
|
389
|
+
/// Block/blur boxes in viewport px, drawn over the ink.
|
|
390
|
+
public func maskBoxesView() -> [QvpBox] { boxes(collect(128) { o, c in qvp_mask_boxes_view(p, o, c) }) }
|
|
391
|
+
/// Greyed page with a lit window; returns steps.
|
|
392
|
+
@discardableResult public func revealStart(lit: Int = QvpDefaults.REVEAL_LIT, byAyah: Bool = false, grey: UInt32 = QvpDefaults.REVEAL_GREY, ink: UInt32 = QvpDefaults.INK, ayahMarks: Bool = true, transitionMs: Int = 0) -> Int {
|
|
393
|
+
Int(qvp_reveal_start(p, UInt32(truncatingIfNeeded: lit), byAyah ? 1 : 0, grey, ink, ayahMarks ? 1 : 0, UInt32(truncatingIfNeeded: transitionMs)))
|
|
394
|
+
}
|
|
395
|
+
@discardableResult public func revealGoto(_ at: Int) -> Bool { qvp_reveal_goto(p, Int64(at)) != 0 }
|
|
396
|
+
/// Current step, -1 when nothing is lit yet, nil when no reveal is running.
|
|
397
|
+
public func revealPosition() -> Int? { let v = qvp_reveal_position(p); return v == -2 ? nil : Int(v) }
|
|
398
|
+
public func revealStepCount() -> Int { Int(qvp_reveal_step_count(p)) }
|
|
399
|
+
/// The step that lights word `wordIndex`.
|
|
400
|
+
public func revealStepOf(_ wordIndex: Int) -> Int { Int(qvp_reveal_step_of(p, UInt32(truncatingIfNeeded: wordIndex))) }
|
|
401
|
+
public func revealStop() { qvp_reveal_stop(p) }
|
|
402
|
+
|
|
403
|
+
// ── crop ──
|
|
404
|
+
public func cropBounds(_ t: Target, pad: Float = QvpDefaults.CROP_PAD, keepAyahMarks: Bool = true) -> QvpCropBounds? {
|
|
405
|
+
var b = QvpFFI.QvpCropBounds()
|
|
406
|
+
guard t.withC({ qvp_crop_bounds(p, $0, pad, keepAyahMarks ? 1 : 0, &b) }) != 0 else { return nil }
|
|
407
|
+
return QvpCropBounds(x0: b.x0, y0: b.y0, x1: b.x1, y1: b.y1, nWords: Int(b.n_words), ayahMarkDecoration: index(b.ayah_mark_decoration))
|
|
408
|
+
}
|
|
409
|
+
public func cropBounds(_ s: String, pad: Float = QvpDefaults.CROP_PAD, keepAyahMarks: Bool = true) -> QvpCropBounds? { cropBounds(target(s), pad: pad, keepAyahMarks: keepAyahMarks) }
|
|
410
|
+
/// Standalone SVG with the current colours; background alpha 0 = transparent.
|
|
411
|
+
public func cropSvg(_ t: Target, pad: Float = QvpDefaults.CROP_PAD, keepAyahMarks: Bool = true, background: UInt32 = 0) -> String? {
|
|
412
|
+
var s = QvpStr()
|
|
413
|
+
return t.withC({ qvp_crop_svg(p, $0, pad, keepAyahMarks ? 1 : 0, background, &s) }) != 0 ? s.string : nil
|
|
414
|
+
}
|
|
415
|
+
public func cropSvg(_ key: String, pad: Float = QvpDefaults.CROP_PAD, keepAyahMarks: Bool = true, background: UInt32 = 0) -> String? { cropSvg(target(key), pad: pad, keepAyahMarks: keepAyahMarks, background: background) }
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
/// Cross-page lookup from atlas.qva.
|
|
419
|
+
public final class QvpAtlas {
|
|
420
|
+
private var h: OpaquePointer?
|
|
421
|
+
public init(bytes: Data) throws {
|
|
422
|
+
h = bytes.withUnsafeBytes { qvp_atlas_load($0.bindMemory(to: UInt8.self).baseAddress, $0.count) }
|
|
423
|
+
if h == nil { throw QvpError.badAtlas }
|
|
424
|
+
}
|
|
425
|
+
deinit { close() }
|
|
426
|
+
/// Free the native atlas. A second call does nothing.
|
|
427
|
+
public func close() { if let a = h { qvp_atlas_free(a); h = nil } }
|
|
428
|
+
/// The engine handle. Internal: the wrapper marshals through it, no app ever sees it.
|
|
429
|
+
/// A call on a closed atlas stops the program with the fatal error `QvpAtlas is closed`.
|
|
430
|
+
var p: OpaquePointer { guard let h else { fatalError("QvpAtlas is closed") }; return h }
|
|
431
|
+
private func surah(_ ok: Int32, _ s: QvpFFI.QvpAtlasSurah) -> QvpAtlasSurah? {
|
|
432
|
+
ok != 0 ? QvpAtlasSurah(number: Int(s.number), page: Int(s.first_page), ayahCount: Int(s.ayah_count), place: place(s.place), arabic: s.arabic.string, latin: s.latin.string, english: s.english.string) : nil
|
|
433
|
+
}
|
|
434
|
+
/// The page that prints `surah:ayah`, or nil.
|
|
435
|
+
public func pageOf(_ surah: Int, _ ayah: Int) -> Int? { let v = qvp_atlas_page_of(p, UInt16(truncatingIfNeeded: surah), UInt16(truncatingIfNeeded: ayah)); return v < 0 ? nil : Int(v) }
|
|
436
|
+
/// ((first surah, first ayah), (last surah, last ayah)) printed on the page.
|
|
437
|
+
public func pageRange(_ page: Int) -> (first: (Int, Int), last: (Int, Int))? {
|
|
438
|
+
var r: (UInt16, UInt16, UInt16, UInt16) = (0, 0, 0, 0)
|
|
439
|
+
let ok = withUnsafeMutablePointer(to: &r) { $0.withMemoryRebound(to: UInt16.self, capacity: 4) { qvp_atlas_page_range(p, UInt16(truncatingIfNeeded: page), $0) } }
|
|
440
|
+
return ok != 0 ? ((Int(r.0), Int(r.1)), (Int(r.2), Int(r.3))) : nil
|
|
441
|
+
}
|
|
442
|
+
public func pageCount() -> Int { Int(qvp_atlas_page_count(p)) }
|
|
443
|
+
/// Surah `n` and the page it starts on, or nil.
|
|
444
|
+
public func surah(_ n: Int) -> QvpAtlasSurah? { var s = QvpFFI.QvpAtlasSurah(); return surah(qvp_atlas_surah(p, UInt16(truncatingIfNeeded: n), &s), s) }
|
|
445
|
+
/// Every surah, in order.
|
|
446
|
+
public func surahs() -> [QvpAtlasSurah] { (0..<Int(qvp_atlas_surah_count(p))).compactMap { var s = QvpFFI.QvpAtlasSurah(); return surah(qvp_atlas_surah_at(p, UInt32(truncatingIfNeeded: $0), &s), s) } }
|
|
447
|
+
public func pageOfSurah(_ n: Int) -> Int? { surah(n)?.page }
|
|
448
|
+
public func division(_ division: Division, _ n: Int) -> QvpAtlasRubuAlHizb? {
|
|
449
|
+
var r = QvpFFI.QvpAtlasRubuAlHizb()
|
|
450
|
+
return qvp_atlas_division(p, UInt8(truncatingIfNeeded: division.rawValue), UInt16(truncatingIfNeeded: n), &r) != 0 ? QvpAtlasRubuAlHizb(rubuAlHizb: Int(r.rubu_al_hizb), surah: Int(r.surah), ayah: Int(r.ayah), page: Int(r.page)) : nil
|
|
451
|
+
}
|
|
452
|
+
public func juz(_ n: Int) -> QvpAtlasRubuAlHizb? { division(.juz, n) }
|
|
453
|
+
public func hizb(_ n: Int) -> QvpAtlasRubuAlHizb? { division(.hizb, n) }
|
|
454
|
+
public func rubuAlHizb(_ n: Int) -> QvpAtlasRubuAlHizb? { division(.rubuAlHizb, n) }
|
|
455
|
+
/// The number of the division that contains `surah:ayah`, or nil.
|
|
456
|
+
public func divisionOf(_ division: Division, _ surah: Int, _ ayah: Int) -> Int? { let v = qvp_atlas_division_of(p, UInt8(truncatingIfNeeded: division.rawValue), UInt16(truncatingIfNeeded: surah), UInt16(truncatingIfNeeded: ayah)); return v < 0 ? nil : Int(v) }
|
|
457
|
+
public func juzOf(_ surah: Int, _ ayah: Int) -> Int? { divisionOf(.juz, surah, ayah) }
|
|
458
|
+
public func pagesOfJuz(_ n: Int) -> (Int, Int)? {
|
|
459
|
+
var r: (UInt16, UInt16) = (0, 0)
|
|
460
|
+
let ok = withUnsafeMutablePointer(to: &r) { $0.withMemoryRebound(to: UInt16.self, capacity: 2) { qvp_atlas_pages_of_juz(p, UInt16(truncatingIfNeeded: n), $0) } }
|
|
461
|
+
return ok != 0 ? (Int(r.0), Int(r.1)) : nil
|
|
462
|
+
}
|
|
463
|
+
/// 'cow' | 'البقرة' | '2' → matching surahs.
|
|
464
|
+
public func searchSurahs(_ text: String) -> [QvpAtlasSurah] {
|
|
465
|
+
let ns: [UInt16] = withBytes(text) { tp, tn in collect(128) { o, c in qvp_atlas_search_surahs(p, tp, tn, o, c) } }
|
|
466
|
+
return ns.compactMap { surah(Int($0)) }
|
|
467
|
+
}
|
|
468
|
+
public func json() -> String { var s = QvpStr(); qvp_atlas_json(p, &s); return s.string }
|
|
469
|
+
}
|