Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add rounded rectangle #9

Merged
merged 3 commits into from
Mar 10, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions apps/web/test9.html
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
color: solarizedWhite,
borderColor: solarizedGray,
borderWidth: 3,
radius: 10.0,
});
page.setFont(ubuntuFont);
page.setFontColor(solarizedGray);
Expand Down
3 changes: 3 additions & 0 deletions src/api/PDFPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1354,6 +1354,7 @@ export default class PDFPage {
* color: rgb(0.75, 0.2, 0.2),
* opacity: 0.5,
* borderOpacity: 0.75,
* radius: 0.1,
* })
* ```
* @param options The options to be used when drawing the rectangle.
Expand Down Expand Up @@ -1390,6 +1391,7 @@ export default class PDFPage {
1,
);
assertIsOneOfOrUndefined(options.blendMode, 'options.blendMode', BlendMode);
assertOrUndefined(options.radius, 'options.radius', ['number']);

const graphicsStateKey = this.maybeEmbedGraphicsState({
opacity: options.opacity,
Expand Down Expand Up @@ -1420,6 +1422,7 @@ export default class PDFPage {
borderLineCap: options.borderLineCap ?? undefined,
matrix: options.matrix,
clipSpaces: options.clipSpaces,
radius: options.radius ?? 0,
}),
);
}
Expand Down
1 change: 1 addition & 0 deletions src/api/PDFPageOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ export interface PDFPageDrawRectangleOptions extends SvgOptions {
borderDashPhase?: number;
borderLineCap?: LineCapStyle;
blendMode?: BlendMode;
radius?: number;
}

export interface PDFPageDrawSquareOptions extends SvgOptions {
Expand Down
158 changes: 105 additions & 53 deletions src/api/operations.ts
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do not commit formatting changes.

Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@ export interface DrawTextOptions {
clipSpaces?: Space[];
}

const clipSpace = ({
const clipSpace = ({
topLeft,
topRight,
bottomRight,
bottomLeft
}: Space) =>
}: Space) =>
[
moveTo(topLeft.x, topLeft.y),
lineTo(topRight.x, topRight.y),
Expand Down Expand Up @@ -225,33 +225,85 @@ export const drawRectangle = (options: {
graphicsState?: string | PDFName;
matrix?: TransformationMatrix;
clipSpaces?: Space[];
}) => [
pushGraphicsState(),
options.graphicsState && setGraphicsState(options.graphicsState),
options.color && setFillingColor(options.color),
options.borderColor && setStrokingColor(options.borderColor),
setLineWidth(options.borderWidth),
options.borderLineCap && setLineCap(options.borderLineCap),
setDashPattern(options.borderDashArray ?? [], options.borderDashPhase ?? 0),
...(options.clipSpaces ? clipSpaces(options.clipSpaces) : []),
options.matrix && concatTransformationMatrix(...options.matrix),
translate(options.x, options.y),
rotateRadians(toRadians(options.rotate)),
skewRadians(toRadians(options.xSkew), toRadians(options.ySkew)),
moveTo(0, 0),
lineTo(0, options.height),
lineTo(options.width, options.height),
lineTo(options.width, 0),
closePath(),

// prettier-ignore
options.color && options.borderWidth ? fillAndStroke()
radius?: number | PDFNumber;
}) => {
if (!options.radius || asNumber(options.radius) <= 0) {
return [
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are many redundancies, so please write it more efficiently.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The drawRectangle function is divided into two large blocks due to conditional branching, resulting in significant code duplication.

You can extract the common parts to make the function more DRY.

pushGraphicsState(),
options.graphicsState && setGraphicsState(options.graphicsState),
options.color && setFillingColor(options.color),
options.borderColor && setStrokingColor(options.borderColor),
setLineWidth(options.borderWidth),
options.borderLineCap && setLineCap(options.borderLineCap),
setDashPattern(options.borderDashArray ?? [], options.borderDashPhase ?? 0),
...(options.clipSpaces ? clipSpaces(options.clipSpaces) : []),
options.matrix && concatTransformationMatrix(...options.matrix),
translate(options.x, options.y),
rotateRadians(toRadians(options.rotate)),
skewRadians(toRadians(options.xSkew), toRadians(options.ySkew)),
moveTo(0, 0),
lineTo(0, options.height),
lineTo(options.width, options.height),
lineTo(options.width, 0),
closePath(),

// prettier-ignore
options.color && options.borderWidth ? fillAndStroke()
: options.color ? fill()
: options.borderColor ? stroke()
: closePath(),
: closePath(),

popGraphicsState(),
].filter(Boolean) as PDFOperator[];
} else {
const radius = asNumber(options.radius);
const width = asNumber(options.width);
const height = asNumber(options.height);

if (radius > width / 2.0 || radius > height / 2.0) {
console.warn("radius exceeds the rectangle's width or height");
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the radius exceeds half of the rectangle's width/height, a warning is issued, and an empty array is returned. This causes the rendering to fail, but the error may not be clear.

Please adjust it to the maximum possible radius.

return [];
}
const kappa = KAPPA * radius;

return [
pushGraphicsState(),
options.graphicsState && setGraphicsState(options.graphicsState),
options.color && setFillingColor(options.color),
options.borderColor && setStrokingColor(options.borderColor),
setLineWidth(options.borderWidth),
options.borderLineCap && setLineCap(options.borderLineCap),
setDashPattern(options.borderDashArray ?? [], options.borderDashPhase ?? 0),
...(options.clipSpaces ? clipSpaces(options.clipSpaces) : []),
options.matrix && concatTransformationMatrix(...options.matrix),
translate(options.x, options.y),
rotateRadians(toRadians(options.rotate)),
skewRadians(toRadians(options.xSkew), toRadians(options.ySkew)),
moveTo(0, radius),
// Lower Left corner
appendBezierCurve(0, radius - kappa, radius - kappa, 0, radius, 0),
lineTo(width - radius, 0),
// Lower Right corner
appendBezierCurve(width - radius + kappa, 0, width, radius - kappa, width, radius),
lineTo(width, height - radius),
// Upper Right corner
appendBezierCurve(width, height - radius + kappa, width - radius + kappa, height, width - radius, height),
lineTo(radius, height),
// Upper Left corner
appendBezierCurve(radius - kappa, height, 0, height - radius + kappa, 0, height - radius),
closePath(),

// prettier-ignore
options.color && options.borderWidth ? fillAndStroke()
: options.color ? fill()
: options.borderColor ? stroke()
: closePath(),

popGraphicsState(),
].filter(Boolean) as PDFOperator[];
}

popGraphicsState(),
].filter(Boolean) as PDFOperator[];
}

const KAPPA = 4.0 * ((Math.sqrt(2) - 1.0) / 3.0);

Expand Down Expand Up @@ -352,24 +404,24 @@ export const drawEllipse = (options: {
// See https://github.com/Hopding/pdf-lib/pull/511#issuecomment-667685655.
...(options.rotate === undefined
? drawEllipsePath({
x: options.x,
y: options.y,
xScale: options.xScale,
yScale: options.yScale,
})
x: options.x,
y: options.y,
xScale: options.xScale,
yScale: options.yScale,
})
: drawEllipseCurves({
x: options.x,
y: options.y,
xScale: options.xScale,
yScale: options.yScale,
rotate: options.rotate ?? degrees(0),
})),
x: options.x,
y: options.y,
xScale: options.xScale,
yScale: options.yScale,
rotate: options.rotate ?? degrees(0),
})),

// prettier-ignore
options.color && options.borderWidth ? fillAndStroke()
: options.color ? fill()
: options.borderColor ? stroke()
: closePath(),
: closePath(),

popGraphicsState(),
].filter(Boolean) as PDFOperator[];
Expand Down Expand Up @@ -416,7 +468,7 @@ export const drawSvgPath = (
options.color && options.borderWidth ? fillAndStroke()
: options.color ? options.fillRule === FillRule.EvenOdd ? fillEvenOdd() : fill()
: options.borderColor ? stroke()
: closePath(),
: closePath(),

popGraphicsState(),
].filter(Boolean) as PDFOperator[];
Expand Down Expand Up @@ -485,23 +537,23 @@ export const rotateInPlace = (options: {
height: number | PDFNumber;
rotation: 0 | 90 | 180 | 270;
}) =>
options.rotation === 0 ? [
translate(0, 0),
rotateDegrees(0)
]
: options.rotation === 90 ? [
options.rotation === 0 ? [
translate(0, 0),
rotateDegrees(0)
]
: options.rotation === 90 ? [
translate(options.width, 0),
rotateDegrees(90)
]
: options.rotation === 180 ? [
translate(options.width, options.height),
rotateDegrees(180)
]
: options.rotation === 270 ? [
translate(0, options.height),
rotateDegrees(270)
]
: []; // Invalid rotation - noop
: options.rotation === 180 ? [
translate(options.width, options.height),
rotateDegrees(180)
]
: options.rotation === 270 ? [
translate(0, options.height),
rotateDegrees(270)
]
: []; // Invalid rotation - noop

export const drawCheckBox = (options: {
x: number | PDFNumber;
Expand Down