2
0

fonts_utils.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2022 Mozilla Foundation
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. *
  19. * @licend The above is the entire license notice for the
  20. * Javascript code in this page
  21. */
  22. "use strict";
  23. Object.defineProperty(exports, "__esModule", {
  24. value: true
  25. });
  26. exports.SEAC_ANALYSIS_ENABLED = exports.MacStandardGlyphOrdering = exports.FontFlags = void 0;
  27. exports.getFontType = getFontType;
  28. exports.normalizeFontName = normalizeFontName;
  29. exports.recoverGlyphName = recoverGlyphName;
  30. exports.type1FontGlyphMapping = type1FontGlyphMapping;
  31. var _util = require("../shared/util.js");
  32. var _encodings = require("./encodings.js");
  33. var _glyphlist = require("./glyphlist.js");
  34. var _unicode = require("./unicode.js");
  35. const SEAC_ANALYSIS_ENABLED = true;
  36. exports.SEAC_ANALYSIS_ENABLED = SEAC_ANALYSIS_ENABLED;
  37. const FontFlags = {
  38. FixedPitch: 1,
  39. Serif: 2,
  40. Symbolic: 4,
  41. Script: 8,
  42. Nonsymbolic: 32,
  43. Italic: 64,
  44. AllCap: 65536,
  45. SmallCap: 131072,
  46. ForceBold: 262144
  47. };
  48. exports.FontFlags = FontFlags;
  49. const MacStandardGlyphOrdering = [".notdef", ".null", "nonmarkingreturn", "space", "exclam", "quotedbl", "numbersign", "dollar", "percent", "ampersand", "quotesingle", "parenleft", "parenright", "asterisk", "plus", "comma", "hyphen", "period", "slash", "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "colon", "semicolon", "less", "equal", "greater", "question", "at", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "bracketleft", "backslash", "bracketright", "asciicircum", "underscore", "grave", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "braceleft", "bar", "braceright", "asciitilde", "Adieresis", "Aring", "Ccedilla", "Eacute", "Ntilde", "Odieresis", "Udieresis", "aacute", "agrave", "acircumflex", "adieresis", "atilde", "aring", "ccedilla", "eacute", "egrave", "ecircumflex", "edieresis", "iacute", "igrave", "icircumflex", "idieresis", "ntilde", "oacute", "ograve", "ocircumflex", "odieresis", "otilde", "uacute", "ugrave", "ucircumflex", "udieresis", "dagger", "degree", "cent", "sterling", "section", "bullet", "paragraph", "germandbls", "registered", "copyright", "trademark", "acute", "dieresis", "notequal", "AE", "Oslash", "infinity", "plusminus", "lessequal", "greaterequal", "yen", "mu", "partialdiff", "summation", "product", "pi", "integral", "ordfeminine", "ordmasculine", "Omega", "ae", "oslash", "questiondown", "exclamdown", "logicalnot", "radical", "florin", "approxequal", "Delta", "guillemotleft", "guillemotright", "ellipsis", "nonbreakingspace", "Agrave", "Atilde", "Otilde", "OE", "oe", "endash", "emdash", "quotedblleft", "quotedblright", "quoteleft", "quoteright", "divide", "lozenge", "ydieresis", "Ydieresis", "fraction", "currency", "guilsinglleft", "guilsinglright", "fi", "fl", "daggerdbl", "periodcentered", "quotesinglbase", "quotedblbase", "perthousand", "Acircumflex", "Ecircumflex", "Aacute", "Edieresis", "Egrave", "Iacute", "Icircumflex", "Idieresis", "Igrave", "Oacute", "Ocircumflex", "apple", "Ograve", "Uacute", "Ucircumflex", "Ugrave", "dotlessi", "circumflex", "tilde", "macron", "breve", "dotaccent", "ring", "cedilla", "hungarumlaut", "ogonek", "caron", "Lslash", "lslash", "Scaron", "scaron", "Zcaron", "zcaron", "brokenbar", "Eth", "eth", "Yacute", "yacute", "Thorn", "thorn", "minus", "multiply", "onesuperior", "twosuperior", "threesuperior", "onehalf", "onequarter", "threequarters", "franc", "Gbreve", "gbreve", "Idotaccent", "Scedilla", "scedilla", "Cacute", "cacute", "Ccaron", "ccaron", "dcroat"];
  50. exports.MacStandardGlyphOrdering = MacStandardGlyphOrdering;
  51. function getFontType(type, subtype, isStandardFont = false) {
  52. switch (type) {
  53. case "Type1":
  54. if (isStandardFont) {
  55. return _util.FontType.TYPE1STANDARD;
  56. }
  57. return subtype === "Type1C" ? _util.FontType.TYPE1C : _util.FontType.TYPE1;
  58. case "CIDFontType0":
  59. return subtype === "CIDFontType0C" ? _util.FontType.CIDFONTTYPE0C : _util.FontType.CIDFONTTYPE0;
  60. case "OpenType":
  61. return _util.FontType.OPENTYPE;
  62. case "TrueType":
  63. return _util.FontType.TRUETYPE;
  64. case "CIDFontType2":
  65. return _util.FontType.CIDFONTTYPE2;
  66. case "MMType1":
  67. return _util.FontType.MMTYPE1;
  68. case "Type0":
  69. return _util.FontType.TYPE0;
  70. default:
  71. return _util.FontType.UNKNOWN;
  72. }
  73. }
  74. function recoverGlyphName(name, glyphsUnicodeMap) {
  75. if (glyphsUnicodeMap[name] !== undefined) {
  76. return name;
  77. }
  78. const unicode = (0, _unicode.getUnicodeForGlyph)(name, glyphsUnicodeMap);
  79. if (unicode !== -1) {
  80. for (const key in glyphsUnicodeMap) {
  81. if (glyphsUnicodeMap[key] === unicode) {
  82. return key;
  83. }
  84. }
  85. }
  86. (0, _util.info)("Unable to recover a standard glyph name for: " + name);
  87. return name;
  88. }
  89. function type1FontGlyphMapping(properties, builtInEncoding, glyphNames) {
  90. const charCodeToGlyphId = Object.create(null);
  91. let glyphId, charCode, baseEncoding;
  92. const isSymbolicFont = !!(properties.flags & FontFlags.Symbolic);
  93. if (properties.isInternalFont) {
  94. baseEncoding = builtInEncoding;
  95. for (charCode = 0; charCode < baseEncoding.length; charCode++) {
  96. glyphId = glyphNames.indexOf(baseEncoding[charCode]);
  97. if (glyphId >= 0) {
  98. charCodeToGlyphId[charCode] = glyphId;
  99. } else {
  100. charCodeToGlyphId[charCode] = 0;
  101. }
  102. }
  103. } else if (properties.baseEncodingName) {
  104. baseEncoding = (0, _encodings.getEncoding)(properties.baseEncodingName);
  105. for (charCode = 0; charCode < baseEncoding.length; charCode++) {
  106. glyphId = glyphNames.indexOf(baseEncoding[charCode]);
  107. if (glyphId >= 0) {
  108. charCodeToGlyphId[charCode] = glyphId;
  109. } else {
  110. charCodeToGlyphId[charCode] = 0;
  111. }
  112. }
  113. } else if (isSymbolicFont) {
  114. for (charCode in builtInEncoding) {
  115. charCodeToGlyphId[charCode] = builtInEncoding[charCode];
  116. }
  117. } else {
  118. baseEncoding = _encodings.StandardEncoding;
  119. for (charCode = 0; charCode < baseEncoding.length; charCode++) {
  120. glyphId = glyphNames.indexOf(baseEncoding[charCode]);
  121. if (glyphId >= 0) {
  122. charCodeToGlyphId[charCode] = glyphId;
  123. } else {
  124. charCodeToGlyphId[charCode] = 0;
  125. }
  126. }
  127. }
  128. const differences = properties.differences;
  129. let glyphsUnicodeMap;
  130. if (differences) {
  131. for (charCode in differences) {
  132. const glyphName = differences[charCode];
  133. glyphId = glyphNames.indexOf(glyphName);
  134. if (glyphId === -1) {
  135. if (!glyphsUnicodeMap) {
  136. glyphsUnicodeMap = (0, _glyphlist.getGlyphsUnicode)();
  137. }
  138. const standardGlyphName = recoverGlyphName(glyphName, glyphsUnicodeMap);
  139. if (standardGlyphName !== glyphName) {
  140. glyphId = glyphNames.indexOf(standardGlyphName);
  141. }
  142. }
  143. if (glyphId >= 0) {
  144. charCodeToGlyphId[charCode] = glyphId;
  145. } else {
  146. charCodeToGlyphId[charCode] = 0;
  147. }
  148. }
  149. }
  150. return charCodeToGlyphId;
  151. }
  152. function normalizeFontName(name) {
  153. return name.replace(/[,_]/g, "-").replace(/\s/g, "");
  154. }