default_appearance.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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.createDefaultAppearance = createDefaultAppearance;
  27. exports.getPdfColor = getPdfColor;
  28. exports.parseDefaultAppearance = parseDefaultAppearance;
  29. var _core_utils = require("./core_utils.js");
  30. var _util = require("../shared/util.js");
  31. var _colorspace = require("./colorspace.js");
  32. var _evaluator = require("./evaluator.js");
  33. var _primitives = require("./primitives.js");
  34. var _stream = require("./stream.js");
  35. class DefaultAppearanceEvaluator extends _evaluator.EvaluatorPreprocessor {
  36. constructor(str) {
  37. super(new _stream.StringStream(str));
  38. }
  39. parse() {
  40. const operation = {
  41. fn: 0,
  42. args: []
  43. };
  44. const result = {
  45. fontSize: 0,
  46. fontName: "",
  47. fontColor: new Uint8ClampedArray(3)
  48. };
  49. try {
  50. while (true) {
  51. operation.args.length = 0;
  52. if (!this.read(operation)) {
  53. break;
  54. }
  55. if (this.savedStatesDepth !== 0) {
  56. continue;
  57. }
  58. const {
  59. fn,
  60. args
  61. } = operation;
  62. switch (fn | 0) {
  63. case _util.OPS.setFont:
  64. const [fontName, fontSize] = args;
  65. if (fontName instanceof _primitives.Name) {
  66. result.fontName = fontName.name;
  67. }
  68. if (typeof fontSize === "number" && fontSize > 0) {
  69. result.fontSize = fontSize;
  70. }
  71. break;
  72. case _util.OPS.setFillRGBColor:
  73. _colorspace.ColorSpace.singletons.rgb.getRgbItem(args, 0, result.fontColor, 0);
  74. break;
  75. case _util.OPS.setFillGray:
  76. _colorspace.ColorSpace.singletons.gray.getRgbItem(args, 0, result.fontColor, 0);
  77. break;
  78. case _util.OPS.setFillColorSpace:
  79. _colorspace.ColorSpace.singletons.cmyk.getRgbItem(args, 0, result.fontColor, 0);
  80. break;
  81. }
  82. }
  83. } catch (reason) {
  84. (0, _util.warn)(`parseDefaultAppearance - ignoring errors: "${reason}".`);
  85. }
  86. return result;
  87. }
  88. }
  89. function parseDefaultAppearance(str) {
  90. return new DefaultAppearanceEvaluator(str).parse();
  91. }
  92. function getPdfColor(color, isFill) {
  93. if (color[0] === color[1] && color[1] === color[2]) {
  94. const gray = color[0] / 255;
  95. return `${(0, _core_utils.numberToString)(gray)} ${isFill ? "g" : "G"}`;
  96. }
  97. return Array.from(color, c => (0, _core_utils.numberToString)(c / 255)).join(" ") + ` ${isFill ? "rg" : "RG"}`;
  98. }
  99. function createDefaultAppearance({
  100. fontSize,
  101. fontName,
  102. fontColor
  103. }) {
  104. return `/${(0, _core_utils.escapePDFName)(fontName)} ${fontSize} Tf ${getPdfColor(fontColor, true)}`;
  105. }