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.
@@ -1250,6 +1250,34 @@ function syncHeightKeepingCenter(element, newHeight) {
1250
1250
  y: element.y - deltaY
1251
1251
  });
1252
1252
  }
1253
+
1254
+ // Safari can resolve document.fonts.load() (and fire loadingdone) before the canvas
1255
+ // actually renders with the new face, so a re-measure at that moment still gets
1256
+ // fallback-font widths. Poll until canvas text width differs from the plain fallback's
1257
+ // (i.e. the face is really in use), capped so system fonts / fonts that happen to match
1258
+ // the fallback's metrics can't loop forever. Resolves either way.
1259
+ var FONT_READY_MAX_FRAMES = 30;
1260
+ function waitForCanvasFont(fontSpec, family, sample, isCancelled) {
1261
+ return new Promise(function (resolve) {
1262
+ var ctx = document.createElement('canvas').getContext('2d');
1263
+ var text = sample || 'Hamburgefonstiv';
1264
+ var widthWith = function widthWith(font) {
1265
+ ctx.font = font;
1266
+ return ctx.measureText(text).width;
1267
+ };
1268
+ var frames = 0;
1269
+ var _check = function check() {
1270
+ if (isCancelled() || frames >= FONT_READY_MAX_FRAMES) return resolve();
1271
+ var withFont = widthWith("".concat(fontSpec, " \"").concat(family, "\", serif"));
1272
+ var withFallbackSerif = widthWith("".concat(fontSpec, " serif"));
1273
+ var withFallbackSans = widthWith("".concat(fontSpec, " sans-serif"));
1274
+ if (withFont !== withFallbackSerif || withFont !== withFallbackSans) return resolve();
1275
+ frames += 1;
1276
+ requestAnimationFrame(_check);
1277
+ };
1278
+ _check();
1279
+ });
1280
+ }
1253
1281
  var TextElementComponent = observer(function (_ref) {
1254
1282
  var _element$backgroundOp;
1255
1283
  var element = _ref.element;
@@ -1304,9 +1332,14 @@ var TextElementComponent = observer(function (_ref) {
1304
1332
 
1305
1333
  // Any font finishing a load (covers fonts requested elsewhere, e.g. store.loadFont).
1306
1334
  useEffect(function () {
1335
+ // Two frames' delay: Safari's canvas can still be on the fallback at event time.
1307
1336
  var onFontLoaded = function onFontLoaded() {
1308
- var _relayoutRef$current;
1309
- return (_relayoutRef$current = relayoutRef.current) === null || _relayoutRef$current === void 0 ? void 0 : _relayoutRef$current.call(relayoutRef);
1337
+ return requestAnimationFrame(function () {
1338
+ return requestAnimationFrame(function () {
1339
+ var _relayoutRef$current;
1340
+ return (_relayoutRef$current = relayoutRef.current) === null || _relayoutRef$current === void 0 ? void 0 : _relayoutRef$current.call(relayoutRef);
1341
+ });
1342
+ });
1310
1343
  };
1311
1344
  document.fonts.addEventListener('loadingdone', onFontLoaded);
1312
1345
  return function () {
@@ -1323,7 +1356,12 @@ var TextElementComponent = observer(function (_ref) {
1323
1356
  var cancelled = false;
1324
1357
  var style = element.fontStyle === 'italic' ? 'italic ' : '';
1325
1358
  var weight = element.fontWeight && element.fontWeight !== 'normal' ? "".concat(element.fontWeight, " ") : '';
1326
- document.fonts.load("".concat(style).concat(weight, "16px \"").concat(element.fontFamily, "\""), element.text || element.placeholder || '').then(function () {
1359
+ var sample = element.text || element.placeholder || '';
1360
+ document.fonts.load("".concat(style).concat(weight, "16px \"").concat(element.fontFamily, "\""), sample).then(function () {
1361
+ return waitForCanvasFont("".concat(style).concat(weight, "16px"), element.fontFamily, sample, function () {
1362
+ return cancelled;
1363
+ });
1364
+ }).then(function () {
1327
1365
  var _relayoutRef$current2;
1328
1366
  if (!cancelled) (_relayoutRef$current2 = relayoutRef.current) === null || _relayoutRef$current2 === void 0 || _relayoutRef$current2.call(relayoutRef);
1329
1367
  })["catch"](function () {});