badge

package
v0.6.1 Latest Latest
Warning

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

Go to latest
Published: Jun 5, 2026 License: AGPL-3.0, AGPL-3.0-only Imports: 11 Imported by: 0

Documentation

Overview

Package badge provides a configurable SVG badge engine with dynamic font measurement.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ContrastColor added in v0.6.0

func ContrastColor(hex string) string

ContrastColor returns a legible text color for the given hex background. Light backgrounds (luminance > 0.5) get near-black text; dark backgrounds get white.

func StatusColor

func StatusColor(status string) string

StatusColor maps a status keyword to a badge hex color.

Types

type Badge

type Badge struct {
	Label string // left side text
	Value string // right side text
	Color string // hex color for right side (e.g. "#4c1")
}

Badge defines the content and appearance of a single badge.

type Engine

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

Engine generates SVG badges using a specific font.

func New

func New(metrics *FontMetrics) *Engine

New creates a badge engine with the given font metrics.

func NewDefault added in v0.4.0

func NewDefault() (*Engine, error)

NewDefault creates an engine with dejavu-sans 11pt (the standard).

func NewForSpec added in v0.4.0

func NewForSpec(font string, fontSize float64, fontFile string) (*Engine, error)

NewForSpec creates an engine from font override parameters. Falls back to dejavu-sans if no overrides given.

func (*Engine) Generate

func (e *Engine) Generate(b Badge) string

Generate produces a shields.io-compatible SVG badge string.

type FontMetrics

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

FontMetrics holds measured glyph widths and font data for SVG embedding.

func LoadBuiltinFont

func LoadBuiltinFont(name string, size float64) (*FontMetrics, error)

LoadBuiltinFont loads an embedded font by config name.

func LoadFont

func LoadFont(name string, data []byte, size float64) (*FontMetrics, error)

LoadFont loads a TTF/OTF from raw bytes and measures glyph advances at the given size. This is the single code path for ALL fonts — built-in and custom alike.

func LoadFontFile

func LoadFontFile(path string, size float64) (*FontMetrics, error)

LoadFontFile loads a TTF/OTF from a filesystem path.

func (*FontMetrics) FontData

func (m *FontMetrics) FontData() []byte

FontData returns the raw font bytes for SVG embedding.

func (*FontMetrics) FontName

func (m *FontMetrics) FontName() string

FontName returns the font family name.

func (*FontMetrics) FontSize

func (m *FontMetrics) FontSize() float64

FontSize returns the configured point size.

func (*FontMetrics) TextWidth

func (m *FontMetrics) TextWidth(s string) float64

TextWidth returns the pixel width of s using measured glyph advances.

Jump to

Keyboard shortcuts

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