icu_lib 0.6.0

Image Converter Ultra Library
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
use image::RgbaImage;
use image::imageops::overlay;
use mirui::render::backends::sw::SwRenderer;
use mirui::render::canvas::Canvas;
use mirui::render::font::Font;
use mirui::render::path::{Path, PathCmd};
use mirui::render::raster::FillRule;
use mirui::render::texture::{AlphaMode, ColorFormat, Texture};
use mirui::types::{Fixed, Point, Rect};
use mirx::{FontChunkKind, Paint};

pub fn render_font_atlas(font: &mirx::Font) -> RgbaImage {
    let atlas = &font.atlas;
    let source = atlas.source_size as u32;
    if source == 0 || font.metrics.is_empty() {
        return RgbaImage::new(0, 0);
    }
    let payload: &'static [u8] = leak_font_payload(font);
    let mirui_font = match font.chunk_header.kind {
        FontChunkKind::Sdf => {
            match mirui::render::font::sdf::font_from_mirx_chunk("atlas", payload) {
                Ok(f) => f,
                Err(_) => return RgbaImage::new(0, 0),
            }
        }
        FontChunkKind::Grayscale => {
            match mirui::render::font::gray::font_from_mirx_chunk("atlas", payload) {
                Ok(f) => f,
                Err(_) => return RgbaImage::new(0, 0),
            }
        }
    };

    let cell = source;
    let gap = 1u32;
    let cols = (font.metrics.len() as f64).sqrt().ceil() as u32;
    let cols = cols.max(1);
    let rows = (font.metrics.len() as u32 + cols - 1) / cols;
    let grid_w = cols * cell + (cols + 1) * gap;
    let grid_h = rows * cell + (rows + 1) * gap;
    let mut buffer = vec![0u8; (grid_w * grid_h * 4) as usize];
    let w = grid_w.min(u16::MAX as u32) as u16;
    let h = grid_h.min(u16::MAX as u32) as u16;
    let texture = Texture::new(&mut buffer, w, h, ColorFormat::RGBA8888);
    let mut renderer = SwRenderer::new(texture).with_alpha_mode(AlphaMode::Blend);
    let clip = Rect::new(
        Fixed::ZERO,
        Fixed::ZERO,
        Fixed::from_int(w as i32),
        Fixed::from_int(h as i32),
    );
    let color = mirui::types::Color {
        r: 255,
        g: 255,
        b: 255,
        a: 255,
    };

    for (i, m) in font.metrics.iter().enumerate() {
        let row = i as u32 / cols;
        let col = i as u32 % cols;
        let x0 = gap + col * (cell + gap);
        let y0 = gap + row * (cell + gap);
        let ch = char::from_u32(m.codepoint).unwrap_or('?');
        let pos = Point::new(Fixed::from_int(x0 as i32), Fixed::from_int(y0 as i32));
        renderer.draw_label(&pos, &ch.to_string(), &mirui_font, &clip, &color, 255);
    }
    renderer.flush();
    RgbaImage::from_raw(grid_w, grid_h, buffer).unwrap_or_else(|| RgbaImage::new(grid_w, grid_h))
}

pub fn render_freetype_glyph_at(
    font: &crate::midata::FreeTypeFontData,
    ch: char,
    width: u32,
    height: u32,
    color: mirx::Color,
) -> Option<RgbaImage> {
    let glyph = font.glyphs.iter().find(|g| g.codepoint == ch as u32)?;
    if glyph.outline.is_empty() {
        return None;
    }
    let mut buffer = vec![0u8; (width * height * 4) as usize];
    let w = width.min(u16::MAX as u32) as u16;
    let h = height.min(u16::MAX as u32) as u16;
    let texture = Texture::new(&mut buffer, w, h, ColorFormat::RGBA8888);
    let mut renderer = SwRenderer::new(texture).with_alpha_mode(AlphaMode::Blend);
    let clip = Rect::new(
        Fixed::ZERO,
        Fixed::ZERO,
        Fixed::from_int(w as i32),
        Fixed::from_int(h as i32),
    );
    let units = font.units_per_em.max(1) as f32;
    let scale = width as f32 * 0.7 / units;
    let baseline = height as f32 * 0.8;
    let mut path = Path::new();
    for cmd in &glyph.outline {
        let mirui_cmd: PathCmd = cmd.clone().into();
        let mapped = map_freetype_cmd(&mirui_cmd, 0, 0, scale, baseline);
        match mapped {
            PathCmd::MoveTo(p) => {
                path.move_to(p);
            }
            PathCmd::LineTo(p) => {
                path.line_to(p);
            }
            PathCmd::QuadTo { ctrl, end } => {
                path.quad_to(ctrl, end);
            }
            PathCmd::CubicTo { ctrl1, ctrl2, end } => {
                path.cubic_to(ctrl1, ctrl2, end);
            }
            PathCmd::Close => {
                path.close();
            }
        }
    }
    let paint = mirui::render::canvas::Paint::Color(
        mirui::types::Color {
            r: color.r,
            g: color.g,
            b: color.b,
            a: color.a,
        }
        .into(),
    );
    renderer.fill_path(
        &path,
        &clip,
        &paint,
        255,
        mirui::render::raster::FillRule::NonZero,
    );
    renderer.flush();
    let mut img = RgbaImage::from_raw(width, height, buffer)?;
    for px in img.pixels_mut() {
        px.0[0] = color.r;
        px.0[1] = color.g;
        px.0[2] = color.b;
    }
    Some(img)
}

