cvkg-runic-text 0.2.12

Natively integrated Cyber Viking text shaping and layout engine for CVKG
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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
// ── Color Emoji Atlas ───────────────────────────────────────────────────────
///
/// Provides emoji glyph detection and atlas packing for color emoji rendering.
/// Uses Unicode emoji range detection and shelf-packing for atlas management.
/// A single color emoji glyph.
#[derive(Debug, Clone, PartialEq)]
pub struct EmojiGlyph {
    /// The Unicode codepoint for this emoji.
    pub codepoint: u32,
    /// X position in the atlas (pixels).
    pub atlas_x: u32,
    /// Y position in the atlas (pixels).
    pub atlas_y: u32,
    /// Width of the glyph cell in the atlas.
    pub atlas_w: u32,
    /// Height of the glyph cell in the atlas.
    pub atlas_h: u32,
    /// RGBA bitmap data for the emoji, row-major, 4 bytes per pixel.
    pub bitmap: Vec<u8>,
}

impl EmojiGlyph {
    /// Creates a new emoji glyph entry.
    pub fn new(
        codepoint: u32,
        atlas_x: u32,
        atlas_y: u32,
        w: u32,
        h: u32,
        bitmap: Vec<u8>,
    ) -> Self {
        Self {
            codepoint,
            atlas_x,
            atlas_y,
            atlas_w: w,
            atlas_h: h,
            bitmap,
        }
    }

    /// Returns the expected bitmap size in bytes for the given dimensions.
    pub fn expected_bitmap_size(w: u32, h: u32) -> usize {
        w as usize * h as usize * 4
    }

    /// Returns true if the bitmap has the correct size.
    pub fn bitmap_valid(&self) -> bool {
        self.bitmap.len() == Self::expected_bitmap_size(self.atlas_w, self.atlas_h)
    }
}

/// A shelf-packed atlas of color emoji glyphs.
#[derive(Debug, Clone, PartialEq)]
pub struct EmojiAtlas {
    /// Atlas width in pixels.
    pub width: u32,
    /// Atlas height in pixels.
    pub height: u32,
    /// Emoji glyphs in this atlas.
    pub glyphs: Vec<EmojiGlyph>,
}

impl EmojiAtlas {
    /// Creates an empty emoji atlas with the given dimensions.
    pub fn new(width: u32, height: u32) -> Self {
        Self {
            width,
            height,
            glyphs: Vec::new(),
        }
    }

    /// Inserts an emoji glyph into the atlas.
    pub fn insert(&mut self, glyph: EmojiGlyph) {
        self.glyphs.push(glyph);
    }

    /// Looks up an emoji glyph by codepoint.
    pub fn get_glyph(&self, codepoint: u32) -> Option<&EmojiGlyph> {
        self.glyphs.iter().find(|g| g.codepoint == codepoint)
    }

    /// Returns the number of glyphs.
    pub fn len(&self) -> usize {
        self.glyphs.len()
    }

    /// Returns true if the atlas is empty.
    pub fn is_empty(&self) -> bool {
        self.glyphs.is_empty()
    }

    /// Packs glyphs into this atlas using shelf packing.
    /// Sets atlas_x and atlas_y on each glyph.
    pub fn pack(&mut self) -> Result<(), String> {
        if self.glyphs.is_empty() {
            return Ok(());
        }

        // Sort by height descending
        self.glyphs.sort_by_key(|b| std::cmp::Reverse(b.atlas_h));

        let mut current_x: u32 = 0;
        let mut current_y: u32 = 0;
        let mut shelf_height: u32 = 0;

        for glyph in self.glyphs.iter_mut() {
            if glyph.atlas_w > self.width || glyph.atlas_h > self.height {
                return Err(format!(
                    "Emoji U+{:04X} ({}x{}) exceeds atlas size {}x{}",
                    glyph.codepoint, glyph.atlas_w, glyph.atlas_h, self.width, self.height
                ));
            }

            if current_x + glyph.atlas_w > self.width {
                current_y += shelf_height;
                current_x = 0;
                shelf_height = 0;
            }

            if current_y + glyph.atlas_h > self.height {
                return Err(format!(
                    "Atlas {}x{} full, cannot pack emoji U+{:04X}",
                    self.width, self.height, glyph.codepoint
                ));
            }

            glyph.atlas_x = current_x;
            glyph.atlas_y = current_y;
            current_x += glyph.atlas_w;
            shelf_height = shelf_height.max(glyph.atlas_h);
        }

        Ok(())
    }
}

