Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

增加页面宽度高度、字体设置,增加序号等 #44

Closed
wants to merge 17 commits into from
25 changes: 21 additions & 4 deletions apirun.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,22 @@ func (r *Run) Size(size string) *Run {
return r
}

// SizeCs allows to set run size
func (r *Run) SizeCs(size string) *Run {
r.RunProperties.SizeCs = &SizeCs{
Val: size,
}
return r
}

// Spacing allows to set run spacing
func (r *Run) Spacing(line int) *Run {
r.RunProperties.Spacing = &Spacing{
Line: line,
}
return r
}

// Shade allows to set run shade
func (r *Run) Shade(val, color, fill string) *Run {
r.RunProperties.Shade = &Shade{
Expand Down Expand Up @@ -100,11 +116,12 @@ func (r *Run) AddTab() *Run {
}

// Font sets the font of the run
func (r *Run) Font(ascii, hansi, hint string) *Run {
func (r *Run) Font(ascii, eastAsia, hansi, hint string) *Run {
r.RunProperties.Fonts = &RunFonts{
ASCII: ascii,
HAnsi: hansi,
Hint: hint,
ASCII: ascii,
EastAsia: eastAsia,
HAnsi: hansi,
Hint: hint,
}
return r
}
4 changes: 2 additions & 2 deletions cmd/main/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,11 @@ 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()
para1.AddText("font").Font("Consolas", "", "", "cs").AddTab()

para2 := w.AddParagraph().Justification("end")
para2.AddText("test all font attrs").
Size("44").Color("ff0000").Font("Consolas", "", "cs").
Size("44").Color("ff0000").Font("Consolas", "", "", "cs").
Shade("clear", "auto", "E7E6E6").
Bold().Italic().Underline("wave").
Highlight("yellow")
Expand Down
8 changes: 8 additions & 0 deletions structpara.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type ParagraphProperties struct {
XMLName xml.Name `xml:"w:pPr,omitempty"`
Tabs *Tabs
Spacing *Spacing
NumProperties *NumProperties
Ind *Ind
Justification *Justification
Shade *Shade
Expand Down Expand Up @@ -108,6 +109,13 @@ func (p *ParagraphProperties) UnmarshalXML(d *xml.Decoder, _ xml.StartElement) e
p.RunProperties = &value
case "pStyle":
p.Style = &Style{Val: getAtt(tt.Attr, "val")}
case "numPr":
var value NumProperties
err = d.DecodeElement(&value, &tt)
if err != nil && !strings.HasPrefix(err.Error(), "expected") {
return err
}
p.NumProperties = &value
case "textAlignment":
p.TextAlignment = &TextAlignment{Val: getAtt(tt.Attr, "val")}
case "adjustRightInd":
Expand Down
49 changes: 49 additions & 0 deletions structrun.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,55 @@
Strike *Strike
}

type NumProperties struct {

Check failure on line 222 in structrun.go

View workflow job for this annotation

GitHub Actions / lint

exported: exported type NumProperties should have comment or be unexported (revive)
XMLName xml.Name `xml:"w:numPr,omitempty"`
NumId *NumId

Check failure on line 224 in structrun.go

View workflow job for this annotation

GitHub Actions / lint

var-naming: struct field NumId should be NumID (revive)
Ilvl *Ilevel
}

type NumId struct {

Check failure on line 228 in structrun.go

View workflow job for this annotation

GitHub Actions / lint

exported: exported type NumId should have comment or be unexported (revive)
XMLName xml.Name `xml:"w:numId,omitempty"`
Val string `xml:"w:val,attr"`
}

type Ilevel struct {

Check failure on line 233 in structrun.go

View workflow job for this annotation

GitHub Actions / lint

exported: exported type Ilevel should have comment or be unexported (revive)
XMLName xml.Name `xml:"w:ilvl,omitempty"`
Val string `xml:"w:val,attr"`
}

func (n *NumProperties) UnmarshalXML(d *xml.Decoder, _ xml.StartElement) error {

Check failure on line 238 in structrun.go

View workflow job for this annotation

GitHub Actions / lint

exported: exported method NumProperties.UnmarshalXML should have comment or be unexported (revive)
for {
t, err := d.Token()
if err == io.EOF {
break
}
if err != nil {
return err
}

if tt, ok := t.(xml.StartElement); ok {
switch tt.Name.Local {
case "numId":
var value NumId
value.Val = getAtt(tt.Attr, "val")
n.NumId = &value
case "ilvl":
var value Ilevel
value.Val = getAtt(tt.Attr, "val")
n.Ilvl = &value
default:
err = d.Skip() // skip unsupported tags
if err != nil {
return err
}
continue
}
}
}

return nil
}

// UnmarshalXML ...
func (r *RunProperties) UnmarshalXML(d *xml.Decoder, _ xml.StartElement) error {
for {
Expand Down
132 changes: 132 additions & 0 deletions structsect.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
type SectPr struct {
XMLName xml.Name `xml:"w:sectPr,omitempty"` // properties of the document, including paper size
PgSz *PgSz `xml:"w:pgSz,omitempty"`
PgMar *PgMar `xml:"w:pgMar,omitempty"`
Cols *Cols `xml:"w:cols,omitempty"`
DocGrid *DocGrid `xml:"w:docGrid,omitempty"`
}

// PgSz show the paper size
Expand All @@ -36,6 +39,25 @@
H int `xml:"w:h,attr"` // high of paper
}

type PgMar struct {

Check failure on line 42 in structsect.go

View workflow job for this annotation

GitHub Actions / lint

exported: exported type PgMar should have comment or be unexported (revive)
Top int `xml:"w:top,attr"`
Left int `xml:"w:left,attr"`
Bottom int `xml:"w:bottom,attr"`
Right int `xml:"w:right,attr"`
Header int `xml:"w:header,attr"`
Footer int `xml:"w:footer,attr"`
Gutter int `xml:"w:gutter,attr"`
}

type Cols struct {

Check failure on line 52 in structsect.go

View workflow job for this annotation

GitHub Actions / lint

exported: exported type Cols should have comment or be unexported (revive)
Space int `xml:"w:space,attr"`
}

type DocGrid struct {

Check failure on line 56 in structsect.go

View workflow job for this annotation

GitHub Actions / lint

exported: exported type DocGrid should have comment or be unexported (revive)
Type string `xml:"w:type,attr"`
LinePitch int `xml:"w:linePitch,attr"`
}

// UnmarshalXML ...
func (sect *SectPr) UnmarshalXML(d *xml.Decoder, _ xml.StartElement) error {
for {
Expand All @@ -55,6 +77,27 @@
return err
}
sect.PgSz = &value
case "pgMar":
var value PgMar
err = d.DecodeElement(&value, &tt)
if err != nil && !strings.HasPrefix(err.Error(), "expected") {
return err
}
sect.PgMar = &value
case "cols":
var value Cols
err = d.DecodeElement(&value, &tt)
if err != nil && !strings.HasPrefix(err.Error(), "expected") {
return err
}
sect.Cols = &value
case "docGrid":
var value DocGrid
err = d.DecodeElement(&value, &tt)
if err != nil && !strings.HasPrefix(err.Error(), "expected") {
return err
}
sect.DocGrid = &value
default:
err = d.Skip() // skip unsupported tags
if err != nil {
Expand Down Expand Up @@ -90,3 +133,92 @@
_, err = d.Token()
return err
}

func (pgmar *PgMar) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {

Check failure on line 137 in structsect.go

View workflow job for this annotation

GitHub Actions / lint

exported: exported method PgMar.UnmarshalXML should have comment or be unexported (revive)
var err error

for _, attr := range start.Attr {
switch attr.Name.Local {
case "top":
pgmar.Top, err = strconv.Atoi(attr.Value)
if err != nil {
return err
}
case "left":
pgmar.Left, err = strconv.Atoi(attr.Value)
if err != nil {
return err
}
case "bottom":
pgmar.Bottom, err = strconv.Atoi(attr.Value)
if err != nil {
return err
}
case "right":
pgmar.Right, err = strconv.Atoi(attr.Value)
if err != nil {
return err
}
case "header":
pgmar.Header, err = strconv.Atoi(attr.Value)
if err != nil {
return err
}
case "footer":
pgmar.Footer, err = strconv.Atoi(attr.Value)
if err != nil {
return err
}
case "gutter":
pgmar.Gutter, err = strconv.Atoi(attr.Value)
if err != nil {
return err
}
default:
// ignore other attributes now
}
}
// Consume the end element
_, err = d.Token()
return err
}

func (cols *Cols) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {

Check failure on line 186 in structsect.go

View workflow job for this annotation

GitHub Actions / lint

exported: exported method Cols.UnmarshalXML should have comment or be unexported (revive)
var err error

for _, attr := range start.Attr {
switch attr.Name.Local {
case "space":
cols.Space, err = strconv.Atoi(attr.Value)
if err != nil {
return err
}
default:
// ignore other attributes now
}
}
// Consume the end element
_, err = d.Token()
return err
}

func (dg *DocGrid) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
var err error

for _, attr := range start.Attr {
switch attr.Name.Local {
case "linePitch":
dg.LinePitch, err = strconv.Atoi(attr.Value)
if err != nil {
return err
}
case "type":
dg.Type = attr.Value
default:
// ignore other attributes now
}
}
// Consume the end element
_, err = d.Token()
return err
}
Loading