pub fn render_freetype_glyphs(
    font: &crate::midata::FreeTypeFontData,
    color: mirx::Color,
) -> RgbaImage {
    if font.glyphs.is_empty() {
        return RgbaImage::new(0, 0);
    }
    let cell: u32 = 48;
    let gap: u32 = 2;
    let cols = (font.glyphs.len() as f64).sqrt().ceil() as u32;
    let cols = cols.max(1);
    let rows = (font.glyphs.len() as u32 + cols - 1) / cols;
    let grid_w = cols * cell + (cols + 1) * gap;
    let grid_h = rows * cell + (rows + 1) * gap;
    let mut buffer = vec![0u8; (grid_w * grid_h * 4) as usize];
    let w = grid_w.min(u16::MAX as u32) as u16;
    let h = grid_h.min(u16::MAX as u32) as u16;
    let texture = Texture::new(&mut buffer, w, h, ColorFormat::RGBA8888);
    let mut renderer = SwRenderer::new(texture).with_alpha_mode(AlphaMode::Blend);
    let clip = Rect::new(
        Fixed::ZERO,
        Fixed::ZERO,
        Fixed::from_int(w as i32),
        Fixed::from_int(h as i32),
    );
    let units = font.units_per_em.max(1) as f32;
    let scale = cell as f32 * 0.7 / units;
    let baseline = cell as f32 * 0.8;
    for (i, glyph) in font.glyphs.iter().enumerate() {
        let row = i as u32 / cols;
        let col = i as u32 % cols;
        let x0 = gap + col * (cell + gap);
        let y0 = gap + row * (cell + gap);
        if glyph.outline.is_empty() {
            continue;
        }
        let mut path = Path::new();
        for cmd in &glyph.outline {
            let mirui_cmd: PathCmd = cmd.clone().into();
            let mapped = map_freetype_cmd(&mirui_cmd, x0, y0, scale, baseline);
            match mapped {
                PathCmd::MoveTo(p) => {
                    path.move_to(p);
                }
                PathCmd::LineTo(p) => {
                    path.line_to(p);
                }
                PathCmd::QuadTo { ctrl, end } => {
                    path.quad_to(ctrl, end);
                }
                PathCmd::CubicTo { ctrl1, ctrl2, end } => {
                    path.cubic_to(ctrl1, ctrl2, end);
                }
                PathCmd::Close => {
                    path.close();
                }
            }
        }
        let paint = Paint::Color(color);
        renderer.fill_path(&path, &clip, &paint, 255, FillRule::NonZero);
    }
    renderer.flush();
    let mut img = RgbaImage::from_raw(grid_w, grid_h, buffer)
        .unwrap_or_else(|| RgbaImage::new(grid_w, grid_h));
    for px in img.pixels_mut() {
        px.0[0] = color.r;
        px.0[1] = color.g;
        px.0[2] = color.b;
    }
    img
}

pub fn render_freetype_text(
    font: &crate::midata::FreeTypeFontData,
    text: &str,
    width: u32,
    height: u32,
    color: mirx::Color,
) -> RgbaImage {
    if width == 0 || height == 0 || text.is_empty() {
        return RgbaImage::new(0, 0);
    }
    let chars: Vec<char> = text.chars().collect();
    if chars.is_empty() {
        return RgbaImage::new(0, 0);
    }
    let cell_width = (width / chars.len() as u32).max(1);
    let mut img = RgbaImage::new(width, height);
    for (idx, ch) in chars.into_iter().enumerate() {
        if let Some(glyph) = render_freetype_glyph_at(font, ch, cell_width, height, color) {
            overlay(&mut img, &glyph, i64::from(idx as u32 * cell_width), 0);
        }
    }
    img
}

fn map_freetype_cmd(cmd: &PathCmd, x0: u32, y0: u32, scale: f32, baseline: f32) -> PathCmd {
    let map_pt = |p: mirui::types::Point| -> mirui::types::Point {
        let raw_x = p.x.raw() as f32 / 256.0;
        let raw_y = p.y.raw() as f32 / 256.0;
        mirui::types::Point::new(
            Fixed::from_f32(x0 as f32 + raw_x * scale),
            Fixed::from_f32(y0 as f32 + baseline - raw_y * scale),
        )
    };
    match cmd {
        PathCmd::MoveTo(p) => PathCmd::MoveTo(map_pt(*p)),
        PathCmd::LineTo(p) => PathCmd::LineTo(map_pt(*p)),
        PathCmd::QuadTo { ctrl, end } => PathCmd::QuadTo {
            ctrl: map_pt(*ctrl),
            end: map_pt(*end),
        },
        PathCmd::CubicTo { ctrl1, ctrl2, end } => PathCmd::CubicTo {
            ctrl1: map_pt(*ctrl1),
            ctrl2: map_pt(*ctrl2),
            end: map_pt(*end),
        },
        PathCmd::Close => PathCmd::Close,
    }
}

