コード例 #1
0
func main() {
	data, err := ioutil.ReadFile(filepath.Join("..", "ClearSans-Regular.ttf"))
	if err != nil {
		panic(err)
	}

	font, err := truetype.InitFont(data, truetype.GetFontOffsetForIndex(data, 0))
	if err != nil {
		panic(err)
	}

	var c int
	if len(os.Args) > 1 {
		c = int(os.Args[1][0])
	} else {
		c = int('a')
	}

	bitmap, w, h := font.GetCodepointBitmap(0, font.ScaleForPixelHeight(20), c, 0, 0)

	for j := 0; j < h; j++ {
		for i := 0; i < w; i++ {
			fmt.Printf("%s", string(" .:ioVM@"[bitmap[j*w+i]>>5]))
		}
		fmt.Printf("\n")
	}
}
コード例 #2
0
ファイル: image.go プロジェクト: shibukawa/fontstash.go
func main() {
	data, err := ioutil.ReadFile(filepath.Join("..", "ClearSans-Regular.ttf"))
	if err != nil {
		panic(err)
	}

	font, err := truetype.InitFont(data, truetype.GetFontOffsetForIndex(data, 0))
	if err != nil {
		panic(err)
	}

	bW := 512 // bitmap width
	bH := 128 // bitmap height
	lH := 64  // line height

	scale := font.ScaleForPixelHeight(float64(lH))

	word := "how are you?"

	ascent, descent, _ := font.GetFontVMetrics()

	ascent = int(float64(ascent) * scale)
	descent = int(float64(descent) * scale)

	bitmap := make([]byte, bW*bH)

	x := 0
	for i, b := range word {
		cX1, cY1, cX2, cY2 := font.GetCodepointBitmapBox(int(b), scale, scale)

		y := ascent + cY1

		byteOffset := x + (y * bW)
		tmp := font.MakeCodepointBitmap(bitmap[byteOffset:], cX2-cX1, cY2-cY1, bW, scale, scale, int(b))
		copy(bitmap[byteOffset:], tmp)

		ax, _ := font.GetCodepointHMetrics(int(b))
		x += int(float64(ax) * scale)

		if len(word) > i+1 {
			kern := font.GetCodepointKernAdvance(int(b), int(word[i+1]))
			x += int(float64(kern) * scale)
		}
	}

	r := image.Rect(0, 0, bW, bH)
	g := image.NewGray(r)
	g.Pix = []uint8(bitmap)

	file, err := os.Create("test.png")
	if err != nil {
		panic(err)
	}
	png.Encode(file, g)
}
コード例 #3
0
func main() {
	data, err := ioutil.ReadFile(filepath.Join("..", "ClearSans-Regular.ttf"))
	if err != nil {
		panic(err)
	}

	font, err := truetype.InitFont(data, truetype.GetFontOffsetForIndex(data, 0))
	if err != nil {
		panic(err)
	}

	text := "Heljo World!"

	scale := font.ScaleForPixelHeight(15)
	ascent, _, _ := font.GetFontVMetrics()
	baseline := int(float64(ascent) * scale)

	var screen [1580]byte

	var xpos float64
	for ch, b := range text {
		xShift := xpos - math.Floor(xpos)
		advance, _ := font.GetCodepointHMetrics(int(b))
		x0, y0, x1, y1 := font.GetCodepointBitmapBoxSubpixel(int(b), scale, scale, xShift, 0)
		tmp := font.MakeCodepointBitmapSubpixel(screen[(baseline+y0)*79+int(xpos)+x0:], x1-x0, y1-y0, 79, scale, scale, xShift, 0, int(b))
		copy(screen[(baseline+y0)*79+int(xpos)+x0:], tmp)
		xpos += (float64(advance) * scale)
		if len(text) > ch+1 {
			xpos += scale * float64(font.GetCodepointKernAdvance(int(b), int(text[ch+1])))
		}
	}

	for j := 0; j < 20; j++ {
		for i := 0; i < 78; i++ {
			fmt.Printf("%s", string(" .:ioVM@"[screen[j*79+i]>>5]))
		}
		fmt.Printf("\n")
	}
}