Skip to content

Commit

Permalink
Added highlevel ability to provide lineCap style (#499)
Browse files Browse the repository at this point in the history
* Added highlevel ability to provide dashArray, and dashPhase for page.drawLine

* Added dashArray and dashPhase for the rest of the drawing functions: rectangle, square, ellipse, circle, svgpath

* Updated tests to reflect dashpattern changes

* Added highlevel ability to provide lineCapStyle to the following page.draw functions: line, square, rectangle, circle, ellipse

* Applying requested changes

* Updated tests to reflect changes

* Applying requested changes: lineCap to borderLineCap

Co-authored-by: Peter Varga <peter.varga@xyicon.com>
  • Loading branch information
soadzoor and Peter Varga authored Jun 26, 2020
1 parent bc4b172 commit 435bddf
Show file tree
Hide file tree
Showing 11 changed files with 106 additions and 39 deletions.
23 changes: 16 additions & 7 deletions apps/deno/tests/test1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ import {
popGraphicsState,
pushGraphicsState,
rgb,
setDashPattern,
setLineCap,
setLineJoin,
StandardFonts,
} from '../../../dist/pdf-lib.esm.js';
Expand Down Expand Up @@ -144,10 +142,6 @@ export default async (assets: Assets) => {
// Lower-left quadrant
page1.moveTo(0, 0);
page1.drawSquare({ size: size / 2, color: cmyk(1, 0, 0, 0) });
page1.pushOperators(
pushGraphicsState(),
setLineCap(LineCapStyle.Round),
);
page1.drawCircle({
x: size / 4,
y: size / 4,
Expand All @@ -156,8 +150,23 @@ export default async (assets: Assets) => {
borderDashArray: [25],
borderDashPhase: 25,
borderColor: cmyk(0, 1, 0, 0),
borderLineCap: LineCapStyle.Round,
});

page1.drawLine({
start: {
x: size / 4,
y: size / 4
},
end: {
x: size / 4 + 100,
y: size / 4 + 100
},
color: rgb(0, 1, 0),
thickness: 3,
dashArray: [12, 6],
lineCap: LineCapStyle.Round,
});
page1.pushOperators(popGraphicsState());

page1.drawLine({
start: {
Expand Down
6 changes: 4 additions & 2 deletions apps/deno/tests/test12.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
PageSizes,
PDFDocument,
BlendMode,
LineCapStyle,
cmyk,
degrees,
rgb,
Expand Down Expand Up @@ -72,8 +73,9 @@ const secondPage = async (pdfDoc: PDFDocument) => {
x: inchToPt(-1),
y: inchToPt(11),
scale: 0.5,
borderWidth: 2,
borderDashArray: [12, 6],
borderWidth: 4,
borderDashArray: [24, 12],
borderLineCap: LineCapStyle.Round,
});
page.drawSvgPath('M200,300 L400,50 L600,300 L800,550 L1000,300', {
x: inchToPt(-1),
Expand Down
22 changes: 16 additions & 6 deletions apps/node/tests/test1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import {
popGraphicsState,
pushGraphicsState,
rgb,
setLineCap,
setLineJoin,
StandardFonts,
typedArrayFor,
Expand Down Expand Up @@ -143,10 +142,6 @@ export default async (assets: Assets) => {
// Lower-left quadrant
page1.moveTo(0, 0);
page1.drawSquare({ size: size / 2, color: cmyk(1, 0, 0, 0) });
page1.pushOperators(
pushGraphicsState(),
setLineCap(LineCapStyle.Round),
);
page1.drawCircle({
x: size / 4,
y: size / 4,
Expand All @@ -155,8 +150,23 @@ export default async (assets: Assets) => {
borderDashArray: [25],
borderDashPhase: 25,
borderColor: cmyk(0, 1, 0, 0),
borderLineCap: LineCapStyle.Round,
});

page1.drawLine({
start: {
x: size / 4,
y: size / 4
},
end: {
x: size / 4 + 100,
y: size / 4 + 100
},
color: rgb(0, 1, 0),
thickness: 3,
dashArray: [12, 6],
lineCap: LineCapStyle.Round,
});
page1.pushOperators(popGraphicsState());

page1.drawLine({
start: {
Expand Down
6 changes: 4 additions & 2 deletions apps/node/tests/test12.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
PageSizes,
PDFDocument,
BlendMode,
LineCapStyle,
cmyk,
degrees,
rgb,
Expand Down Expand Up @@ -69,8 +70,9 @@ const secondPage = async (pdfDoc: PDFDocument) => {
x: inchToPt(-1),
y: inchToPt(11),
scale: 0.5,
borderWidth: 2,
borderDashArray: [12, 6],
borderWidth: 4,
borderDashArray: [24, 12],
borderLineCap: LineCapStyle.Round,
});
page.drawSvgPath('M200,300 L400,50 L600,300 L800,550 L1000,300', {
x: inchToPt(-1),
Expand Down
22 changes: 16 additions & 6 deletions apps/rn/src/tests/test1.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import {
popGraphicsState,
pushGraphicsState,
rgb,
setLineCap,
setLineJoin,
StandardFonts,
} from 'pdf-lib';
Expand Down Expand Up @@ -148,10 +147,6 @@ export default async () => {
// Lower-left quadrant
page1.moveTo(0, 0);
page1.drawSquare({ size: size / 2, color: cmyk(1, 0, 0, 0) });
page1.pushOperators(
pushGraphicsState(),
setLineCap(LineCapStyle.Round),
);
page1.drawCircle({
x: size / 4,
y: size / 4,
Expand All @@ -160,8 +155,23 @@ export default async () => {
borderDashArray: [25],
borderDashPhase: 25,
borderColor: cmyk(0, 1, 0, 0),
borderLineCap: LineCapStyle.Round,
});

page1.drawLine({
start: {
x: size / 4,
y: size / 4
},
end: {
x: size / 4 + 100,
y: size / 4 + 100
},
color: rgb(0, 1, 0),
thickness: 3,
dashArray: [12, 6],
lineCap: LineCapStyle.Round,
});
page1.pushOperators(popGraphicsState());

page1.drawLine({
start: {
Expand Down
6 changes: 4 additions & 2 deletions apps/rn/src/tests/test12.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
PageSizes,
PDFDocument,
BlendMode,
LineCapStyle,
cmyk,
degrees,
rgb,
Expand Down Expand Up @@ -70,8 +71,9 @@ const secondPage = async (pdfDoc) => {
x: inchToPt(-1),
y: inchToPt(11),
scale: 0.5,
borderWidth: 2,
borderDashArray: [12, 6],
borderWidth: 4,
borderDashArray: [24, 12],
borderLineCap: LineCapStyle.Round,
});
page.drawSvgPath('M200,300 L400,50 L600,300 L800,550 L1000,300', {
x: inchToPt(-1),
Expand Down
23 changes: 16 additions & 7 deletions apps/web/test1.html
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,10 @@
clipEvenOdd,
closePath,
cmyk,
setDashPattern,
degrees,
drawRectangle,
endPath,
grayscale,
setLineCap,
LineCapStyle,
setLineJoin,
LineJoinStyle,
Expand Down Expand Up @@ -197,10 +195,6 @@
// Lower-left quadrant
page1.moveTo(0, 0);
page1.drawSquare({ size: size / 2, color: cmyk(1, 0, 0, 0) });
page1.pushOperators(
pushGraphicsState(),
setLineCap(LineCapStyle.Round),
);
page1.drawCircle({
x: size / 4,
y: size / 4,
Expand All @@ -209,8 +203,23 @@
borderDashArray: [25],
borderDashPhase: 25,
borderColor: cmyk(0, 1, 0, 0),
borderLineCap: LineCapStyle.Round,
});

page1.drawLine({
start: {
x: size / 4,
y: size / 4
},
end: {
x: size / 4 + 100,
y: size / 4 + 100
},
color: rgb(0, 1, 0),
thickness: 3,
dashArray: [12, 6],
lineCap: LineCapStyle.Round,
});
page1.pushOperators(popGraphicsState());

page1.drawLine({
start: {
Expand Down
6 changes: 4 additions & 2 deletions apps/web/test12.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
const {
PageSizes,
PDFDocument,
LineCapStyle,
rgb,
cmyk,
BlendMode,
Expand Down Expand Up @@ -119,8 +120,9 @@
x: inchToPt(-1),
y: inchToPt(11),
scale: 0.5,
borderWidth: 2,
borderDashArray: [12, 6],
borderWidth: 4,
borderDashArray: [24, 12],
borderLineCap: LineCapStyle.Round,
});
page.drawSvgPath('M200,300 L400,50 L600,300 L800,550 L1000,300', {
x: inchToPt(-1),
Expand Down
7 changes: 7 additions & 0 deletions src/api/PDFPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1135,6 +1135,7 @@ export default class PDFPage {
]);
assertOrUndefined(options.borderDashArray, 'options.borderDashArray', [Array]);
assertOrUndefined(options.borderDashPhase, 'options.borderDashPhase', ['number']);
assertOrUndefined(options.borderLineCap, 'options.borderLineCap', ['number']);
assertRangeOrUndefined(
options.borderOpacity,
'options.borderOpacity',
Expand Down Expand Up @@ -1164,6 +1165,7 @@ export default class PDFPage {
borderWidth: options.borderWidth ?? 0,
borderDashArray: options.borderDashArray ?? undefined,
borderDashPhase: options.borderDashPhase ?? undefined,
borderLineCap: options.borderLineCap ?? undefined,
graphicsState: graphicsStateKey,
}),
);
Expand Down Expand Up @@ -1201,6 +1203,7 @@ export default class PDFPage {
assertOrUndefined(options.dashPhase, 'options.dashPhase', ['number']);
assertIsOneOf(options.lineCap, 'options.lineCap', LineCapStyle);
assertRangeOrUndefined(options.opacity, 'opacity.opacity', 0, 1);
assertOrUndefined(options.lineCap, 'options.lineCap', ['number']);
assertIsOneOfOrUndefined(options.blendMode, 'options.blendMode', BlendMode);

const graphicsStateKey = this.maybeEmbedGraphicsState({
Expand Down Expand Up @@ -1263,6 +1266,7 @@ export default class PDFPage {
]);
assertOrUndefined(options.borderDashArray, 'options.borderDashArray', [Array]);
assertOrUndefined(options.borderDashPhase, 'options.borderDashPhase', ['number']);
assertOrUndefined(options.borderLineCap, 'options.borderLineCap', ['number']);
assertRangeOrUndefined(
options.borderOpacity,
'options.borderOpacity',
Expand Down Expand Up @@ -1297,6 +1301,7 @@ export default class PDFPage {
borderDashArray: options.borderDashArray ?? undefined,
borderDashPhase: options.borderDashPhase ?? undefined,
graphicsState: graphicsStateKey,
borderLineCap: options.borderLineCap ?? undefined,
}),
);
}
Expand Down Expand Up @@ -1364,6 +1369,7 @@ export default class PDFPage {
assertOrUndefined(options.borderWidth, 'options.borderWidth', ['number']);
assertOrUndefined(options.borderDashArray, 'options.borderDashArray', [Array]);
assertOrUndefined(options.borderDashPhase, 'options.borderDashPhase', ['number']);
assertOrUndefined(options.borderLineCap, 'options.borderLineCap', ['number']);
assertIsOneOfOrUndefined(options.blendMode, 'options.blendMode', BlendMode);

const graphicsStateKey = this.maybeEmbedGraphicsState({
Expand All @@ -1388,6 +1394,7 @@ export default class PDFPage {
borderWidth: options.borderWidth ?? 0,
borderDashArray: options.borderDashArray ?? undefined,
borderDashPhase: options.borderDashPhase ?? undefined,
borderLineCap: options.borderLineCap ?? undefined,
graphicsState: graphicsStateKey,
}),
);
Expand Down
13 changes: 9 additions & 4 deletions src/api/PDFPageOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ export interface PDFPageDrawSVGOptions {
borderOpacity?: number;
borderDashArray?: number[];
borderDashPhase?: number;
borderLineCap?: LineCapStyle;
blendMode?: BlendMode;
}

Expand Down Expand Up @@ -100,7 +101,8 @@ export interface PDFPageDrawRectangleOptions {
borderColor?: Color;
borderOpacity?: number;
borderDashArray?: number[];
borderDashPhase?: number;
borderDashPhase?: number;
borderLineCap?: LineCapStyle;
blendMode?: BlendMode;
}

Expand All @@ -117,7 +119,8 @@ export interface PDFPageDrawSquareOptions {
borderColor?: Color;
borderOpacity?: number;
borderDashArray?: number[];
borderDashPhase?: number;
borderDashPhase?: number;
borderLineCap?: LineCapStyle;
blendMode?: BlendMode;
}

Expand All @@ -132,7 +135,8 @@ export interface PDFPageDrawEllipseOptions {
borderOpacity?: number;
borderWidth?: number;
borderDashArray?: number[];
borderDashPhase?: number;
borderDashPhase?: number;
borderLineCap?: LineCapStyle;
blendMode?: BlendMode;
}

Expand All @@ -146,6 +150,7 @@ export interface PDFPageDrawCircleOptions {
borderOpacity?: number;
borderWidth?: number;
borderDashArray?: number[];
borderDashPhase?: number;
borderDashPhase?: number;
borderLineCap?: LineCapStyle;
blendMode?: BlendMode;
}
Loading

0 comments on commit 435bddf

Please sign in to comment.