scoop:scoop.ui:0.1.0
src/text/native_semantics.scoop
Selected production source · src/text/native_semantics.scoop
1
package scoop.ui.text3
import scoop.graphics.Rect4
import scoop.graphics.native.NativeGeometry5
import scoop.graphics.text.TextLayout6
import scoop.graphics.text.native.*7
import scoop.window.accessibility.native.*9
@CLayout10
internal struct SemanticTextMapping(11
val sourceStart: Long,12
val sourceEnd: Long,13
val displayStart: Long,14
val displayEnd: Long15
)17
@CLayout18
internal struct SemanticTextLine(19
val start: Long,20
val end: Long,21
val visibleStart: Long,22
val visibleEnd: Long23
)25
@CLayout26
internal struct SemanticTextCell(27
val start: Long,28
val end: Long,29
val bounds: Rect,30
val caret: Rect,31
val hitX: Double32
)34
@CLayout35
private 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: Long50
)52
@CLayout53
private 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
) -> Int69
>,70
val visibleRange: FunPtr<(Ptr<Unit>) -> NativeSemanticTextRange>71
)73
/** Native layout and prepared mappings, reused across scene-only updates. */74
internal class NativeTextSource(private val pointer: Ptr<Unit>): Closeable {75
private var closed = false76
val isClosed: Boolean get() = closed78
@Unsafe79
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.visibleRange98
)99
)100
} finally {101
value.release(context)102
}103
}105
public override fun close() {106
if (isClosed)107
return108
closed = true109
@Unsafe110
{111
nativeTextRelease(pointer)112
}113
}114
}116
internal 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.0126
): NativeTextSource {127
@Unsafe128
{129
return layout.withNativeLayout {130
native -> @Unsafe131
{132
return@withNativeLayout graphemes.withDataPointer {133
boundaries,134
boundaryCount -> @Unsafe135
{136
return@withDataPointer mapping.withDataPointer {137
replacements,138
replacementCount -> @Unsafe139
{140
return@withDataPointer lines.withDataPointer {141
rows,142
rowCount -> @Unsafe143
{144
return@withDataPointer cells.withDataPointer {145
values,146
valueCount -> @Unsafe147
{148
val data =149
SemanticTextData(150
sourceLength,151
layout.text.byteLength,152
if (valueCount > 0L)153
1154
else155
0,156
if (expand)157
1158
else159
0,160
caretWidth,161
caretAspect,162
boundaries,163
boundaryCount,164
replacements,165
replacementCount,166
rows,167
rowCount,168
values,169
valueCount170
)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
}190
}192
@Extern(name = "scoop_ui_semantic_text_source")193
private fun nativeTextSource(layout: NativeTextLayout, data: SemanticTextData): Ptr<Unit>?195
@Extern(name = "scoop_ui_semantic_text_source_release")196
private fun nativeTextRelease(source: Ptr<Unit>): Unit198
@Extern(name = "scoop_ui_semantic_text_geometry")199
private fun nativeTextGeometry(source: Ptr<Unit>, scene: NativeGeometry): SemanticTextExport