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)) }
// 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) }
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.") }