package textmeasure
Import Path
oss.terrastruct.com/d2/lib/textmeasure (on go.dev)
Dependency Relation
imports 22 packages, and imported by 6 packages
Package-Level Type Names (only one)
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.
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
lineHeight is the vertical distance between two lines of text.
Example:
txt.lineHeight = 1.5 * txt.atlas.lineHeight
Orig specifies the text origin, usually the top-left dot position. Dot is always aligned
to Orig when writing newlines.
(*Ruler) HasFontFamilyLoaded(fontFamily *d2fonts.FontFamily) bool
(*Ruler) Measure(font d2fonts.Font, s string) (width, height int)
(*Ruler) MeasureMono(font d2fonts.Font, s string) (width, height int)
(*Ruler) MeasurePrecise(font d2fonts.Font, s string) (width, height float64)
func NewRuler() (*Ruler, error)
func MeasureMarkdown(mdText string, ruler *Ruler, fontFamily *d2fonts.FontFamily, fontSize int) (width, height int, err error)
func oss.terrastruct.com/d2/d2graph.GetTextDimensions(mtexts []*d2target.MText, ruler *Ruler, t *d2target.MText, fontFamily *d2fonts.FontFamily) *d2target.TextDimensions
func oss.terrastruct.com/d2/d2graph.(*Graph).SetDimensions(mtexts []*d2target.MText, ruler *Ruler, fontFamily *d2fonts.FontFamily) error
func oss.terrastruct.com/d2/d2graph.(*Object).GetDefaultSize(mtexts []*d2target.MText, ruler *Ruler, fontFamily *d2fonts.FontFamily, labelDims d2target.TextDimensions, withLabelPadding bool) (*d2target.TextDimensions, error)
func oss.terrastruct.com/d2/d2graph.(*Object).GetLabelSize(mtexts []*d2target.MText, ruler *Ruler, fontFamily *d2fonts.FontFamily) (*d2target.TextDimensions, error)
Package-Level Functions (total 6)
func HeaderToFontSize(baseFontSize int, header string) int func MeasureMarkdown(mdText string, ruler *Ruler, fontFamily *d2fonts.FontFamily, fontSize int) (width, height int, err error)
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.
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 RenderMarkdown(m string) (string, error) func ReplaceSubstitutionsMarkdown(mdText string, variables map[string]string) string
Package-Level Variables (only one)
Runes encompasses ASCII, Latin-1, and geometric shapes like black square
Package-Level Constants (total 26)
these are css values from github-markdown.css so we can accurately compute the rendered dimensions
these are css values from github-markdown.css so we can accurately compute the rendered dimensions
const CODE_LINE_HEIGHT = 1.3
these are css values from github-markdown.css so we can accurately compute the rendered dimensions
these are css values from github-markdown.css so we can accurately compute the rendered dimensions
these are css values from github-markdown.css so we can accurately compute the rendered dimensions
these are css values from github-markdown.css so we can accurately compute the rendered dimensions
these are css values from github-markdown.css so we can accurately compute the rendered dimensions
these are css values from github-markdown.css so we can accurately compute the rendered dimensions
these are css values from github-markdown.css so we can accurately compute the rendered dimensions
these are css values from github-markdown.css so we can accurately compute the rendered dimensions
these are css values from github-markdown.css so we can accurately compute the rendered dimensions
these are css values from github-markdown.css so we can accurately compute the rendered dimensions
these are css values from github-markdown.css so we can accurately compute the rendered dimensions
these are css values from github-markdown.css so we can accurately compute the rendered dimensions
these are css values from github-markdown.css so we can accurately compute the rendered dimensions
these are css values from github-markdown.css so we can accurately compute the rendered dimensions
these are css values from github-markdown.css so we can accurately compute the rendered dimensions
these are css values from github-markdown.css so we can accurately compute the rendered dimensions
these are css values from github-markdown.css so we can accurately compute the rendered dimensions
these are css values from github-markdown.css so we can accurately compute the rendered dimensions
these are css values from github-markdown.css so we can accurately compute the rendered dimensions
these are css values from github-markdown.css so we can accurately compute the rendered dimensions
these are css values from github-markdown.css so we can accurately compute the rendered dimensions
const SIZELESS_FONT_SIZE = 0 const TAB_SIZE = 4![]() |
The pages are generated with Golds v0.8.2. (GOOS=linux GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds. |