scoop:scoop.graphics.text:0.1.0
src/paragraph.scoop
Selected production source · src/paragraph.scoop
1
package scoop.graphics.text3
import scoop.graphics.*5
/** Owns UTF-8 text, run settings and font sources. Each layout is an independent result. */6
public 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.text12
private val resource = makeParagraph(fonts, content, settings)13
public val isClosed: Boolean get() = resource.isClosed15
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 = style26
},27
settings28
) {}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
@Unsafe47
{48
val native =49
metrics.withDataPointer {50
data,51
count -> @Unsafe52
{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 error65
}66
}67
}68
public override fun close() {69
resource.close()70
}71
}73
private fun makeParagraph(74
fonts: FontCollection,75
content: StyledText,76
settings: ParagraphStyle77
): TextResource {78
val maxLines = settings.maxLines ?: -1L79
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 -> 594
}95
val direction =96
if (settings.direction == TextDirection.Ltr)97
198
else99
0100
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.ellipsis110
)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 -> @Unsafe119
{120
text.withUtf8Bytes {121
bytes,122
count -> @Unsafe123
{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 -> @Unsafe134
{135
textResult(paragraphInline(pointer, style), "append inline object")136
}137
}138
}139
}140
@Unsafe141
{142
return@use ownedText(paragraphFinish(pointer), "build paragraph")143
}144
}145
}147
private fun beginParagraph(148
fonts: Ptr<Unit>,149
style: Ptr<Unit>,150
alignment: Int,151
direction: Int,152
maxLines: Long,153
ellipsis: String154
): TextResource {155
@Unsafe156
{157
return ellipsis.withUtf8Bytes {158
bytes,159
count -> @Unsafe160
{161
return@withUtf8Bytes ownedText(162
paragraphBegin(fonts, style, alignment, direction, maxLines, bytes, count),163
"create paragraph builder"164
)165
}166
}167
}168
}170
@Extern(name = "scoop_graphics_text_paragraph_begin")171
private 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: Long179
): Ptr<Unit>?181
@Extern(name = "scoop_graphics_text_paragraph_run")182
private fun paragraphRun(builder: Ptr<Unit>, style: Ptr<Unit>, text: Ptr<UInt8>, length: Long): Int184
@Extern(name = "scoop_graphics_text_paragraph_finish")185
private fun paragraphFinish(builder: Ptr<Unit>): Ptr<Unit>?187
@Extern(name = "scoop_graphics_text_paragraph_inline")188
private fun paragraphInline(builder: Ptr<Unit>, style: Ptr<Unit>): Int190
@Extern(name = "scoop_graphics_text_layout")191
private fun textLayout(192
paragraph: Ptr<Unit>,193
width: Double,194
wrap: Boolean,195
metrics: Ptr<NativeInlineMetrics>,196
count: Long197
): Ptr<Unit>?