Skip to content

Commit

Permalink
Refactor on equality check function (foliojs#1280)
Browse files Browse the repository at this point in the history
Avoidable 'if' statement
  • Loading branch information
leonho2001 committed Apr 29, 2022
1 parent 3f69586 commit 4a24d10
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 1 deletion.
Binary file added demo/spotColor.pdf
Binary file not shown.
50 changes: 50 additions & 0 deletions demo/testSpotColor.js
@@ -0,0 +1,50 @@
var PDFDocument = require('../');

var fs = require('fs');

// Create a new PDFDocument
var doc = new PDFDocument();

doc.pipe(fs.createWriteStream('spotColor.pdf'));

// Set some meta data
doc.info['Title'] = 'Test Document for Sport Color';

doc.info['Author'] = 'Nicola A.';


// Register a font name for use later
doc.registerFont('Palatino', 'fonts/PalatinoBold.ttf');

// Set the font, draw some text, and embed an image
doc
.font('Palatino')
.fontSize(18)
.text('Testing Spot Colors: MYFIRST_SPOT_COLOR, MYSECOND_SPOT_COLOR', 50, 50)
.fontSize(12)
.text('Check it out on Swatches palette', 50, 100)

// Add another page
// doc
// .addPage()
// .fontSize(25)
// .text('Here is some vector graphics...', 100, 100);

doc.addSpotColor('MYFIRST_SPOT_COLOR', 0, 50, 0, 0);
doc.addSpotColor('MYSECOND_SPOT_COLOR', 50, 0, 0, 0);


// Draw a triangle and a circle
doc
.moveTo(100, 150)
.lineTo(100, 250)
.lineTo(200, 250)
.stroke('MYFIRST_SPOT_COLOR');

doc
.moveTo(200, 250)
.lineTo(100, 150)
.lineTo(200, 150)
.lineTo(200, 250)
.fillAndStroke('MYFIRST_SPOT_COLOR', 'MYSECOND_SPOT_COLOR');
doc.end();
1 change: 1 addition & 0 deletions lib/document.js
Expand Up @@ -328,6 +328,7 @@ Please pipe the document into a Node stream.\
_finalize() {
// generate xref
const xRefOffset = this._offset;
this._putSpotColors();
this._write('xref');
this._write(`0 ${this._offsets.length + 1}`);
this._write('0000000000 65535 f ');
Expand Down
29 changes: 29 additions & 0 deletions lib/mixins/color.js
Expand Up @@ -5,6 +5,8 @@ const { PDFGradient, PDFLinearGradient, PDFRadialGradient } = Gradient;
const { PDFTilingPattern } = pattern;

export default {
spotColors: {},
spotColorsCount: 0,
initColor() {
// The opacity dictionaries
this._opacityRegistry = {};
Expand Down Expand Up @@ -38,6 +40,8 @@ export default {
color = color.map(part => part / 100);
}
return color;
} else if (this.spotColors[color]) {
return color;
}

return null;
Expand All @@ -48,6 +52,9 @@ export default {
color.apply(stroke);
return true;
// see if tiling pattern, decode & apply it it
} else if (typeof color === 'string') {
this.addContent(`/CS${this.spotColors[color].index} ${stroke ? 'CS' : 'cs'} 1 ${op}`);
return false;
} else if (Array.isArray(color) && color[0] instanceof PDFTilingPattern) {
color[0].apply(stroke, color[1]);
return true;
Expand Down Expand Up @@ -163,6 +170,28 @@ export default {

pattern(bbox, xStep, yStep, stream) {
return new PDFTilingPattern(this, bbox, xStep, yStep, stream);
},

addSpotColor: function (name, C, M, Y, K) {
this._offsets.push(this._offset);
this.spotColors[name] = {
id: this._offsets.length,
index: ++this.spotColorsCount,
name: name,
values: [C, M, Y, K],
};
},
_putSpotColors: function () {
for (var item in this.spotColors) {
let output = '';
item = this.spotColors[item];
output += `${item.id} 0 obj \n`;
output += `[/Separation /${item.name} /DeviceCMYK << /Range [0 1 0 1 0 1 0 1] /C0 [0 0 0 0] /C1 [${
item.values.map(value => value / 100.0).join(' ')
}] /FunctionType 2 /Domain [0 1] /N 1>>]`;
output += `\nendobj`;
this._write(output);
}
}
};

Expand Down
2 changes: 1 addition & 1 deletion lib/object.js
Expand Up @@ -40,7 +40,7 @@ class PDFObject {
static convert(object, encryptFn = null) {
// String literals are converted to the PDF name type
if (typeof object === 'string') {
return `/${object}`;
return /^[\/\[\(\d].*/.test(object) ? object : '/' + object;

// String objects are converted to PDF strings (UTF-16)
} else if (object instanceof String) {
Expand Down
6 changes: 6 additions & 0 deletions lib/page.js
Expand Up @@ -157,6 +157,12 @@ class PDFPage {

end() {
this.dictionary.end();
this.resources.data.ColorSpace = {};
let i = 1;
for (let item in this.document.spotColors) {
item = this.document.spotColors[item];
this.resources.data.ColorSpace[`CS${i++}`] = `${item.id} 0 R`;
}
this.resources.end();
return this.content.end();
}
Expand Down

0 comments on commit 4a24d10

Please sign in to comment.