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.
@@ -1250,6 +1250,67 @@ function syncHeightKeepingCenter(element, newHeight) {
1250
1250
  y: element.y - deltaY
1251
1251
  });
1252
1252
  }
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
+
1264
+ // Safari can resolve document.fonts.load() (and fire loadingdone) before the canvas
1265
+ // actually renders with the new face, so a re-measure at that moment still gets
1266
+ // fallback-font widths. Poll until canvas text width differs from the plain fallback's
1267
+ // (i.e. the face is really in use), capped so system fonts / fonts that happen to match
1268
+ // the fallback's metrics can't loop forever. Resolves either way.
1269
+ var FONT_READY_MAX_FRAMES = 30;
1270
+ function waitForCanvasFont(fontSpec, family, sample, isCancelled) {
1271
+ return new Promise(function (resolve) {
1272
+ var ctx = document.createElement('canvas').getContext('2d');
1273
+ var text = sample || 'Hamburgefonstiv';
1274
+ var widthWith = function widthWith(font) {
1275
+ ctx.font = font;
1276
+ return ctx.measureText(text).width;
1277
+ };
1278
+ var frames = 0;
1279
+ var _check = function check() {
1280
+ if (isCancelled() || frames >= FONT_READY_MAX_FRAMES) return resolve();
1281
+ primeCanvasFont("".concat(fontSpec, " \"").concat(family, "\", serif"), text);
1282
+ var withFont = widthWith("".concat(fontSpec, " \"").concat(family, "\", serif"));
1283
+ var withFallbackSerif = widthWith("".concat(fontSpec, " serif"));
1284
+ var withFallbackSans = widthWith("".concat(fontSpec, " sans-serif"));
1285
+ if (withFont !== withFallbackSerif || withFont !== withFallbackSans) return resolve();
1286
+ frames += 1;
1287
+ requestAnimationFrame(_check);
1288
+ };
1289
+ _check();
1290
+ });
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
+ }
1253
1314
  var TextElementComponent = observer(function (_ref) {
1254
1315
  var _element$backgroundOp;
1255
1316
  var element = _ref.element;
@@ -1277,6 +1338,7 @@ var TextElementComponent = observer(function (_ref) {
1277
1338
  var _node$getLayer2;
1278
1339
  var node = textRef.current;
1279
1340
  if (!node) return;
1341
+ fontDebug('relayout:before', element, node);
1280
1342
 
1281
1343
  // CurvedText's sceneFunc re-measures glyphs via ctx.measureText on
1282
1344
  // every draw (no internal cached layout like Konva.Text has), so
@@ -1287,9 +1349,21 @@ var TextElementComponent = observer(function (_ref) {
1287
1349
  (_node$getLayer = node.getLayer()) === null || _node$getLayer === void 0 || _node$getLayer.batchDraw();
1288
1350
  return;
1289
1351
  }
1290
- var f = node.fontFamily();
1291
- node.fontFamily('serif');
1292
- 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);
1293
1367
  var h = node.height();
1294
1368
  if (h > 0) {
1295
1369
  setTextHeight(h);
@@ -1301,12 +1375,21 @@ var TextElementComponent = observer(function (_ref) {
1301
1375
  }
1302
1376
  (_node$getLayer2 = node.getLayer()) === null || _node$getLayer2 === void 0 || _node$getLayer2.batchDraw();
1303
1377
  };
1378
+ useEffect(function () {
1379
+ fontDebug('mount', element, textRef.current);
1380
+ }, []);
1304
1381
 
1305
1382
  // Any font finishing a load (covers fonts requested elsewhere, e.g. store.loadFont).
1306
1383
  useEffect(function () {
1384
+ // Two frames' delay: Safari's canvas can still be on the fallback at event time.
1307
1385
  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);
1386
+ fontDebug('loadingdone', element, textRef.current);
1387
+ requestAnimationFrame(function () {
1388
+ return requestAnimationFrame(function () {
1389
+ var _relayoutRef$current;
1390
+ return (_relayoutRef$current = relayoutRef.current) === null || _relayoutRef$current === void 0 ? void 0 : _relayoutRef$current.call(relayoutRef);
1391
+ });
1392
+ });
1310
1393
  };
1311
1394
  document.fonts.addEventListener('loadingdone', onFontLoaded);
1312
1395
  return function () {
@@ -1323,12 +1406,26 @@ var TextElementComponent = observer(function (_ref) {
1323
1406
  var cancelled = false;
1324
1407
  var style = element.fontStyle === 'italic' ? 'italic ' : '';
1325
1408
  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 () {
1409
+ var sample = element.text || element.placeholder || '';
1410
+ document.fonts.load("".concat(style).concat(weight, "16px \"").concat(element.fontFamily, "\""), sample).then(function () {
1411
+ return waitForCanvasFont("".concat(style).concat(weight, "16px"), element.fontFamily, sample, function () {
1412
+ return cancelled;
1413
+ });
1414
+ }).then(function () {
1327
1415
  var _relayoutRef$current2;
1328
1416
  if (!cancelled) (_relayoutRef$current2 = relayoutRef.current) === null || _relayoutRef$current2 === void 0 || _relayoutRef$current2.call(relayoutRef);
1329
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
+ });
1330
1426
  return function () {
1331
1427
  cancelled = true;
1428
+ retryTimers.forEach(clearTimeout);
1332
1429
  };
1333
1430
  }, [element.fontFamily, element.fontWeight, element.fontStyle]);
1334
1431