package typeface import "encoding/binary" // Outline checks: what Inspect can check about a face's glyphs cheaply, // before the file is kept. They read structure only, never a glyph's // instructions or charstrings: a TrueType face's loca must climb inside // glyf, each glyph must hold at least its header, and maxp's glyph count // must agree with loca and hmtx; a CFF or CFF2 table's header and // top-level INDEXes must parse inside the table, with as many // CharStrings as maxp has glyphs. Anything deeper is left to the // browser's own font sanitiser and to the app's parser when it draws. // // A WOFF2's transformed glyf, loca and hmtx are not rebuilt, so they are // not checked here. // checkOutlines runs the checks for the face's outline kind. func checkOutlines(ft fontTables, outline Outline) error { maxp := ft.data["maxp"] if len(maxp) < 6 { return malformed("no maxp table") } n := int(binary.BigEndian.Uint16(maxp[4:])) if n == 0 { return malformed("maxp counts no glyphs") } if outline == CFF { if b, ok := ft.data["CFF2"]; ok { return checkCFF2(b, n) } return checkCFF(ft.data["CFF "], n) } if err := checkMetrics(ft, n); err != nil { return err } return checkGlyf(ft, n) } // checkMetrics checks hhea's numberOfHMetrics against maxp and hmtx. func checkMetrics(ft fontTables, n int) error { hhea := ft.data["hhea"] if len(hhea) < 36 { return malformed("no hhea table") } m := int(binary.BigEndian.Uint16(hhea[34:])) if m == 0 || m > n { return malformed("%d horizontal metrics for %d glyphs", m, n) } if ft.transformed["hmtx"] { return nil } if need := 4*m + 2*(n-m); len(ft.data["hmtx"]) < need { return malformed("hmtx holds %d bytes, its metrics need %d", len(ft.data["hmtx"]), need) } return nil } // checkGlyf checks loca against glyf and maxp. func checkGlyf(ft fontTables, n int) error { if ft.transformed["glyf"] { return nil } head := ft.data["head"] // describe has checked its length size := 0 switch int16(binary.BigEndian.Uint16(head[50:])) { case 0: size = 2 case 1: size = 4 default: return malformed("indexToLocFormat is neither short nor long") } loca, glyf := ft.data["loca"], ft.data["glyf"] if len(loca) < size*(n+1) { return malformed("loca holds %d bytes, %d glyphs need %d", len(loca), n, size*(n+1)) } at := func(i int) uint64 { if size == 2 { return 2 * uint64(binary.BigEndian.Uint16(loca[2*i:])) } return uint64(binary.BigEndian.Uint32(loca[4*i:])) } prev := at(0) for i := 1; i <= n; i++ { next := at(i) if next < prev { return malformed("loca goes backwards at glyph %d", i-1) } if next > uint64(len(glyf)) { return malformed("glyph %d runs past glyf", i-1) } // A glyph with any bytes has at least its 10-byte header. if l := next - prev; l != 0 && l < 10 { return malformed("glyph %d is %d bytes, shorter than its header", i-1, l) } prev = next } return nil } // cffIndex is a CFF INDEX that readIndex has checked. type cffIndex struct { b []byte count, offSize int offsets, data int // where the offset array and the data begin end int // the first byte after the INDEX } // off is the INDEX's offset i (1-based, as stored). func (x cffIndex) off(i int) int { v := 0 for _, c := range x.b[x.offsets+i*x.offSize : x.offsets+(i+1)*x.offSize] { v = v<<8 | int(c) } return v } // item is the INDEX's item i. func (x cffIndex) item(i int) []byte { return x.b[x.data+x.off(i)-1 : x.data+x.off(i+1)-1] } // readIndex reads the INDEX at b[at:]. countSize is 2 in CFF and 4 in // CFF2. Offsets must start at 1 and never go back, and the data must lie // inside b. func readIndex(b []byte, at, countSize int) (cffIndex, error) { x := cffIndex{b: b} if at < 0 || at+countSize > len(b) { return x, malformed("CFF INDEX past the table") } if countSize == 4 { c := binary.BigEndian.Uint32(b[at:]) if c > uint32(len(b)) { return x, malformed("CFF INDEX of %d items in a %d-byte table", c, len(b)) } x.count = int(c) } else { x.count = int(binary.BigEndian.Uint16(b[at:])) } at += countSize if x.count == 0 { x.end = at return x, nil } if at >= len(b) { return x, malformed("CFF INDEX cut short") } x.offSize, x.offsets = int(b[at]), at+1 if x.offSize < 1 || x.offSize > 4 { return x, malformed("CFF INDEX offSize %d", x.offSize) } if (x.count+1)*x.offSize > len(b)-x.offsets { return x, malformed("CFF INDEX offsets past the table") } if x.off(0) != 1 { return x, malformed("CFF INDEX's first offset is not 1") } prev := 1 for i := 1; i <= x.count; i++ { o := x.off(i) if o < prev { return x, malformed("CFF INDEX offsets go back") } prev = o } x.data = x.offsets + (x.count+1)*x.offSize if prev-1 > len(b)-x.data { return x, malformed("CFF INDEX data past the table") } x.end = x.data + prev - 1 return x, nil } // charStringsAt reads a Top DICT and returns its CharStrings offset (the // operand before operator 17), or -1 when it names none. func charStringsAt(dict []byte) (int, error) { var last int64 have, at := false, -1 for i := 0; i < len(dict); { b := dict[i] switch { case b == 28: if i+3 > len(dict) { return 0, malformed("CFF DICT cut short") } last, have = int64(int16(binary.BigEndian.Uint16(dict[i+1:]))), true i += 3 case b == 29: if i+5 > len(dict) { return 0, malformed("CFF DICT cut short") } last, have = int64(int32(binary.BigEndian.Uint32(dict[i+1:]))), true i += 5 case b == 30: // a real: nibbles to an 0xf i++ for { if i >= len(dict) { return 0, malformed("CFF DICT real cut short") } c := dict[i] i++ if c&0x0f == 0x0f || c>>4 == 0x0f { break } } have = false case b >= 32 && b <= 246: last, have = int64(b)-139, true i++ case b >= 247 && b <= 254: if i+2 > len(dict) { return 0, malformed("CFF DICT cut short") } w := int64(b) if b <= 250 { last = (w-247)*256 + int64(dict[i+1]) + 108 } else { last = -(w-251)*256 - int64(dict[i+1]) - 108 } have = true i += 2 case b == 12: // two-byte operator i += 2 have = false case b <= 27: if b == 17 { if !have || last < 0 { return 0, malformed("CFF CharStrings with no offset") } at = int(last) } i++ have = false default: return 0, malformed("CFF DICT byte %d is reserved", b) } } return at, nil } // checkCharStrings checks the CharStrings INDEX at b[at:] holds n glyphs. func checkCharStrings(b []byte, at, countSize, n int) error { if at < 0 { return malformed("CFF names no CharStrings") } x, err := readIndex(b, at, countSize) if err != nil { return err } if x.count != n { return malformed("%d CharStrings for %d glyphs", x.count, n) } return nil } // checkCFF checks a CFF table: header, Name, Top DICT, String and Global // Subr INDEXes, and the CharStrings INDEX the one Top DICT names. func checkCFF(b []byte, n int) error { if len(b) < 4 || b[0] != 1 { return malformed("CFF header is not one") } hdrSize := int(b[2]) if hdrSize < 4 || hdrSize > len(b) || b[3] < 1 || b[3] > 4 { return malformed("CFF header is not one") } names, err := readIndex(b, hdrSize, 2) if err != nil { return err } if names.count != 1 { return malformed("CFF with %d fonts", names.count) } top, err := readIndex(b, names.end, 2) if err != nil { return err } if top.count != 1 { return malformed("CFF with %d Top DICTs", top.count) } strs, err := readIndex(b, top.end, 2) if err != nil { return err } if _, err := readIndex(b, strs.end, 2); err != nil { // Global Subrs return err } cs, err := charStringsAt(top.item(0)) if err != nil { return err } return checkCharStrings(b, cs, 2, n) } // checkCFF2 checks a CFF2 table: header, Top DICT, Global Subr INDEX, // and the CharStrings INDEX the Top DICT names. func checkCFF2(b []byte, n int) error { if len(b) < 5 || b[0] != 2 { return malformed("CFF2 header is not one") } hdrSize := int(b[2]) topLen := int(binary.BigEndian.Uint16(b[3:])) if hdrSize < 5 || hdrSize+topLen > len(b) { return malformed("CFF2 header is not one") } if _, err := readIndex(b, hdrSize+topLen, 4); err != nil { // Global Subrs return err } cs, err := charStringsAt(b[hdrSize : hdrSize+topLen]) if err != nil { return err } return checkCharStrings(b, cs, 4, n) }