gofont

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Feb 28, 2021 License: MIT Imports: 8 Imported by: 1

README

gofont

gofont is a Go library to write text on Go's standard draw.Image. It loads True Type Fonts (TTF) and lets you pick a (potentially transparent) color and size to write text in a formatted way.

Here is a link to the Godoc reference.

Example

This example image is created by the following example code:

Example

package main

import (
	"bytes"
	"image"
	"image/color"
	"image/draw"
	"image/png"
	"os"

	"golang.org/x/image/font/gofont/goregular"

	"github.com/gonutz/gofont"
)

func main() {
	// The font can be read from an io.Reader or loaded from file.
	//  On Windows you could also do:
	// font, err := gofont.LoadFromFile("c:/windows/fonts/arial.ttf")
	font, err := gofont.Read(bytes.NewReader(goregular.TTF))
	check(err)

	// Create a solid black image.
	img := image.NewRGBA(image.Rect(0, 0, 300, 200))
	clearToBlack(img)

	// Write some text, by default Write will align it top-left.
	backgroundText := `This is some text with line
breaks in it. \n is used for
line breaks; do not place
any \r in the string, even
if you are on Windows ;-)`
	font.HeightInPixels = 25
	font.R, font.G, font.B, font.A = 0, 255, 0, 255 // solid green
	font.Write(img, backgroundText, 0, 0)

	// Place a larger, semi-transparent text, centered vertically and
	// horizontally.
	overlayText := `Centered
overlay`
	font.HeightInPixels = 50
	font.R, font.G, font.B, font.A = 255, 255, 0, 128 // half-transparent yellow
	font.WriteAnchor(img, overlayText, 150, 100, gofont.AnchorCenter)

	// Write the image to a file.
	f, err := os.Create("example.png")
	check(err)
	defer f.Close()
	check(png.Encode(f, img))
}

func clearToBlack(img draw.Image) {
	draw.Draw(img, img.Bounds(), image.NewUniform(color.Black), image.ZP, draw.Src)
}

func check(err error) {
	if err != nil {
		panic(err)
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Anchor

type Anchor int

Anchor is the position of the anchor point, see Font.WriteAnchor.

const (
	AnchorTopLeft Anchor = iota
	AnchorCenterLeft
	AnchorBottomLeft
	AnchorTopCenter
	AnchorCenter
	AnchorBottomCenter
	AnchorTopRight
	AnchorCenterRight
	AnchorBottomRight
)

type Font

type Font struct {
	R, G, B, A     uint8
	HeightInPixels int
	// contains filtered or unexported fields
}

Font contains a font face, color and size information. To change the font color, set the R, G, B, A values. These are red/green/blue/opacity color channels in the range [0..255]. A=0 is fully transparent, A=255 is solid. To change the font size, set the HeightInPixels.

func LoadFromFile

func LoadFromFile(path string) (*Font, error)

LoadFromFile loads a True Type Font file (.ttf) and creates a font from it. The returned font is solid black and 20 pixels high.

func Read

func Read(r io.Reader) (*Font, error)

Read reads the True Type Font (.ttf) and creates a font from it. The returned font is solid black and 20 pixels high.

func (*Font) Measure

func (f *Font) Measure(text string) (w, h int)

Measure returns the size of the text when written in the Font's current pixel height.

func (*Font) Write

func (f *Font) Write(dest draw.Image, text string, startX, startY int) (newX, newY int)

Write writes the given text aligned top-left at the given image position. It returns the position where the text ends. This can be used to write the next text, e.g. if you want to write a single word in a text with a different color.

func (*Font) WriteAnchor

func (f *Font) WriteAnchor(dest draw.Image, text string, anchorX, anchorY int, anchor Anchor)

WriteAnchor lets you justify the text horizontally and vertically. The anchor determines the point that the text "gravitates to". Here are some examples of what the Anchor type means, 'X' marks the anchor point in the image:

AnchorCenter    AnchorCenterLeft  AnchorBottomRight
+----------+      +----------+      +----------+
|          |      |          |      |          |
|   this   |      |centered  |      |          |
|    iX    |      Xleft      |      |   text at|
| centered |      |vertically|      |    bottom|
|          |      |          |      |     right|
+----------+      +----------+      +----------X

Directories

Path Synopsis
Package goregular provides the "Go Regular" TrueType font from the Go font family.
Package goregular provides the "Go Regular" TrueType font from the Go font family.

Jump to

Keyboard shortcuts

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