@files-preview-app/preview-file 1.3.4 → 1.3.5

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.cjs CHANGED
@@ -5361,7 +5361,7 @@ function cleanRtfText(raw) {
5361
5361
  return String.fromCharCode(num);
5362
5362
  }).replace(/\\'([0-9a-fA-F]{2})/g, (_, hex) => {
5363
5363
  return String.fromCharCode(parseInt(hex, 16));
5364
- }).replace(/\\tab\b/g, " ").replace(/\\emdash\b/g, "\u2014").replace(/\\endash\b/g, "\u2013").replace(/\\bullet\b/g, "\u2022").replace(/\\lquote\b/g, "\u2018").replace(/\\rquote\b/g, "\u2019").replace(/\\ldblquote\b/g, "\u201C").replace(/\\rdblquote\b/g, "\u201D").replace(/\\\w+(?:-?\d+)?\s?/g, " ").replace(/[{}\r\n]/g, " ").replace(/\s+/g, " ").trim();
5364
+ }).replace(/\\tab\b/g, " ").replace(/\\emdash\b/g, "\u2014").replace(/\\endash\b/g, "\u2013").replace(/\\bullet\b/g, "\u2022").replace(/\\lquote\b/g, "\u2018").replace(/\\rquote\b/g, "\u2019").replace(/\\ldblquote\b/g, "\u201C").replace(/\\rdblquote\b/g, "\u201D").replace(/\\[a-zA-Z]+-?\d*\s?/g, " ").replace(/[{}\r\n]/g, " ").replace(/\s+/g, " ").trim();
5365
5365
  }
5366
5366
  function parseRtfTables(rtfText) {
5367
5367
  const rowSegments = rtfText.split(/\\row\b/);
@@ -5401,17 +5401,12 @@ function parseRtfTables(rtfText) {
5401
5401
  }
5402
5402
  const isNewTable = !currentTable || segmentStart - currentTable.lastEnd > 2500;
5403
5403
  if (isNewTable) {
5404
- const prevTextSlice = rtfText.slice(Math.max(0, segmentStart - 4e3), segmentStart);
5405
- const cleanPrev = cleanRtfText(prevTextSlice);
5406
- const anchorBefore = cleanPrev.slice(-80).trim();
5407
5404
  currentTable = {
5408
5405
  id: `RTF_TABLE_${rawTables.length}`,
5409
5406
  startPos: segmentStart,
5410
5407
  lastEnd: segmentEnd,
5411
5408
  rows: [],
5412
- columnWidths: cellWidths,
5413
- anchorBefore,
5414
- anchorAfter: ""
5409
+ columnWidths: cellWidths
5415
5410
  };
5416
5411
  rawTables.push(currentTable);
5417
5412
  }
@@ -5425,20 +5420,13 @@ function parseRtfTables(rtfText) {
5425
5420
  }
5426
5421
  charOffset += segment.length + 4;
5427
5422
  }
5428
- rawTables.forEach((tbl) => {
5429
- const afterSlice = rtfText.slice(tbl.lastEnd, Math.min(rtfText.length, tbl.lastEnd + 2e3));
5430
- const cleanAfter = cleanRtfText(afterSlice);
5431
- tbl.anchorAfter = cleanAfter.slice(0, 80).trim();
5432
- });
5433
5423
  return rawTables.map((tbl) => {
5434
5424
  const totalW = (tbl.columnWidths || []).reduce((a, b) => a + b, 0);
5435
5425
  const colPercents = totalW > 0 ? tbl.columnWidths.map((w) => Math.round(w / totalW * 100)) : [];
5436
5426
  return {
5437
5427
  id: tbl.id,
5438
5428
  rows: tbl.rows.map((r) => r.cells),
5439
- colPercents,
5440
- anchorBefore: tbl.anchorBefore,
5441
- anchorAfter: tbl.anchorAfter
5429
+ colPercents
5442
5430
  };
5443
5431
  });
5444
5432
  }
