Scoop API

scoop:scoop.ui:0.1.0

src/text/native_semantics.scoop

Selected production source · src/text/native_semantics.scoop

1package scoop.ui.text
3import scoop.graphics.Rect
4import scoop.graphics.native.NativeGeometry
5import scoop.graphics.text.TextLayout
6import scoop.graphics.text.native.*
7import scoop.window.accessibility.native.*
9@CLayout
10internal struct SemanticTextMapping(
11 val sourceStart: Long,
12 val sourceEnd: Long,
13 val displayStart: Long,
14 val displayEnd: Long
17@CLayout
18internal struct SemanticTextLine(
19 val start: Long,
20 val end: Long,
21 val visibleStart: Long,
22 val visibleEnd: Long
25@CLayout
26internal struct SemanticTextCell(
27 val start: Long,
28 val end: Long,
29 val bounds: Rect,
30 val caret: Rect,
31 val hitX: Double
34@CLayout
35private struct SemanticTextData(
36 val sourceLength: Long,
37 val displayLength: Long,
38 val cells: Int,
39 val expand: Int,
40 val caretWidth: Double,
41 val caretAspect: Double,
42 val graphemes: Ptr<Long>,
43 val graphemeCount: Long,
44 val mapping: Ptr<SemanticTextMapping>,
45 val mappingCount: Long,
46 val lines: Ptr<SemanticTextLine>,
47 val lineCount: Long,
48 val cellValues: Ptr<SemanticTextCell>,
49 val cellCount: Long
52@CLayout
53private struct SemanticTextExport(
54 val context: Ptr<Unit>?,
55 val retain: FunPtr<(Ptr<Unit>) -> Unit>,
56 val release: FunPtr<(Ptr<Unit>) -> Unit>,
57 val range: FunPtr<(Ptr<Unit>, Long, Int, Int) -> NativeSemanticTextRange>,
58 val lineRange: FunPtr<(Ptr<Unit>, Long) -> NativeSemanticTextRange>,
59 val lineIndex: FunPtr<(Ptr<Unit>, Long, Int) -> Long>,
60 val rangeAtPoint: FunPtr<(Ptr<Unit>, Double, Double) -> NativeSemanticTextRange>,
61 val boxes: FunPtr<
62 (
63 Ptr<Unit>,
64 Long,
65 Long,
66 Ptr<Unit>,
67 FunPtr<(Ptr<Unit>, Double, Double, Double, Double) -> Unit>
68 ) -> Int
69 >,
70 val visibleRange: FunPtr<(Ptr<Unit>) -> NativeSemanticTextRange>
73/** Native layout and prepared mappings, reused across scene-only updates. */
74internal class NativeTextSource(private val pointer: Ptr<Unit>): Closeable {
75 private var closed = false
76 val isClosed: Boolean get() = closed
78 @Unsafe
79 fun <T> withGeometry(scene: NativeGeometry, block: (NativeSemanticTextGeometry) -> T): T {
80 if (isClosed)
81 throw IllegalStateException(Some("semantic text source is closed"))
82 val value = nativeTextGeometry(pointer, scene)
83 val context =
84 value.context ?:
85 throw IllegalStateException(Some("cannot retain semantic text geometry"))
86 try {
87 return block(
88 NativeSemanticTextGeometry(
89 context,
90 value.retain,
91 value.release,
92 value.range,
93 value.lineRange,
94 value.lineIndex,
95 value.rangeAtPoint,
96 value.boxes,
97 value.visibleRange
98 )
99 )
100 } finally {
101 value.release(context)
102 }
103 }
105 public override fun close() {
106 if (isClosed)
107 return
108 closed = true
109 @Unsafe
110 {
111 nativeTextRelease(pointer)
112 }
113 }
116internal fun semanticTextSource(
117 layout: TextLayout,
118 sourceLength: Long,
119 graphemes: Array<Long>,
120 mapping: Array<SemanticTextMapping>,
121 lines: Array<SemanticTextLine>,
122 cells: Array<SemanticTextCell> = [],
123 expand: Boolean = false,
124 caretWidth: Double = 1.0,
125 caretAspect: Double = 0.0
126): NativeTextSource {
127 @Unsafe
128 {
129 return layout.withNativeLayout {
130 native -> @Unsafe
131 {
132 return@withNativeLayout graphemes.withDataPointer {
133 boundaries,
134 boundaryCount -> @Unsafe
135 {
136 return@withDataPointer mapping.withDataPointer {
137 replacements,
138 replacementCount -> @Unsafe
139 {
140 return@withDataPointer lines.withDataPointer {
141 rows,
142 rowCount -> @Unsafe
143 {
144 return@withDataPointer cells.withDataPointer {
145 values,
146 valueCount -> @Unsafe
147 {
148 val data =
149 SemanticTextData(
150 sourceLength,
151 layout.text.byteLength,
152 if (valueCount > 0L)
153 1
154 else
155 0,
156 if (expand)
157 1
158 else
159 0,
160 caretWidth,
161 caretAspect,
162 boundaries,
163 boundaryCount,
164 replacements,
165 replacementCount,
166 rows,
167 rowCount,
168 values,
169 valueCount
170 )
171 return@withDataPointer NativeTextSource(
172 nativeTextSource(native, data) ?:
173 throw IllegalStateException(
174 Some(
175 "cannot retain semantic text source"
176 )
177 )
178 )
179 }
180 }
181 }
182 }
183 }
184 }
185 }
186 }
187 }
188 }
189 }
192@Extern(name = "scoop_ui_semantic_text_source")
193private fun nativeTextSource(layout: NativeTextLayout, data: SemanticTextData): Ptr<Unit>?
195@Extern(name = "scoop_ui_semantic_text_source_release")
196private fun nativeTextRelease(source: Ptr<Unit>): Unit
198@Extern(name = "scoop_ui_semantic_text_geometry")
199private fun nativeTextGeometry(source: Ptr<Unit>, scene: NativeGeometry): SemanticTextExport