@stll/folio-core 0.49.0 → 0.50.0
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/ai-edits/headless.d.ts +37 -1
- package/dist/ai-edits/headless.js +47 -16
- package/dist/display-list/dom/renderDisplayListToDom.d.ts +10 -1
- package/dist/display-list/dom/renderDisplayListToDom.js +8 -7
- package/dist/display-list/html/renderDisplayListToHtml.d.ts +21 -0
- package/dist/display-list/html/renderDisplayListToHtml.js +126 -0
- package/dist/display-list/selectDisplayPages.d.ts +6 -0
- package/dist/display-list/selectDisplayPages.js +44 -0
- package/dist/export-pdf.d.ts +35 -2
- package/dist/export-pdf.js +51 -20
- package/dist/fonts/fontsourceFaces.d.ts +47 -0
- package/dist/fonts/fontsourceFaces.js +230 -0
- package/dist/layout-bridge/convert/flowBlockPostprocessing.d.ts +8 -0
- package/dist/layout-bridge/convert/flowBlockPostprocessing.js +106 -0
- package/dist/layout-bridge/convert/flowBorders.d.ts +17 -0
- package/dist/layout-bridge/convert/flowBorders.js +31 -0
- package/dist/layout-bridge/convert/flowConversionShared.d.ts +111 -0
- package/dist/layout-bridge/convert/flowConversionShared.js +23 -0
- package/dist/layout-bridge/convert/footnoteLayout.d.ts +2 -1
- package/dist/layout-bridge/convert/footnoteLayout.js +2 -1
- package/dist/layout-bridge/convert/headerFooterLayout.d.ts +2 -1
- package/dist/layout-bridge/convert/imageConversion.d.ts +33 -0
- package/dist/layout-bridge/convert/imageConversion.js +118 -0
- package/dist/layout-bridge/convert/listMarkers.d.ts +28 -0
- package/dist/layout-bridge/convert/listMarkers.js +98 -0
- package/dist/layout-bridge/convert/noteReferences.d.ts +14 -0
- package/dist/layout-bridge/convert/noteReferences.js +29 -0
- package/dist/layout-bridge/convert/pageBreakSplitting.d.ts +16 -0
- package/dist/layout-bridge/convert/pageBreakSplitting.js +140 -0
- package/dist/layout-bridge/convert/paragraphAttrs.d.ts +20 -0
- package/dist/layout-bridge/convert/paragraphAttrs.js +234 -0
- package/dist/layout-bridge/convert/paragraphConversion.d.ts +12 -0
- package/dist/layout-bridge/convert/paragraphConversion.js +112 -0
- package/dist/layout-bridge/convert/paragraphRuns.d.ts +15 -0
- package/dist/layout-bridge/convert/paragraphRuns.js +281 -0
- package/dist/layout-bridge/convert/runFormattingMerge.d.ts +19 -0
- package/dist/layout-bridge/convert/runFormattingMerge.js +167 -0
- package/dist/layout-bridge/convert/runMarkFormatting.d.ts +11 -0
- package/dist/layout-bridge/convert/runMarkFormatting.js +291 -0
- package/dist/layout-bridge/convert/sectionBoundaries.d.ts +35 -0
- package/dist/layout-bridge/convert/sectionBoundaries.js +114 -0
- package/dist/layout-bridge/convert/tableConversion.d.ts +12 -0
- package/dist/layout-bridge/convert/tableConversion.js +303 -0
- package/dist/layout-bridge/convert/textBoxFill.d.ts +20 -0
- package/dist/layout-bridge/convert/textBoxFill.js +42 -0
- package/dist/layout-bridge/convert/textFormattingConversion.d.ts +22 -0
- package/dist/layout-bridge/convert/textFormattingConversion.js +120 -0
- package/dist/layout-bridge/convert/toFlowBlocks.d.ts +5 -113
- package/dist/layout-bridge/convert/toFlowBlocks.js +11 -2082
- package/dist/layout-bridge/engine/hitTest.js +1 -1
- package/dist/layout-bridge/engine/measuring/index.d.ts +2 -1
- package/dist/layout-bridge/engine/measuring/index.js +2 -1
- package/dist/layout-bridge/engine/selectionRects.js +1 -1
- package/dist/layout-engine/imageLayout.d.ts +9 -0
- package/dist/layout-engine/imageLayout.js +48 -0
- package/dist/layout-engine/index.d.ts +3 -18
- package/dist/layout-engine/index.js +12 -937
- package/dist/layout-engine/layoutFlowShared.d.ts +6 -0
- package/dist/layout-engine/layoutFlowShared.js +8 -0
- package/dist/layout-engine/measure/crossRunWords.d.ts +45 -0
- package/dist/layout-engine/measure/crossRunWords.js +172 -0
- package/dist/layout-engine/measure/floatingLineClearance.d.ts +24 -0
- package/dist/layout-engine/measure/floatingLineClearance.js +44 -0
- package/dist/layout-engine/measure/inlineObjectMetrics.d.ts +6 -0
- package/dist/layout-engine/measure/inlineObjectMetrics.js +52 -0
- package/dist/layout-engine/measure/justification.d.ts +32 -0
- package/dist/layout-engine/measure/justification.js +83 -0
- package/dist/layout-engine/measure/lineFitting.d.ts +48 -0
- package/dist/layout-engine/measure/lineFitting.js +145 -0
- package/dist/layout-engine/measure/lineTypography.d.ts +44 -0
- package/dist/layout-engine/measure/lineTypography.js +124 -0
- package/dist/layout-engine/measure/measureBlocks.js +2 -1
- package/dist/layout-engine/measure/measureParagraph.d.ts +1 -20
- package/dist/layout-engine/measure/measureParagraph.js +32 -734
- package/dist/layout-engine/measure/paragraphMeasureShared.d.ts +109 -0
- package/dist/layout-engine/measure/paragraphMeasureShared.js +82 -0
- package/dist/layout-engine/measure/paragraphTabs.d.ts +18 -0
- package/dist/layout-engine/measure/paragraphTabs.js +78 -0
- package/dist/layout-engine/paragraphLayout.d.ts +23 -0
- package/dist/layout-engine/paragraphLayout.js +204 -0
- package/dist/layout-engine/sectionLayout.d.ts +45 -0
- package/dist/layout-engine/sectionLayout.js +216 -0
- package/dist/layout-engine/tableLayout.d.ts +17 -0
- package/dist/layout-engine/tableLayout.js +369 -0
- package/dist/layout-engine/textBoxLayout.d.ts +15 -0
- package/dist/layout-engine/textBoxLayout.js +137 -0
- package/dist/layout-painter/renderPage.js +1 -0
- package/dist/paged-layout/sectionBlockWidths.d.ts +2 -1
- package/dist/paged-layout/sectionBlockWidths.js +1 -1
- package/dist/server.d.ts +3 -2
- package/package.json +2 -2
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
import { bytesToDataUrl } from "../utils/base64.js";
|
|
2
|
+
import { FONT_MAPPING } from "../utils/fontLoader.js";
|
|
3
|
+
import { parseFontFamilyList, resolveFontFamily } from "../utils/fontResolver.js";
|
|
4
|
+
//#region src/fonts/fontsourceFaces.ts
|
|
5
|
+
/**
|
|
6
|
+
* The subsets served, in resolution priority: a code point two subsets cover
|
|
7
|
+
* resolves to the earlier one on both sides.
|
|
8
|
+
*/
|
|
9
|
+
const SUBSET_PRIORITY = [
|
|
10
|
+
"latin",
|
|
11
|
+
"latin-ext",
|
|
12
|
+
"arabic"
|
|
13
|
+
];
|
|
14
|
+
/** Folio paints 400 and 700 only, as `DisplayFontFace` states. */
|
|
15
|
+
const REGULAR_WEIGHT = 400;
|
|
16
|
+
const BOLD_WEIGHT = 700;
|
|
17
|
+
/** Each bundled family and the `@fontsource` package that ships it. */
|
|
18
|
+
const FONTSOURCE_PACKAGES = {
|
|
19
|
+
Arimo: "arimo",
|
|
20
|
+
Caladea: "caladea",
|
|
21
|
+
Carlito: "carlito",
|
|
22
|
+
Cousine: "cousine",
|
|
23
|
+
Lato: "lato",
|
|
24
|
+
"Noto Sans Arabic": "noto-sans-arabic",
|
|
25
|
+
"Source Sans 3": "source-sans-3",
|
|
26
|
+
Tinos: "tinos"
|
|
27
|
+
};
|
|
28
|
+
/** A CSS stack ends in a generic, so a generic must resolve to a bundled family too. */
|
|
29
|
+
const GENERIC_FAMILIES = {
|
|
30
|
+
serif: "Tinos",
|
|
31
|
+
"sans-serif": "Arimo",
|
|
32
|
+
monospace: "Cousine"
|
|
33
|
+
};
|
|
34
|
+
const isBundledFamily = (name) => Object.hasOwn(FONTSOURCE_PACKAGES, name);
|
|
35
|
+
const isGenericFamily = (name) => Object.hasOwn(GENERIC_FAMILIES, name);
|
|
36
|
+
/** Every name a bundled family answers to: its own, and the authored names mapped to it. */
|
|
37
|
+
const familyIndexEntries = () => {
|
|
38
|
+
const entries = [];
|
|
39
|
+
for (const family of Object.keys(FONTSOURCE_PACKAGES)) if (isBundledFamily(family)) entries.push([family.toLowerCase(), family]);
|
|
40
|
+
for (const [authored, mapped] of Object.entries(FONT_MAPPING)) {
|
|
41
|
+
if (isBundledFamily(mapped)) {
|
|
42
|
+
entries.push([authored.toLowerCase(), mapped]);
|
|
43
|
+
continue;
|
|
44
|
+
}
|
|
45
|
+
if (isGenericFamily(mapped)) entries.push([authored.toLowerCase(), GENERIC_FAMILIES[mapped]]);
|
|
46
|
+
}
|
|
47
|
+
for (const [generic, bundled] of Object.entries(GENERIC_FAMILIES)) entries.push([generic, bundled]);
|
|
48
|
+
return entries;
|
|
49
|
+
};
|
|
50
|
+
const FAMILY_INDEX = new Map(familyIndexEntries());
|
|
51
|
+
/** Every family name the faces answer to, in a stable order for the CSS. */
|
|
52
|
+
const bundledFamilyAliases = () => [...Object.keys(FONTSOURCE_PACKAGES), ...Object.keys(FONT_MAPPING).filter((authored) => FAMILY_INDEX.has(authored.toLowerCase()))].sort((a, b) => a.localeCompare(b, "en"));
|
|
53
|
+
/**
|
|
54
|
+
* The bundled family a requested name resolves to: directly, or through the
|
|
55
|
+
* same fallback chain the editor's font resolver walks.
|
|
56
|
+
*/
|
|
57
|
+
const resolveBundledFamily = (family) => {
|
|
58
|
+
const direct = FAMILY_INDEX.get(family.trim().toLowerCase());
|
|
59
|
+
if (direct !== void 0) return direct;
|
|
60
|
+
for (const candidate of parseFontFamilyList(resolveFontFamily(family).cssFallback)) {
|
|
61
|
+
const resolved = FAMILY_INDEX.get(candidate.trim().toLowerCase());
|
|
62
|
+
if (resolved !== void 0) return resolved;
|
|
63
|
+
}
|
|
64
|
+
return null;
|
|
65
|
+
};
|
|
66
|
+
const weightOf = (bold) => bold ? BOLD_WEIGHT : REGULAR_WEIGHT;
|
|
67
|
+
/** `@fontsource` ships the ranges it cut each subset with, beside the files. */
|
|
68
|
+
const UNICODE_RANGE_FILE = "unicode.json";
|
|
69
|
+
const RANGE_TOKEN_RE = /^U\+([0-9A-Fa-f]+)(?:-([0-9A-Fa-f]+))?$/u;
|
|
70
|
+
const parseRangeToken = (token) => {
|
|
71
|
+
const match = RANGE_TOKEN_RE.exec(token.trim());
|
|
72
|
+
if (match === null) return [];
|
|
73
|
+
const first = Number.parseInt(match[1] ?? "", 16);
|
|
74
|
+
const last = match[2] === void 0 ? first : Number.parseInt(match[2], 16);
|
|
75
|
+
if (Number.isNaN(first) || Number.isNaN(last)) return [];
|
|
76
|
+
return [[first, last]];
|
|
77
|
+
};
|
|
78
|
+
const parseRangeList = (declared) => declared.split(",").flatMap(parseRangeToken);
|
|
79
|
+
const parseDeclaredRanges = (bytes) => {
|
|
80
|
+
const ranges = /* @__PURE__ */ new Map();
|
|
81
|
+
const parsed = JSON.parse(new TextDecoder().decode(bytes));
|
|
82
|
+
if (typeof parsed !== "object" || parsed === null) return ranges;
|
|
83
|
+
for (const [subset, declared] of Object.entries(parsed)) if (typeof declared === "string") ranges.set(subset, parseRangeList(declared));
|
|
84
|
+
return ranges;
|
|
85
|
+
};
|
|
86
|
+
const withoutRange = (range, [start, end]) => {
|
|
87
|
+
const [first, last] = range;
|
|
88
|
+
if (end < first || start > last) return [range];
|
|
89
|
+
const remainder = [];
|
|
90
|
+
if (start > first) remainder.push([first, start - 1]);
|
|
91
|
+
if (end < last) remainder.push([end + 1, last]);
|
|
92
|
+
return remainder;
|
|
93
|
+
};
|
|
94
|
+
/**
|
|
95
|
+
* `subject` minus everything a higher-priority subset already serves. The
|
|
96
|
+
* declared ranges overlap (`latin` and `latin-ext` both claim some combining
|
|
97
|
+
* marks); cutting the overlap out of the lower-priority rule makes a browser,
|
|
98
|
+
* which resolves an overlap by declaration order, agree with this table.
|
|
99
|
+
*/
|
|
100
|
+
const subtractRanges = (subject, taken) => {
|
|
101
|
+
let remaining = subject;
|
|
102
|
+
for (const range of taken) remaining = remaining.flatMap((candidate) => withoutRange(candidate, range));
|
|
103
|
+
return remaining;
|
|
104
|
+
};
|
|
105
|
+
const hex = (codePoint) => codePoint.toString(16).toUpperCase().padStart(4, "0");
|
|
106
|
+
const formatRangeList = (ranges) => ranges.map(([first, last]) => first === last ? `U+${hex(first)}` : `U+${hex(first)}-${hex(last)}`).join(",");
|
|
107
|
+
/**
|
|
108
|
+
* A CSS `<string>` token, quoted and escaped. A family name comes from an
|
|
109
|
+
* authored document and is emitted into a `<style>` element, so the
|
|
110
|
+
* backslash is escaped first, then the quote, and line terminators become hex
|
|
111
|
+
* escapes, which CSS strings require.
|
|
112
|
+
*/
|
|
113
|
+
const cssString = (value) => `"${value.replaceAll("\\", "\\\\").replaceAll("\"", "\\\"").replaceAll("\n", "\\A ").replaceAll("\r", "\\D ").replaceAll("\f", "\\C ")}"`;
|
|
114
|
+
const FACE_VARIANTS = [
|
|
115
|
+
{
|
|
116
|
+
weight: REGULAR_WEIGHT,
|
|
117
|
+
italic: false
|
|
118
|
+
},
|
|
119
|
+
{
|
|
120
|
+
weight: REGULAR_WEIGHT,
|
|
121
|
+
italic: true
|
|
122
|
+
},
|
|
123
|
+
{
|
|
124
|
+
weight: BOLD_WEIGHT,
|
|
125
|
+
italic: false
|
|
126
|
+
},
|
|
127
|
+
{
|
|
128
|
+
weight: BOLD_WEIGHT,
|
|
129
|
+
italic: true
|
|
130
|
+
}
|
|
131
|
+
];
|
|
132
|
+
/** The media type a `.woff` face is inlined under. */
|
|
133
|
+
const WOFF_MEDIA_TYPE = "font/woff";
|
|
134
|
+
const fontFaceRule = ({ family, dataUrl, weight, italic, unicodeRange }) => [
|
|
135
|
+
"@font-face {",
|
|
136
|
+
` font-family: ${cssString(family)};`,
|
|
137
|
+
` src: url(${dataUrl}) format("woff");`,
|
|
138
|
+
` font-weight: ${String(weight)};`,
|
|
139
|
+
` font-style: ${italic ? "italic" : "normal"};`,
|
|
140
|
+
` unicode-range: ${formatRangeList(unicodeRange)};`,
|
|
141
|
+
" font-display: block;",
|
|
142
|
+
"}"
|
|
143
|
+
].join("\n");
|
|
144
|
+
/** The `@fontsource` faces, read through `files` and cached per face. */
|
|
145
|
+
const createFontsourceFaces = (files) => {
|
|
146
|
+
const faceCache = /* @__PURE__ */ new Map();
|
|
147
|
+
const rangeCache = /* @__PURE__ */ new Map();
|
|
148
|
+
const dataUrlCache = /* @__PURE__ */ new Map();
|
|
149
|
+
const faceBinaries = ({ family, weight, italic }) => {
|
|
150
|
+
const bundled = resolveBundledFamily(family);
|
|
151
|
+
if (bundled === null) return [];
|
|
152
|
+
const packageName = FONTSOURCE_PACKAGES[bundled];
|
|
153
|
+
const style = italic ? "italic" : "normal";
|
|
154
|
+
const key = `${packageName}|${String(weight)}|${style}`;
|
|
155
|
+
const cached = faceCache.get(key);
|
|
156
|
+
if (cached !== void 0) return cached;
|
|
157
|
+
const binaries = SUBSET_PRIORITY.flatMap((subset) => {
|
|
158
|
+
const bytes = files.read(packageName, `files/${packageName}-${subset}-${String(weight)}-${style}.woff`);
|
|
159
|
+
return bytes === null ? [] : [{
|
|
160
|
+
subset,
|
|
161
|
+
bytes
|
|
162
|
+
}];
|
|
163
|
+
});
|
|
164
|
+
faceCache.set(key, binaries);
|
|
165
|
+
return binaries;
|
|
166
|
+
};
|
|
167
|
+
const declaredRanges = (packageName) => {
|
|
168
|
+
const cached = rangeCache.get(packageName);
|
|
169
|
+
if (cached !== void 0) return cached;
|
|
170
|
+
const bytes = files.read(packageName, UNICODE_RANGE_FILE);
|
|
171
|
+
const ranges = bytes === null ? /* @__PURE__ */ new Map() : parseDeclaredRanges(bytes);
|
|
172
|
+
rangeCache.set(packageName, ranges);
|
|
173
|
+
return ranges;
|
|
174
|
+
};
|
|
175
|
+
const dataUrlOf = (bytes) => {
|
|
176
|
+
const cached = dataUrlCache.get(bytes);
|
|
177
|
+
if (cached !== void 0) return cached;
|
|
178
|
+
const encoded = bytesToDataUrl(bytes, WOFF_MEDIA_TYPE);
|
|
179
|
+
dataUrlCache.set(bytes, encoded);
|
|
180
|
+
return encoded;
|
|
181
|
+
};
|
|
182
|
+
/**
|
|
183
|
+
* The rules for one variant of one family name. A subset whose range a
|
|
184
|
+
* higher-priority subset already serves, or that `unicode.json` does not
|
|
185
|
+
* describe, is not declared: a rule with no `unicode-range` would cover
|
|
186
|
+
* every code point.
|
|
187
|
+
*/
|
|
188
|
+
const variantRules = ({ family, weight, italic }) => {
|
|
189
|
+
const bundled = resolveBundledFamily(family);
|
|
190
|
+
if (bundled === null) return [];
|
|
191
|
+
const ranges = declaredRanges(FONTSOURCE_PACKAGES[bundled]);
|
|
192
|
+
const rules = [];
|
|
193
|
+
const taken = [];
|
|
194
|
+
for (const binary of faceBinaries({
|
|
195
|
+
family,
|
|
196
|
+
weight,
|
|
197
|
+
italic
|
|
198
|
+
})) {
|
|
199
|
+
const unicodeRange = subtractRanges(ranges.get(binary.subset) ?? [], taken);
|
|
200
|
+
if (unicodeRange.length === 0) continue;
|
|
201
|
+
rules.push(fontFaceRule({
|
|
202
|
+
family,
|
|
203
|
+
dataUrl: dataUrlOf(binary.bytes),
|
|
204
|
+
weight,
|
|
205
|
+
italic,
|
|
206
|
+
unicodeRange
|
|
207
|
+
}));
|
|
208
|
+
taken.push(...unicodeRange);
|
|
209
|
+
}
|
|
210
|
+
return rules;
|
|
211
|
+
};
|
|
212
|
+
return {
|
|
213
|
+
source: { load: ({ family, bold, italic }) => faceBinaries({
|
|
214
|
+
family,
|
|
215
|
+
weight: weightOf(bold),
|
|
216
|
+
italic
|
|
217
|
+
}).map(({ bytes }) => bytes) },
|
|
218
|
+
fontFaceCss: (options = {}) => {
|
|
219
|
+
const declared = /* @__PURE__ */ new Map();
|
|
220
|
+
for (const family of [...bundledFamilyAliases(), ...options.families ?? []]) declared.set(family.trim().toLowerCase(), family.trim());
|
|
221
|
+
return [...declared.values()].flatMap((family) => FACE_VARIANTS.flatMap(({ weight, italic }) => variantRules({
|
|
222
|
+
family,
|
|
223
|
+
weight,
|
|
224
|
+
italic
|
|
225
|
+
}))).join("\n");
|
|
226
|
+
}
|
|
227
|
+
};
|
|
228
|
+
};
|
|
229
|
+
//#endregion
|
|
230
|
+
export { FONTSOURCE_PACKAGES, createFontsourceFaces, cssString };
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { FlowBlock } from "../../layout-engine/types.js";
|
|
2
|
+
//#region src/layout-bridge/convert/flowBlockPostprocessing.d.ts
|
|
3
|
+
declare function suppressFinalEmptyParagraphAfterTable(blocks: FlowBlock[]): void;
|
|
4
|
+
declare function suppressFinalParagraphInRepeatedEmptySuffix(blocks: FlowBlock[]): void;
|
|
5
|
+
declare function reserveLeadingEmptyOutlineHeight(blocks: FlowBlock[]): void;
|
|
6
|
+
declare function mergeRunInParagraphs(blocks: FlowBlock[]): FlowBlock[];
|
|
7
|
+
//#endregion
|
|
8
|
+
export { mergeRunInParagraphs, reserveLeadingEmptyOutlineHeight, suppressFinalEmptyParagraphAfterTable, suppressFinalParagraphInRepeatedEmptySuffix };
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { headingLevelOf } from "@stll/docx-core/model";
|
|
2
|
+
//#region src/layout-bridge/convert/flowBlockPostprocessing.ts
|
|
3
|
+
/**
|
|
4
|
+
* Post-conversion passes over the flow block list: terminal empty-paragraph
|
|
5
|
+
* suppression, leading empty outline reservation, and run-in paragraph
|
|
6
|
+
* merging.
|
|
7
|
+
*/
|
|
8
|
+
/**
|
|
9
|
+
* Word keeps a final empty body paragraph after a table as an editable anchor,
|
|
10
|
+
* but that final anchor does not create a page of its own. Earlier authored
|
|
11
|
+
* empty paragraphs retain their height and may carry the document onto a blank
|
|
12
|
+
* page. Preserve every block and PM range while collapsing only the final one.
|
|
13
|
+
*/
|
|
14
|
+
function isPaintlessTerminalParagraph(block) {
|
|
15
|
+
if (block?.kind !== "paragraph" || block.runs.length !== 0) return false;
|
|
16
|
+
const attrs = block.attrs;
|
|
17
|
+
if (attrs?.suppressEmptyParagraphHeight === false) return false;
|
|
18
|
+
return !(attrs?.listMarker !== void 0 && !attrs.listMarkerHidden || attrs?.borders?.top || attrs?.borders?.bottom || attrs?.borders?.left || attrs?.borders?.right || attrs?.borders?.between || attrs?.borders?.bar || attrs?.shading || attrs?.spacingExplicit?.before || attrs?.spacingExplicit?.after || attrs?.pageBreakBefore || attrs?.renderedPageBreakBefore);
|
|
19
|
+
}
|
|
20
|
+
function suppressFinalEmptyParagraphAfterTable(blocks) {
|
|
21
|
+
let suffixStart = blocks.length;
|
|
22
|
+
while (suffixStart > 0 && isPaintlessTerminalParagraph(blocks[suffixStart - 1])) suffixStart -= 1;
|
|
23
|
+
if (suffixStart === blocks.length || suffixStart === 0 || blocks[suffixStart - 1]?.kind !== "table") return;
|
|
24
|
+
const finalBlock = blocks.at(-1);
|
|
25
|
+
if (isPaintlessTerminalParagraph(finalBlock)) finalBlock.attrs = {
|
|
26
|
+
...finalBlock.attrs,
|
|
27
|
+
suppressEmptyParagraphHeight: true
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
function suppressFinalParagraphInRepeatedEmptySuffix(blocks) {
|
|
31
|
+
let suffixStart = blocks.length;
|
|
32
|
+
while (suffixStart > 0 && isPaintlessTerminalParagraph(blocks[suffixStart - 1])) suffixStart -= 1;
|
|
33
|
+
if (suffixStart === 0 || blocks.length - suffixStart < 2 || blocks[suffixStart - 1]?.kind === "table") return;
|
|
34
|
+
const finalBlock = blocks.at(-1);
|
|
35
|
+
if (isPaintlessTerminalParagraph(finalBlock)) finalBlock.attrs = {
|
|
36
|
+
...finalBlock.attrs,
|
|
37
|
+
suppressEmptyParagraphHeight: true
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
function reserveLeadingEmptyOutlineHeight(blocks) {
|
|
41
|
+
const firstBlock = blocks.at(0);
|
|
42
|
+
if (firstBlock?.kind !== "paragraph" || firstBlock.runs.length !== 0 || headingLevelOf(firstBlock.attrs?.outlineLevel) !== 0) return;
|
|
43
|
+
firstBlock.attrs = {
|
|
44
|
+
...firstBlock.attrs,
|
|
45
|
+
reserveEmptyOutlineHeight: true
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Merge consecutive paragraph blocks where the first carries
|
|
50
|
+
* `runInWithNext` (`<w:specVanish/>` on the paragraph mark).
|
|
51
|
+
*
|
|
52
|
+
* Word's run-in heading feature renders the next paragraph inline on
|
|
53
|
+
* the same line, so for layout we collapse the pair into one
|
|
54
|
+
* ParagraphBlock with combined runs. The merged block keeps the first
|
|
55
|
+
* paragraph's attrs (heading formatting, list marker, indent) and
|
|
56
|
+
* extends pmEnd to the second paragraph's range so click-to-position
|
|
57
|
+
* resolution still maps both ranges back to body content.
|
|
58
|
+
*
|
|
59
|
+
* Chains: runInWithNext on the merged block is dropped because the
|
|
60
|
+
* second paragraph's mark wasn't `specVanish`. If a chain of
|
|
61
|
+
* specVanish paragraphs needs collapsing (rare in practice), the loop
|
|
62
|
+
* naturally handles it by re-inspecting the merged block's flag (we
|
|
63
|
+
* preserve runInWithNext only when the second paragraph itself has
|
|
64
|
+
* specVanish).
|
|
65
|
+
*/
|
|
66
|
+
function sdtGroupStacksEqual(a, b) {
|
|
67
|
+
const lenA = a?.length ?? 0;
|
|
68
|
+
if (lenA !== (b?.length ?? 0)) return false;
|
|
69
|
+
if (lenA === 0) return true;
|
|
70
|
+
for (let i = 0; i < lenA; i++) if (a?.[i]?.pmPos !== b?.[i]?.pmPos) return false;
|
|
71
|
+
return true;
|
|
72
|
+
}
|
|
73
|
+
function mergeRunInParagraphs(blocks) {
|
|
74
|
+
const out = [];
|
|
75
|
+
for (let i = 0; i < blocks.length; i++) {
|
|
76
|
+
let current = blocks[i];
|
|
77
|
+
if (!current) continue;
|
|
78
|
+
while (current.kind === "paragraph" && current.attrs?.runInWithNext && i + 1 < blocks.length) {
|
|
79
|
+
const next = blocks[i + 1];
|
|
80
|
+
if (!next || next.kind !== "paragraph") break;
|
|
81
|
+
const a = current;
|
|
82
|
+
const b = next;
|
|
83
|
+
if (!sdtGroupStacksEqual(a.sdtGroups, b.sdtGroups)) break;
|
|
84
|
+
const mergedAttrs = { ...a.attrs };
|
|
85
|
+
if (b.attrs?.spacing?.after !== void 0) mergedAttrs.spacing = {
|
|
86
|
+
...mergedAttrs.spacing,
|
|
87
|
+
after: b.attrs.spacing.after
|
|
88
|
+
};
|
|
89
|
+
if (b.attrs?.runInWithNext) mergedAttrs.runInWithNext = true;
|
|
90
|
+
else delete mergedAttrs.runInWithNext;
|
|
91
|
+
const merged = {
|
|
92
|
+
...a,
|
|
93
|
+
runs: [...a.runs, ...b.runs],
|
|
94
|
+
attrs: mergedAttrs
|
|
95
|
+
};
|
|
96
|
+
const mergedPmEnd = b.pmEnd ?? a.pmEnd;
|
|
97
|
+
if (mergedPmEnd !== void 0) merged.pmEnd = mergedPmEnd;
|
|
98
|
+
current = merged;
|
|
99
|
+
i += 1;
|
|
100
|
+
}
|
|
101
|
+
out.push(current);
|
|
102
|
+
}
|
|
103
|
+
return out;
|
|
104
|
+
}
|
|
105
|
+
//#endregion
|
|
106
|
+
export { mergeRunInParagraphs, reserveLeadingEmptyOutlineHeight, suppressFinalEmptyParagraphAfterTable, suppressFinalParagraphInRepeatedEmptySuffix };
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { document_d_exports } from "../../types/document.js";
|
|
2
|
+
import { BorderStyle } from "../../layout-engine/types.js";
|
|
3
|
+
//#region src/layout-bridge/convert/flowBorders.d.ts
|
|
4
|
+
/** A parsed border, or the same shape read off ProseMirror attributes. */
|
|
5
|
+
type ConvertibleBorder = {
|
|
6
|
+
style?: document_d_exports.BorderStyleValue;
|
|
7
|
+
size?: number;
|
|
8
|
+
space?: number;
|
|
9
|
+
color?: document_d_exports.ColorValue;
|
|
10
|
+
};
|
|
11
|
+
/**
|
|
12
|
+
* Convert an OOXML BorderSpec to a layout-engine BorderStyle.
|
|
13
|
+
* Shared by paragraph borders, cell borders, and header/footer borders.
|
|
14
|
+
*/
|
|
15
|
+
declare function convertBorderSpecToLayout(border: ConvertibleBorder, theme?: document_d_exports.Theme | null): BorderStyle | undefined;
|
|
16
|
+
//#endregion
|
|
17
|
+
export { ConvertibleBorder, convertBorderSpecToLayout };
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { cssBorderStyle } from "../../utils/borderCss.js";
|
|
2
|
+
import { resolveColor } from "../../utils/colorResolver.js";
|
|
3
|
+
import { pointsToPixels } from "../../utils/units.js";
|
|
4
|
+
import { statesNoBorder } from "@stll/docx-core/model";
|
|
5
|
+
//#region src/layout-bridge/convert/flowBorders.ts
|
|
6
|
+
/**
|
|
7
|
+
* Converts document-model border specifications to layout BorderStyle.
|
|
8
|
+
*/
|
|
9
|
+
/**
|
|
10
|
+
* Convert border width from eighths of a point to pixels.
|
|
11
|
+
* OOXML stores border widths in eighths of a point.
|
|
12
|
+
*/
|
|
13
|
+
function borderWidthToPixels(eighthsOfPoint) {
|
|
14
|
+
return pointsToPixels(eighthsOfPoint / 8);
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Convert an OOXML BorderSpec to a layout-engine BorderStyle.
|
|
18
|
+
* Shared by paragraph borders, cell borders, and header/footer borders.
|
|
19
|
+
*/
|
|
20
|
+
function convertBorderSpecToLayout(border, theme) {
|
|
21
|
+
if (border.style === void 0 || statesNoBorder(border.style)) return;
|
|
22
|
+
const result = {
|
|
23
|
+
style: cssBorderStyle(border.style),
|
|
24
|
+
width: border.size === void 0 ? 1 : borderWidthToPixels(border.size),
|
|
25
|
+
color: border.color ? resolveColor(border.color, theme) : "#000000"
|
|
26
|
+
};
|
|
27
|
+
if (border.space !== void 0) result.space = pointsToPixels(border.space);
|
|
28
|
+
return result;
|
|
29
|
+
}
|
|
30
|
+
//#endregion
|
|
31
|
+
export { convertBorderSpecToLayout };
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { document_d_exports } from "../../types/document.js";
|
|
2
|
+
import { RunStyleResolver } from "../../prosemirror/runStyleFormatting.js";
|
|
3
|
+
import { ParagraphAttrs, ParagraphBlock, TableBlock } from "../../layout-engine/types.js";
|
|
4
|
+
import { FontAlternates } from "../../fonts/fontAlternates.js";
|
|
5
|
+
import { ListCounterStreams } from "../../prosemirror/listMarker.js";
|
|
6
|
+
import { Node } from "prosemirror-model";
|
|
7
|
+
//#region src/layout-bridge/convert/flowConversionShared.d.ts
|
|
8
|
+
/**
|
|
9
|
+
* Options for the conversion.
|
|
10
|
+
*/
|
|
11
|
+
type ToFlowBlocksOptions = {
|
|
12
|
+
/** Default font family. */
|
|
13
|
+
defaultFont?: string;
|
|
14
|
+
/** Default font size in points. */
|
|
15
|
+
defaultSize?: number;
|
|
16
|
+
/** Theme for resolving theme colors. */
|
|
17
|
+
theme?: document_d_exports.Theme | null;
|
|
18
|
+
/** Document styles used to resolve character-style toggles without per-run copies. */
|
|
19
|
+
styles?: document_d_exports.StyleDefinitions;
|
|
20
|
+
/** Document-scoped OOXML primary-font to `w:altName` lookup. */
|
|
21
|
+
fontAlternates?: FontAlternates;
|
|
22
|
+
/** Page content height in pixels (pageHeight - marginTop - marginBottom). Images taller than this are scaled down to fit. */
|
|
23
|
+
pageContentHeight?: number;
|
|
24
|
+
/** Whether a trailing page break moves its paragraph mark to the next page. */
|
|
25
|
+
splitPageBreakAndParagraphMark?: boolean;
|
|
26
|
+
/** Shared list counters for nested containers. */
|
|
27
|
+
listCounters?: Map<number, number[]>;
|
|
28
|
+
/** Latest concrete counters by abstract numbering definition. */
|
|
29
|
+
listAbstractCounters?: Map<number, number[]>;
|
|
30
|
+
/** Shared startOverride state for nested containers. */
|
|
31
|
+
listSeenNumIds?: Set<string>;
|
|
32
|
+
/**
|
|
33
|
+
* Parallel counter state for the "original" (pre-revision) document, used to
|
|
34
|
+
* number tracked-deletion list items. Word numbers inserted and deleted list
|
|
35
|
+
* runs as if they never coexist: insertions get final-document numbering,
|
|
36
|
+
* deletions keep their original numbering. Without a separate stream a deleted
|
|
37
|
+
* item continues the counter of the inserted item before it (a, b → c, d, e
|
|
38
|
+
* instead of a, b and a, b, c). Normal items advance both streams.
|
|
39
|
+
*/
|
|
40
|
+
originalListCounters?: Map<number, number[]>;
|
|
41
|
+
/** Latest concrete original-stream counters by abstract numbering definition. */
|
|
42
|
+
originalListAbstractCounters?: Map<number, number[]>;
|
|
43
|
+
/** Original-stream startOverride state. */
|
|
44
|
+
originalListSeenNumIds?: Set<string>;
|
|
45
|
+
/**
|
|
46
|
+
* Document-wide `w:defaultTabStop` (§17.6.13) in twips. Stamped onto
|
|
47
|
+
* every paragraph block so paragraph-local layout helpers (list marker
|
|
48
|
+
* tab-stop math) can read it without taking a `Document` reference.
|
|
49
|
+
* Defaults to the OOXML 720-twip value when absent.
|
|
50
|
+
*/
|
|
51
|
+
defaultTabStopTwips?: number;
|
|
52
|
+
/** Document-wide custom Word line-breaking settings. */
|
|
53
|
+
lineBreakRules?: {
|
|
54
|
+
noLineBreaksBefore?: {
|
|
55
|
+
language?: string;
|
|
56
|
+
characters: string;
|
|
57
|
+
};
|
|
58
|
+
noLineBreaksAfter?: {
|
|
59
|
+
language?: string;
|
|
60
|
+
characters: string;
|
|
61
|
+
};
|
|
62
|
+
useLegacyEthiopicAmharicRules?: boolean;
|
|
63
|
+
};
|
|
64
|
+
/** Document-generation policy for justified line fitting. */
|
|
65
|
+
justificationCompatibility?: NonNullable<ParagraphAttrs["justificationCompatibility"]>;
|
|
66
|
+
/** Document-generation policy for where `w:tblInd` is measured from. */
|
|
67
|
+
tableIndentCompatibility?: NonNullable<TableBlock["indentCompatibility"]>;
|
|
68
|
+
/**
|
|
69
|
+
* When set, every table-cell anchor lays out inside its cell regardless of
|
|
70
|
+
* an authored `wp:anchor/@layoutInCell="0"`. Compatibility mode 15 and
|
|
71
|
+
* above ignore that opt-out; see `resolveAnchorLayoutInCellCompatibility`.
|
|
72
|
+
*/
|
|
73
|
+
forceAnchorLayoutInCell?: boolean;
|
|
74
|
+
/** Document-wide automatic hyphenation policy. */
|
|
75
|
+
automaticHyphenation?: NonNullable<ParagraphAttrs["automaticHyphenation"]>;
|
|
76
|
+
/** Line pitch for the final body section, whose properties live outside the PM body. */
|
|
77
|
+
finalSectionDocumentGridLinePitchTwips?: number;
|
|
78
|
+
/**
|
|
79
|
+
* The number a note story's `w:footnoteRef`/`w:endnoteRef` marks show. The
|
|
80
|
+
* mark stands for its note's reference number and shows it where it sits
|
|
81
|
+
* among the story's runs, in that run's formatting.
|
|
82
|
+
*/
|
|
83
|
+
noteReferenceMarkText?: string;
|
|
84
|
+
/**
|
|
85
|
+
* Endnotes laid out after the body's last block (`w:pos="docEnd"`). A body
|
|
86
|
+
* that references one ends in its endnote area, so its final paragraph is no
|
|
87
|
+
* longer the document's last and keeps its height.
|
|
88
|
+
*/
|
|
89
|
+
trailingEndnoteIds?: ReadonlySet<number>;
|
|
90
|
+
};
|
|
91
|
+
type FlowConversionOptions = ToFlowBlocksOptions & {
|
|
92
|
+
firstPageBreakRunPosition: (node: Node) => number | undefined;
|
|
93
|
+
/** `w:doNotUseIndentAsNumberingTabStop`, read from the document node. */
|
|
94
|
+
numberingTabIgnoresIndent: boolean;
|
|
95
|
+
listCounterStreams: ListCounterStreams;
|
|
96
|
+
numberedRefResults?: ReadonlyMap<Node, string>;
|
|
97
|
+
textBoxAnchorBlockIds: Map<string, ParagraphBlock["id"]>;
|
|
98
|
+
styleResolver: RunStyleResolver;
|
|
99
|
+
};
|
|
100
|
+
/**
|
|
101
|
+
* Convert twips to pixels (1 twip = 1/1440 inch, 1 inch = 96 CSS px).
|
|
102
|
+
* No rounding — precision prevents cumulative layout drift across paragraphs.
|
|
103
|
+
*/
|
|
104
|
+
declare function twipsToPixels(twips: number): number;
|
|
105
|
+
declare function nextBlockId(): string;
|
|
106
|
+
/**
|
|
107
|
+
* Reset the block ID counter (useful for testing).
|
|
108
|
+
*/
|
|
109
|
+
declare function resetBlockIdCounter(): void;
|
|
110
|
+
//#endregion
|
|
111
|
+
export { FlowConversionOptions, ToFlowBlocksOptions, nextBlockId, resetBlockIdCounter, twipsToPixels };
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
//#region src/layout-bridge/convert/flowConversionShared.ts
|
|
2
|
+
/**
|
|
3
|
+
* Convert twips to pixels (1 twip = 1/1440 inch, 1 inch = 96 CSS px).
|
|
4
|
+
* No rounding — precision prevents cumulative layout drift across paragraphs.
|
|
5
|
+
*/
|
|
6
|
+
function twipsToPixels(twips) {
|
|
7
|
+
return twips / 1440 * 96;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Generate a unique block ID.
|
|
11
|
+
*/
|
|
12
|
+
let blockIdCounter = 0;
|
|
13
|
+
function nextBlockId() {
|
|
14
|
+
return `block-${++blockIdCounter}`;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Reset the block ID counter (useful for testing).
|
|
18
|
+
*/
|
|
19
|
+
function resetBlockIdCounter() {
|
|
20
|
+
blockIdCounter = 0;
|
|
21
|
+
}
|
|
22
|
+
//#endregion
|
|
23
|
+
export { nextBlockId, resetBlockIdCounter, twipsToPixels };
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { document_d_exports } from "../../types/document.js";
|
|
2
2
|
import { FOOTNOTE_ENTRY_MARGIN_BOTTOM, FOOTNOTE_FALLBACK_LINE_HEIGHT, FOOTNOTE_SEPARATOR_HEIGHT, FlowBlock, FootnoteContent, Measure, Page } from "../../layout-engine/types.js";
|
|
3
|
-
import { ToFlowBlocksOptions } from "./
|
|
3
|
+
import { ToFlowBlocksOptions } from "./flowConversionShared.js";
|
|
4
|
+
import "./toFlowBlocks.js";
|
|
4
5
|
//#region src/layout-bridge/convert/footnoteLayout.d.ts
|
|
5
6
|
type MeasureBlocksFn = (blocks: FlowBlock[], contentWidth: number) => Measure[];
|
|
6
7
|
type ConvertFootnoteOptions = {
|
|
@@ -3,7 +3,8 @@ import { layoutTextBoxContent } from "../../layout-engine/measure/textBoxParagra
|
|
|
3
3
|
import { DEFAULT_TEXTBOX_MARGINS, FOOTNOTE_ENTRY_MARGIN_BOTTOM, FOOTNOTE_FALLBACK_LINE_HEIGHT, FOOTNOTE_SEPARATOR_HEIGHT, isFloatingImageRun, resolveTableWidthPx } from "../../layout-engine/types.js";
|
|
4
4
|
import { expectPreservedXmlAttrs } from "../../prosemirror/attrs/index.js";
|
|
5
5
|
import { footnoteToProseDoc } from "../../prosemirror/conversion/toProseDoc.js";
|
|
6
|
-
import { collectNoteRefs, isNoteReferenceMarkXml
|
|
6
|
+
import { collectNoteRefs, isNoteReferenceMarkXml } from "./noteReferences.js";
|
|
7
|
+
import { toFlowBlocks } from "./toFlowBlocks.js";
|
|
7
8
|
//#region src/layout-bridge/convert/footnoteLayout.ts
|
|
8
9
|
/** Default footnote font size in points */
|
|
9
10
|
const FOOTNOTE_FONT_SIZE = 8;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { document_d_exports } from "../../types/document.js";
|
|
2
2
|
import { FlowBlock, HeaderFooterContent, ImageRun, ImageRunPosition, Measure, PageMargins } from "../../layout-engine/types.js";
|
|
3
|
-
import { ToFlowBlocksOptions } from "./
|
|
3
|
+
import { ToFlowBlocksOptions } from "./flowConversionShared.js";
|
|
4
|
+
import "./toFlowBlocks.js";
|
|
4
5
|
import { MeasureBlocksFn } from "./footnoteLayout.js";
|
|
5
6
|
import { Node } from "prosemirror-model";
|
|
6
7
|
//#region src/layout-bridge/convert/headerFooterLayout.d.ts
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { ImageAttrs } from "../../prosemirror/schema/nodes.js";
|
|
2
|
+
import { ImageBlock, ImageRun, RunFormatting } from "../../layout-engine/types.js";
|
|
3
|
+
import { Node } from "prosemirror-model";
|
|
4
|
+
//#region src/layout-bridge/convert/imageConversion.d.ts
|
|
5
|
+
/**
|
|
6
|
+
* Constrain image dimensions to fit within the page content area.
|
|
7
|
+
* Scales proportionally if height exceeds pageContentHeight.
|
|
8
|
+
*/
|
|
9
|
+
declare function constrainImageToPage(width: number, height: number, pageContentHeight: number | undefined): {
|
|
10
|
+
width: number;
|
|
11
|
+
height: number;
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* Build an ImageRun from ProseMirror node attrs, applying conditional property assignment
|
|
15
|
+
* to satisfy exactOptionalPropertyTypes.
|
|
16
|
+
*/
|
|
17
|
+
declare function buildImageRun(attrs: ImageAttrs, constrained: {
|
|
18
|
+
width: number;
|
|
19
|
+
height: number;
|
|
20
|
+
}, pmStart: number, pmEnd: number, trackedChange?: Pick<RunFormatting, "isInsertion" | "isDeletion" | "changeAuthor" | "changeDate" | "changeRevisionId">, forceLayoutInCell?: boolean): ImageRun;
|
|
21
|
+
/** A package image whose bytes cannot paint still owns its authored line box. */
|
|
22
|
+
declare const hasRelationshipBackedImageBox: (attrs: ImageAttrs) => boolean;
|
|
23
|
+
/**
|
|
24
|
+
* A preview counts as a source: the bytes are not in the attrs, but the
|
|
25
|
+
* display list can produce them without I/O when it interns the descriptor.
|
|
26
|
+
*/
|
|
27
|
+
declare const hasPaintableImageSource: (attrs: Pick<ImageAttrs, "src" | "preview">) => boolean;
|
|
28
|
+
/**
|
|
29
|
+
* Convert an image node to an ImageBlock.
|
|
30
|
+
*/
|
|
31
|
+
declare function convertImage(node: Node, startPos: number, pageContentHeight?: number): ImageBlock | undefined;
|
|
32
|
+
//#endregion
|
|
33
|
+
export { buildImageRun, constrainImageToPage, convertImage, hasPaintableImageSource, hasRelationshipBackedImageBox };
|