You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
78 lines
1.7 KiB
78 lines
1.7 KiB
package plotter
|
|
|
|
import (
|
|
"encoding/xml"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"os"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
type OneChar struct {
|
|
Letter rune
|
|
Root XMLFont
|
|
Path []*Point
|
|
}
|
|
|
|
// XMLChar ...
|
|
type XMLChar struct {
|
|
XMLName xml.Name `xml:"letter"`
|
|
ID string `xml:"id,attr"`
|
|
Paths []string `xml:"path"`
|
|
// XMLName xml.Name `xml:"bite"`
|
|
// ID rune `xml:"id,attr"`
|
|
// Paths []XMLPath `xml:"path"`
|
|
}
|
|
|
|
// XMLFont ...
|
|
type XMLFont struct {
|
|
XMLName xml.Name `xml:"root"`
|
|
Name string `xml:"name,attr"`
|
|
Width float64 `xml:"width,attr"`
|
|
Height float64 `xml:"height,attr"`
|
|
Space float64 `xml:"space,attr"`
|
|
Letters []XMLChar `xml:"letter"`
|
|
}
|
|
|
|
type FontParser struct {
|
|
Root XMLFont
|
|
ByLetter map[rune][]*OnePath
|
|
}
|
|
|
|
// Parse one font file
|
|
func (f *FontParser) Parse(fontPath string) {
|
|
// Open our xmlFile
|
|
xmlFile, err := os.Open(fontPath)
|
|
// if we os.Open returns an error then handle it
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
}
|
|
|
|
fmt.Println("Successfully Opened " + fontPath)
|
|
// defer the closing of our xmlFile so that we can parse it later on
|
|
defer xmlFile.Close()
|
|
|
|
// read our opened xmlFile as a byte array.
|
|
byteValue, _ := ioutil.ReadAll(xmlFile)
|
|
|
|
xml.Unmarshal(byteValue, &f.Root)
|
|
|
|
f.ByLetter = make(map[rune][]*OnePath)
|
|
|
|
for _, letter := range f.Root.Letters {
|
|
for _, path := range letter.Paths {
|
|
onePath := OnePath{}
|
|
onePath.Type = TypeText
|
|
for _, pt := range strings.Split(path, " ") {
|
|
ptSt := strings.Split(pt, ",")
|
|
point := Point{}
|
|
point.X, _ = strconv.ParseFloat(ptSt[0], 64)
|
|
point.Y, _ = strconv.ParseFloat(ptSt[1], 64)
|
|
onePath.List = append(onePath.List, &point)
|
|
}
|
|
letterID := []rune(letter.ID)[0]
|
|
f.ByLetter[letterID] = append(f.ByLetter[letterID], &onePath)
|
|
}
|
|
}
|
|
}
|