fontutil

package module
v0.0.0-...-d50e085 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 20, 2015 License: MIT Imports: 5 Imported by: 0

README

fontutil

fontutil is a small collection of utility functions to make working with fonts easier.

Functions

Find

Find returns the path to a font file given a font name. It's (intended to be) an OS-agnostic way to locate font files installed on the system. If you need to include fonts with your application instead of finding them on the system, this isn't for you.

Find currently only supports Windows but contributions for other systems are quite welcome! I'm also open to changing the interface. For example, separately specifying style, variant, or weight or a css-like list of prioritized font or family names. I'm unsure if/how these changes could be implemented on the different systems.

Supported platforms and resources for future implementation
FindAndParse

FindAndParse calls Find to find a font file, reads it, parses it, and returns the *truetype.Font.

DrawWidth

DrawWidth returns the width of a string if it were drawn to a Context, as the X coordinate of a raster.Point.

CenterX

CenterX returns the the start point required to center a string within a Rectangle, as the X coordinate of a raster.Point.

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

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

func DrawWidth(c *freetype.Context, s string) (fixed.Point26_6, error)

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 Find

func Find(fontname string) (filename string, err error)

Find is a platform-agnostic way to locate font files installed on the system by name.

func FindAndParse

func FindAndParse(fontname string) (*truetype.Font, error)

FindAndParse Finds a font file named by fontname, reads it, and parses it.

func Int26_6

func Int26_6(f float64) fixed.Int26_6

Int26_6 returns a fixed.Int26_6 representation of f.

func Must

func Must(t *truetype.Font, e error) *truetype.Font

Must is a helper that wraps a call to FindAndParse and panics if the err is non-nil. It is intended for use in package variable initializations.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL