labellife-design-tool 2.5.6 → 2.5.8

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/esm/index.js CHANGED
@@ -11579,6 +11579,67 @@ function syncHeightKeepingCenter(element, newHeight) {
11579
11579
  y: element.y - deltaY
11580
11580
  });
11581
11581
  }
11582
+
11583
+ // Safari doesn't make a webfont available to canvas (not even to measureText) until some
11584
+ // canvas has actually drawn text with it, so loading alone isn't enough: draw once on a
11585
+ // hidden canvas to activate the face, then measure.
11586
+ var _scratchCtx = null;
11587
+ function primeCanvasFont(font, text) {
11588
+ if (!_scratchCtx) _scratchCtx = document.createElement('canvas').getContext('2d');
11589
+ _scratchCtx.font = font;
11590
+ _scratchCtx.fillText(text || 'Hamburgefonstiv', 0, 0);
11591
+ }
11592
+
11593
+ // Safari can resolve document.fonts.load() (and fire loadingdone) before the canvas
11594
+ // actually renders with the new face, so a re-measure at that moment still gets
11595
+ // fallback-font widths. Poll until canvas text width differs from the plain fallback's
11596
+ // (i.e. the face is really in use), capped so system fonts / fonts that happen to match
11597
+ // the fallback's metrics can't loop forever. Resolves either way.
11598
+ var FONT_READY_MAX_FRAMES = 30;
11599
+ function waitForCanvasFont(fontSpec, family, sample, isCancelled) {
11600
+ return new Promise(function (resolve) {
11601
+ var ctx = document.createElement('canvas').getContext('2d');
11602
+ var text = sample || 'Hamburgefonstiv';
11603
+ var widthWith = function widthWith(font) {
11604
+ ctx.font = font;
11605
+ return ctx.measureText(text).width;
11606
+ };
11607
+ var frames = 0;
11608
+ var _check = function check() {
11609
+ if (isCancelled() || frames >= FONT_READY_MAX_FRAMES) return resolve();
11610
+ primeCanvasFont("".concat(fontSpec, " \"").concat(family, "\", serif"), text);
11611
+ var withFont = widthWith("".concat(fontSpec, " \"").concat(family, "\", serif"));
11612
+ var withFallbackSerif = widthWith("".concat(fontSpec, " serif"));
11613
+ var withFallbackSans = widthWith("".concat(fontSpec, " sans-serif"));
11614
+ if (withFont !== withFallbackSerif || withFont !== withFallbackSans) return resolve();
11615
+ frames += 1;
11616
+ requestAnimationFrame(_check);
11617
+ };
11618
+ _check();
11619
+ });
11620
+ }
11621
+
11622
+ // TEMPORARY font-timing diagnostics. Enable in the browser console with
11623
+ // window.__labellifeFontDebug = true
11624
+ // before loading a template, then copy the "[font-debug]" lines. Remove once Safari is sorted.
11625
+ function fontDebug(label, element, node) {
11626
+ if (typeof window === 'undefined' || !window.__labellifeFontDebug) return;
11627
+ try {
11628
+ var family = element.fontFamily;
11629
+ var faces = [];
11630
+ document.fonts.forEach(function (f) {
11631
+ if (f.family.replace(/["']/g, '') === family) faces.push("".concat(f.weight, "/").concat(f.status, "/").concat(f.unicodeRange.slice(0, 18)));
11632
+ });
11633
+ var lines = node && node.textArr ? node.textArr.map(function (l) {
11634
+ return Math.round(l.width);
11635
+ }) : null;
11636
+ // eslint-disable-next-line no-console
11637
+ console.log("[font-debug] t=".concat(Math.round(performance.now()), " ").concat(label, " family=\"").concat(family, "\" ") + "check=".concat(document.fonts.check("16px \"".concat(family, "\"")), " faces=").concat(JSON.stringify(faces), " ") + "boxW=".concat(Math.round(element.width), " nodeW=").concat(node ? Math.round(node.width()) : '-', " ") + "lineWidths=".concat(JSON.stringify(lines), " lines=").concat(lines ? lines.length : '-'));
11638
+ } catch (e) {
11639
+ // eslint-disable-next-line no-console
11640
+ console.log('[font-debug] failed', e);
11641
+ }
11642
+ }
11582
11643
  var TextElementComponent = observer(function (_ref) {
11583
11644
  var _element$backgroundOp;
11584
11645
  var element = _ref.element;
@@ -11606,6 +11667,7 @@ var TextElementComponent = observer(function (_ref) {
11606
11667
  var _node$getLayer2;
11607
11668
  var node = textRef.current;
11608
11669
  if (!node) return;
11670
+ fontDebug('relayout:before', element, node);
11609
11671
 
11610
11672
  // CurvedText's sceneFunc re-measures glyphs via ctx.measureText on
11611
11673
  // every draw (no internal cached layout like Konva.Text has), so
@@ -11616,9 +11678,21 @@ var TextElementComponent = observer(function (_ref) {
11616
11678
  (_node$getLayer = node.getLayer()) === null || _node$getLayer === void 0 || _node$getLayer.batchDraw();
11617
11679
  return;
11618
11680
  }
11619
- var f = node.fontFamily();
11620
- node.fontFamily('serif');
11621
- node.fontFamily(f);
11681
+
11682
+ // Re-run Konva's own layout directly — what changing `align` does, which is the manual
11683
+ // workaround that fixes this in Safari. Toggling fontFamily is the fallback if the
11684
+ // private method is ever removed.
11685
+ if (typeof node._getContextFont === 'function') {
11686
+ primeCanvasFont(node._getContextFont(), node.text());
11687
+ }
11688
+ if (typeof node._setTextData === 'function') {
11689
+ node._setTextData();
11690
+ } else {
11691
+ var f = node.fontFamily();
11692
+ node.fontFamily('serif');
11693
+ node.fontFamily(f);
11694
+ }
11695
+ fontDebug('relayout:after', element, node);
11622
11696
  var h = node.height();
11623
11697
  if (h > 0) {
11624
11698
  setTextHeight(h);
@@ -11630,12 +11704,21 @@ var TextElementComponent = observer(function (_ref) {
11630
11704
  }
11631
11705
  (_node$getLayer2 = node.getLayer()) === null || _node$getLayer2 === void 0 || _node$getLayer2.batchDraw();
11632
11706
  };
11707
+ useEffect(function () {
11708
+ fontDebug('mount', element, textRef.current);
11709
+ }, []);
11633
11710
 
11634
11711
  // Any font finishing a load (covers fonts requested elsewhere, e.g. store.loadFont).
11635
11712
  useEffect(function () {
11713
+ // Two frames' delay: Safari's canvas can still be on the fallback at event time.
11636
11714
  var onFontLoaded = function onFontLoaded() {
11637
- var _relayoutRef$current;
11638
- return (_relayoutRef$current = relayoutRef.current) === null || _relayoutRef$current === void 0 ? void 0 : _relayoutRef$current.call(relayoutRef);
11715
+ fontDebug('loadingdone', element, textRef.current);
11716
+ requestAnimationFrame(function () {
11717
+ return requestAnimationFrame(function () {
11718
+ var _relayoutRef$current;
11719
+ return (_relayoutRef$current = relayoutRef.current) === null || _relayoutRef$current === void 0 ? void 0 : _relayoutRef$current.call(relayoutRef);
11720
+ });
11721
+ });
11639
11722
  };
11640
11723
  document.fonts.addEventListener('loadingdone', onFontLoaded);
11641
11724
  return function () {
@@ -11652,12 +11735,26 @@ var TextElementComponent = observer(function (_ref) {
11652
11735
  var cancelled = false;
11653
11736
  var style = element.fontStyle === 'italic' ? 'italic ' : '';
11654
11737
  var weight = element.fontWeight && element.fontWeight !== 'normal' ? "".concat(element.fontWeight, " ") : '';
11655
- document.fonts.load("".concat(style).concat(weight, "16px \"").concat(element.fontFamily, "\""), element.text || element.placeholder || '').then(function () {
11738
+ var sample = element.text || element.placeholder || '';
11739
+ document.fonts.load("".concat(style).concat(weight, "16px \"").concat(element.fontFamily, "\""), sample).then(function () {
11740
+ return waitForCanvasFont("".concat(style).concat(weight, "16px"), element.fontFamily, sample, function () {
11741
+ return cancelled;
11742
+ });
11743
+ }).then(function () {
11656
11744
  var _relayoutRef$current2;
11657
11745
  if (!cancelled) (_relayoutRef$current2 = relayoutRef.current) === null || _relayoutRef$current2 === void 0 || _relayoutRef$current2.call(relayoutRef);
11658
11746
  })["catch"](function () {});
11747
+ // Safari can still be on fallback metrics after the checks above, and re-measuring is
11748
+ // idempotent, so repeat a few times while the page settles.
11749
+ var retryTimers = [300, 1000, 2500].map(function (ms) {
11750
+ return setTimeout(function () {
11751
+ var _relayoutRef$current3;
11752
+ if (!cancelled) (_relayoutRef$current3 = relayoutRef.current) === null || _relayoutRef$current3 === void 0 || _relayoutRef$current3.call(relayoutRef);
11753
+ }, ms);
11754
+ });
11659
11755
  return function () {
11660
11756
  cancelled = true;
11757
+ retryTimers.forEach(clearTimeout);
11661
11758
  };
11662
11759
  }, [element.fontFamily, element.fontWeight, element.fontStyle]);
11663
11760