Skip to content

Instantly share code, notes, and snippets.

@mattn
Created February 20, 2016 13:59
Show Gist options
  • Save mattn/2b8ee9e6787d7fd4d163 to your computer and use it in GitHub Desktop.
Save mattn/2b8ee9e6787d7fd4d163 to your computer and use it in GitHub Desktop.
package main
import (
"bytes"
"image"
"image/draw"
"image/png"
"log"
"os"
"github.com/golang/freetype"
"github.com/mattn/go-runewidth"
"github.com/mattn/go-sixel"
)
func main() {
font, err := freetype.ParseFont(MustAsset("data/ipag-mona.ttf"))
if err != nil {
log.Fatal(err)
}
img, err := png.Decode(bytes.NewReader(MustAsset("data/shoei.png")))
if err != nil {
log.Fatal(err)
}
mw, mh := 0, 0
for _, arg := range os.Args[1:] {
w := runewidth.StringWidth(arg)
if w > mw {
mw = w
}
mh++
}
rgba := image.NewRGBA(image.Rect(0, 0, img.Bounds().Dx(), img.Bounds().Dy()))
draw.Draw(rgba, rgba.Bounds(), img, image.ZP, draw.Src)
fc := freetype.NewContext()
fc.SetDPI(72)
fc.SetFont(font)
sizeh := float64(60 / mh)
sizew := float64(580 / mw)
size := sizeh
if sizew < sizeh {
size = sizew
}
fc.SetFontSize(size)
fc.SetClip(rgba.Bounds())
fc.SetDst(rgba)
fc.SetSrc(image.Black)
pt := freetype.Pt(80, 70-int(sizeh/2*float64(mh-1)))
for _, arg := range os.Args[1:] {
_, err = fc.DrawString(arg, pt)
if err != nil {
log.Fatal(err)
}
pt.Y += fc.PointToFixed(sizeh)
}
err = sixel.NewEncoder(os.Stdout).Encode(rgba)
if err != nil {
log.Fatal(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment