Scoop API

scoop:scoop.ui:0.1.0

src/text/editor/cells.scoop

Selected production source · src/text/editor/cells.scoop

1package scoop.ui.text
3import scoop.graphics.*
4import scoop.graphics.text.*
5import scoop.math.abs
6import scoop.math.max
7import scoop.math.min
8import scoop.ui.*
9import scoop.window.Utf8Range
11/** Logical cell order and spacing; component-specific borders stay with the caller. */
12public class TextEditCells public constructor(
13 public val count: Long,
14 public val cellSize: Size,
15 public val gaps: Array<Double> = [],
16 public val direction: LayoutDirection = LayoutDirection.Ltr
17) {
18 init {
19 requireUi(count > 0L, "cell count must be positive")
20 requireUi(
21 cellSize.width.isFinite() &&
22 cellSize.height.isFinite() &&
23 cellSize.width >
24 0.0 &&
25 cellSize.height >
26 0.0,
27 "invalid text cell size"
28 )
29 requireUi(gaps.size == 0L || gaps.size == count - 1L, "text cell gaps must match count")
30 for (gap in gaps)
31 requireUi(gap.isFinite() && gap >= 0.0, "invalid text cell gap")
32 }
35internal class EditorCells(
36 val paragraph: TextLayout,
37 val glyphs: Array<TextLayout>,
38 val plan: TextEditCells
39) {
40 private val bytes = paragraph.text.toByteArray()
41 private val boundaries = paragraph.graphemeBoundaries
42 private val count = max(plan.count, glyphs.size)
43 private val rtl = plan.direction == LayoutDirection.Rtl
44 val size = Size(width(), plan.cellSize.height)
45 val rects = rectangles()
46 val origins = Array<Point>(glyphs.size) { index -> origin(index) }
47 private val lineHeight = paragraph.lines[0L].rect.height
48 private val textTop = (size.height - lineHeight) / 2.0
49 val alphabeticBaseline = textTop + paragraph.alphabeticBaseline
50 val ideographicBaseline = textTop + paragraph.ideographicBaseline
51 val lines =
52 [
53 TextLine(
54 TextRange(0L, paragraph.text.byteLength),
55 TextRange(0L, paragraph.text.byteLength),
56 TextRange(0L, paragraph.text.byteLength),
57 Rect(0.0, 0.0, size.width, size.height),
58 alphabeticBaseline,
59 paragraph.lines[0L].ascent,
60 paragraph.lines[0L].descent,
61 false
62 )
63 ]
64 private fun width(): Double {
65 var result = plan.cellSize.width * count.toDouble()
66 for (gap in plan.gaps)
67 result += gap
68 requireUi(result.isFinite(), "text cell extent is not finite")
69 return result
70 }
71 private fun rectangles(): Array<Rect> {
72 val result = ArrayList<Rect>()
73 var offset = 0.0
74 for (index in 0L ..< count) {
75 val x =
76 if (rtl)
77 size.width - offset - plan.cellSize.width
78 else
79 offset
80 result.add(Rect(x, 0.0, plan.cellSize.width, plan.cellSize.height))
81 offset += plan.cellSize.width
82 if (index < plan.gaps.size)
83 offset += plan.gaps[index]
84 }
85 return result.toArray()
86 }
87 private fun origin(index: Long): Point {
88 val cell = rects[index]
89 val glyph = glyphs[index]
90 return Point(
91 cell.x + (cell.width - glyph.size.width) / 2.0,
92 cell.y + (cell.height - glyph.size.height) / 2.0
93 )
94 }
95 fun checkPosition(position: TextPosition) {
96 checkEditorRange(bytes, Utf8Range(position.byteOffset, position.byteOffset))
97 }
98 fun index(position: TextPosition): Long {
99 checkPosition(position)
100 val boundary = editorBoundaryPosition(boundaries, position).byteOffset
101 var index = 0L
102 while (index + 1L < boundaries.size && boundaries[index] < boundary)
103 index += 1L
104 return min(index, count - 1L)
105 }
106 fun caret(position: TextPosition, thickness: Double): Rect {
107 checkPosition(position)
108 requireUi(
109 editorBoundaryPosition(boundaries, position) == position,
110 "caret must be a grapheme boundary"
111 )
112 val index = index(position)
113 val cell = rects[index]
114 val end = position.byteOffset == paragraph.text.byteLength
115 val empty = end && glyphs.size < count
116 val width =
117 if (empty)
118 0.0
119 else
120 min(cell.width, glyphs[index].size.width)
121 val edge =
122 if (end && !empty)
123 1.0
124 else
125 -1.0
126 val x =
127 cell.x +
128 cell.width /
129 2.0 +
130 width /
131 2.0 *
132 edge *
133 (
134 if (rtl)
135 -1.0
136 else
137 1.0
138 )
139 val height = min(cell.height, lineHeight)
140 val stroke = min(thickness, cell.width)
141 return Rect(
142 min(max(x - stroke / 2.0, cell.x), cell.x + cell.width - stroke),
143 (cell.height - height) / 2.0,
144 stroke,
145 height
146 )
147 }
148 fun hitTest(point: Point): TextPosition {
149 var selected = TextPosition(0L)
150 var distance = abs(point.x - caret(selected, 0.0).x)
151 for (boundary in boundaries) {
152 val candidate = TextPosition(boundary)
153 val location = caret(candidate, 0.0)
154 val delta = abs(point.x - location.x)
155 if (delta < distance) {
156 selected = candidate
157 distance = delta
158 }
159 }
160 return selected
161 }
162 fun selection(range: TextRange): Array<TextBox> {
163 checkEditorRange(bytes, Utf8Range(range.start, range.end))
164 val result = ArrayList<TextBox>()
165 for (index in 0L ..< glyphs.size) {
166 if (boundaries[index] < range.end && boundaries[index + 1L] > range.start)
167 result.add(
168 TextBox(
169 rects[index],
170 if (rtl)
171 TextDirection.Rtl
172 else
173 TextDirection.Ltr
174 )
175 )
176 }
177 return result.toArray()
178 }
179 fun move(
180 source: TextLayout,
181 position: TextPosition,
182 movement: CaretMovement,
183 preferredX: Double?
184 ): CaretMove {
185 checkPosition(position)
186 val current = editorBoundaryPosition(boundaries, position)
187 val next =
188 when (movement) {
189 case Left -> source.moveGrapheme(current.byteOffset, rtl)
190 case Right -> source.moveGrapheme(current.byteOffset, !rtl)
191 case WordLeft -> source.moveWord(current.byteOffset, rtl)
192 case WordRight -> source.moveWord(current.byteOffset, !rtl)
193 case LineStart -> 0L
194 case LineEnd -> source.text.byteLength
195 case DocumentStart -> 0L
196 case DocumentEnd -> source.text.byteLength
197 case LineLeft -> if (rtl)
198 source.text.byteLength
199 else
200 0L
201 case LineRight -> if (rtl)
202 0L
203 else
204 source.text.byteLength
205 case Up -> current.byteOffset
206 case Down -> current.byteOffset
207 }
208 val target = TextPosition(next)
209 return CaretMove(target, preferredX ?: caret(target, 0.0).x)
210 }
213internal fun sameEditorCells(left: TextEditCells?, right: TextEditCells?): Boolean {
214 return when (left) {
215 case None -> when (right) {
216 case None -> true;
217 case Some(_) -> false
218 }
219 case Some(val a) -> when (right) {
220 case None -> false
221 case Some(val b) -> {
222 if (
223 a.count !=
224 b.count ||
225 a.cellSize !=
226 b.cellSize ||
227 a.direction !=
228 b.direction ||
229 a.gaps.size !=
230 b.gaps.size
231 )
232 return false
233 for (index in 0L ..< a.gaps.size)
234 if (a.gaps[index] != b.gaps[index])
235 return false
236 true
237 }
238 }
239 }