Scoop API

scoop:scoop.graphics.text:0.1.0

src/navigation.scoop

Selected production source · src/navigation.scoop

1package scoop.graphics.text
3import scoop.graphics.GraphicsException
5/** Physical character/word movement and explicit logical or physical line edges. */
6public enum CaretMovement {
7 Left, Right, Up, Down, LineStart, LineEnd, DocumentStart, DocumentEnd,
8 WordLeft, WordRight, LineLeft, LineRight
9}
11/** The preferred column survives vertical movement through shorter lines. */
12public struct CaretMove(val position: TextPosition, val preferredX: Double)
14/** Unicode word segment containing a scalar position; affinity resolves an exact boundary. */
15public fun TextLayout.wordRange(position: TextPosition): TextRange {
16 @Unsafe
17 {
18 val value =
19 textWordRange(
20 resource.get(),
21 position.byteOffset,
22 navigationAffinity(position.affinity)
23 )
24 checkNavigation(value.status)
25 return value.range
26 }
29/** Moves to a segment edge, skipping intervening Unicode whitespace segments. */
30public fun TextLayout.moveWord(byteOffset: Long, forward: Boolean): Long {
31 @Unsafe
32 {
33 val value = textMoveWord(resource.get(), byteOffset, forward)
34 checkNavigation(value.status)
35 return value.range.start
36 }
39/** Accepts scalar boundaries, including positions inside a grapheme. */
40public fun TextLayout.moveGrapheme(byteOffset: Long, forward: Boolean): Long {
41 @Unsafe
42 {
43 val value = textMoveGrapheme(resource.get(), byteOffset, forward)
44 checkNavigation(value.status)
45 return value.range.start
46 }
49/** Uses the actual shaped glyph edges and keeps affinity across visual lines. */
50public fun TextLayout.moveCaret(
51 position: TextPosition,
52 movement: CaretMovement,
53 preferredX: Double? = None
54): CaretMove? {
55 when (preferredX) {
56 case Some(val value) -> checkTextScalar(value)
57 case None -> {}
58 }
59 @Unsafe
60 {
61 val value =
62 textMoveCaret(
63 resource.get(),
64 position.byteOffset,
65 navigationAffinity(position.affinity),
66 navigationMovement(movement),
67 preferredX != None,
68 preferredX ?: 0.0
69 )
70 checkNavigation(value.status)
71 if (value.status == 0)
72 return None
73 return Some(
74 CaretMove(
75 TextPosition(
76 value.byteOffset,
77 if (value.affinity == 0)
78 TextAffinity.Upstream
79 else
80 TextAffinity.Downstream
81 ),
82 value.preferredX
83 )
84 )
85 }
88/** Includes the hard break and all soft-wrapped parts of a logical line. */
89public fun TextLayout.logicalLineRange(position: TextPosition): TextRange? {
90 @Unsafe
91 {
92 val value =
93 textLogicalLine(
94 resource.get(),
95 position.byteOffset,
96 navigationAffinity(position.affinity)
97 )
98 checkNavigation(value.status)
99 return if (value.status == 0)
100 None
101 else
102 Some(value.range)
103 }
106private fun checkNavigation(status: Int) {
107 if (status == -1)
108 throw IllegalArgumentException(Some("invalid text navigation boundary"))
109 if (status == -2)
110 throw GraphicsException("query text navigation")
113private fun navigationAffinity(value: TextAffinity): Int =
114 if (value == TextAffinity.Upstream)
115 0
116 else
117 1
119private fun navigationMovement(value: CaretMovement): Int =
120 when (value) {
121 case Left -> 0
122 case Right -> 1
123 case Up -> 2
124 case Down -> 3
125 case LineStart -> 4
126 case LineEnd -> 5
127 case DocumentStart -> 6
128 case DocumentEnd -> 7
129 case WordLeft -> 8
130 case WordRight -> 9
131 case LineLeft -> 10
132 case LineRight -> 11
133 }
135@CLayout
136private struct NativeNavigationRange(val range: TextRange, val status: Int)
138@CLayout
139private struct NativeCaretMove(
140 val byteOffset: Long,
141 val preferredX: Double,
142 val affinity: Int,
143 val status: Int
146@Extern(name = "scoop_graphics_text_word_range")
147private fun textWordRange(layout: Ptr<Unit>, byteOffset: Long, affinity: Int): NativeNavigationRange
149@Extern(name = "scoop_graphics_text_move_word")
150private fun textMoveWord(
151 layout: Ptr<Unit>,
152 byteOffset: Long,
153 forward: Boolean
154): NativeNavigationRange
156@Extern(name = "scoop_graphics_text_move_grapheme")
157private fun textMoveGrapheme(
158 layout: Ptr<Unit>,
159 byteOffset: Long,
160 forward: Boolean
161): NativeNavigationRange
163@Extern(name = "scoop_graphics_text_move_caret")
164private fun textMoveCaret(
165 layout: Ptr<Unit>,
166 byteOffset: Long,
167 affinity: Int,
168 movement: Int,
169 hasX: Boolean,
170 preferredX: Double
171): NativeCaretMove
173@Extern(name = "scoop_graphics_text_logical_line")
174private fun textLogicalLine(
175 layout: Ptr<Unit>,
176 byteOffset: Long,
177 affinity: Int
178): NativeNavigationRange