@pixldocs/canvas-renderer 0.5.61 → 0.5.62

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.cjs CHANGED
@@ -12561,7 +12561,7 @@ function PixldocsPreview(props) {
12561
12561
  !canvasSettled && /* @__PURE__ */ jsxRuntime.jsx("div", { style: { position: "absolute", inset: 0, display: "flex", alignItems: "center", justifyContent: "center", minHeight: 200 }, children: /* @__PURE__ */ jsxRuntime.jsx("div", { style: { color: "#888", fontSize: 14 }, children: "Loading preview..." }) })
12562
12562
  ] });
12563
12563
  }
12564
- const PACKAGE_VERSION = "0.5.61";
12564
+ const PACKAGE_VERSION = "0.5.62";
12565
12565
  let __underlineFixInstalled = false;
12566
12566
  function installUnderlineFix(fab) {
12567
12567
  var _a;
@@ -13322,6 +13322,7 @@ class PixldocsRenderer {
13322
13322
  { width: canvasWidth, height: canvasHeight },
13323
13323
  { cssOnly: false, backstoreOnly: false }
13324
13324
  );
13325
+ this.resyncTextMetricsForSvgExport(fabricInstance);
13325
13326
  const rawSvgString = fabricInstance.toSVG();
13326
13327
  const svgString = this.normalizeSvgDimensions(
13327
13328
  rawSvgString,
@@ -13432,6 +13433,59 @@ class PixldocsRenderer {
13432
13433
  }
13433
13434
  return null;
13434
13435
  }
13436
+ resyncTextMetricsForSvgExport(fabricInstance) {
13437
+ if (typeof document === "undefined" || !(fabricInstance == null ? void 0 : fabricInstance.getObjects)) return;
13438
+ const ctx = document.createElement("canvas").getContext("2d");
13439
+ if (!ctx) return;
13440
+ const syncTextbox = (textbox) => {
13441
+ const tb = textbox;
13442
+ const rawLines = tb._textLines ?? textbox.textLines ?? [];
13443
+ if (!Array.isArray(rawLines) || rawLines.length === 0) return;
13444
+ const nextCharBounds = [];
13445
+ const nextLineWidths = [];
13446
+ rawLines.forEach((rawLine, lineIndex) => {
13447
+ const graphemes = Array.isArray(rawLine) ? rawLine.map((part) => String(part ?? "")) : Array.from(String(rawLine ?? ""));
13448
+ const bounds = [];
13449
+ let left = 0;
13450
+ graphemes.forEach((grapheme, charIndex) => {
13451
+ var _a, _b, _c, _d, _e, _f;
13452
+ const fontSize = Number(((_a = tb.getValueOfPropertyAt) == null ? void 0 : _a.call(tb, lineIndex, charIndex, "fontSize")) ?? textbox.fontSize ?? 16);
13453
+ const fontStyle = String(((_b = tb.getValueOfPropertyAt) == null ? void 0 : _b.call(tb, lineIndex, charIndex, "fontStyle")) ?? textbox.fontStyle ?? "normal");
13454
+ const fontWeight = String(((_c = tb.getValueOfPropertyAt) == null ? void 0 : _c.call(tb, lineIndex, charIndex, "fontWeight")) ?? textbox.fontWeight ?? "400");
13455
+ const fontFamily = String(((_d = tb.getValueOfPropertyAt) == null ? void 0 : _d.call(tb, lineIndex, charIndex, "fontFamily")) ?? textbox.fontFamily ?? "sans-serif");
13456
+ const charSpacing = Number(((_e = tb.getValueOfPropertyAt) == null ? void 0 : _e.call(tb, lineIndex, charIndex, "charSpacing")) ?? textbox.charSpacing ?? 0);
13457
+ const deltaY = Number(((_f = tb.getValueOfPropertyAt) == null ? void 0 : _f.call(tb, lineIndex, charIndex, "deltaY")) ?? 0);
13458
+ ctx.font = `${fontStyle} normal ${fontWeight} ${fontSize}px ${fontFamily}`;
13459
+ const charWidth = Math.max(0, ctx.measureText(grapheme).width);
13460
+ let kernedWidth = charWidth;
13461
+ const previous = charIndex > 0 ? graphemes[charIndex - 1] : "";
13462
+ if (previous) {
13463
+ const pairWidth = ctx.measureText(previous + grapheme).width;
13464
+ const previousWidth = ctx.measureText(previous).width;
13465
+ if (Number.isFinite(pairWidth) && Number.isFinite(previousWidth)) {
13466
+ kernedWidth = Math.max(0, pairWidth - previousWidth);
13467
+ }
13468
+ }
13469
+ if (charIndex < graphemes.length - 1 && charSpacing) {
13470
+ kernedWidth += charSpacing / 1e3 * fontSize;
13471
+ }
13472
+ bounds[charIndex] = { left, width: charWidth, kernedWidth, height: fontSize, deltaY };
13473
+ left += kernedWidth;
13474
+ });
13475
+ bounds[graphemes.length] = { left, width: 0, kernedWidth: 0, height: Number(textbox.fontSize ?? 16), deltaY: 0 };
13476
+ nextCharBounds[lineIndex] = bounds;
13477
+ nextLineWidths[lineIndex] = Math.max(0, left);
13478
+ });
13479
+ tb.__charBounds = nextCharBounds;
13480
+ tb.__lineWidths = nextLineWidths;
13481
+ textbox.dirty = true;
13482
+ };
13483
+ const visit = (obj) => {
13484
+ if (obj instanceof fabric__namespace.Textbox) syncTextbox(obj);
13485
+ else if (obj instanceof fabric__namespace.Group) obj.getObjects().forEach(visit);
13486
+ };
13487
+ fabricInstance.getObjects().forEach(visit);
13488
+ }
13435
13489
  async waitForStableTextMetrics(container, config) {
13436
13490
  var _a, _b, _c;
13437
13491
  if (typeof document !== "undefined") {