aboutsummaryrefslogtreecommitdiffstats
path: root/gui/text/text.go
diff options
context:
space:
mode:
authorSam Anthony <sam@samanthony.xyz>2024-05-10 14:04:55 -0400
committerSam Anthony <sam@samanthony.xyz>2024-05-10 14:04:55 -0400
commitf81e4a813766980c6e837893e19cc10bf6d7c41e (patch)
treec5917a944647f14a430a1ffdf514d9aa714589d2 /gui/text/text.go
parent39c1d74160b268cf8b573b3c0d5e5ce6b0920ee7 (diff)
downloadvolute-f81e4a813766980c6e837893e19cc10bf6d7c41e.zip
text alignment
Diffstat (limited to 'gui/text/text.go')
-rw-r--r--gui/text/text.go20
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 {