From 30913cc232c7bd22dbd34e06bff137ab48218cb6 Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Thu, 14 May 2015 17:13:07 +0200 Subject: Add Max and Width --- hershey.go | 46 ++++++++++++++++++++++++++++++++++++++-------- main.go | 7 ++++++- 2 files changed, 44 insertions(+), 9 deletions(-) diff --git a/hershey.go b/hershey.go index 01211f5..6da6553 100644 --- a/hershey.go +++ b/hershey.go @@ -19,10 +19,10 @@ type Set []Path type Glyph struct { N, K int L, R int - S Set + S Set } -type Font map[int]Glyph +type Font map[rune]Glyph func parseInt(s string) (n int) { s = strings.TrimSpace(s) @@ -71,7 +71,7 @@ func loadFont(fname string) Font { R: parsePoint(line[9]), S: parseData(line[10:]), } - fnt[gl.N] = gl + fnt[rune(gl.N)] = gl } if err := scanner.Err(); err != nil { log.Fatal(err) @@ -80,13 +80,17 @@ func loadFont(fname string) Font { } func (p Point) String() string { - return fmt.Sprintf("%v,%v,", p.X, p.Y) + return fmt.Sprintf("(%v %v)", p.X, p.Y) } func (p Path) String() (s string) { - s = fmt.Sprint("M", p[0]) - for _, pt := range p[1:] { - s += fmt.Sprint("D", pt) + for i, pt := range p { + s += fmt.Sprint(pt) + if i < len(p)-1 { + s += fmt.Sprint(",") + } else { + s += fmt.Sprint(" ") + } } return } @@ -102,10 +106,36 @@ func (g Glyph) String() string { return fmt.Sprint(g.S) } +func (g Glyph) Width() int { + return g.R - g.L +} + +func (g Glyph) Max() (x, y int) { + var top, bottom int + var left, right int + for _, pt := range g.S { + for _, p := range pt { + if p.X > right { + right = p.X + } + if p.X < left { + left = p.X + } + if p.Y > top { + top = p.Y + } + if p.Y < bottom { + bottom = p.Y + } + } + } + return right - left, top - bottom +} + func (f Font) Select(n []int) Font { ret := make(Font) for i, p := range n { - ret[i+32] = f[p] + ret[rune(i+32)] = f[rune(p)] } return ret } diff --git a/main.go b/main.go index 5f6a0bd..38ca8d8 100644 --- a/main.go +++ b/main.go @@ -3,6 +3,7 @@ package main import "fmt" var selector = map[string]string{ + /* "Gothic English Triplex": "gothgbt.hmp", "Gothic German Triplex": "gothgrt.hmp", "Gothic Italian Triplex": "gothitt.hmp", @@ -20,8 +21,11 @@ var selector = map[string]string{ "Roman Complex Small": "romancs.hmp", "Roman Duplex": "romand.hmp", "Roman Plain": "romanp.hmp", + */ "Roman Simplex": "romans.hmp", + /* "Roman Triplex": "romant.hmp", + */ } func main() { @@ -32,7 +36,8 @@ func main() { m := getMap("data/" + v) for k, gl := range fnt.Select(m) { - fmt.Println(k, gl) + w, h := gl.Max() + fmt.Println(string(k), gl.Width(), w, h, gl) } } } -- cgit v1.2.3