Scoop API

scoop:scoop.ui:0.1.0

src/text/paragraph.scoop

Selected production source · src/text/paragraph.scoop

1package scoop.ui.text
3import scoop.graphics.Point
4import scoop.graphics.Rect
5import scoop.graphics.text.*
6import scoop.platform.concurrent.currentThreadId
7import scoop.surface.InputArea
8import scoop.ui.*
10/** Public text/child adapter for any Element; it owns layouts but not fonts or children. */
11public class ParagraphHost public constructor(): Closeable {
12 private val thread = currentThreadId()
13 private val cache = ParagraphCache()
14 private val paintKey = PaintKey()
15 private val inkVersion = TextInkVersion()
16 private var closed = false
18 public val isClosed: Boolean get() {
19 checkUiThread(thread)
20 return closed
21 }
22 private fun checkOpen() {
23 checkUiThread(thread)
24 if (closed)
25 throw IllegalStateException(Some("paragraph host is closed"))
26 }
28 public fun measure(
29 context: MeasureContext,
30 constraints: Constraints,
31 fonts: FontCollection,
32 content: StyledText,
33 settings: ParagraphStyle? = None,
34 inlines: Array<InlineView> = []
35 ): ParagraphMeasurement {
36 checkOpen()
37 constraints.check()
38 val ordered = orderedInlines(content, inlines)
39 requireUi(
40 context.childCount == ordered.size,
41 "paragraph children do not match inline views"
42 )
43 val metrics =
44 Array<InlineMetrics>(ordered.size) {
45 index ->
46 val item = ordered[index]
47 val limits =
48 when (item.size) {
49 case Some(val size) -> Constraints.tight(size)
50 case None -> Constraints(maxWidth = constraints.maxWidth)
51 }
52 item.metrics(context.measureChild(index, limits))
53 }
54 val resolved =
55 settings ?: paragraphStyle { direction = context.layoutDirection.textDirection() }
56 val result = cache.update(fonts, content, resolved, constraints.maxWidth, metrics)
57 var index = 0L
58 for (placement in result.inlineObjects) {
59 when (placement.box) {
60 case Some(val box) -> context.placeChild(index, Point(box.rect.x, box.rect.y))
61 case None -> {}
62 }
63 index += 1L
64 }
65 return ParagraphMeasurement(
66 LayoutMetrics(
67 constraints.constrain(result.size),
68 Some(result.alphabeticBaseline),
69 Some(result.ideographicBaseline)
70 ),
71 cache.retainLayout(),
72 cache.generation
73 )
74 }
76 public fun scene(context: SceneContext, measurement: ParagraphMeasurement) {
77 scene(context, measurement, None, [])
78 }
80 internal fun scene(
81 context: SceneContext,
82 measurement: ParagraphMeasurement,
83 ink: TextSelectionInk?,
84 boxes: Array<TextBox>
85 ) {
86 checkOpen()
87 val layout = measurement.layout
88 val bounds = layout.paintBounds
89 val paintBounds =
90 Rect(bounds.x - 1.0, bounds.y - 1.0, bounds.width + 2.0, bounds.height + 2.0)
91 context.drawTextLayer(
92 paintKey,
93 paintBounds,
94 inkVersion.update(measurement.generation, ink)
95 ) {
96 paint -> drawSelectionText(paint.canvas, paintBounds, boxes, ink) {
97 canvas -> canvas.drawText(layout)
98 }
99 }
100 context.children()
101 }
103 public override fun close() {
104 checkUiThread(thread)
105 if (closed)
106 return
107 closed = true
108 cache.close()
109 }