scoop:scoop.graphics.text:0.1.0
src/style.scoop
Selected production source · src/style.scoop
1
package scoop.graphics.text3
import scoop.graphics.*5
public enum FontSlant {6
Upright, Italic, Oblique7
}9
public enum TextDirection {10
Rtl, Ltr11
}13
public enum TextAlignment {14
Left, Right, Center, Justify, Start, End15
}17
public enum TextAffinity {18
Upstream, Downstream19
}21
public struct FontFeature(val tag: String, val value: Int = 1)23
public struct FontVariation(val tag: String, val value: Double)25
/** Immutable shaping and painting settings; family order also controls explicit fallback. */26
public class TextStyle internal constructor(27
public val families: Array<String> = ["sans-serif"],28
public val fontSize: Double = 14.0,29
public val color: Color = Color.Black,30
public val weight: Int = 400,31
public val width: Int = 5,32
public val slant: FontSlant = FontSlant.Upright,33
public val locale: String = "",34
public val letterSpacing: Double = 0.0,35
public val wordSpacing: Double = 0.0,36
public val lineHeight: Double? = None,37
public val features: Array<FontFeature> = [],38
public val variations: Array<FontVariation> = []39
) {40
public fun copy(configure: TextStyleBuilder.() -> Unit = {}): TextStyle =41
configureTextStyle(this, configure)43
internal fun <T> withNative(block: (Ptr<Unit>) -> T): T {44
checkTextScalar(fontSize)45
checkTextScalar(letterSpacing)46
checkTextScalar(wordSpacing)47
checkTextColor(color)48
requireText(49
fontSize > 0.0 && weight >= 1 && weight <= 1000 && width >= 1 && width <= 9,50
"invalid font size, weight or width"51
)52
val height = lineHeight ?: 1.053
checkTextScalar(height)54
requireText(height > 0.0, "line height must be positive")55
val value =56
NativeTextStyleData(57
color,58
fontSize,59
height,60
letterSpacing,61
wordSpacing,62
weight,63
width,64
when (slant) {65
case Upright -> 0;66
case Italic -> 1;67
case Oblique -> 268
},69
if (lineHeight == None)70
071
else72
173
)74
return makeTextStyle(value).use {75
resource ->76
val pointer = resource.get()77
for (family in families) {78
requireText(family.byteLength > 0L, "font family must not be empty")79
setStyleString(pointer, family, false)80
}81
setStyleString(pointer, locale, true)82
for (feature in features) {83
requireText(feature.value >= 0, "negative OpenType feature value")84
setStyleFeature(pointer, feature.tag, feature.value.toDouble(), false)85
}86
for (variation in variations) {87
checkTextScalar(variation.value)88
setStyleFeature(pointer, variation.tag, variation.value, true)89
}90
block(pointer)91
}92
}93
}95
public class ParagraphStyle internal constructor(96
public val alignment: TextAlignment = TextAlignment.Start,97
public val direction: TextDirection = TextDirection.Ltr,98
public val maxLines: Long? = None,99
public val ellipsis: String = "…"100
) {101
public fun copy(configure: ParagraphStyleBuilder.() -> Unit = {}): ParagraphStyle {102
val builder = ParagraphStyleBuilder(this)103
builder.configure()104
return builder.snapshot()105
}106
}108
private fun makeTextStyle(value: NativeTextStyleData): TextResource {109
@Unsafe110
{111
return ownedText(textStyleCreate(value), "create text style")112
}113
}115
private fun setStyleString(style: Ptr<Unit>, value: String, locale: Boolean) {116
@Unsafe117
{118
value.withUtf8Bytes {119
bytes,120
count -> @Unsafe121
{122
textResult(123
textStyleString(style, bytes, count, locale),124
"set font family or locale"125
)126
}127
}128
}129
}131
private fun setStyleFeature(style: Ptr<Unit>, tag: String, value: Double, variation: Boolean) {132
requireText(tag.byteLength == 4L, "OpenType tags require four ASCII bytes")133
for (character in tag)134
requireText(character.code >= 32 && character.code <= 126, "invalid OpenType tag")135
@Unsafe136
{137
tag.withUtf8Bytes {138
bytes,139
_ -> @Unsafe140
{141
textResult(142
textStyleFeature(style, bytes, value, variation),143
"set OpenType feature or variation"144
)145
}146
}147
}148
}150
@CLayout151
private struct NativeTextStyleData(152
val color: Color,153
val size: Double,154
val height: Double,155
val letter: Double,156
val word: Double,157
val weight: Int,158
val width: Int,159
val slant: Int,160
val hasHeight: Int161
)163
@Extern(name = "scoop_graphics_text_style_create")164
private fun textStyleCreate(value: NativeTextStyleData): Ptr<Unit>?166
@Extern(name = "scoop_graphics_text_style_string")167
private fun textStyleString(style: Ptr<Unit>, value: Ptr<UInt8>, length: Long, locale: Boolean): Int169
@Extern(name = "scoop_graphics_text_style_feature")170
private fun textStyleFeature(171
style: Ptr<Unit>,172
tag: Ptr<UInt8>,173
value: Double,174
variation: Boolean175
): Int