Skip to content

Commit be0ae5d

Browse files
committed
fix exported classes
- correctly export GState, Shading/TilingPattern - Matrix can't be exported because it depends on document scope - export Acroform classes in Typings
1 parent 620ac9c commit be0ae5d

File tree

5 files changed

+212
-143
lines changed

5 files changed

+212
-143
lines changed

src/jspdf.js

Lines changed: 115 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
/* eslint-disable no-console */
22

3+
// @if MODULE_FORMAT!='cjs'
4+
import { saveAs } from "./libs/FileSaver.js";
5+
// @endif
36
import { globalObject } from "./libs/globalObject.js";
47
import { RGBColor } from "./libs/rgbcolor.js";
58
import { btoa } from "./libs/AtobBtoa.js";
6-
// @if MODULE_FORMAT!='cjs'
7-
import { saveAs } from "./libs/FileSaver.js";
89
import { console } from "./libs/console.js";
9-
// @endif
10+
1011
/**
1112
* jsPDF's Internal PubSub Implementation.
1213
* Backward compatible rewritten on 2014 by
@@ -84,6 +85,91 @@ function PubSub(context) {
8485
};
8586
}
8687

88+
function GState(parameters) {
89+
if (!(this instanceof GState)) {
90+
return new GState(parameters);
91+
}
92+
93+
/**
94+
* @name GState#opacity
95+
* @type {any}
96+
*/
97+
/**
98+
* @name GState#stroke-opacity
99+
* @type {any}
100+
*/
101+
var supported = "opacity,stroke-opacity".split(",");
102+
for (var p in parameters) {
103+
if (parameters.hasOwnProperty(p) && supported.indexOf(p) >= 0) {
104+
this[p] = parameters[p];
105+
}
106+
}
107+
/**
108+
* @name GState#id
109+
* @type {string}
110+
*/
111+
this.id = ""; // set by addGState()
112+
/**
113+
* @name GState#objectNumber
114+
* @type {number}
115+
*/
116+
this.objectNumber = -1; // will be set by putGState()
117+
}
118+
119+
GState.prototype.equals = function equals(other) {
120+
var ignore = "id,objectNumber,equals";
121+
var p;
122+
if (!other || typeof other !== typeof this) return false;
123+
var count = 0;
124+
for (p in this) {
125+
if (ignore.indexOf(p) >= 0) continue;
126+
if (this.hasOwnProperty(p) && !other.hasOwnProperty(p)) return false;
127+
if (this[p] !== other[p]) return false;
128+
count++;
129+
}
130+
for (p in other) {
131+
if (other.hasOwnProperty(p) && ignore.indexOf(p) < 0) count--;
132+
}
133+
return count === 0;
134+
};
135+
136+
function Pattern(gState, matrix) {
137+
this.gState = gState;
138+
this.matrix = matrix;
139+
140+
this.id = ""; // set by addPattern()
141+
this.objectNumber = -1; // will be set by putPattern()
142+
}
143+
144+
function ShadingPattern(type, coords, colors, gState, matrix) {
145+
if (!(this instanceof ShadingPattern)) {
146+
return new ShadingPattern(type, coords, colors, gState, matrix);
147+
}
148+
149+
// see putPattern() for information how they are realized
150+
this.type = type === "axial" ? 2 : 3;
151+
this.coords = coords;
152+
this.colors = colors;
153+
154+
Pattern.call(this, gState, matrix);
155+
}
156+
157+
function TilingPattern(boundingBox, xStep, yStep, gState, matrix) {
158+
if (!(this instanceof TilingPattern)) {
159+
return new TilingPattern(boundingBox, xStep, yStep, gState, matrix);
160+
}
161+
162+
this.boundingBox = boundingBox;
163+
this.xStep = xStep;
164+
this.yStep = yStep;
165+
166+
this.stream = ""; // set by endTilingPattern();
167+
168+
this.cloneIndex = 0;
169+
170+
Pattern.call(this, gState, matrix);
171+
}
172+
87173
/**
88174
* Creates new jsPDF document object instance.
89175
* @name jsPDF
@@ -1317,14 +1403,6 @@ function jsPDF(options) {
13171403
var identityMatrix = new Matrix(1, 0, 0, 1, 0, 0);
13181404
API.unitMatrix = API.identityMatrix = identityMatrix;
13191405

1320-
var Pattern = function(gState, matrix) {
1321-
this.gState = gState;
1322-
this.matrix = matrix;
1323-
1324-
this.id = ""; // set by addPattern()
1325-
this.objectNumber = -1; // will be set by putPattern()
1326-
};
1327-
13281406
/**
13291407
* Adds a new pattern for later use.
13301408
* @param {String} key The key by it can be referenced later. The keys must be unique!
@@ -1334,7 +1412,7 @@ function jsPDF(options) {
13341412
// only add it if it is not already present (the keys provided by the user must be unique!)
13351413
if (patternMap[key]) return;
13361414

1337-
var prefix = pattern instanceof API.ShadingPattern ? "Sh" : "P";
1415+
var prefix = pattern instanceof ShadingPattern ? "Sh" : "P";
13381416
var patternKey = prefix + (Object.keys(patterns).length + 1).toString(10);
13391417
pattern.id = patternKey;
13401418

@@ -1360,26 +1438,7 @@ function jsPDF(options) {
13601438
* @constructor
13611439
* @extends API.Pattern
13621440
*/
1363-
API.ShadingPattern = function ShadingPattern(
1364-
type,
1365-
coords,
1366-
colors,
1367-
gState,
1368-
matrix
1369-
) {
1370-
advancedApiModeTrap("ShadingPattern");
1371-
1372-
if (!(this instanceof ShadingPattern)) {
1373-
return new ShadingPattern(type, coords, colors, gState, matrix);
1374-
}
1375-
1376-
// see putPattern() for information how they are realized
1377-
this.type = type === "axial" ? 2 : 3;
1378-
this.coords = coords;
1379-
this.colors = colors;
1380-
1381-
Pattern.call(this, gState, matrix);
1382-
};
1441+
API.ShadingPattern = ShadingPattern;
13831442

