fix(slides): strip stale embedded-font subsets after text edits (#283)
Decks saved by WPS/PowerPoint with font embedding carry subsetted,
MicroType-Express-compressed EOT fonts we cannot re-generate (licensed
compressor). After any text edit the subsets no longer cover the new
glyphs; WPS honors the stale subset strictly and renders the missing
glyphs as invisible ("white") text, while the in-app system-font
fallback shows everything fine.
On save, once any slide has content edits (structureDirty or a dirty
element — including new slides from insert/duplicate), strip the
embedding: drop p:embeddedFontLst + embedTrueTypeFonts/saveSubsetFonts
from presentation.xml, the font rels, the ppt/fonts/*.fntdata parts and
the fntdata content-type. Viewers then fall back to system fonts.
No-edit saves stay byte-identical and keep the embedding.
Known limit: master/layout text edits write archive parts directly and
don't trigger the strip.
Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -48,6 +48,51 @@ export function eotToSfnt(bytes: Uint8Array): Uint8Array | null {
|
||||
return sfnt
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the package's embedded fonts: <p:embeddedFontLst> and the
|
||||
* embedTrueTypeFonts/saveSubsetFonts attributes in presentation.xml, the font
|
||||
* relationships in its rels, the ppt/fonts/*.fntdata parts, and the
|
||||
* [Content_Types] fntdata declaration.
|
||||
*
|
||||
* Why: the embedded subsets cannot be re-generated after a text edit (MicroType
|
||||
* Express compression needs a licensed compressor), and WPS honors a stale
|
||||
* subset strictly — glyphs it doesn't cover render as invisible text while the
|
||||
* in-app system-font fallback shows them fine. Stripping makes every viewer
|
||||
* fall back to system fonts. Idempotent; returns whether anything was stripped.
|
||||
*/
|
||||
export function stripEmbeddedFonts(archive: PackageArchive): boolean {
|
||||
const presPath = 'ppt/presentation.xml'
|
||||
const pres = archive.readText(presPath)
|
||||
if (!pres || !pres.includes('embeddedFont')) return false
|
||||
|
||||
const nextPres = pres
|
||||
.replace(/<p:embeddedFontLst>[\s\S]*?<\/p:embeddedFontLst>/, '')
|
||||
.replace(/ embedTrueTypeFonts="[^"]*"/, '')
|
||||
.replace(/ saveSubsetFonts="[^"]*"/, '')
|
||||
archive.entries.set(presPath, Buffer.from(nextPres, 'utf8'))
|
||||
|
||||
const relsPath = 'ppt/_rels/presentation.xml.rels'
|
||||
const rels = archive.readText(relsPath)
|
||||
if (rels) {
|
||||
const nextRels = rels.replace(/<Relationship\b[^>]*\bType="[^"]*\/font"[^>]*\/>/g, '')
|
||||
if (nextRels !== rels) archive.entries.set(relsPath, Buffer.from(nextRels, 'utf8'))
|
||||
}
|
||||
|
||||
const ctPath = '[Content_Types].xml'
|
||||
const ct = archive.readText(ctPath)
|
||||
if (ct) {
|
||||
const nextCt = ct
|
||||
.replace(/<Default\b[^>]*\bExtension="fntdata"[^>]*\/>/g, '')
|
||||
.replace(/<Override\b[^>]*\bPartName="\/ppt\/fonts\/[^"]*"[^>]*\/>/g, '')
|
||||
if (nextCt !== ct) archive.entries.set(ctPath, Buffer.from(nextCt, 'utf8'))
|
||||
}
|
||||
|
||||
for (const path of [...archive.entries.keys()]) {
|
||||
if (/^ppt\/fonts\/[^/]+\.fntdata$/.test(path)) archive.entries.delete(path)
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
/** Usable (uncompressed) embedded faces of a package; empty when none are declared. */
|
||||
export function listEmbeddedFonts(archive: PackageArchive): EmbeddedFontFace[] {
|
||||
const presXml = archive.readText('ppt/presentation.xml')
|
||||
|
||||
@@ -53,6 +53,7 @@ import {
|
||||
import { BLANK_SLIDE_XML } from './blank'
|
||||
import { escapeXmlAttr } from './xml-utils'
|
||||
import { elementSpid } from './animation'
|
||||
import { stripEmbeddedFonts } from './embedded-fonts'
|
||||
import { ensureCreationId, matchesElementRef } from './identity'
|
||||
import { listMasterParts, parseMasterPart } from './master-edit'
|
||||
import type {
|
||||
@@ -736,6 +737,14 @@ function slideIsDirty(s: Slide): boolean {
|
||||
|
||||
function buildZip(opened: OpenedPptx): JSZip {
|
||||
const { deck, archive } = opened
|
||||
// Embedded-font subsets can't be re-generated after a text edit (MicroType
|
||||
// Express needs a licensed compressor), and WPS honors a stale subset
|
||||
// strictly: glyphs it doesn't cover render as invisible text while the in-app
|
||||
// system-font fallback shows them fine. Once any content edit lands, strip
|
||||
// the embedding so every viewer falls back to system fonts.
|
||||
if (deck.slides.some((s) => s.structureDirty || s.elements.some((e) => e.dirty))) {
|
||||
stripEmbeddedFonts(archive)
|
||||
}
|
||||
const dirtyByPath = new Map<string, Slide>()
|
||||
for (const s of deck.slides) {
|
||||
if (slideIsDirty(s)) dirtyByPath.set(s.path, s)
|
||||
@@ -1718,6 +1727,9 @@ function registerNewSlide(opened: OpenedPptx, sourceIndex: number, newPath: stri
|
||||
|
||||
const slide = parseSlideFromArchive(archive, newPath)
|
||||
if (!slide) return null
|
||||
// A new slide can carry text the embedded-font subsets don't cover (they go
|
||||
// stale the moment text changes); structureDirty lets the save strip them.
|
||||
slide.structureDirty = true
|
||||
deck.slides.splice(sourceIndex + 1, 0, slide)
|
||||
return slide
|
||||
}
|
||||
|
||||
@@ -1,7 +1,14 @@
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import JSZip from 'jszip'
|
||||
import { readFileSync } from 'node:fs'
|
||||
import { fileURLToPath } from 'node:url'
|
||||
import { dirname, join } from 'node:path'
|
||||
import { PackageArchive } from '../src/zip'
|
||||
import { eotToSfnt, listEmbeddedFonts } from '../src/embedded-fonts'
|
||||
import { eotToSfnt, listEmbeddedFonts, stripEmbeddedFonts } from '../src/embedded-fonts'
|
||||
import { openPptx, savePptx, setElementFont, duplicateSlide } from '../src/index'
|
||||
|
||||
const here = dirname(fileURLToPath(import.meta.url))
|
||||
const fx = (name: string) => readFileSync(join(here, 'fixtures', name))
|
||||
|
||||
/** Minimal valid-enough sfnt payload: TrueType magic + filler. */
|
||||
function fakeSfnt(seed = 1): Uint8Array {
|
||||
@@ -94,3 +101,139 @@ describe('listEmbeddedFonts', () => {
|
||||
expect(listEmbeddedFonts(archive)).toEqual([])
|
||||
})
|
||||
})
|
||||
|
||||
describe('stripEmbeddedFonts', () => {
|
||||
async function archiveWithFonts(): Promise<PackageArchive> {
|
||||
const zip = new JSZip()
|
||||
zip.file(
|
||||
'ppt/presentation.xml',
|
||||
'<?xml version="1.0"?><p:presentation xmlns:p="http://schemas.openxmlformats.org/presentationml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" embedTrueTypeFonts="1" saveSubsetFonts="1">' +
|
||||
'<p:sldIdLst><p:sldId id="256" r:id="rId2"/></p:sldIdLst>' +
|
||||
'<p:embeddedFontLst>' +
|
||||
'<p:embeddedFont><p:font typeface="MiSans"/><p:regular r:id="rId7"/></p:embeddedFont>' +
|
||||
'</p:embeddedFontLst>' +
|
||||
'<p:sldSz cx="12192000" cy="6858000"/></p:presentation>',
|
||||
)
|
||||
zip.file(
|
||||
'ppt/_rels/presentation.xml.rels',
|
||||
'<?xml version="1.0"?><Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">' +
|
||||
'<Relationship Id="rId2" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/slide" Target="slides/slide1.xml"/>' +
|
||||
'<Relationship Id="rId7" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/font" Target="fonts/font1.fntdata"/>' +
|
||||
'</Relationships>',
|
||||
)
|
||||
zip.file(
|
||||
'[Content_Types].xml',
|
||||
'<?xml version="1.0"?><Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types">' +
|
||||
'<Default Extension="fntdata" ContentType="application/x-fontdata"/>' +
|
||||
'<Default Extension="xml" ContentType="application/xml"/>' +
|
||||
'</Types>',
|
||||
)
|
||||
zip.file('ppt/fonts/font1.fntdata', eotWrap(fakeSfnt(), 0x5))
|
||||
return PackageArchive.open(await zip.generateAsync({ type: 'uint8array' }))
|
||||
}
|
||||
|
||||
it('removes embeddedFontLst, font rels, fntdata parts and the content-type declaration', async () => {
|
||||
const archive = await archiveWithFonts()
|
||||
expect(stripEmbeddedFonts(archive)).toBe(true)
|
||||
|
||||
const pres = archive.readText('ppt/presentation.xml')!
|
||||
expect(pres).not.toContain('embeddedFont')
|
||||
expect(pres).not.toContain('embedTrueTypeFonts')
|
||||
expect(pres).not.toContain('saveSubsetFonts')
|
||||
expect(pres).toContain('<p:sldIdLst>')
|
||||
|
||||
const rels = archive.readText('ppt/_rels/presentation.xml.rels')!
|
||||
expect(rels).not.toContain('/font"')
|
||||
expect(rels).toContain('slides/slide1.xml') // non-font rels survive
|
||||
|
||||
const ct = archive.readText('[Content_Types].xml')!
|
||||
expect(ct).not.toContain('fntdata')
|
||||
expect(ct).toContain('Extension="xml"')
|
||||
|
||||
expect(archive.has('ppt/fonts/font1.fntdata')).toBe(false)
|
||||
})
|
||||
|
||||
it('is a no-op when no embedded fonts are declared', async () => {
|
||||
const archive = await archiveWithFonts()
|
||||
stripEmbeddedFonts(archive)
|
||||
expect(stripEmbeddedFonts(archive)).toBe(false)
|
||||
})
|
||||
})
|
||||
|
||||
describe('save strips stale embedded-font subsets after edits', () => {
|
||||
/** Fixture deck with an embeddedFontLst + fntdata part injected. */
|
||||
async function fixtureWithEmbeddedFonts(): Promise<Uint8Array> {
|
||||
const zip = await JSZip.loadAsync(fx('01_standard_business.pptx'))
|
||||
const pres = await zip.file('ppt/presentation.xml')!.async('string')
|
||||
zip.file(
|
||||
'ppt/presentation.xml',
|
||||
pres
|
||||
.replace('<p:presentation ', '<p:presentation embedTrueTypeFonts="1" saveSubsetFonts="1" ')
|
||||
.replace(
|
||||
'</p:presentation>',
|
||||
'<p:embeddedFontLst><p:embeddedFont><p:font typeface="MiSans"/><p:regular r:id="rId900"/></p:embeddedFont></p:embeddedFontLst></p:presentation>',
|
||||
),
|
||||
)
|
||||
const rels = await zip.file('ppt/_rels/presentation.xml.rels')!.async('string')
|
||||
zip.file(
|
||||
'ppt/_rels/presentation.xml.rels',
|
||||
rels.replace(
|
||||
'</Relationships>',
|
||||
'<Relationship Id="rId900" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/font" Target="fonts/font1.fntdata"/></Relationships>',
|
||||
),
|
||||
)
|
||||
const ct = await zip.file('[Content_Types].xml')!.async('string')
|
||||
zip.file(
|
||||
'[Content_Types].xml',
|
||||
ct.replace(
|
||||
/<Types[^>]*>/,
|
||||
'$&<Default Extension="fntdata" ContentType="application/x-fontdata"/>',
|
||||
),
|
||||
)
|
||||
zip.file('ppt/fonts/font1.fntdata', eotWrap(fakeSfnt(), 0x5))
|
||||
return zip.generateAsync({ type: 'uint8array' })
|
||||
}
|
||||
|
||||
async function savedPresentationXml(bytes: Uint8Array): Promise<JSZip> {
|
||||
return JSZip.loadAsync(bytes)
|
||||
}
|
||||
|
||||
it('keeps embedded fonts on a no-edit save', async () => {
|
||||
const opened = await openPptx(await fixtureWithEmbeddedFonts())
|
||||
const zip = await savedPresentationXml(await savePptx(opened))
|
||||
const pres = await zip.file('ppt/presentation.xml')!.async('string')
|
||||
expect(pres).toContain('embeddedFontLst')
|
||||
expect(zip.file('ppt/fonts/font1.fntdata')).not.toBeNull()
|
||||
})
|
||||
|
||||
it('strips embedded fonts once a run is edited', async () => {
|
||||
const opened = await openPptx(await fixtureWithEmbeddedFonts())
|
||||
const slide = opened.deck.slides.find((s) =>
|
||||
s.elements.some(
|
||||
(e) => (e.type === 'text' || e.type === 'shape') && (e as any).text?.paragraphs?.length,
|
||||
),
|
||||
)!
|
||||
const el = slide.elements.find(
|
||||
(e) => (e.type === 'text' || e.type === 'shape') && (e as any).text?.paragraphs?.length,
|
||||
)!
|
||||
expect(setElementFont(slide, el.id, { color: '#123456' })).toBe(true)
|
||||
|
||||
const zip = await savedPresentationXml(await savePptx(opened))
|
||||
const pres = await zip.file('ppt/presentation.xml')!.async('string')
|
||||
expect(pres).not.toContain('embeddedFont')
|
||||
expect(pres).not.toContain('embedTrueTypeFonts')
|
||||
const rels = await zip.file('ppt/_rels/presentation.xml.rels')!.async('string')
|
||||
expect(rels).not.toContain('/font"')
|
||||
expect(zip.file('ppt/fonts/font1.fntdata')).toBeNull()
|
||||
})
|
||||
|
||||
it('strips embedded fonts when a slide is duplicated (new text may not be in the subsets)', async () => {
|
||||
const opened = await openPptx(await fixtureWithEmbeddedFonts())
|
||||
expect(duplicateSlide(opened, 0)).not.toBeNull()
|
||||
|
||||
const zip = await savedPresentationXml(await savePptx(opened))
|
||||
const pres = await zip.file('ppt/presentation.xml')!.async('string')
|
||||
expect(pres).not.toContain('embeddedFont')
|
||||
expect(zip.file('ppt/fonts/font1.fntdata')).toBeNull()
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user