daegun 1.1.0

Your all-in-one text engine. Rust, no_std, zero dependencies.
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
// SAFETY, once for the file. Every entry point null-checks its pointers and answers
// `Status::Null` before dereferencing anything, so the `unsafe` blocks below rest on a check
// immediately above them. Anything the caller must uphold beyond that is noted at its site.

use alloc::vec::Vec;

use crate::{DrawTarget, DrawnGlyph, Font, GpuBatch, Policy, Prefer};

use crate::ffi::handle::{Status, borrow, deliver, release};
use crate::ffi::list::{Axis, axes_of};
use crate::ffi::options::RasterOptionsC;
use crate::ffi::raster::Bitmap;

pub struct Batch(GpuBatch);

impl Batch {
    pub fn inner(&self) -> &GpuBatch {
        &self.0
    }
}

#[unsafe(no_mangle)]
pub unsafe extern "C" fn daegun_batch_new(out: *mut *mut Batch) -> Status {
    unsafe { deliver(out, Batch(GpuBatch::new())) }
}

#[unsafe(no_mangle)]
pub unsafe extern "C" fn daegun_batch_clear(batch: *mut Batch) -> Status {
    if batch.is_null() {
        return Status::Null;
    }
    unsafe { (*batch).0.clear() };
    Status::Ok
}

#[unsafe(no_mangle)]
pub unsafe extern "C" fn daegun_batch_append(
    batch: *mut Batch,
    quads: *const f32,
    count: usize,
    out: *mut crate::GlyphSlot,
) -> Status {
    if batch.is_null() || quads.is_null() || out.is_null() {
        return Status::Null;
    }
    let Some(floats) = count.checked_mul(6) else { return Status::Range };
    let src = unsafe { core::slice::from_raw_parts(quads, floats) };
    let mut curves: Vec<[[f32; 2]; 3]> = src
        .chunks_exact(6)
        .map(|q| [[q[0], q[1]], [q[2], q[3]], [q[4], q[5]]])
        .collect();
    match unsafe { (*batch).0.append(&mut curves) } {
        Some(slot) => {
            unsafe { *out = slot };
            Status::Ok
        }
        None => Status::Range,
    }
}

#[unsafe(no_mangle)]
pub unsafe extern "C" fn daegun_batch_revision(batch: *const Batch, out: *mut u64) -> Status {
    let Some(b) = (unsafe { borrow(batch) }) else { return Status::Null };
    if out.is_null() {
        return Status::Null;
    }
    unsafe { *out = b.0.revision() };
    Status::Ok
}

macro_rules! batch_view {
    ($fn_name:ident, $method:ident, $elem:ty, $doc:literal) => {
        #[doc = $doc]
        #[unsafe(no_mangle)]
        pub unsafe extern "C" fn $fn_name(
            batch: *const Batch,
            out_count: *mut usize,
        ) -> *const $elem {
            let Some(b) = (unsafe { borrow(batch) }) else { return core::ptr::null() };
            if out_count.is_null() {
                return core::ptr::null();
            }
            let slice = b.0.$method();
            unsafe { *out_count = slice.len() };
            slice.as_ptr()
        }
    };
}

batch_view!(
    daegun_batch_curves,
    curves,
    crate::CurvePoint,
    "The curve points every glyph in the batch contributed."
);
batch_view!(
    daegun_batch_band_curves,
    band_curves,
    u32,
    "Which curves each band holds, as indices into the curve points."
);
batch_view!(daegun_batch_bands, bands, crate::Band, "The bands, horizontal then vertical.");
batch_view!(
    daegun_batch_hulls,
    hulls,
    crate::HullVertex,
    "The drawn polygon of every glyph, as vertices."
);

#[unsafe(no_mangle)]
pub unsafe extern "C" fn daegun_batch_free(batch: *mut Batch) {
    unsafe { release(batch) }
}

