Preserve QPowerPoint text run styles in PPTX
This commit is contained in:
@@ -15,6 +15,9 @@ interface SlideTextBlock {
|
||||
text: string;
|
||||
listStyle?: SlideListStyle;
|
||||
hyperlink?: string;
|
||||
bold?: boolean;
|
||||
italics?: boolean;
|
||||
underline?: boolean;
|
||||
color?: string;
|
||||
fontSize?: number;
|
||||
fontFamily?: string;
|
||||
@@ -99,9 +102,15 @@ export async function importPptxFile(file: File): Promise<ImportedPowerPointDeck
|
||||
...(hidden ? { hidden } : {}),
|
||||
...(transition ? { transition } : {}),
|
||||
...(backgroundColor ? { backgroundColor } : {}),
|
||||
titleBold: slideTextBlocks[0]?.bold ?? false,
|
||||
...(slideTextBlocks[0]?.italics ? { titleItalics: true } : {}),
|
||||
...(slideTextBlocks[0]?.underline ? { titleUnderline: true } : {}),
|
||||
...(slideTextBlocks[0]?.color ? { titleColor: slideTextBlocks[0].color } : {}),
|
||||
...(slideTextBlocks[0]?.fontSize ? { titleFontSize: slideTextBlocks[0].fontSize } : {}),
|
||||
...(slideTextBlocks[0]?.fontFamily ? { titleFontFamily: slideTextBlocks[0].fontFamily } : {}),
|
||||
...(slideTextBlocks[1]?.bold ? { subtitleBold: true } : {}),
|
||||
...(slideTextBlocks[1]?.italics ? { subtitleItalics: true } : {}),
|
||||
...(slideTextBlocks[1]?.underline ? { subtitleUnderline: true } : {}),
|
||||
...(slideTextBlocks[1]?.color ? { subtitleColor: slideTextBlocks[1].color } : {}),
|
||||
...(slideTextBlocks[1]?.fontSize ? { subtitleFontSize: slideTextBlocks[1].fontSize } : {}),
|
||||
...(slideTextBlocks[1]?.fontFamily ? { subtitleFontFamily: slideTextBlocks[1].fontFamily } : {}),
|
||||
@@ -147,6 +156,12 @@ export async function exportPptxBlob(deck: ExportablePowerPointDeck): Promise<Bl
|
||||
const subtitleFontSize = normalizeFontSize(sourceSlide.subtitleFontSize) || 18;
|
||||
const titleFontFamily = normalizeFontFamily(sourceSlide.titleFontFamily) || "Arial";
|
||||
const subtitleFontFamily = normalizeFontFamily(sourceSlide.subtitleFontFamily) || "Arial";
|
||||
const titleBold = sourceSlide.titleBold ?? true;
|
||||
const titleItalics = Boolean(sourceSlide.titleItalics);
|
||||
const titleUnderline = Boolean(sourceSlide.titleUnderline);
|
||||
const subtitleBold = Boolean(sourceSlide.subtitleBold);
|
||||
const subtitleItalics = Boolean(sourceSlide.subtitleItalics);
|
||||
const subtitleUnderline = Boolean(sourceSlide.subtitleUnderline);
|
||||
const titleHyperlink = normalizedExternalHyperlinkTarget(sourceSlide.titleHyperlink);
|
||||
const subtitleHyperlink = normalizedExternalHyperlinkTarget(sourceSlide.subtitleHyperlink);
|
||||
const slide = pptx.addSlide();
|
||||
@@ -175,7 +190,9 @@ export async function exportPptxBlob(deck: ExportablePowerPointDeck): Promise<Bl
|
||||
h: 0.75,
|
||||
fontFace: titleFontFamily,
|
||||
fontSize: titleFontSize,
|
||||
bold: true,
|
||||
bold: titleBold,
|
||||
italic: titleItalics,
|
||||
...(titleUnderline ? { underline: { style: "sng" } } : {}),
|
||||
color: titleColor,
|
||||
margin: 0.04,
|
||||
breakLine: false,
|
||||
@@ -188,6 +205,9 @@ export async function exportPptxBlob(deck: ExportablePowerPointDeck): Promise<Bl
|
||||
h: 4.35,
|
||||
fontFace: subtitleFontFamily,
|
||||
fontSize: subtitleFontSize,
|
||||
bold: subtitleBold,
|
||||
italic: subtitleItalics,
|
||||
...(subtitleUnderline ? { underline: { style: "sng" } } : {}),
|
||||
color: subtitleColor,
|
||||
valign: "top",
|
||||
fit: "shrink",
|
||||
@@ -216,7 +236,7 @@ export function extractTextBlocks(xmlNode: unknown): string[] {
|
||||
function extractSlideTextBlocks(slideXml: unknown, hyperlinkTargets: Record<string, string> = {}): SlideTextBlock[] {
|
||||
const textBodies = slideTextBodyRecords(slideXml);
|
||||
const blocks = textBodies
|
||||
.map(({ textBody, hyperlinkSource }) => {
|
||||
.map(({ textBody, hyperlinkSource }): SlideTextBlock | null => {
|
||||
const text = extractTextBodyText(textBody);
|
||||
if (!text) {
|
||||
return null;
|
||||
@@ -225,6 +245,9 @@ function extractSlideTextBlocks(slideXml: unknown, hyperlinkTargets: Record<stri
|
||||
const runProperties = firstRecordByKey(textBody, "rPr");
|
||||
const hyperlink = textBodyHyperlink(hyperlinkSource, hyperlinkTargets);
|
||||
const listStyle = textBodyListStyle(textBody);
|
||||
const bold = runTextBold(runProperties);
|
||||
const italics = runTextItalics(runProperties);
|
||||
const underline = runTextUnderline(runProperties);
|
||||
const color = runTextColor(runProperties);
|
||||
const fontSize = runTextFontSize(runProperties);
|
||||
const fontFamily = runTextFontFamily(runProperties);
|
||||
@@ -232,6 +255,9 @@ function extractSlideTextBlocks(slideXml: unknown, hyperlinkTargets: Record<stri
|
||||
text,
|
||||
...(listStyle ? { listStyle } : {}),
|
||||
...(hyperlink ? { hyperlink } : {}),
|
||||
bold,
|
||||
italics,
|
||||
underline,
|
||||
...(color ? { color } : {}),
|
||||
...(fontSize ? { fontSize } : {}),
|
||||
...(fontFamily ? { fontFamily } : {})
|
||||
@@ -667,6 +693,19 @@ function runTextColor(runProperties: Record<string, unknown>) {
|
||||
return normalizeHexColor(stringValue(srgbColor.val ?? srgbColor["@_val"]));
|
||||
}
|
||||
|
||||
function runTextBold(runProperties: Record<string, unknown>) {
|
||||
return trueXmlBooleanAttribute(runProperties, "b");
|
||||
}
|
||||
|
||||
function runTextItalics(runProperties: Record<string, unknown>) {
|
||||
return trueXmlBooleanAttribute(runProperties, "i");
|
||||
}
|
||||
|
||||
function runTextUnderline(runProperties: Record<string, unknown>) {
|
||||
const value = stringValue(runProperties.u ?? runProperties["@_u"]).trim().toLowerCase();
|
||||
return Boolean(value && value !== "none");
|
||||
}
|
||||
|
||||
function runTextFontSize(runProperties: Record<string, unknown>) {
|
||||
const rawSize = numberAttribute(runProperties, "sz");
|
||||
return rawSize ? normalizeFontSize(rawSize / 100) : undefined;
|
||||
@@ -703,6 +742,12 @@ function isFalseXmlBoolean(value: unknown) {
|
||||
return normalized === "0" || normalized === "false";
|
||||
}
|
||||
|
||||
function trueXmlBooleanAttribute(record: Record<string, unknown>, name: string) {
|
||||
const value = record[name] ?? record[`@_${name}`];
|
||||
const normalized = String(value ?? "").trim();
|
||||
return Boolean(normalized && !isFalseXmlBoolean(value));
|
||||
}
|
||||
|
||||
function emuToInches(value: number) {
|
||||
return Math.max(0, Math.round((value / emuPerInch) * 1000) / 1000);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user