cff_font.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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.CFFFont = void 0;
  27. var _cff_parser = require("./cff_parser.js");
  28. var _fonts_utils = require("./fonts_utils.js");
  29. var _util = require("../shared/util.js");
  30. class CFFFont {
  31. constructor(file, properties) {
  32. this.properties = properties;
  33. const parser = new _cff_parser.CFFParser(file, properties, _fonts_utils.SEAC_ANALYSIS_ENABLED);
  34. this.cff = parser.parse();
  35. this.cff.duplicateFirstGlyph();
  36. const compiler = new _cff_parser.CFFCompiler(this.cff);
  37. this.seacs = this.cff.seacs;
  38. try {
  39. this.data = compiler.compile();
  40. } catch (e) {
  41. (0, _util.warn)("Failed to compile font " + properties.loadedName);
  42. this.data = file;
  43. }
  44. this._createBuiltInEncoding();
  45. }
  46. get numGlyphs() {
  47. return this.cff.charStrings.count;
  48. }
  49. getCharset() {
  50. return this.cff.charset.charset;
  51. }
  52. getGlyphMapping() {
  53. const cff = this.cff;
  54. const properties = this.properties;
  55. const charsets = cff.charset.charset;
  56. let charCodeToGlyphId;
  57. let glyphId;
  58. if (properties.composite) {
  59. charCodeToGlyphId = Object.create(null);
  60. let charCode;
  61. if (cff.isCIDFont) {
  62. for (glyphId = 0; glyphId < charsets.length; glyphId++) {
  63. const cid = charsets[glyphId];
  64. charCode = properties.cMap.charCodeOf(cid);
  65. charCodeToGlyphId[charCode] = glyphId;
  66. }
  67. } else {
  68. for (glyphId = 0; glyphId < cff.charStrings.count; glyphId++) {
  69. charCode = properties.cMap.charCodeOf(glyphId);
  70. charCodeToGlyphId[charCode] = glyphId;
  71. }
  72. }
  73. return charCodeToGlyphId;
  74. }
  75. const encoding = cff.encoding ? cff.encoding.encoding : null;
  76. charCodeToGlyphId = (0, _fonts_utils.type1FontGlyphMapping)(properties, encoding, charsets);
  77. return charCodeToGlyphId;
  78. }
  79. hasGlyphId(id) {
  80. return this.cff.hasGlyphId(id);
  81. }
  82. _createBuiltInEncoding() {
  83. const {
  84. charset,
  85. encoding
  86. } = this.cff;
  87. if (!charset || !encoding) {
  88. return;
  89. }
  90. const charsets = charset.charset,
  91. encodings = encoding.encoding;
  92. const map = [];
  93. for (const charCode in encodings) {
  94. const glyphId = encodings[charCode];
  95. if (glyphId >= 0) {
  96. const glyphName = charsets[glyphId];
  97. if (glyphName) {
  98. map[charCode] = glyphName;
  99. }
  100. }
  101. }
  102. if (map.length > 0) {
  103. this.properties.builtInEncoding = map;
  104. }
  105. }
  106. }
  107. exports.CFFFont = CFFFont;