1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | "use strict";
|
23 |
|
24 | Object.defineProperty(exports, "__esModule", {
|
25 | value: true
|
26 | });
|
27 | exports.createDefaultAppearance = createDefaultAppearance;
|
28 | exports.getPdfColor = getPdfColor;
|
29 | exports.parseDefaultAppearance = parseDefaultAppearance;
|
30 |
|
31 | var _core_utils = require("./core_utils.js");
|
32 |
|
33 | var _util = require("../shared/util.js");
|
34 |
|
35 | var _colorspace = require("./colorspace.js");
|
36 |
|
37 | var _evaluator = require("./evaluator.js");
|
38 |
|
39 | var _primitives = require("./primitives.js");
|
40 |
|
41 | var _stream = require("./stream.js");
|
42 |
|
43 | class 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 |
|
115 | function parseDefaultAppearance(str) {
|
116 | return new DefaultAppearanceEvaluator(str).parse();
|
117 | }
|
118 |
|
119 | function 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 |
|
128 | function 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 |