func TestLoadFonts(t *testing.T) { if _, err := freetype.ParseFont(Default()); err != nil { t.Fatalf("default font: %v", err) } if _, err := freetype.ParseFont(Monospace()); err != nil { t.Fatalf("monospace font: %v", err) } }
// 画一个带有text的图片 func drawStringImage(text string, fontFile string) image.Image { fontBytes, err := ioutil.ReadFile(fontFile) if err != nil { log.Fatalln(err) } font, err := freetype.ParseFont(fontBytes) if err != nil { log.Fatalln(err) } 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(72) c.SetFont(font) c.SetFontSize(12) c.SetClip(rgba.Bounds()) c.SetDst(rgba) c.SetSrc(fg) // Draw the text. pt := freetype.Pt(10, 10+int(c.PointToFix32(12)>>8)) for _, s := range strings.Split(text, "\r\n") { _, err = c.DrawString(s, pt) pt.Y += c.PointToFix32(12 * 1.5) } return rgba }
func (a *Annotator) init() error { // load the font fontBytes, err := resources.Asset(fontfile) if err != nil { return err } font, 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(font) 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(freetype.NoHinting) case "full": a.context.SetHinting(freetype.FullHinting) } return nil }
// getFont returns the truetype.Font for the given font // name. func getFont(name string) (*truetype.Font, error) { if f, ok := loadedFonts[name]; ok { return f, nil } n, ok := FontMap[name] if !ok { return nil, fmt.Errorf("No matching font: %s", name) } pkg, err := build.Import(importString, "", build.FindOnly) if err != nil { return nil, err } path := filepath.Join(pkg.Dir, "fonts", n+".ttf") file, err := os.Open(path) if err != nil { return nil, err } defer file.Close() bytes, err := ioutil.ReadAll(file) if err != nil { return nil, err } font, err := freetype.ParseFont(bytes) if err != nil { loadedFonts[name] = font } return font, err }
func (t *Tiler) setupDrawer() { t.bondFudgeX = int(Floor(float64(t.TileWidth) / 80)) t.bondFudgeY = int(Floor(float64(t.TileHeight) / 80)) t.fontSize = int(Sqrt(float64(t.TileHeight*t.TileWidth)) / 4) t.tileHorizShift = int(float64(t.TileWidth) / 10) t.tileVertShift = int(float64(t.TileHeight) / 10) t.tileHorizMargin = int(float64(t.TileWidth) / 20) t.tileVertMargin = int(float64(t.TileHeight) / 20) bytes, err := ioutil.ReadFile(t.FontPath) if err != nil { log.Panicf("Couldn't read font: %s", err) } t.font, err = freetype.ParseFont(bytes) if err != nil { log.Panicf("Couldn't parse font: %s", err) } // figure out how big a representative character is for approximate layout purposes ex := '5' fupe := t.font.FUnitsPerEm() horiz := t.font.HMetric(fupe, t.font.Index(ex)) vert := t.font.VMetric(fupe, t.font.Index(ex)) t.fontX = int(horiz.LeftSideBearing) t.fontWidth = int(horiz.AdvanceWidth) t.fontY = int(vert.TopSideBearing) t.fontHeight = int(vert.AdvanceHeight) log.Printf("%#v", t) }
func init() { var err error defaultFont, err = freetype.ParseFont(defaultFontData()) if err != nil { panic(err) } }
// 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 }
func (q *Quote) TextToImage() *image.RGBA { spacing := 1.5 var fontsize float64 = 8 // read font fontBytes, err := ioutil.ReadFile(os.Getenv("FONT_FILE")) checkErr(err) font, err := freetype.ParseFont(fontBytes) checkErr(err) // Initialize the context. fg, bg := image.White, image.Transparent // 755px by 378px is the size Vox uses rgba := image.NewRGBA(image.Rect(0, 0, 755, 378)) draw.Draw(rgba, rgba.Bounds(), bg, image.ZP, draw.Src) c := freetype.NewContext() c.SetDPI(300) c.SetFont(font) c.SetFontSize(fontsize) c.SetClip(rgba.Bounds()) c.SetDst(rgba) c.SetSrc(fg) // Draw the text pt := freetype.Pt(50, 50+int(c.PointToFix32(fontsize)>>8)) lines := strings.Split(q.Text, "\n") for _, s := range lines { _, err = c.DrawString(s, pt) checkErr(err) pt.Y += c.PointToFix32(fontsize * spacing) } return rgba }
// TODO(crawshaw): It looks like we need a gl.RegisterInit feature. // TODO(crawshaw): The gldebug mode needs to complain loudly when GL functions // are called before init, because often they fail silently. func fpsInit() { font := "" switch runtime.GOOS { case "android": font = "/system/fonts/DroidSansMono.ttf" case "darwin": font = "/Library/Fonts/Andale Mono.ttf" case "linux": font = "/usr/share/fonts/truetype/droid/DroidSansMono.ttf" default: panic(fmt.Sprintf("go.mobile/app/debug: unsupported runtime.GOOS %q", runtime.GOOS)) } b, err := ioutil.ReadFile(font) if err != nil { panic(err) } f, err := freetype.ParseFont(b) if err != nil { panic(err) } monofont.SetFont(f) monofont.SetSrc(image.Black) monofont.SetHinting(freetype.FullHinting) toPx := func(x geom.Pt) int { return int(math.Ceil(float64(geom.Pt(x).Px()))) } fps.Image = glutil.NewImage(toPx(50), toPx(12)) monofont.SetDst(fps.Image.RGBA) monofont.SetClip(fps.Bounds()) monofont.SetDPI(72 * float64(geom.PixelsPerPt)) monofont.SetFontSize(12) }
func builtInFont() *truetype.Font { font, err := freetype.ParseFont(misc.DejavusansTtf) if err != nil { logger.Warning.Printf("Could not parse default font because: %v", err) return nil } return font }
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 }
func convertString2image(s string) (*image.RGBA, error) { if debug { fmt.Println("convertString2image") } dpi := float64(72.0) //fontfile := "../font/MS Gothic.ttf" //fontfile := "../font/VL.ttf" fontfile := fontName hinting := "none" size := float64(17) spacing := float64(0) // Read the font data. fontBytes, err := ioutil.ReadFile(fontfile) if err != nil { log.Println(err) return nil, err } font, err := freetype.ParseFont(fontBytes) if err != nil { log.Println(err) return nil, err } // Initialize the context. fg, bg := image.Black, image.White rgba := image.NewRGBA(image.Rect(0, 0, 16, 16)) 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) switch hinting { default: c.SetHinting(freetype.NoHinting) case "full": c.SetHinting(freetype.FullHinting) } // Draw the text. pt := freetype.Pt(-1, -3+int(c.PointToFix32(size)>>8)) _, err = c.DrawString(s, pt) if err != nil { log.Println(err) return nil, err } pt.Y += c.PointToFix32(size * spacing) return rgba, nil }
func init() { // Load the font. fontBytes, err := ioutil.ReadFile("Impact.ttf") if err != nil { panic(fmt.Sprint("Error reading font: %s", err)) } font, err = freetype.ParseFont(fontBytes) if err != nil { panic(fmt.Sprintf("Error parsing font: %s", err)) } }
func initFont(fontPath string) (*truetype.Font, error) { fontBytes, err := ioutil.ReadFile(fontPath) if err != nil { return nil, err } font, err := freetype.ParseFont(fontBytes) if err != nil { return nil, err } return font, nil }
func GetFont(location string) (*truetype.Font, error) { fontBytes, err := ioutil.ReadFile(location) if err != nil { return nil, err } font, err := freetype.ParseFont(fontBytes) if err != nil { return nil, err } return font, nil }
func init() { // Read the font data. fontBytes, err := ioutil.ReadFile("luxisr.ttf") if err != nil { log.Fatal(err) } font, err = freetype.ParseFont(fontBytes) if err != nil { log.Fatal(err) } }
func init() { fontData, err := Asset("font/Anonymous Pro Minus.ttf") if err != nil { fmt.Fprintln(os.Stderr, err) os.Exit(1) } font, err = freetype.ParseFont(fontData) if err != nil { fmt.Fprintln(os.Stderr, err) os.Exit(1) } }
func NewFontData(fontpath string) *FontData { // Read the font data. fontBytes, err := ioutil.ReadFile(fontpath) if err != nil { log.Fatal(err) } font, err := freetype.ParseFont(fontBytes) if err != nil { log.Fatal(err) } return &FontData{font: font, name: fontpath, mtex: make(map[string]*FontTexture)} }
func loadFont() (*truetype.Font, error) { fontBytes, err := ioutil.ReadFile(*fontFile) if err != nil { log.Println(err) return nil, err } font, err := freetype.ParseFont(fontBytes) if err != nil { log.Println(err) return nil, err } return font, nil }
// ParseFont reads a font file and creates a freetype.Font type func ParseFont(fontReader io.Reader) (*truetype.Font, error) { fontBytes, err := ioutil.ReadAll(fontReader) if err != nil { return nil, err } font, err := freetype.ParseFont(fontBytes) if err != nil { return nil, err } return font, nil }
func loadFont(fontFileName string) *truetype.Font { fontBytes, err := ioutil.ReadFile(path.Join(fontFolder, fontFileName)) if err != nil { log.Println(err) return nil } font, err := freetype.ParseFont(fontBytes) if err != nil { log.Println(err) return nil } return font }
func MustLoadFontAs(path, name string) { if _, ok := basic_fonts[name]; ok { panic(fmt.Sprintf("Cannot load two fonts with the same name: '%s'.", name)) } data, err := ioutil.ReadFile(path) if err != nil { panic(err.Error()) } font, err := freetype.ParseFont(data) if err != nil { panic(err.Error()) } basic_fonts[name] = font }
func loadFont(fontPath string) *truetype.Font { b, err := ioutil.ReadFile(fontPath) if err != nil { log.Fatal(err) return nil } font, err := freetype.ParseFont(b) if err != nil { log.Fatal(err) return nil } return font }
func init() { im := image.NewRGBA(image.Rect(0, 0, tilesize, tilesize)) var buf bytes.Buffer err := png.Encode(&buf, im) if err != nil { panic(err) } emptytile = buf.Bytes() nstfont, err = freetype.ParseFont(luxiSansFontData()) if err != nil { panic(err) } }
func LoadFontAs(path, name string) error { if _, ok := basic_fonts[name]; ok { return &guiError{fmt.Sprintf("Cannot load two fonts with the same name: '%s'.", name)} } data, err := ioutil.ReadFile(path) if err != nil { return err } font, err := freetype.ParseFont(data) if err != nil { return err } basic_fonts[name] = font return nil }
func NewRenderer() *Renderer { r := Renderer{} fontBytes, _ := ioutil.ReadFile(*fontfile) r.font, _ = freetype.ParseFont(fontBytes) r.img = image.NewRGBA(image.Rect(0, 0, *width, *height)) r.context = freetype.NewContext() r.context.SetDPI(*dpi) r.context.SetFont(r.font) r.context.SetFontSize(*size) r.context.SetClip(r.img.Bounds()) draw.Draw(r.img, r.img.Bounds(), &image.Uniform{color.RGBA{0, 0, 0, 255}}, image.ZP, draw.Src) r.context.SetDst(r.img) return &r }
func init() { fontPath := os.Getenv("TEMPWEB_FONT") if fontPath == "" { goPath := os.Getenv("GOPATH") fontPath = filepath.Join(goPath, "src/code.google.com/p/freetype-go/luxi-fonts/luximr.ttf") } fontBytes, err := ioutil.ReadFile(fontPath) if err != nil { log.Fatalf("Error loading font (try setting TEMPWEB_FONT): %v", err) } font, err = freetype.ParseFont(fontBytes) if err != nil { log.Fatalf("Error parsing font: %v", err) } }
func init() { con = freetype.NewContext() data, err := ioutil.ReadFile("C:/Windows/Fonts/simsun.ttc") if err != nil { fmt.Println("Can't load simsun.ttc") return } font, err := freetype.ParseFont(data) if err != nil { fmt.Println("Can't parse simsun.ttc") return } con.SetDPI(120) con.SetFont(font) con.SetFontSize(12) }
// NewFont creates a new Font object based on the font file provided. func NewFont(fontFile string) (*Font, error) { font := &Font{} fontData, err := ioutil.ReadFile(fontFile) if err != nil { return font, err } font.font, err = freetype.ParseFont(fontData) if err != nil { return font, err } return font, nil }
func init() { data, err := ioutil.ReadFile(`d:\consola.ttf`) if err != nil { os.Exit(1) } font, err := freetype.ParseFont(data) if err != nil { os.Exit(1) } con.SetDPI(72) con.SetFont(font) con.SetFontSize(20) con.SetSrc(frc) con.SetDst(img) con.SetClip(img.Bounds()) draw.Draw(img, img.Bounds(), bgc, image.Point{0, 0}, draw.Src) }