scoop:scoop.graphics.text:0.1.0
src/font_metadata.scoop
Selected production source · src/font_metadata.scoop
1
package scoop.graphics.text3
import scoop.graphics.GraphicsException5
internal class FontCatalogIdentity7
public class FontFamilyId internal constructor(8
internal val catalog: FontCatalogIdentity,9
internal val index: Long10
): Equals<FontFamilyId> {11
public override operator fun equals(other: FontFamilyId): Boolean =12
catalog === other.catalog && index == other.index13
}15
public class FontFaceId internal constructor(16
internal val catalog: FontCatalogIdentity,17
internal val index: Long18
): Equals<FontFaceId> {19
public override operator fun equals(other: FontFaceId): Boolean =20
catalog === other.catalog && index == other.index21
}23
public struct FontFamilyInfo(val id: FontFamilyId, val name: String)25
public 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?35
)37
public 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: Boolean44
)46
public class FontNamedInstance internal constructor(47
public val name: String?,48
public val postScriptName: String?,49
public val coordinates: Array<FontVariation>50
)52
@CLayout53
internal struct NativeFontString(val data: Ptr<UInt8>?, val length: Long)55
@CLayout56
internal 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: Int66
)68
internal fun fontString(value: NativeFontString): String {69
@Unsafe70
{71
return when (value.data) {72
case Some(val data) -> String.fromUtf8(data, value.length)73
case None -> ""74
}75
}76
}78
internal fun fontName(value: NativeFontString): String? =79
if (value.length == 0L)80
None81
else82
Some(fontString(value))84
internal 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.Upright96
case 1 -> FontSlant.Italic97
case 2 -> FontSlant.Oblique98
else -> throw GraphicsException("read font slant")99
},100
value.variable != 0,101
if (value.collectionIndex < 0L)102
None103
else104
Some(value.collectionIndex.toInt())105
)106
}108
internal fun fontTag(value: String): Int {109
requireText(value.byteLength == 4L, "OpenType tags require four ASCII bytes")110
var result = 0111
for (character in value) {112
requireText(character.code >= 32 && character.code <= 126, "invalid OpenType tag")113
result = (result shl 8) or character.code114
}115
return result116
}