From 1aec500f91e439efa1df7d63f0bbcc83300552ab Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Wed, 13 Apr 2016 12:17:21 +0200 Subject: simplify --- hershey.go | 26 ++++++++++++-------------- main.go | 16 ++++++++-------- 2 files changed, 20 insertions(+), 22 deletions(-) diff --git a/hershey.go b/hershey.go index 585f3ce..6323d69 100644 --- a/hershey.go +++ b/hershey.go @@ -9,18 +9,16 @@ import ( "strings" ) -type Unit float64 - type Point struct { - X, Y Unit + X, Y int } type Path []Point type Set []Path type Glyph struct { - S Set - W Unit + Set Set + Width int } type Font map[rune]Glyph @@ -31,11 +29,11 @@ func parseInt(s string) (n int) { return } -func parsePoint(in uint8) Unit { - return Unit(in) - Unit('R') +func parsePoint(in uint8) int { + return int(in) - int('R') } -func parseData(s string, w, h, scale Unit) Set { +func parseData(s string, w, h int) Set { var st Set for i, el := range strings.Fields(s) { var ph Path @@ -44,8 +42,8 @@ func parseData(s string, w, h, scale Unit) Set { } for n := 0; n < len(el); n += 2 { var p Point - p.Y = scale * (w/2 + parsePoint(el[n])) - p.X = scale * (h/2 + parsePoint(el[n+1])) + p.Y = w/2 + parsePoint(el[n]) + p.X = h/2 + parsePoint(el[n+1]) ph = append(ph, p) } st = append(st, ph) @@ -53,7 +51,7 @@ func parseData(s string, w, h, scale Unit) Set { return st } -func loadFont(fname string, scale Unit) Font { +func loadFont(fname string) Font { fnt := make(Font) f, err := os.Open(fname) @@ -71,8 +69,8 @@ func loadFont(fname string, scale Unit) Font { r := parsePoint(line[9]) w := r - l fnt[rune(n)] = Glyph{ - S: parseData(line[10:], w, 32, scale), - W: w * scale, + Set: parseData(line[10:], w, 32), + Width: w, } } if err := scanner.Err(); err != nil { @@ -102,7 +100,7 @@ func (st Set) String() (s string) { } func (g Glyph) String() string { - return fmt.Sprint(g.S) + return fmt.Sprint(g.Set) } func (f Font) Select(n []int) Font { diff --git a/main.go b/main.go index 0cfc06d..f90f82e 100644 --- a/main.go +++ b/main.go @@ -29,28 +29,28 @@ var selector = map[string]string{ } func printAll(f Font) { - var x, y Unit + var x, y int for i := 32; i < 128; i++ { gl := f[rune(i)] - if y+gl.W >= 4000 { + if y+gl.Width >= 4000 { y = 0 x += 100 } fmt.Printf("^%v,%v,%s", x, y, gl) - y += gl.W + y += gl.Width } } func printStruct(f Font) { - fmt.Println("var height = Unit(72)") + fmt.Println("var height = 32") fmt.Println("var font = Font{") for i := 0; i < len(f); i++ { r := rune(i + 32) gl := f[r] fmt.Printf("%q: Glyph{\n", r) - fmt.Println("S: Set{") - for _, s := range gl.S { + fmt.Println("Set: Set{") + for _, s := range gl.Set { fmt.Println("Path{") for _, p := range s { fmt.Printf("Point{%v, %v},\n", p.X, p.Y) @@ -58,7 +58,7 @@ func printStruct(f Font) { fmt.Println("},") } fmt.Println("},") - fmt.Printf("W: %v,\n", gl.W) + fmt.Printf("Width: %v,\n", gl.Width) fmt.Println("},") } fmt.Println("}") @@ -68,7 +68,7 @@ var font = flag.String("font", "Roman Simplex", "Font to use") func main() { flag.Parse() - f := loadFont("data/hershey", Unit(2)) + f := loadFont("data/hershey") s, ok := selector[*font] if !ok { log.Fatal("no such font") -- cgit v1.2.3