#[allow(dead_code)]
fn sample_atlas_pixel(bytes: &[u8], source: u32, x: u32, y: u32, bit_depth: u8) -> u8 {
    let idx = y * source + x;
    match bit_depth {
        1 => {
            let byte = bytes[idx as usize / 8];
            let bit = 7 - (idx % 8) as u8;
            if (byte >> bit) & 1 == 1 {
                255
            } else {
                0
            }
        }
        2 => {
            let byte = bytes[idx as usize / 4];
            let shift = 6 - (idx % 4) as u8 * 2;
            let v = (byte >> shift) & 0x3;
            v * 85
        }
        4 => {
            let byte = bytes[idx as usize / 2];
            let shift = 4 - (idx % 2) as u8 * 4;
            let v = (byte >> shift) & 0xF;
            v * 17
        }
        8 => bytes[idx as usize],
        _ => 0,
    }
}

pub fn render_font_text(
    font: &mirx::Font,
    text: &str,
    width: u32,
    height: u32,
    color: mirx::Color,
) -> RgbaImage {
    if width == 0 || height == 0 || text.is_empty() {
        return RgbaImage::new(0, 0);
    }
    let payload: &'static [u8] = leak_font_payload(font);
    let mirui_font = match font.chunk_header.kind {
        FontChunkKind::Sdf => {
            match mirui::render::font::sdf::font_from_mirx_chunk("icu-preview", payload) {
                Ok(f) => f,
                Err(_) => return RgbaImage::new(width, height),
            }
        }
        FontChunkKind::Grayscale => {
            match mirui::render::font::gray::font_from_mirx_chunk("icu-preview", payload) {
                Ok(f) => f,
                Err(_) => return RgbaImage::new(width, height),
            }
        }
    };
    render_text_with_font(&mirui_font, text, width, height, color)
}

fn leak_font_payload(font: &mirx::Font) -> &'static [u8] {
    let payload = font.encode();
    Box::leak(payload.into_boxed_slice())
}

fn render_text_with_font(
    font: &Font,
    text: &str,
    width: u32,
    height: u32,
    color: mirx::Color,
) -> RgbaImage {
    let mut buffer = vec![0u8; (width * height * 4) as usize];
    let w = width.min(u16::MAX as u32) as u16;
    let h = height.min(u16::MAX as u32) as u16;
    let texture = Texture::new(&mut buffer, w, h, ColorFormat::RGBA8888);
    let mut renderer = SwRenderer::new(texture).with_alpha_mode(AlphaMode::Blend);
    let clip = Rect::new(
        Fixed::ZERO,
        Fixed::ZERO,
        Fixed::from_int(w as i32),
        Fixed::from_int(h as i32),
    );
    let pos = Point::new(Fixed::ZERO, Fixed::ZERO);
    let render_color = mirui::types::Color {
        r: color.r,
        g: color.g,
        b: color.b,
        a: color.a,
    };
    renderer.draw_label(&pos, text, font, &clip, &render_color, 255);
    renderer.flush();
    let mut img =
        RgbaImage::from_raw(width, height, buffer).unwrap_or_else(|| RgbaImage::new(width, height));
    for px in img.pixels_mut() {
        px.0[0] = color.r;
        px.0[1] = color.g;
        px.0[2] = color.b;
    }
    img
}

#[cfg(test)]
mod tests {
    use super::*;

    fn build_sdf_payload() -> Vec<u8> {
        mirx::Font {
            chunk_header: mirx::FontChunkHeader {
                kind: mirx::FontChunkKind::Sdf,
                format: 4,
                size: 24,
            },
            atlas: mirx::AtlasHeader {
                version: mirx::SUPPORTED_VERSION,
                bit_depth: 4,
                _pad0: 0,
                source_size: 4,
                spread: 1,
                glyph_count: 1,
                metric_offset: mirx::HEADER_LEN as u32,
                data_offset: (mirx::HEADER_LEN + mirx::METRIC_LEN) as u32,
                bytes_per_glyph: 8,
                ascender: 3,
                descender: 1,
                line_height: 4,
                _pad1: 0,
            },
            metrics: vec![mirx::GlyphMetric {
                codepoint: 'A' as u32,
                advance: 4,
                bearing_x: 0,
                bearing_y: 3,
            }],
            data: vec![0xFFu8; 8],
        }
        .encode()
    }

    #[test]
    fn render_atlas_returns_grid() {
        let payload = build_sdf_payload();
        let font = mirx::Font::decode(&payload).unwrap();
        let img = render_font_atlas(&font);
        assert!(img.width() > 0);
        assert!(img.height() > 0);
    }

    #[test]
    fn render_text_returns_image() {
        let payload = build_sdf_payload();
        let font = mirx::Font::decode(&payload).unwrap();
        let img = render_font_text(
            &font,
            "A",
            32,
            16,
            mirx::Color {
                r: 255,
                g: 255,
                b: 255,
                a: 255,
            },
        );
        assert_eq!(img.dimensions(), (32, 16));
    }
}