@pdfme/ui 6.1.0-dev.2 → 6.1.1-dev.4

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.js CHANGED
@@ -9453,7 +9453,7 @@ function parseExpressionAt(input, pos, options) {
9453
9453
  }
9454
9454
  //#endregion
9455
9455
  //#region ../common/dist/index.js
9456
- var PDFME_VERSION = "6.1.0";
9456
+ var PDFME_VERSION = "6.1.1";
9457
9457
  var PT_TO_PX_RATIO = 1.333;
9458
9458
  var PT_TO_MM_RATIO = .3528;
9459
9459
  var MM_TO_PT_RATIO = 2.8346;
@@ -98478,7 +98478,7 @@ $d636bc798e7178db$export$36b2f24e97d43be($21ee218f84ac7f32$export$2e2bcd8739ae03
98478
98478
  $d636bc798e7178db$export$36b2f24e97d43be($cd5853a56c68fec7$export$2e2bcd8739ae039);
98479
98479
  $d636bc798e7178db$export$36b2f24e97d43be($05f49f930186144e$export$2e2bcd8739ae039);
98480
98480
  //#endregion
98481
- //#region ../schemas/dist/helper-M_MmV_d5.js
98481
+ //#region ../schemas/dist/helper-6FilIoVM.js
98482
98482
  var ALIGN_LEFT = "left";
98483
98483
  var ALIGN_CENTER = "center";
98484
98484
  var ALIGN_RIGHT = "right";
@@ -98598,10 +98598,27 @@ var heightOfFontAtSize = (fontKitFont, fontSize) => {
98598
98598
  var calculateCharacterSpacing = (textContent, textCharacterSpacing) => {
98599
98599
  return (textContent.length - 1) * textCharacterSpacing;
98600
98600
  };
98601
+ var TEXT_WIDTH_CACHE_LIMIT = 5e3;
98602
+ var textWidthCache = /* @__PURE__ */ new WeakMap();
98603
+ var getTextWidthCache = (fontKitFont) => {
98604
+ let cache = textWidthCache.get(fontKitFont);
98605
+ if (!cache) {
98606
+ cache = /* @__PURE__ */ new Map();
98607
+ textWidthCache.set(fontKitFont, cache);
98608
+ }
98609
+ return cache;
98610
+ };
98601
98611
  var widthOfTextAtSize = (text, fontKitFont, fontSize, characterSpacing) => {
98612
+ const cache = getTextWidthCache(fontKitFont);
98613
+ const cacheKey = `${fontSize}\0${characterSpacing}\0${text}`;
98614
+ const cachedWidth = cache.get(cacheKey);
98615
+ if (cachedWidth !== void 0) return cachedWidth;
98602
98616
  const { glyphs } = fontKitFont.layout(text);
98603
98617
  const scale = 1e3 / fontKitFont.unitsPerEm;
98604
- return glyphs.reduce((totalWidth, glyph) => totalWidth + glyph.advanceWidth * scale, 0) * (fontSize / 1e3) + calculateCharacterSpacing(text, characterSpacing);
98618
+ const width = glyphs.reduce((totalWidth, glyph) => totalWidth + glyph.advanceWidth * scale, 0) * (fontSize / 1e3) + calculateCharacterSpacing(text, characterSpacing);
98619
+ if (cache.size >= TEXT_WIDTH_CACHE_LIMIT) cache.clear();
98620
+ cache.set(cacheKey, width);
98621
+ return width;
98605
98622
  };
98606
98623
  var getFallbackFont = (font) => {
98607
98624
  return font[getFallbackFontName(font)];
@@ -98721,10 +98738,15 @@ var splitTextToSize = (arg) => {
98721
98738
  return lines;
98722
98739
  };
98723
98740
  var isFirefox = () => navigator.userAgent.toLowerCase().indexOf("firefox") > -1;
98741
+ var wordSegmenter;
98742
+ var getWordSegmenter = () => {
98743
+ wordSegmenter ?? (wordSegmenter = new Intl.Segmenter(void 0, { granularity: "word" }));
98744
+ return wordSegmenter;
98745
+ };
98724
98746
  var getSplittedLinesBySegmenter = (line, calcValues) => {
98725
98747
  if (line.trim() === "") return [""];
98726
98748
  const { font, fontSize, characterSpacing, boxWidthInPt } = calcValues;
98727
- const iterator = new Intl.Segmenter(void 0, { granularity: "word" }).segment(line.trimEnd())[Symbol.iterator]();
98749
+ const iterator = getWordSegmenter().segment(line.trimEnd())[Symbol.iterator]();
98728
98750
  let lines = [];
98729
98751
  let lineCounter = 0;
98730
98752
  let currentTextSize = 0;
@@ -98827,7 +98849,7 @@ var filterEndJP = (lines) => {
98827
98849
  } else return filtered;
98828
98850
  };
98829
98851
  //#endregion
98830
- //#region ../schemas/dist/dynamicTemplate-DmuRoTw4.js
98852
+ //#region ../schemas/dist/dynamicTemplate-DslOH4FZ.js
98831
98853
  function _typeof$18(o) {
98832
98854
  "@babel/helpers - typeof";
98833
98855
  return _typeof$18 = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(o) {
@@ -99677,7 +99699,7 @@ var Underline = [["path", { d: "M6 4v6a6 6 0 0 0 12 0V4" }], ["line", {
99677
99699
  y2: "20"
99678
99700
  }]];
99679
99701
  //#endregion
99680
- //#region ../schemas/dist/builtins-CWHhKSVA.js
99702
+ //#region ../schemas/dist/builtins-CFqn6U5J.js
99681
99703
  var MARKDOWN_ESCAPABLE_CHARS = new Set([
99682
99704
  "\\",
99683
99705
  "*",
@@ -100095,11 +100117,16 @@ var calculateDynamicRichTextFontSize = async (arg) => {
100095
100117
  var getSyntheticBoldWidth = (run, fontSize) => run.syntheticBold ? fontSize * SYNTHETIC_BOLD_OFFSET_RATIO * 2 : 0;
100096
100118
  var getSyntheticItalicWidth = (run, fontSize) => run.syntheticItalic ? heightOfFontAtSize(run.fontKitFont, fontSize) * Math.tan(12 * Math.PI / 180) : 0;
100097
100119
  var getRunWidth = (run, fontSize, characterSpacing) => widthOfTextAtSize(run.text, run.fontKitFont, fontSize, characterSpacing) + getSyntheticBoldWidth(run, fontSize) + getSyntheticItalicWidth(run, fontSize);
100098
- var getPdfFont = (run, pdfFontObj) => {
100120
+ var getPdfFontFromObj = (run, pdfFontObj) => {
100099
100121
  const pdfFont = pdfFontObj[run.fontName];
100100
100122
  if (!pdfFont) throw new Error(`[@pdfme/schemas] Missing embedded font "${run.fontName}".`);
100101
100123
  return pdfFont;
100102
100124
  };
100125
+ var embedFontsForRuns = async (runs, embedPdfFont) => {
100126
+ const fontNames = Array.from(new Set(runs.map((run) => run.fontName)));
100127
+ const pdfFonts = await Promise.all(fontNames.map(async (fontName) => [fontName, await embedPdfFont(fontName)]));
100128
+ return Object.fromEntries(pdfFonts);
100129
+ };
100103
100130
  var drawDecorationLine = (arg) => {
100104
100131
  const { page, x, y, width, rotate, pivotPoint, fontSize, color, opacity } = arg;
100105
100132
  if (width <= 0) return;
@@ -100180,7 +100207,7 @@ var drawRun = (arg) => {
100180
100207
  }
100181
100208
  };
100182
100209
  var renderInlineMarkdownText = async (arg) => {
100183
- const { value, schema, font, pdfFontObj, fontKitFont, page, pdfLib, _cache, colorType, fontSize, color, alignment, verticalAlignment, lineHeight, characterSpacing, x, width, height, pageHeight, pivotPoint, rotate, opacity } = arg;
100210
+ const { value, schema, font, embedPdfFont, fontKitFont, page, pdfLib, _cache, colorType, fontSize, color, alignment, verticalAlignment, lineHeight, characterSpacing, x, width, height, pageHeight, pivotPoint, rotate, opacity } = arg;
100184
100211
  const lines = layoutRichTextLines({
100185
100212
  runs: await resolveRichTextRuns({
100186
100213
  runs: parseInlineMarkdown(value),
@@ -100192,6 +100219,7 @@ var renderInlineMarkdownText = async (arg) => {
100192
100219
  characterSpacing,
100193
100220
  boxWidthInPt: width
100194
100221
  });
100222
+ const pdfFontObj = await embedFontsForRuns(lines.flatMap((line) => line.runs), embedPdfFont);
100195
100223
  const firstLineTextHeight = heightOfFontAtSize(fontKitFont, fontSize);
100196
100224
  const descent = getFontDescentInPt(fontKitFont, fontSize);
100197
100225
  const halfLineHeightAdjustment = lineHeight === 0 ? 0 : (lineHeight - 1) * fontSize / 2;
@@ -100249,7 +100277,7 @@ var renderInlineMarkdownText = async (arg) => {
100249
100277
  page,
100250
100278
  pdfLib,
100251
100279
  run,
100252
- pdfFont: getPdfFont(run, pdfFontObj),
100280
+ pdfFont: getPdfFontFromObj(run, pdfFontObj),
100253
100281
  x: currentX,
100254
100282
  y: yLine,
100255
100283
  rotate,
@@ -100266,17 +100294,29 @@ var renderInlineMarkdownText = async (arg) => {
100266
100294
  }, xLine);
100267
100295
  });
100268
100296
  };
100269
- var embedAndGetFontObj = async (arg) => {
100270
- const { pdfDoc, font, _cache } = arg;
100271
- if (_cache.has(pdfDoc)) return _cache.get(pdfDoc);
100272
- const fontValues = await Promise.all(Object.values(font).map(async (v) => {
100273
- let fontData = v.data;
100297
+ var PDF_FONT_CACHE_KEY = "text-pdf-font-cache";
100298
+ var getPdfFontCache = (_cache) => {
100299
+ let pdfFontCache = _cache.get(PDF_FONT_CACHE_KEY);
100300
+ if (!pdfFontCache) {
100301
+ pdfFontCache = {};
100302
+ _cache.set(PDF_FONT_CACHE_KEY, pdfFontCache);
100303
+ }
100304
+ return pdfFontCache;
100305
+ };
100306
+ var embedAndGetFont = (arg) => {
100307
+ const { pdfDoc, font, fontName, _cache } = arg;
100308
+ const pdfFontCache = getPdfFontCache(_cache);
100309
+ const cachedFont = pdfFontCache[fontName];
100310
+ if (cachedFont) return cachedFont;
100311
+ const fontValue = font[fontName];
100312
+ if (!fontValue) return Promise.reject(/* @__PURE__ */ new Error(`[@pdfme/schemas] Font "${fontName}" is not found.`));
100313
+ const pdfFontPromise = (async () => {
100314
+ let fontData = fontValue.data;
100274
100315
  if (typeof fontData === "string" && fontData.startsWith("http")) fontData = await fetchRemoteFontData(fontData);
100275
- return pdfDoc.embedFont(fontData, { subset: typeof v.subset === "undefined" ? true : v.subset });
100276
- }));
100277
- const fontObj = Object.keys(font).reduce((acc, cur, i) => Object.assign(acc, { [cur]: fontValues[i] }), {});
100278
- _cache.set(pdfDoc, fontObj);
100279
- return fontObj;
100316
+ return pdfDoc.embedFont(fontData, { subset: typeof fontValue.subset === "undefined" ? true : fontValue.subset });
100317
+ })();
100318
+ pdfFontCache[fontName] = pdfFontPromise;
100319
+ return pdfFontPromise;
100280
100320
  };
100281
100321
  var getFontProp = ({ value, fontKitFont, schema, colorType, fontSize: resolvedFontSize }) => {
100282
100322
  const fontSize = resolvedFontSize ?? (schema.dynamicFontSize ? calculateDynamicFontSize({
@@ -100294,16 +100334,24 @@ var getFontProp = ({ value, fontKitFont, schema, colorType, fontSize: resolvedFo
100294
100334
  color
100295
100335
  };
100296
100336
  };
100337
+ var graphemeSegmenter;
100338
+ var getGraphemeSegmenter = () => {
100339
+ graphemeSegmenter ?? (graphemeSegmenter = new Intl.Segmenter(void 0, { granularity: "grapheme" }));
100340
+ return graphemeSegmenter;
100341
+ };
100297
100342
  var pdfRender = async (arg) => {
100298
100343
  const { value, pdfDoc, pdfLib, page, options, schema, _cache } = arg;
100299
100344
  if (!value) return;
100300
100345
  const { font = getDefaultFont(), colorType } = options;
100301
- const [pdfFontObj, fontKitFont] = await Promise.all([embedAndGetFontObj({
100346
+ const fontName = schema.fontName ? schema.fontName : getFallbackFontName(font);
100347
+ const enableInlineMarkdown = isInlineMarkdownTextSchema(schema);
100348
+ const pdfFontValuePromise = enableInlineMarkdown ? void 0 : embedAndGetFont({
100302
100349
  pdfDoc,
100303
100350
  font,
100351
+ fontName,
100304
100352
  _cache
100305
- }), getFontKitFont(schema.fontName, font, _cache)]);
100306
- const enableInlineMarkdown = isInlineMarkdownTextSchema(schema);
100353
+ });
100354
+ const fontKitFont = await getFontKitFont(schema.fontName, font, _cache);
100307
100355
  const { fontSize, color, alignment, verticalAlignment, lineHeight, characterSpacing } = getFontProp({
100308
100356
  value: enableInlineMarkdown ? stripInlineMarkdown(value) : value,
100309
100357
  fontKitFont,
@@ -100316,8 +100364,6 @@ var pdfRender = async (arg) => {
100316
100364
  _cache
100317
100365
  }) : void 0
100318
100366
  });
100319
- const fontName = schema.fontName ? schema.fontName : getFallbackFontName(font);
100320
- const pdfFontValue = pdfFontObj && pdfFontObj[fontName];
100321
100367
  const pageHeight = page.getHeight();
100322
100368
  const { width, height, rotate, position: { x, y }, opacity } = convertForPdfLayoutProps({
100323
100369
  schema,
@@ -100357,7 +100403,12 @@ var pdfRender = async (arg) => {
100357
100403
  value,
100358
100404
  schema,
100359
100405
  font,
100360
- pdfFontObj,
100406
+ embedPdfFont: (fontName) => embedAndGetFont({
100407
+ pdfDoc,
100408
+ font,
100409
+ fontName,
100410
+ _cache
100411
+ }),
100361
100412
  fontKitFont,
100362
100413
  page,
100363
100414
  pdfLib,
@@ -100379,6 +100430,8 @@ var pdfRender = async (arg) => {
100379
100430
  });
100380
100431
  return;
100381
100432
  }
100433
+ if (!pdfFontValuePromise) throw new Error("[@pdfme/schemas] Failed to prepare PDF font for text rendering.");
100434
+ const pdfFontValue = await pdfFontValuePromise;
100382
100435
  const firstLineTextHeight = heightOfFontAtSize(fontKitFont, fontSize);
100383
100436
  const descent = getFontDescentInPt(fontKitFont, fontSize);
100384
100437
  const halfLineHeightAdjustment = lineHeight === 0 ? 0 : (lineHeight - 1) * fontSize / 2;
@@ -100389,6 +100442,8 @@ var pdfRender = async (arg) => {
100389
100442
  fontKitFont,
100390
100443
  boxWidthInPt: width
100391
100444
  });
100445
+ const needsTextWidth = alignment !== "left" || Boolean(schema.strikethrough || schema.underline);
100446
+ const needsTextHeight = Boolean(schema.strikethrough || schema.underline);
100392
100447
  let yOffset = 0;
100393
100448
  if (verticalAlignment === "top") yOffset = firstLineTextHeight + halfLineHeightAdjustment;
100394
100449
  else {
@@ -100396,11 +100451,10 @@ var pdfRender = async (arg) => {
100396
100451
  if (verticalAlignment === "bottom") yOffset = height - otherLinesHeight + descent - halfLineHeightAdjustment;
100397
100452
  else if (verticalAlignment === "middle") yOffset = (height - otherLinesHeight - firstLineTextHeight + descent) / 2 + firstLineTextHeight;
100398
100453
  }
100399
- const segmenter = new Intl.Segmenter(void 0, { granularity: "grapheme" });
100400
100454
  lines.forEach((line, rowIndex) => {
100401
100455
  const trimmed = line.replace("\n", "");
100402
- const textWidth = widthOfTextAtSize(trimmed, fontKitFont, fontSize, characterSpacing);
100403
- const textHeight = heightOfFontAtSize(fontKitFont, fontSize);
100456
+ const textWidth = needsTextWidth ? widthOfTextAtSize(trimmed, fontKitFont, fontSize, characterSpacing) : 0;
100457
+ const textHeight = needsTextHeight ? heightOfFontAtSize(fontKitFont, fontSize) : 0;
100404
100458
  const rowYOffset = lineHeight * fontSize * rowIndex;
100405
100459
  if (line === "") line = "\r\n";
100406
100460
  let xLine = x;
@@ -100451,7 +100505,7 @@ var pdfRender = async (arg) => {
100451
100505
  }
100452
100506
  let spacing = characterSpacing;
100453
100507
  if (alignment === "justify" && line.slice(-1) !== "\n") {
100454
- const iterator = segmenter.segment(trimmed)[Symbol.iterator]();
100508
+ const iterator = getGraphemeSegmenter().segment(trimmed)[Symbol.iterator]();
100455
100509
  const len = Array.from(iterator).length;
100456
100510
  spacing += (width - textWidth) / len;
100457
100511
  }
@@ -235040,7 +235094,7 @@ var Designer = class extends BaseUIClass {
235040
235094
  }
235041
235095
  };
235042
235096
  //#endregion
235043
- //#region ../schemas/dist/lists-B6dmgpkS.js
235097
+ //#region ../schemas/dist/lists-BmAAx0lx.js
235044
235098
  var normalizeListItems = (value) => {
235045
235099
  if (Array.isArray(value)) return value.map((item) => String(item));
235046
235100
  if (typeof value !== "string") return value == null ? [] : [String(value)];