Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restructured #9

Merged
merged 8 commits into from
Jun 15, 2024
Merged
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
96 changes: 63 additions & 33 deletions ooxml/dml/anchor.go → dml/anchor.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ package dml
import (
"encoding/xml"
"fmt"

"github.com/gomutex/godocx/common/constants"
"github.com/gomutex/godocx/ooxml/types"
)

type Anchor struct {
Expand All @@ -30,17 +27,15 @@ type Anchor struct {
LockedAttr int

// Child elements:
SimplePos *types.PositionType
PositionH *types.PoistionH
PositionV *types.PoistionV
SimplePos *PositionType
PositionH *PoistionH
PositionV *PoistionV
Graphic *Graphic
Extent *Extent
DocProp *DocProp
cNvGraphicFramePr *NonVisualGraphicFrameProp
// TODO:
// EffectExtent
//
// implement any other
EffectExtent *EffectExtent
WrapNone *WrapNone `xml:"wrapNone"`
}

func NewAnchor() *Anchor {
Expand All @@ -50,8 +45,8 @@ func NewAnchor() *Anchor {
func (a *Anchor) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
start.Name.Local = "wp:anchor"
start.Attr = []xml.Attr{
{Name: xml.Name{Local: "xmlns:a"}, Value: constants.DrawingMLMainNS},
{Name: xml.Name{Local: "xmlns:pic"}, Value: constants.DrawingMLPicNS},
// {Name: xml.Name{Local: "xmlns:a"}, Value: constants.DrawingMLMainNS},
// {Name: xml.Name{Local: "xmlns:pic"}, Value: constants.DrawingMLPicNS},
}

start.Attr = append(start.Attr, xml.Attr{Name: xml.Name{Local: "behindDoc"}, Value: fmt.Sprintf("%d", a.BehindDocAttr)})
Expand All @@ -70,37 +65,59 @@ func (a *Anchor) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
return err
}

// Placement(the order) of these elements is important especially the wrapNone

if a.SimplePos != nil {
if err := e.EncodeElement(a.SimplePos, xml.StartElement{Name: xml.Name{Local: "wp:simplePos"}}); err != nil {
return err
}
}

if a.PositionH != nil {
if err := e.EncodeElement(a.PositionH, xml.StartElement{Name: xml.Name{Local: "wp:positionH"}}); err != nil {
return err
}
}

if a.PositionV != nil {
if err := e.EncodeElement(a.PositionV, xml.StartElement{Name: xml.Name{Local: "wp:positionV"}}); err != nil {
return err
}
}

if a.Extent != nil {
if err := e.EncodeElement(a.Extent, xml.StartElement{Name: xml.Name{Local: "wp:extent"}}); err != nil {
return err
}
}

if a.DocProp != nil {
if err := e.EncodeElement(a.DocProp, xml.StartElement{Name: xml.Name{Local: "wp:docPr"}}); err != nil {
if a.EffectExtent != nil {
if err := e.EncodeElement(a.EffectExtent, xml.StartElement{Name: xml.Name{Local: "wp:effectExtent"}}); err != nil {
return err
}
}
if a.cNvGraphicFramePr != nil {
if err := e.EncodeElement(a.cNvGraphicFramePr, xml.StartElement{Name: xml.Name{Local: "wp:cNvGraphicFramePr"}}); err != nil {

// if a.WrapNone.Valid && a.WrapNone.Bool {
if a.WrapNone != nil {
err := e.EncodeElement(a.WrapNone, xml.StartElement{Name: xml.Name{Local: "wp:wrapNone"}})
if err != nil {
return err
}
}

if a.SimplePos != nil {
if err := e.EncodeElement(a.SimplePos, xml.StartElement{Name: xml.Name{Local: "wp:simplePos"}}); err != nil {
if a.DocProp != nil {
if err := e.EncodeElement(a.DocProp, xml.StartElement{Name: xml.Name{Local: "wp:docPr"}}); err != nil {
return err
}
}

if a.PositionH != nil {
if err := e.EncodeElement(a.PositionH, xml.StartElement{Name: xml.Name{Local: "wp:positionH"}}); err != nil {
if a.cNvGraphicFramePr != nil {
if err := e.EncodeElement(a.cNvGraphicFramePr, xml.StartElement{Name: xml.Name{Local: "wp:cNvGraphicFramePr"}}); err != nil {
return err
}
}

if a.PositionV != nil {
if err := e.EncodeElement(a.PositionV, xml.StartElement{Name: xml.Name{Local: "wp:positionV"}}); err != nil {
if a.Graphic != nil {
if err := e.EncodeElement(a.Graphic, xml.StartElement{Name: xml.Name{Local: "a:graphic"}}); err != nil {
return err
}
}
Expand Down Expand Up @@ -147,35 +164,48 @@ func (a *Anchor) UnmarshalXML(decoder *xml.Decoder, start xml.StartElement) erro
a.Extent = &Extent{
XMLName: "wp:extent",
}
if err := decoder.DecodeElement(a.Extent, &elem); err != nil {
if err = decoder.DecodeElement(a.Extent, &elem); err != nil {
return err
}
case "effectExtent":
a.EffectExtent = &EffectExtent{
XMLName: "wp:effectExtent",
}
if err = decoder.DecodeElement(a.EffectExtent, &elem); err != nil {
return err
}

case "cNvGraphicFramePr":
a.cNvGraphicFramePr = &NonVisualGraphicFrameProp{}
if err := decoder.DecodeElement(a.cNvGraphicFramePr, &elem); err != nil {
if err = decoder.DecodeElement(a.cNvGraphicFramePr, &elem); err != nil {
return err
}
case "docPr":
a.DocProp = &DocProp{}
if err := decoder.DecodeElement(a.DocProp, &elem); err != nil {
if err = decoder.DecodeElement(a.DocProp, &elem); err != nil {
return err
}
case "simplePos":
a.SimplePos = &types.PositionType{}
if err := decoder.DecodeElement(a.SimplePos, &elem); err != nil {
a.SimplePos = &PositionType{}
if err = decoder.DecodeElement(a.SimplePos, &elem); err != nil {
return err
}
case "positionV":
a.PositionV = &types.PoistionV{}
if err := decoder.DecodeElement(a.PositionV, &elem); err != nil {
a.PositionV = &PoistionV{}
if err = decoder.DecodeElement(a.PositionV, &elem); err != nil {
return err
}
case "positionH":
a.PositionH = &types.PoistionH{}
if err := decoder.DecodeElement(a.PositionH, &elem); err != nil {
a.PositionH = &PoistionH{}
if err = decoder.DecodeElement(a.PositionH, &elem); err != nil {
return err
}
case "graphic":
a.Graphic = &Graphic{}
if err = decoder.DecodeElement(a.Graphic, &elem); err != nil {
return err
}
case "wrapNone":
a.WrapNone = &WrapNone{}
default:
if err = decoder.Skip(); err != nil {
return err
Expand Down
93 changes: 93 additions & 0 deletions dml/avList.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package dml

import (
"encoding/xml"
)

type ShapeGuide struct {
Name string
Formula string
}

func (s *ShapeGuide) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
start.Name.Local = "a:gd"
start.Attr = []xml.Attr{
{Name: xml.Name{Local: "name"}, Value: s.Name},
{Name: xml.Name{Local: "fmla"}, Value: s.Formula},
}

err := e.EncodeToken(start)
if err != nil {
return err
}

return e.EncodeToken(xml.EndElement{Name: start.Name})
}

func (s *ShapeGuide) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
for _, a := range start.Attr {
switch a.Name.Local {
case "name":
s.Name = a.Value
case "fmla":
s.Formula = a.Value
}
}

return nil
}

// List of Shape Adjust Values
type AdjustValues struct {
ShapeGuides []ShapeGuide
}

func (a *AdjustValues) MarshalXML(e *xml.Encoder, start xml.StartElement) (err error) {
start.Name.Local = "a:avLst"

err = e.EncodeToken(start)
if err != nil {
return err
}

for _, data := range a.ShapeGuides {
err := data.MarshalXML(e, start)
if err != nil {
return err
}
}

return e.EncodeToken(xml.EndElement{Name: start.Name})
}

func (a *AdjustValues) UnmarshalXML(d *xml.Decoder, start xml.StartElement) (err error) {
loop:
for {
currentToken, err := d.Token()
if err != nil {
return err
}

switch elem := currentToken.(type) {
case xml.StartElement:
switch elem.Name.Local {
case "t":
gd := ShapeGuide{}
if err = d.DecodeElement(&gd, &elem); err != nil {
return err
}

a.ShapeGuides = append(a.ShapeGuides, gd)

default:
if err = d.Skip(); err != nil {
return err
}
}
case xml.EndElement:
break loop
}
}

return nil
}
55 changes: 55 additions & 0 deletions dml/cNvGraphicFramePr.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package dml

import (
"encoding/xml"
)

type NonVisualGraphicFrameProp struct {
GraphicFrameLocks *GraphicFrameLocks
}

func (n *NonVisualGraphicFrameProp) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
start.Name.Local = "wp:cNvGraphicFramePr"
start.Attr = []xml.Attr{}

err := e.EncodeToken(start)
if err != nil {
return err
}

if n.GraphicFrameLocks != nil {
if err := e.EncodeElement(n.GraphicFrameLocks, xml.StartElement{Name: xml.Name{Local: "a:graphicFrameLocks"}}); err != nil {
return err
}
}

return e.EncodeToken(xml.EndElement{Name: start.Name})
}

func (n *NonVisualGraphicFrameProp) UnmarshalXML(decoder *xml.Decoder, start xml.StartElement) error {
for {
token, err := decoder.Token()
if err != nil {
return err
}

switch elem := token.(type) {
case xml.StartElement:
switch elem.Name.Local {
case "graphicFrameLocks":
n.GraphicFrameLocks = &GraphicFrameLocks{}
if err = decoder.DecodeElement(n.GraphicFrameLocks, &elem); err != nil {
return err
}
default:
if err = decoder.Skip(); err != nil {
return err
}
}
case xml.EndElement:
if elem == start.End() {
return nil
}
}
}
}
Loading
Loading