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