Skip to content

Commit 8a4c291

Browse files
authored
🤖 Merge PR DefinitelyTyped#74537 svg-to-pdfkit: Update types by @EliasWatson
1 parent 73ce84c commit 8a4c291

2 files changed

Lines changed: 46 additions & 14 deletions

File tree

types/svg-to-pdfkit/index.d.ts

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,63 @@
11
import PDFDocument = require("pdfkit");
22

3+
/**
4+
* Insert SVG into a PDF document created with PDFKit.
5+
*
6+
* @param doc the PDF document created with PDFKit
7+
* @param svg the SVG object or XML code
8+
* @param x the x position where the SVG will be added
9+
* @param y the y position where the SVG will be added
10+
* @param options See {@link SVGtoPDF.Options}
11+
*/
312
declare function SVGtoPDF(
413
doc: typeof PDFDocument,
514
svg: SVGElement | string,
615
x?: number,
716
y?: number,
8-
options?: SVGtoPDF.SVGtoPDFOptions,
17+
options?: SVGtoPDF.Options,
918
): void;
1019

1120
declare namespace SVGtoPDF {
12-
interface SVGtoPDFOptions {
21+
type Color = [[number, number, number], number];
22+
interface Options {
23+
/** initial viewport width, by default it's the page width */
1324
width?: number;
25+
26+
/** initial viewport width, by default it's the page height */
1427
height?: number;
28+
29+
/** override alignment of the SVG content inside its viewport */
1530
preserveAspectRatio?: string;
31+
32+
/** use the CSS styles computed by the browser (for SVGElement only) */
1633
useCSS?: boolean;
34+
35+
/** function called to get the fonts, see source code */
1736
fontCallback?: (
1837
family: string,
1938
bold: boolean,
2039
italic: boolean,
2140
fontOptions: { fauxItalic: boolean; fauxBold: boolean },
2241
) => string;
42+
43+
/** same as above for the images (for Node.js) */
2344
imageCallback?: (link: string) => string;
24-
documentCallback?: (file: string) => string;
25-
colorCallback?: (result: string, raw: string) => [[number, number, number], number];
26-
warningCallback?: (str: string) => void;
45+
46+
/** same as above for the external SVG documents */
47+
documentCallback?: (
48+
file: string,
49+
) => SVGElement | string | (SVGElement | string)[];
50+
51+
/** function called to get color, making mapping to CMYK possible */
52+
colorCallback?: (color: Color) => Color;
53+
54+
/** function called when there is a warning */
55+
warningCallback?: (warning: string) => void;
56+
57+
/** assume that units are PDF points instead of SVG pixels */
2758
assumePt?: boolean;
59+
60+
/** precision factor for approximate calculations (default = 3) */
2861
precision?: number;
2962
}
3063
}

types/svg-to-pdfkit/svg-to-pdfkit-tests.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ SvgToPdf(doc, "<svg>", 100, 100); // $ExpectType void
99

1010
SvgToPdf(doc, "<svg>", 100, 100, {}); // $ExpectType void
1111

12-
// https://github.com/alafr/SVG-to-PDFKit/blob/4d2d8746bda1e9335c47bbe9ad91277c51fd5a07/source.js#L2477
13-
const options: SvgToPdf.SVGtoPDFOptions = {
12+
// https://github.com/alafr/SVG-to-PDFKit/blob/b091ebd4e7b7d2310eb1003511cd5de480f7e0e1/source.js#L2617
13+
const options: SvgToPdf.Options = {
1414
width: 100,
1515
height: 100,
1616
preserveAspectRatio: "xMinYMin",
1717
useCSS: true,
18-
// https://github.com/alafr/SVG-to-PDFKit/blob/4d2d8746bda1e9335c47bbe9ad91277c51fd5a07/source.js#L2500
18+
// https://github.com/alafr/SVG-to-PDFKit/blob/b091ebd4e7b7d2310eb1003511cd5de480f7e0e1/source.js#L2640
1919
fontCallback: (family, bold, italic, fontOptions) => {
2020
family; // $ExpectType string
2121
bold; // $ExpectType boolean
@@ -24,18 +24,17 @@ const options: SvgToPdf.SVGtoPDFOptions = {
2424
fontOptions.fauxItalic; // $ExpectType boolean
2525
return family;
2626
},
27-
// https://github.com/alafr/SVG-to-PDFKit/blob/4d2d8746bda1e9335c47bbe9ad91277c51fd5a07/source.js#L2558
27+
// https://github.com/alafr/SVG-to-PDFKit/blob/b091ebd4e7b7d2310eb1003511cd5de480f7e0e1/source.js#L2698
2828
imageCallback: link => {
2929
link; // $ExpectType string
3030
return link;
3131
},
32-
// https://github.com/alafr/SVG-to-PDFKit/blob/4d2d8746bda1e9335c47bbe9ad91277c51fd5a07/source.js#L2562
33-
colorCallback: (result, row) => {
34-
result; // $ExpectType string
35-
row; // $ExpectType string
32+
// https://github.com/alafr/SVG-to-PDFKit/blob/b091ebd4e7b7d2310eb1003511cd5de480f7e0e1/source.js#L2703
33+
colorCallback: (color) => {
34+
color; // $ExpectType Color
3635
return [[255, 255, 255], 1];
3736
},
38-
// https://github.com/alafr/SVG-to-PDFKit/blob/4d2d8746bda1e9335c47bbe9ad91277c51fd5a07/source.js#L2494
37+
// https://github.com/alafr/SVG-to-PDFKit/blob/b091ebd4e7b7d2310eb1003511cd5de480f7e0e1/source.js#L2635
3938
warningCallback: str => {
4039
str; // $ExpectType string
4140
console.log(str);

0 commit comments

Comments
 (0)