scoop:scoop.ui:0.1.0
src/text/cache.scoop
Selected production source · src/text/cache.scoop
1
package scoop.ui.text3
import scoop.graphics.text.*4
import scoop.ui.cleanupUi5
import scoop.ui.throwUi6
import scoop.ui.observeFonts8
/** Owns the two native snapshots; replacement publishes only fully built results. */9
internal class ParagraphCache: Closeable {10
private var current: CachedParagraph? = None11
private var serial = 0L12
val generation: Long get() = serial14
val layout: TextLayout get() =15
(current ?: throw IllegalStateException(Some("paragraph has not been measured"))).layout17
fun retainLayout(): RetainedParagraphLayout =18
(19
current ?: throw IllegalStateException(Some("paragraph has not been measured"))20
).retained.retain()22
fun update(23
fonts: FontCollection,24
content: StyledText,25
settings: ParagraphStyle,26
width: Double?,27
metrics: Array<InlineMetrics>28
): TextLayout {29
observeFonts(fonts)30
when (current) {31
case Some(val previous) -> {32
if (previous.matches(fonts, content, settings)) {33
if (previous.width == width && equalMetrics(previous.metrics, metrics))34
return previous.layout35
val next = ParagraphLayout(createLayout(previous.paragraph, width, metrics))36
val old = previous.retained37
previous.retained = next38
previous.width = width39
previous.metrics = metrics40
serial += 1L41
old.release()42
return next.layout43
}44
}45
case None -> {}46
}47
val paragraph = Paragraph(fonts, content, settings)48
val next =49
try {50
CachedParagraph(51
fonts,52
fonts.revision,53
content,54
settings,55
paragraph,56
ParagraphLayout(createLayout(paragraph, width, metrics)),57
width,58
metrics59
)60
} catch (error: Throwable) {61
throwUi(cleanupUi(Some(error)) { paragraph.close() })62
throw error63
}64
val previous = current65
current = Some(next)66
serial += 1L67
previous?.close()68
return next.layout69
}71
public override fun close() {72
val previous = current73
current = None74
previous?.close()75
}76
}78
private class CachedParagraph(79
val fonts: FontCollection,80
val revision: Long,81
val content: StyledText,82
val settings: ParagraphStyle,83
val paragraph: Paragraph,84
var retained: ParagraphLayout,85
var width: Double?,86
var metrics: Array<InlineMetrics>87
): Closeable {88
val layout: TextLayout get() = retained.layout89
fun matches(source: FontCollection, text: StyledText, style: ParagraphStyle): Boolean =90
fonts ===91
source &&92
revision ==93
source.revision &&94
content ===95
text &&96
settings.alignment ==97
style.alignment &&98
settings.direction ==99
style.direction &&100
settings.maxLines ==101
style.maxLines &&102
settings.ellipsis ==103
style.ellipsis105
public override fun close() {106
var failure: Throwable? = None107
failure = cleanupUi(failure) { retained.release() }108
failure = cleanupUi(failure) { paragraph.close() }109
throwUi(failure)110
}111
}113
private fun createLayout(114
paragraph: Paragraph,115
width: Double?,116
metrics: Array<InlineMetrics>117
): TextLayout =118
when (width) {119
case Some(val value) -> paragraph.layout(value, metrics)120
case None -> paragraph.layoutUnwrapped(metrics)121
}123
private fun equalMetrics(left: Array<InlineMetrics>, right: Array<InlineMetrics>): Boolean {124
if (left.size != right.size)125
return false126
var index = 0L127
while (index < left.size) {128
if (left[index] != right[index])129
return false130
index += 1L131
}132
return true133
}