fonts_utils.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2021 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.getFontType = getFontType;
  27. exports.recoverGlyphName = recoverGlyphName;
  28. exports.type1FontGlyphMapping = type1FontGlyphMapping;
  29. exports.SEAC_ANALYSIS_ENABLED = exports.MacStandardGlyphOrdering = exports.FontFlags = void 0;
  30. var _util = require("../shared/util.js");
  31. var _encodings = require("./encodings.js");
  32. var _glyphlist = require("./glyphlist.js");
  33. var _unicode = require("./unicode.js");
  34. const SEAC_ANALYSIS_ENABLED = true;
  35. exports.SEAC_ANALYSIS_ENABLED = SEAC_ANALYSIS_ENABLED;
  36. const FontFlags = {
  37. FixedPitch: 1,
  38. Serif: 2,
  39. Symbolic: 4,
  40. Script: 8,
  41. Nonsymbolic: 32,
  42. Italic: 64,
  43. AllCap: 65536,
  44. SmallCap: 131072,
  45. ForceBold: 262144
  46. };
  47. exports.FontFlags = FontFlags;
  48. 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"];
  49. exports.MacStandardGlyphOrdering = MacStandardGlyphOrdering;
  50. function getFontType(type, subtype) {
  51. switch (type) {
  52. case "Type1":
  53. return subtype === "Type1C" ? _util.FontType.TYPE1C : _util.FontType.TYPE1;
  54. case "CIDFontType0":
  55. return subtype === "CIDFontType0C" ? _util.FontType.CIDFONTTYPE0C : _util.FontType.CIDFONTTYPE0;
  56. case "OpenType":
  57. return _util.FontType.OPENTYPE;
  58. case "TrueType":
  59. return _util.FontType.TRUETYPE;
  60. case "CIDFontType2":
  61. return _util.FontType.CIDFONTTYPE2;
  62. case "MMType1":
  63. return _util.FontType.MMTYPE1;
  64. case "Type0":
  65. return _util.FontType.TYPE0;
  66. default:
  67. return _util.FontType.UNKNOWN;
  68. }
  69. }
  70. function recoverGlyphName(name, glyphsUnicodeMap) {
  71. if (glyphsUnicodeMap[name] !== undefined) {
  72. return name;
  73. }
  74. const unicode = (0, _unicode.getUnicodeForGlyph)(name, glyphsUnicodeMap);
  75. if (unicode !== -1) {
  76. for (const key in glyphsUnicodeMap) {
  77. if (glyphsUnicodeMap[key] === unicode) {
  78. return key;
  79. }
  80. }
  81. }
  82. (0, _util.info)("Unable to recover a standard glyph name for: " + name);
  83. return name;
  84. }
  85. function type1FontGlyphMapping(properties, builtInEncoding, glyphNames) {
  86. const charCodeToGlyphId = Object.create(null);
  87. let glyphId, charCode, baseEncoding;
  88. const isSymbolicFont = !!(properties.flags & FontFlags.Symbolic);
  89. if (properties.baseEncodingName) {
  90. baseEncoding = (0, _encodings.getEncoding)(properties.baseEncodingName);
  91. for (charCode = 0; charCode < baseEncoding.length; charCode++) {
  92. glyphId = glyphNames.indexOf(baseEncoding[charCode]);
  93. if (glyphId >= 0) {
  94. charCodeToGlyphId[charCode] = glyphId;
  95. } else {
  96. charCodeToGlyphId[charCode] = 0;
  97. }
  98. }
  99. } else if (isSymbolicFont) {
  100. for (charCode in builtInEncoding) {
  101. charCodeToGlyphId[charCode] = builtInEncoding[charCode];
  102. }
  103. } else {
  104. baseEncoding = _encodings.StandardEncoding;
  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. }
  114. const differences = properties.differences;
  115. let glyphsUnicodeMap;
  116. if (differences) {
  117. for (charCode in differences) {
  118. const glyphName = differences[charCode];
  119. glyphId = glyphNames.indexOf(glyphName);
  120. if (glyphId === -1) {
  121. if (!glyphsUnicodeMap) {
  122. glyphsUnicodeMap = (0, _glyphlist.getGlyphsUnicode)();
  123. }
  124. const standardGlyphName = recoverGlyphName(glyphName, glyphsUnicodeMap);
  125. if (standardGlyphName !== glyphName) {
  126. glyphId = glyphNames.indexOf(standardGlyphName);
  127. }
  128. }
  129. if (glyphId >= 0) {
  130. charCodeToGlyphId[charCode] = glyphId;
  131. } else {
  132. charCodeToGlyphId[charCode] = 0;
  133. }
  134. }
  135. }
  136. return charCodeToGlyphId;
  137. }