當前位置: 首頁>>代碼示例>>Golang>>正文


Golang freetype.ParseFont函數代碼示例

本文整理匯總了Golang中code/google/com/p/freetype-go/freetype.ParseFont函數的典型用法代碼示例。如果您正苦於以下問題:Golang ParseFont函數的具體用法?Golang ParseFont怎麽用?Golang ParseFont使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。


在下文中一共展示了ParseFont函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: TestLoadFonts

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)
	}
}
開發者ID:Miaque,項目名稱:mojo,代碼行數:8,代碼來源:font_test.go

示例2: drawStringImage

// 畫一個帶有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
}
開發者ID:jianfengye,項目名稱:GO_Works,代碼行數:33,代碼來源:main.go

示例3: init

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
}
開發者ID:shmick,項目名稱:rtl-gopow,代碼行數:34,代碼來源:annotater.go

示例4: getFont

// 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
}
開發者ID:jackielii,項目名稱:go-plotinum,代碼行數:36,代碼來源:font.go

示例5: setupDrawer

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)
}
開發者ID:athomason,項目名稱:Turing-Tiler,代碼行數:29,代碼來源:image.go

示例6: init

func init() {
	var err error
	defaultFont, err = freetype.ParseFont(defaultFontData())
	if err != nil {
		panic(err)
	}
}
開發者ID:rbberry,項目名稱:chart,代碼行數:7,代碼來源:image.go

示例7: 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
}
開發者ID:jen6,項目名稱:plotinum,代碼行數:36,代碼來源:font.go

示例8: TextToImage

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
}
開發者ID:kleitz,項目名稱:quotable,代碼行數:35,代碼來源:quote.go

示例9: fpsInit

// 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)
}
開發者ID:dylanpoe,項目名稱:golang.org,代碼行數:35,代碼來源:fps.go

示例10: builtInFont

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
}
開發者ID:Pursuit92,項目名稱:wingo,代碼行數:8,代碼來源:theme.go

示例11: 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
}
開發者ID:amrhassan,項目名稱:rsc,代碼行數:57,代碼來源:pic.go

示例12: convertString2image

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
}
開發者ID:RobotClubKut,項目名稱:MatrixLEDTOOLS,代碼行數:55,代碼來源:main.go

示例13: init

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))
	}
}
開發者ID:joelgwebber,項目名稱:memething,代碼行數:11,代碼來源:memething.go

示例14: initFont

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
}
開發者ID:jShi-git,項目名稱:go-demos,代碼行數:11,代碼來源:signer.go

示例15: GetFont

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
}
開發者ID:abarax,項目名稱:gomeboycolor,代碼行數:11,代碼來源:debugger.go


注:本文中的code/google/com/p/freetype-go/freetype.ParseFont函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。