@@ -5451,8 +5439,6 @@ function renderRtfTableElement(table) {
5451
5439
  tableEl.style.fontSize = "10pt";
5452
5440
  tableEl.style.lineHeight = "1.5";
5453
5441
  tableEl.style.border = "1px solid #cbd5e1";
5454
- tableEl.style.borderRadius = "4px";
5455
- tableEl.style.boxSizing = "border-box";
5456
5442
  tableEl.style.backgroundColor = "#ffffff";
5457
5443
  const thead = document.createElement("thead");
5458
5444
  const tbody = document.createElement("tbody");
@@ -5461,27 +5447,17 @@ function renderRtfTableElement(table) {
5461
5447
  const isHeader = rIdx === 0;
5462
5448
  if (isHeader) {
5463
5449
  tr.style.backgroundColor = "#f8fafc";
5464
- tr.style.borderBottom = "2px solid #cbd5e1";
5465
- } else {
5466
- tr.style.borderBottom = "1px solid #e2e8f0";
5467
- if (rIdx % 2 === 0) {
5468
- tr.style.backgroundColor = "#fcfdfe";
5469
- }
5470
5450
  }
5471
5451
  rowCells.forEach((cellText, cIdx) => {
5472
5452
  const cellEl = document.createElement(isHeader ? "th" : "td");
5473
5453
  cellEl.style.padding = "8px 12px";
5474
5454
  cellEl.style.border = "1px solid #cbd5e1";
5475
5455
  cellEl.style.verticalAlign = "top";
5476
- cellEl.style.color = isHeader ? "#0f172a" : "#1e293b";
5477
- cellEl.style.fontWeight = isHeader ? "600" : "normal";
5478
5456
  if (table.colPercents && table.colPercents[cIdx]) {
5479
5457
  cellEl.style.width = `${table.colPercents[cIdx]}%`;
5480
5458
  }
5481
5459
  if (/^\d+$/.test(cellText.trim()) || cellText.trim() === "#") {
5482
5460
  cellEl.style.textAlign = "center";
5483
- } else {
5484
- cellEl.style.textAlign = "left";
5485
5461
  }
5486
5462
  cellEl.textContent = cellText;
5487
5463
  tr.appendChild(cellEl);
@@ -5496,23 +5472,19 @@ function renderRtfTableElement(table) {
5496
5472
  tableEl.appendChild(tbody);
5497
5473
  return tableEl;
5498
5474
  }
5499
- function extractMediaFingerprint(el) {
5500
- const img = el.tagName.toLowerCase() === "img" ? el : el.querySelector("img");
5501
- if (img && img.src) {
5502
- const s = img.src;
5503
- return s.length > 300 ? `img_${s.slice(0, 80)}_${s.length}_${s.slice(-80)}` : `img_${s}`;
5504
- }
5505
- const innerImg = el.querySelector("image");
5506
- if (innerImg) {
5507
- const h = innerImg.getAttribute("xlink:href") || innerImg.getAttribute("href") || "";
5508
- if (h) return h.length > 300 ? `svgimg_${h.slice(0, 80)}_${h.length}_${h.slice(-80)}` : `svgimg_${h}`;
5509
- }
5510
- const svg = el.tagName.toLowerCase() === "svg" ? el : el.querySelector("svg");
5511
- if (svg) {
5512
- const vb = svg.getAttribute("viewBox") || "";
5513
- return `svg_${vb}_${svg.innerHTML.length}`;
5514
- }
5515
- return null;
5475
+ function isHeadingElement(el) {
5476
+ if (["H1", "H2", "H3", "H4"].includes(el.tagName)) return true;
5477
+ const spans = el.querySelectorAll ? Array.from(el.querySelectorAll("span, b, strong, div")) : [];
5478
+ for (const s of spans) {
5479
+ const hEl = s;
5480
+ const fs = parseFloat(hEl.style.fontSize) || 0;
5481
+ const isBold = hEl.style.fontWeight === "bold" || parseInt(hEl.style.fontWeight, 10) >= 600 || ["B", "STRONG"].includes(hEl.tagName);
5482
+ if (isBold && fs >= 14) return true;
5483
+ }
5484
+ const elFs = parseFloat(el.style.fontSize) || 0;
5485
+ const elBold = el.style.fontWeight === "bold" || parseInt(el.style.fontWeight, 10) >= 600;
5486
+ if (elBold && elFs >= 14) return true;
5487
+ return false;
5516
5488
  }
5517
5489
  var RtfPlugin = class {
5518
5490
  id = "rtf";
@@ -5625,10 +5597,66 @@ var RtfPlugin = class {
5625
5597
  return actions;
5626
5598
  }
5627
5599
  async render(ctx) {
5600
+ const rawRtf = new TextDecoder("latin1").decode(ctx.buffer);
5601
+ const paperwMatch = rawRtf.match(/\\paperw(\d+)/);
5602
+ const paperhMatch = rawRtf.match(/\\paperh(\d+)/);
5603
+ const marglMatch = rawRtf.match(/\\margl(\d+)/);
5604
+ const margrMatch = rawRtf.match(/\\margr(\d+)/);
5605
+ const margtMatch = rawRtf.match(/\\margt(\d+)/);
5606
+ const margbMatch = rawRtf.match(/\\margb(\d+)/);
5607
+ const paperwTwips = paperwMatch ? parseInt(paperwMatch[1], 10) : 11906;
5608
+ const paperhTwips = paperhMatch ? parseInt(paperhMatch[1], 10) : 16838;
5609
+ const marglTwips = marglMatch ? parseInt(marglMatch[1], 10) : 1134;
5610
+ const margrTwips = margrMatch ? parseInt(margrMatch[1], 10) : 1134;
5611
+ const margtTwips = margtMatch ? parseInt(margtMatch[1], 10) : 1134;
5612
+ const margbTwips = margbMatch ? parseInt(margbMatch[1], 10) : 1134;
5613
+ const pageWidth = Math.round(paperwTwips / 15);
5614
+ const pageHeight = Math.round(paperhTwips / 15);
5615
+ const padLeft = Math.round(marglTwips / 15);
5616
+ const padRight = Math.round(margrTwips / 15);
5617
+ const padTop = Math.round(margtTwips / 15);
5618
+ const padBottom = Math.round(margbTwips / 15);
5619
+ const printableHeight = pageHeight - padTop - padBottom;
5620
+ const extractedImages = [];
5621
+ let p = 0;
5622
+ while ((p = rawRtf.indexOf("\\pict", p)) !== -1) {
5623
+ const startP = p;
5624
+ p += 5;
5625
+ const header = rawRtf.substring(startP, startP + 600);
5626
+ let mime = "";
5627
+ let magic = "";
5628
+ if (header.includes("\\pngblip")) {
5629
+ mime = "image/png";
5630
+ magic = "89504e47";
5631
+ } else if (header.includes("\\jpegblip")) {
5632
+ mime = "image/jpeg";
5633
+ magic = "ffd8ff";
5634
+ }
5635
+ if (!mime || !magic) continue;
5636
+ const magicPos = rawRtf.indexOf(magic, startP);
5637
+ if (magicPos === -1 || magicPos - startP > 3e3) continue;
5638
+ let endP = magicPos;
5639
+ while (endP < rawRtf.length && /[0-9a-fA-F\r\n\s]/.test(rawRtf[endP])) {
5640
+ endP++;
5641
+ }
5642
+ const cleanHex = rawRtf.substring(magicPos, endP).replace(/[\r\n\s]/g, "");
5643
+ if (cleanHex.length < 100) continue;
5644
+ const hexLen = cleanHex.length / 2;
5645
+ const u8 = new Uint8Array(hexLen);
5646
+ for (let k = 0; k < hexLen; k++) {
5647
+ u8[k] = parseInt(cleanHex.substr(k * 2, 2), 16);
5648
+ }
5649
+ let bStr = "";
5650
+ for (let k = 0; k < u8.length; k += 8192) {
5651
+ bStr += String.fromCharCode.apply(null, Array.from(u8.subarray(k, k + 8192)));
5652
+ }
5653
+ extractedImages.push({ mime, dataUrl: `data:${mime};base64,${btoa(bStr)}`, offset: startP });
5654
+ }
5655
+ const tables = parseRtfTables(rawRtf);
5628
5656
  const wrapper = document.createElement("div");
5629
5657
  wrapper.className = "fp-rtf-wrapper";
5630
5658
  wrapper.style.padding = "0";
5631
- wrapper.style.width = "816px";
5659
+ wrapper.style.width = `${pageWidth}px`;
5632
5660
  wrapper.style.margin = "0 auto";
5633
5661
  wrapper.style.boxSizing = "border-box";
5634
5662
  wrapper.style.display = "flex";
@@ -5648,9 +5676,8 @@ var RtfPlugin = class {
5648
5676
  let isUserZoomed = false;
5649
5677
  let fitMode = ctx?.options?.fitMode || "width";
5650
5678
  const calculateFitScale = (mode = fitMode) => {
5651
- const activeEl = pageElements[currentPage - 1] || wrapper;
5652
- const elW = 816;
5653
- const singlePageH = activeEl?.offsetHeight || 1056;
5679
+ const elW = pageWidth;
5680
+ const singlePageH = pageHeight;
5654
5681
  const availW = Math.max(280, ctx.container.clientWidth - 32);
5655
5682
  const availH = Math.max(280, ctx.container.clientHeight - 32);
5656
5683
  const sW = availW / elW;
@@ -5663,8 +5690,7 @@ var RtfPlugin = class {
5663
5690
  const applyTransform = () => {
5664
5691
  wrapper.style.transform = `scale(${scale}) rotate(${rotation}deg)`;
5665
5692
  wrapper.style.transformOrigin = "top center";
5666
- const activeEl = pageElements[currentPage - 1];
5667
- const baseH = activeEl?.offsetHeight || 1056;
5693
+ const baseH = pageHeight;
5668
5694
  const scaledH = baseH * scale;
5669
5695
  const extraH = Math.max(0, scaledH - baseH);
5670
5696
  wrapper.style.marginBottom = `${extraH + 32}px`;
@@ -5684,193 +5710,220 @@ var RtfPlugin = class {
5684
5710
  if (typeof RTFJS__namespace.loggingEnabled === "function") {
5685
5711
  RTFJS__namespace.loggingEnabled(false);
5686
5712
  }
5687
- const rawText = new TextDecoder("latin1").decode(ctx.buffer);
5688
- const tables = parseRtfTables(rawText);
5689
- const seenMediaSignatures = /* @__PURE__ */ new Set();
5690
5713
  const doc = new RTFJS__namespace.Document(ctx.buffer, {
5691
5714
  onPicture: (isLegacy, createPic) => {
5692
- if (isLegacy === true) {
5693
- return null;
5694
- }
5695
- const pic = createPic();
5696
- if (!pic) return null;
5697
- const fp = extractMediaFingerprint(pic);
5698
- if (fp) {
5699
- if (seenMediaSignatures.has(fp)) {
5700
- return null;
5701
- }
5702
- seenMediaSignatures.add(fp);
5703
- }
5704
- return pic;
5715
+ if (isLegacy === true) return null;
5716
+ return createPic();
5705
5717
  }
5706
5718
  });
5707
- const htmlElements = await doc.render();
5708
- const contentNodes = [];
5709
- const seenInContentBlocks = /* @__PURE__ */ new Set();
5710
- const extractBlocks = (nodes) => {
5711
- for (const item of nodes) {
5712
- if (!item || !(item instanceof HTMLElement)) continue;
5713
- const tag = item.tagName.toLowerCase();
5714
- const hasChildBlocks = Array.from(item.children).some((c) => {
5715
- const ct = c.tagName.toLowerCase();
5716
- return ["p", "div", "table", "h1", "h2", "h3", "h4", "h5", "h6", "section", "article", "ul", "ol"].includes(ct);
5717
- });
5718
- if (hasChildBlocks && !["table", "tr", "td", "th", "ul", "ol", "li"].includes(tag)) {
5719
- extractBlocks(Array.from(item.children));
5720
- } else {
5721
- const fp = extractMediaFingerprint(item);
5722
- if (fp) {
5723
- if (seenInContentBlocks.has(fp)) {
5724
- continue;
5725
- }
5726
- seenInContentBlocks.add(fp);
5727
- }
5728
- const hasMedia = item.querySelector("img, svg, canvas, table") !== null || ["img", "svg", "table"].includes(tag);
5729
- const textContent = (item.textContent || "").trim();
5730
- if (!hasMedia && textContent.length === 0) {
5731
- continue;
5732
- }
5733
- const svgs = item.querySelectorAll("svg, img");
5734
- svgs.forEach((s) => {
5735
- const el = s;
5736
- el.style.maxWidth = "100%";
5737
- el.style.maxHeight = "360px";
5738
- el.style.display = "block";
5739
- el.style.margin = "12px auto";
5740
- if (s.tagName.toLowerCase() === "svg") {
5741
- s.removeAttribute("width");
5742
- s.removeAttribute("height");
5743
- el.style.width = "100%";
5744
- el.style.maxHeight = "360px";
5745
- }
5746
- });
5747
- if (tag === "p" || tag === "div") {
5748
- item.style.display = "block";
5749
- item.style.marginBottom = "12px";
5750
- item.style.lineHeight = "1.6";
5751
- }
5752
- contentNodes.push(item);
5719
+ const elements = await doc.render();
5720
+ let unsupportedIndex = 1;
5721
+ const usedTables = /* @__PURE__ */ new Set();
5722
+ for (let i = 0; i < elements.length; i++) {
5723
+ const el = elements[i];
5724
+ const text = el.textContent || "";
5725
+ if (text.includes("[Unsupported image format]")) {
5726
+ if (extractedImages[unsupportedIndex]) {
5727
+ const img = document.createElement("img");
5728
+ img.src = extractedImages[unsupportedIndex].dataUrl;
5729
+ img.style.width = "100%";
5730
+ img.style.height = "auto";
5731
+ img.style.maxHeight = "430px";
5732
+ img.style.objectFit = "contain";
5733
+ img.style.display = "block";
5734
+ img.style.margin = "12px auto";
5735
+ el.innerHTML = "";
5736
+ el.appendChild(img);
5737
+ unsupportedIndex++;
5753
5738
  }
5754
5739
  }
5755
- };
5756
- extractBlocks(htmlElements);
5757
- tables.forEach((table) => {
5758
- const tableEl = renderRtfTableElement(table);
5759
- let inserted = false;
5760
- if (table.anchorBefore && table.anchorBefore.length > 10) {
5761
- const keyword = table.anchorBefore.slice(-35).trim();
5762
- for (let i = 0; i < contentNodes.length; i++) {
5763
- if (contentNodes[i].textContent?.includes(keyword)) {
5764
- contentNodes.splice(i + 1, 0, tableEl);
5765
- inserted = true;
5766
- break;
5740
+ if (tables.length > 0) {
5741
+ for (let t = 0; t < tables.length; t++) {
5742
+ if (usedTables.has(t)) continue;
5743
+ const tbl = tables[t];
5744
+ if (tbl.rows.length >= 2) {
5745
+ const sampleCell1 = tbl.rows[1]?.[1] || tbl.rows[0]?.[1] || "";
5746
+ const sampleCell2 = tbl.rows[2]?.[1] || tbl.rows[1]?.[0] || "";
5747
+ const match1 = sampleCell1 && text.includes(sampleCell1.slice(0, 15).trim());
5748
+ const match2 = sampleCell2 && text.includes(sampleCell2.slice(0, 15).trim());
5749
+ if (match1 && match2) {
5750
+ el.innerHTML = "";
5751
+ el.appendChild(renderRtfTableElement(tbl));
5752
+ usedTables.add(t);
5753
+ break;
5754
+ }
5767
5755
  }
5768
5756
  }
5769
5757
  }
5770
- if (!inserted && table.anchorAfter && table.anchorAfter.length > 10) {
5771
- const keyword = table.anchorAfter.slice(0, 35).trim();
5772
- for (let i = 0; i < contentNodes.length; i++) {
5773
- if (contentNodes[i].textContent?.includes(keyword)) {
5774
- contentNodes.splice(i, 0, tableEl);
5775
- inserted = true;
5776
- break;
5777
- }
5778
- }
5758
+ }
5759
+ elements.forEach((el) => {
5760
+ const svgs = el.querySelectorAll ? Array.from(el.querySelectorAll("svg")) : [];
5761
+ svgs.forEach((svg) => {
5762
+ svg.removeAttribute("width");
5763
+ svg.removeAttribute("height");
5764
+ svg.style.maxWidth = "100%";
5765
+ svg.style.maxHeight = "430px";
5766
+ svg.style.height = "auto";
5767
+ svg.style.display = "block";
5768
+ svg.style.margin = "12px auto";
5769
+ });
5770
+ const imgs = el.querySelectorAll ? Array.from(el.querySelectorAll("img")) : [];
5771
+ imgs.forEach((img) => {
5772
+ img.style.maxWidth = "100%";
5773
+ img.style.maxHeight = "430px";
5774
+ img.style.height = "auto";
5775
+ img.style.objectFit = "contain";
5776
+ img.style.display = "block";
5777
+ img.style.margin = "12px auto";
5778
+ });
5779
+ });
5780
+ const allImgs = Array.from(document.querySelectorAll("img")).concat(
5781
+ elements.flatMap((el) => Array.from(el.querySelectorAll ? el.querySelectorAll("img") : []))
5782
+ );
5783
+ await Promise.all(allImgs.map((img) => {
5784
+ if (img.complete && img.naturalHeight > 0) return Promise.resolve();
5785
+ if (img.decode) return img.decode().catch(() => {
5786
+ });
5787
+ return new Promise((r) => {
5788
+ img.onload = r;
5789
+ img.onerror = r;
5790
+ });
5791
+ }));
5792
+ elements.forEach((el) => {
5793
+ const text = el.textContent || "";
5794
+ if (text.includes("\uF0B7")) {
5795
+ const cleanText = text.replace(/^[\s\uf0b7\t]+/, "");
5796
+ el.style.display = "flex";
5797
+ el.style.alignItems = "flex-start";
5798
+ el.style.margin = "6px 0 6px 28px";
5799
+ el.style.fontSize = "11pt";
5800
+ el.style.lineHeight = "1.5";
5801
+ el.innerHTML = `<span style="margin-right:10px; user-select:none;">&#9633;</span><span>${cleanText}</span>`;
5779
5802
  }
5780
- if (!inserted) {
5781
- if (contentNodes.length > 2) {
5782
- contentNodes.splice(2, 0, tableEl);
5783
- } else {
5784
- contentNodes.push(tableEl);
5785
- }
5803
+ });
5804
+ elements.forEach((el) => {
5805
+ const text = (el.textContent || "").trim();
5806
+ const hasMedia = el.querySelector("img, svg, canvas, table") !== null || ["IMG", "SVG", "TABLE"].includes(el.tagName);
5807
+ if (isHeadingElement(el)) {
5808
+ el.style.marginTop = "20px";
5809
+ el.style.marginBottom = "10px";
5810
+ el.style.lineHeight = "1.25";
5811
+ } else if (text.length > 0 && !hasMedia) {
5812
+ el.style.marginBottom = "12px";
5813
+ el.style.lineHeight = "1.45";
5786
5814
  }
5787
5815
  });
5788
- const createRtfCard = (pageNum) => {
5816
+ const measureCard = document.createElement("div");
5817
+ measureCard.style.width = `${pageWidth}px`;
5818
+ measureCard.style.padding = `${padTop}px ${padRight}px ${padBottom}px ${padLeft}px`;
5819
+ measureCard.style.boxSizing = "border-box";
5820
+ measureCard.style.fontFamily = 'Calibri, "Segoe UI", Arial, sans-serif';
5821
+ measureCard.style.lineHeight = "1.45";
5822
+ measureCard.style.visibility = "hidden";
5823
+ measureCard.style.position = "absolute";
5824
+ measureCard.style.top = "-99999px";
5825
+ document.body.appendChild(measureCard);
5826
+ const createPageCard = (num) => {
5789
5827
  const card = document.createElement("div");
5790
5828
  card.className = "fp-rtf-page-card";
5791
- card.setAttribute("data-page-number", String(pageNum));
5829
+ card.setAttribute("data-page-number", String(num));
5792
5830
  card.style.backgroundColor = "#ffffff";
5793
5831
  card.style.boxShadow = "0 4px 24px rgba(0,0,0,0.08), 0 1px 3px rgba(0,0,0,0.04)";
5794
5832
  card.style.borderRadius = "4px";
5795
- card.style.padding = "64px 56px";
5796
- card.style.width = "816px";
5797
- card.style.minHeight = "1056px";
5798
- card.style.maxHeight = "1056px";
5799
- card.style.overflow = "hidden";
5833
+ card.style.width = `${pageWidth}px`;
5834
+ card.style.height = `${pageHeight}px`;
5835
+ card.style.padding = `${padTop}px ${padRight}px ${padBottom}px ${padLeft}px`;
5800
5836
  card.style.boxSizing = "border-box";
5837
+ card.style.overflow = "hidden";
5801
5838
  card.style.marginBottom = "24px";
5802
- card.style.fontFamily = 'Calibri, "Segoe UI", Arial, sans-serif';
5803
- card.style.lineHeight = "1.6";
5804
5839
  card.style.color = "#1e293b";
5840
+ card.style.fontFamily = 'Calibri, "Segoe UI", Arial, sans-serif';
5841
+ card.style.lineHeight = "1.45";
5805
5842
  card.style.position = "relative";
5843
+ card.style.textAlign = "left";
5806
5844
  return card;
5807
5845
  };
5808
- wrapper.innerHTML = "";
5809
- const measureCard = createRtfCard(0);
5810
- measureCard.style.maxHeight = "none";
5811
- measureCard.style.height = "auto";
5812
- wrapper.appendChild(measureCard);
5813
- contentNodes.forEach((node) => measureCard.appendChild(node));
5814
- const childHeights = contentNodes.map((c) => {
5815
- const rect = c.getBoundingClientRect ? c.getBoundingClientRect() : null;
5816
- const rectH = rect ? rect.height : 0;
5817
- const offH = c.offsetHeight || 0;
5818
- let realH = Math.max(rectH, offH);
5819
- try {
5820
- const cs = window.getComputedStyle(c);
5821
- realH += (parseFloat(cs.marginTop) || 0) + (parseFloat(cs.marginBottom) || 0);
5822
- } catch {
5823
- }
5824
- const hasMedia = c.querySelector("img, svg") !== null || ["img", "svg"].includes(c.tagName.toLowerCase());
5825
- const hasTable = c.querySelector("table") !== null || c.tagName.toLowerCase() === "table";
5826
- if (hasMedia) {
5827
- realH = Math.max(realH, 300);
5828
- } else if (hasTable) {
5829
- realH = Math.max(realH, 240);
5830
- }
5831
- if (realH > 0) return realH;
5832
- const textLen = c.textContent?.trim().length || 0;
5833
- return Math.max(28, Math.ceil(textLen / 75) * 26 + 16);
5834
- });
5835
- wrapper.innerHTML = "";
5836
- let curCard = createRtfCard(1);
5837
- wrapper.appendChild(curCard);
5838
- pageElements = [curCard];
5839
- let curH = 0;
5840
- const maxH = 860;
5841
- for (let i = 0; i < contentNodes.length; i++) {
5842
- const child = contentNodes[i];
5843
- const chH = childHeights[i];
5844
- const isChildEmpty = (child.textContent || "").trim().length === 0 && !child.querySelector("table, img, svg, canvas");
5845
- if (curH === 0 && isChildEmpty) {
5846
+ const pages = [];
5847
+ let curCard = createPageCard(1);
5848
+ pages.push(curCard);
5849
+ let curHeight = 0;
5850
+ for (let i = 0; i < elements.length; i++) {
5851
+ const el = elements[i];
5852
+ if (!el || !(el instanceof HTMLElement)) continue;
5853
+ const text = (el.textContent || "").trim();
5854
+ const hasMedia = el.querySelector("img, svg, canvas, table") !== null || ["IMG", "SVG", "TABLE"].includes(el.tagName);
5855
+ const isEmptyPar = !hasMedia && text.length === 0;
5856
+ if (isEmptyPar) {
5857
+ const emptyH = 35;
5858
+ if (curHeight + emptyH > printableHeight) {
5859
+ if (curCard.childNodes.length > 0) {
5860
+ curCard = createPageCard(pages.length + 1);
5861
+ pages.push(curCard);
5862
+ curHeight = 0;
5863
+ }
5864
+ }
5865
+ const spacer = document.createElement("div");
5866
+ spacer.style.height = `${emptyH}px`;
5867
+ curCard.appendChild(spacer);
5868
+ curHeight += emptyH;
5846
5869
  continue;
5847
5870
  }
5848
- const isTable = child.querySelector("table") !== null || child.tagName.toLowerCase() === "table";
5849
- const shouldBreakBeforeTable = isTable && curH > 400 && curCard.childNodes.length > 0;
5850
- if ((curH + chH > maxH || shouldBreakBeforeTable) && curCard.childNodes.length > 0) {
5851
- curCard = createRtfCard(pageElements.length + 1);
5852
- wrapper.appendChild(curCard);
5853
- pageElements.push(curCard);
5854
- curH = 0;
5871
+ measureCard.innerHTML = "";
5872
+ const clone = el.cloneNode(true);
5873
+ measureCard.appendChild(clone);
5874
+ const compStyle = window.getComputedStyle(clone);
5875
+ const mt = parseFloat(compStyle.marginTop) || 0;
5876
+ const mb = parseFloat(compStyle.marginBottom) || 0;
5877
+ const elH = clone.offsetHeight + mt + mb;
5878
+ const isHeading = isHeadingElement(el);
5879
+ let nextContentH = 0;
5880
+ if (isHeading || hasMedia) {
5881
+ for (let j = i + 1; j < elements.length; j++) {
5882
+ const nextEl = elements[j];
5883
+ if (nextEl && (nextEl.textContent || "").trim().length > 0) {
5884
+ measureCard.innerHTML = "";
5885
+ const nClone = nextEl.cloneNode(true);
5886
+ measureCard.appendChild(nClone);
5887
+ const nStyle = window.getComputedStyle(nClone);
5888
+ nextContentH = nClone.offsetHeight + (parseFloat(nStyle.marginTop) || 0) + (parseFloat(nStyle.marginBottom) || 0);
5889
+ break;
5890
+ }
5891
+ }
5855
5892
  }
5856
- curCard.appendChild(child);
5857
- curH += chH;
5858
- }
5859
- pageElements = pageElements.filter((card) => {
5893
+ const remainingSpace = printableHeight - curHeight;
5894
+ const isLargeMedia = hasMedia && elH > 300;
5895
+ const keepWithNextOverflow = isHeading && nextContentH > 0 && elH + nextContentH + 20 > remainingSpace;
5896
+ const largeMediaOverflow = isLargeMedia && curHeight > 0 && elH + 80 > remainingSpace;
5897
+ const shouldBreak = (curHeight + elH > printableHeight || keepWithNextOverflow || largeMediaOverflow) && curCard.childNodes.length > 0;
5898
+ if (shouldBreak) {
5899
+ curCard = createPageCard(pages.length + 1);
5900
+ pages.push(curCard);
5901
+ curHeight = 0;
5902
+ }
5903
+ curCard.appendChild(el);
5904
+ curHeight += elH;
5905
+ }
5906
+ measureCard.remove();
5907
+ pageElements = pages.filter((card) => {
5860
5908
  const hasText = (card.textContent || "").trim().length > 0;
5861
- const hasMedia = card.querySelector("table, img, svg, canvas") !== null;
5862
- return hasText || hasMedia;
5909
+ const hasCardMedia = card.querySelector("table, img, svg, canvas") !== null;
5910
+ return hasText || hasCardMedia;
5863
5911
  });
5864
5912
  if (pageElements.length === 0) {
5865
5913
  pageElements = [curCard];
5866
5914
  }
5915
+ wrapper.innerHTML = "";
5916
+ pageElements.forEach((card, idx) => {
5917
+ card.setAttribute("data-page-number", String(idx + 1));
5918
+ card.style.display = idx === 0 ? "block" : "none";
5919
+ wrapper.appendChild(card);
5920
+ });
5867
5921
  } catch (err) {
5868
5922
  console.warn("[RtfPlugin] RTF render error, fallback text:", err);
5869
- const text = new TextDecoder("latin1").decode(ctx.buffer);
5870
- const tables = parseRtfTables(text);
5923
+ const text = rawRtf;
5871
5924
  const rawPages = text.split(/\\page\b/).map((segment) => {
5872
5925
  return segment.replace(/\\par[d]?/g, "\n").replace(/\\[a-zA-Z0-9\-]+/g, "").replace(/[{}]/g, "").trim();
5873
- }).filter((p) => p.length > 0);
5926
+ }).filter((p2) => p2.length > 0);
5874
5927
  const pages = rawPages.length > 0 ? rawPages : [text.replace(/\\par[d]?/g, "\n").replace(/\\[a-zA-Z0-9\-]+/g, "").replace(/[{}]/g, "").trim()];
5875
5928
  for (let i = 0; i < pages.length; i++) {
5876
5929
  const pageCard = document.createElement("div");
@@ -5878,9 +5931,9 @@ var RtfPlugin = class {
5878
5931
  pageCard.style.backgroundColor = "#ffffff";
5879
5932
  pageCard.style.boxShadow = "0 2px 12px rgba(0,0,0,0.08)";
5880
5933
  pageCard.style.borderRadius = "4px";
5881
- pageCard.style.padding = "64px 56px";
5882
- pageCard.style.width = "816px";
5883
- pageCard.style.minHeight = "1056px";
5934
+ pageCard.style.padding = `${padTop}px ${padRight}px ${padBottom}px ${padLeft}px`;
5935
+ pageCard.style.width = `${pageWidth}px`;
5936
+ pageCard.style.minHeight = `${pageHeight}px`;
5884
5937
  pageCard.style.boxSizing = "border-box";
5885
5938
  pageCard.style.fontFamily = 'Calibri, "Segoe UI", Arial, sans-serif';
5886
5939
  pageCard.style.fontSize = "12pt";
@@ -5900,11 +5953,9 @@ var RtfPlugin = class {
5900
5953
  let currentPage = 1;
5901
5954
  const showPage = (pageNum) => {
5902
5955
  currentPage = Math.max(1, Math.min(totalPages, pageNum));
5903
- if (totalPages > 1) {
5904
- pageElements.forEach((el, idx) => {
5905
- el.style.display = idx + 1 === currentPage ? "block" : "none";
5906
- });
5907
- }
5956
+ pageElements.forEach((el, idx) => {
5957
+ el.style.display = idx + 1 === currentPage ? "block" : "none";
5958
+ });
5908
5959
  ctx.container.scrollTop = 0;
5909
5960
  ctx.emit("page-change", { page: currentPage, total: totalPages });
5910
5961
  };