Skip to content

Instantly share code, notes, and snippets.

@proglottis
Created May 16, 2018 09:50
Show Gist options
  • Save proglottis/7c4263787b1caad598839f3f8e6ac3cd to your computer and use it in GitHub Desktop.
Save proglottis/7c4263787b1caad598839f3f8e6ac3cd to your computer and use it in GitHub Desktop.
package main
import (
"image"
"image/color"
"image/draw"
"image/png"
"os"
"github.com/golang/freetype/truetype"
"golang.org/x/image/font"
"golang.org/x/image/font/gofont/goregular"
"golang.org/x/image/math/fixed"
)
const label = "Sorry. We have no imagery here."
const half fixed.Int26_6 = 2 << 4
func main() {
f, err := truetype.Parse(goregular.TTF)
if err != nil {
panic(err)
}
face := truetype.NewFace(f, &truetype.Options{
Size: 20,
})
bg := image.NewUniform(color.Gray{200})
fg := image.NewUniform(color.Gray{100})
w, h := 500, 500
x, y := w/2, h/4
img := image.NewRGBA(image.Rect(0, 0, w, h))
draw.Draw(img, img.Bounds(), bg, image.ZP, draw.Src)
drawer := font.Drawer{
Dst: img,
Src: fg,
Face: face,
Dot: fixed.P(x, y),
}
drawer.Dot.X = drawer.Dot.X - drawer.MeasureString(label).Mul(half)
drawer.DrawString(label)
if err := png.Encode(os.Stdout, img); err != nil {
panic(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment