txt

package
v0.4.7 Latest Latest
Warning

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

Go to latest
Published: Apr 22, 2021 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const (
	Instant int8 = iota
	Changing
	TextType
)

Effect types

Variables

View Source
var (
	MarkdownIdent = '!'
	ColorIdent    = '#'
	BlockStart    = '['
	BlockEnd      = ']'
	NullIdent     = string([]rune{1})
	DefaultFont   = "default"
)

key markdown sintax is stored in variable so it can be customized

View Source
var ASCII []rune

ASCII is a set of all ASCII runes. These runes are codepoints from 32 to 127 inclusive.

View Source
var Aligns = map[string]Align{
	"left":   Left,
	"middle": Middle,
	"right":  Right,
}

Functions

func LoadTTF

func LoadTTF(path string, size float64) (font.Face, error)

LoadTTF loads TTF file into font.Face

Types

type Align

type Align float64

Allign determinate text align

const (
	Left   Align = 0
	Middle Align = .5
	Right  Align = 1
)

Align constants

func (Align) String

func (a Align) String() string

type Atlas

type Atlas struct {
	Pic *image.NRGBA

	Name string
	// contains filtered or unexported fields
}

Atlas is a set of pre-drawn glyphs of a fixed set of runes. This allows for efficient text drawing.

var Atlas7x13 *Atlas

Atlas7x13 is an Atlas using basicfont.Face7x13 with the ASCII rune set

func NAtlas added in v0.2.9

func NAtlas(name string, face font.Face, spacing int, runeSets ...[]rune) *Atlas

NAtlas creates a new Atlas containing glyphs of the union of the given sets of runes (plus unicode.ReplacementChar) from the given font face. Spacing is space in pixels that is added around each glyph. This is very usefull when applying shaders to your text, mind that bigger sprites burdens gpu more.

Creating an Atlas is rather expensive, do not create a new Atlas each frame.

Do not destroy or close the font.Face after creating the Atlas. Atlas still uses it.

func (*Atlas) Ascent

func (a *Atlas) Ascent() float64

Ascent returns the distance from the top of the line to the baseline.

func (*Atlas) Contains

func (a *Atlas) Contains(r rune) bool

Contains reports wheter r in contained within the Atlas.

func (*Atlas) Descent

func (a *Atlas) Descent() float64

Descent returns the distance from the baseline to the bottom of the line.

func (*Atlas) DrawRune

func (a *Atlas) DrawRune(prevR, r rune, dot mat.Vec) (rect, frame, bounds mat.AABB, newDot mat.Vec)

DrawRune returns parameters necessary for drawing a rune glyph.

Rect is a rectangle where the glyph should be positioned. Frame is the glyph frame inside the Atlas's Picture. NewDot is the new position of the dot.

func (*Atlas) Glyph

func (a *Atlas) Glyph(r rune) Glyph

Glyph returns the description of r within the Atlas.

func (*Atlas) Kern

func (a *Atlas) Kern(r0, r1 rune) float64

Kern returns the kerning distance between runes r0 and r1. Positive distance means that the glyphs should be further apart.

func (*Atlas) LineHeight

func (a *Atlas) LineHeight() float64

LineHeight returns the recommended vertical distance between two lines of text.

type ColorEffect

type ColorEffect struct {
	EffectBase
	Color mat.RGBA
}

ColorEffect is one shot effect that changes color of a text

func NColorEffect

func NColorEffect(hex string, start int) (*ColorEffect, error)

NColorEffect constructs new color effect as long as s is valid hex color

func (*ColorEffect) Apply

func (e *ColorEffect) Apply(try ggl.Vertexes, _ float64)

Apply implements Effect

func (*ColorEffect) Close

func (e *ColorEffect) Close(endIdx int)

Close implements Effect

func (*ColorEffect) Copy

func (e *ColorEffect) Copy(start int) Effect

Copy implements Effect

func (*ColorEffect) Kind

func (e *ColorEffect) Kind() int8

Kind implements Effect

type Drawer

type Drawer struct {
	*Atlas
	Region mat.Vec
	// contains filtered or unexported fields
}

Drawer draws text for ContentBox

func NDrawer

func NDrawer(atlas *Atlas) *Drawer

NDrawer is drawer constructor

func (*Drawer) Advance

func (d *Drawer) Advance(prev, r rune) (l float64)

Advance calculates glyph advance for this text

func (*Drawer) Draw

func (d *Drawer) Draw(t Target, text string)

Draw draws a text to Target, string is decoded by utf8

func (*Drawer) DrawParagraph added in v0.2.9

