| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
package main |
| 13 |
|
| 14 |
import ( |
| 15 |
"bufio" |
| 16 |
"flag" |
| 17 |
"fmt" |
| 18 |
"image" |
| 19 |
"image/color" |
| 20 |
"image/draw" |
| 21 |
"image/png" |
| 22 |
"io/ioutil" |
| 23 |
"log" |
| 24 |
"os" |
| 25 |
|
| 26 |
"github.com/golang/freetype" |
| 27 |
"golang.org/x/image/font" |
| 28 |
) |
| 29 |
|
| 30 |
var ( |
| 31 |
dpi = flag.Float64("dpi", 72, "screen resolution in Dots Per Inch") |
| 32 |
fontfile = flag.String("fontfile", "../../testdata/luxisr.ttf", "filename of the ttf font") |
| 33 |
hinting = flag.String("hinting", "none", "none | full") |
| 34 |
size = flag.Float64("size", 12, "font size in points") |
| 35 |
spacing = flag.Float64("spacing", 1.5, "line spacing (e.g. 2 means double spaced)") |
| 36 |
wonb = flag.Bool("whiteonblack", false, "white text on a black background") |
| 37 |
) |
| 38 |
|
| 39 |
var text = []string{ |
| 40 |
"’Twas brillig, and the slithy toves", |
| 41 |
"Did gyre and gimble in the wabe;", |
| 42 |
"All mimsy were the borogoves,", |
| 43 |
"And the mome raths outgrabe.", |
| 44 |
"", |
| 45 |
"“Beware the Jabberwock, my son!", |
| 46 |
"The jaws that bite, the claws that catch!", |
| 47 |
"Beware the Jubjub bird, and shun", |
| 48 |
"The frumious Bandersnatch!”", |
| 49 |
"", |
| 50 |
"He took his vorpal sword in hand:", |
| 51 |
"Long time the manxome foe he sought—", |
| 52 |
"So rested he by the Tumtum tree,", |
| 53 |
"And stood awhile in thought.", |
| 54 |
"", |
| 55 |
"And as in uffish thought he stood,", |
| 56 |
"The Jabberwock, with eyes of flame,", |
| 57 |
"Came whiffling through the tulgey wood,", |
| 58 |
"And burbled as it came!", |
| 59 |
"", |
| 60 |
"One, two! One, two! and through and through", |
| 61 |
"The vorpal blade went snicker-snack!", |
| 62 |
"He left it dead, and with its head", |
| 63 |
"He went galumphing back.", |
| 64 |
"", |
| 65 |
"“And hast thou slain the Jabberwock?", |
| 66 |
"Come to my arms, my beamish boy!", |
| 67 |
"O frabjous day! Callooh! Callay!”", |
| 68 |
"He chortled in his joy.", |
| 69 |
"", |
| 70 |
"’Twas brillig, and the slithy toves", |
| 71 |
"Did gyre and gimble in the wabe;", |
| 72 |
"All mimsy were the borogoves,", |
| 73 |
"And the mome raths outgrabe.", |
| 74 |
} |
| 75 |
|
| 76 |
func main() { |
| 77 |
flag.Parse() |
| 78 |
|
| 79 |
|
| 80 |
fontBytes, err := ioutil.ReadFile(*fontfile) |
| 81 |
if err != nil { |
| 82 |
log.Println(err) |
| 83 |
return |
| 84 |
} |
| 85 |
f, err := freetype.ParseFont(fontBytes) |
| 86 |
if err != nil { |
| 87 |
log.Println(err) |
| 88 |
return |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
fg, bg := image.Black, image.White |
| 93 |
ruler := color.RGBA{0xdd, 0xdd, 0xdd, 0xff} |
| 94 |
if *wonb { |
| 95 |
fg, bg = image.White, image.Black |
| 96 |
ruler = color.RGBA{0x22, 0x22, 0x22, 0xff} |
| 97 |
} |
| 98 |
rgba := image.NewRGBA(image.Rect(0, 0, 640, 480)) |
| 99 |
draw.Draw(rgba, rgba.Bounds(), bg, image.ZP, draw.Src) |
| 100 |
c := freetype.NewContext() |
| 101 |
c.SetDPI(*dpi) |
| 102 |
c.SetFont(f) |
| 103 |
c.SetFontSize(*size) |
| 104 |
c.SetClip(rgba.Bounds()) |
| 105 |
c.SetDst(rgba) |
| 106 |
c.SetSrc(fg) |
| 107 |
switch *hinting { |
| 108 |
default: |
| 109 |
c.SetHinting(font.HintingNone) |
| 110 |
case "full": |
| 111 |
c.SetHinting(font.HintingFull) |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
for i := 0; i < 200; i++ { |
| 116 |
rgba.Set(10, 10+i, ruler) |
| 117 |
rgba.Set(10+i, 10, ruler) |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
pt := freetype.Pt(10, 10+int(c.PointToFixed(*size)>>6)) |
| 122 |
for _, s := range text { |
| 123 |
_, err = c.DrawString(s, pt) |
| 124 |
if err != nil { |
| 125 |
log.Println(err) |
| 126 |
return |
| 127 |
} |
| 128 |
pt.Y += c.PointToFixed(*size * *spacing) |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
outFile, err := os.Create("out.png") |
| 133 |
if err != nil { |
| 134 |
log.Println(err) |
| 135 |
os.Exit(1) |
| 136 |
} |
| 137 |
defer outFile.Close() |
| 138 |
b := bufio.NewWriter(outFile) |
| 139 |
err = png.Encode(b, rgba) |
| 140 |
if err != nil { |
| 141 |
log.Println(err) |
| 142 |
os.Exit(1) |
| 143 |
} |
| 144 |
err = b.Flush() |
| 145 |
if err != nil { |
| 146 |
log.Println(err) |
| 147 |
os.Exit(1) |
| 148 |
} |
| 149 |
fmt.Println("Wrote out.png OK.") |
| 150 |
} |