UNPKG

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