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

Change drawSvg to be synchronous #53

Merged
merged 2 commits into from
May 20, 2024
Merged

Change drawSvg to be synchronous #53

merged 2 commits into from
May 20, 2024

Conversation

MatheusrdSantos
Copy link
Collaborator

No description provided.

src/api/svg.ts Outdated
};
}

export const drawSvgSync = (
Copy link
Collaborator

Choose a reason for hiding this comment

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

For me, you missed half of the point.

We want to expose the same API as Images. This means that you need and embedSvg that will return a PDFSvg object method and a drawSvgSync that will take that PDFSvg and draw it on the pdf page.

Your methods also seem to be misplaced between PDFPage and svg. The method drawSvg from svg has no more reason to be async and should be made sync. No need to create a drawSvgAsync here.

Copy link
Collaborator Author

@MatheusrdSantos MatheusrdSantos May 16, 2024

Choose a reason for hiding this comment

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

I fixed the implementation, but it's a bit different from your suggestion. I didn't implement the PDFSvg class because it would be a breaking change of the current drawSvg api.

@@ -897,10 +894,10 @@ const parse = (
);
};

export const drawSvg = async (
export const drawSvg = (
Copy link
Collaborator

Choose a reason for hiding this comment

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

This should have this signature: drawSvg(page: PDFPage, svg: PDFSvg, options: PDFPageDrawSVGElementOptions)

type PDFSvg = {
  svg: string,
  images: Record<string, image>
}

@@ -174,4 +175,5 @@ export interface PDFPageDrawSVGElementOptions {
height?: number;
fontSize?: number;
fonts?: { [fontName: string]: PDFFont };
images?: Record<string, PDFImage>
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should not be part of the options

@@ -1585,22 +1585,23 @@ export default class PDFPage {
* @param svg The SVG to be drawn.
* @param options The options to be used when drawing the SVG.
*/
async drawSvg(
drawSvg(
Copy link
Collaborator

Choose a reason for hiding this comment

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

If we want to avoid breaking changes, this method can't be made sync, because it is exposed to the public. But we could decide to make a breaking change. In this case, we should change the signature of this method to drawSvg(svg: PDFSvg, options: PDFPageDrawSVGElementOptions)

In case you want to go with the no breaking change option, you'd need to create the method:

drawSvgSync(svg: PDFSvg, options: PDFPageDrawSVGElementOptions): void {
  drawSvg(this, svg, options)
}

async drawSvg(svg: string, options: PDFPageDrawSVGElementOptions): void {
  const svgPdf = await embedSvg(svg)
  this.drawSvgSync(svgPdf, options)
}

The other option would lead to only:

drawSvg(svg: PDFSvg, options: PDFPageDrawSVGElementOptions): void {
  drawSvg(this, svg, options)
}

Of course, you can keep the asserts and detail the options like in the current code

@MatheusrdSantos MatheusrdSantos changed the title Implement drawSvgSync Change drawSvg to be synchronous May 17, 2024
@MatheusrdSantos MatheusrdSantos force-pushed the implement-drawSvgSync branch from 3f93a29 to b1a9a26 Compare May 17, 2024 15:45
@MatheusrdSantos MatheusrdSantos force-pushed the implement-drawSvgSync branch from b1a9a26 to 372bfce Compare May 17, 2024 15:50
@Sharcoux Sharcoux merged commit 1b33249 into master May 20, 2024
1 check passed
@Sharcoux Sharcoux deleted the implement-drawSvgSync branch May 20, 2024 07:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants