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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
This version is forked from [Hopding/pdf-lib](https://github.com/Hopding/pdf-lib).
Incorporated several bug fixes and additional features into the original code.

<details>
<summary><a href="https://github.com/pdfme/pdf-lib/pull/9">2025/03/09 - Add `radius` option for drawRectangle. This enables to render rounded corner rectangles</a></summary>
Draw rounded corners with the specified radius using the Bezier curve function. The radius must be a positive number. If it exceeds half of the width or height, it will be clipped.
</details>
<details>
<summary><a href="https://github.com/cantoo-scribe/pdf-lib/pull/42">2023/12/22 - Incorporate @cantoo/pdf-lib Refactor drawSvg PR</a></summary>

Expand Down
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
44 changes: 38 additions & 6 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 @@ -225,7 +225,41 @@ export const drawRectangle = (options: {
graphicsState?: string | PDFName;
matrix?: TransformationMatrix;
clipSpaces?: Space[];
}) => [
radius?: number | PDFNumber;
}) => {
let ops = [];

if (!options.radius || asNumber(options.radius) <= 0) {
ops = [
moveTo(0, 0),
lineTo(0, options.height),
lineTo(options.width, options.height),
lineTo(options.width, 0),
closePath(),
];
} else {
let radius = asNumber(options.radius);
const width = asNumber(options.width);
const height = asNumber(options.height);

if (radius > width / 2.0 || radius > height / 2.0) {
radius = Math.min(width / 2.0, height / 2.0);
}
const offset = KAPPA * radius;
ops = [
moveTo(0, radius),
appendBezierCurve(0, radius - offset, radius - offset, 0, radius, 0),
lineTo(width - radius, 0),
appendBezierCurve(width - radius + offset, 0, width, radius - offset, width, radius),
lineTo(width, height - radius),
appendBezierCurve(width, height - radius + offset, width - radius + offset, height, width - radius, height),
lineTo(radius, height),
appendBezierCurve(radius - offset, height, 0, height - radius + offset, 0, height - radius),
closePath(),
];
}

return [
pushGraphicsState(),
options.graphicsState && setGraphicsState(options.graphicsState),
options.color && setFillingColor(options.color),
Expand All @@ -238,11 +272,8 @@ export const drawRectangle = (options: {
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(),

...ops,

// prettier-ignore
options.color && options.borderWidth ? fillAndStroke()
Expand All @@ -252,6 +283,7 @@ export const drawRectangle = (options: {

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

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

Expand Down