gltext

package module
v0.0.0-...-918212d Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2022 License: BSD-3-Clause Imports: 19 Imported by: 0

README

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

Install

  • go get github.com/4ydx/gltext

Example

  • Provided using Japanese text.

Dependencies

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 Freetype.org

License

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.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var IsDebug = false

Functions

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 NewProgram

func NewProgram(vertexShaderSource, fragmentShaderSource string) (uint32, 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)

Types

type BoundingBox

type BoundingBox struct {

	// X1, X2: the lower left and upper right points of a box that bounds the text
	X1 Point
	X2 Point
	// contains filtered or unexported fields
}

func (*BoundingBox) Draw

func (b *BoundingBox) Draw()

func (*BoundingBox) Release

func (b *BoundingBox) Release()

type CharacterSide

type CharacterSide int

CharacterSide shows which side of a character is clicked

const (
	CSLeft CharacterSide = iota
	CSRight
	CSUnknown
)

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 Font

type Font struct {
	Config *FontConfig // Character set for this font.

	OrthographicMatrix mgl32.Mat4

	WindowWidth  float32
	WindowHeight float32
	// contains filtered or unexported fields
}

func NewFont

func NewFont(config *FontConfig, img *image.NRGBA) (f *Font, err error)

func (*Font) GetTextureHeight

func (f *Font) GetTextureHeight() float32

func (*Font) GetTextureWidth

func (f *Font) GetTextureWidth() float32

func (*Font) Release

func (f *Font) Release()

func (*Font) ResizeWindow

func (f *Font) ResizeWindow(width float32, height float32)

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

	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, *image.NRGBA, error)

func NewFromConfig

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

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

func NewTruetypeFontConfig

func NewTruetypeFontConfig(r io.Reader, scale fixed.Int26_6, runeRanges RuneRanges, runesPerRow, adjustHeight fixed.Int26_6) (*FontConfig, *image.NRGBA, 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) Save

func (fc *FontConfig) Save(rootPath, name string, img *image.NRGBA) 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

type Text

type Text struct {
	Font *Font

	// scaling the text
	Scale    float32
	ScaleMin float32
	ScaleMax float32

	// Fadeout reduces alpha
	FadeOutBegun      bool
	FadeOutFrameCount float32 // number of frames since drawing began
	FadeOutPerFrame   float32 // smaller value takes more time

	// bounding box of text
	BoundingBox *BoundingBox

	// determines how many prefix characters are drawn on screen
	RuneCount int

	// no longer than this string
	MaxRuneCount int

	// lower left
	X1 Point
	// upper right
	X2 Point

	// Screen position away from center
	Position mgl32.Vec2

	String      string
	CharSpacing []float32
	// contains filtered or unexported fields
}

Text is not designed to be accessed concurrently

func NewText

func NewText(f *Font, scaleMin, scaleMax float32) (t *Text)

NewText creates a new text object with scaling boundaries the rest state of the text when not being interacted with is scaleMin. most likely one wants to use 1.0.

func (*Text) AddScale

func (t *Text) AddScale(s float32) bool

AddScale returns true when a change occured

func (*Text) BeginFadeOut

func (t *Text) BeginFadeOut()

func (*Text) CharPosition

func (t *Text) CharPosition(index int) float64

func (*Text) ClickedCharacter

func (t *Text) ClickedCharacter(xPos, offset float64) (index int, side CharacterSide)

ClickedCharacter should only be called after a bounding box hit is confirmed because it does not check y-axis values at all. Returns the index and side of the char clicked.

func (*Text) Draw

func (t *Text) Draw()

func (*Text) GetBoundingBox

func (t *Text) GetBoundingBox() (X1, X2 Point)

func (*Text) GetLength

func (t *Text) GetLength() int

func (*Text) HasRune

func (t *Text) HasRune(r rune) bool

func (*Text) Height

func (t *Text) Height() float32

func (*Text) Hide

func (t *Text) Hide()

func (*Text) PrintCharSpacing

func (t *Text) PrintCharSpacing()

PrintCharSpacing is used for debugging

func (*Text) Release

func (t *Text) Release()

Release releases text resources.

func (*Text) SetColor

func (t *Text) SetColor(color mgl32.Vec3)

func (*Text) SetPosition

func (t *Text) SetPosition(v mgl32.Vec2)

SetPosition prepares variables passed to the shader as well as values used for bounding box calculations when clicking or hovering above text

func (*Text) SetScale

func (t *Text) SetScale(s float32) bool

SetScale returns true when a change occured

func (*Text) SetString

func (t *Text) SetString(fs string, argv ...interface{})

SetString performs creates new vbo and ebo objects as well as to perform all binding required for displaying text to screen

func (*Text) Show

func (t *Text) Show()

func (*Text) Width

func (t *Text) Width() float32

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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