-
Notifications
You must be signed in to change notification settings - Fork 1
/
xml.go
72 lines (62 loc) · 1.88 KB
/
xml.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package bmf
import (
"encoding/xml"
"fmt"
"io"
)
// UnmarshalXMLAttr converts from format <up>,<right>,<down>,<left>
func (pad *Padding) UnmarshalXMLAttr(attr xml.Attr) error {
*pad = parsePadding(attr.Value)
return nil
}
// MarshalXMLAttr converts to format <up>,<right>,<down>,<left>
func (pad Padding) MarshalXMLAttr(name xml.Name) (xml.Attr, error) {
return xml.Attr{Name: name, Value: fmt.Sprintf("%d,%d,%d,%d", pad.Up, pad.Right, pad.Down, pad.Left)}, nil
}
// UnmarshalXMLAttr converts from format <horizontal>,<vertical>
func (sp *Spacing) UnmarshalXMLAttr(attr xml.Attr) error {
*sp = parseSpacing(attr.Value)
return nil
}
// MarshalXMLAttr converts to format <horizontal>,<vertical>
func (sp Spacing) MarshalXMLAttr(name xml.Name) (xml.Attr, error) {
return xml.Attr{Name: name, Value: fmt.Sprintf("%d,%d", sp.Horizontal, sp.Vertical)}, nil
}
// MarshalXMLAttr converts bool to number
func (nb BinBool) MarshalXMLAttr(name xml.Name) (xml.Attr, error) {
if nb {
return xml.Attr{Name: name, Value: "1"}, nil
}
return xml.Attr{Name: name, Value: "0"}, nil
}
type xmlFont struct {
XMLName xml.Name `xml:"font"`
Info Info `xml:"info"`
Common Common `xml:"common"`
Pages []Page `xml:"pages>page"`
Chars []Char `xml:"chars>char"`
Kernings []Kerning `xml:"kernings>kerning"`
}
// MarshalXML converts a Font struct to XML
func (font Font) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
start.Name.Local = "font"
return e.EncodeElement(xmlFont{
Info: font.Info,
Common: font.Common,
Pages: font.Pages,
Chars: font.Chars,
Kernings: font.Kernings,
}, start)
}
// ParseXML parses a bmf font file in XML format
func ParseXML(src io.Reader) (*Font, error) {
data, err := io.ReadAll(src)
if err != nil {
return nil, err
}
fnt := &Font{}
if err := xml.Unmarshal(data, fnt); err != nil {
return nil, err
}
return fnt, nil
}