@effing/canvas 0.24.7 → 0.24.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.
package/dist/index.js CHANGED
@@ -261,6 +261,12 @@ import { GlobalFonts } from "@napi-rs/canvas";
261
261
 
262
262
  // src/jsx/font-metrics.ts
263
263
  import { inflateSync } from "zlib";
264
+ function fontMetricsToPx(metrics, fontSize) {
265
+ return {
266
+ ascent: metrics.ascender / metrics.unitsPerEm * fontSize,
267
+ descent: -metrics.descender / metrics.unitsPerEm * fontSize
268
+ };
269
+ }
264
270
  var WOFF_SIGNATURE = 2001684038;
265
271
  function readWoffTable(view, entry) {
266
272
  const compressed = entry.compLength < entry.origLength;
@@ -474,8 +480,9 @@ function measureTrimMetrics(fontSize, fontFamily, fontWeight, fontStyle, lineHei
474
480
  let fontAscent;
475
481
  let fontDescent;
476
482
  if (fontMetrics) {
477
- fontAscent = fontMetrics.ascender / fontMetrics.unitsPerEm * fontSize;
478
- fontDescent = -fontMetrics.descender / fontMetrics.unitsPerEm * fontSize;
483
+ const px = fontMetricsToPx(fontMetrics, fontSize);
484
+ fontAscent = px.ascent;
485
+ fontDescent = px.descent;
479
486
  } else {
480
487
  fontAscent = refMetrics.fontBoundingBoxAscent ?? refMetrics.actualBoundingBoxAscent ?? fontSize * 0.8;
481
488
  fontDescent = refMetrics.fontBoundingBoxDescent ?? refMetrics.actualBoundingBoxDescent ?? fontSize * 0.2;
@@ -699,7 +706,8 @@ function layoutText(text, style, maxWidth, ctx, emojiEnabled) {
699
706
  let totalHeight = 0;
700
707
  let maxLineWidth = 0;
701
708
  const isAutoLineHeight = style.lineHeight === void 0 || style.lineHeight === "normal";
702
- let lastMetrics;
709
+ const fontPx = fontMetrics ? fontMetricsToPx(fontMetrics, fontSize) : null;
710
+ let lastPositioningDescent;
703
711
  for (let i = 0; i < lines.length; i++) {
704
712
  const line = lines[i];
705
713
  const lineWidth = measure(line);
@@ -717,14 +725,16 @@ function layoutText(text, style, maxWidth, ctx, emojiEnabled) {
717
725
  fontStyle,
718
726
  ctx
719
727
  );
720
- lastMetrics = metrics;
721
- const contentHeight = metrics.ascent + metrics.descent;
722
- let effectiveAscent = metrics.ascent;
723
- let effectiveDescent = metrics.descent;
728
+ const positioningAscent = fontPx?.ascent ?? metrics.ascent;
729
+ const positioningDescent = fontPx?.descent ?? metrics.descent;
730
+ lastPositioningDescent = positioningDescent;
731
+ const contentHeight = positioningAscent + positioningDescent;
732
+ let effectiveAscent = positioningAscent;
733
+ let effectiveDescent = positioningDescent;
724
734
  if (isAutoLineHeight && contentHeight > lineHeightPx) {
725
735
  const scale = lineHeightPx / contentHeight;
726
- effectiveAscent = metrics.ascent * scale;
727
- effectiveDescent = metrics.descent * scale;
736
+ effectiveAscent = positioningAscent * scale;
737
+ effectiveDescent = positioningDescent * scale;
728
738
  }
729
739
  const baselineY = totalHeight + (lineHeightPx + effectiveAscent - effectiveDescent) / 2;
730
740
  segments.push({
@@ -748,9 +758,9 @@ function layoutText(text, style, maxWidth, ctx, emojiEnabled) {
748
758
  totalHeight += lineHeightPx;
749
759
  maxLineWidth = Math.max(maxLineWidth, lineWidth);
750
760
  }
751
- if (isAutoLineHeight && lastMetrics && segments.length > 0) {
761
+ if (isAutoLineHeight && lastPositioningDescent !== void 0 && segments.length > 0) {
752
762
  const lastSeg = segments[segments.length - 1];
753
- const descentOverflow = lastSeg.y + lastMetrics.descent - totalHeight;
763
+ const descentOverflow = lastSeg.y + lastPositioningDescent - totalHeight;
754
764
  if (descentOverflow > 0) {
755
765
  totalHeight += descentOverflow;
756
766
  }
@@ -788,8 +798,8 @@ function layoutText(text, style, maxWidth, ctx, emojiEnabled) {
788
798
  function resolveLineHeight(lineHeight, fontSize, canvasMetrics, fontMetrics) {
789
799
  if (lineHeight === void 0 || lineHeight === "normal") {
790
800
  if (fontMetrics) {
791
- const { ascender, descender, unitsPerEm } = fontMetrics;
792
- return (ascender - descender) / unitsPerEm * fontSize;
801
+ const px = fontMetricsToPx(fontMetrics, fontSize);
802
+ return px.ascent + px.descent;
793
803
  }
794
804
  return canvasMetrics ? canvasMetrics.ascent + canvasMetrics.descent : fontSize * 1.2;
795
805
  }