@file-viewer/docx 0.3.36 → 0.3.39
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/README.md +6 -0
- package/dist/document-parser.d.ts +1 -0
- package/dist/docx-preview.js +227 -12
- package/dist/docx-preview.min.js +2 -2
- package/dist/docx-preview.min.mjs +2 -2
- package/dist/docx-preview.mjs +227 -12
- package/dist/docx-preview.worker.js +46 -0
- package/dist/html-renderer.d.ts +10 -0
- package/dist/vml/vml.d.ts +1 -0
- package/package.json +2 -2
package/dist/docx-preview.mjs
CHANGED
|
@@ -3733,6 +3733,8 @@ function parseVmlElement(elem, parser) {
|
|
|
3733
3733
|
result.tagName = "g";
|
|
3734
3734
|
result.isGroup = true;
|
|
3735
3735
|
break;
|
|
3736
|
+
case "roundrect":
|
|
3737
|
+
result.roundRectArc = 0.2;
|
|
3736
3738
|
case "rect":
|
|
3737
3739
|
result.tagName = "rect";
|
|
3738
3740
|
Object.assign(result.attrs, { width: '100%', height: '100%' });
|
|
@@ -3767,6 +3769,10 @@ function parseVmlElement(elem, parser) {
|
|
|
3767
3769
|
case "coordsize":
|
|
3768
3770
|
result.coordSize = parseNumericPoint(at.value);
|
|
3769
3771
|
break;
|
|
3772
|
+
case "arcsize":
|
|
3773
|
+
if (result.roundRectArc != null)
|
|
3774
|
+
result.roundRectArc = parseRoundRectArc(at.value);
|
|
3775
|
+
break;
|
|
3770
3776
|
case "inset":
|
|
3771
3777
|
if (result.textInset) {
|
|
3772
3778
|
const values = at.value.trim().split(/\s*,\s*|\s+/);
|
|
@@ -3864,6 +3870,13 @@ function parseVmlElement(elem, parser) {
|
|
|
3864
3870
|
}
|
|
3865
3871
|
return result;
|
|
3866
3872
|
}
|
|
3873
|
+
function parseRoundRectArc(value) {
|
|
3874
|
+
const source = value.trim();
|
|
3875
|
+
if (!/^[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:%|f)?$/i.test(source))
|
|
3876
|
+
return 0.2;
|
|
3877
|
+
const fraction = parseFloat(source) / (/%$/.test(source) ? 100 : /f$/i.test(source) ? 65536 : 1);
|
|
3878
|
+
return Number.isFinite(fraction) ? Math.max(0, Math.min(1, fraction)) : 0.2;
|
|
3879
|
+
}
|
|
3867
3880
|
function vmlImageAttrs(el) {
|
|
3868
3881
|
const cropLeft = parseVmlFraction(globalXmlParser.attr(el, "cropleft"));
|
|
3869
3882
|
const cropTop = parseVmlFraction(globalXmlParser.attr(el, "croptop"));
|
|
@@ -5421,6 +5434,9 @@ class DocumentParser {
|
|
|
5421
5434
|
const prstGeom = globalXmlParser.element(spPr, "prstGeom");
|
|
5422
5435
|
const fillStyle = this.parseDmlFillStyle(spPr);
|
|
5423
5436
|
const lineStyle = this.parseDmlLineStyle(globalXmlParser.element(spPr, "ln"));
|
|
5437
|
+
const roundedRadius = !geom ? this.parseDmlRoundRectRadius(spPr, bounds.width, bounds.height, transform) : null;
|
|
5438
|
+
if (roundedRadius != null)
|
|
5439
|
+
cssStyle["border-radius"] = roundedRadius;
|
|
5424
5440
|
if (geom) {
|
|
5425
5441
|
const path = this.parseDmlCustomGeometry(geom);
|
|
5426
5442
|
return {
|
|
@@ -5458,6 +5474,23 @@ class DocumentParser {
|
|
|
5458
5474
|
alt: cNvPr ? globalXmlParser.attr(cNvPr, "descr") : null
|
|
5459
5475
|
};
|
|
5460
5476
|
}
|
|
5477
|
+
parseDmlRoundRectRadius(spPr, width, height, transform) {
|
|
5478
|
+
const preset = globalXmlParser.element(spPr, "prstGeom");
|
|
5479
|
+
if (!preset || globalXmlParser.attr(preset, "prst") != "roundRect")
|
|
5480
|
+
return null;
|
|
5481
|
+
const guides = globalXmlParser.element(preset, "avLst");
|
|
5482
|
+
const adjustment = (guides ? globalXmlParser.elements(guides, "gd") : [])
|
|
5483
|
+
.find(guide => globalXmlParser.attr(guide, "name") == "adj");
|
|
5484
|
+
const formula = adjustment ? globalXmlParser.attr(adjustment, "fmla") : "";
|
|
5485
|
+
const value = /^val\s+[+-]?(?:\d+(?:\.\d*)?|\.\d+)\s*$/.test(formula ?? "")
|
|
5486
|
+
? Number(formula.trim().split(/\s+/)[1]) : 16667;
|
|
5487
|
+
const fraction = Math.max(0, Math.min(50000, value)) / 100000;
|
|
5488
|
+
const scaleX = Math.abs(transform?.scaleX ?? 1);
|
|
5489
|
+
const scaleY = Math.abs(transform?.scaleY ?? 1);
|
|
5490
|
+
const radius = Number.isFinite(scaleX) && Number.isFinite(scaleY) && scaleX > 0 && scaleY > 0
|
|
5491
|
+
? Math.min(width / scaleX, height / scaleY) * fraction : 0;
|
|
5492
|
+
return `${this.pt(Number.isFinite(radius * scaleX) ? radius * scaleX : 0)} / ${this.pt(Number.isFinite(radius * scaleY) ? radius * scaleY : 0)}`;
|
|
5493
|
+
}
|
|
5461
5494
|
parseDmlShapeBounds(spPr, transform) {
|
|
5462
5495
|
const xfrm = globalXmlParser.element(spPr, "xfrm");
|
|
5463
5496
|
const off = xfrm ? globalXmlParser.element(xfrm, "off") : null;
|
|
@@ -5594,6 +5627,7 @@ class DocumentParser {
|
|
|
5594
5627
|
return {
|
|
5595
5628
|
dmlTextBox: true,
|
|
5596
5629
|
textInsets: textInsetStyle,
|
|
5630
|
+
textCompatibleLineSpacing: bodyPr ? globalXmlParser.boolAttr(bodyPr, "compatLnSpc", false) : false,
|
|
5597
5631
|
textVerticalAlign: this.dmlVerticalAlign(bodyPr)
|
|
5598
5632
|
};
|
|
5599
5633
|
}
|
|
@@ -5693,6 +5727,10 @@ class DocumentParser {
|
|
|
5693
5727
|
}
|
|
5694
5728
|
parseDmlShapeProperties(elem, style) {
|
|
5695
5729
|
const xfrm = globalXmlParser.element(elem, "xfrm");
|
|
5730
|
+
const bounds = this.parseDmlShapeBounds(elem);
|
|
5731
|
+
const roundedRadius = this.parseDmlRoundRectRadius(elem, bounds.width, bounds.height);
|
|
5732
|
+
if (roundedRadius != null)
|
|
5733
|
+
style["border-radius"] = roundedRadius;
|
|
5696
5734
|
if (xfrm) {
|
|
5697
5735
|
for (const n of globalXmlParser.elements(xfrm)) {
|
|
5698
5736
|
switch (n.localName) {
|
|
@@ -6137,6 +6175,10 @@ class DocumentParser {
|
|
|
6137
6175
|
case "textAlignment":
|
|
6138
6176
|
style["vertical-align"] = values.valueOfTextAlignment(c);
|
|
6139
6177
|
break;
|
|
6178
|
+
case "autoSpaceDE":
|
|
6179
|
+
case "autoSpaceDN":
|
|
6180
|
+
style[c.localName == "autoSpaceDE" ? "--docx-auto-space-de" : "--docx-auto-space-dn"] = `${globalXmlParser.boolAttr(c, "val", true)}`;
|
|
6181
|
+
break;
|
|
6140
6182
|
case "color":
|
|
6141
6183
|
style["color"] = xmlUtil.colorAttr(c, "val", null, autos.color);
|
|
6142
6184
|
break;
|
|
@@ -6435,6 +6477,7 @@ class DocumentParser {
|
|
|
6435
6477
|
fonts.push(...[asciiTheme, hAnsiTheme]
|
|
6436
6478
|
.filter(x => x)
|
|
6437
6479
|
.map(x => x.startsWith("var(") ? x : encloseFontFamily(x)));
|
|
6480
|
+
const latinFonts = fonts.slice();
|
|
6438
6481
|
if (eastAsia) {
|
|
6439
6482
|
fonts.push(...eastAsianFontFamilyStack(eastAsia, eastAsiaScript));
|
|
6440
6483
|
}
|
|
@@ -6448,6 +6491,9 @@ class DocumentParser {
|
|
|
6448
6491
|
if (fonts.length > 0) {
|
|
6449
6492
|
const currentFonts = (style["font-family"] ?? "").split(',').map(x => x.trim()).filter(x => x);
|
|
6450
6493
|
style["font-family"] = [...new Set([...currentFonts, ...fonts])].join(', ');
|
|
6494
|
+
if (latinFonts.length)
|
|
6495
|
+
style["--docx-latin-font-family"] = [...new Set(latinFonts)].join(', ');
|
|
6496
|
+
style["--docx-partial-eastasia-font"] = !latinFonts.length && (eastAsia || eastAsiaTheme) && !cs && !csTheme ? "true" : "false";
|
|
6451
6497
|
}
|
|
6452
6498
|
}
|
|
6453
6499
|
scriptThemeFontValue(value, language) {
|
|
@@ -8729,6 +8775,7 @@ class HtmlRenderer {
|
|
|
8729
8775
|
this.headingIndex = 0;
|
|
8730
8776
|
this.tasks = [];
|
|
8731
8777
|
this.postRenderTasks = [];
|
|
8778
|
+
this.compatibleTextBoxSpacing = false;
|
|
8732
8779
|
this.h = h;
|
|
8733
8780
|
this.darkModeVarNames = new Map();
|
|
8734
8781
|
this.darkModePrintReset = {};
|
|
@@ -9654,6 +9701,29 @@ class HtmlRenderer {
|
|
|
9654
9701
|
effectiveParagraphCssValue(elem, style, name) {
|
|
9655
9702
|
return elem.cssStyle?.[name] ?? this.styleValue(style, "p", name);
|
|
9656
9703
|
}
|
|
9704
|
+
textBoxAutospace(elem, style, supports = (value) => typeof CSS == "undefined" || typeof CSS.supports != "function" || CSS.supports("text-autospace", value)) {
|
|
9705
|
+
const enabled = (key) => !/^(0|false|off)$/i.test(this.effectiveParagraphCssValue(elem, style, key) ?? "true");
|
|
9706
|
+
const alpha = enabled("--docx-auto-space-de"), numeric = enabled("--docx-auto-space-dn");
|
|
9707
|
+
const value = alpha && numeric ? "normal" : alpha ? "ideograph-alpha" : numeric ? "ideograph-numeric" : "no-autospace";
|
|
9708
|
+
return supports(value) ? value : "no-autospace";
|
|
9709
|
+
}
|
|
9710
|
+
applyTextBoxFontInheritance(elem, style, result) {
|
|
9711
|
+
if (!this.compatibleTextBoxSpacing)
|
|
9712
|
+
return;
|
|
9713
|
+
const paragraph = elem.type == DomType.Paragraph ? elem : this.currentParagraph;
|
|
9714
|
+
const paragraphStyle = this.findStyle(paragraph?.styleName);
|
|
9715
|
+
const defaults = this.document.stylesPart?.styles?.find(item => item.id == null);
|
|
9716
|
+
const levels = [elem.cssStyle, this.styleValuesForTarget(style, elem.type == DomType.Paragraph ? "p" : "span"),
|
|
9717
|
+
paragraph?.cssStyle, this.styleValuesForTarget(paragraphStyle, "span"), this.styleValuesForTarget(defaults, "span")];
|
|
9718
|
+
const value = (key) => levels.find(level => level?.[key] != null)?.[key];
|
|
9719
|
+
if (value("--docx-partial-eastasia-font") != "true")
|
|
9720
|
+
return;
|
|
9721
|
+
const latin = value("--docx-latin-font-family"), family = value("font-family");
|
|
9722
|
+
if (!latin || !family)
|
|
9723
|
+
return;
|
|
9724
|
+
this.setCssStyle(result, "--docx-latin-font-family", latin);
|
|
9725
|
+
this.setCssStyle(result, "font-family", `var(--docx-latin-font-family), ${family}`);
|
|
9726
|
+
}
|
|
9657
9727
|
isInsideTableCell(elem) {
|
|
9658
9728
|
for (let p = elem.parent; p; p = p.parent) {
|
|
9659
9729
|
if (p.type == DomType.Cell)
|
|
@@ -10692,6 +10762,8 @@ class HtmlRenderer {
|
|
|
10692
10762
|
splitParagraphBlock(paragraph, page, nextArticle) {
|
|
10693
10763
|
if (!paragraph.hasChildNodes())
|
|
10694
10764
|
return false;
|
|
10765
|
+
if (this.fitOversizedInlinePicture(paragraph, page))
|
|
10766
|
+
return false;
|
|
10695
10767
|
const clone = this.cloneBlockShell(paragraph);
|
|
10696
10768
|
clone.dataset.docxContinued = "true";
|
|
10697
10769
|
nextArticle.prepend(clone);
|
|
@@ -10714,6 +10786,44 @@ class HtmlRenderer {
|
|
|
10714
10786
|
this.rebalanceWidowOrphan(paragraph, clone, page);
|
|
10715
10787
|
return true;
|
|
10716
10788
|
}
|
|
10789
|
+
fitOversizedInlinePicture(paragraph, page) {
|
|
10790
|
+
const article = paragraph.parentElement;
|
|
10791
|
+
if (article?.localName !== "article" || article.firstElementChild !== paragraph
|
|
10792
|
+
|| page.querySelector(":scope > article > *") !== paragraph
|
|
10793
|
+
|| paragraph.textContent?.trim() || paragraph.querySelector("table,math,object,canvas,video,[data-docx-note-reference]"))
|
|
10794
|
+
return false;
|
|
10795
|
+
const drawings = Array.from(paragraph.querySelectorAll("[data-docx-drawing]"));
|
|
10796
|
+
const drawing = drawings[0];
|
|
10797
|
+
if (drawings.length !== 1 || !drawing || drawing.children.length !== 1
|
|
10798
|
+
|| paragraph.querySelectorAll("img").length !== 1 || drawing.dataset.docxFloat)
|
|
10799
|
+
return false;
|
|
10800
|
+
const picture = drawing.firstElementChild;
|
|
10801
|
+
if (picture.localName !== "img" && picture.dataset.docxImageCrop !== "true")
|
|
10802
|
+
return false;
|
|
10803
|
+
const view = paragraph.ownerDocument.defaultView;
|
|
10804
|
+
const style = view?.getComputedStyle(drawing);
|
|
10805
|
+
if (!view || !style || style.position === "absolute" || style.position === "fixed"
|
|
10806
|
+
|| style.transform !== "none" || style.cssFloat !== "none")
|
|
10807
|
+
return false;
|
|
10808
|
+
const paragraphRect = paragraph.getBoundingClientRect();
|
|
10809
|
+
const drawingRect = drawing.getBoundingClientRect();
|
|
10810
|
+
const pageRect = page.getBoundingClientRect();
|
|
10811
|
+
const screenScale = page.clientHeight > 0 ? pageRect.height / page.clientHeight : 1;
|
|
10812
|
+
const bottomMargin = (parseFloat(view.getComputedStyle(paragraph).marginBottom) || 0) * screenScale;
|
|
10813
|
+
const overhead = Math.max(0, paragraphRect.height - drawingRect.height);
|
|
10814
|
+
const available = this.getPageBodyBottom(page) - paragraphRect.top - overhead - bottomMargin - screenScale;
|
|
10815
|
+
const width = parseFloat(style.width), height = parseFloat(style.height);
|
|
10816
|
+
if (![available, width, height, drawingRect.height].every(Number.isFinite)
|
|
10817
|
+
|| available <= 0 || width <= 0 || height <= 0 || drawingRect.height <= available)
|
|
10818
|
+
return false;
|
|
10819
|
+
const ratio = available / drawingRect.height;
|
|
10820
|
+
drawing.style.width = `${width * ratio}px`;
|
|
10821
|
+
drawing.style.height = `${height * ratio}px`;
|
|
10822
|
+
picture.style.width = "100%";
|
|
10823
|
+
picture.style.height = "100%";
|
|
10824
|
+
drawing.dataset.docxPageFitScale = `${ratio}`;
|
|
10825
|
+
return true;
|
|
10826
|
+
}
|
|
10717
10827
|
splitTableBlock(table, page, nextArticle) {
|
|
10718
10828
|
const rows = this.tableContentRows(table);
|
|
10719
10829
|
if (rows.length <= 1)
|
|
@@ -11679,6 +11789,7 @@ section.${c}>footer { z-index: 1; }
|
|
|
11679
11789
|
children = [flow];
|
|
11680
11790
|
}
|
|
11681
11791
|
var result = this.toHTML(elem, ns.html, "p", this.wrapTabLeaderChildren(children, true));
|
|
11792
|
+
this.applyTextBoxFontInheritance(elem, style, result);
|
|
11682
11793
|
if (this.options.exposeDisplayTargets && elem.displayAddress) {
|
|
11683
11794
|
const part = this.currentPart ?? this.document.documentPart;
|
|
11684
11795
|
result.setAttribute("data-office-target", `${part.path}#${elem.displayAddress}`);
|
|
@@ -11712,6 +11823,16 @@ section.${c}>footer { z-index: 1; }
|
|
|
11712
11823
|
result.dataset.docxWidowControl = "true";
|
|
11713
11824
|
}
|
|
11714
11825
|
this.applyDocumentGridLinePitch(elem, style, result);
|
|
11826
|
+
if (this.compatibleTextBoxSpacing)
|
|
11827
|
+
this.setCssStyle(result, "text-autospace", this.textBoxAutospace(elem, style));
|
|
11828
|
+
const lineRule = (elem.lineSpacing ?? style?.paragraphProps?.lineSpacing)?.lineRule;
|
|
11829
|
+
if (this.compatibleTextBoxSpacing && ["lines", "linesAndChars"].includes(this.currentSectionProps?.docGrid?.type)
|
|
11830
|
+
&& this.paragraphUsesDocumentGrid(elem, style)
|
|
11831
|
+
&& lineRule != "exact" && lineRule != "exactly") {
|
|
11832
|
+
result.dataset.docxTextboxGridPitch = this.currentSectionProps.docGrid.linePitch;
|
|
11833
|
+
result.dataset.docxTextboxBaseLineHeight = result.style.lineHeight ?? "";
|
|
11834
|
+
result.style.flexShrink = "0";
|
|
11835
|
+
}
|
|
11715
11836
|
const leaderTab = elem.tabs?.find(t => this.isLeaderTabStop(t));
|
|
11716
11837
|
if (leaderTab) {
|
|
11717
11838
|
result.classList.add(`${this.className}-tab-leader-paragraph`);
|
|
@@ -11828,6 +11949,68 @@ section.${c}>footer { z-index: 1; }
|
|
|
11828
11949
|
}
|
|
11829
11950
|
return true;
|
|
11830
11951
|
}
|
|
11952
|
+
textBoxGridLineHeight(natural, pitch) {
|
|
11953
|
+
if (!Number.isFinite(natural) || !Number.isFinite(pitch) || natural < 0 || pitch <= 0)
|
|
11954
|
+
return null;
|
|
11955
|
+
return Math.max(1, Math.ceil((natural - 1 / 64) / pitch)) * pitch;
|
|
11956
|
+
}
|
|
11957
|
+
refreshTextBoxLineGrid(root) {
|
|
11958
|
+
const view = root.ownerDocument?.defaultView;
|
|
11959
|
+
const paragraphs = Array.from(root.querySelectorAll("[data-docx-textbox-grid-pitch]"))
|
|
11960
|
+
.filter(paragraph => this.ownsLayoutElement(paragraph));
|
|
11961
|
+
if (!view || !paragraphs.length)
|
|
11962
|
+
return;
|
|
11963
|
+
const probe = root.ownerDocument.createElement("span");
|
|
11964
|
+
Object.assign(probe.style, {
|
|
11965
|
+
position: "absolute", visibility: "hidden", pointerEvents: "none", display: "inline-block",
|
|
11966
|
+
whiteSpace: "pre", width: "max-content", margin: "0", padding: "0", border: "0", minHeight: "0"
|
|
11967
|
+
});
|
|
11968
|
+
probe.setAttribute("aria-hidden", "true");
|
|
11969
|
+
const scope = root.getRootNode();
|
|
11970
|
+
const probeParent = scope.nodeType == 11 && "host" in scope ? scope : root.ownerDocument.body;
|
|
11971
|
+
probeParent.appendChild(probe);
|
|
11972
|
+
const setFont = (target, element, lineHeight = "normal") => {
|
|
11973
|
+
const style = view.getComputedStyle(element);
|
|
11974
|
+
Object.assign(target.style, {
|
|
11975
|
+
fontFamily: style.fontFamily, fontSize: style.fontSize, fontWeight: style.fontWeight,
|
|
11976
|
+
fontStyle: style.fontStyle, fontStretch: style.fontStretch, verticalAlign: style.verticalAlign, lineHeight
|
|
11977
|
+
});
|
|
11978
|
+
};
|
|
11979
|
+
try {
|
|
11980
|
+
for (const paragraph of paragraphs) {
|
|
11981
|
+
if (paragraph.querySelector("svg, img, table, math, [data-docx-drawing]"))
|
|
11982
|
+
continue;
|
|
11983
|
+
this.preserveGridInlineMetrics(paragraph);
|
|
11984
|
+
probe.style.height = paragraph.dataset.docxTextboxGridPitch;
|
|
11985
|
+
const pitch = parseFloat(view.getComputedStyle(probe).height);
|
|
11986
|
+
setFont(probe, paragraph, paragraph.dataset.docxTextboxBaseLineHeight || "normal");
|
|
11987
|
+
probe.style.height = "auto";
|
|
11988
|
+
probe.replaceChildren();
|
|
11989
|
+
const walker = root.ownerDocument.createTreeWalker(paragraph, 4);
|
|
11990
|
+
while (walker.nextNode()) {
|
|
11991
|
+
const node = walker.currentNode;
|
|
11992
|
+
const parent = node.parentElement;
|
|
11993
|
+
if (!node.textContent || !parent || !Array.from(parent.getClientRects()).some(rect => rect.width > 0 && rect.height > 0))
|
|
11994
|
+
continue;
|
|
11995
|
+
const lineHeight = parent == paragraph ? paragraph.dataset.docxTextboxBaseLineHeight : parent.style.lineHeight;
|
|
11996
|
+
const span = root.ownerDocument.createElement("span");
|
|
11997
|
+
setFont(span, parent, lineHeight || "normal");
|
|
11998
|
+
span.style.whiteSpace = "pre";
|
|
11999
|
+
span.textContent = node.textContent.replace(/[\r\n]/g, " ");
|
|
12000
|
+
probe.appendChild(span);
|
|
12001
|
+
}
|
|
12002
|
+
if (!probe.hasChildNodes())
|
|
12003
|
+
probe.textContent = "\u00a0";
|
|
12004
|
+
const natural = parseFloat(view.getComputedStyle(probe).height);
|
|
12005
|
+
const height = this.textBoxGridLineHeight(natural, pitch);
|
|
12006
|
+
if (height != null)
|
|
12007
|
+
paragraph.style.lineHeight = `${Math.round(height * 1000) / 1000}px`;
|
|
12008
|
+
}
|
|
12009
|
+
}
|
|
12010
|
+
finally {
|
|
12011
|
+
probe.remove();
|
|
12012
|
+
}
|
|
12013
|
+
}
|
|
11831
12014
|
async prepareLayout(root, generation = getLayoutGeneration(root)) {
|
|
11832
12015
|
if (!root.isConnected || !generation.isCurrent())
|
|
11833
12016
|
return;
|
|
@@ -11848,6 +12031,7 @@ section.${c}>footer { z-index: 1; }
|
|
|
11848
12031
|
(owner ?? root).appendChild(this.commentLayer.root);
|
|
11849
12032
|
}
|
|
11850
12033
|
}
|
|
12034
|
+
this.refreshTextBoxLineGrid(root);
|
|
11851
12035
|
this.refreshTabStops(root);
|
|
11852
12036
|
this.refreshAnchors(root);
|
|
11853
12037
|
await this.commentLayer?.bind(root, generation);
|
|
@@ -12224,17 +12408,31 @@ section.${c}>footer { z-index: 1; }
|
|
|
12224
12408
|
return result;
|
|
12225
12409
|
}
|
|
12226
12410
|
renderImage(elem) {
|
|
12227
|
-
|
|
12411
|
+
const rect = elem.srcRect;
|
|
12412
|
+
const cropWidth = rect ? 1 - rect[0] - rect[2] : 1;
|
|
12413
|
+
const cropHeight = rect ? 1 - rect[1] - rect[3] : 1;
|
|
12414
|
+
const cropped = rect?.length === 4 && rect.some(x => x !== 0)
|
|
12415
|
+
&& rect.every(Number.isFinite) && cropWidth > 0 && cropHeight > 0
|
|
12416
|
+
&& Number.isFinite(100 / cropWidth) && Number.isFinite(100 / cropHeight);
|
|
12417
|
+
const result = this.toHTML(elem, ns.html, cropped ? "span" : "img", []);
|
|
12418
|
+
const image = cropped ? this.h({ tagName: "img" }) : result;
|
|
12228
12419
|
let transform = elem.cssStyle?.transform;
|
|
12229
12420
|
if (elem.alt)
|
|
12230
|
-
|
|
12421
|
+
image.alt = elem.alt;
|
|
12231
12422
|
if (elem.title)
|
|
12232
|
-
|
|
12233
|
-
if (
|
|
12234
|
-
|
|
12235
|
-
|
|
12236
|
-
result.style.
|
|
12237
|
-
|
|
12423
|
+
image.title = elem.title;
|
|
12424
|
+
if (cropped) {
|
|
12425
|
+
result.setAttribute("data-docx-image-crop", "true");
|
|
12426
|
+
result.style.overflow = "hidden";
|
|
12427
|
+
result.style.position = "relative";
|
|
12428
|
+
Object.assign(image.style, {
|
|
12429
|
+
position: "absolute", display: "block", margin: "0",
|
|
12430
|
+
left: `${ -100 * rect[0] / cropWidth}%`,
|
|
12431
|
+
top: `${ -100 * rect[1] / cropHeight}%`,
|
|
12432
|
+
width: `${100 / cropWidth}%`, height: `${100 / cropHeight}%`,
|
|
12433
|
+
maxWidth: "none", maxHeight: "none", objectFit: "fill"
|
|
12434
|
+
});
|
|
12435
|
+
result.appendChild(image);
|
|
12238
12436
|
}
|
|
12239
12437
|
if (elem.rotation)
|
|
12240
12438
|
transform = `rotate(${elem.rotation}deg) ${transform ?? ''}`;
|
|
@@ -12242,7 +12440,7 @@ section.${c}>footer { z-index: 1; }
|
|
|
12242
12440
|
if (this.document && elem.src) {
|
|
12243
12441
|
this.tasks.push(this.document.loadDocumentImage(elem.src, this.currentPart, this.options.externalResourcePolicy).then(x => {
|
|
12244
12442
|
if (x)
|
|
12245
|
-
|
|
12443
|
+
image.setAttribute("src", x);
|
|
12246
12444
|
}));
|
|
12247
12445
|
}
|
|
12248
12446
|
return result;
|
|
@@ -12264,9 +12462,17 @@ section.${c}>footer { z-index: 1; }
|
|
|
12264
12462
|
}
|
|
12265
12463
|
renderDmlTextBox(elem) {
|
|
12266
12464
|
const previousParagraph = this.currentParagraph;
|
|
12465
|
+
const previousSpacing = this.compatibleTextBoxSpacing;
|
|
12267
12466
|
this.currentParagraph = null;
|
|
12268
|
-
|
|
12269
|
-
|
|
12467
|
+
this.compatibleTextBoxSpacing = elem.props?.textCompatibleLineSpacing === true;
|
|
12468
|
+
let children;
|
|
12469
|
+
try {
|
|
12470
|
+
children = this.renderElements(elem.children);
|
|
12471
|
+
}
|
|
12472
|
+
finally {
|
|
12473
|
+
this.currentParagraph = previousParagraph;
|
|
12474
|
+
this.compatibleTextBoxSpacing = previousSpacing;
|
|
12475
|
+
}
|
|
12270
12476
|
const content = this.h({
|
|
12271
12477
|
tagName: "div",
|
|
12272
12478
|
className: `${this.className}-shape-textbox`,
|
|
@@ -13967,6 +14173,7 @@ section.${c}>footer { z-index: 1; }
|
|
|
13967
14173
|
}
|
|
13968
14174
|
const result = this.toHTML(elem, ns.html, "span", children);
|
|
13969
14175
|
const style = this.findStyle(elem.styleName);
|
|
14176
|
+
this.applyTextBoxFontInheritance(elem, style, result);
|
|
13970
14177
|
const snapToGrid = elem.cssStyle?.["--docx-snap-to-grid"] ?? this.styleValue(style, "span", "--docx-snap-to-grid");
|
|
13971
14178
|
if (/^(0|false|off)$/i.test(snapToGrid ?? ""))
|
|
13972
14179
|
result.style.letterSpacing = "normal";
|
|
@@ -14294,6 +14501,11 @@ section.${c}>footer { z-index: 1; }
|
|
|
14294
14501
|
rect.setAttribute("width", `${bounds.width}`);
|
|
14295
14502
|
rect.setAttribute("height", `${bounds.height}`);
|
|
14296
14503
|
this.applyVmlAttributes(rect, elem.attrs, new Set(["x", "y", "width", "height"]));
|
|
14504
|
+
if (elem.roundRectArc != null) {
|
|
14505
|
+
const radius = Math.min(bounds.width, bounds.height) * elem.roundRectArc / 2;
|
|
14506
|
+
rect.setAttribute("rx", `${radius}`);
|
|
14507
|
+
rect.setAttribute("ry", `${radius}`);
|
|
14508
|
+
}
|
|
14297
14509
|
result = rect;
|
|
14298
14510
|
break;
|
|
14299
14511
|
}
|
|
@@ -14707,8 +14919,11 @@ section.${c}>footer { z-index: 1; }
|
|
|
14707
14919
|
if (!style)
|
|
14708
14920
|
return style;
|
|
14709
14921
|
const result = { ...style };
|
|
14710
|
-
if (normalized)
|
|
14922
|
+
if (normalized) {
|
|
14711
14923
|
delete result["font-family"];
|
|
14924
|
+
delete result["--docx-latin-font-family"];
|
|
14925
|
+
delete result["--docx-partial-eastasia-font"];
|
|
14926
|
+
}
|
|
14712
14927
|
return result;
|
|
14713
14928
|
}
|
|
14714
14929
|
normalizeNumberingLevelText(text, format, runStyle) {
|
|
@@ -12336,6 +12336,8 @@
|
|
|
12336
12336
|
result.tagName = "g";
|
|
12337
12337
|
result.isGroup = true;
|
|
12338
12338
|
break;
|
|
12339
|
+
case "roundrect":
|
|
12340
|
+
result.roundRectArc = 0.2;
|
|
12339
12341
|
case "rect":
|
|
12340
12342
|
result.tagName = "rect";
|
|
12341
12343
|
Object.assign(result.attrs, { width: '100%', height: '100%' });
|
|
@@ -12370,6 +12372,10 @@
|
|
|
12370
12372
|
case "coordsize":
|
|
12371
12373
|
result.coordSize = parseNumericPoint(at.value);
|
|
12372
12374
|
break;
|
|
12375
|
+
case "arcsize":
|
|
12376
|
+
if (result.roundRectArc != null)
|
|
12377
|
+
result.roundRectArc = parseRoundRectArc(at.value);
|
|
12378
|
+
break;
|
|
12373
12379
|
case "inset":
|
|
12374
12380
|
if (result.textInset) {
|
|
12375
12381
|
const values = at.value.trim().split(/\s*,\s*|\s+/);
|
|
@@ -12467,6 +12473,13 @@
|
|
|
12467
12473
|
}
|
|
12468
12474
|
return result;
|
|
12469
12475
|
}
|
|
12476
|
+
function parseRoundRectArc(value) {
|
|
12477
|
+
const source = value.trim();
|
|
12478
|
+
if (!/^[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:%|f)?$/i.test(source))
|
|
12479
|
+
return 0.2;
|
|
12480
|
+
const fraction = parseFloat(source) / (/%$/.test(source) ? 100 : /f$/i.test(source) ? 65536 : 1);
|
|
12481
|
+
return Number.isFinite(fraction) ? Math.max(0, Math.min(1, fraction)) : 0.2;
|
|
12482
|
+
}
|
|
12470
12483
|
function vmlImageAttrs(el) {
|
|
12471
12484
|
const cropLeft = parseVmlFraction(globalXmlParser.attr(el, "cropleft"));
|
|
12472
12485
|
const cropTop = parseVmlFraction(globalXmlParser.attr(el, "croptop"));
|
|
@@ -14024,6 +14037,9 @@
|
|
|
14024
14037
|
const prstGeom = globalXmlParser.element(spPr, "prstGeom");
|
|
14025
14038
|
const fillStyle = this.parseDmlFillStyle(spPr);
|
|
14026
14039
|
const lineStyle = this.parseDmlLineStyle(globalXmlParser.element(spPr, "ln"));
|
|
14040
|
+
const roundedRadius = !geom ? this.parseDmlRoundRectRadius(spPr, bounds.width, bounds.height, transform) : null;
|
|
14041
|
+
if (roundedRadius != null)
|
|
14042
|
+
cssStyle["border-radius"] = roundedRadius;
|
|
14027
14043
|
if (geom) {
|
|
14028
14044
|
const path = this.parseDmlCustomGeometry(geom);
|
|
14029
14045
|
return {
|
|
@@ -14061,6 +14077,23 @@
|
|
|
14061
14077
|
alt: cNvPr ? globalXmlParser.attr(cNvPr, "descr") : null
|
|
14062
14078
|
};
|
|
14063
14079
|
}
|
|
14080
|
+
parseDmlRoundRectRadius(spPr, width, height, transform) {
|
|
14081
|
+
const preset = globalXmlParser.element(spPr, "prstGeom");
|
|
14082
|
+
if (!preset || globalXmlParser.attr(preset, "prst") != "roundRect")
|
|
14083
|
+
return null;
|
|
14084
|
+
const guides = globalXmlParser.element(preset, "avLst");
|
|
14085
|
+
const adjustment = (guides ? globalXmlParser.elements(guides, "gd") : [])
|
|
14086
|
+
.find(guide => globalXmlParser.attr(guide, "name") == "adj");
|
|
14087
|
+
const formula = adjustment ? globalXmlParser.attr(adjustment, "fmla") : "";
|
|
14088
|
+
const value = /^val\s+[+-]?(?:\d+(?:\.\d*)?|\.\d+)\s*$/.test(formula ?? "")
|
|
14089
|
+
? Number(formula.trim().split(/\s+/)[1]) : 16667;
|
|
14090
|
+
const fraction = Math.max(0, Math.min(50000, value)) / 100000;
|
|
14091
|
+
const scaleX = Math.abs(transform?.scaleX ?? 1);
|
|
14092
|
+
const scaleY = Math.abs(transform?.scaleY ?? 1);
|
|
14093
|
+
const radius = Number.isFinite(scaleX) && Number.isFinite(scaleY) && scaleX > 0 && scaleY > 0
|
|
14094
|
+
? Math.min(width / scaleX, height / scaleY) * fraction : 0;
|
|
14095
|
+
return `${this.pt(Number.isFinite(radius * scaleX) ? radius * scaleX : 0)} / ${this.pt(Number.isFinite(radius * scaleY) ? radius * scaleY : 0)}`;
|
|
14096
|
+
}
|
|
14064
14097
|
parseDmlShapeBounds(spPr, transform) {
|
|
14065
14098
|
const xfrm = globalXmlParser.element(spPr, "xfrm");
|
|
14066
14099
|
const off = xfrm ? globalXmlParser.element(xfrm, "off") : null;
|
|
@@ -14197,6 +14230,7 @@
|
|
|
14197
14230
|
return {
|
|
14198
14231
|
dmlTextBox: true,
|
|
14199
14232
|
textInsets: textInsetStyle,
|
|
14233
|
+
textCompatibleLineSpacing: bodyPr ? globalXmlParser.boolAttr(bodyPr, "compatLnSpc", false) : false,
|
|
14200
14234
|
textVerticalAlign: this.dmlVerticalAlign(bodyPr)
|
|
14201
14235
|
};
|
|
14202
14236
|
}
|
|
@@ -14296,6 +14330,10 @@
|
|
|
14296
14330
|
}
|
|
14297
14331
|
parseDmlShapeProperties(elem, style) {
|
|
14298
14332
|
const xfrm = globalXmlParser.element(elem, "xfrm");
|
|
14333
|
+
const bounds = this.parseDmlShapeBounds(elem);
|
|
14334
|
+
const roundedRadius = this.parseDmlRoundRectRadius(elem, bounds.width, bounds.height);
|
|
14335
|
+
if (roundedRadius != null)
|
|
14336
|
+
style["border-radius"] = roundedRadius;
|
|
14299
14337
|
if (xfrm) {
|
|
14300
14338
|
for (const n of globalXmlParser.elements(xfrm)) {
|
|
14301
14339
|
switch (n.localName) {
|
|
@@ -14740,6 +14778,10 @@
|
|
|
14740
14778
|
case "textAlignment":
|
|
14741
14779
|
style["vertical-align"] = values.valueOfTextAlignment(c);
|
|
14742
14780
|
break;
|
|
14781
|
+
case "autoSpaceDE":
|
|
14782
|
+
case "autoSpaceDN":
|
|
14783
|
+
style[c.localName == "autoSpaceDE" ? "--docx-auto-space-de" : "--docx-auto-space-dn"] = `${globalXmlParser.boolAttr(c, "val", true)}`;
|
|
14784
|
+
break;
|
|
14743
14785
|
case "color":
|
|
14744
14786
|
style["color"] = xmlUtil.colorAttr(c, "val", null, autos.color);
|
|
14745
14787
|
break;
|
|
@@ -15038,6 +15080,7 @@
|
|
|
15038
15080
|
fonts.push(...[asciiTheme, hAnsiTheme]
|
|
15039
15081
|
.filter(x => x)
|
|
15040
15082
|
.map(x => x.startsWith("var(") ? x : encloseFontFamily(x)));
|
|
15083
|
+
const latinFonts = fonts.slice();
|
|
15041
15084
|
if (eastAsia) {
|
|
15042
15085
|
fonts.push(...eastAsianFontFamilyStack(eastAsia, eastAsiaScript));
|
|
15043
15086
|
}
|
|
@@ -15051,6 +15094,9 @@
|
|
|
15051
15094
|
if (fonts.length > 0) {
|
|
15052
15095
|
const currentFonts = (style["font-family"] ?? "").split(',').map(x => x.trim()).filter(x => x);
|
|
15053
15096
|
style["font-family"] = [...new Set([...currentFonts, ...fonts])].join(', ');
|
|
15097
|
+
if (latinFonts.length)
|
|
15098
|
+
style["--docx-latin-font-family"] = [...new Set(latinFonts)].join(', ');
|
|
15099
|
+
style["--docx-partial-eastasia-font"] = !latinFonts.length && (eastAsia || eastAsiaTheme) && !cs && !csTheme ? "true" : "false";
|
|
15054
15100
|
}
|
|
15055
15101
|
}
|
|
15056
15102
|
scriptThemeFontValue(value, language) {
|
package/dist/html-renderer.d.ts
CHANGED
|
@@ -159,6 +159,7 @@ export declare class HtmlRenderer {
|
|
|
159
159
|
headingIndex: number;
|
|
160
160
|
tasks: Promise<any>[];
|
|
161
161
|
postRenderTasks: any[];
|
|
162
|
+
private compatibleTextBoxSpacing;
|
|
162
163
|
h: typeof h;
|
|
163
164
|
/**
|
|
164
165
|
* 暗色模式下,任意字面量颜色(非 --docx-*-color 主题/auto 变量)都会被包装成
|
|
@@ -230,6 +231,8 @@ export declare class HtmlRenderer {
|
|
|
230
231
|
styleValuesForTarget(style: IDomStyle, target: string): Record<string, string>;
|
|
231
232
|
styleValue(style: IDomStyle, target: string, name: string): string;
|
|
232
233
|
effectiveParagraphCssValue(elem: WmlParagraph, style: IDomStyle, name: string): string;
|
|
234
|
+
textBoxAutospace(elem: WmlParagraph, style: IDomStyle, supports?: (value: string) => boolean): string;
|
|
235
|
+
applyTextBoxFontInheritance(elem: OpenXmlElement, style: IDomStyle, result: HTMLElement): void;
|
|
233
236
|
isInsideTableCell(elem: OpenXmlElement): boolean;
|
|
234
237
|
paragraphUsesDocumentGrid(elem: WmlParagraph, style: IDomStyle): boolean;
|
|
235
238
|
setCssStyle(element: HTMLElement, name: string, value: string): void;
|
|
@@ -286,6 +289,11 @@ export declare class HtmlRenderer {
|
|
|
286
289
|
refreshDynamicHeaderFooters(pages: HTMLElement[]): Promise<void>;
|
|
287
290
|
splitOverflowBlock(block: HTMLElement, page: HTMLElement, nextArticle: HTMLElement): boolean;
|
|
288
291
|
splitParagraphBlock(paragraph: HTMLElement, page: HTMLElement, nextArticle: HTMLElement): boolean;
|
|
292
|
+
/** Fixed-page previews must not move one oversized picture through an
|
|
293
|
+
* unlimited sequence of empty pages. Fit its entire frame proportionally,
|
|
294
|
+
* only when it is the first body block and cannot fit on an otherwise empty
|
|
295
|
+
* page. Ordinary paragraphs and pictures fitting a fresh page are unchanged. */
|
|
296
|
+
fitOversizedInlinePicture(paragraph: HTMLElement, page: HTMLElement): boolean;
|
|
289
297
|
splitTableBlock(table: HTMLTableElement, page: HTMLElement, nextArticle: HTMLElement): boolean;
|
|
290
298
|
cloneBlockShell<T extends HTMLElement>(element: T): T;
|
|
291
299
|
tableContentRows(table: HTMLTableElement): HTMLTableRowElement[];
|
|
@@ -339,6 +347,8 @@ export declare class HtmlRenderer {
|
|
|
339
347
|
renderAnchoredElement(model: OpenXmlElement, element: HTMLElement | SVGElement): HTMLElement | SVGElement;
|
|
340
348
|
refreshAnchors(root: HTMLElement): void;
|
|
341
349
|
ownsLayoutElement(element: Element): boolean;
|
|
350
|
+
textBoxGridLineHeight(natural: number, pitch: number): number | null;
|
|
351
|
+
refreshTextBoxLineGrid(root: HTMLElement): void;
|
|
342
352
|
prepareLayout(root: HTMLElement, generation?: LayoutGeneration): Promise<void>;
|
|
343
353
|
disposeLayout(): void;
|
|
344
354
|
renderHyperlink(elem: WmlHyperlink): HTMLAnchorElement;
|
package/dist/vml/vml.d.ts
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@file-viewer/docx",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.39",
|
|
4
4
|
"description": "Pure HTML DOCX renderer fork for browser-based Word previews.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"keywords": [
|
|
@@ -43,7 +43,7 @@
|
|
|
43
43
|
},
|
|
44
44
|
"overrides": {
|
|
45
45
|
"brace-expansion": "1.1.21",
|
|
46
|
-
"serialize-javascript": "7.1.
|
|
46
|
+
"serialize-javascript": "7.1.3"
|
|
47
47
|
},
|
|
48
48
|
"scripts": {
|
|
49
49
|
"build": "node scripts/clean-dist.cjs && rollup --config rollup.config.mjs && tsc --project tsconfig.build.json && node scripts/copy-build-assets.cjs",
|