cff_font.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. let encoding = cff.encoding ? cff.encoding.encoding : null;
  76. if (properties.isInternalFont) {
  77. encoding = properties.defaultEncoding;
  78. }
  79. charCodeToGlyphId = (0, _fonts_utils.type1FontGlyphMapping)(properties, encoding, charsets);
  80. return charCodeToGlyphId;
  81. }
  82. hasGlyphId(id) {
  83. return this.cff.hasGlyphId(id);
  84. }
  85. _createBuiltInEncoding() {
  86. const {
  87. charset,
  88. encoding
  89. } = this.cff;
  90. if (!charset || !encoding) {
  91. return;
  92. }
  93. const charsets = charset.charset,
  94. encodings = encoding.encoding;
  95. const map = [];
  96. for (const charCode in encodings) {
  97. const glyphId = encodings[charCode];
  98. if (glyphId >= 0) {
  99. const glyphName = charsets[glyphId];
  100. if (glyphName) {
  101. map[charCode] = glyphName;
  102. }
  103. }
  104. }
  105. if (map.length > 0) {
  106. this.properties.builtInEncoding = map;
  107. }
  108. }
  109. }
  110. exports.CFFFont = CFFFont;