d2lang/d2 · error

invalid kern table length

Error message

invalid kern table length

What it means

Consistency check on the kern subtable: the declared length must be at least 14 bytes, exactly fit nPairs × 6-byte pair records, and fit within the extracted table bytes. Any mismatch means the table is corrupt or mis-declared, so it is rejected before out-of-bounds reads.

Source

Thrown at lib/textmeasure/fontface.go:175

	}
	if len(table) < 18 {
		return nil, fmt.Errorf("invalid kern table: too short")
	}
	if binary.BigEndian.Uint16(table[0:2]) != 0 {
		return nil, fmt.Errorf("unsupported kern table version")
	}
	if binary.BigEndian.Uint16(table[2:4]) == 0 {
		return nil, fmt.Errorf("invalid kern table: no subtables")
	}

	length := int(binary.BigEndian.Uint16(table[6:8]))
	coverage := binary.BigEndian.Uint16(table[8:10])
	if coverage != 0x0001 {
		return nil, fmt.Errorf("unsupported kern table coverage 0x%04x", coverage)
	}
	n := int(binary.BigEndian.Uint16(table[10:12]))
	if length < 14 || 6*n != length-14 || 4+length > len(table) {
		return nil, fmt.Errorf("invalid kern table length")
	}

	pairs := make([]kernPair, n)
	for i := range pairs {
		offset := 18 + 6*i
		pairs[i] = kernPair{
			key:   binary.BigEndian.Uint32(table[offset : offset+4]),
			value: int16(binary.BigEndian.Uint16(table[offset+4 : offset+6])),
		}
	}
	return pairs, nil
}

func sfntTable(src []byte, tag string) ([]byte, error) {
	if len(src) < 12 {
		return nil, fmt.Errorf("invalid sfnt: too short")
	}

View on GitHub (pinned to 0d69dca6f5)

Solutions

  1. Replace the font file with a verified-good copy and compare checksums.
  2. Run fonttools to inspect/rebuild the kern table (f['kern']) and re-save the font.
  3. Strip the kern table so the library skips kerning rather than failing.
Defensive patterns

Strategy: validation

Validate before calling

// length := int(binary.BigEndian.Uint16(kern[6:8]))
// n := int(binary.BigEndian.Uint16(kern[10:12]))
// consistent := length >= 14 && 6*n == length-14 && 4+length <= len(kern)

Try / catch

pairs, err := parseLegacyKern(fontData)
if err != nil {
    return fmt.Errorf("font kern table corrupt, re-provision font: %w", err)
}

Prevention

When it happens

Trigger: parseFont reads a font where the kern subtable's length field, nPairs field, and actual table byte length disagree — a hallmark of truncation or corruption.

Common situations: Corrupted downloads; fonts extracted from PDFs with wrong table lengths; maliciously or accidentally malformed font files.

Related errors


AI-assisted analysis of d2lang/d2@0d69dca6f5 (2026-08-31). Data as JSON: /api/errors/94a9d37c6649039f. Report an issue: GitHub.