From 60489440e7dd0e642b2c252b679834e3bca204f5 Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Wed, 10 Sep 2025 14:37:40 +0800 Subject: [PATCH 1/2] feat: add alt text support --- docx/pic.go | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/docx/pic.go b/docx/pic.go index dd88b59..bad0e4e 100644 --- a/docx/pic.go +++ b/docx/pic.go @@ -3,6 +3,7 @@ package docx import ( "github.com/gomutex/godocx/common/units" "github.com/gomutex/godocx/dml" + "github.com/gomutex/godocx/dml/dmlpic" ) type PicMeta struct { @@ -39,3 +40,34 @@ func (rd *RootDoc) AddPicture(path string, width units.Inch, height units.Inch) return p.AddPicture(path, width, height) } + +// SetAltText sets the alternative text (alt text) for the picture. +// +// Alt text is used to provide a textual description of the image for accessibility purposes. +// It is important for users who rely on screen readers or have images disabled in their browsers. +// +// Example usage: +// +// // Set alternative text for the picture +// picMeta.SetAltText("Gopher", "A cute gopher mascot") +// +// Parameters: +// - title: The title of the image, which serves as a brief description. +// - desc: The description of the image, providing more detailed information. +func (pm *PicMeta) SetAltText(title, desc string) { + if pm.Inline == nil { + return + } + + pm.Inline.DocProp.Name = title + pm.Inline.DocProp.Description = desc + + if pm.Inline.Graphic.Data == nil { + pm.Inline.Graphic.Data = &dml.GraphicData{} + } + if pm.Inline.Graphic.Data.Pic == nil { + pm.Inline.Graphic.Data.Pic = &dmlpic.Pic{} + } + pm.Inline.Graphic.Data.Pic.NonVisualPicProp.CNvPr.Name = title + pm.Inline.Graphic.Data.Pic.NonVisualPicProp.CNvPr.Description = desc +} From 19f5399428070fbe2cbd5487db80175eb7f06643 Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Wed, 10 Sep 2025 20:30:46 +0800 Subject: [PATCH 2/2] fix: update pointer usage in Drawing and Inline types --- dml/drawing.go | 11 ++++++----- dml/inline.go | 4 ++-- docx/paragraph.go | 3 +-- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/dml/drawing.go b/dml/drawing.go index 5ce1bdc..ff561f3 100644 --- a/dml/drawing.go +++ b/dml/drawing.go @@ -14,7 +14,7 @@ const ( ) type Drawing struct { - Inline []Inline `xml:"inline,omitempty"` + Inline []*Inline `xml:"inline,omitempty"` Anchor []*Anchor `xml:"anchor,omitempty"` } @@ -34,20 +34,21 @@ loop: if err = d.DecodeElement(ar, &elem); err != nil { return err } - dr.Anchor = append(dr.Anchor, ar) + case xml.Name{Space: constants.WMLDrawingNS, Local: "inline"}: - il := Inline{} - if err = d.DecodeElement(&il, &elem); err != nil { + il := &Inline{} + if err = d.DecodeElement(il, &elem); err != nil { return err } - dr.Inline = append(dr.Inline, il) + default: if err = d.Skip(); err != nil { return err } } + case xml.EndElement: break loop } diff --git a/dml/inline.go b/dml/inline.go index 308443b..c763938 100644 --- a/dml/inline.go +++ b/dml/inline.go @@ -50,8 +50,8 @@ type Inline struct { Graphic Graphic `xml:"graphic,omitempty"` } -func NewInline(extent dmlct.PSize2D, docProp DocProp, graphic Graphic) Inline { - return Inline{ +func NewInline(extent dmlct.PSize2D, docProp DocProp, graphic Graphic) *Inline { + return &Inline{ Extent: extent, DocProp: docProp, Graphic: graphic, diff --git a/docx/paragraph.go b/docx/paragraph.go index b4bc58b..6c7fa52 100644 --- a/docx/paragraph.go +++ b/docx/paragraph.go @@ -311,11 +311,10 @@ func (p *Paragraph) addDrawing(rID string, imgCount uint, width units.Inch, heig p.ct.Children = append(p.ct.Children, ctypes.ParagraphChild{Run: run}) - return &inline + return inline } func (p *Paragraph) AddPicture(path string, width units.Inch, height units.Inch) (*PicMeta, error) { - imgBytes, err := internal.FileToByte(path) if err != nil { return nil, err