@pdfme/ui 3.2.3-dev.2 → 3.3.0-dev.0

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.
package/dist/index.es.js CHANGED
@@ -9290,6 +9290,7 @@ z.object({
9290
9290
  "schemas.barcodes.includetext": z.string()
9291
9291
  });
9292
9292
  z.enum(["viewer", "form", "designer"]);
9293
+ const ColorType = z.enum(["rgb", "cmyk"]).optional();
9293
9294
  z.object({ height: z.number(), width: z.number() });
9294
9295
  const Schema$1 = z.object({
9295
9296
  type: z.string(),
@@ -9320,6 +9321,7 @@ const ArrayBufferSchema = z.any().refine((a) => a instanceof ArrayBuffer), Uint8
9320
9321
  options: CommonOptions.optional(),
9321
9322
  plugins: z.record(z.object({ ui: z.any(), pdf: z.any(), propPanel: z.any() })).optional()
9322
9323
  }), GeneratorOptions = CommonOptions.extend({
9324
+ colorType: ColorType,
9323
9325
  author: z.string().optional(),
9324
9326
  creationDate: z.date().optional(),
9325
9327
  creator: z.string().optional(),
@@ -80672,7 +80674,7 @@ var colorStringExports = colorString$1.exports, ColorTypes;
80672
80674
  (function(a) {
80673
80675
  a.Grayscale = "Grayscale", a.RGB = "RGB", a.CMYK = "CMYK";
80674
80676
  })(ColorTypes || (ColorTypes = {}));
80675
- const rgb = (a, s, $) => (assertRange(a, "red", 0, 1), assertRange(s, "green", 0, 1), assertRange($, "blue", 0, 1), { type: ColorTypes.RGB, red: a, green: s, blue: $ });
80677
+ const rgb = (a, s, $) => (assertRange(a, "red", 0, 1), assertRange(s, "green", 0, 1), assertRange($, "blue", 0, 1), { type: ColorTypes.RGB, red: a, green: s, blue: $ }), cmyk = (a, s, $, _) => (assertRange(a, "cyan", 0, 1), assertRange(s, "magenta", 0, 1), assertRange($, "yellow", 0, 1), assertRange(_, "key", 0, 1), { type: ColorTypes.CMYK, cyan: a, magenta: s, yellow: $, key: _ });
80676
80678
  var TextAlignment;
80677
80679
  (function(a) {
80678
80680
  a[a.Left = 0] = "Left", a[a.Center = 1] = "Center", a[a.Right = 2] = "Right";
@@ -81510,7 +81512,18 @@ const convertForPdfLayoutProps = ({ schema: a, pageHeight: s, applyRotateTransla
81510
81512
  const [$, _, _e] = hex2rgb(a);
81511
81513
  return rgb($ / 255, _ / 255, _e / 255);
81512
81514
  }
81513
- }, embedAndGetFontObj = async (a) => {
81515
+ }, hex2CmykColor = (a) => {
81516
+ if (a) {
81517
+ if (!isHexValid(a))
81518
+ throw new Error(`Invalid hex color value ${a}`);
81519
+ a = a.replace("#", "");
81520
+ const $ = a.substring(0, 6), _ = a.substring(6, 8), _e = _ ? parseInt(_, 16) / 255 : 1;
81521
+ let tt = parseInt($.substring(0, 2), 16) / 255, nt = parseInt($.substring(2, 4), 16) / 255, at = parseInt($.substring(4, 6), 16) / 255;
81522
+ tt = tt * _e + (1 - _e), nt = nt * _e + (1 - _e), at = at * _e + (1 - _e);
81523
+ const ot = 1 - Math.max(tt, nt, at), rt = tt === 0 ? 0 : (1 - tt - ot) / (1 - ot), st = nt === 0 ? 0 : (1 - nt - ot) / (1 - ot), lt = at === 0 ? 0 : (1 - at - ot) / (1 - ot);
81524
+ return cmyk(rt, st, lt, ot);
81525
+ }
81526
+ }, hex2PrintingColor = (a, s) => (s == null ? void 0 : s.toLocaleLowerCase()) == "cmyk" ? hex2CmykColor(a) : hex2RgbColor(a), embedAndGetFontObj = async (a) => {
81514
81527
  const { pdfDoc: s, font: $, _cache: _ } = a;
81515
81528
  if (_.has(s))
81516
81529
  return _.get(s);
@@ -81521,66 +81534,66 @@ const convertForPdfLayoutProps = ({ schema: a, pageHeight: s, applyRotateTransla
81521
81534
  });
81522
81535
  })), tt = Object.keys($).reduce((nt, at, ot) => Object.assign(nt, { [at]: _e[ot] }), {});
81523
81536
  return _.set(s, tt), tt;
81524
- }, getFontProp = async ({ value: a, font: s, schema: $, _cache: _ }) => {
81525
- const _e = $.dynamicFontSize ? await calculateDynamicFontSize({ textSchema: $, font: s, value: a, _cache: _ }) : $.fontSize ?? DEFAULT_FONT_SIZE, tt = hex2RgbColor($.fontColor || DEFAULT_FONT_COLOR);
81537
+ }, getFontProp = async ({ value: a, font: s, schema: $, colorType: _, _cache: _e }) => {
81538
+ const tt = $.dynamicFontSize ? await calculateDynamicFontSize({ textSchema: $, font: s, value: a, _cache: _e }) : $.fontSize ?? DEFAULT_FONT_SIZE, nt = hex2PrintingColor($.fontColor || DEFAULT_FONT_COLOR, _);
81526
81539
  return {
81527
81540
  alignment: $.alignment ?? DEFAULT_ALIGNMENT,
81528
81541
  verticalAlignment: $.verticalAlignment ?? DEFAULT_VERTICAL_ALIGNMENT,
81529
81542
  lineHeight: $.lineHeight ?? DEFAULT_LINE_HEIGHT,
81530
81543
  characterSpacing: $.characterSpacing ?? DEFAULT_CHARACTER_SPACING,
81531
- fontSize: _e,
81532
- color: tt
81544
+ fontSize: tt,
81545
+ color: nt
81533
81546
  };
81534
81547
  }, pdfRender = async (a) => {
81535
81548
  const { value: s, pdfDoc: $, pdfLib: _, page: _e, options: tt, schema: nt, _cache: at } = a;
81536
81549
  if (!s)
81537
81550
  return;
81538
- const { font: ot = getDefaultFont() } = tt, [rt, st, lt] = await Promise.all([
81551
+ const { font: ot = getDefaultFont(), colorType: rt } = tt, [st, lt, et] = await Promise.all([
81539
81552
  embedAndGetFontObj({ pdfDoc: $, font: ot, _cache: at }),
81540
81553
  getFontKitFont(nt, ot, at),
81541
- getFontProp({ value: s, font: ot, schema: nt, _cache: at })
81542
- ]), { fontSize: et, color: it, alignment: ut, verticalAlignment: ft, lineHeight: ht, characterSpacing: dt } = lt, gt = nt.fontName ? nt.fontName : getFallbackFontName(ot), pt = rt && rt[gt], mt = _e.getHeight(), { width: ct, height: vt, rotate: At, position: { x: xt, y: bt }, opacity: Et } = convertForPdfLayoutProps({ schema: nt, pageHeight: mt, applyRotateTranslate: !1 });
81554
+ getFontProp({ value: s, font: ot, schema: nt, _cache: at, colorType: rt })
81555
+ ]), { fontSize: it, color: ut, alignment: ft, verticalAlignment: ht, lineHeight: dt, characterSpacing: gt } = et, pt = nt.fontName ? nt.fontName : getFallbackFontName(ot), mt = st && st[pt], ct = _e.getHeight(), { width: vt, height: At, rotate: xt, position: { x: bt, y: Et }, opacity: St } = convertForPdfLayoutProps({ schema: nt, pageHeight: ct, applyRotateTranslate: !1 });
81543
81556
  if (nt.backgroundColor) {
81544
- const yt = hex2RgbColor(nt.backgroundColor);
81545
- _e.drawRectangle({ x: xt, y: bt, width: ct, height: vt, rotate: At, color: yt });
81546
- }
81547
- _e.pushOperators(_.setCharacterSpacing(dt ?? DEFAULT_CHARACTER_SPACING));
81548
- const St = heightOfFontAtSize(st, et), Dt = getFontDescentInPt(st, et), Rt = ht === 0 ? 0 : (ht - 1) * et / 2, Tt = {
81549
- font: st,
81550
- fontSize: et,
81551
- characterSpacing: dt,
81552
- boxWidthInPt: ct
81553
- };
81554
- let wt = [];
81555
- s.split(/\r\n|\r|\n|\f|\u000B/g).forEach((yt) => {
81556
- wt = wt.concat(getSplittedLines(yt, Tt));
81557
+ const Pt = hex2PrintingColor(nt.backgroundColor, rt);
81558
+ _e.drawRectangle({ x: bt, y: Et, width: vt, height: At, rotate: xt, color: Pt });
81559
+ }
81560
+ _e.pushOperators(_.setCharacterSpacing(gt ?? DEFAULT_CHARACTER_SPACING));
81561
+ const Dt = heightOfFontAtSize(lt, it), Rt = getFontDescentInPt(lt, it), Tt = dt === 0 ? 0 : (dt - 1) * it / 2, wt = {
81562
+ font: lt,
81563
+ fontSize: it,
81564
+ characterSpacing: gt,
81565
+ boxWidthInPt: vt
81566
+ };
81567
+ let Ct = [];
81568
+ s.split(/\r\n|\r|\n|\f|\u000B/g).forEach((Pt) => {
81569
+ Ct = Ct.concat(getSplittedLines(Pt, wt));
81557
81570
  });
81558
- let Ct = 0;
81559
- if (ft === VERTICAL_ALIGN_TOP)
81560
- Ct = St + Rt;
81571
+ let It = 0;
81572
+ if (ht === VERTICAL_ALIGN_TOP)
81573
+ It = Dt + Tt;
81561
81574
  else {
81562
- const yt = ht * et * (wt.length - 1);
81563
- ft === VERTICAL_ALIGN_BOTTOM ? Ct = vt - yt + Dt - Rt : ft === VERTICAL_ALIGN_MIDDLE && (Ct = (vt - yt - St + Dt) / 2 + St);
81564
- }
81565
- const It = { x: xt + ct / 2, y: mt - mm2pt(nt.position.y) - vt / 2 };
81566
- wt.forEach((yt, Pt) => {
81567
- const kt = widthOfTextAtSize(yt, st, et, dt), jt = ht * et * Pt;
81568
- let Ht = xt;
81569
- ut === "center" ? Ht += (ct - kt) / 2 : ut === "right" && (Ht += ct - kt);
81570
- let Ft = mt - mm2pt(nt.position.y) - Ct - jt;
81571
- if (At.angle !== 0) {
81572
- const Lt = rotatePoint({ x: Ht, y: Ft }, It, At.angle);
81573
- Ht = Lt.x, Ft = Lt.y;
81574
- }
81575
- _e.drawText(yt, {
81576
- x: Ht,
81577
- y: Ft,
81578
- rotate: At,
81579
- size: et,
81580
- color: it,
81581
- lineHeight: ht * et,
81582
- font: pt,
81583
- opacity: Et
81575
+ const Pt = dt * it * (Ct.length - 1);
81576
+ ht === VERTICAL_ALIGN_BOTTOM ? It = At - Pt + Rt - Tt : ht === VERTICAL_ALIGN_MIDDLE && (It = (At - Pt - Dt + Rt) / 2 + Dt);
81577
+ }
81578
+ const yt = { x: bt + vt / 2, y: ct - mm2pt(nt.position.y) - At / 2 };
81579
+ Ct.forEach((Pt, kt) => {
81580
+ const jt = widthOfTextAtSize(Pt, lt, it, gt), Ht = dt * it * kt;
81581
+ let Ft = bt;
81582
+ ft === "center" ? Ft += (vt - jt) / 2 : ft === "right" && (Ft += vt - jt);
81583
+ let Lt = ct - mm2pt(nt.position.y) - It - Ht;
81584
+ if (xt.angle !== 0) {
81585
+ const Nt = rotatePoint({ x: Ft, y: Lt }, yt, xt.angle);
81586
+ Ft = Nt.x, Lt = Nt.y;
81587
+ }
81588
+ _e.drawText(Pt, {
81589
+ x: Ft,
81590
+ y: Lt,
81591
+ rotate: xt,
81592
+ size: it,
81593
+ color: ut,
81594
+ lineHeight: dt * it,
81595
+ font: mt,
81596
+ opacity: St
81584
81597
  });
81585
81598
  });
81586
81599
  }, DEFAULT_OPACITY = 1, HEX_COLOR_PATTERN = "^#(?:[A-Fa-f0-9]{3,4}|[A-Fa-f0-9]{6}|[A-Fa-f0-9]{8})$", UseDynamicFontSize = (a) => {