2010-05-28 07:22:49 +02:00
|
|
|
|
// Copyright 2010 The Freetype-Go Authors. All rights reserved.
|
|
|
|
|
// Use of this source code is governed by your choice of either the
|
2010-08-03 03:07:23 +02:00
|
|
|
|
// FreeType License or the GNU General Public License version 2 (or
|
|
|
|
|
// any later version), both of which can be found in the LICENSE file.
|
2010-05-28 07:22:49 +02:00
|
|
|
|
|
2010-05-14 05:29:53 +02:00
|
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bufio"
|
|
|
|
|
"flag"
|
|
|
|
|
"fmt"
|
|
|
|
|
"image"
|
2011-10-06 04:55:02 +02:00
|
|
|
|
"image/color"
|
2011-06-16 04:25:21 +02:00
|
|
|
|
"image/draw"
|
2010-05-14 05:29:53 +02:00
|
|
|
|
"image/png"
|
|
|
|
|
"io/ioutil"
|
|
|
|
|
"log"
|
|
|
|
|
"os"
|
2011-12-23 00:55:36 +01:00
|
|
|
|
|
|
|
|
|
"code.google.com/p/freetype-go/freetype"
|
2010-05-14 05:29:53 +02:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
|
dpi = flag.Int("dpi", 72, "screen resolution in Dots Per Inch")
|
|
|
|
|
fontfile = flag.String("fontfile", "../../luxi-fonts/luxisr.ttf", "filename of the ttf font")
|
2011-01-21 02:27:50 +01:00
|
|
|
|
size = flag.Float64("size", 12, "font size in points")
|
|
|
|
|
spacing = flag.Float64("spacing", 1.5, "line spacing (e.g. 2 means double spaced)")
|
2010-05-14 05:29:53 +02:00
|
|
|
|
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 {
|
2010-10-15 05:02:11 +02:00
|
|
|
|
log.Println(err)
|
2010-05-14 05:29:53 +02:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
font, err := freetype.ParseFont(fontBytes)
|
|
|
|
|
if err != nil {
|
2010-10-15 05:02:11 +02:00
|
|
|
|
log.Println(err)
|
2010-05-14 05:29:53 +02:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Initialize the context.
|
|
|
|
|
fg, bg := image.Black, image.White
|
2011-10-06 04:55:02 +02:00
|
|
|
|
ruler := color.RGBA{0xdd, 0xdd, 0xdd, 0xff}
|
2010-05-14 05:29:53 +02:00
|
|
|
|
if *wonb {
|
|
|
|
|
fg, bg = image.White, image.Black
|
2011-10-06 04:55:02 +02:00
|
|
|
|
ruler = color.RGBA{0x22, 0x22, 0x22, 0xff}
|
2010-05-14 05:29:53 +02:00
|
|
|
|
}
|
2011-09-14 14:02:41 +02:00
|
|
|
|
rgba := image.NewRGBA(image.Rect(0, 0, 640, 480))
|
2011-06-16 04:25:21 +02:00
|
|
|
|
draw.Draw(rgba, rgba.Bounds(), bg, image.ZP, draw.Src)
|
2010-05-28 07:22:49 +02:00
|
|
|
|
c := freetype.NewContext()
|
2010-05-14 05:29:53 +02:00
|
|
|
|
c.SetDPI(*dpi)
|
|
|
|
|
c.SetFont(font)
|
|
|
|
|
c.SetFontSize(*size)
|
2010-09-13 10:20:47 +02:00
|
|
|
|
c.SetClip(rgba.Bounds())
|
2010-09-09 11:22:48 +02:00
|
|
|
|
c.SetDst(rgba)
|
|
|
|
|
c.SetSrc(fg)
|
2010-05-14 05:29:53 +02:00
|
|
|
|
|
|
|
|
|
// Draw the guidelines.
|
|
|
|
|
for i := 0; i < 200; i++ {
|
|
|
|
|
rgba.Set(10, 10+i, ruler)
|
|
|
|
|
rgba.Set(10+i, 10, ruler)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Draw the text.
|
2010-08-27 07:49:35 +02:00
|
|
|
|
pt := freetype.Pt(10, 10+c.FUnitToPixelRU(font.UnitsPerEm()))
|
2010-05-14 05:29:53 +02:00
|
|
|
|
for _, s := range text {
|
2010-09-15 06:35:22 +02:00
|
|
|
|
_, err = c.DrawString(s, pt)
|
2010-05-14 05:29:53 +02:00
|
|
|
|
if err != nil {
|
2010-10-15 05:02:11 +02:00
|
|
|
|
log.Println(err)
|
2010-05-14 05:29:53 +02:00
|
|
|
|
return
|
|
|
|
|
}
|
2010-07-01 01:42:43 +02:00
|
|
|
|
pt.Y += c.PointToFix32(*size * *spacing)
|
2010-05-14 05:29:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Save that RGBA image to disk.
|
2011-04-11 02:48:00 +02:00
|
|
|
|
f, err := os.Create("out.png")
|
2010-05-14 05:29:53 +02:00
|
|
|
|
if err != nil {
|
2010-10-15 05:02:11 +02:00
|
|
|
|
log.Println(err)
|
2010-05-14 05:29:53 +02:00
|
|
|
|
os.Exit(1)
|
|
|
|
|
}
|
|
|
|
|
defer f.Close()
|
|
|
|
|
b := bufio.NewWriter(f)
|
|
|
|
|
err = png.Encode(b, rgba)
|
|
|
|
|
if err != nil {
|
2010-10-15 05:02:11 +02:00
|
|
|
|
log.Println(err)
|
2010-05-14 05:29:53 +02:00
|
|
|
|
os.Exit(1)
|
|
|
|
|
}
|
|
|
|
|
err = b.Flush()
|
|
|
|
|
if err != nil {
|
2010-10-15 05:02:11 +02:00
|
|
|
|
log.Println(err)
|
2010-05-14 05:29:53 +02:00
|
|
|
|
os.Exit(1)
|
|
|
|
|
}
|
|
|
|
|
fmt.Println("Wrote out.png OK.")
|
|
|
|
|
}
|