d2fonts

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Sep 7, 2026 License: MPL-2.0 Imports: 23 Imported by: 0

README

d2fonts

The SVG renderer embeds fonts directly into the SVG as base64 data. This is to give deterministic outputs and load without a network call.

To include your own font, e.g. Helvetica, you must include the Truetype glyphs:

  • ./ttf/Helvetica-Bold.ttf
  • ./ttf/Helvetica-Italic.ttf
  • ./ttf/Helvetica-Regular.ttf

You must also include an encoded version of these of mimetype application/font-woff:

  • ./ttf/Helvetica-Bold.txt
  • ./ttf/Helvetica-Italic.txt
  • ./ttf/Helvetica-Regular.txt

If you include a font to contribute, it must have an open license.

Documentation

Overview

TODO write a script to do this as part of CI Currently using an online converter: https://dopiaza.org/tools/datauri/index.php

Package d2fonts provides D2's embedded font families and bounded native fallback resolution for renderer text.

Index

Constants

View Source
const (
	FONT_SIZE_XS   = 13
	FONT_SIZE_S    = 14
	FONT_SIZE_M    = 16
	FONT_SIZE_L    = 20
	FONT_SIZE_XL   = 24
	FONT_SIZE_XXL  = 28
	FONT_SIZE_XXXL = 32

	FONT_STYLE_REGULAR  FontStyle = "regular"
	FONT_STYLE_BOLD     FontStyle = "bold"
	FONT_STYLE_SEMIBOLD FontStyle = "semibold"
	FONT_STYLE_ITALIC   FontStyle = "italic"

	SourceSansPro FontFamily = "SourceSansPro"
	SourceCodePro FontFamily = "SourceCodePro"
	HandDrawn     FontFamily = "HandDrawn"
)

Variables

View Source
var D2_FONT_TO_FAMILY = map[string]FontFamily{
	"default": SourceSansPro,
	"mono":    SourceCodePro,
}
View Source
var FontEncodings syncmap.SyncMap[Font, string]
View Source
var FontFaces syncmap.SyncMap[Font, []byte]
View Source
var FontFamiliesMu sync.Mutex

Functions

func AddFontStyle

func AddFontStyle(font Font, style FontStyle, ttf []byte) error

Types

type BundledFallbackLimits added in v0.9.0

type BundledFallbackLimits struct {
	MaxRequestedRunes int
	// MaxBundledBytes defaults to MaxResolvedBytes when zero.
	MaxBundledBytes  int64
	MaxResolvedBytes int64
}

BundledFallbackLimits bound all requests and returned bytes over the lifetime of one composite resolver. MaxBundledBytes reserves an explicit sub-budget for owned copies of D2's trusted font; MaxResolvedBytes bounds those copies plus downstream results. The downstream resolver remains responsible for its own discovery and coverage-work limits.

type BundledFallbackResolver added in v0.9.0

type BundledFallbackResolver struct {
	// contains filtered or unexported fields
}

BundledFallbackResolver resolves supported symbols and emoji from D2's pinned Noto Color Emoji face before consulting the host. This makes those glyphs deterministic across machines while leaving other scripts to a bounded downstream resolver. On js/wasm the bundled face is absent and all requests pass through to downstream.

func NewBundledFallbackResolver added in v0.9.0

func NewBundledFallbackResolver(next FallbackResolver, limits BundledFallbackLimits) (*BundledFallbackResolver, error)

func (*BundledFallbackResolver) ResolveFallbacks added in v0.9.0

func (r *BundledFallbackResolver) ResolveFallbacks(ctx context.Context, request FallbackRequest) ([]FallbackFont, error)

type FallbackFont added in v0.9.0

type FallbackFont struct {
	Name      string
	MIMEType  string
	Data      []byte
	FaceIndex uint16
}

FallbackFont is one fully resolved host font face. Data is owned by the result and may be retained in a network-free renderer scene. FaceIndex selects a face when Data contains an OpenType collection.

type FallbackRequest added in v0.9.0

type FallbackRequest struct {
	Runes  []rune
	Family string
	Style  string
	Weight int
}

FallbackRequest carries both missing code points and the source text style. Resolvers may ignore style when a face is intrinsically universal, but must preserve it when delegating so bold, italic, semibold, and monospace text do not silently fall back to an unrelated regular proportional face.

type FallbackResolver added in v0.9.0

type FallbackResolver interface {
	ResolveFallbacks(context.Context, FallbackRequest) ([]FallbackFont, error)
}

FallbackResolver resolves an ordered set of font faces covering runes not present in a D2 label's configured primary font. An empty or partial result means the remaining runes have no available face; it is not an error. Implementations must return owned immutable bytes and must honor context cancellation between bounded I/O operations. Malformed resources and work or byte limit violations remain errors.

type Font

type Font struct {
	Family FontFamily
	Style  FontStyle
	Size   int
}

func (Font) GetEncodedSubset

func (f Font) GetEncodedSubset(corpus string) string

type FontFamily

type FontFamily string

func AddFontFamily

func AddFontFamily(name string, regularTTF, italicTTF, boldTTF, semiboldTTF []byte) (*FontFamily, error)

func (FontFamily) Font

func (f FontFamily) Font(size int, style FontStyle) Font

type FontStyle

type FontStyle string

type SystemFallbackLimits added in v0.9.0

type SystemFallbackLimits struct {
	MaxDirectoryEntries int
	MaxFiles            int
	MaxFaces            int
	MaxRequestedRunes   int
	MaxCoverageChecks   int64
	MaxFileBytes        int64
	MaxScannedBytes     int64
	MaxResolvedBytes    int64
}

SystemFallbackLimits bounds deterministic host-font discovery for the lifetime of one resolver. FileBytes bounds one candidate; every other work/byte limit is cumulative across indexing attempts and ResolveFallbacks calls. A CLI export owns one resolver, so these are operation-wide ceilings rather than per-board or per-style allowances.

type SystemFallbackResolver added in v0.9.0

type SystemFallbackResolver struct {
	// contains filtered or unexported fields
}

SystemFallbackResolver discovers fonts only under the operating system's conventional system font roots. It does not follow symlinks or consult a platform API, keeping discovery CGO-free and auditable. User-installed D2 primary fonts continue to flow through AddFontFamily and do not need this resolver.

func NewSystemFallbackResolver added in v0.9.0

func NewSystemFallbackResolver(limits SystemFallbackLimits) (*SystemFallbackResolver, error)

func (*SystemFallbackResolver) ResolveFallbacks added in v0.9.0

func (r *SystemFallbackResolver) ResolveFallbacks(ctx context.Context, request FallbackRequest) ([]FallbackFont, error)

Jump to

Keyboard shortcuts

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