@dtducas/wh-forge-viewer 3.0.0-beta.6 → 3.0.0-beta.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.esm.js CHANGED
@@ -70646,6 +70646,7 @@ var SvgToPdfConverter = /*#__PURE__*/function () {
70646
70646
  }, {
70647
70647
  key: "drawEllipse",
70648
70648
  value: function drawEllipse(element, pdfPage, options, transform) {
70649
+ var _options$pageRotation;
70649
70650
  var cx = parseFloat(element.getAttribute('cx') || '0');
70650
70651
  var cy = parseFloat(element.getAttribute('cy') || '0');
70651
70652
  var rx = parseFloat(element.getAttribute('rx') || '0');
@@ -70657,16 +70658,24 @@ var SvgToPdfConverter = /*#__PURE__*/function () {
70657
70658
  _this$transformPoint34 = _slicedToArray(_this$transformPoint33, 2),
70658
70659
  pdfCx = _this$transformPoint34[0],
70659
70660
  pdfCy = _this$transformPoint34[1];
70660
- // Scale radii (apply SVG transform scale if present)
70661
- var svgScaleX = transform ? transform.scaleX : 1;
70662
- var svgScaleY = transform ? transform.scaleY : 1;
70661
+ // Scale radii (apply SVG transform scale if present). Use the *displayed*
70662
+ // page dims (swapped for 90/270) so radii are not stretched on rotated pages,
70663
+ // and swap rx/ry under a quarter rotation since the axes swap in user space.
70664
+ var svgScaleX = Math.abs(transform ? transform.scaleX : 1);
70665
+ var svgScaleY = Math.abs(transform ? transform.scaleY : 1);
70663
70666
  var mbE = options.modelBounds;
70664
70667
  var mapWE = mbE ? mbE.maxX - mbE.minX : options.svgViewBox.width;
70665
70668
  var mapHE = mbE ? mbE.maxY - mbE.minY : options.svgViewBox.height;
70666
- var scaleX = options.pageWidth / mapWE;
70667
- var scaleY = options.pageHeight / mapHE;
70668
- var pdfRx = rx * svgScaleX * scaleX;
70669
- var pdfRy = ry * svgScaleY * scaleY;
70669
+ var rotE = (((_options$pageRotation = options.pageRotation) !== null && _options$pageRotation !== void 0 ? _options$pageRotation : 0) % 360 + 360) % 360;
70670
+ var quarterE = rotE === 90 || rotE === 270;
70671
+ var dispWE = quarterE ? options.pageHeight : options.pageWidth;
70672
+ var dispHE = quarterE ? options.pageWidth : options.pageHeight;
70673
+ var scaleX = dispWE / mapWE;
70674
+ var scaleY = dispHE / mapHE;
70675
+ var rdx = rx * svgScaleX * scaleX;
70676
+ var rdy = ry * svgScaleY * scaleY;
70677
+ var pdfRx = quarterE ? rdy : rdx;
70678
+ var pdfRy = quarterE ? rdx : rdy;
70670
70679
  // Draw fill
70671
70680
  if (style.fillColor && style.fillOpacity > 0) {
70672
70681
  pdfPage.drawEllipse({
@@ -70698,6 +70707,7 @@ var SvgToPdfConverter = /*#__PURE__*/function () {
70698
70707
  }, {
70699
70708
  key: "drawCircle",
70700
70709
  value: function drawCircle(element, pdfPage, options, transform) {
70710
+ var _options$pageRotation2;
70701
70711
  var cx = parseFloat(element.getAttribute('cx') || '0');
70702
70712
  var cy = parseFloat(element.getAttribute('cy') || '0');
70703
70713
  var r = parseFloat(element.getAttribute('r') || '0');
@@ -70708,12 +70718,18 @@ var SvgToPdfConverter = /*#__PURE__*/function () {
70708
70718
  _this$transformPoint36 = _slicedToArray(_this$transformPoint35, 2),
70709
70719
  pdfCx = _this$transformPoint36[0],
70710
70720
  pdfCy = _this$transformPoint36[1];
70711
- // Scale radius (apply SVG transform scale if present)
70712
- var svgScale = transform ? Math.min(transform.scaleX, transform.scaleY) : 1;
70721
+ // Scale radius (apply SVG transform scale if present). Use displayed page
70722
+ // dims (swapped for 90/270) and absolute scale (the SVG Y-flip makes scaleY
70723
+ // negative, which must not flip the radius sign).
70724
+ var svgScale = transform ? Math.min(Math.abs(transform.scaleX), Math.abs(transform.scaleY)) : 1;
70713
70725
  var mbC = options.modelBounds;
70714
70726
  var mapWC = mbC ? mbC.maxX - mbC.minX : options.svgViewBox.width;
70715
70727
  var mapHC = mbC ? mbC.maxY - mbC.minY : options.svgViewBox.height;
70716
- var scale = svgScale * Math.min(options.pageWidth / mapWC, options.pageHeight / mapHC);
70728
+ var rotC = (((_options$pageRotation2 = options.pageRotation) !== null && _options$pageRotation2 !== void 0 ? _options$pageRotation2 : 0) % 360 + 360) % 360;
70729
+ var quarterC = rotC === 90 || rotC === 270;
70730
+ var dispWC = quarterC ? options.pageHeight : options.pageWidth;
70731
+ var dispHC = quarterC ? options.pageWidth : options.pageHeight;
70732
+ var scale = svgScale * Math.min(dispWC / mapWC, dispHC / mapHC);
70717
70733
  var pdfR = r * scale;
70718
70734
  // Draw fill
70719
70735
  if (style.fillColor && style.fillOpacity > 0) {
@@ -70905,8 +70921,9 @@ var SvgToPdfConverter = /*#__PURE__*/function () {
70905
70921
  var _element$textContent,
70906
70922
  _element$getAttribute,
70907
70923
  _element$getAttribute2,
70908
- _this = this;
70909
- var text, style, inlineStyles, x, y, fontSizeStr, fontSize, _this$transformPoint43, _this$transformPoint44, pdfX, pdfY, svgFontScale, fontScale, pdfFontSize, rawBoxH, pdfBoxH, maxPdfFs, textColor, opacity, mb, mapW, mapH, textAnchor, anchorOffsetX, drawLine, tspans, linesDrawn, lineYWorld, _iterator4, _step4, _tspan$textContent$tr, _tspan$textContent, tspan, lineText, tspanXAttr, lineXWorld, tspanYAttr, dyAttr, _this$transformPoint45, _this$transformPoint46, linePdfX, linePdfY, _element$getAttribute3, _t3;
70924
+ _this = this,
70925
+ _options$pageRotation3;
70926
+ var text, style, inlineStyles, x, y, fontSizeStr, fontSize, _this$transformPoint43, _this$transformPoint44, pdfX, pdfY, svgFontScale, fontScale, pdfFontSize, rawBoxH, pdfBoxH, maxPdfFs, textColor, opacity, mb, mapW, mapH, textAnchor, anchorOffsetX, pageRot, rotateOffset, drawLine, tspans, linesDrawn, lineYWorld, _iterator4, _step4, _tspan$textContent$tr, _tspan$textContent, tspan, lineText, tspanXAttr, lineXWorld, tspanYAttr, dyAttr, _this$transformPoint45, _this$transformPoint46, linePdfX, linePdfY, _element$getAttribute3, _t3;
70910
70927
  return _regenerator().w(function (_context3) {
70911
70928
  while (1) switch (_context3.p = _context3.n) {
70912
70929
  case 0:
@@ -70960,9 +70977,25 @@ var SvgToPdfConverter = /*#__PURE__*/function () {
70960
70977
  } catch (_unused) {
70961
70978
  return 0;
70962
70979
  }
70980
+ }; // Page /Rotate: glyphs must be rotated to read upright on the displayed page,
70981
+ // and the anchor (text-anchor X) + baseline (down) offsets — which are in the
70982
+ // displayed/text frame — must be rotated into the unrotated mediabox space.
70983
+ pageRot = (((_options$pageRotation3 = options.pageRotation) !== null && _options$pageRotation3 !== void 0 ? _options$pageRotation3 : 0) % 360 + 360) % 360; // Rotate a displayed-frame offset vector into mediabox space (same linear part
70984
+ // as the point mapping in transformPoint).
70985
+ rotateOffset = function rotateOffset(ox, oy) {
70986
+ switch (pageRot) {
70987
+ case 90:
70988
+ return [-oy, ox];
70989
+ case 180:
70990
+ return [-ox, -oy];
70991
+ case 270:
70992
+ return [oy, -ox];
70993
+ default:
70994
+ return [ox, oy];
70995
+ }
70963
70996
  }; // ── Helper: draw one text line with fallback ──────────────────────────────
70997
+ // drawX/drawY is the line anchor already mapped into mediabox space.
70964
70998
  drawLine = function drawLine(lineText, drawX, drawY) {
70965
- var baselinePdfY = drawY - pdfFontSize * 0.8;
70966
70999
  // Try primary font, then Helvetica per-line so export never stops.
70967
71000
  for (var _i3 = 0, _arr = [[_this.currentFont, false], [_this.helveticaFont, true]]; _i3 < _arr.length; _i3++) {
70968
71001
  var _arr$_i = _slicedToArray(_arr[_i3], 2),
@@ -70970,14 +71003,20 @@ var SvgToPdfConverter = /*#__PURE__*/function () {
70970
71003
  isHelvetica = _arr$_i[1];
70971
71004
  var safe = isHelvetica ? _this.safeText(lineText).replace(/[^\x20-\x7E]/g, '?') : _this.safeText(lineText);
70972
71005
  var xOff = anchorOffsetX(safe, font);
71006
+ // Offset in the displayed/text frame: along text (xOff), baseline down (-0.8em).
71007
+ var _rotateOffset = rotateOffset(xOff, -pdfFontSize * 0.8),
71008
+ _rotateOffset2 = _slicedToArray(_rotateOffset, 2),
71009
+ offX = _rotateOffset2[0],
71010
+ offY = _rotateOffset2[1];
70973
71011
  try {
70974
71012
  pdfPage.drawText(safe, {
70975
- x: drawX + xOff,
70976
- y: baselinePdfY,
71013
+ x: drawX + offX,
71014
+ y: drawY + offY,
70977
71015
  size: pdfFontSize,
70978
71016
  font: font,
70979
71017
  color: rgb(textColor.r, textColor.g, textColor.b),
70980
- opacity: opacity
71018
+ opacity: opacity,
71019
+ rotate: degrees(pageRot)
70981
71020
  });
70982
71021
  if (isHelvetica) console.warn('[SvgToPdfConverter] drawText: used Helvetica fallback for line:', lineText);
70983
71022
  return true;
@@ -71544,6 +71583,7 @@ var SvgToPdfConverter = /*#__PURE__*/function () {
71544
71583
  }, {
71545
71584
  key: "transformPoint",
71546
71585
  value: function transformPoint(x, y, options, svgTransform) {
71586
+ var _options$pageRotation4;
71547
71587
  // Apply SVG transform (translate, rotate, scale) first
71548
71588
  var svgX = x;
71549
71589
  var svgY = y;
@@ -71573,12 +71613,44 @@ var SvgToPdfConverter = /*#__PURE__*/function () {
71573
71613
  var mapY = mb ? mb.minY : options.svgViewBox.y;
71574
71614
  var mapW = mb ? mb.maxX - mb.minX : options.svgViewBox.width;
71575
71615
  var mapH = mb ? mb.maxY - mb.minY : options.svgViewBox.height;
71576
- var scaleX = options.pageWidth / mapW;
71577
- var scaleY = options.pageHeight / mapH;
71578
- var pdfX = (svgX - mapX) * scaleX;
71616
+ // Raw (unrotated) mediabox dims — pdf-lib draws into this space.
71617
+ var rawW = options.pageWidth;
71618
+ var rawH = options.pageHeight;
71619
+ var rot = (((_options$pageRotation4 = options.pageRotation) !== null && _options$pageRotation4 !== void 0 ? _options$pageRotation4 : 0) % 360 + 360) % 360;
71620
+ var quarter = rot === 90 || rot === 270;
71621
+ // Displayed page dims = the orientation the markup was authored in. For a
71622
+ // 90/270 rotation the visible page is the mediabox with width/height swapped,
71623
+ // so we must scale into the swapped dims (otherwise X/Y scale differ → stretch).
71624
+ var dispW = quarter ? rawH : rawW;
71625
+ var dispH = quarter ? rawW : rawH;
71626
+ var scaleX = dispW / mapW;
71627
+ var scaleY = dispH / mapH;
71628
+ // Displayed-space coords (origin bottom-left of the *visible* page, Y-up).
71629
+ var dx = (svgX - mapX) * scaleX;
71579
71630
  // When SVG has CSS scale(1,-1), coordinates are already Y-up (like PDF)
71580
71631
  // so we map directly. Otherwise flip Y (SVG Y-down → PDF Y-up).
71581
- var pdfY = this.isYFlipped ? (svgY - mapY) * scaleY : options.pageHeight - (svgY - mapY) * scaleY;
71632
+ var dy = this.isYFlipped ? (svgY - mapY) * scaleY : dispH - (svgY - mapY) * scaleY;
71633
+ // Map displayed coords → unrotated mediabox space by inverting the page
71634
+ // /Rotate, so markup lines up with the rotated page content.
71635
+ var pdfX;
71636
+ var pdfY;
71637
+ switch (rot) {
71638
+ case 90:
71639
+ pdfX = rawW - dy;
71640
+ pdfY = dx;
71641
+ break;
71642
+ case 180:
71643
+ pdfX = rawW - dx;
71644
+ pdfY = rawH - dy;
71645
+ break;
71646
+ case 270:
71647
+ pdfX = dy;
71648
+ pdfY = rawH - dx;
71649
+ break;
71650
+ default:
71651
+ pdfX = dx;
71652
+ pdfY = dy;
71653
+ }
71582
71654
  return [pdfX, pdfY];
71583
71655
  }
71584
71656
  /**
@@ -72104,7 +72176,8 @@ var MarkupExportService = /*#__PURE__*/function () {
72104
72176
  pageWidth: width,
72105
72177
  pageHeight: height,
72106
72178
  svgViewBox: svgViewBox,
72107
- modelBounds: modelBounds
72179
+ modelBounds: modelBounds,
72180
+ pageRotation: page.getRotation().angle
72108
72181
  }; // Draw markup on page
72109
72182
  _context.n = 8;
72110
72183
  return this.converter.drawSvgOnPdfPage(svgString, page, converterOptions);
@@ -93863,9 +93936,9 @@ var CanvasExportService = /*#__PURE__*/function () {
93863
93936
  scene,
93864
93937
  _msg,
93865
93938
  _error,
93866
- svgString,
93867
93939
  sceneBounds,
93868
93940
  svgViewBox,
93941
+ svgString,
93869
93942
  modelBounds,
93870
93943
  pb,
93871
93944
  avgStrokePt,
@@ -93985,7 +94058,14 @@ var CanvasExportService = /*#__PURE__*/function () {
93985
94058
  _error.code = 'MISSING_PAGE_BOUNDS';
93986
94059
  throw _error;
93987
94060
  case 13:
93988
- svgString = SceneToSvg.convert(scene);
94061
+ sceneBounds = SceneToSvg.computeBounds(scene); // SVG viewBox: use full page bounds if available, else shape bounding box.
94062
+ // The viewBox must cover all shapes so nothing is clipped.
94063
+ svgViewBox = (_ref = (_scene$pageBounds = scene.pageBounds) !== null && _scene$pageBounds !== void 0 ? _scene$pageBounds : scene.viewportBounds) !== null && _ref !== void 0 ? _ref : sceneBounds; // Build the SVG with the SAME viewBox used for PDF coordinate mapping below.
94064
+ // SceneToSvg derives its viewBox attribute AND the Y-flip (translate/scale(1,-1))
94065
+ // from this rect; if it defaulted to viewportBounds while the converter maps via
94066
+ // modelBounds (=pageBounds), the flip origin and the mapping disagree → markup is
94067
+ // mispositioned. Passing svgViewBox keeps both consistent.
94068
+ svgString = SceneToSvg.convert(scene, svgViewBox);
93989
94069
  if (svgString) {
93990
94070
  _context2.n = 14;
93991
94071
  break;
@@ -93993,9 +94073,7 @@ var CanvasExportService = /*#__PURE__*/function () {
93993
94073
  console.debug("[CanvasExportService] page ".concat(pageIndex, ": SceneToSvg returned empty"));
93994
94074
  return _context2.a(3, 15);
93995
94075
  case 14:
93996
- sceneBounds = SceneToSvg.computeBounds(scene); // SVG viewBox: use full page bounds if available, else shape bounding box.
93997
- // The viewBox must cover all shapes so nothing is clipped.
93998
- svgViewBox = (_ref = (_scene$pageBounds = scene.pageBounds) !== null && _scene$pageBounds !== void 0 ? _scene$pageBounds : scene.viewportBounds) !== null && _ref !== void 0 ? _ref : sceneBounds; // modelBounds: the full page extent in world coordinates.
94076
+ // modelBounds: the full page extent in world coordinates.
93999
94077
  // CRITICAL: must be the full PDF page, not just the shape bounding box.
94000
94078
  // Using the shape bounding box stretches markup to fill the page (wrong scale/position).
94001
94079
  // pageBounds is captured from viewer.model.getBoundingBox() at save time.