cmap format 4 drops glyph IDs above 32767 in the glyphIdArray path

Aperta Adatta ai principianti
#247 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

  • #248 di @msl2246 — chiusa senza merge

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
72/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
rust
Ambito
data

Direzione di ricerca

Il bug si trova nel ramo glyphIdArray del Formato 4 in src/tables/cmap/format4.rs, intorno alle righe 85-93. Leggi come vengono eseguiti il controllo delle voci zero e la somma di idDelta, poi esegui il repro sintetico dell'issue per vedere che viene restituito None per 0xE123. Il lavoro è completo quando quel test restituisce Some(GlyphId(0x8001)), i test cmap esistenti in tests/tables/cmap.rs continuano a passare e viene aggiunto lì un test di regressione.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Summary

In the Format 4 branch where idRangeOffset is nonzero, Subtable4::glyph_index casts the glyphIdArray entry to i16, adds idDelta, and tries to convert the signed result back to u16. When the correct result is above 32767, the signed intermediate is negative and the lookup returns None. As a result, text renderers can treat mapped characters as missing and use a fallback font. This can affect large CJK fonts.

The bug is present in the current release, 0.25.1, and in main at 0c7291223fe9e0bc2808f0255cb991066eedd796. Release v0.25.1 · Format 4 lookup on main

OpenType defines glyphIdArray entries as unsigned 16-bit values. A zero entry denotes a missing glyph. For a nonzero entry, the glyph ID is the entry plus idDelta modulo 65536. OpenType cmap specification, Format 4

Minimal reproduction

Add ttf-parser = "=0.25.1" under [dependencies] in a Rust library crate. Put this test in src/lib.rs and run cargo test. The entire subtable is synthetic and contains one mapping and the required sentinel.

#[test]
fn format4_indirect_lookup_preserves_high_glyph_ids() {
    use ttf_parser::{cmap::Subtable4, GlyphId};

    // Synthetic Format 4 subtable with one mapped code and a sentinel.
    let data = [
        0x00, 0x04, 0x00, 0x22, 0x00, 0x00, // format, length, language
        0x00, 0x04, 0x00, 0x04, 0x00, 0x01, 0x00, 0x00, // search parameters
        0xE1, 0x23, 0xFF, 0xFF, // endCode
        0x00, 0x00, // reservedPad
        0xE1, 0x23, 0xFF, 0xFF, // startCode
        0x00, 0x00, 0x00, 0x01, // idDelta
        0x00, 0x04, 0x00, 0x00, // idRangeOffset
        0x80, 0x01, // glyphIdArray
    ];

    let subtable = Subtable4::parse(&data).unwrap();
    assert_eq!(subtable.glyph_index(0xE123), Some(GlyphId(0x8001)));
}
  • Expected — Some(GlyphId(0x8001)) (32769).
  • Actual — None.

Proposed fix

Add the delta with unsigned 16-bit wrapping arithmetic after the existing zero-entry check. Casting idDelta to u16 preserves its value modulo 65536, including for negative deltas.

diff --git a/src/tables/cmap/format4.rs b/src/tables/cmap/format4.rs
--- a/src/tables/cmap/format4.rs
+++ b/src/tables/cmap/format4.rs
@@ -88,8 +88,8 @@
                     if glyph_array_value == 0 {
                         return None;
                     }
 
-                    let glyph_id = (glyph_array_value as i16).wrapping_add(id_delta);
-                    return u16::try_from(glyph_id).ok().map(GlyphId);
+                    let glyph_id = glyph_array_value.wrapping_add(id_delta as u16);
+                    return Some(GlyphId(glyph_id));
                 }
             } else {

I can open a PR with the fix and a synthetic regression test in tests/tables/cmap.rs.

Lingua principale
Rust
Stelle
798
Fork
96
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di harfbuzz/ttf-parser

Tutte le issue di harfbuzz/ttf-parser

Issue simili

Altre issue su Rust

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.