fonts_utils.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /* Copyright 2012 Mozilla Foundation
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. import { FontType, info } from "../shared/util.js";
  16. import { getEncoding, StandardEncoding } from "./encodings.js";
  17. import { getGlyphsUnicode } from "./glyphlist.js";
  18. import { getUnicodeForGlyph } from "./unicode.js";
  19. // Accented characters have issues on Windows and Linux. When this flag is
  20. // enabled glyphs that use seac and seac style endchar operators are truncated
  21. // and we instead just store the glyph id's of the base glyph and its accent to
  22. // be drawn individually.
  23. // Linux (freetype) requires that when a seac style endchar is used
  24. // that the charset must be a predefined one, however we build a
  25. // custom one. Windows just refuses to draw glyphs with seac operators.
  26. const SEAC_ANALYSIS_ENABLED = true;
  27. const FontFlags = {
  28. FixedPitch: 1,
  29. Serif: 2,
  30. Symbolic: 4,
  31. Script: 8,
  32. Nonsymbolic: 32,
  33. Italic: 64,
  34. AllCap: 65536,
  35. SmallCap: 131072,
  36. ForceBold: 262144,
  37. };
  38. // prettier-ignore
  39. const MacStandardGlyphOrdering = [
  40. ".notdef", ".null", "nonmarkingreturn", "space", "exclam", "quotedbl",
  41. "numbersign", "dollar", "percent", "ampersand", "quotesingle", "parenleft",
  42. "parenright", "asterisk", "plus", "comma", "hyphen", "period", "slash",
  43. "zero", "one", "two", "three", "four", "five", "six", "seven", "eight",
  44. "nine", "colon", "semicolon", "less", "equal", "greater", "question", "at",
  45. "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O",
  46. "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "bracketleft",
  47. "backslash", "bracketright", "asciicircum", "underscore", "grave", "a", "b",
  48. "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q",
  49. "r", "s", "t", "u", "v", "w", "x", "y", "z", "braceleft", "bar", "braceright",
  50. "asciitilde", "Adieresis", "Aring", "Ccedilla", "Eacute", "Ntilde",
  51. "Odieresis", "Udieresis", "aacute", "agrave", "acircumflex", "adieresis",
  52. "atilde", "aring", "ccedilla", "eacute", "egrave", "ecircumflex", "edieresis",
  53. "iacute", "igrave", "icircumflex", "idieresis", "ntilde", "oacute", "ograve",
  54. "ocircumflex", "odieresis", "otilde", "uacute", "ugrave", "ucircumflex",
  55. "udieresis", "dagger", "degree", "cent", "sterling", "section", "bullet",
  56. "paragraph", "germandbls", "registered", "copyright", "trademark", "acute",
  57. "dieresis", "notequal", "AE", "Oslash", "infinity", "plusminus", "lessequal",
  58. "greaterequal", "yen", "mu", "partialdiff", "summation", "product", "pi",
  59. "integral", "ordfeminine", "ordmasculine", "Omega", "ae", "oslash",
  60. "questiondown", "exclamdown", "logicalnot", "radical", "florin",
  61. "approxequal", "Delta", "guillemotleft", "guillemotright", "ellipsis",
  62. "nonbreakingspace", "Agrave", "Atilde", "Otilde", "OE", "oe", "endash",
  63. "emdash", "quotedblleft", "quotedblright", "quoteleft", "quoteright",
  64. "divide", "lozenge", "ydieresis", "Ydieresis", "fraction", "currency",
  65. "guilsinglleft", "guilsinglright", "fi", "fl", "daggerdbl", "periodcentered",
  66. "quotesinglbase", "quotedblbase", "perthousand", "Acircumflex",
  67. "Ecircumflex", "Aacute", "Edieresis", "Egrave", "Iacute", "Icircumflex",
  68. "Idieresis", "Igrave", "Oacute", "Ocircumflex", "apple", "Ograve", "Uacute",
  69. "Ucircumflex", "Ugrave", "dotlessi", "circumflex", "tilde", "macron",
  70. "breve", "dotaccent", "ring", "cedilla", "hungarumlaut", "ogonek", "caron",
  71. "Lslash", "lslash", "Scaron", "scaron", "Zcaron", "zcaron", "brokenbar",
  72. "Eth", "eth", "Yacute", "yacute", "Thorn", "thorn", "minus", "multiply",
  73. "onesuperior", "twosuperior", "threesuperior", "onehalf", "onequarter",
  74. "threequarters", "franc", "Gbreve", "gbreve", "Idotaccent", "Scedilla",
  75. "scedilla", "Cacute", "cacute", "Ccaron", "ccaron", "dcroat"];
  76. function getFontType(type, subtype, isStandardFont = false) {
  77. switch (type) {
  78. case "Type1":
  79. if (isStandardFont) {
  80. return FontType.TYPE1STANDARD;
  81. }
  82. return subtype === "Type1C" ? FontType.TYPE1C : FontType.TYPE1;
  83. case "CIDFontType0":
  84. return subtype === "CIDFontType0C"
  85. ? FontType.CIDFONTTYPE0C
  86. : FontType.CIDFONTTYPE0;
  87. case "OpenType":
  88. return FontType.OPENTYPE;
  89. case "TrueType":
  90. return FontType.TRUETYPE;
  91. case "CIDFontType2":
  92. return FontType.CIDFONTTYPE2;
  93. case "MMType1":
  94. return FontType.MMTYPE1;
  95. case "Type0":
  96. return FontType.TYPE0;
  97. default:
  98. return FontType.UNKNOWN;
  99. }
  100. }
  101. // Some bad PDF generators, e.g. Scribus PDF, include glyph names
  102. // in a 'uniXXXX' format -- attempting to recover proper ones.
  103. function recoverGlyphName(name, glyphsUnicodeMap) {
  104. if (glyphsUnicodeMap[name] !== undefined) {
  105. return name;
  106. }
  107. // The glyph name is non-standard, trying to recover.
  108. const unicode = getUnicodeForGlyph(name, glyphsUnicodeMap);
  109. if (unicode !== -1) {
  110. for (const key in glyphsUnicodeMap) {
  111. if (glyphsUnicodeMap[key] === unicode) {
  112. return key;
  113. }
  114. }
  115. }
  116. info("Unable to recover a standard glyph name for: " + name);
  117. return name;
  118. }
  119. /**
  120. * Shared logic for building a char code to glyph id mapping for Type1 and
  121. * simple CFF fonts. See section 9.6.6.2 of the spec.
  122. * @param {Object} properties Font properties object.
  123. * @param {Object} builtInEncoding The encoding contained within the actual font
  124. * data.
  125. * @param {Array} glyphNames Array of glyph names where the index is the
  126. * glyph ID.
  127. * @returns {Object} A char code to glyph ID map.
  128. */
  129. function type1FontGlyphMapping(properties, builtInEncoding, glyphNames) {
  130. const charCodeToGlyphId = Object.create(null);
  131. let glyphId, charCode, baseEncoding;
  132. const isSymbolicFont = !!(properties.flags & FontFlags.Symbolic);
  133. if (properties.isInternalFont) {
  134. baseEncoding = builtInEncoding;
  135. for (charCode = 0; charCode < baseEncoding.length; charCode++) {
  136. glyphId = glyphNames.indexOf(baseEncoding[charCode]);
  137. if (glyphId >= 0) {
  138. charCodeToGlyphId[charCode] = glyphId;
  139. } else {
  140. charCodeToGlyphId[charCode] = 0; // notdef
  141. }
  142. }
  143. } else if (properties.baseEncodingName) {
  144. // If a valid base encoding name was used, the mapping is initialized with
  145. // that.
  146. baseEncoding = getEncoding(properties.baseEncodingName);
  147. for (charCode = 0; charCode < baseEncoding.length; charCode++) {
  148. glyphId = glyphNames.indexOf(baseEncoding[charCode]);
  149. if (glyphId >= 0) {
  150. charCodeToGlyphId[charCode] = glyphId;
  151. } else {
  152. charCodeToGlyphId[charCode] = 0; // notdef
  153. }
  154. }
  155. } else if (isSymbolicFont) {
  156. // For a symbolic font the encoding should be the fonts built-in encoding.
  157. for (charCode in builtInEncoding) {
  158. charCodeToGlyphId[charCode] = builtInEncoding[charCode];
  159. }
  160. } else {
  161. // For non-symbolic fonts that don't have a base encoding the standard
  162. // encoding should be used.
  163. baseEncoding = StandardEncoding;
  164. for (charCode = 0; charCode < baseEncoding.length; charCode++) {
  165. glyphId = glyphNames.indexOf(baseEncoding[charCode]);
  166. if (glyphId >= 0) {
  167. charCodeToGlyphId[charCode] = glyphId;
  168. } else {
  169. charCodeToGlyphId[charCode] = 0; // notdef
  170. }
  171. }
  172. }
  173. // Lastly, merge in the differences.
  174. const differences = properties.differences;
  175. let glyphsUnicodeMap;
  176. if (differences) {
  177. for (charCode in differences) {
  178. const glyphName = differences[charCode];
  179. glyphId = glyphNames.indexOf(glyphName);
  180. if (glyphId === -1) {
  181. if (!glyphsUnicodeMap) {
  182. glyphsUnicodeMap = getGlyphsUnicode();
  183. }
  184. const standardGlyphName = recoverGlyphName(glyphName, glyphsUnicodeMap);
  185. if (standardGlyphName !== glyphName) {
  186. glyphId = glyphNames.indexOf(standardGlyphName);
  187. }
  188. }
  189. if (glyphId >= 0) {
  190. charCodeToGlyphId[charCode] = glyphId;
  191. } else {
  192. charCodeToGlyphId[charCode] = 0; // notdef
  193. }
  194. }
  195. }
  196. return charCodeToGlyphId;
  197. }
  198. function normalizeFontName(name) {
  199. return name.replace(/[,_]/g, "-").replace(/\s/g, "");
  200. }
  201. export {
  202. FontFlags,
  203. getFontType,
  204. MacStandardGlyphOrdering,
  205. normalizeFontName,
  206. recoverGlyphName,
  207. SEAC_ANALYSIS_ENABLED,
  208. type1FontGlyphMapping,
  209. };