labellife-design-tool 2.5.7 → 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/cjs/index.js CHANGED
@@ -11582,6 +11582,16 @@ function syncHeightKeepingCenter(element, newHeight) {
11582
11582
  });
11583
11583
  }
11584
11584
 
11585
+ // Safari doesn't make a webfont available to canvas (not even to measureText) until some
11586
+ // canvas has actually drawn text with it, so loading alone isn't enough: draw once on a
11587
+ // hidden canvas to activate the face, then measure.
11588
+ var _scratchCtx = null;
11589
+ function primeCanvasFont(font, text) {
11590
+ if (!_scratchCtx) _scratchCtx = document.createElement('canvas').getContext('2d');
11591
+ _scratchCtx.font = font;
11592
+ _scratchCtx.fillText(text || 'Hamburgefonstiv', 0, 0);
11593
+ }
11594
+
11585
11595
  // Safari can resolve document.fonts.load() (and fire loadingdone) before the canvas
11586
11596
  // actually renders with the new face, so a re-measure at that moment still gets
11587
11597
  // fallback-font widths. Poll until canvas text width differs from the plain fallback's
@@ -11599,6 +11609,7 @@ function waitForCanvasFont(fontSpec, family, sample, isCancelled) {
11599
11609
  var frames = 0;
11600
11610
  var _check = function check() {
11601
11611
  if (isCancelled() || frames >= FONT_READY_MAX_FRAMES) return resolve();
11612
+ primeCanvasFont("".concat(fontSpec, " \"").concat(family, "\", serif"), text);
11602
11613
  var withFont = widthWith("".concat(fontSpec, " \"").concat(family, "\", serif"));
11603
11614
  var withFallbackSerif = widthWith("".concat(fontSpec, " serif"));
11604
11615
  var withFallbackSans = widthWith("".concat(fontSpec, " sans-serif"));
@@ -11609,6 +11620,28 @@ function waitForCanvasFont(fontSpec, family, sample, isCancelled) {
11609
11620
  _check();
11610
11621
  });
11611
11622
  }
11623
+
11624
+ // TEMPORARY font-timing diagnostics. Enable in the browser console with
11625
+ // window.__labellifeFontDebug = true
11626
+ // before loading a template, then copy the "[font-debug]" lines. Remove once Safari is sorted.
11627
+ function fontDebug(label, element, node) {
11628
+ if (typeof window === 'undefined' || !window.__labellifeFontDebug) return;
11629
+ try {
11630
+ var family = element.fontFamily;
11631
+ var faces = [];
11632
+ document.fonts.forEach(function (f) {
11633
+ if (f.family.replace(/["']/g, '') === family) faces.push("".concat(f.weight, "/").concat(f.status, "/").concat(f.unicodeRange.slice(0, 18)));
11634
+ });
11635
+ var lines = node && node.textArr ? node.textArr.map(function (l) {
11636
+ return Math.round(l.width);
11637
+ }) : null;
11638
+ // eslint-disable-next-line no-console
11639
+ 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 : '-'));
11640
+ } catch (e) {
11641
+ // eslint-disable-next-line no-console
11642
+ console.log('[font-debug] failed', e);
11643
+ }
11644
+ }
11612
11645
  var TextElementComponent = mobxReactLite.observer(function (_ref) {
11613
11646
  var _element$backgroundOp;
11614
11647
  var element = _ref.element;
@@ -11636,6 +11669,7 @@ var TextElementComponent = mobxReactLite.observer(function (_ref) {
11636
11669
  var _node$getLayer2;
11637
11670
  var node = textRef.current;
11638
11671
  if (!node) return;
11672
+ fontDebug('relayout:before', element, node);
11639
11673
 
11640
11674
  // CurvedText's sceneFunc re-measures glyphs via ctx.measureText on
11641
11675
  // every draw (no internal cached layout like Konva.Text has), so
@@ -11646,9 +11680,21 @@ var TextElementComponent = mobxReactLite.observer(function (_ref) {
11646
11680
  (_node$getLayer = node.getLayer()) === null || _node$getLayer === void 0 || _node$getLayer.batchDraw();
11647
11681
  return;
11648
11682
  }
11649
- var f = node.fontFamily();
11650
- node.fontFamily('serif');
11651
- node.fontFamily(f);
11683
+
11684
+ // Re-run Konva's own layout directly — what changing `align` does, which is the manual
11685
+ // workaround that fixes this in Safari. Toggling fontFamily is the fallback if the
11686
+ // private method is ever removed.
11687
+ if (typeof node._getContextFont === 'function') {
11688
+ primeCanvasFont(node._getContextFont(), node.text());
11689
+ }
11690
+ if (typeof node._setTextData === 'function') {
11691
+ node._setTextData();
11692
+ } else {
11693
+ var f = node.fontFamily();
11694
+ node.fontFamily('serif');
11695
+ node.fontFamily(f);
11696
+ }
11697
+ fontDebug('relayout:after', element, node);
11652
11698
  var h = node.height();
11653
11699
  if (h > 0) {
11654
11700
  setTextHeight(h);
@@ -11660,12 +11706,16 @@ var TextElementComponent = mobxReactLite.observer(function (_ref) {
11660
11706
  }
11661
11707
  (_node$getLayer2 = node.getLayer()) === null || _node$getLayer2 === void 0 || _node$getLayer2.batchDraw();
11662
11708
  };
11709
+ React.useEffect(function () {
11710
+ fontDebug('mount', element, textRef.current);
11711
+ }, []);
11663
11712
 
11664
11713
  // Any font finishing a load (covers fonts requested elsewhere, e.g. store.loadFont).
11665
11714
  React.useEffect(function () {
11666
11715
  // Two frames' delay: Safari's canvas can still be on the fallback at event time.
11667
11716
  var onFontLoaded = function onFontLoaded() {
11668
- return requestAnimationFrame(function () {
11717
+ fontDebug('loadingdone', element, textRef.current);
11718
+ requestAnimationFrame(function () {
11669
11719
  return requestAnimationFrame(function () {
11670
11720
  var _relayoutRef$current;
11671
11721
  return (_relayoutRef$current = relayoutRef.current) === null || _relayoutRef$current === void 0 ? void 0 : _relayoutRef$current.call(relayoutRef);
@@ -11696,8 +11746,17 @@ var TextElementComponent = mobxReactLite.observer(function (_ref) {
11696
11746
  var _relayoutRef$current2;
11697
11747
  if (!cancelled) (_relayoutRef$current2 = relayoutRef.current) === null || _relayoutRef$current2 === void 0 || _relayoutRef$current2.call(relayoutRef);
11698
11748
  })["catch"](function () {});
11749
+ // Safari can still be on fallback metrics after the checks above, and re-measuring is
11750
+ // idempotent, so repeat a few times while the page settles.
11751
+ var retryTimers = [300, 1000, 2500].map(function (ms) {
11752
+ return setTimeout(function () {
11753
+ var _relayoutRef$current3;
11754
+ if (!cancelled) (_relayoutRef$current3 = relayoutRef.current) === null || _relayoutRef$current3 === void 0 || _relayoutRef$current3.call(relayoutRef);
11755
+ }, ms);
11756
+ });
11699
11757
  return function () {
11700
11758
  cancelled = true;
11759
+ retryTimers.forEach(clearTimeout);
11701
11760
  };
11702
11761
  }, [element.fontFamily, element.fontWeight, element.fontStyle]);
11703
11762