Skip to content

Commit 04f8761

Browse files
Fix clipping with cmyk
1 parent 9247036 commit 04f8761

3 files changed

Lines changed: 18 additions & 6 deletions

File tree

lib/write_svg.js

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,15 @@ export default function (doc, svg, x, y, options) {
627627
luminosity: 'Luminosity',
628628
};
629629

630+
const maskBackdrop = [0, 0, 0];
631+
if (doc._activeColorProfile) {
632+
let i = doc._activeColorProfile.channels - 3;
633+
while (i > 0) {
634+
maskBackdrop.push(0);
635+
i--;
636+
}
637+
}
638+
630639
function docBeginGroup(bbox) {
631640
let group = new (function PDFGroup() {})();
632641
group.name = 'G' + (doc._groupCount = (doc._groupCount || 0) + 1);
@@ -690,9 +699,12 @@ export default function (doc, svg, x, y, options) {
690699
ca: 1,
691700
BM: 'Normal',
692701
SMask: {
693-
S: 'Luminosity',
702+
// Needs to be Alpha for clipping to work with cmyk color space
703+
// Clipping mode is reproducing svg clip-path by drawing everything inside clip path with DefaultColors.white [1,1,1,1]
704+
// And everything outside (not draw, defaults to [0,0,0,0]) is masked off
705+
S: clip ? 'Alpha' : 'Luminosity',
694706
G: group.xobj,
695-
BC: clip ? [0, 0, 0] : [1, 1, 1],
707+
BC: maskBackdrop,
696708
},
697709
});
698710
gstate.end();
@@ -1056,7 +1068,7 @@ export default function (doc, svg, x, y, options) {
10561068
let newColor = color[0].slice(),
10571069
newOpacity = color[1] * opacity;
10581070
if (isMask) {
1059-
for (let i = 0; i < color.length; i++) {
1071+
for (let i = 0; i < newColor.length; i++) {
10601072
newColor[i] *= newOpacity;
10611073
}
10621074
return [newColor, 1];
@@ -3325,7 +3337,7 @@ export default function (doc, svg, x, y, options) {
33253337
this.drawChildren(false, true);
33263338
doc.restore();
33273339
docEndGroup(group);
3328-
docApplyMask(group, true);
3340+
docApplyMask(group, false);
33293341
};
33303342
};
33313343

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"document",
1010
"vector"
1111
],
12-
"version": "0.17.5",
12+
"version": "0.17.51",
1313
"homepage": "http://pdfkit.org/",
1414
"author": {
1515
"name": "Devon Govett",

types/write_svg.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export default function SVGtoPDF(
1717
options?: SVGtoPDF.Options,
1818
): void;
1919

20-
declare namespace SVGtoPDF {
20+
export declare namespace SVGtoPDF {
2121
type RGBColor = [[number, number, number], number];
2222
type CMYKColor = [[number, number, number, number], number];
2323

0 commit comments

Comments
 (0)