diff options
Diffstat (limited to 'gui/widget/text.go')
| -rw-r--r-- | gui/widget/text.go | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/gui/widget/text.go b/gui/widget/text.go new file mode 100644 index 0000000..cb66a0a --- /dev/null +++ b/gui/widget/text.go @@ -0,0 +1,74 @@ +package widget + +import ( + "log" + "sync" + + "image" + "image/draw" + + "golang.org/x/image/font" + "golang.org/x/image/font/gofont/goregular" + "golang.org/x/image/font/opentype" + "golang.org/x/image/math/fixed" +) + +var ( + FONT = goregular.TTF + FONT_SIZE float64 = 15 + DPI float64 = 72 + BG_COLOR = image.White + TEXT_COLOR = image.Black +) + +var face *concurrentFace + +func init() { + fnt, err := opentype.Parse(FONT) + if err != nil { + log.Fatal(err) + } + fce, err := opentype.NewFace(fnt, &opentype.FaceOptions{ + Size: FONT_SIZE, + DPI: DPI, + }) + if err != nil { + log.Fatal(err) + } + face = &concurrentFace{sync.Mutex{}, fce} +} + +func drawText(text []byte, dst draw.Image, r image.Rectangle) { + drawer := font.Drawer{ + Src: TEXT_COLOR, + Face: face, + Dot: fixed.P(0, 0), + } + + bounds := textBounds(text, drawer) + + // background + draw.Draw(dst, r, BG_COLOR, image.ZP, draw.Src) + + // text image + textImg := image.NewRGBA(bounds) + draw.Draw(textImg, bounds, BG_COLOR, image.ZP, draw.Src) + drawer.Dst = textImg + drawer.DrawBytes(text) + + // draw text image over background + left := image.Pt(bounds.Min.X, (bounds.Min.Y+bounds.Max.Y)/2) + target := image.Pt(r.Min.X, (r.Min.Y+r.Max.Y)/2) + delta := target.Sub(left) + draw.Draw(dst, bounds.Add(delta).Intersect(r), drawer.Dst, bounds.Min, draw.Src) +} + +func textBounds(text []byte, drawer font.Drawer) image.Rectangle { + b, _ := drawer.BoundBytes(text) + return image.Rect( + b.Min.X.Floor(), + b.Min.Y.Floor(), + b.Max.X.Ceil(), + b.Max.Y.Ceil(), + ) +} |