Scoop API

scoop:scoop.graphics.text:0.1.0

src/paragraph.scoop

Selected production source · src/paragraph.scoop

1package scoop.graphics.text
3import scoop.graphics.*
5/** Owns UTF-8 text, run settings and font sources. Each layout is an independent result. */
6public class Paragraph public constructor(
7 fonts: FontCollection,
8 public val content: StyledText,
9 settings: ParagraphStyle = paragraphStyle()
10): Closeable {
11 public val text: String get() = content.text
12 private val resource = makeParagraph(fonts, content, settings)
13 public val isClosed: Boolean get() = resource.isClosed
15 public constructor(
16 fonts: FontCollection,
17 text: String,
18 style: TextStyle = textStyle(),
19 settings: ParagraphStyle = paragraphStyle()
20 )
21 : this(
22 fonts,
23 styledText {
24 this.text(text)
25 this.style = style
26 },
27 settings
28 ) {}
30 /** Lays out in finite logical width, preserving prior results. */
31 public fun layout(width: Double, inlines: Array<InlineMetrics> = []): TextLayout {
32 checkTextScalar(width)
33 requireText(width >= 0.0, "negative paragraph width")
34 return createLayout(width, true, inlines)
35 }
36 /** Uses natural width; explicit line breaks are preserved. */
37 public fun layoutUnwrapped(inlines: Array<InlineMetrics> = []): TextLayout =
38 createLayout(0.0, false, inlines)
39 private fun createLayout(
40 width: Double,
41 wrap: Boolean,
42 inlines: Array<InlineMetrics>
43 ): TextLayout {
44 val pointer = resource.get()
45 val metrics = resolveInlineMetrics(content, inlines)
46 @Unsafe
47 {
48 val native =
49 metrics.withDataPointer {
50 data,
51 count -> @Unsafe
52 {
53 return@withDataPointer ownedText(
54 textLayout(pointer, width, wrap, data, count),
55 "layout paragraph"
56 )
57 }
58 }
59 try {
60 return TextLayout(native, content)
61 }
62 catch (error: Throwable) {
63 native.close();
64 throw error
65 }
66 }
67 }
68 public override fun close() {
69 resource.close()
70 }
73private fun makeParagraph(
74 fonts: FontCollection,
75 content: StyledText,
76 settings: ParagraphStyle
77): TextResource {
78 val maxLines = settings.maxLines ?: -1L
79 when (settings.maxLines) {
80 case Some(val value) -> requireText(
81 value > 0L && value <= 2147483647L,
82 "invalid maximum line count"
83 )
84 case None -> {}
85 }
86 val alignment =
87 when (settings.alignment) {
88 case Left -> 0;
89 case Right -> 1;
90 case Center -> 2;
91 case Justify -> 3;
92 case Start -> 4;
93 case End -> 5
94 }
95 val direction =
96 if (settings.direction == TextDirection.Ltr)
97 1
98 else
99 0
100 val source = fonts.resource.get()
101 val builder =
102 content.style.withNative {
103 style -> beginParagraph(
104 source,
105 style,
106 alignment,
107 direction,
108 maxLines,
109 settings.ellipsis
110 )
111 }
112 return builder.use {
113 current ->
114 val pointer = current.get()
115 for (run in content.runs) {
116 when (run) {
117 case Text(val text, val value) -> value.withNative {
118 style -> @Unsafe
119 {
120 text.withUtf8Bytes {
121 bytes,
122 count -> @Unsafe
123 {
124 textResult(
125 paragraphRun(pointer, style, bytes, count),
126 "append paragraph run"
127 )
128 }
129 }
130 }
131 }
132 case Object(val value) -> value.withNative {
133 style -> @Unsafe
134 {
135 textResult(paragraphInline(pointer, style), "append inline object")
136 }
137 }
138 }
139 }
140 @Unsafe
141 {
142 return@use ownedText(paragraphFinish(pointer), "build paragraph")
143 }
144 }
147private fun beginParagraph(
148 fonts: Ptr<Unit>,
149 style: Ptr<Unit>,
150 alignment: Int,
151 direction: Int,
152 maxLines: Long,
153 ellipsis: String
154): TextResource {
155 @Unsafe
156 {
157 return ellipsis.withUtf8Bytes {
158 bytes,
159 count -> @Unsafe
160 {
161 return@withUtf8Bytes ownedText(
162 paragraphBegin(fonts, style, alignment, direction, maxLines, bytes, count),
163 "create paragraph builder"
164 )
165 }
166 }
167 }
170@Extern(name = "scoop_graphics_text_paragraph_begin")
171private fun paragraphBegin(
172 fonts: Ptr<Unit>,
173 style: Ptr<Unit>,
174 alignment: Int,
175 direction: Int,
176 maxLines: Long,
177 ellipsis: Ptr<UInt8>,
178 length: Long
179): Ptr<Unit>?
181@Extern(name = "scoop_graphics_text_paragraph_run")
182private fun paragraphRun(builder: Ptr<Unit>, style: Ptr<Unit>, text: Ptr<UInt8>, length: Long): Int
184@Extern(name = "scoop_graphics_text_paragraph_finish")
185private fun paragraphFinish(builder: Ptr<Unit>): Ptr<Unit>?
187@Extern(name = "scoop_graphics_text_paragraph_inline")
188private fun paragraphInline(builder: Ptr<Unit>, style: Ptr<Unit>): Int
190@Extern(name = "scoop_graphics_text_layout")
191private fun textLayout(
192 paragraph: Ptr<Unit>,
193 width: Double,
194 wrap: Boolean,
195 metrics: Ptr<NativeInlineMetrics>,
196 count: Long
197): Ptr<Unit>?