Documentation ¶
Overview ¶
Package fontutil is a small collection of utility functions that make working with fonts easier.
Example ¶
package main import ( "fmt" "hash/fnv" "image" "image/color" "image/png" "github.com/infogulch/fontutil" "github.com/golang/freetype" ) var font = fontutil.Must(fontutil.FindAndParse("Arial")) func main() { // As usual in examples, this ignores all errors. Don't do this in your program. // setup and find start point for centering s := "Hello, World!" size := image.Rect(0, 0, 120, 20) dst := image.NewRGBA(size) c := freetype.NewContext() c.SetFont(font) c.SetFontSize(14.0) c.SetSrc(image.NewUniform(color.Black)) c.SetDst(dst) start, _ := fontutil.CenterX(c, s, size) // CenterX calls c.SetClip(size) // perform draw at start.X + y 16 c.DrawString(s, start.Add(freetype.Pt(0, 16))) // write the image out to a file // out, _ := os.Create("helloworld.png") // defer out.Close() // write image to hash for testing purposes out := fnv.New64() _ = png.Encode(out, dst) fmt.Printf("Hash of compressed image: %x", out.Sum64()) }
Output: Hash of compressed image: fa83a1b8d8abf5f2
Index ¶
- func CenterX(c *freetype.Context, s string, r image.Rectangle) (fixed.Point26_6, error)
- func DrawWidth(c *freetype.Context, s string) (fixed.Point26_6, error)
- func Find(fontname string) (filename string, err error)
- func FindAndParse(fontname string) (*truetype.Font, error)
- func Int26_6(f float64) fixed.Int26_6
- func Must(t *truetype.Font, e error) *truetype.Font
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CenterX ¶
CenterX returns the Point at which string s will be centered within rect r along the X coordinate when passed to c.DrawString. The Y coordinate will be 0. Clip is set to r before returning.
func DrawWidth ¶
DrawWidth simulates drawing s using Context c and returns the raster.Point where X is the width of the drawn string. The context's src and dst are unused, but the font should already be set. Clip is set to an empty Rectangle and should be reset afterwards.
func FindAndParse ¶
FindAndParse Finds a font file named by fontname, reads it, and parses it.
Types ¶
This section is empty.
Click to show internal directories.
Click to hide internal directories.