本文整理匯總了Golang中github.com/golang/freetype.ParseFont函數的典型用法代碼示例。如果您正苦於以下問題:Golang ParseFont函數的具體用法?Golang ParseFont怎麽用?Golang ParseFont使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了ParseFont函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: initTextures
func initTextures() error {
var textureUnit uint32 = gl.TEXTURE0
var err error
boardTexture, err = createAssetTexture(textureUnit, "texture.png")
if err != nil {
return err
}
textureUnit++
plain, err := freetype.ParseFont(asset.MustAsset("CPMono_v07 Plain.ttf"))
if err != nil {
return err
}
bold, err := freetype.ParseFont(asset.MustAsset("CPMono_v07 Bold.ttf"))
if err != nil {
return err
}
makeText := func(text string, font *truetype.Font, size int, color color.Color) (rt *renderableText) {
if err != nil {
return nil
}
rt, err = createText(text, size, color, font, textureUnit)
textureUnit++
return
}
for id, text := range game.MenuTitleText {
menuTitleText[id] = makeText(text, plain, menuTitleFontSize, menuTitleTextColor)
}
for id, text := range game.MenuItemText {
menuItemText[id] = makeText(text, plain, menuItemFontSize, menuItemTextColor)
}
for id, text := range game.MenuChoiceText {
menuChoiceText[id] = makeText(text, plain, menuItemFontSize, menuItemTextColor)
}
for i, v := range game.HUDItemText {
hudItemText[i] = makeText(v, bold, hudFontSize, hudTextColor)
}
for _, v := range menuRuneStrs {
menuRuneText[[]rune(v)[0]] = makeText(v, plain, menuItemFontSize, menuItemTextColor)
}
for _, v := range hudRuneStrs {
hudRuneText[[]rune(v)[0]] = makeText(v, bold, hudFontSize, hudTextColor)
}
for _, v := range markerRuneStrs {
markerRuneText[[]rune(v)[0]] = makeText(v, bold, markerFontSize, markerTextColor)
}
if err != nil {
return err
}
return nil
}
示例2: BenchmarkDrawTile
func BenchmarkDrawTile(b *testing.B) {
nwPt := Point{-4.482421875, 54.162433968067795}
sePt := Point{-4.471435546875, 54.156001090284924}
scale := int64(15)
// Read font
font_, err := ioutil.ReadFile("example/FiraSans-Regular.ttf")
if err != nil {
b.Fatalf("Benchmark setup failed: %#v\n", err)
}
font, err := freetype.ParseFont(font_)
if err != nil {
b.Fatalf("Benchmark setup failed: %#v\n", err)
}
// Read PBF file
data, err := ParsePbf("example/isle-of-man-latest.osm.pbf")
if err != nil {
b.Fatalf("Benchmark setup failed: %#v\n", err)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err := DrawTile(nwPt, sePt, scale, font, data, false)
if err != nil {
b.Fatalf("Received error: %#v\n", err)
}
}
}
示例3: NewFontFace
func NewFontFace(path string, size float64, fg, bg color.Color) (fontface *FontFace, err error) {
var (
font *truetype.Font
fontbytes []byte
bounds truetype.Bounds
context *freetype.Context
scale float32
)
if fontbytes, err = ioutil.ReadFile(path); err != nil {
return
}
if font, err = freetype.ParseFont(fontbytes); err != nil {
return
}
bounds = font.Bounds(1)
fmt.Printf("bounds %v\n", bounds)
context = freetype.NewContext()
context.SetFont(font)
context.SetFontSize(size)
context.SetDPI(72)
scale = float32(context.PointToFixed(size) / 64)
fontface = &FontFace{
font: font,
charw: scale * float32(bounds.XMax-bounds.XMin),
charh: scale * float32(bounds.YMax-bounds.YMin),
fg: fg,
bg: bg,
context: context,
}
return
}
示例4: drawStringImage
func (this *Signer) drawStringImage(text string) (image.Image, error) {
fontBytes, err := ioutil.ReadFile(this.fontPath)
if err != nil {
fmt.Println(err)
}
font, err := freetype.ParseFont(fontBytes)
if err != nil {
fmt.Println(err)
}
rgba := image.NewRGBA(image.Rect(0, 0, 900, 900))
draw.Draw(rgba, rgba.Bounds(), image.Black, image.ZP, draw.Src)
c := freetype.NewContext()
c.SetDPI(this.dpi)
c.SetFontSize(this.fontSize)
c.SetClip(rgba.Bounds())
c.SetDst(rgba)
c.SetSrc(image.White)
c.SetFont(font)
pt := freetype.Pt(100, 100+int(c.PointToFixed(this.fontSize)>>8))
for _, s := range strings.Split(text, "\r\n") {
_, err = c.DrawString(s, pt)
pt.Y += c.PointToFixed(12 * 1.5)
}
return rgba, nil
}
示例5: getFont
// getFont returns the truetype.Font for the given font name or an error.
func getFont(name string) (*truetype.Font, error) {
fontLock.RLock()
f, ok := loadedFonts[name]
fontLock.RUnlock()
if ok {
return f, nil
}
path, err := fontPath(name)
if err != nil {
return nil, err
}
file, err := os.Open(path)
if err != nil {
return nil, errors.New("Failed to open font file: " + err.Error())
}
defer file.Close()
bytes, err := ioutil.ReadAll(file)
if err != nil {
return nil, errors.New("Failed to read font file: " + err.Error())
}
font, err := freetype.ParseFont(bytes)
if err == nil {
fontLock.Lock()
loadedFonts[name] = font
fontLock.Unlock()
} else {
err = errors.New("Failed to parse font file: " + err.Error())
}
return font, err
}
示例6: init
func (a *Annotator) init() error {
// load the font
fontBytes, err := resources.Asset(fontfile)
if err != nil {
return err
}
luxisr, err := freetype.ParseFont(fontBytes)
if err != nil {
return err
}
// Initialize the context.
fg := image.White
a.context = freetype.NewContext()
a.context.SetDPI(dpi)
a.context.SetFont(luxisr)
a.context.SetFontSize(size)
a.context.SetClip(a.image.Bounds())
a.context.SetDst(a.image)
a.context.SetSrc(fg)
switch hinting {
default:
a.context.SetHinting(font.HintingNone)
case "full":
a.context.SetHinting(font.HintingFull)
}
return nil
}
示例7: NewTileHandler
// prefix should be of the form "/tiles" (without the trailing slash)
func NewTileHandler(prefix, pbfPath, fontPath string) *TileHandler {
// Read font
log.Println("Parsing font file...")
font_, err := ioutil.ReadFile(fontPath)
if err != nil {
panic(err)
}
font, err := freetype.ParseFont(font_)
if err != nil {
panic(err)
}
log.Println("Parsing font file... [DONE]")
// Read PBF
log.Println("Parsing PBF file...")
osmData, err := ParsePbf(pbfPath)
if err != nil {
panic(err)
}
log.Println("Parsing PBF file... [DONE]")
return &TileHandler{
prefix: prefix,
font: font,
data: osmData,
}
}
示例8: newDrawer
func newDrawer(fontFile string) (*drawer, error) {
if fontFile == "" {
return nil, errFontRequired
}
g := new(drawer)
g.fontSize = 75.0
g.dpi = 72.0
g.fontHinting = font.HintingNone
ttf, err := getTTF(fontFile)
if err != nil {
return nil, errInvalidTTF
}
g.face = truetype.NewFace(ttf, &truetype.Options{
Size: g.fontSize,
DPI: g.dpi,
Hinting: g.fontHinting,
})
fontBytes, err := ioutil.ReadFile(fontFile)
if err != nil {
return nil, errInvalidTTF
}
font, err := freetype.ParseFont(fontBytes)
if err != nil {
return nil, errInvalidTTF
}
g.font = font
return g, nil
}
示例9: NewFontFace
func NewFontFace(path string, pixels float32, fg, bg color.Color) (fontface *FontFace, err error) {
var (
font *truetype.Font
fontbytes []byte
bounds fixed.Rectangle26_6
context *freetype.Context
points float32
dpi float32 = 96
)
if fontbytes, err = ioutil.ReadFile(path); err != nil {
return
}
if font, err = freetype.ParseFont(fontbytes); err != nil {
return
}
points = pixels * 72 / dpi
bounds = font.Bounds(fixed.I(int(pixels)))
context = freetype.NewContext()
context.SetFont(font)
context.SetFontSize(float64(points))
context.SetDPI(float64(dpi))
fontface = &FontFace{
font: font,
charw: float32(bounds.Max.X-bounds.Min.X) / 64,
charh: float32(bounds.Max.Y-bounds.Min.Y) / 64,
offy: float32(bounds.Min.Y) / 64,
fg: fg,
bg: bg,
context: context,
}
return
}
示例10: loadFont
func (c Cache) loadFont(path string, info os.FileInfo, err error) error {
// process ttf files only
if strings.ToLower(path[len(path)-4:]) != ".ttf" {
return nil
}
ttfBytes, err := ioutil.ReadFile(path)
if err != nil {
log.Fatal(err)
}
font, err := freetype.ParseFont(ttfBytes)
if err != nil {
log.Fatal(err)
}
name, style := strings.ToLower(font.Name(truetype.NameIDFontFamily)), strings.ToLower(font.Name(truetype.NameIDFontSubfamily))
if stylemap, ok := c[name]; !ok {
stylemap := make(Styles)
c[name] = stylemap
stylemap[style] = font
} else {
stylemap[style] = font
}
return nil
}
示例11: init
func init() {
var err error
defaultFont, err = freetype.ParseFont(defaultFontData())
if err != nil {
panic(err)
}
}
示例12: loadFont
func loadFont(r Resource) (*truetype.Font, error) {
ttfBytes, err := ioutil.ReadFile(r.url)
if err != nil {
return nil, err
}
return freetype.ParseFont(ttfBytes)
}
示例13: makeImage
func makeImage(req *http.Request, caption, font string, pt, size, border, scale int, f func(x, y int) uint32) *image.RGBA {
d := (size + 2*border) * scale
csize := 0
if caption != "" {
if pt == 0 {
pt = 11
}
csize = pt * 2
}
c := image.NewRGBA(image.Rect(0, 0, d, d+csize))
// white
u := &image.Uniform{C: color.White}
draw.Draw(c, c.Bounds(), u, image.ZP, draw.Src)
for y := 0; y < size; y++ {
for x := 0; x < size; x++ {
r := image.Rect((x+border)*scale, (y+border)*scale, (x+border+1)*scale, (y+border+1)*scale)
rgba := f(x, y)
u.C = color.RGBA{byte(rgba >> 24), byte(rgba >> 16), byte(rgba >> 8), byte(rgba)}
draw.Draw(c, r, u, image.ZP, draw.Src)
}
}
if csize != 0 {
if font == "" {
font = "data/luxisr.ttf"
}
ctxt := fs.NewContext(req)
dat, _, err := ctxt.Read(font)
if err != nil {
panic(err)
}
tfont, err := freetype.ParseFont(dat)
if err != nil {
panic(err)
}
ft := freetype.NewContext()
ft.SetDst(c)
ft.SetDPI(100)
ft.SetFont(tfont)
ft.SetFontSize(float64(pt))
ft.SetSrc(image.NewUniform(color.Black))
ft.SetClip(image.Rect(0, 0, 0, 0))
wid, err := ft.DrawString(caption, freetype.Pt(0, 0))
if err != nil {
panic(err)
}
p := freetype.Pt(d, d+3*pt/2)
p.X -= wid.X
p.X /= 2
ft.SetClip(c.Bounds())
ft.DrawString(caption, p)
}
return c
}
示例14: main
func main() {
fontBytes, err := ioutil.ReadFile("luxisr.ttf")
if err != nil {
log.Fatal(err)
}
font, err := freetype.ParseFont(fontBytes)
if err != nil {
log.Fatal(err)
}
fg, bg := image.White, image.Black
rgba := image.NewRGBA(image.Rect(0, 0, width, height))
draw.Draw(rgba, rgba.Bounds(), bg, image.ZP, draw.Src)
c := freetype.NewContext()
c.SetDPI(dpi)
c.SetFont(font)
c.SetFontSize(size)
c.SetClip(rgba.Bounds())
c.SetDst(rgba)
c.SetSrc(fg)
c.SetHinting(freetype.FullHinting)
count := 0
l := NewLife(width, height)
for {
if count%10 == 0 {
count = 0
pt := freetype.Pt(rand.Intn(width-width/4), height/4+rand.Intn(height-height/4))
c.DrawString("NOPE", pt)
for x := 0; x < width; x++ {
for y := 0; y < height; y++ {
c := rgba.RGBAAt(x, y)
l.a.Set(x, y, c.R > 0 || c.B > 0 || c.G > 0)
}
}
}
count++
for x := 0; x < width; x++ {
for y := 0; y < height; y++ {
var c color.Color = color.Black
if l.a.Alive(x, y) {
c = rainbow(y)
}
rgba.Set(x, y, c)
}
}
//fmt.Println(l)
sendImage(rgba)
l.Step()
time.Sleep(time.Second / 8)
}
}
示例15: ReadFont
// ReadFont parses the data read from r as a truetype font.
func ReadFont(r io.Reader) (*truetype.Font, error) {
ttfraw, err := ioutil.ReadAll(r)
if err != nil {
return nil, fmt.Errorf("read: %v", err)
}
font, err := freetype.ParseFont(ttfraw)
if err != nil {
return nil, err
}
return font, err
}