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 }
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 }
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) } } }
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 }
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 }
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 }
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 }
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 }
// 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 }
// 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, } }
func init() { var err error defaultFont, err = freetype.ParseFont(defaultFontData()) if err != nil { panic(err) } }
func loadFont(r Resource) (*truetype.Font, error) { ttfBytes, err := ioutil.ReadFile(r.url) if err != nil { return nil, err } return freetype.ParseFont(ttfBytes) }
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) } }
func readFont() *truetype.Font { rawFont, err := ioutil.ReadFile("./static/font.ttf") if err != nil { log.Panic(err) } parsedFont, err := freetype.ParseFont(rawFont) if err != nil { log.Panic(err) } return parsedFont }
// 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 }
// Load font(s) to memory func LoadFont(fontFile string) *truetype.Font { fontBytes, err := ioutil.ReadFile(fontFile) if err != nil { panic(err) } baseFont, err := freetype.ParseFont(fontBytes) if err != nil { panic(err) } return baseFont }
// FindAndParse Finds a font file named by fontname, reads it, and parses it. func FindAndParse(fontname string) (*truetype.Font, error) { filepath, err := Find(fontname) if err != nil { return nil, err } file, err := ioutil.ReadFile(filepath) if err != nil { return nil, err } return freetype.ParseFont(file) }
func initunsplash() { fontfile, err := os.Open(*gillmt) assert(err) defer fontfile.Close() bytes, err := ioutil.ReadAll(fontfile) assert(err) font, err := freetype.ParseFont(bytes) assert(err) quoteFontData = draw2d.FontData{"gillmt", draw2d.FontFamilySans, draw2d.FontStyleBold} draw2d.RegisterFont(quoteFontData, font) }
func initmeme() { fontfile, err := os.Open(*impact) assert(err) defer fontfile.Close() bytes, err := ioutil.ReadAll(fontfile) assert(err) font, err := freetype.ParseFont(bytes) assert(err) memeFontData = draw2d.FontData{"impact", draw2d.FontFamilySans, 0} draw2d.RegisterFont(memeFontData, 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 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 TestFind(t *testing.T) { filename, err := Find("Times New Roman") if err != nil { t.Fatalf("cannot find font path: %s", err) } file, err := ioutil.ReadFile(filename) if err != nil { t.Fatalf("cannot read font file: %s", err) } font, err := freetype.ParseFont(file) if err != nil { t.Fatalf("cannot parse font: %s", err) } _ = font }
// Create is for loading fonts from the disk, given a location func (f *Font) Create() error { // Read and parse the font ttfBytes, err := ioutil.ReadFile(f.URL) if err != nil { return err } ttf, err := freetype.ParseFont(ttfBytes) if err != nil { return err } f.ttf = ttf return nil }
func Init() { fontbytes, err := fonts.Asset("LiberationSans-Regular.ttf") if err != nil { panic(err) } f, err := freetype.ParseFont(fontbytes) if err != nil { panic(err) } vg.AddFont("Helvetica", f) vg.AddFont("LiberationSans-Regular", f) vg.AddFont("LiberationSans-Regular.ttf", f) plot.DefaultFont = "Helvetica" plotter.DefaultFont = "Helvetica" }
func (t *Text) loadFont(path string) *truetype.Font { if t.font != nil { return t.font } ttfBytes, err := ioutil.ReadFile(t.ttfFont) if err != nil { log.Fatal(err) } font, err := freetype.ParseFont(ttfBytes) if err != nil { log.Fatal(err) } return font }
func loadFont(r Resource) (*truetype.Font, error) { if strings.HasPrefix(r.url, "assets/") { r.url = r.url[7:] } file, err := asset.Open(r.url) if err != nil { return nil, err } ttfBytes, err := ioutil.ReadAll(file) if err != nil { return nil, err } return freetype.ParseFont(ttfBytes) }
// LoadAsset loads the asset at path and interprets it as a font for rendering // with golang.org/x/image/font using opt to create the font.Face object. func LoadAsset(path string, opt *truetype.Options) (*truetype.Font, font.Face, error) { f, err := asset.Open(path) if err != nil { return nil, nil, err } defer f.Close() raw, err := ioutil.ReadAll(f) if err != nil { return nil, nil, err } ttf, err := freetype.ParseFont(raw) if err != nil { return nil, nil, err } face := truetype.NewFace(ttf, opt) return ttf, face, nil }
func main() { imgcounter := 123 imgfile, _ := os.Create(fmt.Sprintf("%03d.png", imgcounter)) defer imgfile.Close() img := image.NewNRGBA(image.Rect(0, 0, dx, dy)) for y := 0; y < dy; y++ { for x := 0; x < dx; x++ { img.Set(x, y, color.RGBA{uint8(x), uint8(y), 0, 255}) } } fontBytes, err := ioutil.ReadFile(fontFile) //读取字体数据 if err != nil { fmt.Println(err) return } font, err := freetype.ParseFont(fontBytes) if err != nil { fmt.Print(err) return } c := freetype.NewContext() c.SetDPI(fontDPI) c.SetFont(font) c.SetFontSize(fontSize) c.SetClip(img.Bounds()) c.SetDst(img) c.SetSrc(image.White) pt := freetype.Pt(10, 10+int(c.PointToFixed(fontSize)>>8)) // 字出现的位置 _, err = c.DrawString("ABCDE", pt) if err != nil { fmt.Println(err) return } err = png.Encode(imgfile, img) if err != nil { fmt.Println(err) } }
func loadFont(r Resource) (*truetype.Font, error) { req := xhr.NewRequest("GET", r.url+"_js") err := req.Send("") if err != nil { return &truetype.Font{}, err } fontDataEncoded := bytes.NewBuffer([]byte(req.Response.String())) fontDataCompressed := base64.NewDecoder(base64.StdEncoding, fontDataEncoded) fontDataTtf, err := gzip.NewReader(fontDataCompressed) if err != nil { return nil, err } var ttfBytes []byte ttfBytes, err = ioutil.ReadAll(fontDataTtf) if err != nil { return nil, err } return freetype.ParseFont(ttfBytes) }
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 }