@hadi77ir/pdfme-svg 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +63 -0
- package/dist/applyparseattributes.d.ts +6 -0
- package/dist/applyparseattributes.d.ts.map +1 -0
- package/dist/applyparseattributes.js +333 -0
- package/dist/applyparseattributes.js.map +1 -0
- package/dist/context/attributestate.d.ts +34 -0
- package/dist/context/attributestate.d.ts.map +1 -0
- package/dist/context/attributestate.js +109 -0
- package/dist/context/attributestate.js.map +1 -0
- package/dist/context/context.d.ts +59 -0
- package/dist/context/context.d.ts.map +1 -0
- package/dist/context/context.js +46 -0
- package/dist/context/context.js.map +1 -0
- package/dist/context/referenceshandler.d.ts +15 -0
- package/dist/context/referenceshandler.d.ts.map +1 -0
- package/dist/context/referenceshandler.js +31 -0
- package/dist/context/referenceshandler.js.map +1 -0
- package/dist/context/stylesheets.d.ts +13 -0
- package/dist/context/stylesheets.d.ts.map +1 -0
- package/dist/context/stylesheets.js +163 -0
- package/dist/context/stylesheets.js.map +1 -0
- package/dist/context/textmeasure.d.ts +19 -0
- package/dist/context/textmeasure.d.ts.map +1 -0
- package/dist/context/textmeasure.js +93 -0
- package/dist/context/textmeasure.js.map +1 -0
- package/dist/context/viewport.d.ts +6 -0
- package/dist/context/viewport.d.ts.map +1 -0
- package/dist/context/viewport.js +7 -0
- package/dist/context/viewport.js.map +1 -0
- package/dist/fill/ColorFill.d.ts +10 -0
- package/dist/fill/ColorFill.d.ts.map +1 -0
- package/dist/fill/ColorFill.js +10 -0
- package/dist/fill/ColorFill.js.map +1 -0
- package/dist/fill/Fill.d.ts +14 -0
- package/dist/fill/Fill.d.ts.map +1 -0
- package/dist/fill/Fill.js +2 -0
- package/dist/fill/Fill.js.map +1 -0
- package/dist/fill/GradientFill.d.ts +11 -0
- package/dist/fill/GradientFill.d.ts.map +1 -0
- package/dist/fill/GradientFill.js +37 -0
- package/dist/fill/GradientFill.js.map +1 -0
- package/dist/fill/PatternFill.d.ts +11 -0
- package/dist/fill/PatternFill.d.ts.map +1 -0
- package/dist/fill/PatternFill.js +67 -0
- package/dist/fill/PatternFill.js.map +1 -0
- package/dist/fill/parseFill.d.ts +4 -0
- package/dist/fill/parseFill.d.ts.map +1 -0
- package/dist/fill/parseFill.js +61 -0
- package/dist/fill/parseFill.js.map +1 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +232 -0
- package/dist/index.js.map +1 -0
- package/dist/jspdf-shim.d.ts +132 -0
- package/dist/jspdf-shim.d.ts.map +1 -0
- package/dist/jspdf-shim.js +744 -0
- package/dist/jspdf-shim.js.map +1 -0
- package/dist/jspdf.d.ts +2 -0
- package/dist/jspdf.d.ts.map +1 -0
- package/dist/jspdf.js +2 -0
- package/dist/jspdf.js.map +1 -0
- package/dist/markerlist.d.ts +24 -0
- package/dist/markerlist.d.ts.map +1 -0
- package/dist/markerlist.js +47 -0
- package/dist/markerlist.js.map +1 -0
- package/dist/nodes/anchor.d.ts +6 -0
- package/dist/nodes/anchor.d.ts.map +1 -0
- package/dist/nodes/anchor.js +15 -0
- package/dist/nodes/anchor.js.map +1 -0
- package/dist/nodes/circle.d.ts +11 -0
- package/dist/nodes/circle.d.ts.map +1 -0
- package/dist/nodes/circle.js +17 -0
- package/dist/nodes/circle.js.map +1 -0
- package/dist/nodes/clippath.d.ts +10 -0
- package/dist/nodes/clippath.d.ts.map +1 -0
- package/dist/nodes/clippath.js +48 -0
- package/dist/nodes/clippath.js.map +1 -0
- package/dist/nodes/containernode.d.ts +8 -0
- package/dist/nodes/containernode.d.ts.map +1 -0
- package/dist/nodes/containernode.js +13 -0
- package/dist/nodes/containernode.js.map +1 -0
- package/dist/nodes/ellipse.d.ts +9 -0
- package/dist/nodes/ellipse.d.ts.map +1 -0
- package/dist/nodes/ellipse.js +14 -0
- package/dist/nodes/ellipse.js.map +1 -0
- package/dist/nodes/ellipsebase.d.ts +14 -0
- package/dist/nodes/ellipsebase.d.ts.map +1 -0
- package/dist/nodes/ellipsebase.js +30 -0
- package/dist/nodes/ellipsebase.js.map +1 -0
- package/dist/nodes/geometrynode.d.ts +19 -0
- package/dist/nodes/geometrynode.d.ts.map +1 -0
- package/dist/nodes/geometrynode.js +213 -0
- package/dist/nodes/geometrynode.js.map +1 -0
- package/dist/nodes/gradient.d.ts +28 -0
- package/dist/nodes/gradient.d.ts.map +1 -0
- package/dist/nodes/gradient.js +87 -0
- package/dist/nodes/gradient.js.map +1 -0
- package/dist/nodes/graphicsnode.d.ts +4 -0
- package/dist/nodes/graphicsnode.d.ts.map +1 -0
- package/dist/nodes/graphicsnode.js +4 -0
- package/dist/nodes/graphicsnode.js.map +1 -0
- package/dist/nodes/group.d.ts +8 -0
- package/dist/nodes/group.d.ts.map +1 -0
- package/dist/nodes/group.js +11 -0
- package/dist/nodes/group.js.map +1 -0
- package/dist/nodes/image.d.ts +23 -0
- package/dist/nodes/image.d.ts.map +1 -0
- package/dist/nodes/image.js +152 -0
- package/dist/nodes/image.js.map +1 -0
- package/dist/nodes/line.d.ts +13 -0
- package/dist/nodes/line.d.ts.map +1 -0
- package/dist/nodes/line.js +30 -0
- package/dist/nodes/line.js.map +1 -0
- package/dist/nodes/lineargradient.d.ts +7 -0
- package/dist/nodes/lineargradient.d.ts.map +1 -0
- package/dist/nodes/lineargradient.js +15 -0
- package/dist/nodes/lineargradient.js.map +1 -0
- package/dist/nodes/marker.d.ts +11 -0
- package/dist/nodes/marker.d.ts.map +1 -0
- package/dist/nodes/marker.js +70 -0
- package/dist/nodes/marker.js.map +1 -0
- package/dist/nodes/nonrenderednode.d.ts +11 -0
- package/dist/nodes/nonrenderednode.d.ts.map +1 -0
- package/dist/nodes/nonrenderednode.js +15 -0
- package/dist/nodes/nonrenderednode.js.map +1 -0
- package/dist/nodes/path.d.ts +12 -0
- package/dist/nodes/path.d.ts.map +1 -0
- package/dist/nodes/path.js +75 -0
- package/dist/nodes/path.js.map +1 -0
- package/dist/nodes/pattern.d.ts +11 -0
- package/dist/nodes/pattern.d.ts.map +1 -0
- package/dist/nodes/pattern.js +39 -0
- package/dist/nodes/pattern.js.map +1 -0
- package/dist/nodes/polygon.d.ts +6 -0
- package/dist/nodes/polygon.d.ts.map +1 -0
- package/dist/nodes/polygon.js +7 -0
- package/dist/nodes/polygon.js.map +1 -0
- package/dist/nodes/polyline.d.ts +6 -0
- package/dist/nodes/polyline.d.ts.map +1 -0
- package/dist/nodes/polyline.js +7 -0
- package/dist/nodes/polyline.js.map +1 -0
- package/dist/nodes/radialgradient.d.ts +7 -0
- package/dist/nodes/radialgradient.d.ts.map +1 -0
- package/dist/nodes/radialgradient.js +21 -0
- package/dist/nodes/radialgradient.js.map +1 -0
- package/dist/nodes/rect.d.ts +12 -0
- package/dist/nodes/rect.d.ts.map +1 -0
- package/dist/nodes/rect.js +50 -0
- package/dist/nodes/rect.js.map +1 -0
- package/dist/nodes/renderednode.d.ts +7 -0
- package/dist/nodes/renderednode.d.ts.map +1 -0
- package/dist/nodes/renderednode.js +43 -0
- package/dist/nodes/renderednode.js.map +1 -0
- package/dist/nodes/svg.d.ts +21 -0
- package/dist/nodes/svg.d.ts.map +1 -0
- package/dist/nodes/svg.js +175 -0
- package/dist/nodes/svg.js.map +1 -0
- package/dist/nodes/svgnode.d.ts +18 -0
- package/dist/nodes/svgnode.d.ts.map +1 -0
- package/dist/nodes/svgnode.js +30 -0
- package/dist/nodes/svgnode.js.map +1 -0
- package/dist/nodes/symbol.d.ts +10 -0
- package/dist/nodes/symbol.d.ts.map +1 -0
- package/dist/nodes/symbol.js +62 -0
- package/dist/nodes/symbol.js.map +1 -0
- package/dist/nodes/text.d.ts +13 -0
- package/dist/nodes/text.d.ts.map +1 -0
- package/dist/nodes/text.js +172 -0
- package/dist/nodes/text.js.map +1 -0
- package/dist/nodes/traverse.d.ts +15 -0
- package/dist/nodes/traverse.d.ts.map +1 -0
- package/dist/nodes/traverse.js +47 -0
- package/dist/nodes/traverse.js.map +1 -0
- package/dist/nodes/use.d.ts +15 -0
- package/dist/nodes/use.d.ts.map +1 -0
- package/dist/nodes/use.js +99 -0
- package/dist/nodes/use.js.map +1 -0
- package/dist/nodes/void.d.ts +11 -0
- package/dist/nodes/void.d.ts.map +1 -0
- package/dist/nodes/void.js +19 -0
- package/dist/nodes/void.js.map +1 -0
- package/dist/parse.d.ts +5 -0
- package/dist/parse.d.ts.map +1 -0
- package/dist/parse.js +96 -0
- package/dist/parse.js.map +1 -0
- package/dist/svg2pdf.d.ts +10 -0
- package/dist/svg2pdf.d.ts.map +1 -0
- package/dist/svg2pdf.js +49 -0
- package/dist/svg2pdf.js.map +1 -0
- package/dist/textchunk.d.ts +36 -0
- package/dist/textchunk.d.ts.map +1 -0
- package/dist/textchunk.js +144 -0
- package/dist/textchunk.js.map +1 -0
- package/dist/utils/applyclippath.d.ts +6 -0
- package/dist/utils/applyclippath.d.ts.map +1 -0
- package/dist/utils/applyclippath.js +20 -0
- package/dist/utils/applyclippath.js.map +1 -0
- package/dist/utils/bbox.d.ts +6 -0
- package/dist/utils/bbox.d.ts.map +1 -0
- package/dist/utils/bbox.js +62 -0
- package/dist/utils/bbox.js.map +1 -0
- package/dist/utils/constants.d.ts +7 -0
- package/dist/utils/constants.d.ts.map +1 -0
- package/dist/utils/constants.js +17 -0
- package/dist/utils/constants.js.map +1 -0
- package/dist/utils/fonts.d.ts +14 -0
- package/dist/utils/fonts.d.ts.map +1 -0
- package/dist/utils/fonts.js +59 -0
- package/dist/utils/fonts.js.map +1 -0
- package/dist/utils/geometry.d.ts +11 -0
- package/dist/utils/geometry.d.ts.map +1 -0
- package/dist/utils/geometry.js +26 -0
- package/dist/utils/geometry.js.map +1 -0
- package/dist/utils/misc.d.ts +12 -0
- package/dist/utils/misc.d.ts.map +1 -0
- package/dist/utils/misc.js +26 -0
- package/dist/utils/misc.js.map +1 -0
- package/dist/utils/node.d.ts +9 -0
- package/dist/utils/node.d.ts.map +1 -0
- package/dist/utils/node.js +58 -0
- package/dist/utils/node.js.map +1 -0
- package/dist/utils/parsing.d.ts +13 -0
- package/dist/utils/parsing.d.ts.map +1 -0
- package/dist/utils/parsing.js +41 -0
- package/dist/utils/parsing.js.map +1 -0
- package/dist/utils/path.d.ts +39 -0
- package/dist/utils/path.d.ts.map +1 -0
- package/dist/utils/path.js +86 -0
- package/dist/utils/path.js.map +1 -0
- package/dist/utils/rgbcolor.d.ts +15 -0
- package/dist/utils/rgbcolor.d.ts.map +1 -0
- package/dist/utils/rgbcolor.js +297 -0
- package/dist/utils/rgbcolor.js.map +1 -0
- package/dist/utils/text.d.ts +15 -0
- package/dist/utils/text.d.ts.map +1 -0
- package/dist/utils/text.js +57 -0
- package/dist/utils/text.js.map +1 -0
- package/dist/utils/transform.d.ts +5 -0
- package/dist/utils/transform.d.ts.map +1 -0
- package/dist/utils/transform.js +113 -0
- package/dist/utils/transform.js.map +1 -0
- package/package.json +57 -0
|
@@ -0,0 +1,744 @@
|
|
|
1
|
+
import { rgb, PDFOperator, pushGraphicsState, popGraphicsState, concatTransformationMatrix, setLineWidth, setStrokingRgbColor, setFillingRgbColor, moveTo, lineTo, appendBezierCurve, closePath, stroke, fill, fillEvenOdd, fillAndStroke, endPath, clip, clipEvenOdd, LineCapStyle, LineJoinStyle, setLineCap, setLineJoin, setDashPattern, setGraphicsState, drawObject, PDFName, PDFContentStream, StandardFonts, PDFNumber, PDFRawStream } from '@pdfme/pdf-lib';
|
|
2
|
+
export class Matrix {
|
|
3
|
+
constructor(a, b, c, d, e, f) {
|
|
4
|
+
this.a = a;
|
|
5
|
+
this.b = b;
|
|
6
|
+
this.c = c;
|
|
7
|
+
this.d = d;
|
|
8
|
+
this.e = e;
|
|
9
|
+
this.f = f;
|
|
10
|
+
}
|
|
11
|
+
get sx() { return this.a; }
|
|
12
|
+
set sx(v) { this.a = v; }
|
|
13
|
+
get sy() { return this.d; }
|
|
14
|
+
set sy(v) { this.d = v; }
|
|
15
|
+
get tx() { return this.e; }
|
|
16
|
+
set tx(v) { this.e = v; }
|
|
17
|
+
get ty() { return this.f; }
|
|
18
|
+
set ty(v) { this.f = v; }
|
|
19
|
+
inversed() {
|
|
20
|
+
const { a, b, c, d, e, f } = this;
|
|
21
|
+
const det = a * d - b * c;
|
|
22
|
+
if (!det) {
|
|
23
|
+
return new Matrix(1, 0, 0, 1, 0, 0);
|
|
24
|
+
}
|
|
25
|
+
const invDet = 1 / det;
|
|
26
|
+
return new Matrix(d * invDet, -b * invDet, -c * invDet, a * invDet, (c * f - d * e) * invDet, (b * e - a * f) * invDet);
|
|
27
|
+
}
|
|
28
|
+
applyToPoint(point) {
|
|
29
|
+
return {
|
|
30
|
+
x: point.x * this.a + point.y * this.c + this.e,
|
|
31
|
+
y: point.x * this.b + point.y * this.d + this.f
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
export const unitMatrix = new Matrix(1, 0, 0, 1, 0, 0);
|
|
36
|
+
export function matrixMult(m1, m2) {
|
|
37
|
+
return new Matrix(m1.a * m2.a + m1.b * m2.c, m1.a * m2.b + m1.b * m2.d, m1.c * m2.a + m1.d * m2.c, m1.c * m2.b + m1.d * m2.d, m1.e * m2.a + m1.f * m2.c + m2.e, m1.e * m2.b + m1.f * m2.d + m2.f);
|
|
38
|
+
}
|
|
39
|
+
export class GState {
|
|
40
|
+
constructor(options = {}) {
|
|
41
|
+
Object.assign(this, options);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
export class TilingPattern {
|
|
45
|
+
constructor(boundingBox, xStep, yStep, gState, matrix) {
|
|
46
|
+
this.boundingBox = boundingBox;
|
|
47
|
+
this.xStep = xStep;
|
|
48
|
+
this.yStep = yStep;
|
|
49
|
+
this.gState = gState;
|
|
50
|
+
this.matrix = matrix;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
export class ShadingPattern {
|
|
54
|
+
constructor(type, coords, colors, gState, matrix) {
|
|
55
|
+
this.type = type;
|
|
56
|
+
this.coords = coords;
|
|
57
|
+
this.colors = colors;
|
|
58
|
+
this.gState = gState;
|
|
59
|
+
this.matrix = matrix;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
export class jsPDF {
|
|
63
|
+
convertMatrixWithHeight(m, height) {
|
|
64
|
+
const { a, b, c, d, e, f } = m;
|
|
65
|
+
return new Matrix(a, -b, -c, d, c * height + e, height * (1 - d) - f);
|
|
66
|
+
}
|
|
67
|
+
convertMatrix(m) {
|
|
68
|
+
return this.convertMatrixWithHeight(m, this.internal.pageSize.getHeight());
|
|
69
|
+
}
|
|
70
|
+
convertMatrixForPattern(m) {
|
|
71
|
+
const h = this.internal.pageSize.getHeight();
|
|
72
|
+
return new Matrix(m.a, -m.b, m.c, -m.d, m.e, h - m.f);
|
|
73
|
+
}
|
|
74
|
+
normalizeReferenceKey(value) {
|
|
75
|
+
const trimmed = value.trim();
|
|
76
|
+
const urlMatch = /^url\(\s*['"]?(.*?)['"]?\s*\)$/i.exec(trimmed);
|
|
77
|
+
const raw = urlMatch ? urlMatch[1] : trimmed;
|
|
78
|
+
return raw.startsWith('#') ? raw.slice(1) : raw;
|
|
79
|
+
}
|
|
80
|
+
constructor(page, doc) {
|
|
81
|
+
this.unitMatrix = unitMatrix;
|
|
82
|
+
this.Matrix = (a, b, c, d, e, f) => new Matrix(a, b, c, d, e, f);
|
|
83
|
+
this.matrixMult = matrixMult;
|
|
84
|
+
this.currentMatrix = unitMatrix;
|
|
85
|
+
this.graphicsStateStack = [];
|
|
86
|
+
this.formObjects = {};
|
|
87
|
+
this.activeFormObject = null;
|
|
88
|
+
this.savedPage = null;
|
|
89
|
+
this.shadingPatterns = {};
|
|
90
|
+
this.tilingPatterns = {};
|
|
91
|
+
this.formObjectCounter = 0;
|
|
92
|
+
this.patternCounter = 0;
|
|
93
|
+
this.fontSize = 12;
|
|
94
|
+
this.currentFont = null;
|
|
95
|
+
this.currentTextColor = rgb(0, 0, 0);
|
|
96
|
+
this.page = page;
|
|
97
|
+
this.doc = doc;
|
|
98
|
+
this.internal = {
|
|
99
|
+
pageSize: {
|
|
100
|
+
getWidth: () => this.page.getWidth(),
|
|
101
|
+
getHeight: () => this.page.getHeight(),
|
|
102
|
+
},
|
|
103
|
+
scaleFactor: 1,
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
advancedAPI() { }
|
|
107
|
+
compatAPI() { }
|
|
108
|
+
getStringUnitWidth(text) {
|
|
109
|
+
if (!this.currentFont)
|
|
110
|
+
return 0;
|
|
111
|
+
return this.currentFont.widthOfTextAtSize(text, 1);
|
|
112
|
+
}
|
|
113
|
+
saveGraphicsState() {
|
|
114
|
+
this.page.pushOperators(pushGraphicsState());
|
|
115
|
+
this.graphicsStateStack.push({
|
|
116
|
+
matrix: this.currentMatrix,
|
|
117
|
+
font: this.currentFont,
|
|
118
|
+
fontSize: this.fontSize,
|
|
119
|
+
textColor: this.currentTextColor
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
restoreGraphicsState() {
|
|
123
|
+
this.page.pushOperators(popGraphicsState());
|
|
124
|
+
const state = this.graphicsStateStack.pop();
|
|
125
|
+
if (!state)
|
|
126
|
+
return;
|
|
127
|
+
this.currentMatrix = state.matrix;
|
|
128
|
+
this.currentFont = state.font;
|
|
129
|
+
this.fontSize = state.fontSize;
|
|
130
|
+
this.currentTextColor = state.textColor;
|
|
131
|
+
if (state.font && typeof this.page.setFont === 'function') {
|
|
132
|
+
this.page.setFont(state.font);
|
|
133
|
+
}
|
|
134
|
+
if (typeof this.page.setFontSize === 'function') {
|
|
135
|
+
this.page.setFontSize(state.fontSize);
|
|
136
|
+
}
|
|
137
|
+
if (state.textColor && typeof this.page.setFontColor === 'function') {
|
|
138
|
+
this.page.setFontColor(state.textColor);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
setCurrentTransformationMatrix(matrix) {
|
|
142
|
+
const inv = this.currentMatrix.inversed();
|
|
143
|
+
const rel = matrixMult(matrix, inv);
|
|
144
|
+
const converted = this.convertMatrix(rel);
|
|
145
|
+
this.page.pushOperators(concatTransformationMatrix(converted.a, converted.b, converted.c, converted.d, converted.e, converted.f));
|
|
146
|
+
this.currentMatrix = matrix;
|
|
147
|
+
}
|
|
148
|
+
setLineWidth(width) {
|
|
149
|
+
this.page.pushOperators(setLineWidth(width));
|
|
150
|
+
}
|
|
151
|
+
setDrawColor(r, g, b) {
|
|
152
|
+
this.page.pushOperators(setStrokingRgbColor(r / 255, g / 255, b / 255));
|
|
153
|
+
}
|
|
154
|
+
setFillColor(r, g, b) {
|
|
155
|
+
this.page.pushOperators(setFillingRgbColor(r / 255, g / 255, b / 255));
|
|
156
|
+
}
|
|
157
|
+
setTextColor(r, g, b) {
|
|
158
|
+
const color = rgb(r / 255, g / 255, b / 255);
|
|
159
|
+
this.currentTextColor = color;
|
|
160
|
+
if (typeof this.page.setFontColor === 'function') {
|
|
161
|
+
this.page.setFontColor(color);
|
|
162
|
+
}
|
|
163
|
+
this.page.pushOperators(setFillingRgbColor(r / 255, g / 255, b / 255));
|
|
164
|
+
}
|
|
165
|
+
setLineCap(cap) {
|
|
166
|
+
let style = LineCapStyle.Butt;
|
|
167
|
+
if (cap === 'round' || cap === 1)
|
|
168
|
+
style = LineCapStyle.Round;
|
|
169
|
+
if (cap === 'projecting' || cap === 2)
|
|
170
|
+
style = LineCapStyle.Projecting;
|
|
171
|
+
this.page.pushOperators(setLineCap(style));
|
|
172
|
+
}
|
|
173
|
+
setLineJoin(join) {
|
|
174
|
+
let style = LineJoinStyle.Miter;
|
|
175
|
+
if (join === 'round' || join === 1)
|
|
176
|
+
style = LineJoinStyle.Round;
|
|
177
|
+
if (join === 'bevel' || join === 2)
|
|
178
|
+
style = LineJoinStyle.Bevel;
|
|
179
|
+
this.page.pushOperators(setLineJoin(style));
|
|
180
|
+
}
|
|
181
|
+
setLineDashPattern(dash, offset) {
|
|
182
|
+
this.page.pushOperators(setDashPattern(dash, offset));
|
|
183
|
+
}
|
|
184
|
+
setLineMiterLimit(limit) {
|
|
185
|
+
// @ts-ignore
|
|
186
|
+
this.page.pushOperators(PDFOperator.of('M', [this.doc.context.obj(limit)]));
|
|
187
|
+
}
|
|
188
|
+
setGState(gstate) {
|
|
189
|
+
const options = {};
|
|
190
|
+
if (gstate.opacity !== undefined) {
|
|
191
|
+
options.ca = gstate.opacity;
|
|
192
|
+
}
|
|
193
|
+
if (gstate['stroke-opacity'] !== undefined) {
|
|
194
|
+
options.CA = gstate['stroke-opacity'];
|
|
195
|
+
}
|
|
196
|
+
if (Object.keys(options).length > 0) {
|
|
197
|
+
const gs = this.doc.context.obj({
|
|
198
|
+
Type: 'ExtGState',
|
|
199
|
+
...options
|
|
200
|
+
});
|
|
201
|
+
const key = this.page.node.newExtGState('GS', gs);
|
|
202
|
+
this.page.pushOperators(setGraphicsState(key));
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
setFontSize(size) {
|
|
206
|
+
this.fontSize = size;
|
|
207
|
+
this.page.setFontSize(size);
|
|
208
|
+
}
|
|
209
|
+
getFontSize() {
|
|
210
|
+
return this.fontSize;
|
|
211
|
+
}
|
|
212
|
+
setFont(fontName, fontStyle) {
|
|
213
|
+
let standardFont = StandardFonts.Helvetica;
|
|
214
|
+
const lowerName = fontName.toLowerCase();
|
|
215
|
+
if (lowerName.includes('times'))
|
|
216
|
+
standardFont = StandardFonts.TimesRoman;
|
|
217
|
+
if (lowerName.includes('courier'))
|
|
218
|
+
standardFont = StandardFonts.Courier;
|
|
219
|
+
if (fontStyle) {
|
|
220
|
+
const isBold = fontStyle.includes('bold');
|
|
221
|
+
const isItalic = fontStyle.includes('italic') || fontStyle.includes('oblique');
|
|
222
|
+
if (standardFont === StandardFonts.Helvetica) {
|
|
223
|
+
if (isBold && isItalic)
|
|
224
|
+
standardFont = StandardFonts.HelveticaBoldOblique;
|
|
225
|
+
else if (isBold)
|
|
226
|
+
standardFont = StandardFonts.HelveticaBold;
|
|
227
|
+
else if (isItalic)
|
|
228
|
+
standardFont = StandardFonts.HelveticaOblique;
|
|
229
|
+
}
|
|
230
|
+
else if (standardFont === StandardFonts.TimesRoman) {
|
|
231
|
+
if (isBold && isItalic)
|
|
232
|
+
standardFont = StandardFonts.TimesRomanBoldItalic;
|
|
233
|
+
else if (isBold)
|
|
234
|
+
standardFont = StandardFonts.TimesRomanBold;
|
|
235
|
+
else if (isItalic)
|
|
236
|
+
standardFont = StandardFonts.TimesRomanItalic;
|
|
237
|
+
}
|
|
238
|
+
else if (standardFont === StandardFonts.Courier) {
|
|
239
|
+
if (isBold && isItalic)
|
|
240
|
+
standardFont = StandardFonts.CourierBoldOblique;
|
|
241
|
+
else if (isBold)
|
|
242
|
+
standardFont = StandardFonts.CourierBold;
|
|
243
|
+
else if (isItalic)
|
|
244
|
+
standardFont = StandardFonts.CourierOblique;
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
try {
|
|
248
|
+
const font = this.doc.embedStandardFont(standardFont);
|
|
249
|
+
this.page.setFont(font);
|
|
250
|
+
this.currentFont = font;
|
|
251
|
+
}
|
|
252
|
+
catch (e) {
|
|
253
|
+
// ignore
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
getFontList() {
|
|
257
|
+
return {
|
|
258
|
+
'helvetica': ['normal', 'bold', 'italic', 'bolditalic'],
|
|
259
|
+
'times': ['normal', 'bold', 'italic', 'bolditalic'],
|
|
260
|
+
'courier': ['normal', 'bold', 'italic', 'bolditalic']
|
|
261
|
+
};
|
|
262
|
+
}
|
|
263
|
+
moveTo(x, y) {
|
|
264
|
+
this.page.pushOperators(moveTo(x, this.internal.pageSize.getHeight() - y));
|
|
265
|
+
}
|
|
266
|
+
lineTo(x, y) {
|
|
267
|
+
this.page.pushOperators(lineTo(x, this.internal.pageSize.getHeight() - y));
|
|
268
|
+
}
|
|
269
|
+
curveTo(x1, y1, x2, y2, x, y) {
|
|
270
|
+
const h = this.internal.pageSize.getHeight();
|
|
271
|
+
this.page.pushOperators(appendBezierCurve(x1, h - y1, x2, h - y2, x, h - y));
|
|
272
|
+
}
|
|
273
|
+
close() {
|
|
274
|
+
this.page.pushOperators(closePath());
|
|
275
|
+
}
|
|
276
|
+
registerPattern(id, patternRef) {
|
|
277
|
+
const resources = this.page.node.Resources();
|
|
278
|
+
let patterns = resources.get(PDFName.of('Pattern'));
|
|
279
|
+
if (!patterns) {
|
|
280
|
+
patterns = this.doc.context.obj({});
|
|
281
|
+
resources.set(PDFName.of('Pattern'), patterns);
|
|
282
|
+
}
|
|
283
|
+
patterns.set(PDFName.of(id), patternRef);
|
|
284
|
+
}
|
|
285
|
+
beginFormObject(x, y, w, h, matrix) {
|
|
286
|
+
this.savedPage = this.page;
|
|
287
|
+
const stream = PDFContentStream.of(this.doc.context.obj({}), []);
|
|
288
|
+
this.activeFormObject = { key: '', stream, bBox: [x, y, w, h], matrix, savedMatrix: this.currentMatrix };
|
|
289
|
+
this.currentMatrix = unitMatrix;
|
|
290
|
+
const mockPage = {
|
|
291
|
+
pushOperators: (...ops) => stream.push(...ops),
|
|
292
|
+
getHeight: () => h,
|
|
293
|
+
getWidth: () => w,
|
|
294
|
+
setFontSize: (size) => {
|
|
295
|
+
this.fontSize = size;
|
|
296
|
+
if (this.savedPage && typeof this.savedPage.setFontSize === 'function') {
|
|
297
|
+
;
|
|
298
|
+
this.savedPage.setFontSize(size);
|
|
299
|
+
}
|
|
300
|
+
},
|
|
301
|
+
setFont: (font) => {
|
|
302
|
+
if (this.savedPage && typeof this.savedPage.setFont === 'function') {
|
|
303
|
+
;
|
|
304
|
+
this.savedPage.setFont(font);
|
|
305
|
+
}
|
|
306
|
+
},
|
|
307
|
+
drawText: (text, options) => {
|
|
308
|
+
if (!this.currentFont)
|
|
309
|
+
return;
|
|
310
|
+
const size = options.size || this.fontSize;
|
|
311
|
+
const encoded = this.currentFont.encodeText(text);
|
|
312
|
+
stream.push(pushGraphicsState());
|
|
313
|
+
if (options.angle instanceof Matrix) {
|
|
314
|
+
const m = options.angle;
|
|
315
|
+
const converted = this.convertMatrix(m);
|
|
316
|
+
stream.push(concatTransformationMatrix(converted.a, converted.b, converted.c, converted.d, converted.e, converted.f));
|
|
317
|
+
}
|
|
318
|
+
stream.push(PDFOperator.of('BT'));
|
|
319
|
+
stream.push(PDFOperator.of('Tf', [this.currentFont.name, PDFNumber.of(size)]));
|
|
320
|
+
const tx = options.x || 0;
|
|
321
|
+
const ty = h - (options.y || 0);
|
|
322
|
+
stream.push(PDFOperator.of('Td', [PDFNumber.of(tx), PDFNumber.of(ty)]));
|
|
323
|
+
stream.push(PDFOperator.of('Tj', [encoded]));
|
|
324
|
+
stream.push(PDFOperator.of('ET'));
|
|
325
|
+
stream.push(popGraphicsState());
|
|
326
|
+
},
|
|
327
|
+
node: {
|
|
328
|
+
newExtGState: (name, gs) => this.savedPage.node.newExtGState(name, gs),
|
|
329
|
+
newXObject: (name, ref) => this.savedPage.node.newXObject(name, ref),
|
|
330
|
+
Resources: () => this.savedPage.node.Resources()
|
|
331
|
+
}
|
|
332
|
+
};
|
|
333
|
+
this.page = mockPage;
|
|
334
|
+
}
|
|
335
|
+
endFormObject(key) {
|
|
336
|
+
if (!this.activeFormObject)
|
|
337
|
+
return;
|
|
338
|
+
const { stream, bBox, matrix, savedMatrix } = this.activeFormObject;
|
|
339
|
+
const normalizedKey = this.normalizeReferenceKey(key);
|
|
340
|
+
const formDict = stream.dict;
|
|
341
|
+
formDict.set(PDFName.of('Type'), PDFName.of('XObject'));
|
|
342
|
+
formDict.set(PDFName.of('Subtype'), PDFName.of('Form'));
|
|
343
|
+
formDict.set(PDFName.of('BBox'), this.doc.context.obj([bBox[0], -bBox[1], bBox[0] + bBox[2], bBox[3] - bBox[1]]));
|
|
344
|
+
const formHeight = bBox[3];
|
|
345
|
+
const pageHeight = this.savedPage ? this.savedPage.getHeight() : this.internal.pageSize.getHeight();
|
|
346
|
+
const convertedMatrix = this.convertMatrixWithHeight(matrix, formHeight);
|
|
347
|
+
const correction = new Matrix(1, 0, 0, 1, 0, pageHeight - formHeight);
|
|
348
|
+
const formMatrix = matrixMult(correction, convertedMatrix);
|
|
349
|
+
formDict.set(PDFName.of('Matrix'), this.doc.context.obj([
|
|
350
|
+
formMatrix.a,
|
|
351
|
+
formMatrix.b,
|
|
352
|
+
formMatrix.c,
|
|
353
|
+
formMatrix.d,
|
|
354
|
+
formMatrix.e,
|
|
355
|
+
formMatrix.f
|
|
356
|
+
]));
|
|
357
|
+
formDict.set(PDFName.of('Resources'), this.savedPage.node.Resources());
|
|
358
|
+
const formRef = this.doc.context.register(stream);
|
|
359
|
+
this.formObjects[normalizedKey] = { ref: formRef, bBox };
|
|
360
|
+
this.page = this.savedPage;
|
|
361
|
+
this.currentMatrix = savedMatrix;
|
|
362
|
+
this.internal.pageSize.getWidth = () => this.page.getWidth();
|
|
363
|
+
this.internal.pageSize.getHeight = () => this.page.getHeight();
|
|
364
|
+
this.activeFormObject = null;
|
|
365
|
+
this.savedPage = null;
|
|
366
|
+
}
|
|
367
|
+
doFormObject(key, matrix) {
|
|
368
|
+
const normalizedKey = this.normalizeReferenceKey(key);
|
|
369
|
+
const form = this.formObjects[normalizedKey];
|
|
370
|
+
if (!form)
|
|
371
|
+
return;
|
|
372
|
+
const nameStr = `Xo${++this.formObjectCounter}`;
|
|
373
|
+
const name = this.page.node.newXObject(nameStr, form.ref);
|
|
374
|
+
this.page.pushOperators(pushGraphicsState());
|
|
375
|
+
const converted = this.convertMatrix(matrix);
|
|
376
|
+
this.page.pushOperators(concatTransformationMatrix(converted.a, converted.b, converted.c, converted.d, converted.e, converted.f));
|
|
377
|
+
this.page.pushOperators(drawObject(name));
|
|
378
|
+
this.page.pushOperators(popGraphicsState());
|
|
379
|
+
}
|
|
380
|
+
registerShading(name, shadingRef) {
|
|
381
|
+
const resources = this.page.node.Resources();
|
|
382
|
+
let shadings = resources.get(PDFName.of('Shading'));
|
|
383
|
+
if (!shadings) {
|
|
384
|
+
shadings = this.doc.context.obj({});
|
|
385
|
+
resources.set(PDFName.of('Shading'), shadings);
|
|
386
|
+
}
|
|
387
|
+
;
|
|
388
|
+
shadings.set(PDFName.of(name), shadingRef);
|
|
389
|
+
}
|
|
390
|
+
buildSampledFunction(colors, numberSamples = 21) {
|
|
391
|
+
const count = Math.max(2, numberSamples);
|
|
392
|
+
const samples = this.interpolateSampledColors(colors, count);
|
|
393
|
+
const funcDict = this.doc.context.obj({
|
|
394
|
+
FunctionType: 0,
|
|
395
|
+
Domain: [0, 1],
|
|
396
|
+
Size: [count],
|
|
397
|
+
BitsPerSample: 8,
|
|
398
|
+
Range: [0, 1, 0, 1, 0, 1],
|
|
399
|
+
Decode: [0, 1, 0, 1, 0, 1]
|
|
400
|
+
});
|
|
401
|
+
const funcStream = PDFRawStream.of(funcDict, samples);
|
|
402
|
+
return this.doc.context.register(funcStream);
|
|
403
|
+
}
|
|
404
|
+
interpolateSampledColors(colors, numberSamples) {
|
|
405
|
+
const count = Math.max(2, numberSamples);
|
|
406
|
+
const stops = colors.map(stop => ({
|
|
407
|
+
offset: Number(stop.offset) || 0,
|
|
408
|
+
color: Array.isArray(stop.color) ? stop.color : [0, 0, 0]
|
|
409
|
+
}));
|
|
410
|
+
if (stops.length === 0) {
|
|
411
|
+
return new Uint8Array(count * 3);
|
|
412
|
+
}
|
|
413
|
+
if (stops[0].offset !== 0) {
|
|
414
|
+
stops.unshift({ ...stops[0], offset: 0 });
|
|
415
|
+
}
|
|
416
|
+
if (stops[stops.length - 1].offset !== 1) {
|
|
417
|
+
stops.push({ ...stops[stops.length - 1], offset: 1 });
|
|
418
|
+
}
|
|
419
|
+
const samples = new Uint8Array(count * 3);
|
|
420
|
+
let index = 0;
|
|
421
|
+
for (let i = 0; i < count; i++) {
|
|
422
|
+
const t = i / (count - 1);
|
|
423
|
+
while (index < stops.length - 2 && t > stops[index + 1].offset) {
|
|
424
|
+
index++;
|
|
425
|
+
}
|
|
426
|
+
const a = stops[index];
|
|
427
|
+
const b = stops[index + 1];
|
|
428
|
+
const denom = b.offset - a.offset;
|
|
429
|
+
const d = denom !== 0 ? (t - a.offset) / denom : 0;
|
|
430
|
+
for (let c = 0; c < 3; c++) {
|
|
431
|
+
const aColor = Number(a.color[c]) || 0;
|
|
432
|
+
const bColor = Number(b.color[c]) || 0;
|
|
433
|
+
const value = Math.round((1 - d) * aColor + d * bColor);
|
|
434
|
+
samples[i * 3 + c] = Math.max(0, Math.min(255, value));
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
return samples;
|
|
438
|
+
}
|
|
439
|
+
addShadingPattern(id, pattern) {
|
|
440
|
+
const key = this.normalizeReferenceKey(id);
|
|
441
|
+
if (this.shadingPatterns[key])
|
|
442
|
+
return;
|
|
443
|
+
const shadingType = pattern.type === 'axial' ? 2 : 3;
|
|
444
|
+
const funcRef = this.buildSampledFunction(pattern.colors);
|
|
445
|
+
const coords = shadingType === 2
|
|
446
|
+
? [pattern.coords[0], pattern.coords[1], pattern.coords[2], pattern.coords[3]]
|
|
447
|
+
: [
|
|
448
|
+
pattern.coords[0],
|
|
449
|
+
pattern.coords[1],
|
|
450
|
+
pattern.coords[2],
|
|
451
|
+
pattern.coords[3],
|
|
452
|
+
pattern.coords[4],
|
|
453
|
+
pattern.coords[5]
|
|
454
|
+
];
|
|
455
|
+
const shadingDict = this.doc.context.obj({
|
|
456
|
+
ShadingType: shadingType,
|
|
457
|
+
ColorSpace: 'DeviceRGB',
|
|
458
|
+
Coords: coords,
|
|
459
|
+
Function: funcRef,
|
|
460
|
+
Extend: [true, true]
|
|
461
|
+
});
|
|
462
|
+
if (pattern.matrix) {
|
|
463
|
+
const converted = this.convertMatrixForPattern(pattern.matrix);
|
|
464
|
+
shadingDict.set(PDFName.of('Matrix'), this.doc.context.obj([
|
|
465
|
+
converted.a,
|
|
466
|
+
converted.b,
|
|
467
|
+
converted.c,
|
|
468
|
+
converted.d,
|
|
469
|
+
converted.e,
|
|
470
|
+
converted.f
|
|
471
|
+
]));
|
|
472
|
+
}
|
|
473
|
+
const shadingRef = this.doc.context.register(shadingDict);
|
|
474
|
+
const name = `Sh${++this.patternCounter}`;
|
|
475
|
+
this.shadingPatterns[key] = { name, pattern, ref: shadingRef };
|
|
476
|
+
}
|
|
477
|
+
applyShadingPattern(key, data, evenOdd, strokePath) {
|
|
478
|
+
const shading = this.shadingPatterns[key];
|
|
479
|
+
if (!shading)
|
|
480
|
+
return false;
|
|
481
|
+
this.registerShading(shading.name, shading.ref);
|
|
482
|
+
this.page.pushOperators(pushGraphicsState());
|
|
483
|
+
if (evenOdd) {
|
|
484
|
+
this.page.pushOperators(clipEvenOdd());
|
|
485
|
+
}
|
|
486
|
+
else {
|
|
487
|
+
this.page.pushOperators(clip());
|
|
488
|
+
}
|
|
489
|
+
if (strokePath) {
|
|
490
|
+
this.page.pushOperators(stroke());
|
|
491
|
+
}
|
|
492
|
+
else {
|
|
493
|
+
this.page.pushOperators(endPath());
|
|
494
|
+
}
|
|
495
|
+
if (shading.pattern.gState) {
|
|
496
|
+
this.setGState(shading.pattern.gState);
|
|
497
|
+
}
|
|
498
|
+
const matrix = data.matrix || unitMatrix;
|
|
499
|
+
const converted = this.convertMatrixForPattern(matrix);
|
|
500
|
+
this.page.pushOperators(concatTransformationMatrix(converted.a, converted.b, converted.c, converted.d, converted.e, converted.f), PDFOperator.of('sh', [PDFName.of(shading.name)]));
|
|
501
|
+
this.page.pushOperators(popGraphicsState());
|
|
502
|
+
return true;
|
|
503
|
+
}
|
|
504
|
+
applyTilingPattern(key, data, operator) {
|
|
505
|
+
const base = this.tilingPatterns[key];
|
|
506
|
+
if (!base)
|
|
507
|
+
return false;
|
|
508
|
+
const dict = this.doc.context.obj({});
|
|
509
|
+
const boundingBox = data.boundingBox || base.pattern.boundingBox;
|
|
510
|
+
const xStep = data.xStep || base.pattern.xStep;
|
|
511
|
+
const yStep = data.yStep || base.pattern.yStep;
|
|
512
|
+
dict.set(PDFName.of('Type'), PDFName.of('Pattern'));
|
|
513
|
+
dict.set(PDFName.of('PatternType'), PDFNumber.of(1));
|
|
514
|
+
dict.set(PDFName.of('PaintType'), PDFNumber.of(1));
|
|
515
|
+
dict.set(PDFName.of('TilingType'), PDFNumber.of(1));
|
|
516
|
+
dict.set(PDFName.of('BBox'), this.doc.context.obj(boundingBox));
|
|
517
|
+
dict.set(PDFName.of('XStep'), PDFNumber.of(xStep));
|
|
518
|
+
dict.set(PDFName.of('YStep'), PDFNumber.of(yStep));
|
|
519
|
+
const baseResources = base.stream.dict.get(PDFName.of('Resources'));
|
|
520
|
+
dict.set(PDFName.of('Resources'), baseResources || this.page.node.Resources());
|
|
521
|
+
const matrix = data.matrix || unitMatrix;
|
|
522
|
+
const converted = this.convertMatrix(matrix);
|
|
523
|
+
dict.set(PDFName.of('Matrix'), this.doc.context.obj([converted.a, converted.b, converted.c, converted.d, converted.e, converted.f]));
|
|
524
|
+
const patternStream = PDFRawStream.of(dict, base.stream.getUnencodedContents());
|
|
525
|
+
const patternRef = this.doc.context.register(patternStream);
|
|
526
|
+
const patternName = `P${++this.patternCounter}`;
|
|
527
|
+
this.registerPattern(patternName, patternRef);
|
|
528
|
+
this.page.pushOperators(pushGraphicsState());
|
|
529
|
+
this.page.pushOperators(PDFOperator.of('cs', [PDFName.of('Pattern')]), PDFOperator.of('scn', [PDFName.of(patternName)]));
|
|
530
|
+
if (base.pattern.gState) {
|
|
531
|
+
this.setGState(base.pattern.gState);
|
|
532
|
+
}
|
|
533
|
+
this.page.pushOperators(operator);
|
|
534
|
+
this.page.pushOperators(popGraphicsState());
|
|
535
|
+
return true;
|
|
536
|
+
}
|
|
537
|
+
applyPatternFill(data, operator, evenOdd, strokePath) {
|
|
538
|
+
if (!data || typeof data !== 'object' || !data.key)
|
|
539
|
+
return false;
|
|
540
|
+
const key = this.normalizeReferenceKey(data.key);
|
|
541
|
+
if (this.shadingPatterns[key]) {
|
|
542
|
+
return this.applyShadingPattern(key, data, evenOdd, strokePath);
|
|
543
|
+
}
|
|
544
|
+
if (this.tilingPatterns[key]) {
|
|
545
|
+
return this.applyTilingPattern(key, data, operator);
|
|
546
|
+
}
|
|
547
|
+
return false;
|
|
548
|
+
}
|
|
549
|
+
fill(data) {
|
|
550
|
+
if (this.applyPatternFill(data, fill(), false, false))
|
|
551
|
+
return;
|
|
552
|
+
if (data && typeof data === 'string' && data.startsWith('PT')) {
|
|
553
|
+
this.page.pushOperators(PDFOperator.of('scn', [PDFName.of(data)]));
|
|
554
|
+
}
|
|
555
|
+
this.page.pushOperators(fill());
|
|
556
|
+
}
|
|
557
|
+
fillEvenOdd(data) {
|
|
558
|
+
if (this.applyPatternFill(data, fillEvenOdd(), true, false))
|
|
559
|
+
return;
|
|
560
|
+
this.page.pushOperators(fillEvenOdd());
|
|
561
|
+
}
|
|
562
|
+
stroke(data) {
|
|
563
|
+
this.page.pushOperators(stroke());
|
|
564
|
+
}
|
|
565
|
+
fillStroke(data) {
|
|
566
|
+
if (this.applyPatternFill(data, fillAndStroke(), false, true))
|
|
567
|
+
return;
|
|
568
|
+
this.page.pushOperators(fillAndStroke());
|
|
569
|
+
}
|
|
570
|
+
fillStrokeEvenOdd(data) {
|
|
571
|
+
const operator = PDFOperator.of('B*');
|
|
572
|
+
if (this.applyPatternFill(data, operator, true, true))
|
|
573
|
+
return;
|
|
574
|
+
// @ts-ignore
|
|
575
|
+
this.page.pushOperators(operator);
|
|
576
|
+
}
|
|
577
|
+
discardPath() {
|
|
578
|
+
this.page.pushOperators(endPath());
|
|
579
|
+
}
|
|
580
|
+
clip(rule) {
|
|
581
|
+
if (rule === 'evenodd') {
|
|
582
|
+
this.page.pushOperators(clipEvenOdd());
|
|
583
|
+
}
|
|
584
|
+
else {
|
|
585
|
+
this.page.pushOperators(clip());
|
|
586
|
+
}
|
|
587
|
+
return this;
|
|
588
|
+
}
|
|
589
|
+
rect(x, y, w, h) {
|
|
590
|
+
const ph = this.internal.pageSize.getHeight();
|
|
591
|
+
this.page.pushOperators(moveTo(x, ph - y), lineTo(x + w, ph - y), lineTo(x + w, ph - (y + h)), lineTo(x, ph - (y + h)), closePath());
|
|
592
|
+
return this;
|
|
593
|
+
}
|
|
594
|
+
text(text, x, y, options) {
|
|
595
|
+
this.saveGraphicsState();
|
|
596
|
+
const ph = this.internal.pageSize.getHeight();
|
|
597
|
+
const drawOptions = {
|
|
598
|
+
x,
|
|
599
|
+
y: ph - y,
|
|
600
|
+
size: this.getFontSize(),
|
|
601
|
+
...options
|
|
602
|
+
};
|
|
603
|
+
if (options && options.angle instanceof Matrix) {
|
|
604
|
+
this.setCurrentTransformationMatrix(options.angle);
|
|
605
|
+
delete drawOptions.angle;
|
|
606
|
+
}
|
|
607
|
+
if (options && options.charSpace !== undefined) {
|
|
608
|
+
// @ts-ignore
|
|
609
|
+
this.page.pushOperators(PDFOperator.of('Tc', [this.doc.context.obj(options.charSpace)]));
|
|
610
|
+
}
|
|
611
|
+
if (options && options.renderingMode !== undefined) {
|
|
612
|
+
const modeMap = {
|
|
613
|
+
fill: 0,
|
|
614
|
+
stroke: 1,
|
|
615
|
+
fillThenStroke: 2,
|
|
616
|
+
invisible: 3,
|
|
617
|
+
fillStroke: 2
|
|
618
|
+
};
|
|
619
|
+
const mode = modeMap[options.renderingMode] !== undefined ? modeMap[options.renderingMode] : 0;
|
|
620
|
+
// @ts-ignore
|
|
621
|
+
this.page.pushOperators(PDFOperator.of('Tr', [this.doc.context.obj(mode)]));
|
|
622
|
+
}
|
|
623
|
+
this.page.drawText(text, drawOptions);
|
|
624
|
+
if (options && options.charSpace !== undefined) {
|
|
625
|
+
// @ts-ignore
|
|
626
|
+
this.page.pushOperators(PDFOperator.of('Tc', [this.doc.context.obj(0)]));
|
|
627
|
+
}
|
|
628
|
+
this.restoreGraphicsState();
|
|
629
|
+
}
|
|
630
|
+
async addImage(img, type, x, y, w, h) {
|
|
631
|
+
try {
|
|
632
|
+
const image = await (img.startsWith('data:image/png')
|
|
633
|
+
? this.doc.embedPng(img)
|
|
634
|
+
: this.doc.embedJpg(img));
|
|
635
|
+
this.page.drawImage(image, {
|
|
636
|
+
x,
|
|
637
|
+
y: this.internal.pageSize.getHeight() - y - h,
|
|
638
|
+
width: w,
|
|
639
|
+
height: h
|
|
640
|
+
});
|
|
641
|
+
}
|
|
642
|
+
catch (e) {
|
|
643
|
+
console.warn('Failed to add image to PDF', e);
|
|
644
|
+
}
|
|
645
|
+
}
|
|
646
|
+
link(x, y, w, h, options) {
|
|
647
|
+
try {
|
|
648
|
+
const ph = this.internal.pageSize.getHeight();
|
|
649
|
+
const rect = [x, ph - y - h, x + w, ph - y];
|
|
650
|
+
const annot = this.doc.context.obj({
|
|
651
|
+
Type: 'Annot',
|
|
652
|
+
Subtype: 'Link',
|
|
653
|
+
Rect: rect,
|
|
654
|
+
Border: [0, 0, 0],
|
|
655
|
+
A: {
|
|
656
|
+
Type: 'Action',
|
|
657
|
+
S: 'URI',
|
|
658
|
+
URI: options.url
|
|
659
|
+
}
|
|
660
|
+
});
|
|
661
|
+
const annotRef = this.doc.context.register(annot);
|
|
662
|
+
const annots = this.page.node.Annots() || this.doc.context.obj([]);
|
|
663
|
+
annots.push(annotRef);
|
|
664
|
+
this.page.node.set(PDFName.of('Annots'), annots);
|
|
665
|
+
}
|
|
666
|
+
catch (e) {
|
|
667
|
+
console.warn('Failed to add link to PDF', e);
|
|
668
|
+
}
|
|
669
|
+
}
|
|
670
|
+
beginTilingPattern(pattern) {
|
|
671
|
+
this.savedPage = this.page;
|
|
672
|
+
const stream = PDFContentStream.of(this.doc.context.obj({}), []);
|
|
673
|
+
this.activeFormObject = { key: '', stream, bBox: pattern.boundingBox, matrix: unitMatrix, savedMatrix: this.currentMatrix };
|
|
674
|
+
this.currentMatrix = unitMatrix;
|
|
675
|
+
const h = pattern.boundingBox[3];
|
|
676
|
+
const mockPage = {
|
|
677
|
+
pushOperators: (...ops) => stream.push(...ops),
|
|
678
|
+
getHeight: () => pattern.boundingBox[3],
|
|
679
|
+
getWidth: () => pattern.boundingBox[2],
|
|
680
|
+
setFontSize: (size) => this.savedPage.setFontSize(size),
|
|
681
|
+
setFont: (font) => this.savedPage.setFont(font),
|
|
682
|
+
drawText: (text, options) => {
|
|
683
|
+
if (!this.currentFont)
|
|
684
|
+
return;
|
|
685
|
+
const size = options.size || this.fontSize;
|
|
686
|
+
const encoded = this.currentFont.encodeText(text);
|
|
687
|
+
stream.push(pushGraphicsState());
|
|
688
|
+
if (options.angle instanceof Matrix) {
|
|
689
|
+
const m = options.angle;
|
|
690
|
+
const converted = this.convertMatrix(m);
|
|
691
|
+
stream.push(concatTransformationMatrix(converted.a, converted.b, converted.c, converted.d, converted.e, converted.f));
|
|
692
|
+
}
|
|
693
|
+
stream.push(PDFOperator.of('BT'));
|
|
694
|
+
stream.push(PDFOperator.of('Tf', [this.currentFont.name, PDFNumber.of(size)]));
|
|
695
|
+
const tx = options.x || 0;
|
|
696
|
+
const ty = h - (options.y || 0);
|
|
697
|
+
stream.push(PDFOperator.of('Td', [PDFNumber.of(tx), PDFNumber.of(ty)]));
|
|
698
|
+
stream.push(PDFOperator.of('Tj', [encoded]));
|
|
699
|
+
stream.push(PDFOperator.of('ET'));
|
|
700
|
+
stream.push(popGraphicsState());
|
|
701
|
+
},
|
|
702
|
+
node: {
|
|
703
|
+
newExtGState: (name, gs) => this.savedPage.node.newExtGState(name, gs),
|
|
704
|
+
newXObject: (name, ref) => this.savedPage.node.newXObject(name, ref),
|
|
705
|
+
Resources: () => this.savedPage.node.Resources()
|
|
706
|
+
}
|
|
707
|
+
};
|
|
708
|
+
this.page = mockPage;
|
|
709
|
+
}
|
|
710
|
+
endTilingPattern(id, pattern) {
|
|
711
|
+
if (!this.activeFormObject)
|
|
712
|
+
return;
|
|
713
|
+
const { stream, bBox, savedMatrix } = this.activeFormObject;
|
|
714
|
+
const normalizedKey = this.normalizeReferenceKey(id);
|
|
715
|
+
const patternDict = stream.dict;
|
|
716
|
+
patternDict.set(PDFName.of('Type'), PDFName.of('Pattern'));
|
|
717
|
+
patternDict.set(PDFName.of('PatternType'), PDFNumber.of(1));
|
|
718
|
+
patternDict.set(PDFName.of('PaintType'), PDFNumber.of(1));
|
|
719
|
+
patternDict.set(PDFName.of('TilingType'), PDFNumber.of(1));
|
|
720
|
+
patternDict.set(PDFName.of('BBox'), this.doc.context.obj([bBox[0], -bBox[1], bBox[0] + bBox[2], bBox[3] - bBox[1]]));
|
|
721
|
+
patternDict.set(PDFName.of('XStep'), PDFNumber.of(pattern.xStep));
|
|
722
|
+
patternDict.set(PDFName.of('YStep'), PDFNumber.of(pattern.yStep));
|
|
723
|
+
patternDict.set(PDFName.of('Resources'), this.savedPage.node.Resources());
|
|
724
|
+
if (pattern.matrix) {
|
|
725
|
+
const converted = this.convertMatrixForPattern(pattern.matrix);
|
|
726
|
+
patternDict.set(PDFName.of('Matrix'), this.doc.context.obj([
|
|
727
|
+
converted.a,
|
|
728
|
+
converted.b,
|
|
729
|
+
converted.c,
|
|
730
|
+
converted.d,
|
|
731
|
+
converted.e,
|
|
732
|
+
converted.f
|
|
733
|
+
]));
|
|
734
|
+
}
|
|
735
|
+
this.tilingPatterns[normalizedKey] = { pattern, stream, cloneIndex: 0 };
|
|
736
|
+
this.page = this.savedPage;
|
|
737
|
+
this.currentMatrix = savedMatrix;
|
|
738
|
+
this.activeFormObject = null;
|
|
739
|
+
this.savedPage = null;
|
|
740
|
+
}
|
|
741
|
+
}
|
|
742
|
+
jsPDF.version = '2.5.1';
|
|
743
|
+
jsPDF.API = {};
|
|
744
|
+
//# sourceMappingURL=jspdf-shim.js.map
|