13841443
/**
13851444
* A PDF Tiling pattern.
@@ -1395,45 +1454,7 @@ function jsPDF(options) {
13951454
* @constructor
13961455
* @extends API.Pattern
13971456
*/
1398-
API.TilingPattern = function TilingPattern(
1399-
boundingBox,
1400-
xStep,
1401-
yStep,
1402-
gState,
1403-
matrix
1404-
) {
1405-
advancedApiModeTrap("TilingPattern");
1406-
1407-
if (!(this instanceof TilingPattern)) {
1408-
return new TilingPattern(boundingBox, xStep, yStep, gState, matrix);
1409-
}
1410-
1411-
this.boundingBox = boundingBox;
1412-
this.xStep = xStep;
1413-
this.yStep = yStep;
1414-
1415-
this.stream = ""; // set by endTilingPattern();
1416-
1417-
this.cloneIndex = 0;
1418-
1419-
Pattern.call(this, gState, matrix);
1420-
};
1421-
1422-
API.TilingPattern.prototype = {
1423-
createClone: function(patternKey, boundingBox, xStep, yStep, matrix) {
1424-
var clone = new API.TilingPattern(
1425-
boundingBox || this.boundingBox,
1426-
xStep || this.xStep,
1427-
yStep || this.yStep,
1428-
this.gState,
1429-
matrix || this.matrix
1430-
);
1431-
clone.stream = this.stream;
1432-
var key = patternKey + "$$" + this.cloneIndex++ + "$$";
1433-
addPattern(key, clone);
1434-
return clone;
1435-
}
1436-
};
1457+
API.TilingPattern = TilingPattern;
14371458

