Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions dml/drawing.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const (
)

type Drawing struct {
Inline []Inline `xml:"inline,omitempty"`
Inline []*Inline `xml:"inline,omitempty"`
Anchor []*Anchor `xml:"anchor,omitempty"`
}

Expand All @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions dml/inline.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 1 addition & 2 deletions docx/paragraph.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
32 changes: 32 additions & 0 deletions docx/pic.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}