Scoop API

scoop:scoop.graphics.text:0.1.0

src/style.scoop

Selected production source · src/style.scoop

1package scoop.graphics.text
3import scoop.graphics.*
5public enum FontSlant {
6 Upright, Italic, Oblique
7}
9public enum TextDirection {
10 Rtl, Ltr
13public enum TextAlignment {
14 Left, Right, Center, Justify, Start, End
17public enum TextAffinity {
18 Upstream, Downstream
21public struct FontFeature(val tag: String, val value: Int = 1)
23public struct FontVariation(val tag: String, val value: Double)
25/** Immutable shaping and painting settings; family order also controls explicit fallback. */
26public 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.0
53 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 -> 2
68 },
69 if (lineHeight == None)
70 0
71 else
72 1
73 )
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 }
95public 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 }
108private fun makeTextStyle(value: NativeTextStyleData): TextResource {
109 @Unsafe
110 {
111 return ownedText(textStyleCreate(value), "create text style")
112 }
115private fun setStyleString(style: Ptr<Unit>, value: String, locale: Boolean) {
116 @Unsafe
117 {
118 value.withUtf8Bytes {
119 bytes,
120 count -> @Unsafe
121 {
122 textResult(
123 textStyleString(style, bytes, count, locale),
124 "set font family or locale"
125 )
126 }
127 }
128 }
131private 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 @Unsafe
136 {
137 tag.withUtf8Bytes {
138 bytes,
139 _ -> @Unsafe
140 {
141 textResult(
142 textStyleFeature(style, bytes, value, variation),
143 "set OpenType feature or variation"
144 )
145 }
146 }
147 }
150@CLayout
151private 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: Int
163@Extern(name = "scoop_graphics_text_style_create")
164private fun textStyleCreate(value: NativeTextStyleData): Ptr<Unit>?
166@Extern(name = "scoop_graphics_text_style_string")
167private fun textStyleString(style: Ptr<Unit>, value: Ptr<UInt8>, length: Long, locale: Boolean): Int
169@Extern(name = "scoop_graphics_text_style_feature")
170private fun textStyleFeature(
171 style: Ptr<Unit>,
172 tag: Ptr<UInt8>,
173 value: Double,
174 variation: Boolean
175): Int