#[unsafe(no_mangle)]
pub unsafe extern "C" fn daegun_font_gpu_glyph(
    font: *const Font,
    batch: *mut Batch,
    gid: u16,
    axes: *const Axis,
    axes_len: usize,
    out: *mut crate::GlyphSlot,
) -> Status {
    let Some(font) = (unsafe { borrow(font) }) else { return Status::Null };
    if batch.is_null() || out.is_null() {
        return Status::Null;
    }
    let location = unsafe { axes_of(axes, axes_len) };
    match unsafe { font.gpu_glyph(&mut (*batch).0, gid, &location) } {
        Ok(slot) => {
            unsafe { *out = slot };
            Status::Ok
        }
        Err(e) => {
            crate::ffi::set_error(&alloc::format!("{e:?}"));
            Status::Absent
        }
    }
}

pub struct ColorSlots(Vec<crate::ColorSlot>);

#[unsafe(no_mangle)]
pub unsafe extern "C" fn daegun_font_gpu_color_glyph(
    font: *const Font,
    batch: *mut Batch,
    gid: u16,
    axes: *const Axis,
    axes_len: usize,
    palette_index: u16,
    out: *mut *mut ColorSlots,
) -> Status {
    unsafe {
        daegun_font_gpu_color_glyph_with(
            font, batch, gid, axes, axes_len, palette_index, core::ptr::null(), out,
        )
    }
}

#[unsafe(no_mangle)]
pub unsafe extern "C" fn daegun_font_gpu_color_glyph_with(
    font: *const Font,
    batch: *mut Batch,
    gid: u16,
    axes: *const Axis,
    axes_len: usize,
    palette_index: u16,
    foreground: *const u8,
    out: *mut *mut ColorSlots,
) -> Status {
    let Some(font) = (unsafe { borrow(font) }) else { return Status::Null };
    if batch.is_null() {
        return Status::Null;
    }
    let location = unsafe { axes_of(axes, axes_len) };
    let built = unsafe {
        match crate::ffi::rgba_of(foreground) {
            Some(fg) => {
                font.gpu_color_glyph_with(&mut (*batch).0, gid, &location, palette_index, fg)
            }
            None => font.gpu_color_glyph(&mut (*batch).0, gid, &location, palette_index),
        }
    };
    match built {
        Ok(slots) => unsafe { deliver(out, ColorSlots(slots)) },
        Err(e) => {
            crate::ffi::set_error(&alloc::format!("{e:?}"));
            Status::Absent
        }
    }
}

#[unsafe(no_mangle)]
pub unsafe extern "C" fn daegun_color_slots_data(
    slots: *const ColorSlots,
    out_count: *mut usize,
) -> *const crate::ColorSlot {
    let Some(s) = (unsafe { borrow(slots) }) else { return core::ptr::null() };
    if out_count.is_null() {
        return core::ptr::null();
    }
    unsafe { *out_count = s.0.len() };
    s.0.as_ptr()
}

#[unsafe(no_mangle)]
pub unsafe extern "C" fn daegun_color_slots_free(slots: *mut ColorSlots) {
    unsafe { release(slots) }
}

pub const PREFER_AUTO: i32 = 0;
pub const PREFER_CPU: i32 = 1;
pub const PREFER_GPU: i32 = 2;
pub const PREFER_REFERENCE: i32 = 3;

#[repr(C)]
#[derive(Clone, Copy)]
pub struct PolicyC {
    pub prefer: i32,
    pub strict: bool,
    pub has_cpu_below_ppem: bool,
    pub cpu_below_ppem: f32,
    pub avoid_software_gpu: bool,
}
const _: () = assert!(size_of::<PolicyC>() == 16);

impl PolicyC {
    pub fn to_rust(self) -> Policy {
        Policy {
            prefer: match self.prefer {
                PREFER_CPU => Prefer::Cpu,
                PREFER_GPU => Prefer::Gpu,
                PREFER_REFERENCE => Prefer::Reference,
                _ => Prefer::Auto,
            },
            strict: self.strict,
            cpu_below_ppem: self.has_cpu_below_ppem.then_some(self.cpu_below_ppem),
            avoid_software_gpu: self.avoid_software_gpu,
        }
    }
}

