@bendyline/squisq-formats 2.7.6 → 2.8.1

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.
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  convert,
3
3
  defaultRegistry
4
- } from "./chunk-ONBGBSKA.js";
4
+ } from "./chunk-X6WFQDZD.js";
5
5
  import {
6
6
  ConversionError
7
7
  } from "./chunk-KXOZMWBS.js";
@@ -799,6 +799,13 @@ function flattenInlines(nodes, ctx, state) {
799
799
  }
800
800
  return spans;
801
801
  }
802
+ var STANDARD_FONT_NAMES = new Set(Object.values(StandardFonts));
803
+ function drawnWidth(font, text, size) {
804
+ if (!STANDARD_FONT_NAMES.has(font.name)) return font.widthOfTextAtSize(text, size);
805
+ let width = 0;
806
+ for (const character of text) width += font.widthOfTextAtSize(character, size);
807
+ return width;
808
+ }
802
809
  function textSpans(ctx, text, font, style) {
803
810
  return ctx.runs.split(text, font).map((run) => ({ ...style, text: run.text, font: run.font }));
804
811
  }
@@ -813,7 +820,7 @@ function drawTextLine(ctx, text, font, at) {
813
820
  font: run.font,
814
821
  color: rgb(at.color.r, at.color.g, at.color.b)
815
822
  });
816
- x += run.font.widthOfTextAtSize(run.text, at.size);
823
+ x += drawnWidth(run.font, run.text, at.size);
817
824
  }
818
825
  }
819
826
  function pickFont(ctx, bold, italic) {
@@ -839,7 +846,7 @@ function drawSpans(spans, ctx, availableWidth, x0) {
839
846
  font: span.font,
840
847
  color: rgb(span.color.r, span.color.g, span.color.b)
841
848
  });
842
- const textWidth = span.font.widthOfTextAtSize(span.text, span.fontSize);
849
+ const textWidth = drawnWidth(span.font, span.text, span.fontSize);
843
850
  if (span.link) {
844
851
  const baseline = spanBaseline;
845
852
  ctx.page.drawLine({
@@ -890,7 +897,7 @@ function wrapSpans(spans, maxWidth) {
890
897
  }
891
898
  const words = splitIntoWords(span.text);
892
899
  for (const word of words) {
893
- const wordWidth = span.font.widthOfTextAtSize(word, span.fontSize);
900
+ const wordWidth = drawnWidth(span.font, word, span.fontSize);
894
901
  if (lineWidth + wordWidth > maxWidth && currentLine.length > 0 && word.trim().length > 0) {
895
902
  lines.push(currentLine);
896
903
  currentLine = [];
@@ -898,7 +905,7 @@ function wrapSpans(spans, maxWidth) {
898
905
  }
899
906
  const trimmedWord = currentLine.length === 0 ? word.replace(/^\s+/, "") : word;
900
907
  if (trimmedWord.length > 0) {
901
- const tw = span.font.widthOfTextAtSize(trimmedWord, span.fontSize);
908
+ const tw = drawnWidth(span.font, trimmedWord, span.fontSize);
902
909
  currentLine.push({ ...span, text: trimmedWord });
903
910
  lineWidth += tw;
904
911
  }
@@ -1075,7 +1082,7 @@ function renderListItem(item, ctx, extraIndent, depth, ordered, counter) {
1075
1082
  const indent = extraIndent + depth * LIST_INDENT;
1076
1083
  const bullet = ordered ? `${counter}.` : BULLET_CHAR;
1077
1084
  const bulletFont = ctx.fonts.regular;
1078
- const bulletWidth = bulletFont.widthOfTextAtSize(bullet + " ", ctx.fontSize);
1085
+ const bulletWidth = drawnWidth(bulletFont, bullet + " ", ctx.fontSize);
1079
1086
  const lineHeight = ctx.fontSize * LINE_HEIGHT_FACTOR;
1080
1087
  ensureSpace(ctx, lineHeight);
1081
1088
  ctx.page.drawText(bullet, {
@@ -1197,7 +1204,7 @@ function renderTable(node, ctx, extraIndent) {
1197
1204
  font: span.font,
1198
1205
  color: rgb(span.color.r, span.color.g, span.color.b)
1199
1206
  });
1200
- lx += span.font.widthOfTextAtSize(span.text, span.fontSize);
1207
+ lx += drawnWidth(span.font, span.text, span.fontSize);
1201
1208
  }
1202
1209
  ctx.y -= getLineHeight(line);
1203
1210
  }