kobo-core 0.4.0

Kobo e-reader device SDK: device database, sysfs/ioctl, rendering, audio pipeline, trait surface
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
// SPDX-License-Identifier: MIT
// Copyright (c) 2026 Nayeem Bin Ahsan
use super::*;

fn white_buffer(w: usize, h: usize) -> Vec<u8> {
    vec![0xFF; w * h * 2]
}

#[test]
fn line_height_positive_for_body_size() {
    let lh = line_height(36.0);
    assert!(lh > 0);
    assert!(lh < 100);
}

#[test]
fn line_height_scales_with_size() {
    let small = line_height(20.0);
    let large = line_height(60.0);
    assert!(large > small);
}

#[test]
fn detect_script_latin() {
    assert_eq!(detect_script("Hello world"), Script::Latin);
}

#[test]
fn detect_script_bengali() {
    assert_eq!(detect_script("নমস্কার"), Script::Bengali);
}

#[test]
fn detect_script_arabic() {
    assert_eq!(detect_script("مرحبا"), Script::Arabic);
}

#[test]
fn detect_script_devanagari() {
    assert_eq!(detect_script("नमस्ते"), Script::Devanagari);
}

#[test]
fn detect_script_japanese_from_kana() {
    assert_eq!(detect_script("こんにちは"), Script::Japanese);
    // Kana mixed with Han is still Japanese - the Han belongs to the kana.
    assert_eq!(detect_script("日本語を読む"), Script::Japanese);
}

#[test]
fn detect_script_korean_from_hangul() {
    assert_eq!(detect_script("안녕하세요"), Script::Korean);
}

#[test]
fn detect_script_chinese_from_bare_han() {
    // Han with no kana anywhere. Japanese prose never runs this long without
    // kana, so unaccompanied Han resolves to Chinese.
    assert_eq!(detect_script("我们在读一本书"), Script::Chinese);
}

#[test]
fn detect_script_covers_the_newly_added_scripts() {
    assert_eq!(detect_script("Привет мир"), Script::Cyrillic);
    assert_eq!(detect_script("Καλημέρα"), Script::Greek);
    assert_eq!(detect_script("שלום עולם"), Script::Hebrew);
    assert_eq!(detect_script("வணக்கம்"), Script::Tamil);
    assert_eq!(detect_script("ជំរាបសួរ"), Script::Khmer);
    assert_eq!(detect_script("မင်္ဂလာပါ"), Script::Myanmar);
    assert_eq!(detect_script("ສະບາຍດີ"), Script::Lao);
    assert_eq!(detect_script("ආයුබෝවන්"), Script::Sinhala);
    assert_eq!(detect_script("გამარჯობა"), Script::Georgian);
    assert_eq!(detect_script("ሰላም"), Script::Ethiopic);
}

#[test]
fn detect_script_takes_the_majority_not_the_first_letter() {
    // A Bengali chapter that opens with an English word is still Bengali.
    assert_eq!(
        detect_script("Chapter এক নতুন দিন শুরু হলো আজ"),
        Script::Bengali
    );
    // An English sentence quoting one Greek word is still English.
    assert_eq!(detect_script("the word λόγος means reason"), Script::Latin);
}

#[test]
fn detect_script_latin_covers_vietnamese_diacritics() {
    // Latin Extended Additional. Without it these fall to `Other` and get read
    // aloud in English.
    assert_eq!(detect_script("Xin chào các bạn"), Script::Latin);
}

#[test]
fn detect_script_skips_whitespace_punct() {
    assert_eq!(detect_script("  \"hello"), Script::Latin);
    assert_eq!(detect_script("  \nনমস্কার"), Script::Bengali);
}

#[test]
fn detect_script_skips_unicode_quote_prefix() {
    assert_eq!(detect_script("\u{201c}আমি\u{201d}"), Script::Bengali);
    assert_eq!(
        detect_script("\u{201c}\u{3053}\u{3093}\u{306b}\u{3061}\u{306f}\u{201d}"),
        Script::Japanese
    );
}

#[test]
fn detect_script_skips_digit_prefix() {
    assert_eq!(detect_script("1. নমস্কার"), Script::Bengali);
    // Bare Han, so this resolves to Chinese. A Japanese book reaches Japanese
    // via its kana, or via `dc:language` when the run is this short.
    assert_eq!(detect_script("2. 日本語"), Script::Chinese);
}

#[test]
fn detect_script_empty_defaults_latin() {
    assert_eq!(detect_script(""), Script::Latin);
}

#[test]
fn rtl_detection() {
    assert!(Script::Arabic.is_rtl());
    assert!(Script::Hebrew.is_rtl());
    assert!(!Script::Latin.is_rtl());
    assert!(!Script::Bengali.is_rtl());
}