impl Default for EmojiAtlas {
    fn default() -> Self {
        Self::new(2048, 2048)
    }
}

/// Returns true if the given Unicode codepoint is in an emoji range.
///
/// Covers the major emoji blocks:
/// - U+1F600..U+1F64F: Emoticons
/// - U+1F300..U+1F5FF: Misc Symbols and Pictographs
/// - U+1F680..U+1F6FF: Transport and Map
/// - U+1F1E0..U+1F1FF: Regional Indicators (flags)
/// - U+2600..U+26FF: Misc Symbols
/// - U+2700..U+27BF: Dingbats
/// - U+1F900..U+1F9FF: Supplemental Symbols
/// - U+1FA00..U+1FA6F: Chess Symbols
/// - U+1FA70..U+1FAFF: Symbols and Pictographs Extended-A
/// - U+FE0F: Variation Selector-16 (emoji presentation)
/// - U+200D: Zero Width Joiner (emoji sequences)
pub fn is_emoji(codepoint: u32) -> bool {
    matches!(codepoint,
        0x1F600..=0x1F64F | // Emoticons
        0x1F300..=0x1F5FF | // Misc Symbols and Pictographs
        0x1F680..=0x1F6FF | // Transport and Map
        0x1F1E0..=0x1F1FF | // Regional Indicators
        0x2600..=0x26FF |   // Misc Symbols
        0x2700..=0x27BF |   // Dingbats
        0x1F900..=0x1F9FF | // Supplemental Symbols
        0x1FA00..=0x1FA6F | // Chess Symbols
        0x1FA70..=0x1FAFF | // Extended-A
        0xFE0F |            // Variation Selector-16
        0x200D              // ZWJ
    )
}

/// Returns true if the codepoint is a base emoji (not a modifier or joiner).
pub fn is_emoji_base(codepoint: u32) -> bool {
    matches!(codepoint,
        0x1F600..=0x1F64F |
        0x1F300..=0x1F5FF |
        0x1F680..=0x1F6FF |
        0x1F1E0..=0x1F1FF |
        0x2600..=0x26FF |
        0x2700..=0x27BF |
        0x1F900..=0x1F9FF |
        0x1FA00..=0x1FA6F |
        0x1FA70..=0x1FAFF
    )
}

/// Renders an emoji codepoint to an RGBA bitmap.
///
/// This is a placeholder that generates a colored square with the emoji
/// codepoint rendered as text. For production use, this would load a color
/// emoji font (like Noto Color Emoji) and rasterize the glyph.
///
/// # Arguments
/// * `codepoint` - Unicode codepoint of the emoji.
/// * `size` - Output bitmap size (width = height = size).
///
/// # Returns
/// `Some(Vec<u8>)` with RGBA data, or `None` if the codepoint is not a valid emoji.
pub fn render_emoji(codepoint: u32, size: u32) -> Option<Vec<u8>> {
    if !is_emoji_base(codepoint) {
        return None;
    }

    let total_pixels = (size * size) as usize;
    let mut rgba = vec![0u8; total_pixels * 4];

    // Generate a deterministic color from the codepoint
    let r = ((codepoint >> 16) & 0xFF) as u8;
    let g = ((codepoint >> 8) & 0xFF) as u8;
    let b = (codepoint & 0xFF) as u8;

    // Fill the bitmap with a colored circle on a transparent background
    let center = size as f32 / 2.0;
    let radius = size as f32 * 0.4;

    for y in 0..size {
        for x in 0..size {
            let dx = x as f32 + 0.5 - center;
            let dy = y as f32 + 0.5 - center;
            let dist_sq = dx * dx + dy * dy;
            let idx = (y * size + x) as usize * 4;

            if dist_sq <= radius * radius {
                // Inside the circle
                let dist = dist_sq.sqrt();
                let alpha = if dist > radius - 2.0 {
                    // Anti-aliased edge
                    ((radius - dist) / 2.0 * 255.0) as u8
                } else {
                    255
                };
                rgba[idx] = r;
                rgba[idx + 1] = g;
                rgba[idx + 2] = b;
                rgba[idx + 3] = alpha;
            }
        }
    }

    Some(rgba)
}

