Skip to content

Commit

Permalink
feat: small pdfkit unifications + disable linting it (#2614)
Browse files Browse the repository at this point in the history
  • Loading branch information
diegomura authored Feb 7, 2024
1 parent 00dcd58 commit 87650b0
Show file tree
Hide file tree
Showing 13 changed files with 64 additions and 38 deletions.
3 changes: 2 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
.gitignore
node_modules
lib
dist
rollup.config.js
packages/yoga/dist
packages/pdfkit
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
yoga-layout
lib
packages/pdfkit
6 changes: 4 additions & 2 deletions packages/pdfkit/src/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@ class Data {
const int = this.readUInt32();
if (int >= 0x80000000) {
return int - 0x100000000;
} else {
return int;
}
return int;
}

writeInt32(val) {
Expand All @@ -70,8 +71,9 @@ class Data {
const int = this.readUInt16();
if (int >= 0x8000) {
return int - 0x10000;
} else {
return int;
}
return int;
}

writeInt16(val) {
Expand Down
6 changes: 3 additions & 3 deletions packages/pdfkit/src/image.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ class PDFImage {

if (data[0] === 0xff && data[1] === 0xd8) {
return new JPEG(data, label);
}
if (data[0] === 0x89 && data.toString('ascii', 1, 4) === 'PNG') {
} else if (data[0] === 0x89 && data.toString('ascii', 1, 4) === 'PNG') {
return new PNG(data, label);
} else {
throw new Error('Unknown image format.');
}
throw new Error('Unknown image format.');
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/pdfkit/src/image/jpeg.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class JPEG {
// min and max values from the default, we invert the colors. See
// section 4.8.4 of the spec.
if (this.colorSpace === 'DeviceCMYK') {
this.obj.data.Decode = [1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0];
this.obj.data['Decode'] = [1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0];
}

this.obj.end(this.data);
Expand Down
6 changes: 2 additions & 4 deletions packages/pdfkit/src/mixins/color.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ export default {
color.apply(stroke);
return true;
// see if tiling pattern, decode & apply it it
}
if (Array.isArray(color) && color[0] instanceof PDFTilingPattern) {
} else if (Array.isArray(color) && color[0] instanceof PDFTilingPattern) {
color[0].apply(stroke, color[1]);
return true;
}
Expand Down Expand Up @@ -118,8 +117,7 @@ export default {
},

_doOpacity(fillOpacity, strokeOpacity) {
let dictionary;
let name;
let dictionary, name;
if (fillOpacity == null && strokeOpacity == null) {
return;
}
Expand Down
6 changes: 2 additions & 4 deletions packages/pdfkit/src/mixins/metadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ export default {
_addInfo() {
this.appendXML(`
<rdf:Description rdf:about="" xmlns:xmp="http://ns.adobe.com/xap/1.0/">
<xmp:CreateDate>${
this.info.CreationDate.toISOString().split('.')[0] + 'Z'
}</xmp:CreateDate>
<xmp:CreateDate>${this.info.CreationDate.toISOString().split('.')[0] + 'Z'}</xmp:CreateDate>
<xmp:CreatorTool>${this.info.Creator}</xmp:CreatorTool>
</rdf:Description>
`);
Expand Down Expand Up @@ -88,7 +86,7 @@ export default {
Metadata was introduced in PDF 1.4, so adding it to 1.3
will likely only take up more space.
*/
if (this.version !== 1.3) {
if (this.version != 1.3) {
this.metadataRef = this.ref({
length: this.metadata.getLength(),
Type: 'Metadata',
Expand Down
11 changes: 11 additions & 0 deletions packages/pdfkit/src/mixins/vector.js
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,17 @@ export default {

transform(m11, m12, m21, m22, dx, dy) {
// keep track of the current transformation matrix
if (
m11 === 1 &&
m12 === 0 &&
m21 === 0 &&
m22 === 1 &&
dx === 0 &&
dy === 0
) {
// Ignore identity transforms
return this;
}
const m = this._ctm;
const [m0, m1, m2, m3, m4, m5] = m;
m[0] = m0 * m11 + m2 * m12;
Expand Down
10 changes: 5 additions & 5 deletions packages/pdfkit/src/outline.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ class PDFOutline {
this.outlineData = {};

if (dest !== null) {
this.outlineData.Dest = [dest.dictionary, 'Fit'];
this.outlineData['Dest'] = [dest.dictionary, 'Fit'];
}

if (parent !== null) {
this.outlineData.Parent = parent;
this.outlineData['Parent'] = parent;
}

if (title !== null) {
this.outlineData.Title = new String(title);
this.outlineData['Title'] = new String(title);
}

this.dictionary = this.document.ref(this.outlineData);
Expand All @@ -39,8 +39,8 @@ class PDFOutline {
this.outlineData.Count = this.children.length;
}

const first = this.children[0];
const last = this.children[this.children.length - 1];
const first = this.children[0],
last = this.children[this.children.length - 1];
this.outlineData.First = first.dictionary;
this.outlineData.Last = last.dictionary;

Expand Down
32 changes: 26 additions & 6 deletions packages/pdfkit/src/page.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
/*
PDFPage - represents a single page in the PDF document
By Devon Govett
*/

const DEFAULT_MARGINS = {
top: 0,
left: 0,
bottom: 0,
right: 0
top: 72,
left: 72,
bottom: 72,
right: 72
};

const SIZES = {
Expand Down Expand Up @@ -64,7 +69,20 @@ class PDFPage {
this.size = options.size || 'letter';
this.layout = options.layout || 'portrait';
this.userUnit = options.userUnit || 1.0;
this.margins = DEFAULT_MARGINS;

// process margins
if (typeof options.margin === 'number') {
this.margins = {
top: options.margin,
left: options.margin,
bottom: options.margin,
right: options.margin
};

// default to 1 inch margins
} else {
this.margins = options.margins || DEFAULT_MARGINS;
}

// calculate page dimensions
const dimensions = Array.isArray(this.size)
Expand All @@ -89,6 +107,8 @@ class PDFPage {
Resources: this.resources,
UserUnit: this.userUnit
});

this.markings = [];
}

// Lazily create these objects
Expand Down Expand Up @@ -130,7 +150,7 @@ class PDFPage {
}

maxY() {
return this.height;
return this.height - this.margins.bottom;
}

write(chunk) {
Expand Down
2 changes: 0 additions & 2 deletions packages/pdfkit/src/pattern.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
/* eslint-disable operator-assignment */

/*
PDF tiling pattern support. Uncolored only.
*/
Expand Down
13 changes: 5 additions & 8 deletions packages/pdfkit/src/tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,10 @@ class PDFTree {

const out = ['<<'];
if (this.limits && sortedKeys.length > 1) {
const first = sortedKeys[0];
const last = sortedKeys[sortedKeys.length - 1];
const first = sortedKeys[0],
last = sortedKeys[sortedKeys.length - 1];
out.push(
` /Limits ${PDFObject.convert([
this._dataForKey(first),
this._dataForKey(last)
])}`
` /Limits ${PDFObject.convert([this._dataForKey(first), this._dataForKey(last)])}`
);
}
out.push(` /${this._keysName()} [`);
Expand All @@ -49,15 +46,15 @@ class PDFTree {
return out.join('\n');
}

_compareKeys() {
_compareKeys(/*a, b*/) {
throw new Error('Must be implemented by subclasses');
}

_keysName() {
throw new Error('Must be implemented by subclasses');
}

_dataForKey() {
_dataForKey(/*k*/) {
throw new Error('Must be implemented by subclasses');
}
}
Expand Down
4 changes: 2 additions & 2 deletions packages/stylesheet/tests/expand.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe('stylesheet expand', () => {
{ marginBottom: '1 2 3' },
{ marginHorizontal: '1 2 3' },
{ margin: '1 2 3 4 5' },
{ margin: () => console.log('function') },
{ margin: () => {} },
];
const expanded = {};

Expand Down Expand Up @@ -159,7 +159,7 @@ describe('stylesheet expand', () => {
{ paddingBottom: '1 2 3' },
{ paddingHorizontal: '1 2 3' },
{ padding: '1 2 3 4 5' },
{ padding: () => console.log('function') },
{ padding: () => {} },
];
const expanded = {};

Expand Down

0 comments on commit 87650b0

Please sign in to comment.