textmeasure

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Mar 30, 2023 License: MPL-2.0 Imports: 19 Imported by: 40

Documentation

Index

Constants

View Source
const (
	MarkdownFontSize     = d2fonts.FONT_SIZE_M
	MarkdownLineHeight   = 1.5
	MarkdownLineHeightPx = MarkdownFontSize * MarkdownLineHeight

	PaddingLeft_ul_ol = 32
	MarginBottom_ul   = MarkdownFontSize

	MarginTop_li_p = MarkdownFontSize
	MarginBottom_p = MarkdownFontSize

	LineHeight_h           = 1.25
	MarginTop_h            = 24
	MarginBottom_h         = 16
	PaddingBottom_h1_h2_em = 0.3
	BorderBottom_h1_h2     = 1

	Height_hr          = 4
	MarginTopBottom_hr = 24

	Padding_pre          = 16
	MarginBottom_pre     = 16
	LineHeight_pre       = 1.45
	FontSize_pre_code_em = 0.85

	PaddingTopBottom_code_em = 0.2
	PaddingLeftRight_code_em = 0.4

	PaddingLR_blockquote_em  = 1.
	MarginBottom_blockquote  = 16
	BorderLeft_blockquote_em = 0.25

	FONT_SIZE_H1 = d2fonts.FONT_SIZE_XXXL
	FONT_SIZE_H2 = d2fonts.FONT_SIZE_XL
	FONT_SIZE_H3 = d2fonts.FONT_SIZE_L
	FONT_SIZE_H4 = d2fonts.FONT_SIZE_M
	FONT_SIZE_H5 = d2fonts.FONT_SIZE_S
	FONT_SIZE_H6 = d2fonts.FONT_SIZE_XS
)

these are css values from github-markdown.css so we can accurately compute the rendered dimensions

View Source
const SIZELESS_FONT_SIZE = 0
View Source
const TAB_SIZE = 4

Variables

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 HeaderToFontSize = map[string]int{
	"h1": FONT_SIZE_H1,
	"h2": FONT_SIZE_H2,
	"h3": FONT_SIZE_H3,
	"h4": FONT_SIZE_H4,
	"h5": FONT_SIZE_H5,
	"h6": FONT_SIZE_H6,
}

Functions

func MeasureMarkdown

func MeasureMarkdown(mdText string, ruler *Ruler, fontFamily *d2fonts.FontFamily) (width, height int, err error)

func NewAtlas

func NewAtlas(face font.Face, runeSets ...[]rune) *atlas

NewAtlas creates a new atlas containing glyphs of the union of the given sets of runes (plus unicode.ReplacementChar) from the given font face.

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 RenderMarkdown

func RenderMarkdown(m string) (string, error)

Types

type Ruler

type Ruler struct {
	// Orig specifies the text origin, usually the top-left dot position. Dot is always aligned
	// to Orig when writing newlines.
	Orig *geo.Point

	// Dot is the position where the next character will be written. Dot is automatically moved
	// when writing to a Ruler object, but you can also manipulate it manually
	Dot *geo.Point

	// lineHeight is the vertical distance between two lines of text.
	//
	// Example:
	//   txt.lineHeight = 1.5 * txt.atlas.lineHeight
	LineHeightFactor float64
	// contains filtered or unexported fields
}

Ruler allows for effiecient and convenient text drawing.

To create a Ruler object, use the New constructor:

txt := text.New(pixel.ZV, text.NewAtlas(face, text.ASCII))

As suggested by the constructor, a Ruler object is always associated with one font face and a fixed set of runes. For example, the Ruler we created above can draw text using the font face contained in the face variable and is capable of drawing ASCII characters.

Here we create a Ruler object which can draw ASCII and Katakana characters:

txt := text.New(0, text.NewAtlas(face, text.ASCII, text.RangeTable(unicode.Katakana)))

Similarly to IMDraw, Ruler functions as a buffer. It implements io.Writer interface, so writing text to it is really simple:

fmt.Print(txt, "Hello, world!")

Newlines, tabs and carriage returns are supported.

Finally, if we want the written text to show up on some other Target, we can draw it:

txt.Draw(target)

Ruler exports two important fields: Orig and Dot. Dot is the position where the next character will be written. Dot is automatically moved when writing to a Ruler object, but you can also manipulate it manually. Orig specifies the text origin, usually the top-left dot position. Dot is always aligned to Orig when writing newlines. The Clear method resets the Dot to Orig.

func NewRuler

func NewRuler() (*Ruler, error)

New creates a new Ruler capable of drawing runes contained in the provided atlas. Orig and Dot will be initially set to orig.

Here we create a Ruler capable of drawing ASCII characters using the Go Regular font.

ttf, err := truetype.Parse(goregular.TTF)
if err != nil {
    panic(err)
}
face := truetype.NewFace(ttf, &truetype.Options{
    Size: 14,
})
txt := text.New(orig, text.NewAtlas(face, text.ASCII))

func (*Ruler) HasFontFamilyLoaded added in v0.2.0

func (r *Ruler) HasFontFamilyLoaded(fontFamily *d2fonts.FontFamily) bool

func (*Ruler) Measure

func (t *Ruler) Measure(font d2fonts.Font, s string) (width, height int)

func (*Ruler) MeasurePrecise

func (t *Ruler) MeasurePrecise(font d2fonts.Font, s string) (width, height float64)

Jump to

Keyboard shortcuts

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