From 32cb2064cdc3605d7e904e4dc711234ee3055bec 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: Mon, 6 Mar 2023 15:35:46 +0800 Subject: [PATCH] add more attrs --- structcanvas.go | 16 ++++++++++++ structeffects.go | 65 ++++++++++++++++++++++++++++++++++++++++++++++++ structpara.go | 18 ++++++++++++++ structrun.go | 21 ++++++++++++---- structshape.go | 14 +++++------ structtext.go | 15 ++++++++++- 6 files changed, 136 insertions(+), 13 deletions(-) diff --git a/structcanvas.go b/structcanvas.go index 8da1c83..7246847 100644 --- a/structcanvas.go +++ b/structcanvas.go @@ -70,6 +70,22 @@ func (c *WordprocessingCanvas) UnmarshalXML(d *xml.Decoder, start xml.StartEleme return err } c.Items = append(c.Items, &value) + case "pic": + var value Picture + err = d.DecodeElement(&value, &tt) + if err != nil && !strings.HasPrefix(err.Error(), "expected") { + return err + } + value.XMLPIC = getAtt(tt.Attr, "pic") + c.Items = append(c.Items, &value) + case "wgp": + var value WordprocessingGroup + value.file = c.file + err = d.DecodeElement(&value, &tt) + if err != nil && !strings.HasPrefix(err.Error(), "expected") { + return err + } + c.Items = append(c.Items, &value) default: err = d.Skip() // skip unsupported tags if err != nil { diff --git a/structeffects.go b/structeffects.go index a531de1..c372f48 100644 --- a/structeffects.go +++ b/structeffects.go @@ -271,3 +271,68 @@ func (r *NonVisualProperties) UnmarshalXML(d *xml.Decoder, start xml.StartElemen _, err = d.Token() return } + +// Spacing ... +type Spacing struct { + XMLName xml.Name `xml:"w:spacing,omitempty"` + + Line int `xml:"w:line,attr"` + LineRule string `xml:"w:lineRule,attr"` +} + +// UnmarshalXML ... +func (s *Spacing) UnmarshalXML(d *xml.Decoder, start xml.StartElement) (err error) { + for _, attr := range start.Attr { + switch attr.Name.Local { + case "line": + s.Line, err = strconv.Atoi(attr.Value) + if err != nil { + return + } + case "lineRule": + s.LineRule = attr.Value + default: + // ignore other attributes + } + } + // Consume the end element + _, err = d.Token() + return +} + +// Ind ... +type Ind struct { + XMLName xml.Name `xml:"w:ind,omitempty"` + + FirstLineChars int `xml:"w:firstLineChars,attr"` + FirstLine int `xml:"w:firstLine,attr"` +} + +// UnmarshalXML ... +func (i *Ind) UnmarshalXML(d *xml.Decoder, start xml.StartElement) (err error) { + for _, attr := range start.Attr { + switch attr.Name.Local { + case "firstLineChars": + if attr.Value == "" { + continue + } + i.FirstLineChars, err = strconv.Atoi(attr.Value) + if err != nil { + return + } + case "firstLine": + if attr.Value == "" { + continue + } + i.FirstLine, err = strconv.Atoi(attr.Value) + if err != nil { + return + } + default: + // ignore other attributes + } + } + // Consume the end element + _, err = d.Token() + return +} diff --git a/structpara.go b/structpara.go index b6939a6..9163a20 100644 --- a/structpara.go +++ b/structpara.go @@ -32,6 +32,8 @@ import ( // ParagraphProperties type ParagraphProperties struct { XMLName xml.Name `xml:"w:pPr,omitempty"` + Spacing *Spacing + Ind *Ind Justification *Justification Shade *Shade Kern *Kern @@ -57,6 +59,20 @@ func (p *ParagraphProperties) UnmarshalXML(d *xml.Decoder, start xml.StartElemen } if tt, ok := t.(xml.StartElement); ok { switch tt.Name.Local { + case "spacing": + var value Spacing + err = d.DecodeElement(&value, &tt) + if err != nil && !strings.HasPrefix(err.Error(), "expected") { + return err + } + p.Spacing = &value + case "ind": + var value Ind + err = d.DecodeElement(&value, &tt) + if err != nil && !strings.HasPrefix(err.Error(), "expected") { + return err + } + p.Ind = &value case "jc": p.Justification = &Justification{Val: getAtt(tt.Attr, "val")} case "shd": @@ -183,6 +199,8 @@ func (p *Paragraph) String() string { sb.WriteString(x.Text) case *Tab: sb.WriteByte('\t') + case *BarterRabbet: + sb.WriteByte('\n') case *Drawing: if x.Inline != nil && x.Inline.Graphic != nil && x.Inline.Graphic.GraphicData != nil { if x.Inline.Graphic.GraphicData.Pic != nil { diff --git a/structrun.go b/structrun.go index 614d324..68464b3 100644 --- a/structrun.go +++ b/structrun.go @@ -31,6 +31,8 @@ import ( // a piece of text in bold, or a link type Run struct { XMLName xml.Name `xml:"w:r,omitempty"` + Space string `xml:"xml:space,attr,omitempty"` + RsidR string `xml:"w:rsidR,attr,omitempty"` RsidRPr string `xml:"w:rsidRPr,attr,omitempty"` RunProperties *RunProperties `xml:"w:rPr,omitempty"` @@ -46,6 +48,10 @@ type Run struct { func (r *Run) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error { for _, attr := range start.Attr { switch attr.Name.Local { + case "space": + r.Space = attr.Value + case "rsidR": + r.RsidR = attr.Value case "rsidRPr": r.RsidRPr = attr.Value default: @@ -110,6 +116,8 @@ func (r *Run) parse(d *xml.Decoder, tt xml.StartElement) (child interface{}, err child = &value case "tab": child = &Tab{} + case "br": + child = &BarterRabbet{} case "AlternateContent": /*var value AlternateContent value.file = r.file @@ -174,7 +182,6 @@ type RunProperties struct { Bold *Bold ICs *struct{} `xml:"w:iCs,omitempty"` Italic *Italic - Underline *Underline Highlight *Highlight Color *Color Size *Size @@ -183,6 +190,7 @@ type RunProperties struct { Style *Style Shade *Shade Kern *Kern + Underline *Underline VertAlign *VertAlign } @@ -277,10 +285,11 @@ func (r *RunProperties) UnmarshalXML(d *xml.Decoder, start xml.StartElement) err // RunFonts specifies the fonts used in the text of a run. type RunFonts struct { - XMLName xml.Name `xml:"w:rFonts,omitempty"` - ASCII string `xml:"w:ascii,attr,omitempty"` - HAnsi string `xml:"w:hAnsi,attr,omitempty"` - Hint string `xml:"w:hint,attr,omitempty"` + XMLName xml.Name `xml:"w:rFonts,omitempty"` + ASCII string `xml:"w:ascii,attr,omitempty"` + EastAsia string `xml:"w:eastAsia,attr,omitempty"` + HAnsi string `xml:"w:hAnsi,attr,omitempty"` + Hint string `xml:"w:hint,attr,omitempty"` } // UnmarshalXML ... @@ -289,6 +298,8 @@ func (f *RunFonts) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error { switch attr.Name.Local { case "ascii": f.ASCII = attr.Value + case "eastAsia": + f.EastAsia = attr.Value case "hAnsi": f.HAnsi = attr.Value case "hint": diff --git a/structshape.go b/structshape.go index 388b73d..a53bdd2 100644 --- a/structshape.go +++ b/structshape.go @@ -621,16 +621,16 @@ func (c *WTextBoxContent) UnmarshalXML(d *xml.Decoder, start xml.StartElement) e // WPSBodyPr represents the body properties for a WordprocessingML DrawingML shape. type WPSBodyPr struct { XMLName xml.Name `xml:"wps:bodyPr,omitempty"` - Rot int `xml:"rot,attr,omitempty"` + Rot int `xml:"rot,attr"` Vert string `xml:"vert,attr,omitempty"` Wrap string `xml:"wrap,attr,omitempty"` - LIns int64 `xml:"lIns,attr,omitempty"` - TIns int64 `xml:"tIns,attr,omitempty"` - RIns int64 `xml:"rIns,attr,omitempty"` - BIns int64 `xml:"bIns,attr,omitempty"` + LIns int64 `xml:"lIns,attr"` + TIns int64 `xml:"tIns,attr"` + RIns int64 `xml:"rIns,attr"` + BIns int64 `xml:"bIns,attr"` Anchor string `xml:"anchor,attr,omitempty"` - AnchorCtr int `xml:"anchorCtr,attr,omitempty"` - Upright int `xml:"upright,attr,omitempty"` + AnchorCtr int `xml:"anchorCtr,attr"` + Upright int `xml:"upright,attr"` NoAutofit *struct{} `xml:"a:noAutofit,omitempty"` } diff --git a/structtext.go b/structtext.go index 7d8b35f..5722d5b 100644 --- a/structtext.go +++ b/structtext.go @@ -30,17 +30,30 @@ type Tab struct { XMLName xml.Name `xml:"w:tab,omitempty"` } +// BarterRabbet is
+type BarterRabbet struct { + XMLName xml.Name `xml:"w:br,omitempty"` +} + // Text object contains the actual text type Text struct { XMLName xml.Name `xml:"w:t,omitempty"` - // XMLSpace string `xml:"xml:space,attr,omitempty"` + XMLSpace string `xml:"xml:space,attr,omitempty"` Text string `xml:",chardata"` } // UnmarshalXML ... func (r *Text) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error { + for _, attr := range start.Attr { + switch attr.Name.Local { + case "space": + r.XMLSpace = attr.Value + default: + // ignore other attributes + } + } for { t, err := d.Token() if err == io.EOF {