From f07b5dd9143585fc39ae1ef834e82c3c05a1bcac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=BA=90=E6=96=87=E9=9B=A8?= <41315874+fumiama@users.noreply.github.com> Date: Sat, 18 Feb 2023 22:42:37 +0800 Subject: [PATCH] finish drawing unmarshalling --- structdoc_test.go | 4 ++ structdrawing.go | 99 +++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 100 insertions(+), 3 deletions(-) diff --git a/structdoc_test.go b/structdoc_test.go index 2d13029..c45d153 100644 --- a/structdoc_test.go +++ b/structdoc_test.go @@ -568,7 +568,11 @@ func TestUnmarshalDrawingStructure(t *testing.T) { } if child.Run.Drawing.Inline.Graphic != nil && child.Run.Drawing.Inline.Graphic.GraphicData != nil { t.Log(child.Run.Drawing.Inline.Graphic.GraphicData.URI) + if child.Run.Drawing.Inline.Graphic.GraphicData.Pic != nil { + t.Log(child.Run.Drawing.Inline.Graphic.GraphicData.Pic.NonVisualPicProperties.NonVisualDrawingProperties.ID, child.Run.Drawing.Inline.Graphic.GraphicData.Pic.BlipFill.Blip.Embed) + } } + } } } diff --git a/structdrawing.go b/structdrawing.go index 005b220..c1f6f6a 100644 --- a/structdrawing.go +++ b/structdrawing.go @@ -155,14 +155,65 @@ func (a *AGraphic) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error { type AGraphicData struct { XMLName xml.Name `xml:"http://schemas.openxmlformats.org/drawingml/2006/main graphicData,omitempty"` URI string `xml:"uri,attr"` - Pic PICPic + Pic *PICPic +} + +func (a *AGraphicData) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error { + for { + t, err := d.Token() + if err == io.EOF { + break + } + + switch tt := t.(type) { + case xml.StartElement: + switch tt.Name.Local { + case "pic": + var value PICPic + d.DecodeElement(&value, &start) + a.Pic = &value + default: + continue + } + } + + } + return nil } // PICPic represents a picture in a Word document. type PICPic struct { XMLName xml.Name `xml:"http://schemas.openxmlformats.org/drawingml/2006/picture pic,omitempty"` - NonVisualPicProperties PICNonVisualPicProperties - BlipFill PICBlipFill + NonVisualPicProperties *PICNonVisualPicProperties + BlipFill *PICBlipFill + // is unecessary +} + +func (p *PICPic) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error { + for { + t, err := d.Token() + if err == io.EOF { + break + } + + switch tt := t.(type) { + case xml.StartElement: + switch tt.Name.Local { + case "nvPicPr": + var value PICNonVisualPicProperties + d.DecodeElement(&value, &start) + p.NonVisualPicProperties = &value + case "blipFill": + var value PICBlipFill + d.DecodeElement(&value, &start) + p.BlipFill = &value + default: + continue + } + } + + } + return nil } // PICNonVisualPicProperties represents the non-visual properties of a picture in a Word document. @@ -171,6 +222,27 @@ type PICNonVisualPicProperties struct { NonVisualDrawingProperties PICNonVisualDrawingProperties } +func (p *PICNonVisualPicProperties) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error { + for { + t, err := d.Token() + if err == io.EOF { + break + } + + switch tt := t.(type) { + case xml.StartElement: + switch tt.Name.Local { + case "cNvPr": + p.NonVisualDrawingProperties.ID = getAtt(tt.Attr, "id") + default: + continue + } + } + + } + return nil +} + // PICNonVisualDrawingProperties represents the non-visual drawing properties of a picture in a Word document. type PICNonVisualDrawingProperties struct { XMLName xml.Name `xml:"http://schemas.openxmlformats.org/drawingml/2006/picture cNvPr,omitempty"` @@ -183,6 +255,27 @@ type PICBlipFill struct { Blip ABlip } +func (p *PICBlipFill) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error { + for { + t, err := d.Token() + if err == io.EOF { + break + } + + switch tt := t.(type) { + case xml.StartElement: + switch tt.Name.Local { + case "blip": + p.Blip.Embed = getAtt(tt.Attr, "embed") + default: + continue + } + } + + } + return nil +} + // ABlip represents the blip of a picture in a Word document. type ABlip struct { XMLName xml.Name `xml:"http://schemas.openxmlformats.org/drawingml/2006/main blip,omitempty"`