Sync snapshot (2026-08-09) (#75)
- PDF: in-place text editing — paragraph block selection with reflow, bold/italic and alignment controls, selection-level text color, original-font preservation - PDF: AI image tools (search/generate, insert, transform, delete) plus image rotation and in-place replacement - Docs: picture rotation and mirror flips, in-place image byte swap for crop/background removal, and several Word-fidelity rendering fixes - Docs: fix a renderer hang on tab-heavy forms and a crash when exporting large documents - Slides: AI picture tools (crop, opacity, replace), picture border controls, in-place picture source swap - Linux: AppImage and deb packaging with stable/beta auto-update channels - Refresh the README and clean up internal design-audit artifacts Co-authored-by: GenOffice <merrick-2002@users.noreply.github.com>
This commit is contained in:
co-authored by
GenOffice
parent
767b87bded
commit
e249c3ba81
@@ -779,6 +779,58 @@ export function setPictureOpacity(slide: Slide, sourceId: string, opacity: numbe
|
||||
return true
|
||||
}
|
||||
|
||||
/**
|
||||
* Swap a picture's backing image for new bytes, keeping frame, z-order, border
|
||||
* and effects. New media part + rel; the <a:blip> reference is re-pointed via
|
||||
* byte surgery baked into originalXml (same pattern as setPictureOpacity).
|
||||
* srcRect is kept only when the caller knows the new image shares the old one's
|
||||
* pixel geometry (e.g. background-removal output) — otherwise the stale crop
|
||||
* would show an arbitrary window of the new image.
|
||||
*/
|
||||
export function replacePictureBytes(
|
||||
opened: OpenedPptx,
|
||||
slide: Slide,
|
||||
sourceId: string,
|
||||
bytes: Uint8Array,
|
||||
ext: string,
|
||||
opts?: { keepSrcRect?: boolean },
|
||||
): boolean {
|
||||
const el = slide.elements.find((e) => e.id === sourceId && e.type === 'picture')
|
||||
if (!el) return false
|
||||
const pic = el as import('./types').PictureElement
|
||||
let xml = patchedElementXml(el)
|
||||
const blip = /<a:blip\b[^>]*\/?>/.exec(xml)
|
||||
if (!blip) return false
|
||||
const added = addImageMediaAndRel(opened, slide, bytes, ext)
|
||||
if (!added) return false
|
||||
let tag = blip[0]
|
||||
// A coexisting r:link ("insert and link" pictures) would keep refreshing from
|
||||
// the old external file, so it is dropped once the embed points at new bytes
|
||||
if (/r:embed="/.test(tag))
|
||||
tag = tag.replace(/r:embed="[^"]*"/, `r:embed="${added.rid}"`).replace(/\s+r:link="[^"]*"/, '')
|
||||
else if (/r:link="/.test(tag)) tag = tag.replace(/r:link="[^"]*"/, `r:embed="${added.rid}"`)
|
||||
else tag = tag.replace(/<a:blip\b/, `<a:blip r:embed="${added.rid}"`)
|
||||
xml = xml.slice(0, blip.index) + tag + xml.slice(blip.index + blip[0].length)
|
||||
// The replacement is always raster (IMAGE_MIME gate), and PowerPoint prefers a
|
||||
// leftover Office-2016 <asvg:svgBlip> extension over the retargeted r:embed —
|
||||
// drop that extension entry (and its wrapper when nothing else remains)
|
||||
xml = xml
|
||||
.replace(/<a:ext\b[^>]*>\s*<\w+:svgBlip\b[\s\S]*?<\/a:ext>/, '')
|
||||
.replace(/<a:extLst>\s*<\/a:extLst>/, '')
|
||||
if (!opts?.keepSrcRect) {
|
||||
xml = xml.replace(/<a:srcRect\b[^>]*\/>|<a:srcRect\b[^>]*>[\s\S]*?<\/a:srcRect>/, '')
|
||||
delete pic.srcRect
|
||||
}
|
||||
pic.mediaRef = added.mediaPath
|
||||
delete pic.dataUrl // the media resolver re-derives it from the new mediaRef
|
||||
el.dirty = el.dirtyTransform = el.dirtyFill = el.dirtyStroke = false
|
||||
el.dirtySrcRect = false
|
||||
el.dirtyPPr = undefined
|
||||
el.anchor.originalXml = xml
|
||||
slide.structureDirty = true
|
||||
return true
|
||||
}
|
||||
|
||||
// ── New slides (duplicate an existing slide / blank slide) ────────────
|
||||
|
||||
const SLIDE_CONTENT_TYPE = 'application/vnd.openxmlformats-officedocument.presentationml.slide+xml'
|
||||
|
||||
@@ -9,7 +9,9 @@ import {
|
||||
addElement,
|
||||
addPicture,
|
||||
createBlankPptx,
|
||||
editPictureSrcRect,
|
||||
patchElementStroke,
|
||||
replacePictureBytes,
|
||||
setSlideBackground,
|
||||
} from '../src/index'
|
||||
import type { PictureElement, TextElement } from '../src/types'
|
||||
@@ -62,6 +64,102 @@ describe('addPicture', () => {
|
||||
})
|
||||
})
|
||||
|
||||
describe('replacePictureBytes', () => {
|
||||
// 1x1 transparent GIF (distinct format exercises the Content_Types Default too)
|
||||
const GIF_1PX = Uint8Array.from(
|
||||
Buffer.from('R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7', 'base64'),
|
||||
)
|
||||
|
||||
async function openWithPicture() {
|
||||
const opened0 = await openPptx(await createBlankPptx())
|
||||
addPicture(opened0, opened0.deck.slides[0]!, { bytes: PNG_1PX, ext: 'png', offset: { ...OFF } })
|
||||
// reopen so the picture is a parsed element with on-disk originalXml
|
||||
const opened = await openPptx(await savePptx(opened0))
|
||||
const slide = opened.deck.slides[0]!
|
||||
const pic = slide.elements.find((e) => e.type === 'picture') as PictureElement
|
||||
return { opened, slide, pic }
|
||||
}
|
||||
|
||||
it('swaps media in place, keeps frame + crop when asked, survives save → reopen', async () => {
|
||||
const { opened, slide, pic } = await openWithPicture()
|
||||
expect(editPictureSrcRect(slide, pic.id, { l: 0.1, t: 0.1, r: 0.1, b: 0.1 })).toBe(true)
|
||||
expect(replacePictureBytes(opened, slide, pic.id, GIF_1PX, 'gif', { keepSrcRect: true })).toBe(
|
||||
true,
|
||||
)
|
||||
expect(pic.mediaRef).toBe('ppt/media/image2.gif')
|
||||
|
||||
const out = await savePptx(opened)
|
||||
const zip = await JSZip.loadAsync(out)
|
||||
expect(new Uint8Array(await zip.file('ppt/media/image2.gif')!.async('uint8array'))).toEqual(
|
||||
GIF_1PX,
|
||||
)
|
||||
expect(await zip.file('[Content_Types].xml')!.async('string')).toContain('Extension="gif"')
|
||||
|
||||
const reopened = await openPptx(out)
|
||||
const pic2 = reopened.deck.slides[0]!.elements.find(
|
||||
(e) => e.type === 'picture',
|
||||
) as PictureElement
|
||||
expect(pic2.mediaRef).toBe('ppt/media/image2.gif')
|
||||
expect(pic2.srcRect).toEqual({ l: 0.1, t: 0.1, r: 0.1, b: 0.1 })
|
||||
expect(pic2.transform.offset).toEqual(OFF)
|
||||
})
|
||||
|
||||
it('clears a stale crop window by default', async () => {
|
||||
const { opened, slide, pic } = await openWithPicture()
|
||||
editPictureSrcRect(slide, pic.id, { l: 0.2, t: 0, r: 0.2, b: 0 })
|
||||
expect(replacePictureBytes(opened, slide, pic.id, GIF_1PX, 'gif')).toBe(true)
|
||||
expect(pic.srcRect).toBeUndefined()
|
||||
|
||||
const reopened = await openPptx(await savePptx(opened))
|
||||
const pic2 = reopened.deck.slides[0]!.elements.find(
|
||||
(e) => e.type === 'picture',
|
||||
) as PictureElement
|
||||
expect(pic2.srcRect).toBeUndefined()
|
||||
})
|
||||
|
||||
it('keeps stroke and z-order: the element is mutated, not re-added', async () => {
|
||||
const { opened, slide, pic } = await openWithPicture()
|
||||
pic.stroke = { fill: { type: 'solid', color: '#C43E1C' }, width: 12700 }
|
||||
pic.dirtyStroke = true
|
||||
addElement(slide, { kind: 'rect', offset: { ...OFF }, fillColor: '#112233' })
|
||||
const picIndex = slide.elements.indexOf(pic)
|
||||
expect(replacePictureBytes(opened, slide, pic.id, GIF_1PX, 'gif')).toBe(true)
|
||||
expect(slide.elements.indexOf(pic)).toBe(picIndex)
|
||||
|
||||
const reopened = await openPptx(await savePptx(opened))
|
||||
const els = reopened.deck.slides[0]!.elements
|
||||
expect(els.findIndex((e) => e.type === 'picture')).toBe(picIndex)
|
||||
const pic2 = els[picIndex] as PictureElement
|
||||
expect(pic2.stroke?.fill).toEqual({ type: 'solid', color: '#C43E1C' })
|
||||
})
|
||||
|
||||
it('drops a stale svgBlip extension and companion r:link', async () => {
|
||||
const { opened, slide, pic } = await openWithPicture()
|
||||
// Simulate an SVG picture inserted by Office 2016+: the blip carries a raster
|
||||
// fallback (r:embed), an external link (r:link), and the svgBlip extension
|
||||
const anchor = (pic as unknown as { anchor: { originalXml: string } }).anchor
|
||||
anchor.originalXml = anchor.originalXml.replace(
|
||||
/<a:blip r:embed="(rId\d+)"\s*\/>/,
|
||||
'<a:blip r:embed="$1" r:link="rId99">' +
|
||||
'<a:extLst><a:ext uri="{96DAC541-7B7A-43D3-8B79-37D633B846F1}">' +
|
||||
'<asvg:svgBlip xmlns:asvg="http://schemas.microsoft.com/office/drawing/2016/SVG/main" r:embed="$1"/>' +
|
||||
'</a:ext></a:extLst></a:blip>',
|
||||
)
|
||||
expect(anchor.originalXml).toContain('svgBlip')
|
||||
expect(replacePictureBytes(opened, slide, pic.id, GIF_1PX, 'gif')).toBe(true)
|
||||
expect(anchor.originalXml).not.toContain('svgBlip')
|
||||
expect(anchor.originalXml).not.toContain('r:link')
|
||||
expect(anchor.originalXml).toContain(`r:embed="rId`)
|
||||
expect(anchor.originalXml).not.toContain('<a:extLst></a:extLst>')
|
||||
})
|
||||
|
||||
it('unknown extension is rejected without touching the element', async () => {
|
||||
const { opened, slide, pic } = await openWithPicture()
|
||||
expect(replacePictureBytes(opened, slide, pic.id, GIF_1PX, 'exe')).toBe(false)
|
||||
expect(pic.mediaRef).toBe('ppt/media/image1.png')
|
||||
})
|
||||
})
|
||||
|
||||
describe('patchElementStroke', () => {
|
||||
it('stroke color+width round-trips through save → reopen', async () => {
|
||||
const opened = await openPptx(fx('01_standard_business.pptx'))
|
||||
@@ -76,6 +174,22 @@ describe('patchElementStroke', () => {
|
||||
expect(el2.stroke?.width).toBe(3 * 12700)
|
||||
})
|
||||
|
||||
it('picture border round-trips through save → reopen', async () => {
|
||||
const opened = await openPptx(await createBlankPptx())
|
||||
const slide = opened.deck.slides[0]!
|
||||
const pic = addPicture(opened, slide, { bytes: PNG_1PX, ext: 'png', offset: { ...OFF } })!
|
||||
pic.stroke = { fill: { type: 'solid', color: '#C43E1C' }, width: 2 * 12700, dash: 'sysDot' }
|
||||
pic.dirtyStroke = true
|
||||
|
||||
const reopened = await openPptx(await savePptx(opened))
|
||||
const pic2 = reopened.deck.slides[0]!.elements.find(
|
||||
(e) => e.type === 'picture',
|
||||
) as PictureElement
|
||||
expect(pic2.stroke?.fill).toEqual({ type: 'solid', color: '#C43E1C' })
|
||||
expect(pic2.stroke?.width).toBe(2 * 12700)
|
||||
expect(pic2.stroke?.dash).toBe('sysDot')
|
||||
})
|
||||
|
||||
it('null stroke writes explicit noFill, keeping other ln bytes', () => {
|
||||
const xml =
|
||||
'<p:sp><p:spPr><a:prstGeom prst="rect"><a:avLst/></a:prstGeom><a:ln w="12700"><a:solidFill><a:srgbClr val="000000"/></a:solidFill></a:ln></p:spPr></p:sp>'
|
||||
|
||||
Reference in New Issue
Block a user