本文整理汇总了Golang中code/google/com/p/freetype-go/freetype/truetype.Parse函数的典型用法代码示例。如果您正苦于以下问题:Golang Parse函数的具体用法?Golang Parse怎么用?Golang Parse使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Parse函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: loadFont
func loadFont(path string) (*truetype.Font, error) {
file, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
return truetype.Parse(file)
}
示例2: main
func main() {
flag.Parse()
fmt.Printf("Loading fontfile %q\n", *fontfile)
b, err := ioutil.ReadFile(*fontfile)
if err != nil {
log.Println(err)
return
}
font, err := truetype.Parse(b)
if err != nil {
log.Println(err)
return
}
fupe := font.FUnitsPerEm()
printBounds(font.Bounds(fupe))
fmt.Printf("FUnitsPerEm:%d\n\n", fupe)
c0, c1 := 'A', 'V'
i0 := font.Index(c0)
hm := font.HMetric(fupe, i0)
g := truetype.NewGlyphBuf()
err = g.Load(font, fupe, i0, nil)
if err != nil {
log.Println(err)
return
}
fmt.Printf("'%c' glyph\n", c0)
fmt.Printf("AdvanceWidth:%d LeftSideBearing:%d\n", hm.AdvanceWidth, hm.LeftSideBearing)
printGlyph(g)
i1 := font.Index(c1)
fmt.Printf("\n'%c', '%c' Kerning:%d\n", c0, c1, font.Kerning(fupe, i0, i1))
}
示例3: getFont
func getFont(path string, sizePts int) font {
sizePx := int(float64(sizePts)/ptInch*pxInch + 0.5)
if f, ok := fonts[path]; ok {
f.size = sizePx
return f
}
in, err := os.Open(path)
if err != nil {
panic(err)
}
defer in.Close()
fdata, err := ioutil.ReadAll(in)
if err != nil {
panic(err)
}
f := font{
path: path,
Context: freetype.NewContext(),
}
if f.Font, err = truetype.Parse(fdata); err != nil {
panic(err)
}
f.SetFont(f.Font)
f.SetDPI(pxInch)
fonts[path] = f
f.size = sizePx
return f
}
示例4: GenerateCaptcha
/*
生成验证码
bg:背景色
fg:前景色
length:字符长度
width:宽度
height:高度
size:字体大小
fontPath:字体文件路径
*/
func GenerateCaptcha(bg, fg *image.Uniform, length int, width int, height int, size float64, fontPath string) *Captcha {
fontBytes, err := ioutil.ReadFile(fontPath)
if err != nil {
panic(err)
}
font, err := truetype.Parse(fontBytes)
if err != nil {
panic(err)
}
cap := &Captcha{}
cap.Text = randString(length)
cap.Image = image.NewRGBA(image.Rect(0, 0, width, height))
draw.Draw(cap.Image, cap.Image.Bounds(), bg, image.ZP, draw.Src)
c := freetype.NewContext()
c.SetFont(font)
c.SetFontSize(size)
c.SetClip(cap.Image.Bounds())
c.SetDst(cap.Image)
c.SetSrc(fg)
pt := freetype.Pt(0, int(c.PointToFix32(size)>>8))
for _, s := range cap.Text {
_, err = c.DrawString(string(s), pt)
if err != nil {
panic(err)
return nil
}
pt.X += c.PointToFix32(size * 0.5)
}
return cap
}
示例5: LoadFont
// LoadFont loads the given truetype font file data and returns a new
// *TruetypeFont. For easier loading, consider using LoadTTFFile instead. This
// method is simply short-hand for:
// tt, err := truetype.Parse(data)
// if err != nil {
// return nil, err
// }
// return &TruetypeFont{
// Font: tt,
// }
func LoadFont(data []byte) (*TruetypeFont, error) {
f, err := truetype.Parse(data)
if err != nil {
return nil, err
}
return &TruetypeFont{
Font: f,
}, nil
}
示例6: init
func init() {
font, err := truetype.Parse(luxisr_ttf())
if err != nil {
// TODO: log error
println("error!")
println(err.Error())
}
draw2d.RegisterFont(DefaultFontData, font)
}
示例7: LoadFont
func LoadFont(path string) (*truetype.Font, error) {
bs, err := ioutil.ReadFile(path)
// quick debug
if err != nil {
fmt.Println(err)
}
f, err := truetype.Parse(bs)
return f, err
}
示例8: loadFont
func loadFont(fontFileName string) *truetype.Font {
fontBytes, err := ioutil.ReadFile(path.Join(fontFolder, fontFileName))
if err != nil {
log.Println(err)
return nil
}
font, err := truetype.Parse(fontBytes)
if err != nil {
log.Println(err)
return nil
}
return font
}
示例9: AddFont
//AddFont will add a new font file to the list
func (fm *FontManager) AddFont(pathToFontFile string) error {
fontBytes, err := ioutil.ReadFile(pathToFontFile)
if err != nil {
return err
}
font, err := truetype.Parse(fontBytes)
if err != nil {
return err
}
fm.fontFiles = append(fm.fontFiles, pathToFontFile)
fm.fontObjects[pathToFontFile] = font
return nil
}
示例10: loadFont
func loadFont() (*truetype.Font, error) {
f, err := os.Open(filepath.Join(datadir, "fonts", "luxisr.ttf"))
if err != nil {
return nil, err
}
data, err := ioutil.ReadAll(f)
f.Close()
if err != nil {
return nil, err
}
font, err := truetype.Parse(data)
if err != nil {
return nil, err
}
return font, nil
}
示例11: defaultFont
func defaultFont() *truetype.Font {
goroot := os.Getenv("GOROOT")
if goroot == "" {
log.Fatal("no goroot set")
}
path := goroot + "/src/pkg/freetype-go.googlecode.com/hg/luxi-fonts/luxisr.ttf"
// Read the font data.
fontBytes, err := ioutil.ReadFile(path)
if err != nil {
log.Fatal(err)
}
font, err := truetype.Parse(fontBytes)
if err != nil {
log.Fatal(err)
}
return font
}
示例12: analyzeFont
func analyzeFont(ttfFile string) SortedGS {
defer trackTime(time.Now(), "analyze_font")
f, err := ioutil.ReadFile(ttfFile)
if err != nil {
log.Fatal(err)
}
font, err := truetype.Parse(f)
if err != nil {
log.Fatal(err)
}
a := make(SortedGS, len(*alphabet))
for i, char := range *alphabet {
rgba := getRGBA(string(char), font)
nbp := getNumBlackPixels(rgba)
a[i] = &Artifact{string(char), nbp, nbp}
}
a.Normalize()
sort.Sort(a)
a = removeDuplicates(a)
//saveCharacterMap(a)
return a
}
示例13: testFontFile
func testFontFile(fontfile string) (*truetype.Font, error) {
fontfile = LocateFont(fontfile)
fmt.Printf("Testing fontfile %q\n", fontfile)
b, err := ioutil.ReadFile(fontfile)
if err != nil {
return nil, err
}
font, err := truetype.Parse(b)
if err != nil {
return nil, err
}
fupe := font.FUnitsPerEm()
printBounds(font.Bounds(font.FUnitsPerEm()))
fmt.Printf("FUnitsPerEm:%d\n\n", fupe)
// printGlyph(font, 'A', fupe)
printGlyph(font, 'V', fupe)
// printGlyph(font, 'a', fupe)
// printGlyph(font, 'v', fupe)
return font, nil
}
示例14: newFont
func newFont(data []byte, size int) (*font, error) {
ttf, err := truetype.Parse(data)
if err != nil {
return nil, err
}
scale := int32(size << 6)
bounds := ttf.Bounds(scale)
glyphMaxSizeDips := math.Size{
W: int(bounds.XMax-bounds.XMin) >> 6,
H: int(bounds.YMax-bounds.YMin) >> 6,
}
ascentDips := int(bounds.YMax >> 6)
return &font{
size: size,
scale: scale,
glyphMaxSizeDips: glyphMaxSizeDips,
ascentDips: ascentDips,
ttf: ttf,
resolutions: make(map[resolution]*glyphTable),
glyphs: make(map[rune]*glyph),
}, nil
}
示例15: ParseFont
// ParseFont just calls the Parse function from the freetype/truetype package.
// It is provided here so that code that imports this package doesn't need
// to also include the freetype/truetype package.
func ParseFont(b []byte) (*truetype.Font, error) {
return truetype.Parse(b)
}