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