~mendelmaleh/dummy

6cddb1ef931cff9e8f5bd1e1ba4d014942d688ec — Mendel E 1 year, 6 months ago d5010cc
Don't draw empty text
1 files changed, 21 insertions(+), 18 deletions(-)

M image.go
M image.go => image.go +21 -18
@@ 4,6 4,7 @@ import (
	"image"
	"image/color"
	"image/draw"
	"strings"

	"git.sr.ht/~mendelmaleh/freetype/truetype"
	"golang.org/x/image/font"


@@ 29,28 30,30 @@ func Image(opt Options) image.Image {
	}

	// text
	face := truetype.NewFace(opt.Font, &truetype.Options{
		Size:    opt.Size,
		DPI:     opt.DPI,
		Hinting: font.HintingNone,
	})
	if len(strings.TrimSpace(opt.Text)) > 0 {
		face := truetype.NewFace(opt.Font, &truetype.Options{
			Size:    opt.Size,
			DPI:     opt.DPI,
			Hinting: font.HintingNone,
		})

	// TODO: font color? colors[-1]?
	drawer := &font.Drawer{
		Dst:  im,
		Src:  image.NewUniform(opt.Colors[1]),
		Face: face,
	}
		// TODO: font color? colors[-1]?
		drawer := &font.Drawer{
			Dst:  im,
			Src:  image.NewUniform(opt.Colors[1]),
			Face: face,
		}

	b, x := font.BoundString(face, opt.Text)
	y := b.Max.Y - b.Min.Y
		b, x := font.BoundString(face, opt.Text)
		y := b.Max.Y - b.Min.Y

	drawer.Dot = fixed.Point26_6{
		X: (fixed.I(opt.Width) - x) / 2,
		Y: (fixed.I(opt.Height) + y) / 2,
	}
		drawer.Dot = fixed.Point26_6{
			X: (fixed.I(opt.Width) - x) / 2,
			Y: (fixed.I(opt.Height) + y) / 2,
		}

	drawer.DrawString(opt.Text)
		drawer.DrawString(opt.Text)
	}

	return im
}