Skip to content

Commit a411df8

Browse files
committed
update svg rendering
- add support for transparency masks - add support for clip rect
1 parent 43e16d2 commit a411df8

File tree

3 files changed

+38
-5
lines changed

3 files changed

+38
-5
lines changed

src/api/PDFPage.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1243,13 +1243,14 @@ export default class PDFPage {
12431243
opacity: options.opacity,
12441244
borderOpacity: options.borderOpacity,
12451245
blendMode: options.blendMode,
1246+
softmask: options.softmask,
12461247
});
12471248

12481249
if (!('color' in options) && !('borderColor' in options)) {
12491250
options.borderColor = rgb(0, 0, 0);
12501251
}
12511252

1252-
const contentStream = this.getContentStream();
1253+
const contentStream = options.content_stream || this.getContentStream();
12531254
contentStream.push(
12541255
...drawSvgPath(path, {
12551256
x: options.x ?? this.x,
@@ -1264,6 +1265,7 @@ export default class PDFPage {
12641265
borderLineCap: options.borderLineCap ?? undefined,
12651266
graphicsState: graphicsStateKey,
12661267
pattern: options.pattern ?? undefined,
1268+
clip: options.clip ?? undefined,
12671269
}),
12681270
);
12691271
}
@@ -1385,12 +1387,14 @@ export default class PDFPage {
13851387
opacity: options.opacity,
13861388
borderOpacity: options.borderOpacity,
13871389
blendMode: options.blendMode,
1390+
softmask: options.softmask,
13881391
});
13891392

13901393
if (!('color' in options) && !('borderColor' in options)) {
13911394
options.color = rgb(0, 0, 0);
13921395
}
13931396

1397+
/*
13941398
const test = drawRectangle({
13951399
x: options.x ?? this.x,
13961400
y: options.y ?? this.y,
@@ -1409,8 +1413,13 @@ export default class PDFPage {
14091413
pattern: options.pattern ?? undefined,
14101414
});
14111415
console.info({test, options});
1416+
*/
14121417

1413-
const contentStream = this.getContentStream();
1418+
if (options.content_stream) {
1419+
console.info('using passed content stream');
1420+
}
1421+
1422+
const contentStream = options.content_stream || this.getContentStream();
14141423
contentStream.push(
14151424
...drawRectangle({
14161425
x: options.x ?? this.x,
@@ -1603,13 +1612,15 @@ export default class PDFPage {
16031612
opacity?: number;
16041613
borderOpacity?: number;
16051614
blendMode?: BlendMode;
1615+
softmask?: PDFDict;
16061616
}): PDFName | undefined {
1607-
const { opacity, borderOpacity, blendMode } = options;
1617+
const { opacity, borderOpacity, blendMode, softmask } = options;
16081618

16091619
if (
16101620
opacity === undefined &&
16111621
borderOpacity === undefined &&
1612-
blendMode === undefined
1622+
blendMode === undefined &&
1623+
softmask === undefined
16131624
) {
16141625
return undefined;
16151626
}
@@ -1619,6 +1630,7 @@ export default class PDFPage {
16191630
ca: opacity,
16201631
CA: borderOpacity,
16211632
BM: blendMode,
1633+
SMask: softmask,
16221634
});
16231635

16241636
const key = this.node.newExtGState('GS', graphicsState);

src/api/PDFPageOptions.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Color } from 'src/api/colors';
22
import PDFFont from 'src/api/PDFFont';
33
import { Rotation } from 'src/api/rotations';
44
import { LineCapStyle } from 'src/api/operators';
5-
import { PDFName } from '..';
5+
import { PDFContentStream, PDFDict, PDFName } from '..';
66

77
export enum BlendMode {
88
Normal = 'Normal',
@@ -75,7 +75,13 @@ export interface PDFPageDrawSVGOptions {
7575
borderDashPhase?: number;
7676
borderLineCap?: LineCapStyle;
7777
blendMode?: BlendMode;
78+
7879
pattern?: string|PDFName;
80+
softmask?: PDFDict;
81+
content_stream?: PDFContentStream;
82+
83+
clip?: { x: number, y: number, width: number, height: number };
84+
7985
}
8086

8187
export interface PDFPageDrawLineOptions {
@@ -108,6 +114,8 @@ export interface PDFPageDrawRectangleOptions {
108114
borderLineCap?: LineCapStyle;
109115
blendMode?: BlendMode;
110116
pattern?: string|PDFName;
117+
content_stream?: PDFContentStream;
118+
softmask?: PDFDict;
111119
}
112120

113121
export interface PDFPageDrawSquareOptions {

src/api/operations.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,12 +356,25 @@ export const drawSvgPath = (
356356
borderLineCap?: LineCapStyle;
357357
graphicsState?: string | PDFName;
358358
pattern?: string | PDFName;
359+
clip?: { x: number, y: number, width: number, height: number },
359360
},
360361
) =>
361362
[
362363
pushGraphicsState(),
363364
options.graphicsState && setGraphicsState(options.graphicsState),
364365

366+
///
367+
368+
options.clip && moveTo(options.clip.x, options.clip.y),
369+
options.clip && lineTo(options.clip.x, options.clip.y + options.clip.height),
370+
options.clip && lineTo(options.clip.x + options.clip.width, options.clip.y + options.clip.height),
371+
options.clip && lineTo(options.clip.x + options.clip.width, options.clip.y),
372+
options.clip && closePath(),
373+
options.clip && clip(),
374+
options.clip && endPath(),
375+
376+
///
377+
365378
translate(options.x, options.y),
366379
rotateRadians(toRadians(options.rotate ?? degrees(0))),
367380

0 commit comments

Comments
 (0)