Scoop API

scoop:scoop.graphics.text:0.1.0

src/fonts.scoop

Selected production source · src/fonts.scoop

1package scoop.graphics.text
3import scoop.graphics.*
5/** Owns explicit font data and optional system fallback. Existing paragraphs retain a snapshot. */
6public class FontCollection private constructor(internal val resource: TextResource) : Closeable {
7 public constructor(systemFallback: Boolean = true) : this(makeFonts(systemFallback)) {}
8 private var currentRevision = 0L
9 private val observers = ArrayList<FontObserver>()
10 public val isClosed: Boolean get() = resource.isClosed
11 public val revision: Long get() = currentRevision
13 /** Owns the same font sources independently; mutations, observers and revision are not shared. */
14 public fun copy(): FontCollection {
15 val pointer = resource.get()
16 @Unsafe
17 {
18 return FontCollection(ownedText(fontsCopy(pointer), "copy font collection"))
19 }
20 }
22 /** Copies one face from a font file or collection under a family alias. */
23 public fun register(data: Array<Byte>, family: String, collectionIndex: Int = 0) {
24 requireText(
25 data.size > 0L && family.byteLength > 0L && collectionIndex >= 0,
26 "invalid font data, family or collection index"
27 )
28 val pointer = resource.get()
29 @Unsafe
30 {
31 data
32 .withDataPointer { bytes, length ->
33 @Unsafe
34 {
35 family
36 .withUtf8Bytes { alias, count ->
37 @Unsafe
38 {
39 textResult(
40 fontsAdd(
41 pointer,
42 bytes,
43 length,
44 alias,
45 count,
46 collectionIndex
47 ),
48 "register font data"
49 )
50 }
51 }
52 }
53 }
54 }
55 changed()
56 }
58 public fun systemFonts(): FontCatalog {
59 val pointer = resource.get()
60 @Unsafe
61 {
62 return FontCatalog(ownedText(fontsCatalog(pointer), "enumerate system fonts"))
63 }
64 }
66 public fun register(face: FontFace, family: String) {
67 updateFaces(family, [face], false)
68 }
70 /** Atomically replaces all explicitly registered faces under one alias. */
71 public fun replaceFamily(family: String, faces: Array<FontFace>) {
72 updateFaces(family, faces, true)
73 }
75 private fun updateFaces(family: String, faces: Array<FontFace>, replace: Boolean) {
76 val pointer = resource.get()
77 requireText(
78 family.byteLength > 0L && faces.size > 0L,
79 "font family and faces must not be empty"
80 )
81 val handles = Array<Ptr<Unit>>(faces.size) { index -> faces[index].resource.get() }
82 @Unsafe
83 {
84 handles
85 .withDataPointer { values, count ->
86 @Unsafe
87 {
88 family
89 .withUtf8Bytes { alias, length ->
90 @Unsafe
91 {
92 textResult(
93 fontsFaces(pointer, values, count, alias, length, replace),
94 "register font faces"
95 )
96 }
97 }
98 }
99 }
100 }
101 changed()
102 }
104 public fun observeChanges(onChanged: (Long) -> Unit): Closeable {
105 resource.get()
106 val observer = FontObserver(this, onChanged)
107 observers.add(observer)
108 return observer
109 }
111 internal fun remove(observer: FontObserver) {
112 resource.checkThread()
113 if (!observer.active) return
114 observer.active = false
115 for (index in 0L..<observers.size)
116 if (observers[index] === observer) {
117 observers.removeAt(index)
118 return
119 }
120 }
122 private fun changed() {
123 currentRevision += 1L
124 val revision = currentRevision
125 var failure: Throwable? = None
126 for (observer in observers.toArray())
127 if (observer.active) {
128 try {
129 observer.onChanged(revision)
130 } catch (error: Throwable) {
131 when (failure) {
132 case None -> failure = Some(error)
133 case Some(_) -> {}
134 }
135 }
136 }
137 when (failure) {
138 case Some(val error) -> throw error
139 case None -> {}
140 }
141 }
143 public override fun close() {
144 resource.checkThread()
145 for (observer in observers)
146 observer.active = false
147 observers.clear()
148 resource.close()
149 }
152internal class FontObserver(val owner: FontCollection, val onChanged: (Long) -> Unit) : Closeable {
153 var active = true
154 public override fun close() {
155 owner.remove(this)
156 }
159private fun makeFonts(fallback: Boolean): TextResource {
160 @Unsafe
161 {
162 return ownedText(fontsCreate(fallback), "create font collection")
163 }
166@Extern(name = "scoop_graphics_text_fonts_create")
167private fun fontsCreate(fallback: Boolean): Ptr<Unit>?
169@Extern(name = "scoop_graphics_text_fonts_copy")
170private fun fontsCopy(fonts: Ptr<Unit>): Ptr<Unit>?
172@Extern(name = "scoop_graphics_text_fonts_add")
173private fun fontsAdd(
174 fonts: Ptr<Unit>,
175 bytes: Ptr<Byte>,
176 length: Long,
177 alias: Ptr<UInt8>,
178 aliasLength: Long,
179 index: Int
180): Int
182@Extern(name = "scoop_graphics_text_catalog_system")
183private fun fontsCatalog(fonts: Ptr<Unit>): Ptr<Unit>?
185@Extern(name = "scoop_graphics_text_fonts_faces")
186private fun fontsFaces(
187 fonts: Ptr<Unit>,
188 faces: Ptr<Ptr<Unit>>,
189 count: Long,
190 alias: Ptr<UInt8>,
191 length: Long,
192 replace: Boolean
193): Int