func (d *Drawer) DrawParagraph(p *Paragraph, start, end int)

Draw draws a slice of p.Compiled to p.data, text continused where last draw stopped

func (*Drawer) ParagraphControlRune added in v0.2.9

func (d *Drawer) ParagraphControlRune(r rune, p *Paragraph) bool

ControlRune changes dot accordingly if inputted rune is control rune, also returns whether change happened, it also appends a new dot to slice

type Effect

type Effect interface {
	Apply(try ggl.Vertexes, delta float64)
	Kind() int8
	Start() int
	Close(endIdx int)
	Copy(startIdx int) Effect
}

Effect is interface for eny effect applied on text TriangleData is slice that should be modified, delta is to change state of effect in case this is permanent effect

type EffectBase

type EffectBase struct {
	End int
	// contains filtered or unexported fields
}

EffectBase hold properties that every effect has

func (*EffectBase) Close

func (e *EffectBase) Close(endIdx int)

Close implements Effect

func (*EffectBase) Redundant

func (e *EffectBase) Redundant() bool

Redundant ...

func (*EffectBase) Start

func (e *EffectBase) Start() int

Start implements Effect

type Effs

type Effs []Effect

Effs is a standard Vector type with utility methods

func (*Effs) Clear

func (v *Effs) Clear()

Clear is equivalent to Truncate(0)

func (Effs) Clone

func (v Effs) Clone() Effs

Clone creates new Effs copies content of v to it and returns it

func (*Effs) Filter

func (v *Effs) Filter(filter func(e Effect) bool)

Filter leaves only elements for with filter returns true

func (Effs) Find

func (v Effs) Find(find func(e Effect) bool) (idx int, res Effect)

Find returns first element for which find returns true along with index, if there is none, index equals -1

func (Effs) ForEach

func (v Effs) ForEach(con func(i int, e Effect) Effect)

ForEach is a standard foreach method. Its shortcut for modifying all elements

func (*Effs) Insert

func (v *Effs) Insert(idx int, val Effect)

Insert inserts value to given index

func (*Effs) InsertSlice

func (v *Effs) InsertSlice(idx int, val []Effect)

InsertSlice inserts slice to given index

func (Effs) Last

func (v Effs) Last() Effect

Last returns last element of slice

func (*Effs) Pop

func (v *Effs) Pop() Effect

Pop removes last element

func (*Effs) PopFront

func (v *Effs) PopFront() Effect

PopFront removes first element and returns it

func (*Effs) Remove

func (v *Effs) Remove(idx int) (val Effect)

Remove removes element and returns it

func (*Effs) RemoveSlice

func (v *Effs) RemoveSlice(start, end int)

RemoveSlice removes sequence of slice

func (Effs) Reverse

func (v Effs) Reverse()

Reverse reverses content of slice

func (Effs) Sort

func (v Effs) Sort(comp func(a, b Effect) bool)

Sort is quicksort for Effs, because this is part of a template comp function is necessary

func (Effs) Swap

func (v Effs) Swap(a, b int)

Swap swaps two elements

func (*Effs) Truncate

func (v *Effs) Truncate(l int)

Truncate in comparison to truncating by bracket operator also sets all forgoten elements to default value, witch is useful if this is slice of pointers Effs will have length you specify

type FEffs

type FEffs []*FontEffect

FEffs is a standard Vector type with utility methods

func (*FEffs) Clear

func (v *FEffs) Clear()

Clear is equivalent to Truncate(0)

func (FEffs) Clone

func (v FEffs) Clone() FEffs

Clone creates new FEffs copies content of v to it and returns it

func (*FEffs) Filter

func (v *FEffs) Filter(filter func(e *FontEffect) bool)

Filter leaves only elements for with filter returns true

func (FEffs) Find

func (v FEffs) Find(find func(e *FontEffect) bool) (idx int, res *FontEffect)

Find returns first element for which find returns true along with index, if there is none, index equals -1

func (FEffs) ForEach

func (v FEffs) ForEach(con func(i int, e *FontEffect) *FontEffect)

ForEach is a standard foreach method. Its shortcut for modifying all elements

func (*FEffs) Insert

func (v *FEffs) Insert(idx int, val *FontEffect)

Insert inserts value to given index

func (*FEffs) InsertSlice

func (v *FEffs) InsertSlice(idx int, val []*FontEffect)

InsertSlice inserts slice to given index

func (FEffs) Last

func (v FEffs) Last() *FontEffect

Last returns last element of slice

func (*FEffs) Pop

func (v *FEffs) Pop() *FontEffect

