scoop:scoop.ui:0.1.0
src/text/editor/geometry.scoop
Selected production source · src/text/editor/geometry.scoop
1
package scoop.ui.text3
import scoop.graphics.*4
import scoop.graphics.text.*5
import scoop.ui.*7
/** Borrowed geometry shared by editing, painting and native caret publication. */8
public class TextEditLayout internal constructor(9
internal val paragraph: TextLayout,10
internal val cells: EditorCells? = None,11
private val resources: Array<RetainedParagraphLayout> = [],12
internal val generation: Long = 0L13
) {14
private var owners = 1L15
public val text: String get() = paragraph.text16
public val size: Size get() = cells?.size ?: paragraph.size17
public val alphabeticBaseline: Double get() =18
cells?.alphabeticBaseline ?: paragraph.alphabeticBaseline19
public val ideographicBaseline: Double get() =20
cells?.ideographicBaseline ?: paragraph.ideographicBaseline21
public val graphemeBoundaries: Array<Long> get() = paragraph.graphemeBoundaries22
public val lines: Array<TextLine> get() = cells?.lines ?: paragraph.lines23
public val cellRects: Array<Rect> get() = cells?.rects ?: []24
public val isClosed: Boolean get() = owners == 0L || paragraph.isClosed25
public val paintBounds: Rect get() {26
checkOpen()27
return when (cells) {28
case Some(val value) -> Rect(0.0, 0.0, value.size.width, value.size.height)29
case None -> paragraph.paintBounds30
}31
}32
public fun cellIndex(position: TextPosition): Long? {33
checkOpen()34
return cells?.index(position)35
}36
public fun caret(position: TextPosition, thickness: Double = 1.0): Rect? {37
checkOpen()38
requireUi(thickness.isFinite() && thickness >= 0.0, "invalid caret thickness")39
return when (cells) {40
case Some(val value) -> Some(value.caret(position, thickness))41
case None -> paragraph.caret(position, thickness)42
}43
}44
public fun selection(range: TextRange): Array<TextBox> {45
checkOpen()46
return when (cells) {47
case Some(val value) -> value.selection(range)48
case None -> paragraph.selection(range)49
}50
}51
public fun hitTest(point: Point): TextPosition {52
checkOpen()53
requireUi(point.x.isFinite() && point.y.isFinite(), "invalid text hit point")54
return when (cells) {55
case Some(val value) -> value.hitTest(point)56
case None -> paragraph.hitTest(point)57
}58
}59
public fun wordRange(position: TextPosition): TextRange {60
checkOpen()61
return paragraph.wordRange(position)62
}63
public fun moveGrapheme(byteOffset: Long, forward: Boolean): Long {64
checkOpen()65
return paragraph.moveGrapheme(byteOffset, forward)66
}67
public fun moveWord(byteOffset: Long, forward: Boolean): Long {68
checkOpen()69
return paragraph.moveWord(byteOffset, forward)70
}71
public fun logicalLineRange(position: TextPosition): TextRange? {72
checkOpen()73
return when (cells) {74
case Some(val value) -> {75
value.checkPosition(position)76
Some(TextRange(0L, text.byteLength))77
}78
case None -> paragraph.logicalLineRange(position)79
}80
}81
public fun moveCaret(82
position: TextPosition,83
movement: CaretMovement,84
preferredX: Double? = None85
): CaretMove? {86
checkOpen()87
when (preferredX) {88
case Some(val value) -> requireUi(value.isFinite(), "invalid caret column")89
case None -> {}90
}91
return when (cells) {92
case Some(val value) -> Some(value.move(paragraph, position, movement, preferredX))93
case None -> paragraph.moveCaret(position, movement, preferredX)94
}95
}96
internal fun checkOpen() {97
if (isClosed)98
throw IllegalStateException(Some("text edit layout is closed"))99
}100
internal fun retain(): RetainedEditorLayout {101
checkOpen()102
owners += 1L103
return RetainedEditorLayout(this)104
}105
internal fun release() {106
owners -= 1L107
if (owners != 0L)108
return109
var failure: Throwable? = None110
for (resource in resources)111
failure = cleanupUi(failure) { resource.close() }112
throwUi(failure)113
}114
}116
internal class RetainedEditorLayout(private val owner: TextEditLayout): Closeable {117
private var closed = false118
val layout: TextEditLayout get() {119
if (closed)120
throw IllegalStateException(Some("retained editor layout is closed"))121
return owner122
}123
public override fun close() {124
if (closed)125
return126
closed = true127
owner.release()128
}129
}131
/** Uses exactly the same cell placements as caret, selection and hit testing. */132
public fun Canvas.drawText(layout: TextEditLayout, origin: Point = Point(0.0, 0.0)) {133
layout.checkOpen()134
requireUi(origin.x.isFinite() && origin.y.isFinite(), "invalid text origin")135
when (layout.cells) {136
case Some(val value) -> {137
for (index in 0L ..< value.glyphs.size) {138
val cell = value.rects[index]139
val offset = value.origins[index]140
save()141
try {142
clipRect(Rect(cell.x + origin.x, cell.y + origin.y, cell.width, cell.height))143
drawText(value.glyphs[index], Point(offset.x + origin.x, offset.y + origin.y))144
} finally {145
restore()146
}147
}148
}149
case None -> drawText(layout.paragraph, origin)150
}151
}