fontname.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package transcoder
  2. /*
  3. Fontname.go
  4. Reads the family name out of an sfnt font (TTF/OTF) by parsing its "name"
  5. table directly.
  6. This matters because ASS styles reference fonts by their *internal* family
  7. name, not by filename — and tools like assfonts deliberately rewrite that
  8. internal name to a random string when muxing subsets into a container. A
  9. release whose attachment is called "FOT-Pearl Std L[0]_GGORJFBL_0.ttf" may
  10. well identify itself as "ECZNFERB", which is what the styles will ask for.
  11. Without reading the table there is no way to connect the two.
  12. Implemented by hand rather than pulling in a font library: the name table is
  13. a simple structure and this avoids a new dependency for ~100 lines.
  14. */
  15. import (
  16. "encoding/binary"
  17. "errors"
  18. "strings"
  19. "unicode/utf16"
  20. )
  21. const (
  22. // sfnt offsets
  23. sfntNumTablesOffset = 4
  24. sfntTableDirOffset = 12
  25. sfntTableRecordSize = 16
  26. nameTableHeaderSize = 6
  27. nameRecordSize = 12
  28. nameIDFontFamily = 1
  29. platformIDMacintosh = 1
  30. platformIDMicrosoft = 3
  31. maxReasonableNameLen = 512
  32. )
  33. // FontFamilyName extracts the family name from a TTF/OTF font.
  34. //
  35. // Microsoft platform records (UTF-16BE) are preferred because they are the ones
  36. // Windows-oriented tooling writes; a Macintosh record is accepted as fallback.
  37. func FontFamilyName(font []byte) (string, error) {
  38. nameTable, err := findSfntTable(font, "name")
  39. if err != nil {
  40. return "", err
  41. }
  42. if len(nameTable) < nameTableHeaderSize {
  43. return "", errors.New("name table too short")
  44. }
  45. count := int(binary.BigEndian.Uint16(nameTable[2:4]))
  46. stringOffset := int(binary.BigEndian.Uint16(nameTable[4:6]))
  47. var fallback string
  48. for i := 0; i < count; i++ {
  49. rec := nameTableHeaderSize + i*nameRecordSize
  50. if rec+nameRecordSize > len(nameTable) {
  51. break
  52. }
  53. platformID := binary.BigEndian.Uint16(nameTable[rec : rec+2])
  54. nameID := binary.BigEndian.Uint16(nameTable[rec+6 : rec+8])
  55. length := int(binary.BigEndian.Uint16(nameTable[rec+8 : rec+10]))
  56. offset := int(binary.BigEndian.Uint16(nameTable[rec+10 : rec+12]))
  57. if nameID != nameIDFontFamily || length == 0 || length > maxReasonableNameLen {
  58. continue
  59. }
  60. start := stringOffset + offset
  61. if start < 0 || start+length > len(nameTable) {
  62. continue
  63. }
  64. raw := nameTable[start : start+length]
  65. switch platformID {
  66. case platformIDMicrosoft:
  67. if name := strings.TrimSpace(decodeUTF16BE(raw)); name != "" {
  68. return name, nil
  69. }
  70. case platformIDMacintosh:
  71. if fallback == "" {
  72. fallback = strings.TrimSpace(string(raw))
  73. }
  74. }
  75. }
  76. if fallback != "" {
  77. return fallback, nil
  78. }
  79. return "", errors.New("no family name in font")
  80. }
  81. // findSfntTable locates a table by tag inside an sfnt container.
  82. func findSfntTable(font []byte, wantTag string) ([]byte, error) {
  83. if len(font) < sfntTableDirOffset {
  84. return nil, errors.New("not a font file")
  85. }
  86. // TrueType collections start with a different header; the attachments we
  87. // care about are single fonts, so treat a collection as unsupported rather
  88. // than misreading its offsets.
  89. if string(font[0:4]) == "ttcf" {
  90. return nil, errors.New("font collections are not supported")
  91. }
  92. numTables := int(binary.BigEndian.Uint16(font[sfntNumTablesOffset : sfntNumTablesOffset+2]))
  93. for i := 0; i < numTables; i++ {
  94. rec := sfntTableDirOffset + i*sfntTableRecordSize
  95. if rec+sfntTableRecordSize > len(font) {
  96. break
  97. }
  98. tag := string(font[rec : rec+4])
  99. if tag != wantTag {
  100. continue
  101. }
  102. offset := int(binary.BigEndian.Uint32(font[rec+8 : rec+12]))
  103. length := int(binary.BigEndian.Uint32(font[rec+12 : rec+16]))
  104. if offset < 0 || length < 0 || offset+length > len(font) {
  105. return nil, errors.New("truncated font table")
  106. }
  107. return font[offset : offset+length], nil
  108. }
  109. return nil, errors.New("table not found: " + wantTag)
  110. }
  111. // decodeUTF16BE converts a big-endian UTF-16 name record to a Go string.
  112. func decodeUTF16BE(raw []byte) string {
  113. if len(raw)%2 != 0 {
  114. raw = raw[:len(raw)-1]
  115. }
  116. units := make([]uint16, 0, len(raw)/2)
  117. for i := 0; i+1 < len(raw); i += 2 {
  118. units = append(units, binary.BigEndian.Uint16(raw[i:i+2]))
  119. }
  120. return string(utf16.Decode(units))
  121. }