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

optimize(pgSz): use explicit type int instaed of xml.Attr

This commit is contained in:
源文雨
2024-08-23 11:43:24 +08:00
parent 38ee4fb790
commit 51eca30836
2 changed files with 15 additions and 9 deletions

View File

@@ -20,6 +20,7 @@ package docx
import ( import (
"encoding/xml" "encoding/xml"
"io" "io"
"strconv"
"strings" "strings"
) )
@@ -31,8 +32,8 @@ type SectPr struct {
// PgSz show the paper size // PgSz show the paper size
type PgSz struct { type PgSz struct {
W xml.Attr `xml:"w:w,attr"` // width of paper W int `xml:"w:w,attr"` // width of paper
H xml.Attr `xml:"w:h,attr"` // high of paper H int `xml:"w:h,attr"` // high of paper
} }
// UnmarshalXML ... // UnmarshalXML ...
@@ -72,9 +73,15 @@ func (pgsz *PgSz) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
for _, attr := range start.Attr { for _, attr := range start.Attr {
switch attr.Name.Local { switch attr.Name.Local {
case "w": 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": 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: default:
// ignore other attributes now // ignore other attributes now
} }

View File

@@ -21,7 +21,6 @@
package docx package docx
import ( import (
"encoding/xml"
"io/fs" "io/fs"
) )
@@ -42,8 +41,8 @@ func (f *Docx) WithDefaultTheme() *Docx {
func (f *Docx) WithA3Page() *Docx { func (f *Docx) WithA3Page() *Docx {
sectpr := &SectPr{ sectpr := &SectPr{
PgSz: &PgSz{ PgSz: &PgSz{
W: xml.Attr{Name: xml.Name{Local: "w:w"}, Value: "16838"}, W: 16838,
H: xml.Attr{Name: xml.Name{Local: "w:h"}, Value: "23811"}, H: 23811,
}, },
} }
f.Document.Body.Items = append(f.Document.Body.Items, sectpr) f.Document.Body.Items = append(f.Document.Body.Items, sectpr)
@@ -54,8 +53,8 @@ func (f *Docx) WithA3Page() *Docx {
func (f *Docx) WithA4Page() *Docx { func (f *Docx) WithA4Page() *Docx {
sectpr := &SectPr{ sectpr := &SectPr{
PgSz: &PgSz{ PgSz: &PgSz{
W: xml.Attr{Name: xml.Name{Local: "w:w"}, Value: "11906"}, W: 11906,
H: xml.Attr{Name: xml.Name{Local: "w:h"}, Value: "16838"}, H: 16838,
}, },
} }
f.Document.Body.Items = append(f.Document.Body.Items, sectpr) f.Document.Body.Items = append(f.Document.Body.Items, sectpr)