Scoop API

scoop:scoop.ui:0.1.0

src/text/cache.scoop

Selected production source · src/text/cache.scoop

1package scoop.ui.text
3import scoop.graphics.text.*
4import scoop.ui.cleanupUi
5import scoop.ui.throwUi
6import scoop.ui.observeFonts
8/** Owns the two native snapshots; replacement publishes only fully built results. */
9internal class ParagraphCache: Closeable {
10 private var current: CachedParagraph? = None
11 private var serial = 0L
12 val generation: Long get() = serial
14 val layout: TextLayout get() =
15 (current ?: throw IllegalStateException(Some("paragraph has not been measured"))).layout
17 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.layout
35 val next = ParagraphLayout(createLayout(previous.paragraph, width, metrics))
36 val old = previous.retained
37 previous.retained = next
38 previous.width = width
39 previous.metrics = metrics
40 serial += 1L
41 old.release()
42 return next.layout
43 }
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 metrics
59 )
60 } catch (error: Throwable) {
61 throwUi(cleanupUi(Some(error)) { paragraph.close() })
62 throw error
63 }
64 val previous = current
65 current = Some(next)
66 serial += 1L
67 previous?.close()
68 return next.layout
69 }
71 public override fun close() {
72 val previous = current
73 current = None
74 previous?.close()
75 }
78private 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.layout
89 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.ellipsis
105 public override fun close() {
106 var failure: Throwable? = None
107 failure = cleanupUi(failure) { retained.release() }
108 failure = cleanupUi(failure) { paragraph.close() }
109 throwUi(failure)
110 }
113private 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 }
123private fun equalMetrics(left: Array<InlineMetrics>, right: Array<InlineMetrics>): Boolean {
124 if (left.size != right.size)
125 return false
126 var index = 0L
127 while (index < left.size) {
128 if (left[index] != right[index])
129 return false
130 index += 1L
131 }
132 return true