@file-viewer/docx 0.3.36 → 0.3.41

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.
@@ -9010,7 +9010,7 @@
9010
9010
  props.fontSize = xml.lengthAttr(elem, "val", LengthUsage.FontSize);
9011
9011
  break;
9012
9012
  case "szCs":
9013
- props.fontSize ?? (props.fontSize = xml.lengthAttr(elem, "val", LengthUsage.FontSize));
9013
+ props.complexFontSize = xml.lengthAttr(elem, "val", LengthUsage.FontSize);
9014
9014
  break;
9015
9015
  default:
9016
9016
  return false;
@@ -10268,6 +10268,8 @@
10268
10268
  for (const c of xml.elements(el)) {
10269
10269
  if (c.localName == "adjustLineHeightInTable")
10270
10270
  result.adjustLineHeightInTable = xml.boolAttr(c, "val", true);
10271
+ else if (c.localName == "doNotUseHTMLParagraphAutoSpacing")
10272
+ result.doNotUseHTMLParagraphAutoSpacing = xml.boolAttr(c, "val", true);
10271
10273
  }
10272
10274
  break;
10273
10275
  }
@@ -12336,6 +12338,8 @@
12336
12338
  result.tagName = "g";
12337
12339
  result.isGroup = true;
12338
12340
  break;
12341
+ case "roundrect":
12342
+ result.roundRectArc = 0.2;
12339
12343
  case "rect":
12340
12344
  result.tagName = "rect";
12341
12345
  Object.assign(result.attrs, { width: '100%', height: '100%' });
@@ -12370,6 +12374,10 @@
12370
12374
  case "coordsize":
12371
12375
  result.coordSize = parseNumericPoint(at.value);
12372
12376
  break;
12377
+ case "arcsize":
12378
+ if (result.roundRectArc != null)
12379
+ result.roundRectArc = parseRoundRectArc(at.value);
12380
+ break;
12373
12381
  case "inset":
12374
12382
  if (result.textInset) {
12375
12383
  const values = at.value.trim().split(/\s*,\s*|\s+/);
@@ -12467,6 +12475,13 @@
12467
12475
  }
12468
12476
  return result;
12469
12477
  }
12478
+ function parseRoundRectArc(value) {
12479
+ const source = value.trim();
12480
+ if (!/^[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:%|f)?$/i.test(source))
12481
+ return 0.2;
12482
+ const fraction = parseFloat(source) / (/%$/.test(source) ? 100 : /f$/i.test(source) ? 65536 : 1);
12483
+ return Number.isFinite(fraction) ? Math.max(0, Math.min(1, fraction)) : 0.2;
12484
+ }
12470
12485
  function vmlImageAttrs(el) {
12471
12486
  const cropLeft = parseVmlFraction(globalXmlParser.attr(el, "cropleft"));
12472
12487
  const cropTop = parseVmlFraction(globalXmlParser.attr(el, "croptop"));
@@ -12626,6 +12641,14 @@
12626
12641
  };
12627
12642
  }
12628
12643
 
