Esempio n. 1
0
func (m *Module) setup() (err error) {
	// Retrieve the default system font, encoded as a TTF.
	ttfBytes := font.Default()

	m.font, err = truetype.Parse(ttfBytes)
	return err
}
Esempio n. 2
0
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, truetype.NoHinting)
	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))
}
Esempio n. 3
0
func Fuzz(data []byte) int {
	f, err := truetype.Parse(data)
	if err != nil {
		if f != nil {
			panic("font is not nil on error")
		}
		return 0
	}
	return 1
}
Esempio n. 4
0
func init() {
	var err error
	fnt, err = truetype.Parse(fonts.OpenSansLightBytes())
	if err != nil {
		log.Println(err)
		return
	}

	DefaultBackgroundColor, _ = ColorFromHex("#909090")
	lightDark, _ = ColorFromHex("#505050")
}
Esempio n. 5
0
func LoadFont(name, file string) error {
	f, err := ioutil.ReadFile(file)
	if err != nil {
		return err
	}
	font, err := truetype.Parse(f)
	if err != nil {
		return err
	}
	draw2d.RegisterFont(draw2d.FontData{Name: name}, font)
	return nil
}
Esempio n. 6
0
func init() {
	luximrTTF, err := truetype.Parse(luximr)
	if err != nil {
		common.UsageAndExit("failed to parse luximir font: %s", err)
	}
	luximr = nil // kill 72Kb of font data

	draw2d.RegisterFont(
		draw2d.FontData{Name: "luxi", Family: draw2d.FontFamilyMono, Style: draw2d.FontStyleNormal},
		luximrTTF,
	)
}
Esempio n. 7
0
func init() {
	fontBytes, err := ioutil.ReadFile("brush_strokes.ttf")
	if err != nil {
		panic(err)
	}

	strokeFont, err = truetype.Parse(fontBytes)
	if err != nil {
		panic(err)
	}

	spb, err := ioutil.ReadFile("SansPosterBold.ttf")
	if err != nil {
		panic(err)
	}

	posterFont, err = truetype.Parse(spb)
	if err != nil {
		panic(err)
	}
}
Esempio n. 8
0
func LoadFont(name, path string) error {
	fontBytes, err := ioutil.ReadFile(path)
	if err != nil {
		return err
	}
	theFont, err = truetype.Parse(fontBytes)
	if err != nil {
		return err
	}
	FontName = name
	return nil
}
Esempio n. 9
0
// parseFont parse the font file as *truetype.Font (TTF)
func parseFont(fontFile string) (*truetype.Font, error) {
	fontBytes, err := ioutil.ReadFile(fontFile)
	if err != nil {
		return nil, err
	}

	font, err := truetype.Parse(fontBytes)
	if err != nil {
		return nil, err
	}

	return font, nil
}
Esempio n. 10
0
func readFont(s string) (*truetype.Font, error) {
	b, err := ioutil.ReadFile(s)
	if err != nil {
		return nil, err
	}

	f, err := truetype.Parse(b)
	if err != nil {
		return nil, err
	}

	return f, nil
}
Esempio n. 11
0
File: font.go Progetto: Wayt/draw2d
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
}
Esempio n. 12
0
// read the font file as *truetype.Font
func getTTF(fontFile string) (*truetype.Font, error) {
	fontBytes, err := ioutil.ReadFile(fontFile)
	if err != nil {
		return nil, err
	}

	ttf, err := truetype.Parse(fontBytes)
	if err != nil {
		return nil, err
	}

	return ttf, nil
}
Esempio n. 13
0
// from fogleman/gg
func loadFontFace(path string, points float64) (font.Face, error) {
	fontBytes, err := ioutil.ReadFile(path)
	if err != nil {
		return nil, err
	}
	f, err := truetype.Parse(fontBytes)
	if err != nil {
		return nil, err
	}
	face := truetype.NewFace(f, &truetype.Options{
		Size:    points,
		Hinting: font.HintingFull,
	})
	return face, nil
}
Esempio n. 14
0
func (cache *defaultFontCache) Load(fontData FontData) (font *truetype.Font, err error) {
	var data []byte
	var file = cache.namer(fontData)

	if data, err = ioutil.ReadFile(filepath.Join(cache.folder, file)); err != nil {
		return
	}

	if font, err = truetype.Parse(data); err != nil {
		return
	}

	cache.fonts[file] = font
	return
}
Esempio n. 15
0
func loadFont(fontFile string) FontInfo {

	// Read the font data.
	fontBytes, err := ioutil.ReadFile(fontFile)
	if err != nil {
		log.Println(err)
		return FontInfo{}
	}
	f, err := truetype.Parse(fontBytes)
	if err != nil {
		log.Println(err)
		return FontInfo{}
	}

	return FontInfo(*f)
}
Esempio n. 16
0
func loadFont() *truetype.Font {
	ttf, err := asset.Open("luximr.ttf")
	if err != nil {
		log.Fatal(err)
	}
	b, err := ioutil.ReadAll(ttf)
	if err != nil {
		log.Fatal(err)
	}
	f, err := truetype.Parse(b)
	if err != nil {
		log.Fatal(err)
	}

	return f
}
Esempio n. 17
0
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
}
Esempio n. 18
0
// New returns a newly created Font object
func New(file string) (*Font, error) {

	if font, found := fontList[file]; found {
		return font, nil
	}
	s := &Font{}
	data, err := ioutil.ReadFile(file)
	if err != nil {
		log.Fatal(err)
	}
	s.ttf, err = truetype.Parse(data)
	if err != nil {
		log.Fatal(err)
	}

	fontList[file] = s
	return fontList[file], nil
}
Esempio n. 19
0
func FontLoad(fontName string, fontSize int) (font.Face, error) {

	// TODO: select the correct font path
	fontBytes, err := fonts.LoadFont(fontName)
	if err != nil {
		return nil, err
	}
	fontFace, err := truetype.Parse(fontBytes)
	if err != nil {
		return nil, err
	}
	face := truetype.NewFace(fontFace, &truetype.Options{
		Size: float64(fontSize) * 72.0 / 96.0,
		DPI:  72,
		// Hinting: font.HintingNone,
		Hinting: font.HintingFull,
	})
	return face, nil
}
Esempio n. 20
0
func NewFontAtlas(filename string, dpi, fontSize float64) (*FontAtlas, error) {
	atlas := &FontAtlas{}

	content, err := ioutil.ReadFile(filename)
	if err != nil {
		return nil, err
	}

	atlas.TTF, err = truetype.Parse(content)
	if err != nil {
		return nil, err
	}

	opts := &truetype.Options{}
	opts.Size = fontSize

	atlas.Face = truetype.NewFace(atlas.TTF, opts)
	return atlas, nil
}
Esempio n. 21
0
/** Load a truetype font */
func LoadFont(filename string, size, dpi, spacing float64) *Font {
	fontBytes, err := ioutil.ReadFile(filename)
	if err != nil {
		panic(err)
	}
	f, err := truetype.Parse(fontBytes)
	if err != nil {
		panic(err)
	}

	fnt := &Font{
		Size:    size,
		DPI:     dpi,
		Spacing: spacing,
		fnt:     f,
	}
	fnt.setup()
	return fnt
}
Esempio n. 22
0
func newFont(data []byte, size int) (*font, error) {
	ttf, err := truetype.Parse(data)
	if err != nil {
		return nil, err
	}

	scale := fixed.Int26_6(size << 6)
	bounds := rectangle26_6toRect(ttf.Bounds(scale))
	ascentDips := bounds.Max.Y

	return &font{
		size:             size,
		scale:            scale,
		glyphMaxSizeDips: bounds.Size(),
		ascentDips:       ascentDips,
		ttf:              ttf,
		resolutions:      make(map[resolution]*glyphTable),
		glyphAdvanceDips: make(map[rune]int),
	}, nil
}
Esempio n. 23
0
func main() {
	flag.Parse()
	fmt.Printf("Loading fontfile %q\n", *fontfile)
	b, err := ioutil.ReadFile(*fontfile)
	if err != nil {
		log.Println(err)
		return
	}
	f, err := truetype.Parse(b)
	if err != nil {
		log.Println(err)
		return
	}
	fupe := fixed.Int26_6(f.FUnitsPerEm())
	printBounds(f.Bounds(fupe))
	fmt.Printf("FUnitsPerEm:%d\n\n", fupe)

	c0, c1 := 'A', 'V'

	i0 := f.Index(c0)
	hm := f.HMetric(fupe, i0)
	g := &truetype.GlyphBuf{}
	err = g.Load(f, fupe, i0, font.HintingNone)
	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 := f.Index(c1)
	fmt.Printf("\n'%c', '%c' Kern:%d\n", c0, c1, f.Kern(fupe, i0, i1))

	fmt.Printf("\nThe numbers above are in FUnits.\n" +
		"The numbers below are in 26.6 fixed point pixels, at 12pt and 72dpi.\n\n")
	a := truetype.NewFace(f, &truetype.Options{
		Size: 12,
		DPI:  72,
	})
	fmt.Printf("%#v\n", a.Metrics())
}
Esempio n. 24
0
func loadFont(path string) (font *truetype.Font) {
	var f *os.File
	var b []byte
	var e error

	if f, e = os.Open(path); e != nil {
		panic(e)
	}

	defer f.Close()

	if b, e = ioutil.ReadAll(f); e != nil {
		panic(e)
	}

	if font, e = truetype.Parse(b); e != nil {
		panic(e)
	}

	return
}
Esempio n. 25
0
func NewFontAtlas(filename string, dpi, fontSize float64) (*FontAtlas, error) {
	atlas := &FontAtlas{}
	atlas.Rendered = make(map[rune]Glyph, 256)

	content, err := ioutil.ReadFile(filename)
	if err != nil {
		return nil, err
	}

	atlas.drawPadding = float32(fontSize * 0.5)
	atlas.lineHeight = float32(fontSize * 1.2)

	atlas.TTF, err = truetype.Parse(content)
	if err != nil {
		return nil, err
	}

	atlas.Image = image.NewRGBA(image.Rect(0, 0, 1024, 1024))

	atlas.Context = freetype.NewContext()
	atlas.Context.SetDPI(dpi)

	atlas.Context.SetFont(atlas.TTF)
	atlas.Context.SetFontSize(fontSize)

	atlas.Context.SetClip(atlas.Image.Bounds())
	atlas.Context.SetSrc(image.White)
	atlas.Context.SetDst(atlas.Image)

	atlas.maxBounds = atlas.TTF.Bounds(fixed.I(int(fontSize)))

	opts := &truetype.Options{}
	opts.Size = fontSize
	opts.Hinting = font.HintingFull

	atlas.Face = truetype.NewFace(atlas.TTF, opts)
	return atlas, nil
}
Esempio n. 26
0
func parseFont() error {
	f, err := ebitenutil.OpenFile("_resources/fonts/mplus-1p-regular.ttf")
	if err != nil {
		return err
	}
	defer func() {
		_ = f.Close()
	}()
	b, err := ioutil.ReadAll(f)
	if err != nil {
		return err
	}
	tt, err := truetype.Parse(b)
	if err != nil {
		return err
	}
	w, h := textImage.Size()
	dst := image.NewRGBA(image.Rect(0, 0, w, h))
	const size = 24
	const dpi = 72
	d := &font.Drawer{
		Dst: dst,
		Src: image.White,
		Face: truetype.NewFace(tt, &truetype.Options{
			Size:    size,
			DPI:     dpi,
			Hinting: font.HintingFull,
		}),
	}
	y := size
	for _, s := range text {
		d.Dot = fixed.P(0, y)
		d.DrawString(s)
		y += size
	}
	return textImage.ReplacePixels(dst.Pix)
}
Esempio n. 27
0
File: font.go Progetto: MeGaPk/gxui
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
}
Esempio n. 28
0
func main() {
	flag.Parse()

	// Read the font data.
	fontBytes, err := ioutil.ReadFile(*fontfile)
	if err != nil {
		log.Println(err)
		return
	}
	f, err := truetype.Parse(fontBytes)
	if err != nil {
		log.Println(err)
		return
	}

	// Draw the background and the guidelines.
	fg, bg := image.Black, image.White
	ruler := color.RGBA{0xdd, 0xdd, 0xdd, 0xff}
	if *wonb {
		fg, bg = image.White, image.Black
		ruler = color.RGBA{0x22, 0x22, 0x22, 0xff}
	}
	const imgW, imgH = 640, 480
	rgba := image.NewRGBA(image.Rect(0, 0, imgW, imgH))
	draw.Draw(rgba, rgba.Bounds(), bg, image.ZP, draw.Src)
	for i := 0; i < 200; i++ {
		rgba.Set(10, 10+i, ruler)
		rgba.Set(10+i, 10, ruler)
	}

	// Draw the text.
	h := font.HintingNone
	switch *hinting {
	case "full":
		h = font.HintingFull
	}
	d := &font.Drawer{
		Dst: rgba,
		Src: fg,
		Face: truetype.NewFace(f, &truetype.Options{
			Size:    *size,
			DPI:     *dpi,
			Hinting: h,
		}),
	}
	y := 10 + int(math.Ceil(*size**dpi/72))
	dy := int(math.Ceil(*size * *spacing * *dpi / 72))
	d.Dot = fixed.Point26_6{
		X: (fixed.I(imgW) - d.MeasureString(title)) / 2,
		Y: fixed.I(y),
	}
	d.DrawString(title)
	y += dy
	for _, s := range text {
		d.Dot = fixed.P(10, y)
		d.DrawString(s)
		y += dy
	}

	// Save that RGBA image to disk.
	outFile, err := os.Create("out.png")
	if err != nil {
		log.Println(err)
		os.Exit(1)
	}
	defer outFile.Close()
	b := bufio.NewWriter(outFile)
	err = png.Encode(b, rgba)
	if err != nil {
		log.Println(err)
		os.Exit(1)
	}
	err = b.Flush()
	if err != nil {
		log.Println(err)
		os.Exit(1)
	}
	fmt.Println("Wrote out.png OK.")
}
Esempio n. 29
0
// 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)
}
Esempio n. 30
0
func main() {
	flag.Parse()

	if *flagFontfile == "" || *flagOutdir == "" || *flagPkgname == "" {
		flag.Usage()
		return
	}

	if *flagScale < 1 {
		log.Println("scale must be >= 1")
		return
	}

	bin, err := ioutil.ReadFile(*flagFontfile)
	if err != nil {
		log.Println(err)
		return
	}
	f, err := truetype.Parse(bin)
	if err != nil {
		log.Println(err)
		return
	}

	sdf := NewSDF(*flagTSize, *flagFSize, *flagPad, *flagScale)
	d := &font.Drawer{
		Dst: sdf.src,
		Src: image.Black,
		Face: truetype.NewFace(f, &truetype.Options{
			Size:    sdf.fsize,
			Hinting: font.HintingNone,
		}),
	}

	var glyphs []*glyph
	if *flagAscii {
		glyphs = fromString(ascii, d.Face)
	} else {
		glyphs = enumerate(f, d.Face)
	}
	if len(glyphs) == 0 {
		log.Fatalf("sdf: failed to enumerate glyphs from %s\n", *flagFontfile)
	}
	if a := area(glyphs, sdf.pad); a > sdf.tsize*sdf.tsize {
		asq := math.Sqrt(float64(a))
		log.Fatalf("sdf: glyphs area %[1]v ~= %.2[2]fx%.2[2]f greater than texture area %[3]vx%[3]v\n", a, asq, sdf.tsize)
	}
	sort.Sort(byHeight(glyphs))

	x, y, dy := 0, 0, glyphs[0].height()+sdf.pad*2
	var wg sync.WaitGroup
	for _, g := range glyphs {
		adx, ady := g.width()+sdf.pad*2, g.height()+sdf.pad*2
		if x+adx > sdf.tsize {
			x = 0
			y += dy
			dy = ady
		}

		g.tc = [4]float32{
			float32(x+sdf.pad) / float32(sdf.tsize),
			float32(y+sdf.pad) / float32(sdf.tsize),
			float32(g.width()) / float32(sdf.tsize),
			float32(g.height()) / float32(sdf.tsize),
		}

		d.Dot = fixed.P(x+sdf.pad-int(g.b.Min.X>>6), y+sdf.pad-g.b.Min.Y.Ceil())
		d.DrawString(string(g.r))

		wg.Add(1)
		go func(m image.Image) {
			sdf.calc(m)
			wg.Done()
		}(sdf.src.SubImage(image.Rect(x, y, x+adx, y+ady)))

		x += adx
	}
	wg.Wait()

	sdf.writeSrc()
	sdf.writeDst()
	sdf.writeOut()

	// generate source file to accompany out.png
	buf := new(bytes.Buffer)
	buf.WriteString("// generated by gen.go; DO NOT EDIT\n")
	fmt.Fprintf(buf, "package %s\n\n", *flagPkgname)

	ascent := float32(d.Face.Metrics().Ascent.Ceil())
	descent := float32(d.Face.Metrics().Descent.Floor())
	scale := float32(sdf.fsize) / (ascent + descent)
	fmt.Fprintf(buf, "const AscentUnit = %v\n", (ascent*scale)/float32(sdf.fsize))
	fmt.Fprintf(buf, "const DescentUnit = %v\n", (descent*scale)/float32(sdf.fsize))
	fmt.Fprintf(buf, "const TextureSize = %v\n", *flagTSize)
	fmt.Fprintf(buf, "const FontSize = %v\n", *flagFSize)
	fmt.Fprintf(buf, "const Pad = %v\n\n", *flagPad)

	buf.WriteString("var Texcoords = map[rune][4]float32{\n")
	for _, g := range glyphs {
		s := string(g.r)
		if s == "'" {
			s = `\'`
		} else if s == "\\" {
			s = `\\`
		}
		fmt.Fprintf(buf, "\t'%s': {%v, %v, %v, %v},\n", s, g.tc[0], g.tc[1], g.tc[2], g.tc[3])
	}
	buf.WriteString("}\n\n")

	buf.WriteString("var Bounds = map[rune][5]float32{\n")
	for _, g := range glyphs {
		s := string(g.r)
		if s == "'" {
			s = `\'`
		} else if s == "\\" {
			s = `\\`
		}
		nx := float32(g.b.Min.X>>6) / float32(sdf.fsize)
		ny := float32(g.b.Max.Y>>6) / float32(sdf.fsize)
		rect := g.b.Max.Sub(g.b.Min)
		w, h := float32(rect.X>>6), float32(rect.Y>>6)
		nw := float32(w) / float32(sdf.fsize)
		nh := float32(h) / float32(sdf.fsize)
		na := float32(g.a>>6) / float32(sdf.fsize)
		fmt.Fprintf(buf, "\t'%s': {%v, %v, %v, %v, %v},\n", s, nx, ny, nw, nh, na)
	}
	buf.WriteString("}\n\n")

	if err := ioutil.WriteFile(filepath.Join(*flagOutdir, "texcoords.go"), buf.Bytes(), 0644); err != nil {
		log.Fatal(err)
	}
}