6deea24143
Its implementation is mostly a copy/paste of the freetype.Context type. Follow-up commits will make it more efficient. Also add an example that uses a truetype.Face and the golang.org/x/exp/shiny/font package to draw text.
147 lines
3.6 KiB
Go
147 lines
3.6 KiB
Go
// Copyright 2015 The Freetype-Go Authors. All rights reserved.
|
||
// Use of this source code is governed by your choice of either the
|
||
// FreeType License or the GNU General Public License version 2 (or
|
||
// any later version), both of which can be found in the LICENSE file.
|
||
|
||
// +build ignore
|
||
//
|
||
// This build tag means that "go install github.com/golang/freetype/..."
|
||
// doesn't install this example program. Use "go run main.go" to run it.
|
||
|
||
package main
|
||
|
||
import (
|
||
"bufio"
|
||
"flag"
|
||
"fmt"
|
||
"image"
|
||
"image/color"
|
||
"image/draw"
|
||
"image/png"
|
||
"io/ioutil"
|
||
"log"
|
||
"math"
|
||
"os"
|
||
|
||
"github.com/golang/freetype/truetype"
|
||
"golang.org/x/exp/shiny/font"
|
||
"golang.org/x/image/math/fixed"
|
||
)
|
||
|
||
var (
|
||
dpi = flag.Float64("dpi", 72, "screen resolution in Dots Per Inch")
|
||
fontfile = flag.String("fontfile", "../../testdata/luxisr.ttf", "filename of the ttf font")
|
||
hinting = flag.String("hinting", "none", "none | full")
|
||
size = flag.Float64("size", 12, "font size in points")
|
||
spacing = flag.Float64("spacing", 1.5, "line spacing (e.g. 2 means double spaced)")
|
||
wonb = flag.Bool("whiteonblack", false, "white text on a black background")
|
||
)
|
||
|
||
var text = []string{
|
||
"’Twas brillig, and the slithy toves",
|
||
"Did gyre and gimble in the wabe;",
|
||
"All mimsy were the borogoves,",
|
||
"And the mome raths outgrabe.",
|
||
"",
|
||
"“Beware the Jabberwock, my son!",
|
||
"The jaws that bite, the claws that catch!",
|
||
"Beware the Jubjub bird, and shun",
|
||
"The frumious Bandersnatch!”",
|
||
"",
|
||
"He took his vorpal sword in hand:",
|
||
"Long time the manxome foe he sought—",
|
||
"So rested he by the Tumtum tree,",
|
||
"And stood awhile in thought.",
|
||
"",
|
||
"And as in uffish thought he stood,",
|
||
"The Jabberwock, with eyes of flame,",
|
||
"Came whiffling through the tulgey wood,",
|
||
"And burbled as it came!",
|
||
"",
|
||
"One, two! One, two! and through and through",
|
||
"The vorpal blade went snicker-snack!",
|
||
"He left it dead, and with its head",
|
||
"He went galumphing back.",
|
||
"",
|
||
"“And hast thou slain the Jabberwock?",
|
||
"Come to my arms, my beamish boy!",
|
||
"O frabjous day! Callooh! Callay!”",
|
||
"He chortled in his joy.",
|
||
"",
|
||
"’Twas brillig, and the slithy toves",
|
||
"Did gyre and gimble in the wabe;",
|
||
"All mimsy were the borogoves,",
|
||
"And the mome raths outgrabe.",
|
||
}
|
||
|
||
func main() {
|
||
flag.Parse()
|
||
|
||
// Read the font data.
|
||
fontBytes, err := ioutil.ReadFile(*fontfile)
|
||
if err != nil {
|
||
log.Println(err)
|
||
return
|
||
}
|
||
f, err := truetype.Parse(fontBytes)
|
||
if err != nil {
|
||
log.Println(err)
|
||
return
|
||
}
|
||
|
||
// Draw the background and the guidelines.
|
||
fg, bg := image.Black, image.White
|
||
ruler := color.RGBA{0xdd, 0xdd, 0xdd, 0xff}
|
||
if *wonb {
|
||
fg, bg = image.White, image.Black
|
||
ruler = color.RGBA{0x22, 0x22, 0x22, 0xff}
|
||
}
|
||
rgba := image.NewRGBA(image.Rect(0, 0, 640, 480))
|
||
draw.Draw(rgba, rgba.Bounds(), bg, image.ZP, draw.Src)
|
||
for i := 0; i < 200; i++ {
|
||
rgba.Set(10, 10+i, ruler)
|
||
rgba.Set(10+i, 10, ruler)
|
||
}
|
||
|
||
// Draw the text.
|
||
h := font.HintingNone
|
||
switch *hinting {
|
||
case "full":
|
||
h = font.HintingFull
|
||
}
|
||
d := &font.Drawer{
|
||
Dst: rgba,
|
||
Src: fg,
|
||
Face: truetype.NewFace(f, truetype.Options{
|
||
Size: *size,
|
||
DPI: *dpi,
|
||
Hinting: h,
|
||
}),
|
||
}
|
||
dy0 := int(math.Ceil(*size * *dpi / 72))
|
||
dy := int(math.Ceil(*size * *spacing * *dpi / 72))
|
||
for i, s := range text {
|
||
d.Dot = fixed.P(10, 10+dy0+i*dy)
|
||
d.DrawString(s)
|
||
}
|
||
|
||
// Save that RGBA image to disk.
|
||
outFile, err := os.Create("out.png")
|
||
if err != nil {
|
||
log.Println(err)
|
||
os.Exit(1)
|
||
}
|
||
defer outFile.Close()
|
||
b := bufio.NewWriter(outFile)
|
||
err = png.Encode(b, rgba)
|
||
if err != nil {
|
||
log.Println(err)
|
||
os.Exit(1)
|
||
}
|
||
err = b.Flush()
|
||
if err != nil {
|
||
log.Println(err)
|
||
os.Exit(1)
|
||
}
|
||
fmt.Println("Wrote out.png OK.")
|
||
}
|