From e57b2b9acea7abcbb00f525d99c285ce5765be63 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: Sun, 26 Feb 2023 19:53:36 +0800 Subject: [PATCH] add font to run --- apirun.go | 25 ++++++++++++++++++++++++- cmd/main/main.go | 7 ++++++- structdrawing_test.go | 6 +++++- structrun.go | 33 +++++++++++++++++++++++++++++++++ 4 files changed, 68 insertions(+), 3 deletions(-) diff --git a/apirun.go b/apirun.go index 2b8e1d1..69e4aa6 100644 --- a/apirun.go +++ b/apirun.go @@ -58,7 +58,20 @@ func (r *Run) Italic() *Run { return r } -// Underline ... +// Underline has several possible values including +// +// none: Specifies that no underline should be applied. +// single: Specifies a single underline. +// words: Specifies that only words within the text should be underlined. +// double: Specifies a double underline. +// thick: Specifies a thick underline. +// dotted: Specifies a dotted underline. +// dash: Specifies a dash underline. +// dotDash: Specifies an alternating dot-dash underline. +// dotDotDash: Specifies an alternating dot-dot-dash underline. +// wave: Specifies a wavy underline. +// dashLong: Specifies a long dash underline. +// wavyDouble: Specifies a double wavy underline. func (r *Run) Underline(val string) *Run { r.RunProperties.Underline = &Underline{Val: val} return r @@ -75,3 +88,13 @@ func (r *Run) AddTab() *Run { r.Children = append(r.Children, &Tab{}) return r } + +// Font sets the font of the run +func (r *Run) Font(ascii, hansi, hint string) *Run { + r.RunProperties.Fonts = &RunFonts{ + Ascii: ascii, + HAnsi: hansi, + Hint: hint, + } + return r +} diff --git a/cmd/main/main.go b/cmd/main/main.go index e7a8583..503c4b6 100644 --- a/cmd/main/main.go +++ b/cmd/main/main.go @@ -59,9 +59,14 @@ func main() { para1.AddText("italic").Italic().AddTab() para1.AddText("underline").Underline("double").AddTab() para1.AddText("highlight").Highlight("yellow").AddTab() + para1.AddText("font").Font("Consolas", "", "cs").AddTab() para2 := w.AddParagraph().Justification("end") - para2.AddText("test font size and color").Size("44").Color("ff0000") + para2.AddText("test all font attrs"). + Size("44").Color("ff0000").Font("Consolas", "", "cs"). + Shade("clear", "auto", "E7E6E6"). + Bold().Italic().Underline("wave"). + Highlight("yellow") nextPara := w.AddParagraph() nextPara.AddLink("google", `http://google.com`) diff --git a/structdrawing_test.go b/structdrawing_test.go index b004f62..a2d82f8 100644 --- a/structdrawing_test.go +++ b/structdrawing_test.go @@ -33,7 +33,11 @@ func TestDrawingStructure(t *testing.T) { // add new paragraph para1 := w.AddParagraph() // add text - para1.AddText("直接粘贴 inline").Shade("clear", "auto", "E7E6E6").AddTab().Bold().Underline("single").Highlight("yellow").Italic() + para1.AddText("直接粘贴 inline"). + Shade("clear", "auto", "E7E6E6"). + AddTab().Bold().Underline("single"). + Highlight("yellow").Italic(). + Font("宋体", "宋体", "eastAsia") r, err := para1.AddAnchorDrawingFrom("testdata/fumiama.JPG") if err != nil { t.Fatal(err) diff --git a/structrun.go b/structrun.go index 1dcb912..c10a101 100644 --- a/structrun.go +++ b/structrun.go @@ -102,6 +102,7 @@ func (r *Run) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error { // RunProperties encapsulates visual properties of a run type RunProperties struct { XMLName xml.Name `xml:"w:rPr,omitempty"` + Fonts *RunFonts Bold *Bold Italic *Italic Underline *Underline @@ -126,6 +127,13 @@ func (r *RunProperties) UnmarshalXML(d *xml.Decoder, start xml.StartElement) err if tt, ok := t.(xml.StartElement); ok { switch tt.Name.Local { + case "rFonts": + var value RunFonts + err = d.DecodeElement(&value, &tt) + if err != nil && !strings.HasPrefix(err.Error(), "expected") { + return err + } + r.Fonts = &value case "b": r.Bold = &Bold{} case "i": @@ -173,3 +181,28 @@ func (r *RunProperties) UnmarshalXML(d *xml.Decoder, start xml.StartElement) err return nil } + +// 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"` +} + +// UnmarshalXML ... +func (f *RunFonts) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error { + for _, attr := range start.Attr { + switch attr.Name.Local { + case "ascii": + f.Ascii = attr.Value + case "hAnsi": + f.HAnsi = attr.Value + case "hint": + f.Hint = attr.Value + } + } + // Consume the end element + _, err := d.Token() + return err +}