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/cjs/index.js CHANGED
@@ -11581,6 +11581,67 @@ function syncHeightKeepingCenter(element, newHeight) {
11581
11581
  y: element.y - deltaY
11582
11582
  });
11583
11583
  }
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
+
11595
+ // Safari can resolve document.fonts.load() (and fire loadingdone) before the canvas
11596
+ // actually renders with the new face, so a re-measure at that moment still gets
11597
+ // fallback-font widths. Poll until canvas text width differs from the plain fallback's
11598
+ // (i.e. the face is really in use), capped so system fonts / fonts that happen to match
11599
+ // the fallback's metrics can't loop forever. Resolves either way.
11600
+ var FONT_READY_MAX_FRAMES = 30;
11601
+ function waitForCanvasFont(fontSpec, family, sample, isCancelled) {
11602
+ return new Promise(function (resolve) {
11603
+ var ctx = document.createElement('canvas').getContext('2d');
11604
+ var text = sample || 'Hamburgefonstiv';
11605
+ var widthWith = function widthWith(font) {
11606
+ ctx.font = font;
11607
+ return ctx.measureText(text).width;
11608
+ };
11609
+ var frames = 0;
11610
+ var _check = function check() {
11611
+ if (isCancelled() || frames >= FONT_READY_MAX_FRAMES) return resolve();
11612
+ primeCanvasFont("".concat(fontSpec, " \"").concat(family, "\", serif"), text);
11613
+ var withFont = widthWith("".concat(fontSpec, " \"").concat(family, "\", serif"));
11614
+ var withFallbackSerif = widthWith("".concat(fontSpec, " serif"));
11615
+ var withFallbackSans = widthWith("".concat(fontSpec, " sans-serif"));
11616
+ if (withFont !== withFallbackSerif || withFont !== withFallbackSans) return resolve();
11617
+ frames += 1;
11618
+ requestAnimationFrame(_check);
11619
+ };
11620
+ _check();
11621
+ });
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
+ }
11584
11645
  var TextElementComponent = mobxReactLite.observer(function (_ref) {
11585
11646
  var _element$backgroundOp;
11586
11647
  var element = _ref.element;
@@ -11608,6 +11669,7 @@ var TextElementComponent = mobxReactLite.observer(function (_ref) {
11608
11669
  var _node$getLayer2;
11609
11670
  var node = textRef.current;
11610
11671
  if (!node) return;
11672
+ fontDebug('relayout:before', element, node);
11611
11673
 
11612
11674
  // CurvedText's sceneFunc re-measures glyphs via ctx.measureText on
11613
11675
  // every draw (no internal cached layout like Konva.Text has), so
@@ -11618,9 +11680,21 @@ var TextElementComponent = mobxReactLite.observer(function (_ref) {
11618
11680
  (_node$getLayer = node.getLayer()) === null || _node$getLayer === void 0 || _node$getLayer.batchDraw();
11619
11681
  return;
11620
11682
  }
11621
- var f = node.fontFamily();
11622
- node.fontFamily('serif');
11623
- 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);
11624
11698
  var h = node.height();
11625
11699
  if (h > 0) {
11626
11700
  setTextHeight(h);
@@ -11632,12 +11706,21 @@ var TextElementComponent = mobxReactLite.observer(function (_ref) {
11632
11706
  }
11633
11707
  (_node$getLayer2 = node.getLayer()) === null || _node$getLayer2 === void 0 || _node$getLayer2.batchDraw();
11634
11708
  };
11709
+ React.useEffect(function () {
11710
+ fontDebug('mount', element, textRef.current);
11711
+ }, []);
11635
11712
 
11636
11713
  // Any font finishing a load (covers fonts requested elsewhere, e.g. store.loadFont).
11637
11714
  React.useEffect(function () {
11715
+ // Two frames' delay: Safari's canvas can still be on the fallback at event time.
11638
11716
  var onFontLoaded = function onFontLoaded() {
11639
- var _relayoutRef$current;
11640
- return (_relayoutRef$current = relayoutRef.current) === null || _relayoutRef$current === void 0 ? void 0 : _relayoutRef$current.call(relayoutRef);
11717
+ fontDebug('loadingdone', element, textRef.current);
11718
+ requestAnimationFrame(function () {
11719
+ return requestAnimationFrame(function () {
11720
+ var _relayoutRef$current;
11721
+ return (_relayoutRef$current = relayoutRef.current) === null || _relayoutRef$current === void 0 ? void 0 : _relayoutRef$current.call(relayoutRef);
11722
+ });
11723
+ });
11641
11724
  };
11642
11725
  document.fonts.addEventListener('loadingdone', onFontLoaded);
11643
11726
  return function () {
@@ -11654,12 +11737,26 @@ var TextElementComponent = mobxReactLite.observer(function (_ref) {
11654
11737
  var cancelled = false;
11655
11738
  var style = element.fontStyle === 'italic' ? 'italic ' : '';
11656
11739
  var weight = element.fontWeight && element.fontWeight !== 'normal' ? "".concat(element.fontWeight, " ") : '';
11657
- document.fonts.load("".concat(style).concat(weight, "16px \"").concat(element.fontFamily, "\""), element.text || element.placeholder || '').then(function () {
11740
+ var sample = element.text || element.placeholder || '';
11741
+ document.fonts.load("".concat(style).concat(weight, "16px \"").concat(element.fontFamily, "\""), sample).then(function () {
11742
+ return waitForCanvasFont("".concat(style).concat(weight, "16px"), element.fontFamily, sample, function () {
11743
+ return cancelled;
11744
+ });
11745
+ }).then(function () {
11658
11746
  var _relayoutRef$current2;
11659
11747
  if (!cancelled) (_relayoutRef$current2 = relayoutRef.current) === null || _relayoutRef$current2 === void 0 || _relayoutRef$current2.call(relayoutRef);
11660
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
+ });
11661
11757
  return function () {
11662
11758
  cancelled = true;
11759
+ retryTimers.forEach(clearTimeout);
11663
11760
  };
11664
11761
  }, [element.fontFamily, element.fontWeight, element.fontStyle]);
11665
11762