2
0

fonts.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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.getMetrics = getMetrics;
  27. exports.selectFont = selectFont;
  28. exports.FontFinder = void 0;
  29. var _xfa_object = require("./xfa_object.js");
  30. var _utils = require("./utils.js");
  31. var _util = require("../../shared/util.js");
  32. class FontFinder {
  33. constructor(pdfFonts) {
  34. this.fonts = new Map();
  35. this.cache = new Map();
  36. this.warned = new Set();
  37. this.defaultFont = null;
  38. this.add(pdfFonts);
  39. }
  40. add(pdfFonts, reallyMissingFonts = null) {
  41. for (const pdfFont of pdfFonts) {
  42. this.addPdfFont(pdfFont);
  43. }
  44. for (const pdfFont of this.fonts.values()) {
  45. if (!pdfFont.regular) {
  46. pdfFont.regular = pdfFont.italic || pdfFont.bold || pdfFont.bolditalic;
  47. }
  48. }
  49. if (!reallyMissingFonts || reallyMissingFonts.size === 0) {
  50. return;
  51. }
  52. const myriad = this.fonts.get("PdfJS-Fallback-PdfJS-XFA");
  53. for (const missing of reallyMissingFonts) {
  54. this.fonts.set(missing, myriad);
  55. }
  56. }
  57. addPdfFont(pdfFont) {
  58. const cssFontInfo = pdfFont.cssFontInfo;
  59. const name = cssFontInfo.fontFamily;
  60. let font = this.fonts.get(name);
  61. if (!font) {
  62. font = Object.create(null);
  63. this.fonts.set(name, font);
  64. if (!this.defaultFont) {
  65. this.defaultFont = font;
  66. }
  67. }
  68. let property = "";
  69. const fontWeight = parseFloat(cssFontInfo.fontWeight);
  70. if (parseFloat(cssFontInfo.italicAngle) !== 0) {
  71. property = fontWeight >= 700 ? "bolditalic" : "italic";
  72. } else if (fontWeight >= 700) {
  73. property = "bold";
  74. }
  75. if (!property) {
  76. if (pdfFont.name.includes("Bold") || pdfFont.psName && pdfFont.psName.includes("Bold")) {
  77. property = "bold";
  78. }
  79. if (pdfFont.name.includes("Italic") || pdfFont.name.endsWith("It") || pdfFont.psName && (pdfFont.psName.includes("Italic") || pdfFont.psName.endsWith("It"))) {
  80. property += "italic";
  81. }
  82. }
  83. if (!property) {
  84. property = "regular";
  85. }
  86. font[property] = pdfFont;
  87. }
  88. getDefault() {
  89. return this.defaultFont;
  90. }
  91. find(fontName, mustWarn = true) {
  92. let font = this.fonts.get(fontName) || this.cache.get(fontName);
  93. if (font) {
  94. return font;
  95. }
  96. const pattern = /,|-|_| |bolditalic|bold|italic|regular|it/gi;
  97. let name = fontName.replace(pattern, "");
  98. font = this.fonts.get(name);
  99. if (font) {
  100. this.cache.set(fontName, font);
  101. return font;
  102. }
  103. name = name.toLowerCase();
  104. const maybe = [];
  105. for (const [family, pdfFont] of this.fonts.entries()) {
  106. if (family.replace(pattern, "").toLowerCase().startsWith(name)) {
  107. maybe.push(pdfFont);
  108. }
  109. }
  110. if (maybe.length === 0) {
  111. for (const [, pdfFont] of this.fonts.entries()) {
  112. if (pdfFont.regular.name && pdfFont.regular.name.replace(pattern, "").toLowerCase().startsWith(name)) {
  113. maybe.push(pdfFont);
  114. }
  115. }
  116. }
  117. if (maybe.length === 0) {
  118. name = name.replace(/psmt|mt/gi, "");
  119. for (const [family, pdfFont] of this.fonts.entries()) {
  120. if (family.replace(pattern, "").toLowerCase().startsWith(name)) {
  121. maybe.push(pdfFont);
  122. }
  123. }
  124. }
  125. if (maybe.length === 0) {
  126. for (const pdfFont of this.fonts.values()) {
  127. if (pdfFont.regular.name && pdfFont.regular.name.replace(pattern, "").toLowerCase().startsWith(name)) {
  128. maybe.push(pdfFont);
  129. }
  130. }
  131. }
  132. if (maybe.length >= 1) {
  133. if (maybe.length !== 1 && mustWarn) {
  134. (0, _util.warn)(`XFA - Too many choices to guess the correct font: ${fontName}`);
  135. }
  136. this.cache.set(fontName, maybe[0]);
  137. return maybe[0];
  138. }
  139. if (mustWarn && !this.warned.has(fontName)) {
  140. this.warned.add(fontName);
  141. (0, _util.warn)(`XFA - Cannot find the font: ${fontName}`);
  142. }
  143. return null;
  144. }
  145. }
  146. exports.FontFinder = FontFinder;
  147. function selectFont(xfaFont, typeface) {
  148. if (xfaFont.posture === "italic") {
  149. if (xfaFont.weight === "bold") {
  150. return typeface.bolditalic;
  151. }
  152. return typeface.italic;
  153. } else if (xfaFont.weight === "bold") {
  154. return typeface.bold;
  155. }
  156. return typeface.regular;
  157. }
  158. function getMetrics(xfaFont, real = false) {
  159. let pdfFont = null;
  160. if (xfaFont) {
  161. const name = (0, _utils.stripQuotes)(xfaFont.typeface);
  162. const typeface = xfaFont[_xfa_object.$globalData].fontFinder.find(name);
  163. pdfFont = selectFont(xfaFont, typeface);
  164. }
  165. if (!pdfFont) {
  166. return {
  167. lineHeight: 12,
  168. lineGap: 2,
  169. lineNoGap: 10
  170. };
  171. }
  172. const size = xfaFont.size || 10;
  173. const lineHeight = pdfFont.lineHeight ? Math.max(real ? 0 : 1.2, pdfFont.lineHeight) : 1.2;
  174. const lineGap = pdfFont.lineGap === undefined ? 0.2 : pdfFont.lineGap;
  175. return {
  176. lineHeight: lineHeight * size,
  177. lineGap: lineGap * size,
  178. lineNoGap: Math.max(1, lineHeight - lineGap) * size
  179. };
  180. }