/// Detects emoji runs in text and returns their byte ranges.
///
/// Returns a Vec of (start_byte, end_byte, codepoint) for each emoji found.
pub fn find_emoji_runs(text: &str) -> Vec<(usize, usize, u32)> {
    let mut runs = Vec::new();
    let mut current_start: Option<usize> = None;
    let mut current_codepoint: u32 = 0;

    for (byte_idx, ch) in text.char_indices() {
        let cp = ch as u32;
        if is_emoji_base(cp) {
            if current_start.is_none() {
                current_start = Some(byte_idx);
                current_codepoint = cp;
            }
        } else if is_emoji(cp) {
            // Modifier or joiner, continue the current run
        } else {
            // Not an emoji, close any open run
            if let Some(start) = current_start {
                runs.push((start, byte_idx, current_codepoint));
                current_start = None;
            }
        }
    }

    // Close any remaining run
    if let Some(start) = current_start {
        runs.push((start, text.len(), current_codepoint));
    }

    runs
}

/// Packs a set of emoji glyphs into an atlas.
///
/// # Arguments
/// * `glyphs` - Mutable slice of EmojiGlyph entries.
/// * `max_size` - Maximum atlas dimension.
///
/// # Returns
/// `Ok((width, height))` with final atlas dimensions.
pub fn pack_emoji_atlas(glyphs: &mut [EmojiGlyph], max_size: u32) -> Result<(u32, u32), String> {
    if glyphs.is_empty() {
        return Ok((0, 0));
    }

    // Sort by height descending
    glyphs.sort_by_key(|b| std::cmp::Reverse(b.atlas_h));

    let mut current_x: u32 = 0;
    let mut current_y: u32 = 0;
    let mut shelf_height: u32 = 0;
    let mut max_x: u32 = 0;

    for glyph in glyphs.iter_mut() {
        if glyph.atlas_w > max_size || glyph.atlas_h > max_size {
            return Err(format!(
                "Emoji U+{:04X} ({}x{}) exceeds max atlas size {}",
                glyph.codepoint, glyph.atlas_w, glyph.atlas_h, max_size
            ));
        }

        if current_x + glyph.atlas_w > max_size {
            current_y += shelf_height;
            current_x = 0;
            shelf_height = 0;
        }

        glyph.atlas_x = current_x;
        glyph.atlas_y = current_y;
        current_x += glyph.atlas_w;
        shelf_height = shelf_height.max(glyph.atlas_h);
        max_x = max_x.max(current_x);
    }

    let total_height = current_y + shelf_height;
    Ok((max_x, total_height))
}

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

    #[test]
    fn test_is_emoji_emoticons() {
        assert!(is_emoji(0x1F600)); // 😀
        assert!(is_emoji(0x1F64F)); // 🙏
        assert!(!is_emoji(0x0041)); // 'A'
    }

    #[test]
    fn test_is_emoji_misc_symbols() {
        assert!(is_emoji(0x1F300)); // 🌀
        assert!(is_emoji(0x1F5FF)); // 🗿
    }

    #[test]
    fn test_is_emoji_transport() {
        assert!(is_emoji(0x1F680)); // 🚀
        assert!(is_emoji(0x1F6FF)); // 🛿
    }

    #[test]
    fn test_is_emoji_flags() {
        assert!(is_emoji(0x1F1FA)); // Regional indicator U
        assert!(is_emoji(0x1F1F8)); // Regional indicator S
    }

    #[test]
    fn test_is_emoji_dingbats() {
        assert!(is_emoji(0x2764)); //        assert!(is_emoji(0x2728)); //    }

    #[test]
    fn test_is_emoji_variation_selector() {
        assert!(is_emoji(0xFE0F));
    }

    #[test]
    fn test_is_emoji_zwj() {
        assert!(is_emoji(0x200D));
    }

    #[test]
    fn test_is_emoji_base() {
        assert!(is_emoji_base(0x1F600));
        assert!(!is_emoji_base(0xFE0F));
        assert!(!is_emoji_base(0x200D));
    }

    #[test]
    fn test_render_emoji_smiley() {
        let bitmap = render_emoji(0x1F600, 64);
        assert!(bitmap.is_some());
        let data = bitmap.unwrap();
        assert_eq!(data.len(), 64 * 64 * 4);
    }

    #[test]
    fn test_render_emoji_non_emoji() {
        assert!(render_emoji(0x0041, 64).is_none()); // 'A' is not an emoji
    }

    #[test]
    fn test_render_emoji_vs_is_none() {
        assert!(render_emoji(0xFE0F, 64).is_none()); // variation selector is not base
    }

    #[test]
    fn test_emoji_glyph_valid() {
        let bitmap = vec![0u8; 32 * 32 * 4];
        let glyph = EmojiGlyph::new(0x1F600, 0, 0, 32, 32, bitmap);
        assert!(glyph.bitmap_valid());
    }

    #[test]
    fn test_emoji_glyph_invalid() {
        let bitmap = vec![0u8; 100]; // Wrong size
        let glyph = EmojiGlyph::new(0x1F600, 0, 0, 32, 32, bitmap);
        assert!(!glyph.bitmap_valid());
    }

    #[test]
    fn test_emoji_atlas_insert() {
        let mut atlas = EmojiAtlas::new(512, 512);
        assert!(atlas.is_empty());
        atlas.insert(EmojiGlyph::new(
            0x1F600,
            0,
            0,
            64,
            64,
            vec![0u8; 64 * 64 * 4],
        ));
        assert_eq!(atlas.len(), 1);
        assert!(atlas.get_glyph(0x1F600).is_some());
        assert!(atlas.get_glyph(0x1F601).is_none());
    }

    #[test]
    fn test_find_emoji_runs() {
        let text = "hello 😀 world";
        let runs = find_emoji_runs(text);
        assert_eq!(runs.len(), 1);
        let (start, end, cp) = runs[0];
        assert_eq!(cp, 0x1F600);
        assert!(start > 0);
        assert_eq!(end - start, "😀".len());
    }

    #[test]
    fn test_find_multiple_emoji_runs() {
        let text = "😀 hello 😁 world 😂";
        let runs = find_emoji_runs(text);
        assert_eq!(runs.len(), 3);
        assert_eq!(runs[0].2, 0x1F600);
        assert_eq!(runs[1].2, 0x1F601);
        assert_eq!(runs[2].2, 0x1F602);
    }

    #[test]
    fn test_find_no_emoji() {
        let text = "hello world";
        let runs = find_emoji_runs(text);
        assert!(runs.is_empty());
    }

    #[test]
    fn test_pack_emoji_atlas_empty() {
        let mut glyphs: Vec<EmojiGlyph> = vec![];
        let result = pack_emoji_atlas(&mut glyphs, 1024);
        assert_eq!(result, Ok((0, 0)));
    }

    #[test]
    fn test_pack_emoji_atlas_single() {
        let bitmap = vec![0u8; 64 * 64 * 4];
        let mut glyphs = vec![EmojiGlyph::new(0x1F600, 0, 0, 64, 64, bitmap)];
        let (w, h) = pack_emoji_atlas(&mut glyphs, 1024).unwrap();
        assert_eq!(w, 64);
        assert_eq!(h, 64);
        assert_eq!(glyphs[0].atlas_x, 0);
        assert_eq!(glyphs[0].atlas_y, 0);
    }

    #[test]
    fn test_atlas_pack_method() {
        let bitmap = vec![0u8; 64 * 64 * 4];
        let mut atlas = EmojiAtlas::new(128, 128);
        atlas.insert(EmojiGlyph::new(0x1F600, 0, 0, 64, 64, bitmap.clone()));
        atlas.insert(EmojiGlyph::new(0x1F601, 0, 0, 64, 64, bitmap));
        assert!(atlas.pack().is_ok());
        assert_eq!(atlas.get_glyph(0x1F600).unwrap().atlas_x, 0);
        assert_eq!(atlas.get_glyph(0x1F601).unwrap().atlas_x, 64);
    }

    #[test]
    fn test_atlas_pack_overflow() {
        let bitmap = vec![0u8; 64 * 64 * 4];
        let mut atlas = EmojiAtlas::new(64, 64);
        // Two 64x64 glyphs can't fit in 64x64 after the first (shelf is full height)
        atlas.insert(EmojiGlyph::new(0x1F600, 0, 0, 64, 64, bitmap.clone()));
        atlas.insert(EmojiGlyph::new(0x1F601, 0, 0, 64, 64, bitmap));
        // Actually they fit on separate shelves: shelf 1 at y=0, shelf 2 at y=64
        // But atlas height is only 64, so shelf 2 overflows
        assert!(atlas.pack().is_err());
    }
}