Scoop API

scoop:scoop.graphics.text:0.1.0

src/font_metadata.scoop

Selected production source · src/font_metadata.scoop

1package scoop.graphics.text
3import scoop.graphics.GraphicsException
5internal class FontCatalogIdentity
7public class FontFamilyId internal constructor(
8 internal val catalog: FontCatalogIdentity,
9 internal val index: Long
10): Equals<FontFamilyId> {
11 public override operator fun equals(other: FontFamilyId): Boolean =
12 catalog === other.catalog && index == other.index
15public class FontFaceId internal constructor(
16 internal val catalog: FontCatalogIdentity,
17 internal val index: Long
18): Equals<FontFaceId> {
19 public override operator fun equals(other: FontFaceId): Boolean =
20 catalog === other.catalog && index == other.index
23public struct FontFamilyInfo(val id: FontFamilyId, val name: String)
25public class FontFaceInfo internal constructor(
26 public val id: FontFaceId,
27 public val family: FontFamilyInfo,
28 public val styleName: String,
29 public val postScriptName: String?,
30 public val weight: Int,
31 public val width: Int,
32 public val slant: FontSlant,
33 public val isVariable: Boolean,
34 public val collectionIndex: Int?
37public struct FontAxis(
38 val tag: String,
39 val name: String?,
40 val minimum: Double,
41 val defaultValue: Double,
42 val maximum: Double,
43 val hidden: Boolean
46public class FontNamedInstance internal constructor(
47 public val name: String?,
48 public val postScriptName: String?,
49 public val coordinates: Array<FontVariation>
52@CLayout
53internal struct NativeFontString(val data: Ptr<UInt8>?, val length: Long)
55@CLayout
56internal struct NativeFontInfo(
57 val style: NativeFontString,
58 val postScript: NativeFontString,
59 val family: Long,
60 val collectionIndex: Long,
61 val weight: Int,
62 val width: Int,
63 val slant: Int,
64 val variable: Int,
65 val valid: Int
68internal fun fontString(value: NativeFontString): String {
69 @Unsafe
70 {
71 return when (value.data) {
72 case Some(val data) -> String.fromUtf8(data, value.length)
73 case None -> ""
74 }
75 }
78internal fun fontName(value: NativeFontString): String? =
79 if (value.length == 0L)
80 None
81 else
82 Some(fontString(value))
84internal fun fontInfo(value: NativeFontInfo, id: FontFaceId, family: FontFamilyInfo): FontFaceInfo {
85 if (value.valid == 0)
86 throw GraphicsException("read font face metadata")
87 return FontFaceInfo(
88 id,
89 family,
90 fontString(value.style),
91 fontName(value.postScript),
92 value.weight,
93 value.width,
94 when (value.slant) {
95 case 0 -> FontSlant.Upright
96 case 1 -> FontSlant.Italic
97 case 2 -> FontSlant.Oblique
98 else -> throw GraphicsException("read font slant")
99 },
100 value.variable != 0,
101 if (value.collectionIndex < 0L)
102 None
103 else
104 Some(value.collectionIndex.toInt())
105 )
108internal fun fontTag(value: String): Int {
109 requireText(value.byteLength == 4L, "OpenType tags require four ASCII bytes")
110 var result = 0
111 for (character in value) {
112 requireText(character.code >= 32 && character.code <= 126, "invalid OpenType tag")
113 result = (result shl 8) or character.code
114 }
115 return result