67 lines
1.2 KiB
Go
67 lines
1.2 KiB
Go
package main
|
|
|
|
import (
|
|
"bufio"
|
|
"fmt"
|
|
"image"
|
|
"image/draw"
|
|
"image/png"
|
|
"io/ioutil"
|
|
"log"
|
|
"os"
|
|
|
|
"github.com/golang/freetype"
|
|
)
|
|
|
|
func main() {
|
|
|
|
// Read the font data.
|
|
fontBytes, err := ioutil.ReadFile("../NewYork.ttf")
|
|
if err != nil {
|
|
log.Println(err)
|
|
return
|
|
}
|
|
//produce the fonttype
|
|
f, err := freetype.ParseFont(fontBytes)
|
|
if err != nil {
|
|
log.Println(err)
|
|
return
|
|
}
|
|
|
|
fg, bg := image.Black, image.White
|
|
rgba := image.NewRGBA(image.Rect(0, 0, 640, 480))
|
|
draw.Draw(rgba, rgba.Bounds(), bg, image.ZP, draw.Src)
|
|
|
|
c := freetype.NewContext()
|
|
// 设置像素密度
|
|
c.SetDPI(140)
|
|
// 指定字体
|
|
c.SetFont(f)
|
|
// 指定字体大小
|
|
c.SetFontSize(140)
|
|
// 指定画布对象
|
|
c.SetDst(rgba)
|
|
// 指定画布绘制范围
|
|
c.SetClip(rgba.Bounds())
|
|
// 指定文字颜色
|
|
c.SetSrc(fg)
|
|
|
|
pt := freetype.Pt(10, 10+int(c.PointToFixed(140)>>6))
|
|
//add chinese line
|
|
c.DrawString("B", pt)
|
|
// Save the output to file
|
|
of, err := os.Create("out.png")
|
|
if err != nil {
|
|
fmt.Println(err.Error())
|
|
}
|
|
defer of.Close()
|
|
b := bufio.NewWriter(of)
|
|
if err := png.Encode(b, rgba); err != nil {
|
|
fmt.Println(err.Error())
|
|
}
|
|
if err := b.Flush(); err != nil {
|
|
fmt.Println(err.Error())
|
|
}
|
|
|
|
}
|