labellife-design-tool 2.5.6 → 2.5.7

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,34 @@ function syncHeightKeepingCenter(element, newHeight) {
11579
11579
  y: element.y - deltaY
11580
11580
  });
11581
11581
  }
11582
+
11583
+ // Safari can resolve document.fonts.load() (and fire loadingdone) before the canvas
11584
+ // actually renders with the new face, so a re-measure at that moment still gets
11585
+ // fallback-font widths. Poll until canvas text width differs from the plain fallback's
11586
+ // (i.e. the face is really in use), capped so system fonts / fonts that happen to match
11587
+ // the fallback's metrics can't loop forever. Resolves either way.
11588
+ var FONT_READY_MAX_FRAMES = 30;
11589
+ function waitForCanvasFont(fontSpec, family, sample, isCancelled) {
11590
+ return new Promise(function (resolve) {
11591
+ var ctx = document.createElement('canvas').getContext('2d');
11592
+ var text = sample || 'Hamburgefonstiv';
11593
+ var widthWith = function widthWith(font) {
11594
+ ctx.font = font;
11595
+ return ctx.measureText(text).width;
11596
+ };
11597
+ var frames = 0;
11598
+ var _check = function check() {
11599
+ if (isCancelled() || frames >= FONT_READY_MAX_FRAMES) return resolve();
11600
+ var withFont = widthWith("".concat(fontSpec, " \"").concat(family, "\", serif"));
11601
+ var withFallbackSerif = widthWith("".concat(fontSpec, " serif"));
11602
+ var withFallbackSans = widthWith("".concat(fontSpec, " sans-serif"));
11603
+ if (withFont !== withFallbackSerif || withFont !== withFallbackSans) return resolve();
11604
+ frames += 1;
11605
+ requestAnimationFrame(_check);
11606
+ };
11607
+ _check();
11608
+ });
11609
+ }
11582
11610
  var TextElementComponent = observer(function (_ref) {
11583
11611
  var _element$backgroundOp;
11584
11612
  var element = _ref.element;
@@ -11633,9 +11661,14 @@ var TextElementComponent = observer(function (_ref) {
11633
11661
 
11634
11662
  // Any font finishing a load (covers fonts requested elsewhere, e.g. store.loadFont).
11635
11663
  useEffect(function () {
11664
+ // Two frames' delay: Safari's canvas can still be on the fallback at event time.
11636
11665
  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);
11666
+ return requestAnimationFrame(function () {
11667
+ return requestAnimationFrame(function () {
11668
+ var _relayoutRef$current;
11669
+ return (_relayoutRef$current = relayoutRef.current) === null || _relayoutRef$current === void 0 ? void 0 : _relayoutRef$current.call(relayoutRef);
11670
+ });
11671
+ });
11639
11672
  };
11640
11673
  document.fonts.addEventListener('loadingdone', onFontLoaded);
11641
11674
  return function () {
@@ -11652,7 +11685,12 @@ var TextElementComponent = observer(function (_ref) {
11652
11685
  var cancelled = false;
11653
11686
  var style = element.fontStyle === 'italic' ? 'italic ' : '';
11654
11687
  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 () {
11688
+ var sample = element.text || element.placeholder || '';
11689
+ document.fonts.load("".concat(style).concat(weight, "16px \"").concat(element.fontFamily, "\""), sample).then(function () {
11690
+ return waitForCanvasFont("".concat(style).concat(weight, "16px"), element.fontFamily, sample, function () {
11691
+ return cancelled;
11692
+ });
11693
+ }).then(function () {
11656
11694
  var _relayoutRef$current2;
11657
11695
  if (!cancelled) (_relayoutRef$current2 = relayoutRef.current) === null || _relayoutRef$current2 === void 0 || _relayoutRef$current2.call(relayoutRef);
11658
11696
  })["catch"](function () {});