aboutsummaryrefslogtreecommitdiffstats
path: root/examples/imageviewer/util.go
diff options
context:
space:
mode:
authorfaiface <faiface2202@gmail.com>2019-05-06 00:42:28 +0200
committerfaiface <faiface2202@gmail.com>2019-05-06 00:42:28 +0200
commitff5293e4cbf7b116715fce5b5e91f907ca41a3de (patch)
treec8422d8b370059935f518ec4d5a95e77fd8394d1 /examples/imageviewer/util.go
parent2357ae3c4c1f905dd3f75dea19d48d301252a4af (diff)
downloadgui-ff5293e4cbf7b116715fce5b5e91f907ca41a3de.zip
examples: add imageviewer
Diffstat (limited to 'examples/imageviewer/util.go')
-rw-r--r--examples/imageviewer/util.go104
1 files changed, 104 insertions, 0 deletions
diff --git a/examples/imageviewer/util.go b/examples/imageviewer/util.go
new file mode 100644
index 0000000..94f25fb
--- /dev/null
+++ b/examples/imageviewer/util.go
@@ -0,0 +1,104 @@
+package main
+
+import (
+ "image"
+ "image/color"
+ "image/draw"
+ "sync"
+
+ "github.com/golang/freetype/truetype"
+
+ "golang.org/x/image/font"
+ "golang.org/x/image/math/fixed"
+)
+
+type concurrentFace struct {
+ mu sync.Mutex
+ face font.Face
+}
+
+func (cf *concurrentFace) Close() error {
+ cf.mu.Lock()
+ defer cf.mu.Unlock()
+ return cf.face.Close()
+}
+
+func (cf *concurrentFace) Glyph(dot fixed.Point26_6, r rune) (dr image.Rectangle, mask image.Image, maskp image.Point, advance fixed.Int26_6, ok bool) {
+ cf.mu.Lock()
+ defer cf.mu.Unlock()
+ return cf.face.Glyph(dot, r)
+}
+
+func (cf *concurrentFace) GlyphBounds(r rune) (bounds fixed.Rectangle26_6, advance fixed.Int26_6, ok bool) {
+ cf.mu.Lock()
+ defer cf.mu.Unlock()
+ return cf.face.GlyphBounds(r)
+}
+
+func (cf *concurrentFace) GlyphAdvance(r rune) (advance fixed.Int26_6, ok bool) {
+ cf.mu.Lock()
+ defer cf.mu.Unlock()
+ return cf.face.GlyphAdvance(r)
+}
+
+func (cf *concurrentFace) Kern(r0, r1 rune) fixed.Int26_6 {
+ cf.mu.Lock()
+ defer cf.mu.Unlock()
+ return cf.face.Kern(r0, r1)
+}
+
+func (cf *concurrentFace) Metrics() font.Metrics {
+ cf.mu.Lock()
+ defer cf.mu.Unlock()
+ return cf.face.Metrics()
+}
+
+func TTFToFace(ttf []byte, size float64) (font.Face, error) {
+ font, err := truetype.Parse(ttf)
+ if err != nil {
+ return nil, err
+ }
+ return &concurrentFace{face: truetype.NewFace(font, &truetype.Options{
+ Size: size,
+ })}, nil
+}
+
+func DrawText(text string, face font.Face, clr color.Color) image.Image {
+ drawer := &font.Drawer{
+ Src: &image.Uniform{clr},
+ Face: face,
+ Dot: fixed.P(0, 0),
+ }
+ b26_6, _ := drawer.BoundString(text)
+ bounds := image.Rect(
+ b26_6.Min.X.Floor(),
+ b26_6.Min.Y.Floor(),
+ b26_6.Max.X.Ceil(),
+ b26_6.Max.Y.Ceil(),
+ )
+ drawer.Dst = image.NewRGBA(bounds)
+ drawer.DrawString(text)
+ return drawer.Dst
+}
+
+func DrawCentered(dst draw.Image, r image.Rectangle, src image.Image, op draw.Op) {
+ if src == nil {
+ return
+ }
+ bounds := src.Bounds()
+ center := bounds.Min.Add(bounds.Max).Div(2)
+ target := r.Min.Add(r.Max).Div(2)
+ delta := target.Sub(center)
+ draw.Draw(dst, bounds.Add(delta).Intersect(r), src, bounds.Min, op)
+}
+
+func DrawLeftCentered(dst draw.Image, r image.Rectangle, src image.Image, op draw.Op) {
+ if src == nil {
+ return
+ }
+ bounds := src.Bounds()
+ leftCenter := 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(leftCenter)
+ draw.Draw(dst, bounds.Add(delta).Intersect(r), src, bounds.Min, op)
+}