cmap.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. // Copyright 2017 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package sfnt
  5. import (
  6. "golang.org/x/text/encoding/charmap"
  7. )
  8. // Platform IDs and Platform Specific IDs as per
  9. // https://www.microsoft.com/typography/otspec/name.htm
  10. const (
  11. pidUnicode = 0
  12. pidMacintosh = 1
  13. pidWindows = 3
  14. psidUnicode2BMPOnly = 3
  15. psidUnicode2FullRepertoire = 4
  16. // Note that FontForge may generate a bogus Platform Specific ID (value 10)
  17. // for the Unicode Platform ID (value 0). See
  18. // https://github.com/fontforge/fontforge/issues/2728
  19. psidMacintoshRoman = 0
  20. psidWindowsSymbol = 0
  21. psidWindowsUCS2 = 1
  22. psidWindowsUCS4 = 10
  23. )
  24. // platformEncodingWidth returns the number of bytes per character assumed by
  25. // the given Platform ID and Platform Specific ID.
  26. //
  27. // Very old fonts, from before Unicode was widely adopted, assume only 1 byte
  28. // per character: a character map.
  29. //
  30. // Old fonts, from when Unicode meant the Basic Multilingual Plane (BMP),
  31. // assume that 2 bytes per character is sufficient.
  32. //
  33. // Recent fonts naturally support the full range of Unicode code points, which
  34. // can take up to 4 bytes per character. Such fonts might still choose one of
  35. // the legacy encodings if e.g. their repertoire is limited to the BMP, for
  36. // greater compatibility with older software, or because the resultant file
  37. // size can be smaller.
  38. func platformEncodingWidth(pid, psid uint16) int {
  39. switch pid {
  40. case pidUnicode:
  41. switch psid {
  42. case psidUnicode2BMPOnly:
  43. return 2
  44. case psidUnicode2FullRepertoire:
  45. return 4
  46. }
  47. case pidMacintosh:
  48. switch psid {
  49. case psidMacintoshRoman:
  50. return 1
  51. }
  52. case pidWindows:
  53. switch psid {
  54. case psidWindowsSymbol:
  55. return 2
  56. case psidWindowsUCS2:
  57. return 2
  58. case psidWindowsUCS4:
  59. return 4
  60. }
  61. }
  62. return 0
  63. }
  64. // The various cmap formats are described at
  65. // https://www.microsoft.com/typography/otspec/cmap.htm
  66. var supportedCmapFormat = func(format, pid, psid uint16) bool {
  67. switch format {
  68. case 0:
  69. return pid == pidMacintosh && psid == psidMacintoshRoman
  70. case 4:
  71. return true
  72. case 12:
  73. return true
  74. }
  75. return false
  76. }
  77. func (f *Font) makeCachedGlyphIndex(buf []byte, offset, length uint32, format uint16) ([]byte, glyphIndexFunc, error) {
  78. switch format {
  79. case 0:
  80. return f.makeCachedGlyphIndexFormat0(buf, offset, length)
  81. case 4:
  82. return f.makeCachedGlyphIndexFormat4(buf, offset, length)
  83. case 12:
  84. return f.makeCachedGlyphIndexFormat12(buf, offset, length)
  85. }
  86. panic("unreachable")
  87. }
  88. func (f *Font) makeCachedGlyphIndexFormat0(buf []byte, offset, length uint32) ([]byte, glyphIndexFunc, error) {
  89. if length != 6+256 || offset+length > f.cmap.length {
  90. return nil, nil, errInvalidCmapTable
  91. }
  92. var err error
  93. buf, err = f.src.view(buf, int(f.cmap.offset+offset), int(length))
  94. if err != nil {
  95. return nil, nil, err
  96. }
  97. var table [256]byte
  98. copy(table[:], buf[6:])
  99. return buf, func(f *Font, b *Buffer, r rune) (GlyphIndex, error) {
  100. x, ok := charmap.Macintosh.EncodeRune(r)
  101. if !ok {
  102. // The source rune r is not representable in the Macintosh-Roman encoding.
  103. return 0, nil
  104. }
  105. return GlyphIndex(table[x]), nil
  106. }, nil
  107. }
  108. func (f *Font) makeCachedGlyphIndexFormat4(buf []byte, offset, length uint32) ([]byte, glyphIndexFunc, error) {
  109. const headerSize = 14
  110. if offset+headerSize > f.cmap.length {
  111. return nil, nil, errInvalidCmapTable
  112. }
  113. var err error
  114. buf, err = f.src.view(buf, int(f.cmap.offset+offset), headerSize)
  115. if err != nil {
  116. return nil, nil, err
  117. }
  118. offset += headerSize
  119. segCount := u16(buf[6:])
  120. if segCount&1 != 0 {
  121. return nil, nil, errInvalidCmapTable
  122. }
  123. segCount /= 2
  124. if segCount > maxCmapSegments {
  125. return nil, nil, errUnsupportedNumberOfCmapSegments
  126. }
  127. eLength := 8*uint32(segCount) + 2
  128. if offset+eLength > f.cmap.length {
  129. return nil, nil, errInvalidCmapTable
  130. }
  131. buf, err = f.src.view(buf, int(f.cmap.offset+offset), int(eLength))
  132. if err != nil {
  133. return nil, nil, err
  134. }
  135. offset += eLength
  136. entries := make([]cmapEntry16, segCount)
  137. for i := range entries {
  138. entries[i] = cmapEntry16{
  139. end: u16(buf[0*len(entries)+0+2*i:]),
  140. start: u16(buf[2*len(entries)+2+2*i:]),
  141. delta: u16(buf[4*len(entries)+2+2*i:]),
  142. offset: u16(buf[6*len(entries)+2+2*i:]),
  143. }
  144. }
  145. indexesBase := f.cmap.offset + offset
  146. indexesLength := f.cmap.length - offset
  147. return buf, func(f *Font, b *Buffer, r rune) (GlyphIndex, error) {
  148. if uint32(r) > 0xffff {
  149. return 0, nil
  150. }
  151. c := uint16(r)
  152. for i, j := 0, len(entries); i < j; {
  153. h := i + (j-i)/2
  154. entry := &entries[h]
  155. if c < entry.start {
  156. j = h
  157. } else if entry.end < c {
  158. i = h + 1
  159. } else if entry.offset == 0 {
  160. return GlyphIndex(c + entry.delta), nil
  161. } else {
  162. offset := uint32(entry.offset) + 2*uint32(h-len(entries)+int(c-entry.start))
  163. if offset > indexesLength || offset+2 > indexesLength {
  164. return 0, errInvalidCmapTable
  165. }
  166. x, err := b.view(&f.src, int(indexesBase+offset), 2)
  167. if err != nil {
  168. return 0, err
  169. }
  170. return GlyphIndex(u16(x)), nil
  171. }
  172. }
  173. return 0, nil
  174. }, nil
  175. }
  176. func (f *Font) makeCachedGlyphIndexFormat12(buf []byte, offset, _ uint32) ([]byte, glyphIndexFunc, error) {
  177. const headerSize = 16
  178. if offset+headerSize > f.cmap.length {
  179. return nil, nil, errInvalidCmapTable
  180. }
  181. var err error
  182. buf, err = f.src.view(buf, int(f.cmap.offset+offset), headerSize)
  183. if err != nil {
  184. return nil, nil, err
  185. }
  186. length := u32(buf[4:])
  187. if f.cmap.length < offset || length > f.cmap.length-offset {
  188. return nil, nil, errInvalidCmapTable
  189. }
  190. offset += headerSize
  191. numGroups := u32(buf[12:])
  192. if numGroups > maxCmapSegments {
  193. return nil, nil, errUnsupportedNumberOfCmapSegments
  194. }
  195. eLength := 12 * numGroups
  196. if headerSize+eLength != length {
  197. return nil, nil, errInvalidCmapTable
  198. }
  199. buf, err = f.src.view(buf, int(f.cmap.offset+offset), int(eLength))
  200. if err != nil {
  201. return nil, nil, err
  202. }
  203. offset += eLength
  204. entries := make([]cmapEntry32, numGroups)
  205. for i := range entries {
  206. entries[i] = cmapEntry32{
  207. start: u32(buf[0+12*i:]),
  208. end: u32(buf[4+12*i:]),
  209. delta: u32(buf[8+12*i:]),
  210. }
  211. }
  212. return buf, func(f *Font, b *Buffer, r rune) (GlyphIndex, error) {
  213. c := uint32(r)
  214. for i, j := 0, len(entries); i < j; {
  215. h := i + (j-i)/2
  216. entry := &entries[h]
  217. if c < entry.start {
  218. j = h
  219. } else if entry.end < c {
  220. i = h + 1
  221. } else {
  222. return GlyphIndex(c - entry.start + entry.delta), nil
  223. }
  224. }
  225. return 0, nil
  226. }, nil
  227. }
  228. type cmapEntry16 struct {
  229. end, start, delta, offset uint16
  230. }
  231. type cmapEntry32 struct {
  232. start, end, delta uint32
  233. }