Modern opengl text rendering

A simple package for rendering a string using modern opengl. Based on the bounding box of a string, positioning of the string on screen prior to rendering is possible. There do seem to be issues with the dimensions reported by freetype-go unfortunately.

  • Unicode support.
  • Dynamic text zooming along the z-axis.
  • Dynamic text positioning within the orthographic projection space.
  • Dynamic color changes.

Unicode support is based on the underlying truetype font being used (or bitmap).

Alt text


  • go get


  • Provided using Japanese text.


This packages uses freetype-go which is licensed under GPLv2 and FTL licenses. You can choose which one is a better fit for your use case but FTL requires you to give some form of credit to


Copyright 2012 The go-gl Authors. All rights reserved. Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.

Expand ▾ Collapse ▴




This section is empty.


View Source
var IsDebug = false


func DebugPrefix

func DebugPrefix() string

func IsPow2

func IsPow2(x uint32) bool

IsPow2 returns true if the given value is a power-of-two.

func LoadFontImage

func LoadFontImage(rootPath, name string) (*image.NRGBA, error)

func LoadImage

func LoadImage(path string) (*image.NRGBA, error)

func Pow2

func Pow2(x uint32) uint32

Pow2 returns the first power-of-two value >= to n. This can be used to create suitable texture dimensions.

func Pow2Image

func Pow2Image(src image.Image) image.Image

Pow2Image returns the given image, scaled to the smallest power-of-two dimensions larger or equal to the input dimensions. It preserves the image format and contents.

This is useful if an image is to be used as an OpenGL texture. These often require image data to have power-of-two dimensions.

func PrintVBO

func PrintVBO(vbo []float32, w, h float32)

PrintVBO prints the individual index locations as well as the texture locations

(0,0) (x1,y1): This shows the layout of the runes. There relative locations to one another can be seen here. - If called just after makeBufferData, the left-most x value will start at 0. - If called after centerTheData, all indices will have been shifted so that the entire text value is

centered around the screen's origin of (0,0).

(U,V) (u1,v1) -> (x,y): The (x,y) values refer to pixel locations within the texture - Open the texture in an image editor and, using the upper left hand corner as (0,0)

move to the location (x,y).  This is where opengl will pinpoint your rune within the image.

func SaveImage

func SaveImage(rootPath, name string, img *image.NRGBA) error

func TextDebug

func TextDebug(message string)


type Charset

type Charset []Glyph

A Charset represents a set of glyph descriptors for a font. Each glyph descriptor holds glyph metrics which are used to properly align the given glyph in the resulting rendered string.

func (Charset) Scale

func (c Charset) Scale(factor int)

Scale scales all glyphs by the given factor and repositions them appropriately. A scale of 1 retains the original size. A scale of 2 doubles the size of each glyph, etc.

This is useful when the accompanying sprite sheet is scaled by the same factor. In this case, we want the glyph data to match up with the new image.

type Direction

type Direction uint8

Direction represents the direction in which strings should be rendered.

type FontConfig

type FontConfig struct {
	// The range of glyphs covered by this fontconfig
	// An array of Low, High values allowing the user to select disjoint subsets of the ttf
	RuneRanges RuneRanges

	// Glyphs holds a set of glyph descriptors, defining the location,
	// size and advance of each glyph in the sprite sheet.
	Glyphs Charset

	Image *image.NRGBA `json:"-"`

	Name string

FontConfig describes raster font metadata.

It can be loaded from, or saved to a JSON encoded file, which should come with any bitmap font image.

func LoadTruetypeFontConfig

func LoadTruetypeFontConfig(rootPath, name string) (*FontConfig, error)

func NewTruetypeFontConfig

func NewTruetypeFontConfig(r io.Reader, scale fixed.Int26_6, runeRanges RuneRanges, runesPerRow, adjustHeight fixed.Int26_6) (*FontConfig, error)

LoadTruetype loads a truetype font from the given stream and applies the given font scale in points.

The low and high values determine the lower and upper rune limits we should load for this font. For standard ASCII this would be: 32, 127.

func (*FontConfig) Load

func (fc *FontConfig) Load(rootPath string) (err error)

Load reads font configuration data from the given JSON encoded stream.

func (*FontConfig) Save

func (fc *FontConfig) Save(rootPath, name string) error

Save writes font configuration data to the given stream as JSON data.

type FontLike

type FontLike interface {
	GetTextureWidth() float32
	GetTextureHeight() float32

type Glyph

type Glyph struct {
	X      int `json:"x"`      // The x location of the glyph on a sprite sheet.
	Y      int `json:"y"`      // The y location of the glyph on a sprite sheet.
	Width  int `json:"width"`  // The width of the glyph on a sprite sheet.
	Height int `json:"height"` // The height of the glyph on a sprite sheet.

	// Advance determines the distance to the next glyph.
	// This is used to properly align non-monospaced fonts.
	Advance int `json:"advance"`

func (*Glyph) GetTexturePositions

func (g *Glyph) GetTexturePositions(font FontLike) (tP1, tP2 Point)

type Point

type Point struct {
	X float32
	Y float32

A Glyph describes metrics for a single font glyph. These indicate which area of a given image contains the glyph data and how the glyph should be spaced in a rendered string.

type RuneRange

type RuneRange struct {
	Low, High rune

RuneRanges specify the rune ranges for ordered disjoint subsets of the ttf EG 32 - 127, 5000 - 6000 will created a more compact bitmap that holds the specified ranges of runes.

type RuneRanges

type RuneRanges []RuneRange

func (RuneRanges) GetGlyphIndex

func (rr RuneRanges) GetGlyphIndex(char rune) rune

GetGlyphIndex returns the location of the glyph data within the compressed rune ranges covered by the font EG if runes 0-25, 100-110 are supported by the font then the actual location of 100 will be in position 26 in the png image

func (RuneRanges) Len

func (rr RuneRanges) Len() int

func (RuneRanges) Less

func (rr RuneRanges) Less(i, j int) bool

func (RuneRanges) Swap

func (rr RuneRanges) Swap(i, j int)

func (RuneRanges) Validate

func (rr RuneRanges) Validate() bool


Path Synopsis