Preserve QWord DOCX font size

This commit is contained in:
Курнат Андрей
2026-06-01 06:53:02 +03:00
parent 024f208538
commit 9f5a003bae
4 changed files with 107 additions and 7 deletions
+57 -5
View File
@@ -13,6 +13,7 @@ export interface WordInlineRun {
href?: string;
color?: string;
highlightColor?: string;
fontSize?: number;
}
export interface WordListItem {
@@ -357,6 +358,10 @@ function wordInlineRunToTextRunOption(run: WordInlineRun) {
if (run.highlightColor) {
options.shading = { fill: run.highlightColor };
}
const fontSize = normalizedPointSize(run.fontSize);
if (fontSize) {
options.size = Math.round(fontSize * 2);
}
return options;
}
@@ -496,6 +501,25 @@ function cssColorValue(style: string, property: "color" | "background" | "backgr
return normalizedCssColor(value);
}
function cssFontSizeValue(style: string) {
const raw = /(?:^|;)\s*font-size\s*:\s*([^;]+)/i.exec(style)?.[1]?.trim().toLowerCase() ?? "";
if (!raw || ["inherit", "initial", "unset", "revert"].includes(raw)) {
return undefined;
}
const match = /^([0-9]+(?:\.[0-9]+)?)(pt|px)$/i.exec(raw);
if (!match) {
return undefined;
}
const value = Number.parseFloat(match[1]);
if (!Number.isFinite(value) || value <= 0) {
return undefined;
}
return normalizedPointSize(match[2].toLowerCase() === "px" ? value * 0.75 : value);
}
function normalizedCssColor(value: string) {
const raw = value.trim().toLowerCase();
if (!raw || ["transparent", "inherit", "initial", "currentcolor", "auto"].includes(raw)) {
@@ -642,7 +666,8 @@ function docxRunToHtml(run: unknown) {
const text = docxRunText(record);
const color = docxRunColor(properties);
const highlightColor = docxRunHighlightColor(properties);
const hasInlineColorOrHighlight = Boolean(color || highlightColor);
const fontSize = docxRunFontSize(properties);
const hasInlineColorOrHighlight = Boolean(color || highlightColor || fontSize);
let html = escapeHtml(text);
if (!text) {
@@ -661,7 +686,8 @@ function docxRunToHtml(run: unknown) {
const styles = [
color ? `color: #${color};` : "",
highlightColor ? `background-color: #${highlightColor};` : ""
highlightColor ? `background-color: #${highlightColor};` : "",
fontSize ? `font-size: ${formatPointSize(fontSize)}pt;` : ""
].filter(Boolean);
if (styles.length > 0) {
@@ -712,6 +738,15 @@ function docxRunHighlightColor(properties: XmlRecord) {
return highlightColors[highlight] ?? "";
}
function docxRunFontSize(properties: XmlRecord) {
const value = Number.parseFloat(docxAttribute(childRecord(properties, "w:sz"), "w:val"));
if (!Number.isFinite(value) || value <= 0) {
return undefined;
}
return normalizedPointSize(value / 2);
}
function normalizedDocxHexColor(value: string) {
const normalized = value.trim().toUpperCase();
if (!normalized || normalized === "AUTO" || normalized === "NONE") {
@@ -806,6 +841,7 @@ function formatForElement(element: Element, inherited: InlineFormat): InlineForm
const color = cssColorValue(style, "color") || inherited.color;
const highlightColor =
cssColorValue(style, "background-color") || cssColorValue(style, "background") || inherited.highlightColor;
const fontSize = cssFontSizeValue(style) ?? inherited.fontSize;
return cleanInlineFormat({
bold: inherited.bold || tagName === "b" || tagName === "strong" || /font-weight\s*:\s*(bold|[6-9]00)\b/i.test(style),
@@ -813,7 +849,8 @@ function formatForElement(element: Element, inherited: InlineFormat): InlineForm
underline: inherited.underline || tagName === "u" || /text-decoration(?:-line)?\s*:[^;]*underline/i.test(style),
...(href ? { href } : {}),
...(color ? { color } : {}),
...(highlightColor ? { highlightColor } : {})
...(highlightColor ? { highlightColor } : {}),
...(fontSize ? { fontSize } : {})
});
}
@@ -851,7 +888,8 @@ function sameInlineFormat(first: InlineFormat, second: InlineFormat) {
Boolean(first.underline) === Boolean(second.underline) &&
(first.href ?? "") === (second.href ?? "") &&
(first.color ?? "") === (second.color ?? "") &&
(first.highlightColor ?? "") === (second.highlightColor ?? "")
(first.highlightColor ?? "") === (second.highlightColor ?? "") &&
(first.fontSize ?? 0) === (second.fontSize ?? 0)
);
}
@@ -862,10 +900,24 @@ function cleanInlineFormat(format: InlineFormat): InlineFormat {
...(format.underline ? { underline: true } : {}),
...(format.href ? { href: format.href } : {}),
...(format.color ? { color: format.color } : {}),
...(format.highlightColor ? { highlightColor: format.highlightColor } : {})
...(format.highlightColor ? { highlightColor: format.highlightColor } : {}),
...(format.fontSize ? { fontSize: format.fontSize } : {})
};
}
function normalizedPointSize(value: unknown) {
const numeric = typeof value === "number" ? value : Number.parseFloat(String(value ?? ""));
if (!Number.isFinite(numeric) || numeric <= 0) {
return undefined;
}
return Math.round(numeric * 2) / 2;
}
function formatPointSize(value: number) {
return Number.isInteger(value) ? String(value) : value.toFixed(1);
}
function normalizedHyperlinkTarget(value?: string | null) {
const raw = value?.trim();
if (!raw) {