package typeface import ( "bytes" "encoding/binary" "errors" "testing" ) // Outlines are checked before a file is kept, as far as it is cheap to: // for TrueType, that loca's offsets climb and stay inside glyf, that each // glyph holds at least its header, and that maxp's glyph count agrees // with loca and hmtx; for CFF and CFF2, that the header and the top-level // INDEXes parse inside the table and the CharStrings count is maxp's. // Outline-table presence alone was taken as validation before (final // review, I1). A WOFF2's transformed glyf and loca are not rebuilt, so // they are not checked here; the browser's own sanitiser still is. // buildIndex is a CFF INDEX of items: a count of countSize bytes (2 in // CFF, 4 in CFF2), then 4-byte offsets and the data. func buildIndex(countSize int, items ...[]byte) []byte { var b []byte if countSize == 4 { b = binary.BigEndian.AppendUint32(b, uint32(len(items))) } else { b = binary.BigEndian.AppendUint16(b, uint16(len(items))) } if len(items) == 0 { return b } b = append(b, 4) off := uint32(1) b = binary.BigEndian.AppendUint32(b, off) for _, it := range items { off += uint32(len(it)) b = binary.BigEndian.AppendUint32(b, off) } for _, it := range items { b = append(b, it...) } return b } // charStrings is n glyphs, each a lone endchar. func charStrings(n int) [][]byte { out := make([][]byte, n) for i := range out { out[i] = []byte{14} } return out } // dictInt is a DICT operand in its five-byte form, so a DICT's length // does not depend on the numbers in it. func dictInt(v int32) []byte { return binary.BigEndian.AppendUint32([]byte{29}, uint32(v)) } // cffTable is a CFF table with one font of n glyphs: header, Name INDEX, // Top DICT INDEX (CharStrings only), empty String and Global Subr // INDEXes, then the CharStrings INDEX. func cffTable(n int) []byte { header := []byte{1, 0, 4, 4} name := buildIndex(2, []byte("Test")) empty := buildIndex(2) // The Top DICT INDEX's size is fixed (one item of 6 bytes), so the // CharStrings offset can be worked out before it is written. topLen := len(buildIndex(2, make([]byte, 6))) at := len(header) + len(name) + topLen + 2*len(empty) top := buildIndex(2, append(dictInt(int32(at)), 17)) b := append(append(append(header, name...), top...), empty...) b = append(b, empty...) return append(b, buildIndex(2, charStrings(n)...)...) } // cff2Table is a CFF2 table of n glyphs: header, Top DICT (CharStrings // only), an empty Global Subr INDEX, then the CharStrings INDEX. func cff2Table(n int) []byte { const headerSize, topLen = 5, 6 at := headerSize + topLen + 4 top := append(dictInt(int32(at)), 17) b := []byte{2, 0, headerSize} b = binary.BigEndian.AppendUint16(b, topLen) b = append(b, top...) b = append(b, buildIndex(4)...) return append(b, buildIndex(4, charStrings(n)...)...) } // numGlyphs is maxp's glyph count in tables ts. func numGlyphs(ts []table) int { return int(binary.BigEndian.Uint16(tableData(ts, "maxp")[4:])) } // otto is Liberation's other tables in an OTTO, with outlines cffTag // built by make for Liberation's glyph count, where glyf and loca were. func otto(t *testing.T, cffTag string, make func(n int) []byte) []byte { t.Helper() _, ts := tablesOf(t, liberation(t)) ts = withoutTable(ts, "glyf", "loca") return buildSFNT(sigOTTO, append(ts, table{cffTag, make(numGlyphs(ts))})) } // liberationWith is Liberation with table tag changed by edit. func liberationWith(t *testing.T, tag string, edit func(b []byte) []byte) []byte { t.Helper() v, ts := tablesOf(t, liberation(t)) return buildSFNT(v, withTable(ts, tag, edit(bytes.Clone(tableData(ts, tag))))) } // locaAt and setLoca read and write Liberation's long-format loca. func locaAt(b []byte, i int) uint32 { return binary.BigEndian.Uint32(b[4*i:]) } func setLoca(b []byte, i int, v uint32) { binary.BigEndian.PutUint32(b[4*i:], v) } // someGlyph is the first glyph after .notdef longer than n bytes. func someGlyph(t *testing.T, loca []byte, n uint32) int { t.Helper() for i := 1; 4*(i+2) <= len(loca); i++ { if locaAt(loca, i+1)-locaAt(loca, i) > n { return i } } t.Fatal("no glyph long enough") return 0 } func TestInspectReadsGenuineCFFAndCFF2(t *testing.T) { for _, c := range []struct { name string data []byte }{ {"cff", otto(t, "CFF ", cffTable)}, {"cff2", otto(t, "CFF2", cff2Table)}, {"woff of a cff", buildWOFF(t, otto(t, "CFF ", cffTable))}, {"woff2 of a cff2", buildWOFF2(t, otto(t, "CFF2", cff2Table))}, } { t.Run(c.name, func(t *testing.T) { if f := mustInspect(t, c.data); f.Outline != CFF { t.Errorf("Outline = %q", f.Outline) } }) } } // A WOFF2's transformed glyf and loca are not rebuilt, so their bytes are // not checked: the file is taken on its other tables. func TestInspectTakesATransformedWOFF2Glyf(t *testing.T) { flavor, es, _ := woff2Parts(t, liberation(t)) var stream []byte _, ts := tablesOf(t, liberation(t)) for i := range es { switch es[i].tag { case "glyf": es[i].version, es[i].transform = 0, 64 stream = append(stream, make([]byte, 64)...) case "loca": es[i].version, es[i].transform = 0, 0 default: stream = append(stream, tableData(ts, es[i].tag)...) } } mustInspect(t, assembleWOFF2(flavor, es, brotliOf(stream))) } func TestInspectRefusesBrokenOutlines(t *testing.T) { _, ts := tablesOf(t, liberation(t)) loca := tableData(ts, "loca") glyfLen := uint32(len(tableData(ts, "glyf"))) g := someGlyph(t, loca, 16) backwards := liberationWith(t, "loca", func(b []byte) []byte { setLoca(b, g+1, locaAt(b, g)-2) return b }) pastGlyf := liberationWith(t, "loca", func(b []byte) []byte { setLoca(b, len(b)/4-1, glyfLen+4) return b }) shortGlyph := liberationWith(t, "loca", func(b []byte) []byte { setLoca(b, g+1, locaAt(b, g)+4) return b }) moreGlyphs := liberationWith(t, "maxp", func(b []byte) []byte { binary.BigEndian.PutUint16(b[4:], uint16(numGlyphs(ts)+10)) return b }) badLocFormat := liberationWith(t, "head", func(b []byte) []byte { binary.BigEndian.PutUint16(b[50:], 2) return b }) shortHmtx := liberationWith(t, "hmtx", func(b []byte) []byte { return b[:100] }) noMetrics := liberationWith(t, "hhea", func(b []byte) []byte { binary.BigEndian.PutUint16(b[34:], 0) return b }) tooManyMetrics := liberationWith(t, "hhea", func(b []byte) []byte { binary.BigEndian.PutUint16(b[34:], uint16(numGlyphs(ts)+1)) return b }) v, _ := tablesOf(t, liberation(t)) empty := buildSFNT(v, withTable(withTable(ts, "glyf", nil), "loca", nil)) noMaxp := buildSFNT(v, withoutTable(ts, "maxp")) cffWith := func(edit func(b []byte) []byte) []byte { return otto(t, "CFF ", func(n int) []byte { return edit(cffTable(n)) }) } stub := cffWith(func([]byte) []byte { return []byte{1, 0, 4, 1} }) fewerCharStrings := otto(t, "CFF ", func(n int) []byte { return cffTable(n - 1) }) cffCutShort := cffWith(func(b []byte) []byte { return b[:len(b)-1] }) cffBadVersion := cffWith(func(b []byte) []byte { b[0] = 3; return b }) cffTwoNames := cffWith(func(b []byte) []byte { binary.BigEndian.PutUint16(b[4:], 2) // the Name INDEX's count return b }) cffOffsetsBack := cffWith(func(b []byte) []byte { // The Name INDEX's second offset, before its first. binary.BigEndian.PutUint32(b[4+2+1+4:], 0) return b }) cffNoCharStrings := cffWith(func(b []byte) []byte { // The Top DICT's operator, 17, made a different one (15, // charset), so the DICT names no CharStrings. i := bytes.Index(b, []byte{17, 0, 0}) if i < 0 { t.Fatal("no CharStrings operator") } b[i] = 15 return b }) cff2CutShort := otto(t, "CFF2", func(n int) []byte { return cff2Table(n)[:12] }) cff2Fewer := otto(t, "CFF2", func(n int) []byte { return cff2Table(n - 1) }) for _, c := range []struct { name string data []byte }{ {"loca going backwards", backwards}, {"loca past the end of glyf", pastGlyf}, {"a glyph shorter than its header", shortGlyph}, {"maxp counting more glyphs than loca holds", moreGlyphs}, {"an indexToLocFormat that is neither", badLocFormat}, {"hmtx shorter than its metrics", shortHmtx}, {"no horizontal metrics", noMetrics}, {"more metrics than glyphs", tooManyMetrics}, {"an empty glyf and loca", empty}, {"no maxp", noMaxp}, {"a WOFF of a backwards loca", buildWOFF(t, backwards)}, {"a WOFF2 of a loca past glyf", buildWOFF2(t, pastGlyf)}, {"a stub CFF", stub}, {"a CFF with fewer CharStrings than glyphs", fewerCharStrings}, {"a CFF cut short", cffCutShort}, {"a CFF of an unknown version", cffBadVersion}, {"a CFF with two fonts", cffTwoNames}, {"a CFF INDEX whose offsets go back", cffOffsetsBack}, {"a CFF with no CharStrings", cffNoCharStrings}, {"a CFF2 cut short", cff2CutShort}, {"a CFF2 with fewer CharStrings than glyphs", cff2Fewer}, {"a WOFF2 of a stub CFF", buildWOFF2(t, stub)}, } { t.Run(c.name, func(t *testing.T) { f, err := Inspect(c.data) if !errors.Is(err, ErrMalformed) { t.Fatalf("Inspect = %+v, %v; want ErrMalformed", f, err) } t.Log(err) }) } }