#[test]
fn word_spacing_detection() {
    assert!(Script::Latin.uses_word_spacing());
    assert!(Script::Bengali.uses_word_spacing());
    assert!(!Script::Japanese.uses_word_spacing());
    assert!(!Script::Chinese.uses_word_spacing());
    assert!(!Script::Korean.uses_word_spacing());
    assert!(!Script::Thai.uses_word_spacing());
    assert!(!Script::Khmer.uses_word_spacing());
    assert!(!Script::Lao.uses_word_spacing());
    assert!(!Script::Myanmar.uses_word_spacing());
}

#[test]
fn word_width_positive_for_nonempty() {
    assert!(word_width("hello", 36.0) > 0.0);
}

#[test]
fn word_width_empty_is_zero() {
    assert_eq!(word_width("", 36.0), 0.0);
}

#[test]
fn word_width_longer_word_is_wider() {
    let short = word_width("hi", 36.0);
    let long = word_width("hello world", 36.0);
    assert!(long > short);
}

#[test]
fn decode_image_returns_correct_dimensions() {
    let img = image::DynamicImage::new_rgb8(20, 15);
    let mut buf = Vec::new();
    img.write_to(&mut std::io::Cursor::new(&mut buf), image::ImageFormat::Png)
        .unwrap();
    let decoded = decode_image(&buf, 100, 100).unwrap();
    assert!(decoded.width > 0 && decoded.height > 0);
    assert_eq!(decoded.rgb.len(), decoded.width * decoded.height * 3);
}

#[test]
fn decode_image_caps_height() {
    let img = image::DynamicImage::new_rgb8(10, 200);
    let mut buf = Vec::new();
    img.write_to(&mut std::io::Cursor::new(&mut buf), image::ImageFormat::Png)
        .unwrap();
    let h = decode_image(&buf, 100, 50).unwrap().height;
    assert!(h <= 50);
}

#[test]
fn decode_image_returns_none_for_invalid_data() {
    assert!(decode_image(b"not an image", 100, 100).is_none());
}

#[test]
fn decode_image_returns_none_for_empty() {
    assert!(decode_image(&[], 100, 100).is_none());
}

#[test]
fn blit_draws_visible_text() {
    let (w, h) = (200usize, 64usize);
    let mut buf = white_buffer(w, h);
    blit_rgb565(&mut buf, w, "Hello", 32.0, 10, 10, w, h);
    assert!(
        buf.iter().any(|&b| b != 0xFF),
        "blitting visible text must change at least one pixel"
    );
}

#[test]
fn blit_empty_text_changes_nothing() {
    let (w, h) = (100usize, 32usize);
    let mut buf = white_buffer(w, h);
    let before = buf.clone();
    blit_rgb565(&mut buf, w, "", 32.0, 0, 0, w, h);
    assert_eq!(buf, before, "empty text must not modify the buffer");
}

#[test]
fn blit_out_of_bounds_origin_is_safe() {
    let (w, h) = (50usize, 20usize);
    let mut buf = white_buffer(w, h);
    let before = buf.clone();
    blit_rgb565(&mut buf, w, "Wide", 24.0, 1000, 1000, w, h);
    assert_eq!(buf, before, "OOB blit must not modify the buffer");
}

#[test]
fn blit_color_tints_toward_color() {
    let (w, h) = (120usize, 40usize);
    let mut buf = white_buffer(w, h);
    blit_rgb565_color(&mut buf, w, "ABC", 28.0, 5, 5, 0xF800, w, h);
    let changed: Vec<u16> = (0..w * h)
        .map(|i| {
            let o = i * 2;
            (buf[o] as u16) | ((buf[o + 1] as u16) << 8)
        })
        .filter(|&v| v != 0xFFFF)
        .collect();
    assert!(!changed.is_empty(), "text must be drawn");
    assert!(
        changed.iter().all(|&v| (v >> 11) & 0x1f != 0),
        "changed pixels must carry red color"
    );
}

// ---- styled text ---------------------------------------------------------

fn ink(text: &str, px: f32, style: TextStyle) -> usize {
    let (w, h) = (400usize, 120usize);
    let mut buf = vec![0xFFu8; w * h * 2];
    blit_rgb565_styled(&mut buf, w, text, px, 10, 10, 0x0000, style, w, h);
    buf.chunks_exact(2)
        .filter(|p| p[0] != 0xFF || p[1] != 0xFF)
        .count()
}

/// Monospace is a real face, so it must actually change the metrics -- that is
/// the whole reason it is bundled rather than synthesised.
#[test]
fn mono_changes_advances() {
    let plain = word_width("iiiii", 40.0);
    let mono = word_width_styled(
        "iiiii",
        40.0,
        TextStyle {
            mono: true,
            ..Default::default()
        },
    );
    assert!(
        (mono - plain).abs() > 1.0,
        "narrow glyphs should widen in a fixed-advance face: {plain} vs {mono}"
    );
}

