package textmeasure
Import Path
github.com/pancsta/d2/lib/textmeasure (on go.dev)
Dependency Relation
imports 29 packages, and imported by 9 packages
Involved Source Files
atlas.go
atlas_metrics.go
fontface.go
links.go
markdown.go
markdown_svg.go
rect.go
substitutions.go
textmeasure.go
Package-Level Type Names (total 9)
MarkdownColorRole is a semantic Markdown color. It mirrors the roles used by
github-markdown.css without coupling text measurement to a D2 theme.
const MarkdownColorAccent
const MarkdownColorBorder
const MarkdownColorBorderMuted
const MarkdownColorCanvas
const MarkdownColorCanvasSubtle
const MarkdownColorForeground
const MarkdownColorForegroundStroke
const MarkdownColorMuted
const MarkdownColorMutedStroke
const MarkdownColorNeutralMuted
const MarkdownColorNone
MarkdownFontRole lets an SVG renderer select the correct embedded D2 font.
Font sizes are kept separately on each primitive.
const MarkdownFontBold
const MarkdownFontItalic
const MarkdownFontMono
const MarkdownFontMonoBold
const MarkdownFontMonoItalic
const MarkdownFontMonoSemibold
const MarkdownFontRegular
const MarkdownFontSemibold
MarkdownLayout contains the exact dimensions used by D2 layout and the
positioned primitives used to paint those dimensions. MeasureMarkdown calls
LayoutMarkdown, ensuring measurement and rendering share one code path.
Corpus contains the visible source text used by the primitives. Renderers
embedding subset fonts should include it.
Height int
Primitives []MarkdownPrimitive
Width int
SVG serializes a MarkdownLayout as native SVG elements. It deliberately
emits a fragment rather than an <svg> document so d2svg can translate and
theme it inside a diagram.
func LayoutMarkdown(mdText string, ruler *Ruler, fontFamily *d2fonts.FontFamily, monoFontFamily *d2fonts.FontFamily, fontSize int) (*MarkdownLayout, error)
MarkdownPrimitive is a serializer-neutral native SVG drawing operation.
Text uses X/Y as the baseline origin and Width/Height as its measured box.
Rect uses X/Y/Width/Height/Radius. Line uses X/Y and X2/Y2. FillRole and
StrokeRole are semantic so light/dark themes can serialize the same layout.
Decoration MarkdownTextDecoration
FillRole MarkdownColorRole
Font MarkdownFontRole
FontSize float64
Height float64
Kind MarkdownPrimitiveKind
Link string
LinkTitle string
Radius float64
StrokeRole MarkdownColorRole
StrokeWidth float64
SyntheticBold/SyntheticItalic preserve inherited CSS axes when the
innermost Markdown element selects a different concrete D2 font family.
For example, em > strong uses the bold face with a synthetic slant, while
strong > em uses the italic face with synthetic weight.
SyntheticItalic bool
Text string
TextLength fits the glyphs to Width. CSS paints a discretionary soft
hyphen at one third of an em even inside D2's monospace code face.
Width float64
X float64
X2 float64
Y float64
Y2 float64
MarkdownPrimitiveKind identifies the kind of native SVG primitive in a
MarkdownLayout. The layout package intentionally does not choose concrete
colors so that renderers can map the semantic roles to their active theme.
const MarkdownLinePrimitive
const MarkdownRectPrimitive
const MarkdownTextPrimitive
MarkdownSVGOptions controls native SVG serialization without affecting
layout. A renderer can override role paints and font classes to match its
theme/font embedding scheme.
Class string
DisableLinks emits linked text without inner <a> elements. d2svg uses
this when a Markdown label is already wrapped by a shape/connection link.
FontClasses map[MarkdownFontRole]string
RolePaint map[MarkdownColorRole]MarkdownSVGPaint
Underline applies D2's style.underline to every Markdown text primitive.
func (*MarkdownLayout).SVG(opts MarkdownSVGOptions) string
MarkdownSVGPaint controls how a semantic role is emitted by SVG. Class is
optional; Color is an SVG paint value such as "currentColor", "#fff", or a
CSS variable.
Class string
Color string
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 LayoutMarkdown(mdText string, ruler *Ruler, fontFamily *d2fonts.FontFamily, monoFontFamily *d2fonts.FontFamily, fontSize int) (*MarkdownLayout, error)
func MeasureMarkdown(mdText string, ruler *Ruler, fontFamily *d2fonts.FontFamily, monoFontFamily *d2fonts.FontFamily, fontSize int) (width, height int, err error)
func github.com/pancsta/d2/d2graph.GetTextDimensions(mtexts []*d2target.MText, ruler *Ruler, t *d2target.MText, fontFamily *d2fonts.FontFamily) *d2target.TextDimensions
func github.com/pancsta/d2/d2graph.GetTextDimensionsWithMono(mtexts []*d2target.MText, ruler *Ruler, t *d2target.MText, fontFamily *d2fonts.FontFamily, monoFontFamily *d2fonts.FontFamily) *d2target.TextDimensions
func github.com/pancsta/d2/d2graph.(*Graph).SetDimensions(mtexts []*d2target.MText, ruler *Ruler, fontFamily *d2fonts.FontFamily, monoFontFamily *d2fonts.FontFamily) error
func github.com/pancsta/d2/d2graph.(*Object).GetDefaultSize(mtexts []*d2target.MText, ruler *Ruler, fontFamily *d2fonts.FontFamily, monoFontFamily *d2fonts.FontFamily, labelDims d2target.TextDimensions, withLabelPadding bool) (*d2target.TextDimensions, error)
func github.com/pancsta/d2/d2graph.(*Object).GetLabelSize(mtexts []*d2target.MText, ruler *Ruler, fontFamily *d2fonts.FontFamily, monoFontFamily *d2fonts.FontFamily) (*d2target.TextDimensions, error)
Package-Level Functions (total 11)
HeaderToFontScale returns github-markdown.css's exact heading size. The
legacy measurement path keeps HeaderToFontSize's integer truncation so graph
dimensions remain stable, while native SVG painting uses this scale and can
reproduce fractional CSS sizes such as h6's 13.6px at a 16px base.
func HeaderToFontSize(baseFontSize int, header string) int
IsDangerousLink reports whether link uses a URL scheme that must not be
exposed as interactive metadata. It intentionally shares Goldmark's
dangerous-URL policy with Markdown links while preserving relative URLs,
board targets, and opaque application schemes.
LayoutMarkdown parses Markdown once, measures it with D2's existing box
model, and paints that same box model into native SVG primitives.
func MeasureMarkdown(mdText string, ruler *Ruler, fontFamily *d2fonts.FontFamily, monoFontFamily *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 := parseFont(goregular.TTF)
if err != nil {
panic(err)
}
face := ttf.newFace(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
ReplaceSubstitutionsMarkdownBounded replaces variables outside Markdown code
spans and blocks while bounding candidate-match work and bytes inserted by
replacements. It checks the bound before each allocating replacement.
SafeMarkdownLink returns link when it is safe to expose as interactive
metadata and an empty string for unsafe Markdown URL schemes. PDF and PPTX
annotations use the same policy.
Package-Level Variables (only one)
Runes encompasses ASCII, Latin-1, and geometric shapes like black square
Package-Level Constants (total 52)
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
const MarkdownColorAccent MarkdownColorRole = "accent" const MarkdownColorBorder MarkdownColorRole = "border" const MarkdownColorBorderMuted MarkdownColorRole = "border-muted" const MarkdownColorCanvas MarkdownColorRole = "canvas" const MarkdownColorCanvasSubtle MarkdownColorRole = "canvas-subtle" const MarkdownColorForeground MarkdownColorRole = "foreground" const MarkdownColorForegroundStroke MarkdownColorRole = "foreground-stroke" const MarkdownColorMuted MarkdownColorRole = "muted" const MarkdownColorMutedStroke MarkdownColorRole = "muted-stroke" const MarkdownColorNeutralMuted MarkdownColorRole = "neutral-muted" const MarkdownColorNone MarkdownColorRole = "" const MarkdownFontBold MarkdownFontRole = "bold" const MarkdownFontItalic MarkdownFontRole = "italic" const MarkdownFontMono MarkdownFontRole = "mono" const MarkdownFontMonoBold MarkdownFontRole = "mono-bold" const MarkdownFontMonoItalic MarkdownFontRole = "mono-italic" const MarkdownFontMonoSemibold MarkdownFontRole = "mono-semibold" const MarkdownFontRegular MarkdownFontRole = "regular" const MarkdownFontSemibold MarkdownFontRole = "semibold"
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 MarkdownLinePrimitive MarkdownPrimitiveKind = "line" const MarkdownRectPrimitive MarkdownPrimitiveKind = "rect" const MarkdownTextDecorationLineThrough MarkdownTextDecoration = "line-through" const MarkdownTextDecorationNone MarkdownTextDecoration = "" const MarkdownTextPrimitive MarkdownPrimitiveKind = "text"
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.7. (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. |