Scoop API

scoop:scoop.graphics.text:0.1.0

src/layout.scoop

Selected production source · src/layout.scoop

1package scoop.graphics.text
3import scoop.graphics.*
4import scoop.graphics.native.withSkiaCanvas
6/** Half-open UTF-8 byte range, checked against the text by layout queries. */
7@CLayout
8public struct TextRange(val start: Long, val end: Long)
10public struct TextPosition(
11 val byteOffset: Long,
12 val affinity: TextAffinity = TextAffinity.Downstream
15public struct TextBox(val rect: Rect, val direction: TextDirection)
17public 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: Boolean
28/** One immutable layout snapshot; all geometry is local logical coordinates. */
29public class TextLayout internal constructor(
30 internal val resource: TextResource,
31 public val content: StyledText
32): Closeable {
33 public val text: String get() = content.text
34 private val info = readLayoutInfo(resource.get())
35 public val size: Size = Size(info.width, info.height)
36 public val minIntrinsicWidth: Double = info.minimum
37 public val maxIntrinsicWidth: Double = info.maximum
38 public val alphabeticBaseline: Double = info.alphabetic
39 public val ideographicBaseline: Double = info.ideographic
40 public val didExceedMaxLines: Boolean = info.exceeded != 0
41 public val unresolvedGlyphCount: Int = info.unresolved
42 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.isClosed
46 /** Conservative bounds of the actual text ink, excluding externally drawn inline objects. */
47 public val paintBounds: Rect get() {
48 @Unsafe
49 {
50 val value = textPaintBounds(resource.get())
51 textResult(value.status, "query text paint bounds")
52 return value.rect
53 }
54 }
56 public fun hitTest(point: Point): TextPosition {
57 checkTextPoint(point)
58 @Unsafe
59 {
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.Upstream
66 else
67 TextAffinity.Downstream
68 )
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 @Unsafe
76 {
77 val value =
78 textCaret(
79 resource.get(),
80 position.byteOffset,
81 if (position.affinity == TextAffinity.Upstream)
82 0
83 else
84 1,
85 thickness
86 )
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 None
93 else
94 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 @Unsafe
101 {
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 -> @Unsafe
106 {
107 return@use readBoxes(boxes.get(), textBoxesCount(boxes.get()))
108 }
109 }
110 }
111 }
112 public override fun close() {
113 resource.close()
114 }
117/** Paints the same shaped runs used by hit testing and caret queries. */
118public fun Canvas.drawText(layout: TextLayout, origin: Point = Point(0.0, 0.0)) {
119 checkTextPoint(origin)
120 @Unsafe
121 {
122 val pointer = layout.resource.get()
123 withSkiaCanvas(independentTiles = true) {
124 canvas -> @Unsafe
125 {
126 textResult(textPaint(pointer, canvas, origin), "paint text layout")
127 }
128 }
129 }
132private fun readLayoutInfo(pointer: Ptr<Unit>): NativeLayoutInfo {
133 @Unsafe
134 {
135 return textInfo(pointer)
136 }
139@Extern(name = "scoop_graphics_text_paint_bounds")
140private fun textPaintBounds(layout: Ptr<Unit>): NativeCaret
142private fun readLines(pointer: Ptr<Unit>, count: Long): Array<TextLine> =
143 Array<TextLine>(count) {
144 index -> @Unsafe
145 {
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 != 0
156 )
157 }
158 }
160private fun readGraphemes(pointer: Ptr<Unit>, count: Long): Array<Long> {
161 val result = MutableArray<Long>(count) { _ -> 0L }
162 @Unsafe
163 {
164 result.withDataPointer {
165 data,
166 _ -> @Unsafe
167 {
168 textGraphemes(pointer, data)
169 }
170 }
171 }
172 return result.toArray()
175private fun readBoxes(pointer: Ptr<Unit>, count: Long): Array<TextBox> =
176 Array<TextBox>(count) {
177 index -> @Unsafe
178 {
179 val value = textBox(pointer, index)
180 return@Array TextBox(
181 value.rect,
182 if (value.direction == 1)
183 TextDirection.Ltr
184 else
185 TextDirection.Rtl
186 )
187 }
188 }
190@CLayout
191private 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: Int
204@CLayout
205private 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: Int
216@CLayout
217private struct NativeTextHit(val byteOffset: Long, val affinity: Int, val status: Int)
219@CLayout
220private struct NativeCaret(val rect: Rect, val status: Int)
222@CLayout
223private struct NativeTextBox(val rect: Rect, val direction: Int)
225@CLayout
226private struct NativeSelection(val pointer: Ptr<Unit>?, val status: Int)
228@Extern(name = "scoop_graphics_text_info")
229private fun textInfo(layout: Ptr<Unit>): NativeLayoutInfo
231@Extern(name = "scoop_graphics_text_line")
232private fun textLine(layout: Ptr<Unit>, index: Long): NativeTextLine
234@Extern(name = "scoop_graphics_text_graphemes")
235private fun textGraphemes(layout: Ptr<Unit>, output: Ptr<Long>)
237@Extern(name = "scoop_graphics_text_hit")
238private fun textHit(layout: Ptr<Unit>, point: Point): NativeTextHit
240@Extern(name = "scoop_graphics_text_caret")
241private fun textCaret(
242 layout: Ptr<Unit>,
243 byteOffset: Long,
244 affinity: Int,
245 thickness: Double
246): NativeCaret
248@Extern(name = "scoop_graphics_text_selection")
249private fun textSelection(layout: Ptr<Unit>, start: Long, end: Long): NativeSelection
251@Extern(name = "scoop_graphics_text_boxes_count")
252private fun textBoxesCount(boxes: Ptr<Unit>): Long
254@Extern(name = "scoop_graphics_text_box")
255private fun textBox(boxes: Ptr<Unit>, index: Long): NativeTextBox
257@Extern(name = "scoop_graphics_text_paint")
258private fun textPaint(layout: Ptr<Unit>, canvas: Ptr<Unit>, origin: Point): Int