This is a creation in Article, where the information may have evolved or changed.
Previously introduced using Freetype-go to draw words (http://www.cnblogs.com/ghj1976/p/3445568.html), now the relevant package has been integrated, mainly integrated into the github.com/golang/ FreeType and Golang.org/x/image/font here, so there's this blog post.
Example code in: Https://github.com/golang/freetype/blob/master/example/freetype/main.go
Package Main
Import (
"Bufio"
"Flag"
"FMT"
"Image"
"Image/color"
"Image/draw"
"Image/png"
"Io/ioutil"
"Log"
"OS"
"Github.com/golang/freetype"
"Golang.org/x/image/font"
)
VAR (
DPI = flag. Float64 ("dpi", "page 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", "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",
"Text ......& ^_^ in the test",
"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 He 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: = FreeType. Parsefont (Fontbytes)
If err! = Nil {
Log. PRINTLN (ERR)
Return
}
Initialize the context.
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)
c: = FreeType. Newcontext ()
C.SETDPI (*DPI)
C.setfont (f)
C.setfontsize (*size)
C.setclip (Rgba. Bounds ())
C.SETDST (RGBA)
C.SETSRC (FG)
Switch *hinting {
Default
C.sethinting (font. Hintingnone)
Case ' full ':
C.sethinting (font. Hintingfull)
}
Draw the guidelines.
For I: = 0; I < 200; i++ {
Rgba. Set (Ten, 10+i, ruler)
Rgba. Set (10+i, ten, ruler)
}
//Draw the text.
pt: = FreeType. Pt (10+int (c.pointtofixed (*size) >>6))
for _, S: = Range text {
& nbsp; _, Err = C.drawstring (S, PT)
if err! = Nil {
&N bsp; log. PRINTLN (Err)
return
}
pt. Y + = c.pointtofixed (*size * *spacing)
}
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.")
}
The following results are performed:
./testfont2-fontfile= "Font Butler Amoy body. TTF"
Execution effect:
./testfont2-fontfile= "6f14c8f349377d5ebe2b0a5e2851a6f6.ttf"
Execution effect:
./testfont2-fontfile= "Sliced by Hand.ttf"
Execution effect, this font does not support Chinese, the following situation will occur:
Some free font library download
English Font Library
http://www.freefontspro.com/cn/
http://cn.fontriver.com/
Chinese Font Library
http://fonts.mobanwang.com/
http://www.touwenzi.com/