@pdfme/pdf-lib 6.1.13-dev.36 → 6.1.13-dev.37

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,4 @@
1
- import { Color } from './colors.js';
1
+ import type { Color, RGB } from './colors.js';
2
2
  import PDFFont from './PDFFont.js';
3
3
  import { Rotation } from './rotations.js';
4
4
  import { FillRule, LineCapStyle } from './operators.js';
@@ -159,6 +159,21 @@ export interface PDFPageDrawCircleOptions extends SvgOptions {
159
159
  borderLineCap?: LineCapStyle;
160
160
  blendMode?: BlendMode;
161
161
  }
162
+ export type SvgColorMapper = (input: {
163
+ color: string;
164
+ parsed: {
165
+ rgb: RGB;
166
+ alpha?: number;
167
+ };
168
+ kind: 'fill' | 'stroke';
169
+ inherited?: {
170
+ color?: Color;
171
+ opacity?: number;
172
+ };
173
+ }) => {
174
+ color: Color;
175
+ alpha?: number;
176
+ } | undefined;
162
177
  export interface PDFPageDrawSVGElementOptions {
163
178
  x?: number;
164
179
  y?: number;
@@ -168,5 +183,6 @@ export interface PDFPageDrawSVGElementOptions {
168
183
  fonts?: {
169
184
  [fontName: string]: PDFFont;
170
185
  };
186
+ mapColor?: SvgColorMapper;
171
187
  }
172
188
  export {};
package/dist/api/svg.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { HTMLElement } from 'node-html-better-parser';
2
- import { Color } from './colors.js';
2
+ import { type Color } from './colors.js';
3
3
  import { Degrees } from './rotations.js';
4
4
  import PDFPage from './PDFPage.js';
5
5
  import { PDFPageDrawSVGElementOptions } from './PDFPageOptions.js';
package/dist/index.js CHANGED
@@ -17318,25 +17318,51 @@ var splitFontFamilies = (fontFamily) => {
17318
17318
  var normalizeFontFamily = (fontFamily) => {
17319
17319
  return splitFontFamilies(fontFamily)[0] || fontFamily.trim();
17320
17320
  };
17321
- var parseColor = (color, inherited) => {
17321
+ var parseColor = (color, kind, options, inherited) => {
17322
17322
  if (!color || color.length === 0) return void 0;
17323
17323
  if (["none", "transparent"].includes(color)) return void 0;
17324
- if (color === "currentColor") return inherited || parseColor("#000000");
17324
+ if (color === "currentColor") {
17325
+ if (inherited?.color) return {
17326
+ rgb: inherited.color,
17327
+ alpha: inherited.opacity?.toString()
17328
+ };
17329
+ return parseColor("#000000", kind, options);
17330
+ }
17325
17331
  const parsedColor = colorString(color);
17332
+ const parsed = {
17333
+ rgb: parsedColor.rgb,
17334
+ alpha: parsedColor.alpha
17335
+ };
17336
+ const mappedColor = options.mapColor?.({
17337
+ color,
17338
+ parsed,
17339
+ kind,
17340
+ inherited
17341
+ });
17342
+ if (mappedColor) return {
17343
+ rgb: mappedColor.color,
17344
+ alpha: mappedColor.alpha !== void 0 ? mappedColor.alpha.toString() : parsedColor.alpha !== void 0 ? parsedColor.alpha.toString() : void 0
17345
+ };
17326
17346
  return {
17327
17347
  rgb: parsedColor.rgb,
17328
- alpha: parsedColor.alpha ? parsedColor.alpha + "" : void 0
17348
+ alpha: parsedColor.alpha !== void 0 ? parsedColor.alpha.toString() : void 0
17329
17349
  };
17330
17350
  };
17331
- var parseAttributes = (element, inherited, matrix) => {
17351
+ var parseAttributes = (element, inherited, matrix, options) => {
17332
17352
  const attributes = element.attributes;
17333
17353
  const style = parseStyles(attributes.style);
17334
17354
  const widthRaw = styleOrAttribute(attributes, style, "width", "");
17335
17355
  const heightRaw = styleOrAttribute(attributes, style, "height", "");
17336
- const fillRaw = parseColor(styleOrAttribute(attributes, style, "fill"));
17356
+ const fillRaw = parseColor(styleOrAttribute(attributes, style, "fill"), "fill", options, {
17357
+ color: inherited.fill,
17358
+ opacity: inherited.fillOpacity
17359
+ });
17337
17360
  const fillOpacityRaw = styleOrAttribute(attributes, style, "fill-opacity");
17338
17361
  const opacityRaw = styleOrAttribute(attributes, style, "opacity");
17339
- const strokeRaw = parseColor(styleOrAttribute(attributes, style, "stroke"));
17362
+ const strokeRaw = parseColor(styleOrAttribute(attributes, style, "stroke"), "stroke", options, {
17363
+ color: inherited.stroke,
17364
+ opacity: inherited.strokeOpacity
17365
+ });
17340
17366
  const strokeOpacityRaw = styleOrAttribute(attributes, style, "stroke-opacity");
17341
17367
  const strokeLineCapRaw = styleOrAttribute(attributes, style, "stroke-linecap");
17342
17368
  const strokeLineJoinRaw = styleOrAttribute(attributes, style, "stroke-linejoin");
@@ -17506,18 +17532,18 @@ var getAspectRatioTransformation = (matrix, originalWidth, originalHeight, targe
17506
17532
  content: combineTransformation(combineTransformation(matrix, "translate", [x, y]), "scale", [scale])
17507
17533
  };
17508
17534
  };
17509
- var parseHTMLNode = (node, inherited, matrix, clipSpaces) => {
17535
+ var parseHTMLNode = (node, inherited, matrix, clipSpaces, options) => {
17510
17536
  if (node.nodeType === NodeType.COMMENT_NODE) return [];
17511
17537
  else if (node.nodeType === NodeType.TEXT_NODE) return [];
17512
- else if (node.tagName === "g") return parseGroupNode(node, inherited, matrix, clipSpaces);
17513
- else if (node.tagName === "svg") return parseSvgNode(node, inherited, matrix, clipSpaces);
17538
+ else if (node.tagName === "g") return parseGroupNode(node, inherited, matrix, clipSpaces, options);
17539
+ else if (node.tagName === "svg") return parseSvgNode(node, inherited, matrix, clipSpaces, options);
17514
17540
  else {
17515
17541
  if (node.tagName === "polygon") {
17516
17542
  node.tagName = "path";
17517
17543
  node.attributes.d = `M${node.attributes.points}Z`;
17518
17544
  delete node.attributes.points;
17519
17545
  }
17520
- const attributes = parseAttributes(node, inherited, matrix);
17546
+ const attributes = parseAttributes(node, inherited, matrix, options);
17521
17547
  const svgAttributes = {
17522
17548
  ...attributes.inherited,
17523
17549
  ...attributes.svgAttributes,
@@ -17528,10 +17554,10 @@ var parseHTMLNode = (node, inherited, matrix, clipSpaces) => {
17528
17554
  return [node];
17529
17555
  }
17530
17556
  };
17531
- var parseSvgNode = (node, inherited, matrix, clipSpaces) => {
17557
+ var parseSvgNode = (node, inherited, matrix, clipSpaces, options) => {
17532
17558
  if (!node.attributes.width) node.setAttribute("width", inherited.viewBox.width + "");
17533
17559
  if (!node.attributes.height) node.setAttribute("height", inherited.viewBox.height + "");
17534
- const attributes = parseAttributes(node, inherited, matrix);
17560
+ const attributes = parseAttributes(node, inherited, matrix, options);
17535
17561
  const result = [];
17536
17562
  const viewBox = node.attributes.viewBox ? parseViewBox(node.attributes.viewBox) : node.attributes.width && node.attributes.height ? parseViewBox(`0 0 ${node.attributes.width} ${node.attributes.height}`) : inherited.viewBox;
17537
17563
  let newMatrix = combineTransformation(matrix, "translate", [parseFloat(node.attributes.x) || 0, parseFloat(node.attributes.y) || 0]);
@@ -17559,16 +17585,16 @@ var parseSvgNode = (node, inherited, matrix, clipSpaces) => {
17559
17585
  const parsedNodes = parseHTMLNode(child, {
17560
17586
  ...attributes.inherited,
17561
17587
  viewBox
17562
- }, newMatrix, [...clipSpaces, baseClipSpace]);
17588
+ }, newMatrix, [...clipSpaces, baseClipSpace], options);
17563
17589
  result.push(...parsedNodes);
17564
17590
  });
17565
17591
  return result;
17566
17592
  };
17567
- var parseGroupNode = (node, inherited, matrix, clipSpaces) => {
17568
- const attributes = parseAttributes(node, inherited, matrix);
17593
+ var parseGroupNode = (node, inherited, matrix, clipSpaces, options) => {
17594
+ const attributes = parseAttributes(node, inherited, matrix, options);
17569
17595
  const result = [];
17570
17596
  node.childNodes.forEach((child) => {
17571
- result.push(...parseHTMLNode(child, attributes.inherited, attributes.matrix, clipSpaces));
17597
+ result.push(...parseHTMLNode(child, attributes.inherited, attributes.matrix, clipSpaces, options));
17572
17598
  });
17573
17599
  return result;
17574
17600
  };
@@ -17589,7 +17615,7 @@ var parseViewBox = (viewBox) => {
17589
17615
  height: heightViewBox
17590
17616
  };
17591
17617
  };
17592
- var parse$1 = (svg, { width, height, fontSize }, size, matrix) => {
17618
+ var parse$1 = (svg, { width, height, fontSize, mapColor }, size, matrix) => {
17593
17619
  const htmlElement = parse(svg).firstChild;
17594
17620
  if (width) htmlElement.setAttribute("width", width + "");
17595
17621
  if (height) htmlElement.setAttribute("height", height + "");
@@ -17597,7 +17623,7 @@ var parse$1 = (svg, { width, height, fontSize }, size, matrix) => {
17597
17623
  return parseHTMLNode(htmlElement, {
17598
17624
  ...size,
17599
17625
  viewBox: parseViewBox(htmlElement.attributes.viewBox || "0 0 1 1")
17600
- }, matrix, []);
17626
+ }, matrix, [], { mapColor });
17601
17627
  };
17602
17628
  var drawSvg = async (page, svg, options) => {
17603
17629
  if (!svg) return;
@@ -18967,12 +18993,14 @@ var PDFPage = class {
18967
18993
  assertOrUndefined(options.y, "options.y", ["number"]);
18968
18994
  assertOrUndefined(options.width, "options.width", ["number"]);
18969
18995
  assertOrUndefined(options.height, "options.height", ["number"]);
18996
+ assertOrUndefined(options.mapColor, "options.mapColor", [Function]);
18970
18997
  await drawSvg(this, svg, {
18971
18998
  x: options.x ?? this.x,
18972
18999
  y: options.y ?? this.y,
18973
19000
  fonts: options.fonts,
18974
19001
  width: options.width,
18975
- height: options.height
19002
+ height: options.height,
19003
+ mapColor: options.mapColor
18976
19004
  });
18977
19005
  }
18978
19006
  getFont() {