12644
+ const WORD_PARAGRAPH_LINE_UNIT_PT = 12;
12645
+ function paragraphSpacingDeclaration(points, lines) {
12646
+ const lineValue = Number.isFinite(lines) && lines >= 0 ? `${(lines * WORD_PARAGRAPH_LINE_UNIT_PT / 100).toFixed(2)}pt` : null;
12647
+ const validPoints = /^(?:\d+(?:\.\d*)?|\.\d+)pt$/i.test(points ?? "");
12648
+ return { lines: lineValue, fallback: lines === 0 && validPoints ? points : lineValue ?? points };
12649
+ }
12650
+ const WORD_HTML_AUTO_SPACING_PT = 14;
12651
+
12629
12652
  var autos = {
12630
12653
  shd: "inherit",
12631
12654
  color: "var(--docx-auto-color, black)",
@@ -14024,6 +14047,9 @@
14024
14047
  const prstGeom = globalXmlParser.element(spPr, "prstGeom");
14025
14048
  const fillStyle = this.parseDmlFillStyle(spPr);
14026
14049
  const lineStyle = this.parseDmlLineStyle(globalXmlParser.element(spPr, "ln"));
14050
+ const roundedRadius = !geom ? this.parseDmlRoundRectRadius(spPr, bounds.width, bounds.height, transform) : null;
14051
+ if (roundedRadius != null)
14052
+ cssStyle["border-radius"] = roundedRadius;
14027
14053
  if (geom) {
14028
14054
  const path = this.parseDmlCustomGeometry(geom);
14029
14055
  return {
@@ -14061,6 +14087,23 @@
14061
14087
  alt: cNvPr ? globalXmlParser.attr(cNvPr, "descr") : null
14062
14088
  };
14063
14089
  }
14090
+ parseDmlRoundRectRadius(spPr, width, height, transform) {
14091
+ const preset = globalXmlParser.element(spPr, "prstGeom");
14092
+ if (!preset || globalXmlParser.attr(preset, "prst") != "roundRect")
14093
+ return null;
14094
+ const guides = globalXmlParser.element(preset, "avLst");
14095
+ const adjustment = (guides ? globalXmlParser.elements(guides, "gd") : [])
14096
+ .find(guide => globalXmlParser.attr(guide, "name") == "adj");
14097
+ const formula = adjustment ? globalXmlParser.attr(adjustment, "fmla") : "";
14098
+ const value = /^val\s+[+-]?(?:\d+(?:\.\d*)?|\.\d+)\s*$/.test(formula ?? "")
14099
+ ? Number(formula.trim().split(/\s+/)[1]) : 16667;
14100
+ const fraction = Math.max(0, Math.min(50000, value)) / 100000;
14101
+ const scaleX = Math.abs(transform?.scaleX ?? 1);
14102
+ const scaleY = Math.abs(transform?.scaleY ?? 1);
14103
+ const radius = Number.isFinite(scaleX) && Number.isFinite(scaleY) && scaleX > 0 && scaleY > 0
14104
+ ? Math.min(width / scaleX, height / scaleY) * fraction : 0;
14105
+ return `${this.pt(Number.isFinite(radius * scaleX) ? radius * scaleX : 0)} / ${this.pt(Number.isFinite(radius * scaleY) ? radius * scaleY : 0)}`;
14106
+ }
14064
14107
  parseDmlShapeBounds(spPr, transform) {
14065
14108
  const xfrm = globalXmlParser.element(spPr, "xfrm");
14066
14109
  const off = xfrm ? globalXmlParser.element(xfrm, "off") : null;
@@ -14197,6 +14240,7 @@
14197
14240
  return {
14198
14241
  dmlTextBox: true,
14199
14242
  textInsets: textInsetStyle,
14243
+ textCompatibleLineSpacing: bodyPr ? globalXmlParser.boolAttr(bodyPr, "compatLnSpc", false) : false,
14200
14244
  textVerticalAlign: this.dmlVerticalAlign(bodyPr)
14201
14245
  };
14202
14246
  }
@@ -14296,6 +14340,10 @@
14296
14340
  }
