2015-08-10 10:34:31 +02:00
|
|
|
// Copyright 2015 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2016-04-10 07:25:52 +02:00
|
|
|
// +build example
|
2015-08-10 10:34:31 +02:00
|
|
|
//
|
2018-12-06 17:03:52 +01:00
|
|
|
// This build tag means that "go install git.gutmet.org/golang-image.git/..." doesn't
|
2016-04-10 07:25:52 +02:00
|
|
|
// install this example program. Use "go run main.go" to run it or "go install
|
|
|
|
// -tags=example" to install it.
|
2015-08-10 10:34:31 +02:00
|
|
|
|
|
|
|
// Font is a basic example of using fonts.
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"image"
|
|
|
|
"image/color"
|
|
|
|
"image/draw"
|
|
|
|
"image/png"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"os"
|
2015-08-11 09:49:26 +02:00
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2015-08-10 10:34:31 +02:00
|
|
|
|
2018-12-06 17:03:52 +01:00
|
|
|
"git.gutmet.org/golang-image.git/font"
|
|
|
|
"git.gutmet.org/golang-image.git/font/plan9font"
|
|
|
|
"git.gutmet.org/golang-image.git/math/fixed"
|
2015-08-10 10:34:31 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2015-08-11 09:49:26 +02:00
|
|
|
fontFlag = flag.String("font", "",
|
|
|
|
`filename of the Plan 9 font or subfont file, such as "lucsans/unicode.8.font" or "lucsans/lsr.14"`)
|
|
|
|
firstRuneFlag = flag.Int("firstrune", 0, "the Unicode code point of the first rune in the subfont file")
|
2015-08-10 10:34:31 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func pt(p fixed.Point26_6) image.Point {
|
|
|
|
return image.Point{
|
|
|
|
X: int(p.X+32) >> 6,
|
|
|
|
Y: int(p.Y+32) >> 6,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
flag.Parse()
|
|
|
|
|
2015-08-11 09:49:26 +02:00
|
|
|
// TODO: mmap the files.
|
|
|
|
if *fontFlag == "" {
|
2015-08-10 10:34:31 +02:00
|
|
|
flag.Usage()
|
2015-08-11 09:49:26 +02:00
|
|
|
log.Fatal("no font specified")
|
2015-08-10 10:34:31 +02:00
|
|
|
}
|
2015-08-11 09:49:26 +02:00
|
|
|
var face font.Face
|
|
|
|
if strings.HasSuffix(*fontFlag, ".font") {
|
|
|
|
fontData, err := ioutil.ReadFile(*fontFlag)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
dir := filepath.Dir(*fontFlag)
|
|
|
|
face, err = plan9font.ParseFont(fontData, func(name string) ([]byte, error) {
|
|
|
|
return ioutil.ReadFile(filepath.Join(dir, filepath.FromSlash(name)))
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fontData, err := ioutil.ReadFile(*fontFlag)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
face, err = plan9font.ParseSubfont(fontData, rune(*firstRuneFlag))
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2015-08-10 10:34:31 +02:00
|
|
|
}
|
|
|
|
|
2015-08-11 09:49:26 +02:00
|
|
|
dst := image.NewRGBA(image.Rect(0, 0, 800, 300))
|
2015-08-10 10:34:31 +02:00
|
|
|
draw.Draw(dst, dst.Bounds(), image.Black, image.Point{}, draw.Src)
|
|
|
|
|
|
|
|
d := &font.Drawer{
|
|
|
|
Dst: dst,
|
|
|
|
Src: image.White,
|
|
|
|
Face: face,
|
|
|
|
}
|
2015-08-11 09:49:26 +02:00
|
|
|
ss := []string{
|
|
|
|
"The quick brown fox jumps over the lazy dog.",
|
|
|
|
"Hello, 世界.",
|
|
|
|
"U+FFFD is \ufffd.",
|
|
|
|
}
|
|
|
|
for i, s := range ss {
|
|
|
|
d.Dot = fixed.P(20, 100*i+80)
|
|
|
|
dot0 := pt(d.Dot)
|
|
|
|
d.DrawString(s)
|
|
|
|
dot1 := pt(d.Dot)
|
|
|
|
dst.SetRGBA(dot0.X, dot0.Y, color.RGBA{0xff, 0x00, 0x00, 0xff})
|
|
|
|
dst.SetRGBA(dot1.X, dot1.Y, color.RGBA{0x00, 0x00, 0xff, 0xff})
|
|
|
|
}
|
2015-08-10 10:34:31 +02:00
|
|
|
|
|
|
|
out, err := os.Create("out.png")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
defer out.Close()
|
|
|
|
if err := png.Encode(out, dst); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|