Skip to content

Commit 49b0da7

Browse files
Merge pull request #1 from Kittl/fix-clipping-on-firefox
fix(clipPaths): Fix svg element clipping - CU-86c8x20du
2 parents caa22a7 + 2d48486 commit 49b0da7

1 file changed

Lines changed: 24 additions & 12 deletions

File tree

lib/write_svg.js

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,11 @@ export default function (doc, svg, x, y, options) {
159159
white: [NamedColors.white, 1],
160160
transparent: [NamedColors.black, 0],
161161
};
162+
const RGBDefaultColors = {
163+
black: [NamedColors.black, 1],
164+
white: [NamedColors.white, 1],
165+
transparent: [NamedColors.black, 0],
166+
};
162167
const Entities = {
163168
quot: 34,
164169
amp: 38,
@@ -694,18 +699,16 @@ export default function (doc, svg, x, y, options) {
694699
doc.page.xobjects[group.name] = group.xobj;
695700
doc.addContent('/' + group.name + ' Do');
696701
}
697-
function docApplyMask(group, clip) {
702+
function docApplyMask(group) {
698703
let name = 'M' + (doc._maskCount = (doc._maskCount || 0) + 1);
699704
let gstate = doc.ref({
700705
Type: 'ExtGState',
701706
CA: 1,
702707
ca: 1,
703708
BM: 'Normal',
704709
SMask: {
705-
// Needs to be Alpha for clipping to work with cmyk color space
706-
// Clipping mode is reproducing svg clip-path by drawing everything inside clip path with DefaultColors.white [1,1,1,1]
707-
// And everything outside (not draw, defaults to [0,0,0,0]) is masked off
708-
S: clip ? 'Alpha' : 'Luminosity',
710+
// This should ideally be an Alpha mask for clipping but when we set it to Alpha the firefox pdf viewer doesn't render masks correctly
711+
S: 'Luminosity',
709712
G: group.xobj,
710713
BC: maskBackdrop,
711714
},
@@ -850,7 +853,16 @@ export default function (doc, svg, x, y, options) {
850853
function docEndText() {
851854
doc.addContent('ET');
852855
}
853-
function docFillColor(color) {
856+
function docFillColor(color, isClip) {
857+
if (isClip) {
858+
// always write clips in RGB colorspace
859+
const oldColorProfile = doc._activeColorProfile;
860+
doc._activeColorProfile = null;
861+
doc.fillColor(RGBDefaultColors.white[0], RGBDefaultColors.white[1]);
862+
doc._activeColorProfile = oldColorProfile;
863+
return;
864+
}
865+
854866
if (color[0].constructor.name === 'PDFPattern') {
855867
doc.fillOpacity(color[1]);
856868
docUsePattern(color[0], false);
@@ -2747,7 +2759,7 @@ export default function (doc, svg, x, y, options) {
27472759
doc.image(image, 0, 0);
27482760
} else {
27492761
doc.rect(0, 0, image.width, image.height);
2750-
docFillColor(DefaultColors.white).fill();
2762+
docFillColor(DefaultColors.white, true);
27512763
}
27522764
doc.restore();
27532765
};
@@ -3099,7 +3111,7 @@ export default function (doc, svg, x, y, options) {
30993111
}
31003112
} else {
31013113
this.shape.insertInDocument();
3102-
docFillColor(DefaultColors.white);
3114+
docFillColor(DefaultColors.white, true);
31033115
doc.fill(this.get('clip-rule'));
31043116
}
31053117
doc.restore();
@@ -3312,7 +3324,7 @@ export default function (doc, svg, x, y, options) {
33123324
this.drawChildren(true, false);
33133325
doc.restore();
33143326
docEndGroup(group);
3315-
docApplyMask(group, true);
3327+
docApplyMask(group);
33163328
};
33173329
};
33183330

@@ -3340,7 +3352,7 @@ export default function (doc, svg, x, y, options) {
33403352
this.drawChildren(false, true);
33413353
doc.restore();
33423354
docEndGroup(group);
3343-
docApplyMask(group, false);
3355+
docApplyMask(group);
33443356
};
33453357
};
33463358

@@ -3412,7 +3424,7 @@ export default function (doc, svg, x, y, options) {
34123424
}
34133425
if (fill || stroke || isClip) {
34143426
if (fill) {
3415-
docFillColor(fill);
3427+
docFillColor(fill, isClip);
34163428
}
34173429
if (stroke && strokeWidth) {
34183430
docStrokeColor(stroke);
@@ -3446,7 +3458,7 @@ export default function (doc, svg, x, y, options) {
34463458
let fill = this.getFill(isClip, isMask),
34473459
stroke = this.getStroke(isClip, isMask);
34483460
if (fill) {
3449-
docFillColor(fill);
3461+
docFillColor(fill, isClip);
34503462
}
34513463
if (stroke) {
34523464
docStrokeColor(stroke);

0 commit comments

Comments
 (0)