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