Preserve QPowerPoint font families

This commit is contained in:
Курнат Андрей
2026-06-01 07:28:05 +03:00
parent f388489d2b
commit a4314be285
5 changed files with 74 additions and 12 deletions
+23 -3
View File
@@ -15,6 +15,7 @@ interface SlideTextBlock {
text: string;
color?: string;
fontSize?: number;
fontFamily?: string;
}
type XmlParserConstructor = new (options?: Record<string, unknown>) => { parse: (xml: string) => unknown };
@@ -71,8 +72,10 @@ export async function importPptxFile(file: File): Promise<ImportedPowerPointDeck
...(backgroundColor ? { backgroundColor } : {}),
...(slideTextBlocks[0]?.color ? { titleColor: slideTextBlocks[0].color } : {}),
...(slideTextBlocks[0]?.fontSize ? { titleFontSize: slideTextBlocks[0].fontSize } : {}),
...(slideTextBlocks[0]?.fontFamily ? { titleFontFamily: slideTextBlocks[0].fontFamily } : {}),
...(slideTextBlocks[1]?.color ? { subtitleColor: slideTextBlocks[1].color } : {}),
...(slideTextBlocks[1]?.fontSize ? { subtitleFontSize: slideTextBlocks[1].fontSize } : {}),
...(slideTextBlocks[1]?.fontFamily ? { subtitleFontFamily: slideTextBlocks[1].fontFamily } : {}),
images
};
})
@@ -112,6 +115,8 @@ export async function exportPptxBlob(deck: ExportablePowerPointDeck): Promise<Bl
const subtitleColor = normalizeHexColor(sourceSlide.subtitleColor) || colors.subtitle;
const titleFontSize = normalizeFontSize(sourceSlide.titleFontSize) || 30;
const subtitleFontSize = normalizeFontSize(sourceSlide.subtitleFontSize) || 18;
const titleFontFamily = normalizeFontFamily(sourceSlide.titleFontFamily) || "Arial";
const subtitleFontFamily = normalizeFontFamily(sourceSlide.subtitleFontFamily) || "Arial";
const slide = pptx.addSlide();
slide.background = { color: normalizeHexColor(sourceSlide.backgroundColor) || colors.background };
sourceSlide.images?.forEach((image, index) => {
@@ -134,7 +139,7 @@ export async function exportPptxBlob(deck: ExportablePowerPointDeck): Promise<Bl
y: 0.65,
w: 11.9,
h: 0.75,
fontFace: "Arial",
fontFace: titleFontFamily,
fontSize: titleFontSize,
bold: true,
color: titleColor,
@@ -146,7 +151,7 @@ export async function exportPptxBlob(deck: ExportablePowerPointDeck): Promise<Bl
y: 1.65,
w: 11.5,
h: 4.35,
fontFace: "Arial",
fontFace: subtitleFontFamily,
fontSize: subtitleFontSize,
color: subtitleColor,
valign: "top",
@@ -181,10 +186,12 @@ function extractSlideTextBlocks(slideXml: unknown): SlideTextBlock[] {
const runProperties = firstRecordByKey(textBody, "rPr");
const color = runTextColor(runProperties);
const fontSize = runTextFontSize(runProperties);
const fontFamily = runTextFontFamily(runProperties);
return {
text,
...(color ? { color } : {}),
...(fontSize ? { fontSize } : {})
...(fontSize ? { fontSize } : {}),
...(fontFamily ? { fontFamily } : {})
};
})
.filter((block): block is SlideTextBlock => Boolean(block));
@@ -398,6 +405,14 @@ function runTextFontSize(runProperties: Record<string, unknown>) {
return rawSize ? normalizeFontSize(rawSize / 100) : undefined;
}
function runTextFontFamily(runProperties: Record<string, unknown>) {
return normalizeFontFamily(
[childRecord(runProperties, "latin"), childRecord(runProperties, "ea"), childRecord(runProperties, "cs")]
.map((font) => stringValue(font.typeface ?? font["@_typeface"]))
.find(Boolean)
);
}
function normalizeFontSize(value?: number) {
if (!Number.isFinite(value) || !value || value <= 0) {
return undefined;
@@ -406,6 +421,11 @@ function normalizeFontSize(value?: number) {
return Math.round(value * 2) / 2;
}
function normalizeFontFamily(value?: string) {
const normalized = value?.trim().replace(/["<>]/g, "").slice(0, 80) ?? "";
return normalized && normalized.toLowerCase() !== "arial" ? normalized : "";
}
function numberAttribute(record: Record<string, unknown>, name: string) {
const value = Number.parseFloat(stringValue(record[name] ?? record[`@_${name}`]));
return Number.isFinite(value) ? value : undefined;