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