scoop:scoop.graphics.text:0.1.0
src/navigation.scoop
Selected production source · src/navigation.scoop
1
package scoop.graphics.text3
import scoop.graphics.GraphicsException5
/** Physical character/word movement and explicit logical or physical line edges. */6
public enum CaretMovement {7
Left, Right, Up, Down, LineStart, LineEnd, DocumentStart, DocumentEnd,8
WordLeft, WordRight, LineLeft, LineRight9
}11
/** The preferred column survives vertical movement through shorter lines. */12
public struct CaretMove(val position: TextPosition, val preferredX: Double)14
/** Unicode word segment containing a scalar position; affinity resolves an exact boundary. */15
public fun TextLayout.wordRange(position: TextPosition): TextRange {16
@Unsafe17
{18
val value =19
textWordRange(20
resource.get(),21
position.byteOffset,22
navigationAffinity(position.affinity)23
)24
checkNavigation(value.status)25
return value.range26
}27
}29
/** Moves to a segment edge, skipping intervening Unicode whitespace segments. */30
public fun TextLayout.moveWord(byteOffset: Long, forward: Boolean): Long {31
@Unsafe32
{33
val value = textMoveWord(resource.get(), byteOffset, forward)34
checkNavigation(value.status)35
return value.range.start36
}37
}39
/** Accepts scalar boundaries, including positions inside a grapheme. */40
public fun TextLayout.moveGrapheme(byteOffset: Long, forward: Boolean): Long {41
@Unsafe42
{43
val value = textMoveGrapheme(resource.get(), byteOffset, forward)44
checkNavigation(value.status)45
return value.range.start46
}47
}49
/** Uses the actual shaped glyph edges and keeps affinity across visual lines. */50
public fun TextLayout.moveCaret(51
position: TextPosition,52
movement: CaretMovement,53
preferredX: Double? = None54
): CaretMove? {55
when (preferredX) {56
case Some(val value) -> checkTextScalar(value)57
case None -> {}58
}59
@Unsafe60
{61
val value =62
textMoveCaret(63
resource.get(),64
position.byteOffset,65
navigationAffinity(position.affinity),66
navigationMovement(movement),67
preferredX != None,68
preferredX ?: 0.069
)70
checkNavigation(value.status)71
if (value.status == 0)72
return None73
return Some(74
CaretMove(75
TextPosition(76
value.byteOffset,77
if (value.affinity == 0)78
TextAffinity.Upstream79
else80
TextAffinity.Downstream81
),82
value.preferredX83
)84
)85
}86
}88
/** Includes the hard break and all soft-wrapped parts of a logical line. */89
public fun TextLayout.logicalLineRange(position: TextPosition): TextRange? {90
@Unsafe91
{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
None101
else102
Some(value.range)103
}104
}106
private 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")111
}113
private fun navigationAffinity(value: TextAffinity): Int =114
if (value == TextAffinity.Upstream)115
0116
else117
1119
private fun navigationMovement(value: CaretMovement): Int =120
when (value) {121
case Left -> 0122
case Right -> 1123
case Up -> 2124
case Down -> 3125
case LineStart -> 4126
case LineEnd -> 5127
case DocumentStart -> 6128
case DocumentEnd -> 7129
case WordLeft -> 8130
case WordRight -> 9131
case LineLeft -> 10132
case LineRight -> 11133
}135
@CLayout136
private struct NativeNavigationRange(val range: TextRange, val status: Int)138
@CLayout139
private struct NativeCaretMove(140
val byteOffset: Long,141
val preferredX: Double,142
val affinity: Int,143
val status: Int144
)146
@Extern(name = "scoop_graphics_text_word_range")147
private fun textWordRange(layout: Ptr<Unit>, byteOffset: Long, affinity: Int): NativeNavigationRange149
@Extern(name = "scoop_graphics_text_move_word")150
private fun textMoveWord(151
layout: Ptr<Unit>,152
byteOffset: Long,153
forward: Boolean154
): NativeNavigationRange156
@Extern(name = "scoop_graphics_text_move_grapheme")157
private fun textMoveGrapheme(158
layout: Ptr<Unit>,159
byteOffset: Long,160
forward: Boolean161
): NativeNavigationRange163
@Extern(name = "scoop_graphics_text_move_caret")164
private fun textMoveCaret(165
layout: Ptr<Unit>,166
byteOffset: Long,167
affinity: Int,168
movement: Int,169
hasX: Boolean,170
preferredX: Double171
): NativeCaretMove173
@Extern(name = "scoop_graphics_text_logical_line")174
private fun textLogicalLine(175
layout: Ptr<Unit>,176
byteOffset: Long,177
affinity: Int178
): NativeNavigationRange