#[unsafe(no_mangle)]
pub unsafe extern "C" fn daegun_policy_default(out: *mut PolicyC) -> Status {
    if out.is_null() {
        return Status::Null;
    }
    unsafe {
        *out = PolicyC {
            prefer: PREFER_AUTO,
            strict: false,
            has_cpu_below_ppem: false,
            cpu_below_ppem: 0.0,
            avoid_software_gpu: false,
        }
    };
    Status::Ok
}

pub const DRAWN_NOTHING: i32 = 0;
pub const DRAWN_CPU: i32 = 1;
pub const DRAWN_GPU: i32 = 2;
pub const DRAWN_GPU_COLOR: i32 = 3;
pub const DRAWN_SCENE: i32 = 4;
pub const DRAWN_REFERENCE: i32 = 5;
pub const DRAWN_BATCH_FULL: i32 = 6;
pub const DRAWN_REFUSED: i32 = 7;

pub struct Drawn {
    kind: i32,
    bitmap: Option<Bitmap>,
    slot: Option<crate::GlyphSlot>,
    color_slots: Vec<crate::ColorSlot>,
    scene: Option<crate::ffi::color::Scene>,
}

#[unsafe(no_mangle)]
pub unsafe extern "C" fn daegun_font_draw_glyph(
    font: *const Font,
    batch: *mut Batch,
    device: *const crate::DeviceProfile,
    policy: *const PolicyC,
    gid: u16,
    px: f32,
    axes: *const Axis,
    axes_len: usize,
    opts: *const RasterOptionsC,
    palette: i32,
    out: *mut *mut Drawn,
) -> Status {
    unsafe {
        daegun_font_draw_glyph_with(
            font, batch, device, policy, gid, px, axes, axes_len, opts, palette,
            core::ptr::null(), out,
        )
    }
}

#[unsafe(no_mangle)]
#[allow(clippy::too_many_arguments, reason = "the router needs every input the two paths take")]
pub unsafe extern "C" fn daegun_font_draw_glyph_with(
    font: *const Font,
    batch: *mut Batch,
    device: *const crate::DeviceProfile,
    policy: *const PolicyC,
    gid: u16,
    px: f32,
    axes: *const Axis,
    axes_len: usize,
    opts: *const RasterOptionsC,
    palette: i32,
    foreground: *const u8,
    out: *mut *mut Drawn,
) -> Status {
    let Some(font) = (unsafe { borrow(font) }) else { return Status::Null };
    if batch.is_null() {
        return Status::Null;
    }
    let location = unsafe { axes_of(axes, axes_len) };
    let options = match unsafe { borrow(opts) } {
        Some(o) => o.to_rust(),
        None => RasterOptionsC::DEFAULT.to_rust(),
    };
    let pal = if palette < 0 { None } else { u16::try_from(palette).ok() };

    let built_policy =
        unsafe { borrow(policy) }.map_or_else(Policy::default, |p| p.to_rust());

    let drawn = unsafe {
        let batch = &mut (*batch).0;
        let mut target = match borrow(device) {
            Some(d) => DrawTarget::new(batch, d),
            None => DrawTarget::cpu_only(batch),
        }
        .with_policy(built_policy);
        match crate::ffi::rgba_of(foreground) {
            Some(fg) => {
                font.draw_glyph_with(&mut target, gid, px, &location, &options, pal, fg)
            }
            None => font.draw_glyph(&mut target, gid, px, &location, &options, pal),
        }
    };

    let built = match drawn {
        DrawnGlyph::Nothing => Drawn::empty(DRAWN_NOTHING),
        DrawnGlyph::Cpu(g) => Drawn { bitmap: Some(crate::ffi::raster::wrap(g)), ..Drawn::empty(DRAWN_CPU) },
        DrawnGlyph::Reference(g) => {
            Drawn { bitmap: Some(crate::ffi::raster::wrap(g)), ..Drawn::empty(DRAWN_REFERENCE) }
        }
        DrawnGlyph::Gpu(slot) => Drawn { slot: Some(slot), ..Drawn::empty(DRAWN_GPU) },
        DrawnGlyph::GpuColor(slots) => {
            Drawn { color_slots: slots, ..Drawn::empty(DRAWN_GPU_COLOR) }
        }
        DrawnGlyph::Scene(s) => {
            Drawn { scene: Some(crate::ffi::color::wrap_scene(s)), ..Drawn::empty(DRAWN_SCENE) }
        }
        DrawnGlyph::BatchFull => Drawn::empty(DRAWN_BATCH_FULL),
        DrawnGlyph::Refused(r) => {
            crate::ffi::set_error(&alloc::format!("{r:?}"));
            Drawn::empty(DRAWN_REFUSED)
        }
    };
    unsafe { deliver(out, built) }
}

impl Drawn {
    fn empty(kind: i32) -> Drawn {
        Drawn { kind, bitmap: None, slot: None, color_slots: Vec::new(), scene: None }
    }
}

#[unsafe(no_mangle)]
pub unsafe extern "C" fn daegun_drawn_kind(d: *const Drawn, out: *mut i32) -> Status {
    let Some(d) = (unsafe { borrow(d) }) else { return Status::Null };
    if out.is_null() {
        return Status::Null;
    }
    unsafe { *out = d.kind };
    Status::Ok
}

#[unsafe(no_mangle)]
pub unsafe extern "C" fn daegun_drawn_is_ok(d: *const Drawn, out: *mut bool) -> Status {
    let Some(d) = (unsafe { borrow(d) }) else { return Status::Null };
    if out.is_null() {
        return Status::Null;
    }
    unsafe { *out = d.kind != DRAWN_REFUSED && d.kind != DRAWN_BATCH_FULL };
    Status::Ok
}

#[unsafe(no_mangle)]
pub unsafe extern "C" fn daegun_drawn_bitmap(
    d: *const Drawn,
    out: *mut *const Bitmap,
) -> Status {
    let Some(d) = (unsafe { borrow(d) }) else { return Status::Null };
    if out.is_null() {
        return Status::Null;
    }
    let Some(b) = &d.bitmap else { return Status::Absent };
    unsafe { *out = &raw const *b };
    Status::Ok
}

#[unsafe(no_mangle)]
pub unsafe extern "C" fn daegun_drawn_slot(
    d: *const Drawn,
    out: *mut crate::GlyphSlot,
) -> Status {
    let Some(d) = (unsafe { borrow(d) }) else { return Status::Null };
    if out.is_null() {
        return Status::Null;
    }
    let Some(s) = d.slot else { return Status::Absent };
    unsafe { *out = s };
    Status::Ok
}

#[unsafe(no_mangle)]
pub unsafe extern "C" fn daegun_drawn_color_slots(
    d: *const Drawn,
    out_count: *mut usize,
) -> *const crate::ColorSlot {
    let Some(d) = (unsafe { borrow(d) }) else { return core::ptr::null() };
    if out_count.is_null() {
        return core::ptr::null();
    }
    unsafe { *out_count = d.color_slots.len() };
    d.color_slots.as_ptr()
}

#[unsafe(no_mangle)]
pub unsafe extern "C" fn daegun_drawn_scene(
    d: *const Drawn,
    out: *mut *const crate::ffi::color::Scene,
) -> Status {
    let Some(d) = (unsafe { borrow(d) }) else { return Status::Null };
    if out.is_null() {
        return Status::Null;
    }
    let Some(s) = &d.scene else { return Status::Absent };
    unsafe { *out = &raw const *s };
    Status::Ok
}

#[unsafe(no_mangle)]
pub unsafe extern "C" fn daegun_drawn_free(d: *mut Drawn) {
    unsafe { release(d) }
}