/// The defining property of a monospace face: every character takes the same
/// space, which is what makes code columns line up.
#[test]
fn mono_advances_are_uniform() {
    let style = TextStyle {
        mono: true,
        ..Default::default()
    };
    let narrow = word_width_styled("iiii", 40.0, style);
    let wide = word_width_styled("MMMM", 40.0, style);
    assert!(
        (narrow - wide).abs() < 0.5,
        "fixed advances should match: {narrow} vs {wide}"
    );
}

/// Bold and italic must NOT change advances, or emphasis would reflow the
/// paragraph around it.
#[test]
fn synthetic_emphasis_keeps_the_regular_advances() {
    let plain = word_width("Hello world", 40.0);
    for style in [
        TextStyle {
            bold: true,
            ..Default::default()
        },
        TextStyle {
            italic: true,
            ..Default::default()
        },
        TextStyle {
            bold: true,
            italic: true,
            ..Default::default()
        },
    ] {
        let w = word_width_styled("Hello world", 40.0, style);
        assert_eq!(w, plain, "style {style:?} moved the pen");
    }
}

#[test]
fn synthetic_bold_inks_more_than_regular() {
    let plain = ink("Hello", 40.0, TextStyle::PLAIN);
    let bold = ink(
        "Hello",
        40.0,
        TextStyle {
            bold: true,
            ..Default::default()
        },
    );
    assert!(
        bold > plain,
        "bold should thicken strokes: {plain} -> {bold}"
    );
}

/// Shearing must lean the glyphs, not simply redraw them in place.
#[test]
fn synthetic_italic_moves_pixels() {
    let (w, h) = (400usize, 120usize);
    let render = |style| {
        let mut buf = vec![0xFFu8; w * h * 2];
        blit_rgb565_styled(&mut buf, w, "Hello", 40.0, 10, 10, 0x0000, style, w, h);
        buf
    };
    let plain = render(TextStyle::PLAIN);
    let italic = render(TextStyle {
        italic: true,
        ..Default::default()
    });
    assert_ne!(
        plain, italic,
        "italic should not render identically to regular"
    );
}

#[test]
fn plain_style_renders_exactly_as_the_unstyled_path() {
    let (w, h) = (400usize, 120usize);
    let mut a = vec![0xFFu8; w * h * 2];
    let mut b = vec![0xFFu8; w * h * 2];
    blit_rgb565(&mut a, w, "Hello world", 36.0, 10, 10, w, h);
    blit_rgb565_styled(
        &mut b,
        w,
        "Hello world",
        36.0,
        10,
        10,
        0x0000,
        TextStyle::PLAIN,
        w,
        h,
    );
    assert_eq!(a, b, "adding styles must not disturb ordinary text");
}

#[test]
fn style_bits_are_distinct() {
    let mut seen = std::collections::HashSet::new();
    for bold in [false, true] {
        for italic in [false, true] {
            for mono in [false, true] {
                assert!(seen.insert(
                    TextStyle {
                        bold,
                        italic,
                        mono,
                        link: false
                    }
                    .bits()
                ));
            }
        }
    }
    assert!(TextStyle::PLAIN.is_plain());
}

/// The CJK faces ship as CFF-outline OTF (noto-cjk publishes no static TTF),
/// while every other face is TrueType. `fontdue` rasterises via
/// `ttf_parser::outline_glyph`, which handles both - but a face that parses and
/// then rasterises to nothing would be a silent blank screen on the device, so
/// this asserts real glyph coverage and a non-empty bitmap.
///
/// Skips when the font set has not been fetched; see `scripts/fetch-fonts.ps1`.
#[test]
fn shipped_faces_parse_and_rasterise() {
    let dir = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
        .parent()
        .map(|p| p.join("package").join("fonts"));
    let Some(dir) = dir else { return };
    if !dir.is_dir() {
        return;
    }

    // (file, a character that face must be able to draw)
    let cases = [
        ("NotoSansSC.ttf", ''),
        ("NotoSansKR.ttf", ''),
        ("NotoSansJP.ttf", ''),
        ("NotoSansHebrew.ttf", 'א'),
        ("NotoSansTamil.ttf", ''),
        ("NotoSans.ttf", 'Ж'),
    ];

    let mut checked = 0;
    for (name, ch) in cases {
        let path = dir.join(name);
        if !path.is_file() {
            continue;
        }
        let data = std::fs::read(&path).expect("read font");

        assert!(
            font_covers(&data, &ch.to_string()),
            "{name}: no glyph for {ch:?}"
        );

        let font = fontdue::Font::from_bytes(data.as_slice(), fontdue::FontSettings::default())
            .unwrap_or_else(|e| panic!("{name}: fontdue rejected the face: {e}"));

        let (metrics, bitmap) = font.rasterize(ch, 32.0);
        assert!(
            metrics.width > 0 && metrics.height > 0,
            "{name}: {ch:?} rasterised to a zero-size bitmap"
        );
        assert!(
            bitmap.iter().any(|&p| p > 0),
            "{name}: {ch:?} rasterised to an entirely blank bitmap"
        );
        checked += 1;
    }

    assert!(checked > 0, "font dir present but no expected faces found");
}