text

package
v0.0.0-...-1344eab Latest Latest
Warning

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

Go to latest
Published: Mar 19, 2024 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Overview

Library for drawing text through sprites. This library does not render directly but produces sprites that can be rendered using gitlab.com/onikolas/agl/sprite. To render text, use a sprite store with a FontAlas. To draw text, create a TextDrawer. TextDrawer converts text to sprites and can add text to a region on screen (like a text box or label) and can handle wrapping and styling such as tab width.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CharacterSetASCII

func CharacterSetASCII() []rune

ascii printable chars

func CharacterSetGreek

func CharacterSetGreek() []rune

Greek characters

func RgbaToPng

func RgbaToPng(image *image.RGBA, filename string) error

Save rgba image to disk. Mostly for testing

Types

type FontAtlas

type FontAtlas struct {
	AtlasImage     *image.RGBA        // atlas image
	Glyphs         map[rune]GlyphInfo // render info per glyph
	HSpace, VSpace int                // spacing between chars/lines (also the glyph diminsions for monospace)
	CharSet        []rune             // runes stored in the atlas
}

FontAtlas takes a truetype font and renders it into an image. In addition to the rendered image, it stores the location of each glyph on the image and the glyph adjustments needed to correctly place each glyph.

func (*FontAtlas) GetBoundingBoxes

func (a *FontAtlas) GetBoundingBoxes() []math.Box2D[int]

func (*FontAtlas) GetImage

func (a *FontAtlas) GetImage() *image.RGBA

func (*FontAtlas) Init

func (a *FontAtlas) Init(fontBytes []byte, fontSize float64, charSet []rune) error

Produce an image of a given truetype font that contains bitmapped representations of the characters in charSet.

func (*FontAtlas) InitFromFile

func (a *FontAtlas) InitFromFile(fontFile string, fontSize float64, charSet []rune) error

func (*FontAtlas) ToFile

func (a *FontAtlas) ToFile(imageFile string)

type GlyphInfo

type GlyphInfo struct {
	BBox    math.Box2D[int]
	Origin  math.Vector2[int] // unused
	VAdjust int
	HAdjust int
}

GlyphInfo holds rendering information about a glyph

type TextDrawer

type TextDrawer struct {
	Area     math.Box2D[int]
	TabWidth int // TODO formatting options struct
	Depth    int

	Font  *FontAtlas // font information
	Atlas *sprite.Atlas
	// contains filtered or unexported fields
}

TextDrawer converts text into sprites that fit in an area. The placement of the text depends on the font characteristics and formatting parameters

func NewTextDrawer

func NewTextDrawer(font *FontAtlas, area math.Box2D[int], tabWidth int,
	shader shaders.Shader, maxSprites, renderOrder int) (*TextDrawer, error)

Construct a text drawer. A sprite atlas will be constructed internally.

func (*TextDrawer) AddText

func (t *TextDrawer) AddText(text []rune, shader *shaders.Shader, renderOrder int, sprites []sprite.Sprite) []sprite.Sprite

RunesToSprites converts text into sprites and appends it to the passed sprites array. Subsequent calls add more text.

func (*TextDrawer) GetDrawPosition

func (t *TextDrawer) GetDrawPosition() math.Vector2[int]

Returns the current draw position

func (*TextDrawer) MaxRunes

func (t *TextDrawer) MaxRunes() int

Return the maximum number of characters that can be placed in the area of this TextDrawer. This is a meaningful metric only for monospace fonts

func (*TextDrawer) Reset

func (t *TextDrawer) Reset()

Reset the renderer to start drawing again from the top-left corner.

Jump to

Keyboard shortcuts

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