14381459
/**
14391460
* Adds a new {@link API.ShadingPattern} for later use. Only available in "advanced" API mode.
@@ -2145,9 +2166,9 @@ function jsPDF(options) {
21452166
var patternKey;
21462167
for (patternKey in patterns) {
21472168
if (patterns.hasOwnProperty(patternKey)) {
2148-
if (patterns[patternKey] instanceof API.ShadingPattern) {
2169+
if (patterns[patternKey] instanceof ShadingPattern) {
21492170
putShadingPattern(patterns[patternKey]);
2150-
} else if (patterns[patternKey] instanceof API.TilingPattern) {
2171+
} else if (patterns[patternKey] instanceof TilingPattern) {
21512172
putTilingPattern(patterns[patternKey], deferredResourceDictionaryIds);
21522173
}
21532174
}
@@ -2224,7 +2245,7 @@ function jsPDF(options) {
22242245
for (var patternKey in patterns) {
22252246
if (
22262247
patterns.hasOwnProperty(patternKey) &&
2227-
patterns[patternKey] instanceof API.ShadingPattern &&
2248+
patterns[patternKey] instanceof ShadingPattern &&
22282249
patterns[patternKey].objectNumber >= 0
22292250
) {
22302251
out(
@@ -4174,11 +4195,25 @@ function jsPDF(options) {
41744195
fillWithPattern(patternData, style);
41754196
};
41764197

4198+
function cloneTilingPattern(patternKey, boundingBox, xStep, yStep, matrix) {
4199+
var clone = new TilingPattern(
4200+
boundingBox || this.boundingBox,
4201+
xStep || this.xStep,
4202+
yStep || this.yStep,
4203+
this.gState,
4204+
matrix || this.matrix
4205+
);
4206+
clone.stream = this.stream;
4207+
var key = patternKey + "$$" + this.cloneIndex++ + "$$";
4208+
addPattern(key, clone);
4209+
return clone;
4210+
}
4211+
41774212
var fillWithPattern = function(patternData, style) {
41784213
var patternId = patternMap[patternData.key];
41794214
var pattern = patterns[patternId];
41804215

4181-
if (pattern instanceof API.ShadingPattern) {
4216+
if (pattern instanceof ShadingPattern) {
41824217
out("q");
41834218

41844219
out(clipRuleFromStyle(style));
@@ -4189,7 +4224,7 @@ function jsPDF(options) {
41894224
out(patternData.matrix.toString() + " cm");
41904225
out("/" + patternId + " sh");
41914226
out("Q");
4192-
} else if (pattern instanceof API.TilingPattern) {
4227+
} else if (pattern instanceof TilingPattern) {
41934228
// pdf draws patterns starting at the bottom left corner and they are not affected by the global transformation,
41944229
// so we must flip them
41954230
var matrix = new Matrix(1, 0, 0, -1, 0, getPageHeight());
@@ -4198,7 +4233,8 @@ function jsPDF(options) {
41984233
matrix = matrix.multiply(patternData.matrix || identityMatrix);
41994234
// we cannot apply a matrix to the pattern on use so we must abuse the pattern matrix and create new instances
42004235
// for each use
4201-
patternId = pattern.createClone(
4236+
patternId = cloneTilingPattern.call(
4237+
pattern,
42024238
patternData.key,
42034239
patternData.boundingBox,
42044240
patternData.xStep,
@@ -5362,53 +5398,7 @@ function jsPDF(options) {
53625398
* Supported are: opacity, stroke-opacity
53635399
* @constructor
53645400
*/
5365-
API.GState = function GState(parameters) {
5366-
if (!(this instanceof GState)) {
5367-
return new GState(parameters);
5368-
}
5369-
5370-
/**
5371-
* @name GState#opacity
5372-
* @type {any}
5373-
*/
5374-
/**
5375-
* @name GState#stroke-opacity
5376-
* @type {any}
5377-
*/
5378-
var supported = "opacity,stroke-opacity".split(",");
5379-
for (var p in parameters) {
5380-
if (parameters.hasOwnProperty(p) && supported.indexOf(p) >= 0) {
5381-
this[p] = parameters[p];
5382-
}
5383-
}
5384-
/**
5385-
* @name GState#id
5386-
* @type {string}
5387-
*/
5388-
this.id = ""; // set by addGState()
5389-
/**
5390-
* @name GState#objectNumber
5391-
* @type {number}
5392-
*/
5393-
this.objectNumber = -1; // will be set by putGState()
5394-
};
5395-
5396-
API.GState.prototype.equals = function equals(other) {
5397-
var ignore = "id,objectNumber,equals";
5398-
var p;
5399-
if (!other || typeof other !== typeof this) return false;
5400-
var count = 0;
5401-
for (p in this) {
5402-
if (ignore.indexOf(p) >= 0) continue;
5403-
if (this.hasOwnProperty(p) && !other.hasOwnProperty(p)) return false;
5404-
if (this[p] !== other[p]) return false;
5405-
count++;
5406-
}
5407-
for (p in other) {
5408-
if (other.hasOwnProperty(p) && ignore.indexOf(p) < 0) count--;
5409-
}
5410-
return count === 0;
5411-
};
5401+
API.GState = GState;
54125402

54135403
/**
54145404
* Sets a either previously added {@link GState} (via {@link addGState}) or a new {@link GState}.
@@ -6034,10 +6024,5 @@ jsPDF.API = {
60346024
*/
60356025
jsPDF.version = "0.0.0";
60366026

6037-
export var Matrix = jsPDF.API.Matrix;
6038-
export var ShadingPattern = jsPDF.API.ShadingPattern;
6039-
export var TilingPattern = jsPDF.API.TilingPattern;
6040-
export var GState = jsPDF.API.GState;
6041-
6042-
export { jsPDF };
6027+
export { jsPDF, ShadingPattern, TilingPattern, GState };
60436028
export default jsPDF;

test/deployment/globals/globals.spec.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
const jsPDF = window.jsPDF;
2-
const canvg = window.canvg;
3-
41
describe("globals are defined", () => {
2+
beforeAll(loadGlobals);
53
it("jsPDF", () => {
64
expect(jsPDF).toBeDefined();
75
});

0 commit comments

Comments
 (0)