diff options
Diffstat (limited to 'gui/text/text.go')
| -rw-r--r-- | gui/text/text.go | 20 |
1 files changed, 16 insertions, 4 deletions
diff --git a/gui/text/text.go b/gui/text/text.go index dfe60e1..942dce2 100644 --- a/gui/text/text.go +++ b/gui/text/text.go @@ -38,12 +38,19 @@ func init() { face = &concurrentFace{sync.Mutex{}, fce} } +type Align int + +const ( + ALIGN_LEFT Align = iota + ALIGN_RIGHT +) + func Size(text string) image.Point { bounds := textBounds([]byte(text), font.Drawer{Face: face}) return image.Point{bounds.Max.X - bounds.Min.X + 2*PAD, bounds.Max.Y - bounds.Min.Y + 2*PAD} } -func Draw(text []byte, dst draw.Image, r image.Rectangle, fg, bg color.Color) { +func Draw(text []byte, dst draw.Image, r image.Rectangle, fg, bg color.Color, align Align) { drawer := font.Drawer{ Src: &image.Uniform{fg}, Face: face, @@ -60,11 +67,16 @@ func Draw(text []byte, dst draw.Image, r image.Rectangle, fg, bg color.Color) { drawer.Dst = textImg drawer.DrawBytes(text) - // draw text image over background leftCentre := image.Pt(bounds.Min.X, (bounds.Min.Y+bounds.Max.Y)/2) - target := image.Pt(r.Max.X-bounds.Max.X-PAD, (r.Min.Y+r.Max.Y)/2) + var target image.Point + switch align { + case ALIGN_LEFT: + target = image.Pt(r.Min.X+PAD, (r.Min.Y+r.Max.Y)/2) + case ALIGN_RIGHT: + target = image.Pt(r.Max.X-bounds.Max.X-PAD, (r.Min.Y+r.Max.Y)/2) + } delta := target.Sub(leftCentre) - draw.Draw(dst, bounds.Add(delta).Intersect(r), drawer.Dst, bounds.Min, draw.Src) + draw.Draw(dst, bounds.Add(delta).Intersect(r), textImg, bounds.Min, draw.Src) } func textBounds(text []byte, drawer font.Drawer) image.Rectangle { |