Pop removes last element

func (*FEffs) PopFront

func (v *FEffs) PopFront() *FontEffect

PopFront removes first element and returns it

func (*FEffs) Remove

func (v *FEffs) Remove(idx int) (val *FontEffect)

Remove removes element and returns it

func (*FEffs) RemoveSlice

func (v *FEffs) RemoveSlice(start, end int)

RemoveSlice removes sequence of slice

func (FEffs) Reverse

func (v FEffs) Reverse()

Reverse reverses content of slice

func (FEffs) Sort

func (v FEffs) Sort(comp func(a, b *FontEffect) bool)

Sort is quicksort for FEffs, because this is part of a template comp function is necessary

func (FEffs) Swap

func (v FEffs) Swap(a, b int)

Swap swaps two elements

func (*FEffs) Truncate

func (v *FEffs) Truncate(l int)

Truncate in comparison to truncating by bracket operator also sets all forgoten elements to default value, witch is useful if this is slice of pointers FEffs will have length you specify

type FontEffect

type FontEffect struct {
	EffectBase
	Font string
}

FontEffect stores what font should be used for given slice of text

func NFontEffect

func NFontEffect(font string, start, end int) *FontEffect

NFontEffect is here for consistency

func (*FontEffect) Apply

func (f *FontEffect) Apply(_ ggl.Vertexes, _ float64)

Apply implements Effect

func (*FontEffect) Copy

func (f *FontEffect) Copy(start int) Effect

Copy implements Effect

func (*FontEffect) Kind

func (f *FontEffect) Kind() int8

Kind implements Effect

type Glyph

type Glyph struct {
	Dot     mat.Vec
	Frame   mat.AABB
	Advance float64
}

Glyph describes one glyph in an Atlas.

type Markdown

type Markdown struct {
	Shortcuts map[rune]string
	Fonts     map[string]*Drawer
	Effects   map[string]Effect
	// contains filtered or unexported fields
}

Markdown handles text markdown parsing, markdown sintax is as follows:

#FF0000[hello] 	// hello will appear ugly red
#FF000099[hello] // hello will appear ugly red and slightly transparent

Now writing effects like this is maybe flexible but not always convenient to user thats why you can define your own effects by adding effects to Markdown.Effects. Name of the effect is a key in map thus:

m.Effects["red"] = ColorEffect{Color: mat.RGB(1, 0, 0)}

will do the trick. user then can use this effect like:

!red[hello] // witch does the same

now you can make any kind of triangle mutator you like, it even allows dinamic effects so something like fading text or exploding text is possible. You can also use multiple fonts in one paragraph by adding more atlases to your markdown. Pattern is same as for adding custom effects:

m.Effects["italic"] = NDrawer(italicFontAtlas)

User then can use font like:

!italic[hello] // hello will be italic, syntax will not appear

Last feature of markdown are shortcuts. After you have added all effects you wanted you can call GenerateShortcuts method. This will map all effect names to its starting rune thus user can be very lazy:

!i[hello] // hello will be italic with low effort

Markdown is only compatible with paragraph, mind that parsing markdown is slow and grows linearly with text length, O(n), if course if you want effects to even display you have to set DisplayEffects to true in paragraph

func NMarkdown

func NMarkdown() *Markdown

NMarkdown initializes inner maps and adds default drawer

func (*Markdown) CollectEffects

func (m *Markdown) CollectEffects(p *Paragraph)

CollectEffects removes all valid effect syntax and stores parsed effects in paragraph

func (*Markdown) GenerateShortcuts

func (m *Markdown) GenerateShortcuts()

GenerateShortcuts creates shortcuts for all effects, if names overlap random one is bind

func (*Markdown) MakeTriangles

func (m *Markdown) MakeTriangles(p *Paragraph)

MakeTriangles creates triangles, these are not drawn to screen as only instant effects are applied on them and you have to call Update on paragraph for anything to show up

func (*Markdown) Parse

func (m *Markdown) Parse(p *Paragraph)

Parse turns markdown stored in p.Content into final text with effects, for markdown syntax see struct documentation

func (*Markdown) ResolveChunks

func (m *Markdown) ResolveChunks(p *Paragraph)

ResolveChunks gets rid of nested FontEffects as nesting of then does not make sense it turns ranges like 0-10 3-7 to 0-3 3-7 7-10 so no ranges overlap.

type Paragraph

