fontkit 0.3.0-beta.1

A simple library for font loading and indexing
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
#[allow(clippy::all)]
pub mod fontkit {
  #[allow(unused_imports)]
  use wit_bindgen_wasmtime::{wasmtime, anyhow};
  #[derive(Debug)]
  pub struct FontKey(wit_bindgen_wasmtime::rt::ResourceIndex);
  #[derive(Debug)]
  pub struct Font(wit_bindgen_wasmtime::rt::ResourceIndex);
  #[derive(Debug)]
  pub struct FontKit(wit_bindgen_wasmtime::rt::ResourceIndex);
  
  /// Auxiliary data associated with the wasm exports.
  ///
  /// This is required to be stored within the data of a
  /// `Store<T>` itself so lifting/lowering state can be managed
  /// when translating between the host and wasm.
  #[derive(Default)]
  pub struct FontkitData {
    
    index_slab0: wit_bindgen_wasmtime::rt::IndexSlab,
    resource_slab0: wit_bindgen_wasmtime::rt::ResourceSlab,
    dtor0: Option<wasmtime::TypedFunc<i32, ()>>,
    
    index_slab1: wit_bindgen_wasmtime::rt::IndexSlab,
    resource_slab1: wit_bindgen_wasmtime::rt::ResourceSlab,
    dtor1: Option<wasmtime::TypedFunc<i32, ()>>,
    
    index_slab2: wit_bindgen_wasmtime::rt::IndexSlab,
    resource_slab2: wit_bindgen_wasmtime::rt::ResourceSlab,
    dtor2: Option<wasmtime::TypedFunc<i32, ()>>,
  }
  pub struct Fontkit<T> {
    get_state: Box<dyn Fn(&mut T) -> &mut FontkitData + Send + Sync>,
    canonical_abi_free: wasmtime::TypedFunc<(i32, i32, i32), ()>,
    canonical_abi_realloc: wasmtime::TypedFunc<(i32, i32, i32, i32), i32>,
    font_key_italic: wasmtime::TypedFunc<(i32,), (i32,)>,
    font_key_name: wasmtime::TypedFunc<(i32,), (i32,)>,
    font_key_stretch: wasmtime::TypedFunc<(i32,), (i32,)>,
    font_key_weight: wasmtime::TypedFunc<(i32,), (i32,)>,
    font_kit_add_font_from_buffer: wasmtime::TypedFunc<(i32,i32,i32,), (i32,)>,
    font_kit_query: wasmtime::TypedFunc<(i32,i32,), (i32,)>,
    memory: wasmtime::Memory,
  }
  impl<T> Fontkit<T> {
    
