1
0
mirror of https://github.com/fumiama/go-docx.git synced 2026-06-06 00:00:24 +08:00

add font to run

This commit is contained in:
源文雨
2023-02-26 19:53:36 +08:00
parent f3265280e3
commit e57b2b9ace
4 changed files with 68 additions and 3 deletions

View File

@@ -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
}

View File

@@ -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`)

View File

@@ -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)

View File

@@ -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
}