type Paragraph struct {
	// saves the paragraph transformation, you have to call Update to apply changes
	mat.Tran

	// this data can be freely modified red, serialized, after Update its overwritten
	// anyway
	Data ggl.Data

	// determines how text should wrap, Drawer will tri to display text so
	// it does not overflows Width, though it only breaks on spaces, if width
	// is 0 it will never wrap
	Width float64
	// Fields are only relevent if Custom Lineheight is true, othervise they will
	// get overwritten
	LineHeight, Ascent, Descent float64
	// if this is true no effects are displayed
	NoEffects bool
	// set this to true if you want it custom
	CustomLineheight bool

	// mask is what all triangles solors will be multiplied by
	Mask mat.RGBA

	// Font determinate base
	Font string
	// Content is only important to markdown, when you are drawing directly with
	// drawer, Content is not used
	Content str.String

	// Align defines how the text is aligned
	Align

	Compiled str.String
	// contains filtered or unexported fields
}

Paragraph stores triangles that can form text, there is a lot of passive configurations and to apply changes you have to pass Paragraph to Markdown.Parse method thought you can also use Drawer directly to draw text though then you have to initialize lineheight

func (*Paragraph) AddEff

func (p *Paragraph) AddEff(e Effect)

AddEff appends chunk of test to paragraph

func (*Paragraph) Bounds

func (p *Paragraph) Bounds() mat.AABB

Bounds returns bounding rectangle of untransformed text, bounds are valid only after Markdown.Parse call

func (*Paragraph) Changes

func (p *Paragraph) Changes() bool

Changes returns whether p.Update will change triangles

func (*Paragraph) Clear

func (p *Paragraph) Clear()

Clear is only usefull when drawing to paragraph directly with drawer it clears triangles

func (*Paragraph) CursorFor

func (p *Paragraph) CursorFor(mouse mat.Vec) (global, local, line int)

CursorFor returns index of a dot on witch the cursor is nad insertcion index that is where you should insert the text to make it look like cursor is writing. Then there is line local position and index of a line

func (*Paragraph) Dot

func (p *Paragraph) Dot(i int) mat.Vec

Dot returns dot at given index

func (*Paragraph) Draw

func (p *Paragraph) Draw(t ggl.Target)

Draw draws its triangles to given target

func (*Paragraph) Lines

func (p *Paragraph) Lines() int

Lines returns count of lines in paragraph

func (*Paragraph) ProjectLine

func (p *Paragraph) ProjectLine(i, line int) int

ProjectLine projects line and local index intro global index complexity is O(1)

func (*Paragraph) SetCenter

func (p *Paragraph) SetCenter(pos mat.Vec)

SetCenter moves paragraph so it is centered at given position

func (*Paragraph) Sort

func (p *Paragraph) Sort()

Sort is part of markdown building procedure, it sorts all effects so they can be applied properly

func (*Paragraph) UnprojectLine

func (p *Paragraph) UnprojectLine(idx int) (local, line int)

UnprojectLine does reverse of Project line complexity is O(n) where n is len(p.lines)

func (*Paragraph) Update

func (p *Paragraph) Update(delta float64)

Update has to be called after changes or they will not be visible, it returns false if there is nothing to draw

type Target added in v0.2.9

type Target interface {
	ggl.Target
	// Metrics returns current dot and bounds of Target
	Metrics() (mat.Vec, mat.AABB)
	// SetMetrics sets the metrics of Target to new ones
	SetMetrics(mat.Vec, mat.AABB)
}

Target is something that Drawer can draw to

type Text added in v0.2.9

type Text struct {
	ggl.Data

	Dot    mat.Vec
	Bounds mat.AABB
	// contains filtered or unexported fields
}

Text is a builtin Drawer target, it can act as sprite

func (*Text) Draw added in v0.2.9

func (t *Text) Draw(tg ggl.Target, mat mat.Mat, color mat.RGBA)

Draw implements ggl.Drawer interface

func (*Text) DrawCentered added in v0.2.9

func (t *Text) DrawCentered(tg ggl.Target, mat mat.Mat, color mat.RGBA)

Draws the text centered on matrix center

func (*Text) Fetch added in v0.2.9

func (t *Text) Fetch(tg ggl.Target)

Fetch implements ggl.Fetcher interface

func (*Text) Metrics added in v0.2.9

func (t *Text) Metrics() (mat.Vec, mat.AABB)

Metrics implements Target interface

func (*Text) SetMetrics added in v0.2.9

func (t *Text) SetMetrics(dot mat.Vec, bounds mat.AABB)

SetMetrics implements Target interface

func (*Text) Update added in v0.2.9

func (t *Text) Update(mat mat.Mat, color mat.RGBA)

Update updates the text color and transforation

Jump to

Keyboard shortcuts

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