scoop:scoop.graphics.text:0.1.0
src/layout.scoop
Selected production source · src/layout.scoop
1
package scoop.graphics.text3
import scoop.graphics.*4
import scoop.graphics.native.withSkiaCanvas6
/** Half-open UTF-8 byte range, checked against the text by layout queries. */7
@CLayout8
public struct TextRange(val start: Long, val end: Long)10
public struct TextPosition(11
val byteOffset: Long,12
val affinity: TextAffinity = TextAffinity.Downstream13
)15
public struct TextBox(val rect: Rect, val direction: TextDirection)17
public struct TextLine(18
val range: TextRange,19
val contentRange: TextRange,20
val visibleRange: TextRange,21
val rect: Rect,22
val baseline: Double,23
val ascent: Double,24
val descent: Double,25
val hardBreak: Boolean26
)28
/** One immutable layout snapshot; all geometry is local logical coordinates. */29
public class TextLayout internal constructor(30
internal val resource: TextResource,31
public val content: StyledText32
): Closeable {33
public val text: String get() = content.text34
private val info = readLayoutInfo(resource.get())35
public val size: Size = Size(info.width, info.height)36
public val minIntrinsicWidth: Double = info.minimum37
public val maxIntrinsicWidth: Double = info.maximum38
public val alphabeticBaseline: Double = info.alphabetic39
public val ideographicBaseline: Double = info.ideographic40
public val didExceedMaxLines: Boolean = info.exceeded != 041
public val unresolvedGlyphCount: Int = info.unresolved42
public val lines: Array<TextLine> = readLines(resource.get(), info.lines)43
public val graphemeBoundaries: Array<Long> = readGraphemes(resource.get(), info.graphemes)44
public val inlineObjects: Array<InlinePlacement> = readInlinePlacements(resource.get(), content)45
public val isClosed: Boolean get() = resource.isClosed46
/** Conservative bounds of the actual text ink, excluding externally drawn inline objects. */47
public val paintBounds: Rect get() {48
@Unsafe49
{50
val value = textPaintBounds(resource.get())51
textResult(value.status, "query text paint bounds")52
return value.rect53
}54
}56
public fun hitTest(point: Point): TextPosition {57
checkTextPoint(point)58
@Unsafe59
{60
val value = textHit(resource.get(), point)61
textResult(value.status, "text position hit test")62
return TextPosition(63
value.byteOffset,64
if (value.affinity == 0)65
TextAffinity.Upstream66
else67
TextAffinity.Downstream68
)69
}70
}71
/** Requires a grapheme boundary; elided text has no caret geometry. */72
public fun caret(position: TextPosition, thickness: Double = 1.0): Rect? {73
checkTextScalar(thickness)74
requireText(thickness >= 0.0, "negative caret thickness")75
@Unsafe76
{77
val value =78
textCaret(79
resource.get(),80
position.byteOffset,81
if (position.affinity == TextAffinity.Upstream)82
083
else84
1,85
thickness86
)87
if (value.status == -1)88
throw IllegalArgumentException(Some("caret must be a grapheme boundary"))89
if (value.status == -2)90
throw GraphicsException("query caret geometry")91
return if (value.status == 0)92
None93
else94
Some(value.rect)95
}96
}97
/** A logical selection can have several visual rectangles in bidirectional text. */98
public fun selection(range: TextRange): Array<TextBox> {99
val pointer = resource.get()100
@Unsafe101
{102
val selected = textSelection(pointer, range.start, range.end)103
textResult(selected.status, "selection range must use UTF-8 scalar boundaries")104
return ownedText(selected.pointer, "query text selection").use {105
boxes -> @Unsafe106
{107
return@use readBoxes(boxes.get(), textBoxesCount(boxes.get()))108
}109
}110
}111
}112
public override fun close() {113
resource.close()114
}115
}117
/** Paints the same shaped runs used by hit testing and caret queries. */118
public fun Canvas.drawText(layout: TextLayout, origin: Point = Point(0.0, 0.0)) {119
checkTextPoint(origin)120
@Unsafe121
{122
val pointer = layout.resource.get()123
withSkiaCanvas(independentTiles = true) {124
canvas -> @Unsafe125
{126
textResult(textPaint(pointer, canvas, origin), "paint text layout")127
}128
}129
}130
}132
private fun readLayoutInfo(pointer: Ptr<Unit>): NativeLayoutInfo {133
@Unsafe134
{135
return textInfo(pointer)136
}137
}139
@Extern(name = "scoop_graphics_text_paint_bounds")140
private fun textPaintBounds(layout: Ptr<Unit>): NativeCaret142
private fun readLines(pointer: Ptr<Unit>, count: Long): Array<TextLine> =143
Array<TextLine>(count) {144
index -> @Unsafe145
{146
val value = textLine(pointer, index)147
return@Array TextLine(148
value.range,149
value.content,150
value.visible,151
value.rect,152
value.baseline,153
value.ascent,154
value.descent,155
value.hardBreak != 0156
)157
}158
}160
private fun readGraphemes(pointer: Ptr<Unit>, count: Long): Array<Long> {161
val result = MutableArray<Long>(count) { _ -> 0L }162
@Unsafe163
{164
result.withDataPointer {165
data,166
_ -> @Unsafe167
{168
textGraphemes(pointer, data)169
}170
}171
}172
return result.toArray()173
}175
private fun readBoxes(pointer: Ptr<Unit>, count: Long): Array<TextBox> =176
Array<TextBox>(count) {177
index -> @Unsafe178
{179
val value = textBox(pointer, index)180
return@Array TextBox(181
value.rect,182
if (value.direction == 1)183
TextDirection.Ltr184
else185
TextDirection.Rtl186
)187
}188
}190
@CLayout191
private struct NativeLayoutInfo(192
val width: Double,193
val height: Double,194
val minimum: Double,195
val maximum: Double,196
val alphabetic: Double,197
val ideographic: Double,198
val lines: Long,199
val graphemes: Long,200
val exceeded: Int,201
val unresolved: Int202
)204
@CLayout205
private struct NativeTextLine(206
val range: TextRange,207
val content: TextRange,208
val visible: TextRange,209
val rect: Rect,210
val baseline: Double,211
val ascent: Double,212
val descent: Double,213
val hardBreak: Int214
)216
@CLayout217
private struct NativeTextHit(val byteOffset: Long, val affinity: Int, val status: Int)219
@CLayout220
private struct NativeCaret(val rect: Rect, val status: Int)222
@CLayout223
private struct NativeTextBox(val rect: Rect, val direction: Int)225
@CLayout226
private struct NativeSelection(val pointer: Ptr<Unit>?, val status: Int)228
@Extern(name = "scoop_graphics_text_info")229
private fun textInfo(layout: Ptr<Unit>): NativeLayoutInfo231
@Extern(name = "scoop_graphics_text_line")232
private fun textLine(layout: Ptr<Unit>, index: Long): NativeTextLine234
@Extern(name = "scoop_graphics_text_graphemes")235
private fun textGraphemes(layout: Ptr<Unit>, output: Ptr<Long>)237
@Extern(name = "scoop_graphics_text_hit")238
private fun textHit(layout: Ptr<Unit>, point: Point): NativeTextHit240
@Extern(name = "scoop_graphics_text_caret")241
private fun textCaret(242
layout: Ptr<Unit>,243
byteOffset: Long,244
affinity: Int,245
thickness: Double246
): NativeCaret248
@Extern(name = "scoop_graphics_text_selection")249
private fun textSelection(layout: Ptr<Unit>, start: Long, end: Long): NativeSelection251
@Extern(name = "scoop_graphics_text_boxes_count")252
private fun textBoxesCount(boxes: Ptr<Unit>): Long254
@Extern(name = "scoop_graphics_text_box")255
private fun textBox(boxes: Ptr<Unit>, index: Long): NativeTextBox257
@Extern(name = "scoop_graphics_text_paint")258
private fun textPaint(layout: Ptr<Unit>, canvas: Ptr<Unit>, origin: Point): Int