freetype

package module
v0.0.0-...-7d1d1b4 Latest Latest
Warning

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

Go to latest
Published: Jun 24, 2021 License: Freetype Imports: 7 Imported by: 0

README

The Freetype font rasterizer in the Go programming language.

This version has modifications by onioneffect on GitHub! See changes at the end of this file!

To download and install from source: $ go get github.com/golang/freetype

It is an incomplete port:

  • It only supports TrueType fonts, and not Type 1 fonts nor bitmap fonts.
  • It only supports the Unicode encoding.

There are also some implementation differences:

  • It uses a 26.6 fixed point co-ordinate system everywhere internally, as opposed to the original Freetype's mix of 26.6 (or 10.6 for 16-bit systems) in some places, and 24.8 in the "smooth" rasterizer.

Freetype-Go is derived from Freetype, which is written in C. Freetype is copyright 1996-2010 David Turner, Robert Wilhelm, and Werner Lemberg. Freetype-Go is copyright The Freetype-Go Authors, who are listed in the AUTHORS file.

Unless otherwise noted, the Freetype-Go source files are distributed under the BSD-style license found in the LICENSE file.

Changes:

  • Modify freetype.go to add functions StrAdvanceWidth and StrAlign.
  • Add example freetype/advance to demonstrate how to calculate the advance width of a string.
  • Add example freetype/align to demonstrate StrAlign.

Important note: The examples that I made include "github.com/onioneffect/freetype", while the original examples include "github.com/golang/freetype". Only the former has the changes listed above!

Documentation

Overview

The freetype package provides a convenient API to draw text onto an image. Use the freetype/raster and freetype/truetype packages for lower level control over rasterization and TrueType parsing.

Index

Constants

View Source
const (
	ALIGN_CENTER = iota
	ALIGN_RIGHT
	ALIGN_MIDDLE
	ALIGN_BOTTOM
)

StrAlign takes two strings: the text to be drawn and the align mode, which can be "center" and "right" on the X axis and "middle" and "bottom" on the Y axis. It returns the advance that should be used so that `s` can be drawn according to whichever of the four options you choose. This function only works on one axis at a time!

Variables

This section is empty.

Functions

func ParseFont

func ParseFont(b []byte) (*truetype.Font, error)

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 Pt

func Pt(x, y int) fixed.Point26_6

Pt converts from a co-ordinate pair measured in pixels to a fixed.Point26_6 co-ordinate pair measured in fixed.Int26_6 units.

Types

type Context

type Context struct {
	// contains filtered or unexported fields
}

A Context holds the state for drawing text in a given font and size.

func NewContext

func NewContext() *Context

NewContext creates a new Context.

func (*Context) DrawString

func (c *Context) DrawString(s string, p fixed.Point26_6) (fixed.Point26_6, error)

DrawString draws s at p and returns p advanced by the text extent. The text is placed so that the left edge of the em square of the first character of s and the baseline intersect at p. The majority of the affected pixels will be above and to the right of the point, but some may be below or to the left. For example, drawing a string that starts with a 'J' in an italic font may affect pixels below and left of the point.

p is a fixed.Point26_6 and can therefore represent sub-pixel positions.

func (*Context) PointToFixed

func (c *Context) PointToFixed(x float64) fixed.Int26_6

PointToFixed converts the given number of points (as in "a 12 point font") into a 26.6 fixed point number of pixels.

func (*Context) SetClip

func (c *Context) SetClip(clip image.Rectangle)

SetClip sets the clip rectangle for drawing.

func (*Context) SetDPI

func (c *Context) SetDPI(dpi float64)

SetDPI sets the screen resolution in dots per inch.

func (*Context) SetDst

func (c *Context) SetDst(dst draw.Image)

SetDst sets the destination image for draw operations.

func (*Context) SetFont

func (c *Context) SetFont(f *truetype.Font)

SetFont sets the font used to draw text.

func (*Context) SetFontSize

func (c *Context) SetFontSize(fontSize float64)

SetFontSize sets the font size in points (as in "a 12 point font").

func (*Context) SetHinting

func (c *Context) SetHinting(hinting font.Hinting)

SetHinting sets the hinting policy.

func (*Context) SetSrc

func (c *Context) SetSrc(src image.Image)

SetSrc sets the source image for draw operations. This is typically an image.Uniform.

func (*Context) StrAdvanceWidth

func (c *Context) StrAdvanceWidth(s string) (fixed.Int26_6, error)

StrAdvanceWidth calculates the advance width of a string and returns it. This function borrows code from `DrawString`.

func (*Context) StrAlign

func (c *Context) StrAlign(s string, align int) (fixed.Int26_6, error)

Directories

Path Synopsis
Package raster provides an anti-aliasing 2-D rasterizer.
Package raster provides an anti-aliasing 2-D rasterizer.
Package truetype provides a parser for the TTF and TTC file formats.
Package truetype provides a parser for the TTF and TTC file formats.

Jump to

Keyboard shortcuts

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