scoop:scoop.ui:0.1.0
src/text/editor/cells.scoop
Selected production source · src/text/editor/cells.scoop
1
package scoop.ui.text3
import scoop.graphics.*4
import scoop.graphics.text.*5
import scoop.math.abs6
import scoop.math.max7
import scoop.math.min8
import scoop.ui.*9
import scoop.window.Utf8Range11
/** Logical cell order and spacing; component-specific borders stay with the caller. */12
public 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.Ltr17
) {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
}33
}35
internal class EditorCells(36
val paragraph: TextLayout,37
val glyphs: Array<TextLayout>,38
val plan: TextEditCells39
) {40
private val bytes = paragraph.text.toByteArray()41
private val boundaries = paragraph.graphemeBoundaries42
private val count = max(plan.count, glyphs.size)43
private val rtl = plan.direction == LayoutDirection.Rtl44
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.height48
private val textTop = (size.height - lineHeight) / 2.049
val alphabeticBaseline = textTop + paragraph.alphabeticBaseline50
val ideographicBaseline = textTop + paragraph.ideographicBaseline51
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
false62
)63
]64
private fun width(): Double {65
var result = plan.cellSize.width * count.toDouble()66
for (gap in plan.gaps)67
result += gap68
requireUi(result.isFinite(), "text cell extent is not finite")69
return result70
}71
private fun rectangles(): Array<Rect> {72
val result = ArrayList<Rect>()73
var offset = 0.074
for (index in 0L ..< count) {75
val x =76
if (rtl)77
size.width - offset - plan.cellSize.width78
else79
offset80
result.add(Rect(x, 0.0, plan.cellSize.width, plan.cellSize.height))81
offset += plan.cellSize.width82
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.093
)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).byteOffset101
var index = 0L102
while (index + 1L < boundaries.size && boundaries[index] < boundary)103
index += 1L104
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.byteLength115
val empty = end && glyphs.size < count116
val width =117
if (empty)118
0.0119
else120
min(cell.width, glyphs[index].size.width)121
val edge =122
if (end && !empty)123
1.0124
else125
-1.0126
val x =127
cell.x +128
cell.width /129
2.0 +130
width /131
2.0 *132
edge *133
(134
if (rtl)135
-1.0136
else137
1.0138
)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
height146
)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 = candidate157
distance = delta158
}159
}160
return selected161
}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.Rtl172
else173
TextDirection.Ltr174
)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 -> 0L194
case LineEnd -> source.text.byteLength195
case DocumentStart -> 0L196
case DocumentEnd -> source.text.byteLength197
case LineLeft -> if (rtl)198
source.text.byteLength199
else200
0L201
case LineRight -> if (rtl)202
0L203
else204
source.text.byteLength205
case Up -> current.byteOffset206
case Down -> current.byteOffset207
}208
val target = TextPosition(next)209
return CaretMove(target, preferredX ?: caret(target, 0.0).x)210
}211
}213
internal fun sameEditorCells(left: TextEditCells?, right: TextEditCells?): Boolean {214
return when (left) {215
case None -> when (right) {216
case None -> true;217
case Some(_) -> false218
}219
case Some(val a) -> when (right) {220
case None -> false221
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.size231
)232
return false233
for (index in 0L ..< a.gaps.size)234
if (a.gaps[index] != b.gaps[index])235
return false236
true237
}238
}239
}240
}