@bendyline/squisq-formats 2.4.6 → 2.5.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.
@@ -6,6 +6,9 @@ import {
6
6
  fontAwesomeFaces,
7
7
  fontAwesomeGlyph
8
8
  } from "./chunk-USU6HTKB.js";
9
+ import {
10
+ FootnoteIndex
11
+ } from "./chunk-BXWNU4T5.js";
9
12
 
10
13
  // src/pdf/export.ts
11
14
  import { PDFDocument, StandardFonts, rgb, PDFString } from "pdf-lib";
@@ -450,6 +453,7 @@ async function createExportContext(pdfDoc, options, doc) {
450
453
  contentWidth: pageWidth - 2 * margin,
451
454
  bottomY: margin,
452
455
  colors: { text: colorText, heading: colorHeading, link: colorLink },
456
+ footnotes: new FootnoteIndex(doc),
453
457
  text: new WinAnsiTracker(),
454
458
  signal: options.signal
455
459
  };
@@ -464,19 +468,25 @@ function newPage(ctx) {
464
468
  ctx.page = page;
465
469
  ctx.y = ctx.pageHeight - ctx.margin;
466
470
  }
471
+ var VERT_ALIGN_SCALE = 0.72;
472
+ function vertAlignShift(kind, baseSize) {
473
+ return kind === "superscript" ? baseSize * 0.33 : baseSize * -0.14;
474
+ }
467
475
  function flattenInlines(nodes, ctx, state) {
468
476
  const spans = [];
469
477
  for (const node of nodes) {
470
478
  switch (node.type) {
471
479
  case "text": {
472
480
  const font = state.code ? state.bold ? ctx.fonts.monoBold : ctx.fonts.mono : pickFont(ctx, state.bold, state.italic);
481
+ const baseSize = state.code ? CODE_FONT_SIZE : ctx.fontSize;
473
482
  spans.push({
474
483
  text: ctx.text.clean(node.value),
475
484
  font,
476
- fontSize: state.code ? CODE_FONT_SIZE : ctx.fontSize,
485
+ fontSize: state.vertAlign ? baseSize * VERT_ALIGN_SCALE : baseSize,
477
486
  color: state.code ? COLOR_CODE_TEXT : state.color ?? ctx.colors.text,
478
487
  link: state.link,
479
- strikethrough: state.strikethrough
488
+ strikethrough: state.strikethrough,
489
+ ...state.vertAlign ? { baselineShift: vertAlignShift(state.vertAlign, baseSize) } : {}
480
490
  });
481
491
  break;
482
492
  }
@@ -498,6 +508,15 @@ function flattenInlines(nodes, ctx, state) {
498
508
  })
499
509
  );
500
510
  break;
511
+ case "superscript":
512
+ case "subscript":
513
+ spans.push(
514
+ ...flattenInlines(node.children, ctx, {
515
+ ...state,
516
+ vertAlign: node.type === "superscript" ? "superscript" : "subscript"
517
+ })
518
+ );
519
+ break;
501
520
  case "inlineCode": {
502
521
  spans.push({
503
522
  text: ctx.text.clean(node.value),
@@ -585,11 +604,13 @@ function flattenInlines(nodes, ctx, state) {
585
604
  break;
586
605
  case "footnoteReference": {
587
606
  const ref = node;
607
+ const marker = ctx.footnotes ? String(ctx.footnotes.cite(ref.identifier).number) : ref.label ?? ref.identifier;
588
608
  spans.push({
589
- text: ctx.text.clean(`[${ref.identifier}]`),
609
+ text: ctx.text.clean(marker),
590
610
  font: ctx.fonts.regular,
591
- fontSize: ctx.fontSize * 0.75,
592
- color: ctx.colors.link
611
+ fontSize: ctx.fontSize * VERT_ALIGN_SCALE,
612
+ color: ctx.colors.link,
613
+ baselineShift: vertAlignShift("superscript", ctx.fontSize)
593
614
  });
594
615
  break;
595
616
  }
@@ -626,9 +647,10 @@ function drawSpans(spans, ctx, availableWidth, x0) {
626
647
  ensureSpace(ctx, lineHeight);
627
648
  let x = x0;
628
649
  for (const span of line) {
650
+ const spanBaseline = ctx.y - span.fontSize + (span.baselineShift ?? 0);
629
651
  ctx.page.drawText(span.text, {
630
652
  x,
631
- y: ctx.y - span.fontSize,
653
+ y: spanBaseline,
632
654
  // pdf-lib y is baseline
633
655
  size: span.fontSize,
634
656
  font: span.font,
@@ -636,7 +658,7 @@ function drawSpans(spans, ctx, availableWidth, x0) {
636
658
  });
637
659
  const textWidth = span.font.widthOfTextAtSize(span.text, span.fontSize);
638
660
  if (span.link) {
639
- const baseline = ctx.y - span.fontSize;
661
+ const baseline = spanBaseline;
640
662
  ctx.page.drawLine({
641
663
  start: { x, y: baseline - 1 },
642
664
  end: { x: x + textWidth, y: baseline - 1 },
@@ -659,7 +681,7 @@ function drawSpans(spans, ctx, availableWidth, x0) {
659
681
  ctx.page.node.addAnnot(annotation);
660
682
  }
661
683
  if (span.strikethrough) {
662
- const midY = ctx.y - span.fontSize * 0.6;
684
+ const midY = spanBaseline + span.fontSize * 0.4;
663
685
  ctx.page.drawLine({
664
686
  start: { x, y: midY },
665
687
  end: { x: x + textWidth, y: midY },
@@ -1022,7 +1044,8 @@ function renderMathBlock(node, ctx, extraIndent) {
1022
1044
  ctx.y -= PARAGRAPH_SPACING;
1023
1045
  }
1024
1046
  function renderFootnoteDefinition(node, ctx, extraIndent) {
1025
- const label = ctx.text.clean(`[${node.identifier}]`);
1047
+ const number = ctx.footnotes?.numberFor(node.identifier);
1048
+ const label = ctx.text.clean(number !== void 0 ? `${number}.` : `[${node.identifier}]`);
1026
1049
  const lineH = ctx.fontSize * LINE_HEIGHT_FACTOR;
1027
1050
  ensureSpace(ctx, lineH);
1028
1051
  ctx.page.drawText(label, {