default_appearance.js 3.2 KB

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