Skip to content

Commit

Permalink
optimize(pgSz): use explicit type int instaed of xml.Attr
Browse files Browse the repository at this point in the history
  • Loading branch information
fumiama committed Aug 23, 2024
1 parent 38ee4fb commit 51eca30
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
15 changes: 11 additions & 4 deletions structsect.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package docx
import (
"encoding/xml"
"io"
"strconv"
"strings"
)

Expand All @@ -31,8 +32,8 @@ type SectPr struct {

// PgSz show the paper size
type PgSz struct {
W xml.Attr `xml:"w:w,attr"` // width of paper
H xml.Attr `xml:"w:h,attr"` // high of paper
W int `xml:"w:w,attr"` // width of paper
H int `xml:"w:h,attr"` // high of paper
}

// UnmarshalXML ...
Expand Down Expand Up @@ -72,9 +73,15 @@ func (pgsz *PgSz) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
for _, attr := range start.Attr {
switch attr.Name.Local {
case "w":
pgsz.W = xml.Attr{Name: xml.Name{Local: "w:w"}, Value: attr.Value}
pgsz.W, err = strconv.Atoi(attr.Value)
if err != nil {
return err
}
case "h":
pgsz.H = xml.Attr{Name: xml.Name{Local: "w:h"}, Value: attr.Value}
pgsz.H, err = strconv.Atoi(attr.Value)
if err != nil {
return err
}
default:
// ignore other attributes now
}
Expand Down
9 changes: 4 additions & 5 deletions theme.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
package docx

import (
"encoding/xml"
"io/fs"
)

Expand All @@ -42,8 +41,8 @@ func (f *Docx) WithDefaultTheme() *Docx {
func (f *Docx) WithA3Page() *Docx {
sectpr := &SectPr{
PgSz: &PgSz{
W: xml.Attr{Name: xml.Name{Local: "w:w"}, Value: "16838"},
H: xml.Attr{Name: xml.Name{Local: "w:h"}, Value: "23811"},
W: 16838,
H: 23811,
},
}
f.Document.Body.Items = append(f.Document.Body.Items, sectpr)
Expand All @@ -54,8 +53,8 @@ func (f *Docx) WithA3Page() *Docx {
func (f *Docx) WithA4Page() *Docx {
sectpr := &SectPr{
PgSz: &PgSz{
W: xml.Attr{Name: xml.Name{Local: "w:w"}, Value: "11906"},
H: xml.Attr{Name: xml.Name{Local: "w:h"}, Value: "16838"},
W: 11906,
H: 16838,
},
}
f.Document.Body.Items = append(f.Document.Body.Items, sectpr)
Expand Down

0 comments on commit 51eca30

Please sign in to comment.