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.
@@ -1254,6 +1254,67 @@ function syncHeightKeepingCenter(element, newHeight) {
1254
1254
  y: element.y - deltaY
1255
1255
  });
1256
1256
  }
1257
+
1258
+ // Safari doesn't make a webfont available to canvas (not even to measureText) until some
1259
+ // canvas has actually drawn text with it, so loading alone isn't enough: draw once on a
1260
+ // hidden canvas to activate the face, then measure.
1261
+ var _scratchCtx = null;
1262
+ function primeCanvasFont(font, text) {
1263
+ if (!_scratchCtx) _scratchCtx = document.createElement('canvas').getContext('2d');
1264
+ _scratchCtx.font = font;
1265
+ _scratchCtx.fillText(text || 'Hamburgefonstiv', 0, 0);
1266
+ }
1267
+
1268
+ // Safari can resolve document.fonts.load() (and fire loadingdone) before the canvas
1269
+ // actually renders with the new face, so a re-measure at that moment still gets
1270
+ // fallback-font widths. Poll until canvas text width differs from the plain fallback's
1271
+ // (i.e. the face is really in use), capped so system fonts / fonts that happen to match
1272
+ // the fallback's metrics can't loop forever. Resolves either way.
1273
+ var FONT_READY_MAX_FRAMES = 30;
1274
+ function waitForCanvasFont(fontSpec, family, sample, isCancelled) {
1275
+ return new Promise(function (resolve) {
1276
+ var ctx = document.createElement('canvas').getContext('2d');
1277
+ var text = sample || 'Hamburgefonstiv';
1278
+ var widthWith = function widthWith(font) {
1279
+ ctx.font = font;
1280
+ return ctx.measureText(text).width;
1281
+ };
1282
+ var frames = 0;
1283
+ var _check = function check() {
1284
+ if (isCancelled() || frames >= FONT_READY_MAX_FRAMES) return resolve();
1285
+ primeCanvasFont("".concat(fontSpec, " \"").concat(family, "\", serif"), text);
1286
+ var withFont = widthWith("".concat(fontSpec, " \"").concat(family, "\", serif"));
1287
+ var withFallbackSerif = widthWith("".concat(fontSpec, " serif"));
1288
+ var withFallbackSans = widthWith("".concat(fontSpec, " sans-serif"));
1289
+ if (withFont !== withFallbackSerif || withFont !== withFallbackSans) return resolve();
1290
+ frames += 1;
1291
+ requestAnimationFrame(_check);
1292
+ };
1293
+ _check();
1294
+ });
1295
+ }
1296
+
1297
+ // TEMPORARY font-timing diagnostics. Enable in the browser console with
1298
+ // window.__labellifeFontDebug = true
1299
+ // before loading a template, then copy the "[font-debug]" lines. Remove once Safari is sorted.
1300
+ function fontDebug(label, element, node) {
1301
+ if (typeof window === 'undefined' || !window.__labellifeFontDebug) return;
1302
+ try {
1303
+ var family = element.fontFamily;
1304
+ var faces = [];
1305
+ document.fonts.forEach(function (f) {
1306
+ if (f.family.replace(/["']/g, '') === family) faces.push("".concat(f.weight, "/").concat(f.status, "/").concat(f.unicodeRange.slice(0, 18)));
1307
+ });
1308
+ var lines = node && node.textArr ? node.textArr.map(function (l) {
1309
+ return Math.round(l.width);
1310
+ }) : null;
1311
+ // eslint-disable-next-line no-console
1312
+ 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 : '-'));
1313
+ } catch (e) {
1314
+ // eslint-disable-next-line no-console
1315
+ console.log('[font-debug] failed', e);
1316
+ }
1317
+ }
1257
1318
  var TextElementComponent = mobxReactLite.observer(function (_ref) {
1258
1319
  var _element$backgroundOp;
1259
1320
  var element = _ref.element;
@@ -1281,6 +1342,7 @@ var TextElementComponent = mobxReactLite.observer(function (_ref) {
1281
1342
  var _node$getLayer2;
1282
1343
  var node = textRef.current;
1283
1344
  if (!node) return;
1345
+ fontDebug('relayout:before', element, node);
1284
1346
 
1285
1347
  // CurvedText's sceneFunc re-measures glyphs via ctx.measureText on
1286
1348
  // every draw (no internal cached layout like Konva.Text has), so
@@ -1291,9 +1353,21 @@ var TextElementComponent = mobxReactLite.observer(function (_ref) {
1291
1353
  (_node$getLayer = node.getLayer()) === null || _node$getLayer === void 0 || _node$getLayer.batchDraw();
1292
1354
  return;
1293
1355
  }
1294
- var f = node.fontFamily();
1295
- node.fontFamily('serif');
1296
- node.fontFamily(f);
1356
+
1357
+ // Re-run Konva's own layout directly — what changing `align` does, which is the manual
1358
+ // workaround that fixes this in Safari. Toggling fontFamily is the fallback if the
1359
+ // private method is ever removed.
1360
+ if (typeof node._getContextFont === 'function') {
1361
+ primeCanvasFont(node._getContextFont(), node.text());
1362
+ }
1363
+ if (typeof node._setTextData === 'function') {
1364
+ node._setTextData();
1365
+ } else {
1366
+ var f = node.fontFamily();
1367
+ node.fontFamily('serif');
1368
+ node.fontFamily(f);
1369
+ }
1370
+ fontDebug('relayout:after', element, node);
1297
1371
  var h = node.height();
1298
1372
  if (h > 0) {
1299
1373
  setTextHeight(h);
@@ -1305,12 +1379,21 @@ var TextElementComponent = mobxReactLite.observer(function (_ref) {
1305
1379
  }
1306
1380
  (_node$getLayer2 = node.getLayer()) === null || _node$getLayer2 === void 0 || _node$getLayer2.batchDraw();
1307
1381
  };
1382
+ React.useEffect(function () {
1383
+ fontDebug('mount', element, textRef.current);
1384
+ }, []);
1308
1385
 
1309
1386
  // Any font finishing a load (covers fonts requested elsewhere, e.g. store.loadFont).
1310
1387
  React.useEffect(function () {
1388
+ // Two frames' delay: Safari's canvas can still be on the fallback at event time.
1311
1389
  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);
1390
+ fontDebug('loadingdone', element, textRef.current);
1391
+ requestAnimationFrame(function () {
1392
+ return requestAnimationFrame(function () {
1393
+ var _relayoutRef$current;
1394
+ return (_relayoutRef$current = relayoutRef.current) === null || _relayoutRef$current === void 0 ? void 0 : _relayoutRef$current.call(relayoutRef);
1395
+ });
1396
+ });
1314
1397
  };
1315
1398
  document.fonts.addEventListener('loadingdone', onFontLoaded);
1316
1399
  return function () {
@@ -1327,12 +1410,26 @@ var TextElementComponent = mobxReactLite.observer(function (_ref) {
1327
1410
  var cancelled = false;
1328
1411
  var style = element.fontStyle === 'italic' ? 'italic ' : '';
1329
1412
  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 () {
1413
+ var sample = element.text || element.placeholder || '';
1414
+ document.fonts.load("".concat(style).concat(weight, "16px \"").concat(element.fontFamily, "\""), sample).then(function () {
1415
+ return waitForCanvasFont("".concat(style).concat(weight, "16px"), element.fontFamily, sample, function () {
1416
+ return cancelled;
1417
+ });
1418
+ }).then(function () {
1331
1419
  var _relayoutRef$current2;
1332
1420
  if (!cancelled) (_relayoutRef$current2 = relayoutRef.current) === null || _relayoutRef$current2 === void 0 || _relayoutRef$current2.call(relayoutRef);
1333
1421
  })["catch"](function () {});
1422
+ // Safari can still be on fallback metrics after the checks above, and re-measuring is
1423
+ // idempotent, so repeat a few times while the page settles.
1424
+ var retryTimers = [300, 1000, 2500].map(function (ms) {
1425
+ return setTimeout(function () {
1426
+ var _relayoutRef$current3;
1427
+ if (!cancelled) (_relayoutRef$current3 = relayoutRef.current) === null || _relayoutRef$current3 === void 0 || _relayoutRef$current3.call(relayoutRef);
1428
+ }, ms);
1429
+ });
1334
1430
  return function () {
1335
1431
  cancelled = true;
1432
+ retryTimers.forEach(clearTimeout);
1336
1433
  };
1337
1434
  }, [element.fontFamily, element.fontWeight, element.fontStyle]);
1338
1435