mirror of
https://github.com/fumiama/go-docx.git
synced 2026-06-12 11:40:28 +08:00
add font to run
This commit is contained in:
25
apirun.go
25
apirun.go
@@ -58,7 +58,20 @@ func (r *Run) Italic() *Run {
|
|||||||
return r
|
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 {
|
func (r *Run) Underline(val string) *Run {
|
||||||
r.RunProperties.Underline = &Underline{Val: val}
|
r.RunProperties.Underline = &Underline{Val: val}
|
||||||
return r
|
return r
|
||||||
@@ -75,3 +88,13 @@ func (r *Run) AddTab() *Run {
|
|||||||
r.Children = append(r.Children, &Tab{})
|
r.Children = append(r.Children, &Tab{})
|
||||||
return r
|
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
|
||||||
|
}
|
||||||
|
|||||||
@@ -59,9 +59,14 @@ func main() {
|
|||||||
para1.AddText("italic").Italic().AddTab()
|
para1.AddText("italic").Italic().AddTab()
|
||||||
para1.AddText("underline").Underline("double").AddTab()
|
para1.AddText("underline").Underline("double").AddTab()
|
||||||
para1.AddText("highlight").Highlight("yellow").AddTab()
|
para1.AddText("highlight").Highlight("yellow").AddTab()
|
||||||
|
para1.AddText("font").Font("Consolas", "", "cs").AddTab()
|
||||||
|
|
||||||
para2 := w.AddParagraph().Justification("end")
|
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 := w.AddParagraph()
|
||||||
nextPara.AddLink("google", `http://google.com`)
|
nextPara.AddLink("google", `http://google.com`)
|
||||||
|
|||||||
@@ -33,7 +33,11 @@ func TestDrawingStructure(t *testing.T) {
|
|||||||
// add new paragraph
|
// add new paragraph
|
||||||
para1 := w.AddParagraph()
|
para1 := w.AddParagraph()
|
||||||
// add text
|
// 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")
|
r, err := para1.AddAnchorDrawingFrom("testdata/fumiama.JPG")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
|
|||||||
33
structrun.go
33
structrun.go
@@ -102,6 +102,7 @@ func (r *Run) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
|
|||||||
// RunProperties encapsulates visual properties of a run
|
// RunProperties encapsulates visual properties of a run
|
||||||
type RunProperties struct {
|
type RunProperties struct {
|
||||||
XMLName xml.Name `xml:"w:rPr,omitempty"`
|
XMLName xml.Name `xml:"w:rPr,omitempty"`
|
||||||
|
Fonts *RunFonts
|
||||||
Bold *Bold
|
Bold *Bold
|
||||||
Italic *Italic
|
Italic *Italic
|
||||||
Underline *Underline
|
Underline *Underline
|
||||||
@@ -126,6 +127,13 @@ func (r *RunProperties) UnmarshalXML(d *xml.Decoder, start xml.StartElement) err
|
|||||||
|
|
||||||
if tt, ok := t.(xml.StartElement); ok {
|
if tt, ok := t.(xml.StartElement); ok {
|
||||||
switch tt.Name.Local {
|
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":
|
case "b":
|
||||||
r.Bold = &Bold{}
|
r.Bold = &Bold{}
|
||||||
case "i":
|
case "i":
|
||||||
@@ -173,3 +181,28 @@ func (r *RunProperties) UnmarshalXML(d *xml.Decoder, start xml.StartElement) err
|
|||||||
|
|
||||||
return nil
|
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
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user