Preserve QPowerPoint font families
This commit is contained in:
@@ -263,8 +263,10 @@ describe("powerPointOffice helpers", () => {
|
||||
subtitle: "Подзаголовок",
|
||||
titleColor: "C2410C",
|
||||
titleFontSize: 34,
|
||||
titleFontFamily: "Courier New",
|
||||
subtitleColor: "0F766E",
|
||||
subtitleFontSize: 22
|
||||
subtitleFontSize: 22,
|
||||
subtitleFontFamily: "Times New Roman"
|
||||
});
|
||||
|
||||
const exportedBlob = await exportPptxBlob({
|
||||
@@ -278,8 +280,10 @@ describe("powerPointOffice helpers", () => {
|
||||
theme: "classic",
|
||||
titleColor: "C2410C",
|
||||
titleFontSize: 34,
|
||||
titleFontFamily: "Courier New",
|
||||
subtitleColor: "0F766E",
|
||||
subtitleFontSize: 22
|
||||
subtitleFontSize: 22,
|
||||
subtitleFontFamily: "Times New Roman"
|
||||
}
|
||||
]
|
||||
});
|
||||
@@ -290,6 +294,8 @@ describe("powerPointOffice helpers", () => {
|
||||
expect(exportedSlideXml).toContain("0F766E");
|
||||
expect(exportedSlideXml).toContain('sz="3400"');
|
||||
expect(exportedSlideXml).toContain('sz="2200"');
|
||||
expect(exportedSlideXml).toContain('typeface="Courier New"');
|
||||
expect(exportedSlideXml).toContain('typeface="Times New Roman"');
|
||||
});
|
||||
|
||||
it("нормализует пробелы в импортированном тексте", () => {
|
||||
@@ -308,7 +314,7 @@ function slideXmlWithBackground(title: string, color: string) {
|
||||
}
|
||||
|
||||
function slideXmlWithTextStyles(title: string, subtitle: string) {
|
||||
return `<p:sld xmlns:p="http://schemas.openxmlformats.org/presentationml/2006/main" xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main"><p:cSld><p:spTree><p:sp><p:txBody><a:p><a:r><a:rPr sz="3400"><a:solidFill><a:srgbClr val="C2410C"/></a:solidFill></a:rPr><a:t>${title}</a:t></a:r></a:p></p:txBody></p:sp><p:sp><p:txBody><a:p><a:r><a:rPr sz="2200"><a:solidFill><a:srgbClr val="0F766E"/></a:solidFill></a:rPr><a:t>${subtitle}</a:t></a:r></a:p></p:txBody></p:sp></p:spTree></p:cSld></p:sld>`;
|
||||
return `<p:sld xmlns:p="http://schemas.openxmlformats.org/presentationml/2006/main" xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main"><p:cSld><p:spTree><p:sp><p:txBody><a:p><a:r><a:rPr sz="3400"><a:solidFill><a:srgbClr val="C2410C"/></a:solidFill><a:latin typeface="Courier New"/></a:rPr><a:t>${title}</a:t></a:r></a:p></p:txBody></p:sp><p:sp><p:txBody><a:p><a:r><a:rPr sz="2200"><a:solidFill><a:srgbClr val="0F766E"/></a:solidFill><a:latin typeface="Times New Roman"/></a:rPr><a:t>${subtitle}</a:t></a:r></a:p></p:txBody></p:sp></p:spTree></p:cSld></p:sld>`;
|
||||
}
|
||||
|
||||
function slideXmlWithPicture(title: string) {
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user