2
0

cff_font.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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.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 {
  56. cidToGidMap,
  57. cMap
  58. } = properties;
  59. const charsets = cff.charset.charset;
  60. let charCodeToGlyphId;
  61. let glyphId;
  62. if (properties.composite) {
  63. let invCidToGidMap;
  64. if (cidToGidMap && cidToGidMap.length > 0) {
  65. invCidToGidMap = Object.create(null);
  66. for (let i = 0, ii = cidToGidMap.length; i < ii; i++) {
  67. const gid = cidToGidMap[i];
  68. if (gid !== undefined) {
  69. invCidToGidMap[gid] = i;
  70. }
  71. }
  72. }
  73. charCodeToGlyphId = Object.create(null);
  74. let charCode;
  75. if (cff.isCIDFont) {
  76. for (glyphId = 0; glyphId < charsets.length; glyphId++) {
  77. const cid = charsets[glyphId];
  78. charCode = cMap.charCodeOf(cid);
  79. if (invCidToGidMap && invCidToGidMap[charCode] !== undefined) {
  80. charCode = invCidToGidMap[charCode];
  81. }
  82. charCodeToGlyphId[charCode] = glyphId;
  83. }
  84. } else {
  85. for (glyphId = 0; glyphId < cff.charStrings.count; glyphId++) {
  86. charCode = cMap.charCodeOf(glyphId);
  87. charCodeToGlyphId[charCode] = glyphId;
  88. }
  89. }
  90. return charCodeToGlyphId;
  91. }
  92. let encoding = cff.encoding ? cff.encoding.encoding : null;
  93. if (properties.isInternalFont) {
  94. encoding = properties.defaultEncoding;
  95. }
  96. charCodeToGlyphId = (0, _fonts_utils.type1FontGlyphMapping)(properties, encoding, charsets);
  97. return charCodeToGlyphId;
  98. }
  99. hasGlyphId(id) {
  100. return this.cff.hasGlyphId(id);
  101. }
  102. _createBuiltInEncoding() {
  103. const {
  104. charset,
  105. encoding
  106. } = this.cff;
  107. if (!charset || !encoding) {
  108. return;
  109. }
  110. const charsets = charset.charset,
  111. encodings = encoding.encoding;
  112. const map = [];
  113. for (const charCode in encodings) {
  114. const glyphId = encodings[charCode];
  115. if (glyphId >= 0) {
  116. const glyphName = charsets[glyphId];
  117. if (glyphName) {
  118. map[charCode] = glyphName;
  119. }
  120. }
  121. }
  122. if (map.length > 0) {
  123. this.properties.builtInEncoding = map;
  124. }
  125. }
  126. }
  127. exports.CFFFont = CFFFont;