14297
14341
  parseDmlShapeProperties(elem, style) {
14298
14342
  const xfrm = globalXmlParser.element(elem, "xfrm");
14343
+ const bounds = this.parseDmlShapeBounds(elem);
14344
+ const roundedRadius = this.parseDmlRoundRectRadius(elem, bounds.width, bounds.height);
14345
+ if (roundedRadius != null)
14346
+ style["border-radius"] = roundedRadius;
14299
14347
  if (xfrm) {
14300
14348
  for (const n of globalXmlParser.elements(xfrm)) {
14301
14349
  switch (n.localName) {
@@ -14740,6 +14788,10 @@
14740
14788
  case "textAlignment":
14741
14789
  style["vertical-align"] = values.valueOfTextAlignment(c);
14742
14790
  break;
14791
+ case "autoSpaceDE":
14792
+ case "autoSpaceDN":
14793
+ style[c.localName == "autoSpaceDE" ? "--docx-auto-space-de" : "--docx-auto-space-dn"] = `${globalXmlParser.boolAttr(c, "val", true)}`;
14794
+ break;
14743
14795
  case "color":
14744
14796
  style["color"] = xmlUtil.colorAttr(c, "val", null, autos.color);
14745
14797
  break;
@@ -14747,8 +14799,10 @@
14747
14799
  style["font-size"] = style["min-height"] = globalXmlParser.lengthAttr(c, "val", LengthUsage.FontSize);
14748
14800
  break;
14749
14801
  case "szCs":
14750
- style["font-size"] ?? (style["font-size"] = globalXmlParser.lengthAttr(c, "val", LengthUsage.FontSize));
14751
- style["min-height"] ?? (style["min-height"] = style["font-size"]);
14802
+ style["--docx-complex-font-size"] = globalXmlParser.lengthAttr(c, "val", LengthUsage.FontSize);
14803
+ break;
14804
+ case "cs":
14805
+ style["--docx-complex-script"] = `${globalXmlParser.boolAttr(c, "val", true)}`;
14752
14806
  break;
14753
14807
  case "shd":
14754
14808
  style["background-color"] = xmlUtil.colorAttr(c, "fill", null, autos.shd);
@@ -14812,6 +14866,7 @@
14812
14866
  style["font-variant"] = globalXmlParser.boolAttr(c, "val", true) ? "small-caps" : "none";
14813
14867
  break;
14814
14868
  case "rtl":
14869
+ style["--docx-rtl-run"] = `${globalXmlParser.boolAttr(c, "val", true)}`;
14815
14870
  if (globalXmlParser.boolAttr(c, "val", true)) {
14816
14871
  style["direction"] = "rtl";
14817
14872
  style["unicode-bidi"] = "embed";
@@ -14944,9 +14999,11 @@
14944
14999
  if (globalXmlParser.boolAttr(c, "val", true))
14945
15000
  style["display"] = "none";
14946
15001
  break;
15002
+ case "contextualSpacing":
15003
+ style["--docx-contextual-spacing"] = `${globalXmlParser.boolAttr(c, "val", true)}`;
15004
+ break;
14947
15005
  case "tabs":
14948
15006
  case "outlineLvl":
14949
- case "contextualSpacing":
14950
15007
  case "tblStyleColBandSize":
14951
15008
  case "tblStyleRowBandSize":
14952
15009
  case "suppressLineNumbers":
@@ -15038,6 +15095,7 @@
15038
15095
  fonts.push(...[asciiTheme, hAnsiTheme]
15039
15096
  .filter(x => x)
15040
15097
  .map(x => x.startsWith("var(") ? x : encloseFontFamily(x)));
15098
+ const latinFonts = fonts.slice();
15041
15099
  if (eastAsia) {
15042
15100
  fonts.push(...eastAsianFontFamilyStack(eastAsia, eastAsiaScript));
15043
15101
  }
@@ -15051,6 +15109,21 @@
15051
15109
  if (fonts.length > 0) {
15052
15110
  const currentFonts = (style["font-family"] ?? "").split(',').map(x => x.trim()).filter(x => x);
15053
15111
  style["font-family"] = [...new Set([...currentFonts, ...fonts])].join(', ');
15112
+ if (latinFonts.length)
15113
+ style["--docx-latin-font-family"] = [...new Set(latinFonts)].join(', ');
15114
+ style["--docx-partial-eastasia-font"] = !latinFonts.length && (eastAsia || eastAsiaTheme) && !cs && !csTheme ? "true" : "false";
15115
+ }
15116
+ const slots = {
15117
+ ascii: [ascii, asciiTheme], hansi: [hAnsi, hAnsiTheme],
15118
+ eastasia: [eastAsia, eastAsiaTheme], complex: [cs, csTheme]
15119
+ };
15120
+ for (const [slot, [direct, themed]] of Object.entries(slots)) {
15121
+ const selected = themed || direct;
15122
+ if (!selected)
15123
+ continue;
15124
+ const family = selected.startsWith("var(") ? selected
15125
+ : expandDirectFont(selected, slot == "complex" ? themeScriptForLanguage(languages.bidi) : eastAsiaScript).join(', ');
15126
+ style[`--docx-font-${slot}`] = family;
15054
15127
  }
15055
15128
  }
15056
15129
  scriptThemeFontValue(value, language) {
@@ -15096,18 +15169,28 @@
15096
15169
  var afterAuto = globalXmlParser.boolAttr(node, "afterAutospacing", false);
15097
15170
  var line = globalXmlParser.intAttr(node, "line", null);
15098
15171
  var lineRule = globalXmlParser.attr(node, "lineRule");
15099
- if (beforeAuto)
15100
- style["margin-top"] = "auto";
15101
- else if (Number.isFinite(beforeLines) && beforeLines >= 0)
15102
- style["margin-top"] = `${(beforeLines / 100).toFixed(2)}em`;
15103
- else if (before)
15104
- style["margin-top"] = before;
15105
- if (afterAuto)
15106
- style["margin-bottom"] = "auto";
15107
- else if (Number.isFinite(afterLines) && afterLines >= 0)
15108
- style["margin-bottom"] = `${(afterLines / 100).toFixed(2)}em`;
15109
- else if (after)
15110
- style["margin-bottom"] = after;
15172
+ for (const [side, margin, points, lines, auto] of [
15173
+ ["before", "margin-top", before, beforeLines, beforeAuto],
15174
+ ["after", "margin-bottom", after, afterLines, afterAuto]
15175
+ ]) {
15176
+ const prefix = `--docx-spacing-${side}`;
15177
+ const { lines: lineValue, fallback } = paragraphSpacingDeclaration(points, lines);
15178
+ const autoSpecified = globalXmlParser.attr(node, `${side}Autospacing`) != null;
15179
+ if (points != null)
15180
+ style[`${prefix}-points`] = points;
15181
+ if (lineValue != null)
15182
+ style[`${prefix}-lines`] = lineValue;
15183
+ if (fallback != null)
15184
+ style[`${prefix}-fallback`] = fallback;
15185
+ if (autoSpecified)
15186
+ style[`${prefix}-auto`] = `${auto}`;
15187
+ if (auto)
15188
+ style[margin] = `${WORD_HTML_AUTO_SPACING_PT}pt`;
15189
+ else if (fallback != null)
15190
+ style[margin] = fallback;
15191
+ else if (autoSpecified)
15192
+ style[margin] = "0pt";
15193
+ }
15111
15194
  if (line !== null) {
15112
15195
  switch (lineRule ?? "auto") {
15113
15196
  case "auto":
@@ -110,6 +110,9 @@ export declare class HtmlRenderer {
110
110
  options: Options;
111
111
  /** styleId 到样式对象的索引,渲染段落/run/表格时用于快速合并继承样式。 */
112
112
  styleMap: Record<string, IDomStyle>;
113
+ private defaultParagraphStyle;
114
+ private defaultCharacterStyle;
115
+ private defaultRunStyle;
113
116
  currentPart: Part;
114
117
  currentSectionProps: SectionProperties;
115
118
  currentPageNumber: number;
@@ -159,6 +162,8 @@ export declare class HtmlRenderer {
159
162
  headingIndex: number;
160
163
  tasks: Promise<any>[];
161
164
  postRenderTasks: any[];
165
+ private compatibleTextBoxSpacing;
166
+ private paragraphSpacingStates;
162
167
  h: typeof h;
163
168
  /**
164
169
  * 暗色模式下,任意字面量颜色(非 --docx-*-color 主题/auto 变量)都会被包装成
@@ -177,6 +182,7 @@ export declare class HtmlRenderer {
177
182
  eastAsia: string;
178
183
  bidi: string;
179
184
  };
185
+ addThemeFontFallbackVariables(variables: Record<string, string>, size: "major" | "minor", font: DmlFormInfo): void;
180
186
  addThemeScriptFontVariables(variables: Record<string, string>, size: "major" | "minor", font: DmlFormInfo): void;
181
187
  themeFontFamily(font: DmlFormInfo, slot: "latin" | "eastAsia" | "complexScript", language: string, scriptOverride?: string): string;
182
188
  renderFontTable(fontsPart: FontTablePart): Promise<any[]>;
@@ -229,8 +235,18 @@ export declare class HtmlRenderer {
229
235
  }[];
230
236
  styleValuesForTarget(style: IDomStyle, target: string): Record<string, string>;
231
237
  styleValue(style: IDomStyle, target: string, name: string): string;
238
+ /** Resolve these per-property values even when basedOn styles occur later in XML. */
239
+ styleValueLevels(style: IDomStyle, target: string): Record<string, string>[];
240
+ private effectiveParagraphNumbering;
241
+ applyParagraphAutoSpacing(elem: WmlParagraph, style: IDomStyle, result: HTMLElement): void;
242
+ private applyFlowParagraphSpacing;
232
243
  effectiveParagraphCssValue(elem: WmlParagraph, style: IDomStyle, name: string): string;
244
+ textBoxAutospace(elem: WmlParagraph, style: IDomStyle, supports?: (value: string) => boolean): string;
245
+ applyTextBoxFontInheritance(elem: OpenXmlElement, style: IDomStyle, result: HTMLElement): void;
246
+ /** 字体槽独立继承;不改解析树、文字节点或 display target 的对应关系。 */
247
+ applyRunFontSlots(elem: OpenXmlElement, style: IDomStyle, result: HTMLElement): void;
233
248
  isInsideTableCell(elem: OpenXmlElement): boolean;
249
+ paragraphSnapToGrid(elem: WmlParagraph, style: IDomStyle): string;
234
250
  paragraphUsesDocumentGrid(elem: WmlParagraph, style: IDomStyle): boolean;
235
251
  setCssStyle(element: HTMLElement, name: string, value: string): void;
236
252
  applyDocumentGridMinimumHeight(elem: WmlParagraph, style: IDomStyle, result: HTMLElement, linePitch: string): void;
@@ -286,7 +302,15 @@ export declare class HtmlRenderer {
286
302
  refreshDynamicHeaderFooters(pages: HTMLElement[]): Promise<void>;
287
303
  splitOverflowBlock(block: HTMLElement, page: HTMLElement, nextArticle: HTMLElement): boolean;
288
304
  splitParagraphBlock(paragraph: HTMLElement, page: HTMLElement, nextArticle: HTMLElement): boolean;
305
+ /** Fixed-page previews must not move one oversized picture through an
306
+ * unlimited sequence of empty pages. Fit its entire frame proportionally,
307
+ * only when it is the first body block and cannot fit on an otherwise empty
308
+ * page. Ordinary paragraphs and pictures fitting a fresh page are unchanged. */
309
+ fitOversizedInlinePicture(paragraph: HTMLElement, page: HTMLElement): boolean;
289
310
  splitTableBlock(table: HTMLTableElement, page: HTMLElement, nextArticle: HTMLElement): boolean;
311
+ canRetainTablePrefix(block: HTMLElement, page: HTMLElement): boolean;
312
+ /** Structural and measured row-group safety, independent of float policy. */
313
+ private fitsTableRowPrefix;
290
314
  cloneBlockShell<T extends HTMLElement>(element: T): T;
291
315
  tableContentRows(table: HTMLTableElement): HTMLTableRowElement[];
292
316
  firstMovedTableRow(table: HTMLTableElement): HTMLTableRowElement;
@@ -339,6 +363,9 @@ export declare class HtmlRenderer {
339
363
  renderAnchoredElement(model: OpenXmlElement, element: HTMLElement | SVGElement): HTMLElement | SVGElement;
340
364
  refreshAnchors(root: HTMLElement): void;
341
365
  ownsLayoutElement(element: Element): boolean;
366
+ textBoxGridLineHeight(natural: number, pitch: number): number | null;
367
+ refreshTextBoxLineGrid(root: HTMLElement): void;
368
+ refreshParagraphLineGrid(root: HTMLElement): void;
342
369
  prepareLayout(root: HTMLElement, generation?: LayoutGeneration): Promise<void>;
343
370
  disposeLayout(): void;
344
371
  renderHyperlink(elem: WmlHyperlink): HTMLAnchorElement;
@@ -19,6 +19,8 @@ export interface WmlSettings {
19
19
  evenAndOddHeaders: boolean;
20
20
  /** True when the compatibility setting allows document grid line pitch inside table cells. */
21
21
  adjustLineHeightInTable: boolean;
22
+ /** Use the compatibility-defined 5pt before / 10pt after automatic paragraph spacing. */
23
+ doNotUseHTMLParagraphAutoSpacing?: boolean;
22
24
  /** True when w:splitPgBreakAndParaMark moves a trailing page-break paragraph mark forward. */
23
25
  splitPageBreakAndParagraphMark: boolean;
24
26
  }
package/dist/vml/vml.d.ts CHANGED
@@ -10,6 +10,7 @@ export declare class VmlElement extends OpenXmlElementBase {
10
10
  coordSize?: [number, number];
11
11
  textInset?: [string, string, string, string];
12
12
  rotation?: number;
13
+ roundRectArc?: number;
13
14
  wrapType?: string;
14
15
  imageHref?: {
15
16
  id: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@file-viewer/docx",
3
- "version": "0.3.36",
3
+ "version": "0.3.41",
4
4
  "description": "Pure HTML DOCX renderer fork for browser-based Word previews.",
5
5
  "license": "Apache-2.0",
6
6
  "keywords": [
@@ -23,6 +23,10 @@
23
23
  "type": "git",
24
24
  "url": "git+https://github.com/flyfish-dev/docxjs.git"
25
25
  },
26
+ "homepage": "https://github.com/flyfish-dev/file-viewer#readme",
27
+ "bugs": {
28
+ "url": "https://github.com/flyfish-dev/file-viewer/issues"
29
+ },
26
30
  "dependencies": {
27
31
  "jszip": ">=3.10.2",
28
32
  "@xmldom/xmldom": "^0.9.12"
@@ -37,20 +41,21 @@
37
41
  "playwright": "1.61.1",
38
42
  "tslib": "^2.4.0",
39
43
  "typescript": "^5.0.3",
40
- "@rollup/plugin-node-resolve": "^16.0.1",
44
+ "@rollup/plugin-alias": "6.0.1",
41
45
  "@rollup/plugin-commonjs": "^28.0.6",
42
46
  "jsdom": "^27.4.0"
43
47
  },
44
48
  "overrides": {
45
49
  "brace-expansion": "1.1.21",
46
- "serialize-javascript": "7.1.0"
50
+ "serialize-javascript": "7.1.3"
47
51
  },
48
52
  "scripts": {
49
53
  "build": "node scripts/clean-dist.cjs && rollup --config rollup.config.mjs && tsc --project tsconfig.build.json && node scripts/copy-build-assets.cjs",
50
54
  "build-prod": "npm run build",
51
55
  "typecheck": "tsc --noEmit --pretty false",
52
56
  "watch": "rollup --config rollup.config.mjs --watch",
53
- "verify": "npm run typecheck && npm run build && npm run test:regression && npm run test:package && npm run test:worker",
57
+ "verify": "npm run test:metadata && npm run typecheck && npm run build && npm run test:regression && npm run test:package && npm run test:worker",
58
+ "test:metadata": "node --test scripts/package-metadata.test.cjs",
54
59
  "test:regression": "node scripts/run-regressions.cjs",
55
60
  "e2e": "node scripts/run-browser-specs.cjs all",
56
61
  "e2e:security": "node scripts/run-browser-specs.cjs security",