scoop:scoop.ui:0.1.0
src/text/editor/glyphs.scoop
Selected production source · src/text/editor/glyphs.scoop
1
package scoop.ui.text3
import scoop.graphics.text.*4
import scoop.ui.*5
import scoop.window.Utf8Range7
internal class EditorGlyphCache: Closeable {8
val body = ParagraphCache()9
private var content: StyledText? = None10
fun update(fonts: FontCollection, text: String, style: TextStyle, direction: TextDirection) {11
val previous = content12
val next =13
when (previous) {14
case Some(val value) -> if (value.text == text && value.style === style)15
value16
else17
styledText {18
this.text(text);19
this.style = style20
}21
case None -> styledText {22
this.text(text);23
this.style = style24
}25
}26
body.update(fonts, next, paragraphStyle { this.direction = direction }, None, [])27
content = Some(next)28
}29
public override fun close() {30
body.close()31
}32
}34
internal fun editorGeometry(35
host: TextEditHost,36
paragraph: TextLayout,37
plan: TextEditCells?38
): TextEditLayout {39
when (host.currentLayout) {40
case Some(val previous) -> if (41
previous.paragraph === paragraph && sameEditorCells(previous.cells?.plan, plan)42
)43
return previous44
case None -> {}45
}46
val retained = ArrayList<RetainedParagraphLayout>()47
val next =48
try {49
retained.add(host.body.retainLayout())50
val cells =51
when (plan) {52
case None -> None53
case Some(val value) -> {54
val bytes = paragraph.text.toByteArray()55
val boundaries = paragraph.graphemeBoundaries56
val count = boundaries.size - 1L57
while (host.glyphs.size < count)58
host.glyphs.add(EditorGlyphCache())59
val layouts = ArrayList<TextLayout>()60
for (index in 0L ..< count) {61
val glyph = host.glyphs[index]62
glyph.update(63
host.appearance.fonts,64
editorSlice(65
bytes,66
Utf8Range(boundaries[index], boundaries[index + 1L])67
),68
host.appearance.textStyle,69
value.direction.textDirection()70
)71
val saved = glyph.body.retainLayout()72
retained.add(saved)73
layouts.add(saved.layout)74
}75
Some(EditorCells(paragraph, layouts.toArray(), value))76
}77
}78
TextEditLayout(paragraph, cells, retained.toArray(), host.geometryGeneration + 1L)79
} catch (error: Throwable) {80
var failure: Throwable? = Some(error)81
for (resource in retained)82
failure = cleanupUi(failure) { resource.close() }83
throwUi(failure)84
throw error85
}86
val previous = host.currentLayout87
host.currentLayout = Some(next)88
host.geometryGeneration += 1L89
previous?.release()90
val count = next.cells?.glyphs?.size ?: 0L91
while (host.glyphs.size > count) {92
val last = host.glyphs[host.glyphs.size - 1L]93
host.glyphs.removeAt(host.glyphs.size - 1L)94
last.close()95
}96
return next97
}