diff --git a/apirun.go b/apirun.go index 9843003..2b8e1d1 100644 --- a/apirun.go +++ b/apirun.go @@ -46,8 +46,32 @@ func (r *Run) Shade(val, color, fill string) *Run { return r } -// AddTab add a tab in front of the run -func (r *Run) AddTab() *Run { - r.Children = append(r.Children, &WTab{}) +// Bold ... +func (r *Run) Bold() *Run { + r.RunProperties.Bold = &Bold{} + return r +} + +// Italic ... +func (r *Run) Italic() *Run { + r.RunProperties.Italic = &Italic{} + return r +} + +// Underline ... +func (r *Run) Underline(val string) *Run { + r.RunProperties.Underline = &Underline{Val: val} + return r +} + +// Highlight ... +func (r *Run) Highlight(val string) *Run { + r.RunProperties.Highlight = &Highlight{Val: val} + return r +} + +// AddTab add a tab in front of the run +func (r *Run) AddTab() *Run { + r.Children = append(r.Children, &Tab{}) return r } diff --git a/apitext.go b/apitext.go index 975615a..3baef09 100644 --- a/apitext.go +++ b/apitext.go @@ -25,7 +25,7 @@ import "strings" // AddTab adds tab to para func (p *Paragraph) AddTab() *Run { c := make([]interface{}, 1, 64) - c[0] = &WTab{} + c[0] = &Tab{} run := &Run{ RunProperties: &RunProperties{}, @@ -47,7 +47,7 @@ func (p *Paragraph) AddText(text string) *Run { for i, s := range strings.Split(text, "\t") { if i > 0 { - c = append(c, &WTab{}) + c = append(c, &Tab{}) } if s != "" { c = append(c, &Text{ diff --git a/cmd/main/main.go b/cmd/main/main.go index f165c18..e7a8583 100644 --- a/cmd/main/main.go +++ b/cmd/main/main.go @@ -52,9 +52,13 @@ func main() { r.Children[0].(*docx.Drawing).Anchor.Graphic.GraphicData.Pic.BlipFill.Blip.AlphaModFix = &docx.AAlphaModFix{Amount: 50000} // add text para1.AddText("test").AddTab() - para1.AddText("test font size").Size("44").AddTab() - para1.AddText("test color").Color("808080").AddTab() - para1.AddText("test shade").Shade("clear", "auto", "E7E6E6").AddTab() + para1.AddText("size").Size("44").AddTab() + para1.AddText("color").Color("808080").AddTab() + para1.AddText("shade").Shade("clear", "auto", "E7E6E6").AddTab() + para1.AddText("bold").Bold().AddTab() + para1.AddText("italic").Italic().AddTab() + para1.AddText("underline").Underline("double").AddTab() + para1.AddText("highlight").Highlight("yellow").AddTab() para2 := w.AddParagraph().Justification("end") para2.AddText("test font size and color").Size("44").Color("ff0000") diff --git a/structdrawing_test.go b/structdrawing_test.go index 074a7cf..b004f62 100644 --- a/structdrawing_test.go +++ b/structdrawing_test.go @@ -33,7 +33,7 @@ func TestDrawingStructure(t *testing.T) { // add new paragraph para1 := w.AddParagraph() // add text - para1.AddText("直接粘贴 inline").Shade("clear", "auto", "E7E6E6").AddTab() + para1.AddText("直接粘贴 inline").Shade("clear", "auto", "E7E6E6").AddTab().Bold().Underline("single").Highlight("yellow").Italic() r, err := para1.AddAnchorDrawingFrom("testdata/fumiama.JPG") if err != nil { t.Fatal(err) diff --git a/structeffects.go b/structeffects.go index 6734e83..4689d85 100644 --- a/structeffects.go +++ b/structeffects.go @@ -27,6 +27,28 @@ type Size struct { Val string `xml:"w:val,attr"` } +// Bold ... +type Bold struct { + XMLName xml.Name `xml:"w:b,omitempty"` +} + +// Italic ... +type Italic struct { + XMLName xml.Name `xml:"w:i,omitempty"` +} + +// Underline ... +type Underline struct { + XMLName xml.Name `xml:"w:u,omitempty"` + Val string `xml:"w:val,attr,omitempty"` +} + +// Highlight ... +type Highlight struct { + XMLName xml.Name `xml:"w:highlight,omitempty"` + Val string `xml:"w:val,attr,omitempty"` +} + // Justification contains the way of the horizonal alignment // // w:jc 属性的取值可以是以下之一: diff --git a/structpara.go b/structpara.go index b43e6a7..f44232c 100644 --- a/structpara.go +++ b/structpara.go @@ -105,7 +105,7 @@ func (p *Paragraph) String() string { switch x := c.(type) { case *Text: sb.WriteString(x.Text) - case *WTab: + case *Tab: sb.WriteByte('\t') case *Drawing: if x.Inline != nil && x.Inline.Graphic != nil && x.Inline.Graphic.GraphicData != nil && x.Inline.Graphic.GraphicData.Pic != nil { diff --git a/structrun.go b/structrun.go index 78ad7bc..1dcb912 100644 --- a/structrun.go +++ b/structrun.go @@ -84,7 +84,7 @@ func (r *Run) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error { } child = &value case "tab": - child = &WTab{} + child = &Tab{} default: err = d.Skip() // skip unsupported tags if err != nil { @@ -99,19 +99,18 @@ func (r *Run) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error { return nil } -// WTab is the literal tab -type WTab struct { - XMLName xml.Name `xml:"w:tab,omitempty"` -} - // RunProperties encapsulates visual properties of a run type RunProperties struct { - XMLName xml.Name `xml:"w:rPr,omitempty"` - Color *Color - Size *Size - RunStyle *RunStyle - Style *Style - Shade *Shade + XMLName xml.Name `xml:"w:rPr,omitempty"` + Bold *Bold + Italic *Italic + Underline *Underline + Highlight *Highlight + Color *Color + Size *Size + RunStyle *RunStyle + Style *Style + Shade *Shade } // UnmarshalXML ... @@ -127,6 +126,18 @@ func (r *RunProperties) UnmarshalXML(d *xml.Decoder, start xml.StartElement) err if tt, ok := t.(xml.StartElement); ok { switch tt.Name.Local { + case "b": + r.Bold = &Bold{} + case "i": + r.Italic = &Italic{} + case "u": + var value Underline + value.Val = getAtt(tt.Attr, "val") + r.Underline = &value + case "highlight": + var value Highlight + value.Val = getAtt(tt.Attr, "val") + r.Highlight = &value case "color": var value Color value.Val = getAtt(tt.Attr, "val") diff --git a/structtext.go b/structtext.go index 5478620..7d8b35f 100644 --- a/structtext.go +++ b/structtext.go @@ -25,6 +25,11 @@ import ( "io" ) +// Tab is the literal tab +type Tab struct { + XMLName xml.Name `xml:"w:tab,omitempty"` +} + // Text object contains the actual text type Text struct { XMLName xml.Name `xml:"w:t,omitempty"`