2
0

fonts.js 4.8 KB

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