diff --git a/d2cli/main.go b/d2cli/main.go index 91a1e3181..4e377bdef 100644 --- a/d2cli/main.go +++ b/d2cli/main.go @@ -795,7 +795,7 @@ func renderPPTX(ctx context.Context, ms *xmain.State, presentation *ppt.Presenta return err } - err = presentation.AddSlide(pngImg) + err = presentation.AddSlide(strings.Join(boardPath, " / "), pngImg) if err != nil { return err } diff --git a/lib/ppt/pptx.go b/lib/ppt/pptx.go index 04f30b03e..f6d13b403 100644 --- a/lib/ppt/pptx.go +++ b/lib/ppt/pptx.go @@ -50,6 +50,10 @@ func addFile(zipFile *zip.Writer, filePath, content string) error { // https://startbigthinksmall.wordpress.com/2010/01/04/points-inches-and-emus-measuring-units-in-office-open-xml/ const SLIDE_WIDTH = 9144000 const SLIDE_HEIGHT = 5143500 +const HEADER_HEIGHT = 392471 + +const IMAGE_WIDTH = SLIDE_WIDTH +const IMAGE_HEIGHT = SLIDE_HEIGHT - HEADER_HEIGHT const RELS_SLIDE_XML = `` @@ -57,10 +61,11 @@ func getRelsSlideXml(imageId string) string { return fmt.Sprintf(RELS_SLIDE_XML, imageId, imageId) } -const SLIDE_XML = `` +const SLIDE_XML = `%s` -func getSlideXml(imageId, imageName string, top, left, width, height int) string { - return fmt.Sprintf(SLIDE_XML, imageName, imageName, imageId, left, top, width, height) +func getSlideXml(slideTitle, imageId string, top, left, width, height int) string { + top += HEADER_HEIGHT + return fmt.Sprintf(SLIDE_XML, slideTitle, slideTitle, imageId, left, top, width, height, slideTitle, HEADER_HEIGHT, slideTitle) } func getPresentationXmlRels(slideFileNames []string) string { diff --git a/lib/ppt/presentation.go b/lib/ppt/presentation.go index e1a9dcd86..aee9e7b63 100644 --- a/lib/ppt/presentation.go +++ b/lib/ppt/presentation.go @@ -17,18 +17,19 @@ type Presentation struct { } type Slide struct { - Image []byte - Width int - Height int - Top int - Left int + Title string + Image []byte + ImageWidth int + ImageHeight int + ImageTop int + ImageLeft int } func NewPresentation() *Presentation { return &Presentation{} } -func (p *Presentation) AddSlide(pngContent []byte) error { +func (p *Presentation) AddSlide(title string, pngContent []byte) error { src, err := png.Decode(bytes.NewReader(pngContent)) if err != nil { return fmt.Errorf("error decoding PNG image: %v", err) @@ -39,23 +40,24 @@ func (p *Presentation) AddSlide(pngContent []byte) error { // compute the size and position to fit the slide if srcSize.X > srcSize.Y { - width = SLIDE_WIDTH + width = IMAGE_WIDTH height = int(float64(width) * (float64(srcSize.X) / float64(srcSize.Y))) left = 0 - top = (SLIDE_HEIGHT - height) / 2 + top = (IMAGE_HEIGHT - height) / 2 } else { - height = SLIDE_HEIGHT + height = IMAGE_HEIGHT width = int(float64(height) * (float64(srcSize.X) / float64(srcSize.Y))) top = 0 - left = (SLIDE_WIDTH - width) / 2 + left = (IMAGE_WIDTH - width) / 2 } p.Slides = append(p.Slides, &Slide{ - Image: pngContent, - Width: width, - Height: height, - Top: top, - Left: left, + Title: title, + Image: pngContent, + ImageWidth: width, + ImageHeight: height, + ImageTop: top, + ImageLeft: left, }) return nil @@ -93,7 +95,11 @@ func (p *Presentation) SaveTo(filePath string) error { } // TODO: center the image? - err = addFile(zipFile, fmt.Sprintf("ppt/slides/%s.xml", slideFileName), getSlideXml(imageId, imageId, slide.Top, slide.Left, slide.Width, slide.Height)) + err = addFile( + zipFile, + fmt.Sprintf("ppt/slides/%s.xml", slideFileName), + getSlideXml(slide.Title, imageId, slide.ImageTop, slide.ImageLeft, slide.ImageWidth, slide.ImageHeight), + ) if err != nil { return err }