| 1 | package typeface |
| 2 | |
| 3 | import "encoding/binary" |
| 4 | |
| 5 | // Outline checks: what Inspect can check about a face's glyphs cheaply, |
| 6 | // before the file is kept. They read structure only, never a glyph's |
| 7 | // instructions or charstrings: a TrueType face's loca must climb inside |
| 8 | // glyf, each glyph must hold at least its header, and maxp's glyph count |
| 9 | // must agree with loca and hmtx; a CFF or CFF2 table's header and |
| 10 | // top-level INDEXes must parse inside the table, with as many |
| 11 | // CharStrings as maxp has glyphs. Anything deeper is left to the |
| 12 | // browser's own font sanitiser and to the app's parser when it draws. |
| 13 | // |
| 14 | // A WOFF2's transformed glyf, loca and hmtx are not rebuilt, so they are |
| 15 | // not checked here. |
| 16 | |
| 17 | // checkOutlines runs the checks for the face's outline kind. |
| 18 | func checkOutlines(ft fontTables, outline Outline) error { |
| 19 | maxp := ft.data["maxp"] |
| 20 | if len(maxp) < 6 { |
| 21 | return malformed("no maxp table") |
| 22 | } |
| 23 | n := int(binary.BigEndian.Uint16(maxp[4:])) |
| 24 | if n == 0 { |
| 25 | return malformed("maxp counts no glyphs") |
| 26 | } |
| 27 | if outline == CFF { |
| 28 | if b, ok := ft.data["CFF2"]; ok { |
| 29 | return checkCFF2(b, n) |
| 30 | } |
| 31 | return checkCFF(ft.data["CFF "], n) |
| 32 | } |
| 33 | if err := checkMetrics(ft, n); err != nil { |
| 34 | return err |
| 35 | } |
| 36 | return checkGlyf(ft, n) |
| 37 | } |
| 38 | |
| 39 | // checkMetrics checks hhea's numberOfHMetrics against maxp and hmtx. |
| 40 | func checkMetrics(ft fontTables, n int) error { |
| 41 | hhea := ft.data["hhea"] |
| 42 | if len(hhea) < 36 { |
| 43 | return malformed("no hhea table") |
| 44 | } |
| 45 | m := int(binary.BigEndian.Uint16(hhea[34:])) |
| 46 | if m == 0 || m > n { |
| 47 | return malformed("%d horizontal metrics for %d glyphs", m, n) |
| 48 | } |
| 49 | if ft.transformed["hmtx"] { |
| 50 | return nil |
| 51 | } |
| 52 | if need := 4*m + 2*(n-m); len(ft.data["hmtx"]) < need { |
| 53 | return malformed("hmtx holds %d bytes, its metrics need %d", len(ft.data["hmtx"]), need) |
| 54 | } |
| 55 | return nil |
| 56 | } |
| 57 | |
| 58 | // checkGlyf checks loca against glyf and maxp. |
| 59 | func checkGlyf(ft fontTables, n int) error { |
| 60 | if ft.transformed["glyf"] { |
| 61 | return nil |
| 62 | } |
| 63 | head := ft.data["head"] // describe has checked its length |
| 64 | size := 0 |
| 65 | switch int16(binary.BigEndian.Uint16(head[50:])) { |
| 66 | case 0: |
| 67 | size = 2 |
| 68 | case 1: |
| 69 | size = 4 |
| 70 | default: |
| 71 | return malformed("indexToLocFormat is neither short nor long") |
| 72 | } |
| 73 | loca, glyf := ft.data["loca"], ft.data["glyf"] |
| 74 | if len(loca) < size*(n+1) { |
| 75 | return malformed("loca holds %d bytes, %d glyphs need %d", len(loca), n, size*(n+1)) |
| 76 | } |
| 77 | at := func(i int) uint64 { |
| 78 | if size == 2 { |
| 79 | return 2 * uint64(binary.BigEndian.Uint16(loca[2*i:])) |
| 80 | } |
| 81 | return uint64(binary.BigEndian.Uint32(loca[4*i:])) |
| 82 | } |
| 83 | prev := at(0) |
| 84 | for i := 1; i <= n; i++ { |
| 85 | next := at(i) |
| 86 | if next < prev { |
| 87 | return malformed("loca goes backwards at glyph %d", i-1) |
| 88 | } |
| 89 | if next > uint64(len(glyf)) { |
| 90 | return malformed("glyph %d runs past glyf", i-1) |
| 91 | } |
| 92 | // A glyph with any bytes has at least its 10-byte header. |
| 93 | if l := next - prev; l != 0 && l < 10 { |
| 94 | return malformed("glyph %d is %d bytes, shorter than its header", i-1, l) |
| 95 | } |
| 96 | prev = next |
| 97 | } |
| 98 | return nil |
| 99 | } |
| 100 | |
| 101 | // cffIndex is a CFF INDEX that readIndex has checked. |
| 102 | type cffIndex struct { |
| 103 | b []byte |
| 104 | count, offSize int |
| 105 | offsets, data int // where the offset array and the data begin |
| 106 | end int // the first byte after the INDEX |
| 107 | } |
| 108 | |
| 109 | // off is the INDEX's offset i (1-based, as stored). |
| 110 | func (x cffIndex) off(i int) int { |
| 111 | v := 0 |
| 112 | for _, c := range x.b[x.offsets+i*x.offSize : x.offsets+(i+1)*x.offSize] { |
| 113 | v = v<<8 | int(c) |
| 114 | } |
| 115 | return v |
| 116 | } |
| 117 | |
| 118 | // item is the INDEX's item i. |
| 119 | func (x cffIndex) item(i int) []byte { |
| 120 | return x.b[x.data+x.off(i)-1 : x.data+x.off(i+1)-1] |
| 121 | } |
| 122 | |
| 123 | // readIndex reads the INDEX at b[at:]. countSize is 2 in CFF and 4 in |
| 124 | // CFF2. Offsets must start at 1 and never go back, and the data must lie |
| 125 | // inside b. |
| 126 | func readIndex(b []byte, at, countSize int) (cffIndex, error) { |
| 127 | x := cffIndex{b: b} |
| 128 | if at < 0 || at+countSize > len(b) { |
| 129 | return x, malformed("CFF INDEX past the table") |
| 130 | } |
| 131 | if countSize == 4 { |
| 132 | c := binary.BigEndian.Uint32(b[at:]) |
| 133 | if c > uint32(len(b)) { |
| 134 | return x, malformed("CFF INDEX of %d items in a %d-byte table", c, len(b)) |
| 135 | } |
| 136 | x.count = int(c) |
| 137 | } else { |
| 138 | x.count = int(binary.BigEndian.Uint16(b[at:])) |
| 139 | } |
| 140 | at += countSize |
| 141 | if x.count == 0 { |
| 142 | x.end = at |
| 143 | return x, nil |
| 144 | } |
| 145 | if at >= len(b) { |
| 146 | return x, malformed("CFF INDEX cut short") |
| 147 | } |
| 148 | x.offSize, x.offsets = int(b[at]), at+1 |
| 149 | if x.offSize < 1 || x.offSize > 4 { |
| 150 | return x, malformed("CFF INDEX offSize %d", x.offSize) |
| 151 | } |
| 152 | if (x.count+1)*x.offSize > len(b)-x.offsets { |
| 153 | return x, malformed("CFF INDEX offsets past the table") |
| 154 | } |
| 155 | if x.off(0) != 1 { |
| 156 | return x, malformed("CFF INDEX's first offset is not 1") |
| 157 | } |
| 158 | prev := 1 |
| 159 | for i := 1; i <= x.count; i++ { |
| 160 | o := x.off(i) |
| 161 | if o < prev { |
| 162 | return x, malformed("CFF INDEX offsets go back") |
| 163 | } |
| 164 | prev = o |
| 165 | } |
| 166 | x.data = x.offsets + (x.count+1)*x.offSize |
| 167 | if prev-1 > len(b)-x.data { |
| 168 | return x, malformed("CFF INDEX data past the table") |
| 169 | } |
| 170 | x.end = x.data + prev - 1 |
| 171 | return x, nil |
| 172 | } |
| 173 | |
| 174 | // charStringsAt reads a Top DICT and returns its CharStrings offset (the |
| 175 | // operand before operator 17), or -1 when it names none. |
| 176 | func charStringsAt(dict []byte) (int, error) { |
| 177 | var last int64 |
| 178 | have, at := false, -1 |
| 179 | for i := 0; i < len(dict); { |
| 180 | b := dict[i] |
| 181 | switch { |
| 182 | case b == 28: |
| 183 | if i+3 > len(dict) { |
| 184 | return 0, malformed("CFF DICT cut short") |
| 185 | } |
| 186 | last, have = int64(int16(binary.BigEndian.Uint16(dict[i+1:]))), true |
| 187 | i += 3 |
| 188 | case b == 29: |
| 189 | if i+5 > len(dict) { |
| 190 | return 0, malformed("CFF DICT cut short") |
| 191 | } |
| 192 | last, have = int64(int32(binary.BigEndian.Uint32(dict[i+1:]))), true |
| 193 | i += 5 |
| 194 | case b == 30: // a real: nibbles to an 0xf |
| 195 | i++ |
| 196 | for { |
| 197 | if i >= len(dict) { |
| 198 | return 0, malformed("CFF DICT real cut short") |
| 199 | } |
| 200 | c := dict[i] |
| 201 | i++ |
| 202 | if c&0x0f == 0x0f || c>>4 == 0x0f { |
| 203 | break |
| 204 | } |
| 205 | } |
| 206 | have = false |
| 207 | case b >= 32 && b <= 246: |
| 208 | last, have = int64(b)-139, true |
| 209 | i++ |
| 210 | case b >= 247 && b <= 254: |
| 211 | if i+2 > len(dict) { |
| 212 | return 0, malformed("CFF DICT cut short") |
| 213 | } |
| 214 | w := int64(b) |
| 215 | if b <= 250 { |
| 216 | last = (w-247)*256 + int64(dict[i+1]) + 108 |
| 217 | } else { |
| 218 | last = -(w-251)*256 - int64(dict[i+1]) - 108 |
| 219 | } |
| 220 | have = true |
| 221 | i += 2 |
| 222 | case b == 12: // two-byte operator |
| 223 | i += 2 |
| 224 | have = false |
| 225 | case b <= 27: |
| 226 | if b == 17 { |
| 227 | if !have || last < 0 { |
| 228 | return 0, malformed("CFF CharStrings with no offset") |
| 229 | } |
| 230 | at = int(last) |
| 231 | } |
| 232 | i++ |
| 233 | have = false |
| 234 | default: |
| 235 | return 0, malformed("CFF DICT byte %d is reserved", b) |
| 236 | } |
| 237 | } |
| 238 | return at, nil |
| 239 | } |
| 240 | |
| 241 | // checkCharStrings checks the CharStrings INDEX at b[at:] holds n glyphs. |
| 242 | func checkCharStrings(b []byte, at, countSize, n int) error { |
| 243 | if at < 0 { |
| 244 | return malformed("CFF names no CharStrings") |
| 245 | } |
| 246 | x, err := readIndex(b, at, countSize) |
| 247 | if err != nil { |
| 248 | return err |
| 249 | } |
| 250 | if x.count != n { |
| 251 | return malformed("%d CharStrings for %d glyphs", x.count, n) |
| 252 | } |
| 253 | return nil |
| 254 | } |
| 255 | |
| 256 | // checkCFF checks a CFF table: header, Name, Top DICT, String and Global |
| 257 | // Subr INDEXes, and the CharStrings INDEX the one Top DICT names. |
| 258 | func checkCFF(b []byte, n int) error { |
| 259 | if len(b) < 4 || b[0] != 1 { |
| 260 | return malformed("CFF header is not one") |
| 261 | } |
| 262 | hdrSize := int(b[2]) |
| 263 | if hdrSize < 4 || hdrSize > len(b) || b[3] < 1 || b[3] > 4 { |
| 264 | return malformed("CFF header is not one") |
| 265 | } |
| 266 | names, err := readIndex(b, hdrSize, 2) |
| 267 | if err != nil { |
| 268 | return err |
| 269 | } |
| 270 | if names.count != 1 { |
| 271 | return malformed("CFF with %d fonts", names.count) |
| 272 | } |
| 273 | top, err := readIndex(b, names.end, 2) |
| 274 | if err != nil { |
| 275 | return err |
| 276 | } |
| 277 | if top.count != 1 { |
| 278 | return malformed("CFF with %d Top DICTs", top.count) |
| 279 | } |
| 280 | strs, err := readIndex(b, top.end, 2) |
| 281 | if err != nil { |
| 282 | return err |
| 283 | } |
| 284 | if _, err := readIndex(b, strs.end, 2); err != nil { // Global Subrs |
| 285 | return err |
| 286 | } |
| 287 | cs, err := charStringsAt(top.item(0)) |
| 288 | if err != nil { |
| 289 | return err |
| 290 | } |
| 291 | return checkCharStrings(b, cs, 2, n) |
| 292 | } |
| 293 | |
| 294 | // checkCFF2 checks a CFF2 table: header, Top DICT, Global Subr INDEX, |
| 295 | // and the CharStrings INDEX the Top DICT names. |
| 296 | func checkCFF2(b []byte, n int) error { |
| 297 | if len(b) < 5 || b[0] != 2 { |
| 298 | return malformed("CFF2 header is not one") |
| 299 | } |
| 300 | hdrSize := int(b[2]) |
| 301 | topLen := int(binary.BigEndian.Uint16(b[3:])) |
| 302 | if hdrSize < 5 || hdrSize+topLen > len(b) { |
| 303 | return malformed("CFF2 header is not one") |
| 304 | } |
| 305 | if _, err := readIndex(b, hdrSize+topLen, 4); err != nil { // Global Subrs |
| 306 | return err |
| 307 | } |
| 308 | cs, err := charStringsAt(b[hdrSize : hdrSize+topLen]) |
| 309 | if err != nil { |
| 310 | return err |
| 311 | } |
| 312 | return checkCharStrings(b, cs, 4, n) |
| 313 | } |
| 314 | |