    /// Adds any intrinsics, if necessary for this exported wasm
    /// functionality to the `linker` provided.
    ///
    /// The `get_state` closure is required to access the
    /// auxiliary data necessary for these wasm exports from
    /// the general store's state.
    pub fn add_to_linker(
    linker: &mut wasmtime::Linker<T>,
    get_state: impl Fn(&mut T) -> &mut FontkitData + Send + Sync + Copy + 'static,
    ) -> anyhow::Result<()> {
      
      linker.func_wrap(
      "canonical_abi",
      "resource_drop_font-key",
      move |mut caller: wasmtime::Caller<'_, T>, idx: u32| {
        let state = get_state(caller.data_mut());
        let resource_idx = state.index_slab0.remove(idx)?;
        let wasm = match state.resource_slab0.drop(resource_idx) {
          Some(wasm) => wasm,
          None => return Ok(()),
        };
        let dtor = state.dtor0.expect("destructor not set yet");
        dtor.call(&mut caller, wasm)?;
        Ok(())
      },
      )?;
      linker.func_wrap(
      "canonical_abi",
      "resource_clone_font-key",
      move |mut caller: wasmtime::Caller<'_, T>, idx: u32| {
        let state = get_state(caller.data_mut());
        let resource_idx = state.index_slab0.get(idx)?;
        state.resource_slab0.clone(resource_idx)?;
        Ok(state.index_slab0.insert(resource_idx))
      },
      )?;
      linker.func_wrap(
      "canonical_abi",
      "resource_get_font-key",
      move |mut caller: wasmtime::Caller<'_, T>, idx: u32| {
        let state = get_state(caller.data_mut());
        let resource_idx = state.index_slab0.get(idx)?;
        Ok(state.resource_slab0.get(resource_idx))
      },
      )?;
      linker.func_wrap(
      "canonical_abi",
      "resource_new_font-key",
      move |mut caller: wasmtime::Caller<'_, T>, val: i32| {
        let state = get_state(caller.data_mut());
        let resource_idx = state.resource_slab0.insert(val);
        Ok(state.index_slab0.insert(resource_idx))
      },
      )?;
      
      linker.func_wrap(
      "canonical_abi",
      "resource_drop_font",
      move |mut caller: wasmtime::Caller<'_, T>, idx: u32| {
        let state = get_state(caller.data_mut());
        let resource_idx = state.index_slab1.remove(idx)?;
        let wasm = match state.resource_slab1.drop(resource_idx) {
          Some(wasm) => wasm,
          None => return Ok(()),
        };
        let dtor = state.dtor1.expect("destructor not set yet");
        dtor.call(&mut caller, wasm)?;
        Ok(())
      },
      )?;
      linker.func_wrap(
      "canonical_abi",
      "resource_clone_font",
      move |mut caller: wasmtime::Caller<'_, T>, idx: u32| {
        let state = get_state(caller.data_mut());
        let resource_idx = state.index_slab1.get(idx)?;
        state.resource_slab1.clone(resource_idx)?;
        Ok(state.index_slab1.insert(resource_idx))
      },
      )?;
      linker.func_wrap(
      "canonical_abi",
      "resource_get_font",
      move |mut caller: wasmtime::Caller<'_, T>, idx: u32| {
        let state = get_state(caller.data_mut());
        let resource_idx = state.index_slab1.get(idx)?;
        Ok(state.resource_slab1.get(resource_idx))
      },
      )?;
      linker.func_wrap(
      "canonical_abi",
      "resource_new_font",
      move |mut caller: wasmtime::Caller<'_, T>, val: i32| {
        let state = get_state(caller.data_mut());
        let resource_idx = state.resource_slab1.insert(val);
        Ok(state.index_slab1.insert(resource_idx))
      },
      )?;
      
      linker.func_wrap(
      "canonical_abi",
      "resource_drop_font-kit",
      move |mut caller: wasmtime::Caller<'_, T>, idx: u32| {
        let state = get_state(caller.data_mut());
        let resource_idx = state.index_slab2.remove(idx)?;
        let wasm = match state.resource_slab2.drop(resource_idx) {
          Some(wasm) => wasm,
          None => return Ok(()),
        };
        let dtor = state.dtor2.expect("destructor not set yet");
        dtor.call(&mut caller, wasm)?;
        Ok(())
      },
      )?;
      linker.func_wrap(
      "canonical_abi",
      "resource_clone_font-kit",
      move |mut caller: wasmtime::Caller<'_, T>, idx: u32| {
        let state = get_state(caller.data_mut());
        let resource_idx = state.index_slab2.get(idx)?;
        state.resource_slab2.clone(resource_idx)?;
        Ok(state.index_slab2.insert(resource_idx))
      },
      )?;
      linker.func_wrap(
      "canonical_abi",
      "resource_get_font-kit",
      move |mut caller: wasmtime::Caller<'_, T>, idx: u32| {
        let state = get_state(caller.data_mut());
        let resource_idx = state.index_slab2.get(idx)?;
        Ok(state.resource_slab2.get(resource_idx))
      },
      )?;
      linker.func_wrap(
      "canonical_abi",
      "resource_new_font-kit",
      move |mut caller: wasmtime::Caller<'_, T>, val: i32| {
        let state = get_state(caller.data_mut());
        let resource_idx = state.resource_slab2.insert(val);
        Ok(state.index_slab2.insert(resource_idx))
      },
      )?;
      Ok(())
    }
    
    /// Instantiates the provided `module` using the specified
    /// parameters, wrapping up the result in a structure that
    /// translates between wasm and the host.
    ///
    /// The `linker` provided will have intrinsics added to it
    /// automatically, so it's not necessary to call
    /// `add_to_linker` beforehand. This function will
    /// instantiate the `module` otherwise using `linker`, and
    /// both an instance of this structure and the underlying
    /// `wasmtime::Instance` will be returned.
    ///
    /// The `get_state` parameter is used to access the
    /// auxiliary state necessary for these wasm exports from
    /// the general store state `T`.
    pub fn instantiate(
    mut store: impl wasmtime::AsContextMut<Data = T>,
    module: &wasmtime::Module,
    linker: &mut wasmtime::Linker<T>,
    get_state: impl Fn(&mut T) -> &mut FontkitData + Send + Sync + Copy + 'static,
    ) -> anyhow::Result<(Self, wasmtime::Instance)> {
      Self::add_to_linker(linker, get_state)?;
      let instance = linker.instantiate(&mut store, module)?;
      Ok((Self::new(store, &instance,get_state)?, instance))
    }
    
    /// Low-level creation wrapper for wrapping up the exports
    /// of the `instance` provided in this structure of wasm
    /// exports.
    ///
    /// This function will extract exports from the `instance`
    /// defined within `store` and wrap them all up in the
    /// returned structure which can be used to interact with
    /// the wasm module.
    pub fn new(
    mut store: impl wasmtime::AsContextMut<Data = T>,
    instance: &wasmtime::Instance,
    get_state: impl Fn(&mut T) -> &mut FontkitData + Send + Sync + Copy + 'static,
    ) -> anyhow::Result<Self> {
      let mut store = store.as_context_mut();
      let canonical_abi_free= instance.get_typed_func::<(i32, i32, i32), (), _>(&mut store, "canonical_abi_free")?;
      let canonical_abi_realloc= instance.get_typed_func::<(i32, i32, i32, i32), i32, _>(&mut store, "canonical_abi_realloc")?;
      let font_key_italic= instance.get_typed_func::<(i32,), (i32,), _>(&mut store, "font-key::italic")?;
      let font_key_name= instance.get_typed_func::<(i32,), (i32,), _>(&mut store, "font-key::name")?;
      let font_key_stretch= instance.get_typed_func::<(i32,), (i32,), _>(&mut store, "font-key::stretch")?;
      let font_key_weight= instance.get_typed_func::<(i32,), (i32,), _>(&mut store, "font-key::weight")?;
      let font_kit_add_font_from_buffer= instance.get_typed_func::<(i32,i32,i32,), (i32,), _>(&mut store, "font-kit::add-font-from-buffer")?;
      let font_kit_query= instance.get_typed_func::<(i32,i32,), (i32,), _>(&mut store, "font-kit::query")?;
      let memory= instance
      .get_memory(&mut store, "memory")
      .ok_or_else(|| {
        anyhow::anyhow!("`memory` export not a memory")
      })?
      ;
      
      get_state(store.data_mut()).dtor0 = Some(instance.get_typed_func::<i32, (), _>(&mut store, "canonical_abi_drop_font-key", )?);
      
      
      get_state(store.data_mut()).dtor1 = Some(instance.get_typed_func::<i32, (), _>(&mut store, "canonical_abi_drop_font", )?);
      
      
      get_state(store.data_mut()).dtor2 = Some(instance.get_typed_func::<i32, (), _>(&mut store, "canonical_abi_drop_font-kit", )?);
      
      Ok(Fontkit{
        canonical_abi_free,
        canonical_abi_realloc,
        font_key_italic,
        font_key_name,
        font_key_stretch,
        font_key_weight,
        font_kit_add_font_from_buffer,
        font_kit_query,
        memory,
        get_state: Box::new(get_state),
        
      })
    }
    pub fn font_key_name(&self, mut caller: impl wasmtime::AsContextMut<Data = T>,self_: & FontKey,)-> Result<String, wasmtime::Trap> {
      let func_canonical_abi_free = &self.canonical_abi_free;
      let memory = &self.memory;
      
      let obj0 = self_;
      (self.get_state)(caller.as_context_mut().data_mut()).resource_slab0.clone(obj0.0)?;
      let handle0 = (self.get_state)(caller.as_context_mut().data_mut()).index_slab0.insert(obj0.0);
      let (result1_0,) = self.font_key_name.call(&mut caller, (handle0 as i32, ))?;
      let load2 = memory.data_mut(&mut caller).load::<i32>(result1_0 + 0)?;
      let load3 = memory.data_mut(&mut caller).load::<i32>(result1_0 + 4)?;
      let ptr4 = load2;
      let len4 = load3;
      
      let data4 = copy_slice(
      &mut caller,
      memory,
      ptr4, len4, 1,
      )?;
      func_canonical_abi_free.call(&mut caller, (ptr4, len4, 1))?;
      Ok(String::from_utf8(data4)
      .map_err(|_| wasmtime::Trap::new("invalid utf-8"))?)
    }
    pub fn font_key_italic(&self, mut caller: impl wasmtime::AsContextMut<Data = T>,self_: & FontKey,)-> Result<bool, wasmtime::Trap> {
      
      let obj0 = self_;
      (self.get_state)(caller.as_context_mut().data_mut()).resource_slab0.clone(obj0.0)?;
      let handle0 = (self.get_state)(caller.as_context_mut().data_mut()).index_slab0.insert(obj0.0);
      let (result1_0,) = self.font_key_italic.call(&mut caller, (handle0 as i32, ))?;
      Ok(match result1_0 {
        0 => false,
        1 => true,
        _ => return Err(invalid_variant("bool")),
      })
    }
    pub fn font_key_stretch(&self, mut caller: impl wasmtime::AsContextMut<Data = T>,self_: & FontKey,)-> Result<String, wasmtime::Trap> {
      let func_canonical_abi_free = &self.canonical_abi_free;
      let memory = &self.memory;
      
      let obj0 = self_;
      (self.get_state)(caller.as_context_mut().data_mut()).resource_slab0.clone(obj0.0)?;
      let handle0 = (self.get_state)(caller.as_context_mut().data_mut()).index_slab0.insert(obj0.0);
      let (result1_0,) = self.font_key_stretch.call(&mut caller, (handle0 as i32, ))?;
      let load2 = memory.data_mut(&mut caller).load::<i32>(result1_0 + 0)?;
      let load3 = memory.data_mut(&mut caller).load::<i32>(result1_0 + 4)?;
      let ptr4 = load2;
      let len4 = load3;
      
      let data4 = copy_slice(
      &mut caller,
      memory,
      ptr4, len4, 1,
      )?;
      func_canonical_abi_free.call(&mut caller, (ptr4, len4, 1))?;
      Ok(String::from_utf8(data4)
      .map_err(|_| wasmtime::Trap::new("invalid utf-8"))?)
    }
    pub fn font_key_weight(&self, mut caller: impl wasmtime::AsContextMut<Data = T>,self_: & FontKey,)-> Result<u32, wasmtime::Trap> {
      
      let obj0 = self_;
      (self.get_state)(caller.as_context_mut().data_mut()).resource_slab0.clone(obj0.0)?;
      let handle0 = (self.get_state)(caller.as_context_mut().data_mut()).index_slab0.insert(obj0.0);
      let (result1_0,) = self.font_key_weight.call(&mut caller, (handle0 as i32, ))?;
      Ok(result1_0 as u32)
    }
    pub fn font_kit_add_font_from_buffer(&self, mut caller: impl wasmtime::AsContextMut<Data = T>,self_: & FontKit,buffer: &[u8],)-> Result<Vec<FontKey>, wasmtime::Trap> {
      let func_canonical_abi_realloc = &self.canonical_abi_realloc;
      let func_canonical_abi_free = &self.canonical_abi_free;
      let memory = &self.memory;
      
      let obj0 = self_;
      (self.get_state)(caller.as_context_mut().data_mut()).resource_slab2.clone(obj0.0)?;
      let handle0 = (self.get_state)(caller.as_context_mut().data_mut()).index_slab2.insert(obj0.0);
      let vec1 = buffer;
      let ptr1 = func_canonical_abi_realloc.call(&mut caller, (0, 0, 1, (vec1.len() as i32) * 1))?;
      memory.data_mut(&mut caller).store_many(ptr1, &vec1)?;
      let (result2_0,) = self.font_kit_add_font_from_buffer.call(&mut caller, (handle0 as i32, ptr1, vec1.len() as i32, ))?;
      let load3 = memory.data_mut(&mut caller).load::<i32>(result2_0 + 0)?;
      let load4 = memory.data_mut(&mut caller).load::<i32>(result2_0 + 4)?;
      let len7 = load4;
      let base7 = load3;
      let mut result7 = Vec::with_capacity(len7 as usize);
      for i in 0..len7 {
        let base = base7 + i *4;
        result7.push({
          let load5 = memory.data_mut(&mut caller).load::<i32>(base + 0)?;
          let handle6 = (self.get_state)(caller.as_context_mut().data_mut()).index_slab0.remove(load5 as u32)?;
          FontKey(handle6)
        });
      }
      func_canonical_abi_free.call(&mut caller, (base7, len7 * 4, 4))?;
      Ok(result7)
    }
    pub fn font_kit_query(&self, mut caller: impl wasmtime::AsContextMut<Data = T>,self_: & FontKit,key: & FontKey,)-> Result<Option<Font>, wasmtime::Trap> {
      let memory = &self.memory;
      
      let obj0 = self_;
      (self.get_state)(caller.as_context_mut().data_mut()).resource_slab2.clone(obj0.0)?;
      let handle0 = (self.get_state)(caller.as_context_mut().data_mut()).index_slab2.insert(obj0.0);
      
      let obj1 = key;
      (self.get_state)(caller.as_context_mut().data_mut()).resource_slab0.clone(obj1.0)?;
      let handle1 = (self.get_state)(caller.as_context_mut().data_mut()).index_slab0.insert(obj1.0);
      let (result2_0,) = self.font_kit_query.call(&mut caller, (handle0 as i32, handle1 as i32, ))?;
      let load3 = memory.data_mut(&mut caller).load::<u8>(result2_0 + 0)?;
      Ok(match i32::from(load3) {
        0 => None,
        1 => Some({
          let load4 = memory.data_mut(&mut caller).load::<i32>(result2_0 + 4)?;
          let handle5 = (self.get_state)(caller.as_context_mut().data_mut()).index_slab1.remove(load4 as u32)?;
          Font(handle5)
        }),
        _ => return Err(invalid_variant("option")),
      })
    }
    
    /// Drops the host-owned handle to the resource
    /// specified.
    ///
    /// Note that this may execute the WebAssembly-defined
    /// destructor for this type. This also may not run
    /// the destructor if there are still other references
    /// to this type.
    pub fn drop_font_key(
    &self,
    mut store: impl wasmtime::AsContextMut<Data = T>,
    val: FontKey,
    ) -> Result<(), wasmtime::Trap> {
      let mut store = store.as_context_mut();
      let data = (self.get_state)(store.data_mut());
      let wasm = match data.resource_slab0.drop(val.0) {
        Some(val) => val,
        None => return Ok(()),
      };
      data.dtor0.unwrap().call(&mut store, wasm)?;
      Ok(())
    }
    
    /// Drops the host-owned handle to the resource
    /// specified.
    ///
    /// Note that this may execute the WebAssembly-defined
    /// destructor for this type. This also may not run
    /// the destructor if there are still other references
    /// to this type.
    pub fn drop_font(
    &self,
    mut store: impl wasmtime::AsContextMut<Data = T>,
    val: Font,
    ) -> Result<(), wasmtime::Trap> {
      let mut store = store.as_context_mut();
      let data = (self.get_state)(store.data_mut());
      let wasm = match data.resource_slab1.drop(val.0) {
        Some(val) => val,
        None => return Ok(()),
      };
      data.dtor1.unwrap().call(&mut store, wasm)?;
      Ok(())
    }
    
    /// Drops the host-owned handle to the resource
    /// specified.
    ///
    /// Note that this may execute the WebAssembly-defined
    /// destructor for this type. This also may not run
    /// the destructor if there are still other references
    /// to this type.
    pub fn drop_font_kit(
    &self,
    mut store: impl wasmtime::AsContextMut<Data = T>,
    val: FontKit,
    ) -> Result<(), wasmtime::Trap> {
      let mut store = store.as_context_mut();
      let data = (self.get_state)(store.data_mut());
      let wasm = match data.resource_slab2.drop(val.0) {
        Some(val) => val,
        None => return Ok(()),
      };
      data.dtor2.unwrap().call(&mut store, wasm)?;
      Ok(())
    }
  }
  use wit_bindgen_wasmtime::rt::RawMem;
  use wit_bindgen_wasmtime::rt::invalid_variant;
  use wit_bindgen_wasmtime::rt::copy_slice;
}