Documentation
¶
Overview ¶
Package ansifonts provides a library for rendering text using ANSI art fonts.
The loader package handles font discovery and loading from the fonts directory.
Package ansifonts provides a library for rendering text using ANSI art fonts.
The renderer package handles text rendering with various formatting options.
Package ansifonts provides a standalone library for rendering text using ANSI art fonts.
The library dynamically discovers ANSI fonts from a fonts directory and provides simple APIs for loading fonts and rendering text with various formatting options.
Basic usage:
font, _ := ansifonts.LoadFont("dogica")
lines := ansifonts.RenderText("Hello", font)
for _, line := range lines {
fmt.Println(line)
}
Advanced usage with options:
options := ansifonts.RenderOptions{
CharSpacing: 2,
TextColor: "#FF0000",
UseGradient: true,
GradientColor: "#0000FF",
}
lines := ansifonts.RenderTextWithOptions("Hello", font, options)
Index ¶
- Constants
- Variables
- func ApplyHorizontalAlignment(line string, canvasWidth int, alignment HorizontalAlignment) string
- func ApplyVerticalAlignment(lines []string, canvasHeight int, alignment VerticalAlignment) []string
- func DetectHalfPixelUsage(text string, fontData FontData, scaleFactor float64) bool
- func ListFonts() ([]string, error)
- func RegisterCustomPath(path string) ([]string, error)
- func RegisterFontDirectory(dirPath string) ([]string, error)
- func RegisterFontFile(path string) (string, error)
- func RenderText(text string, font *Font) []string
- func RenderTextWithColor(text string, font *Font, colorCode string) []string
- func RenderTextWithFont(text string, fontData FontData, options RenderOptions) []string
- func RenderTextWithOptions(text string, font *Font, options RenderOptions) []string
- type ColorValidationError
- type DescenderInfo
- type Font
- type FontData
- type GradientDirection
- type HorizontalAlignment
- type RenderOptions
- type ScaleValidationError
- type ShadowStyle
- type ShadowStyleOption
- type TextAlignment
- type ValidationError
- type VerticalAlignment
Constants ¶
const ( MinCharSpacing = 0 MaxCharSpacing = 10 MinWordSpacing = 0 MaxWordSpacing = 20 MinLineSpacing = 0 MaxLineSpacing = 10 MinScaleFactor = 0.5 // 0.5x MaxScaleFactor = 4.0 // 4x MinShadowOffset = -5 MaxShadowOffset = 5 )
Validation constants for RenderOptions
Variables ¶
var ANSIColorMap = map[string]string{
"30": "#000000",
"31": "#CD3131",
"32": "#0DBC79",
"33": "#E5E510",
"34": "#2472C8",
"35": "#BC3FBC",
"36": "#11A8CD",
"37": "#E5E5E5",
"90": "#808080",
"91": "#FF9999",
"92": "#99FF99",
"93": "#FFFF99",
"94": "#66BBFF",
"95": "#FF99FF",
"96": "#99FFFF",
"97": "#FFFFFF",
}
ANSIColorMap provides mappings from ANSI color codes to hex values This is the canonical color mapping used throughout the library
var EmbeddedFonts embed.FS
Functions ¶
func ApplyHorizontalAlignment ¶
func ApplyHorizontalAlignment(line string, canvasWidth int, alignment HorizontalAlignment) string
ApplyHorizontalAlignment applies horizontal alignment to a single line of text.
func ApplyVerticalAlignment ¶
func ApplyVerticalAlignment(lines []string, canvasHeight int, alignment VerticalAlignment) []string
ApplyVerticalAlignment applies vertical alignment to a set of lines.
func DetectHalfPixelUsage ¶
DetectHalfPixelUsage checks if the current text rendering would use half-pixels that would interfere with shadow rendering. This function should only return true when shadows would actually cause visual artifacts.
func ListFonts ¶
ListFonts returns a list of available font names from both custom and embedded fonts
func RegisterCustomPath ¶ added in v0.3.0
RegisterCustomPath is the smart entry point that handles both files and directories
func RegisterFontDirectory ¶ added in v0.3.0
RegisterFontDirectory loads all .bit font files from a directory
func RegisterFontFile ¶ added in v0.3.0
RegisterFontFile loads a single .bit font file and registers it
func RenderText ¶
RenderText renders text using the specified font with default options
func RenderTextWithColor ¶
RenderTextWithColor renders text using the specified font and applies ANSI color
func RenderTextWithFont ¶
func RenderTextWithFont(text string, fontData FontData, options RenderOptions) []string
RenderTextWithFont renders text using the specified font with advanced rendering options
func RenderTextWithOptions ¶
func RenderTextWithOptions(text string, font *Font, options RenderOptions) []string
RenderTextWithOptions renders text using the specified font and advanced options
Types ¶
type ColorValidationError ¶
ColorValidationError represents a color validation error
func (*ColorValidationError) Error ¶
func (e *ColorValidationError) Error() string
type DescenderInfo ¶
type DescenderInfo struct {
HasDescender bool
BaselineHeight int // Height of the main character body (excluding descender)
DescenderHeight int // Height of the descender part
TotalHeight int // Total character height
VerticalOffset int // How much to offset this character vertically
}
DescenderInfo holds information about a character's descender properties
type FontData ¶
type FontData struct {
Name string `json:"name"`
Author string `json:"author"`
License string `json:"license"`
Characters map[string][]string `json:"characters"`
}
FontData represents the overall structure of our .bit font file (JSON format)
type GradientDirection ¶
type GradientDirection int
GradientDirection represents gradient direction options
const ( UpDown GradientDirection = iota DownUp LeftRight RightLeft )
type HorizontalAlignment ¶
type HorizontalAlignment int
HorizontalAlignment represents the horizontal alignment of a character or line.
const ( // AlignLeft aligns the left of the character/line to the left of the canvas. AlignLeft HorizontalAlignment = iota // AlignCenter aligns the center of the character/line to the center of the canvas. AlignCenter // AlignRight aligns the right of the character/line to the right of the canvas. AlignRight )
type RenderOptions ¶
type RenderOptions struct {
// Spacing options
CharSpacing int // Character spacing (0 to 10)
WordSpacing int // Word spacing (0 to 20)
LineSpacing int // Line spacing (0 to 10)
// Text alignment
Alignment TextAlignment
// Text color options
TextColor string // Hex color code (e.g., "#FFFFFF")
GradientColor string // Hex color code for gradient end color
GradientDirection GradientDirection
UseGradient bool
// Text scale
ScaleFactor float64 // 0.5: half size, 1.0: normal, 2.0: double, 4.0: quadruple
// Shadow options
ShadowEnabled bool
ShadowHorizontalOffset int // -5 to 5
ShadowVerticalOffset int // -5 to 5
ShadowStyle ShadowStyle
// Multi-line text
TextLines []string
}
RenderOptions contains all the options for rendering text
func DefaultRenderOptions ¶
func DefaultRenderOptions() RenderOptions
DefaultRenderOptions returns RenderOptions with default values
func (*RenderOptions) Validate ¶
func (opts *RenderOptions) Validate() error
Validate checks if the RenderOptions are valid and returns an error if not
type ScaleValidationError ¶
ScaleValidationError represents a scale validation error
func (*ScaleValidationError) Error ¶
func (e *ScaleValidationError) Error() string
type ShadowStyle ¶
type ShadowStyle int
ShadowStyle represents shadow style options
const ( LightShade ShadowStyle = iota MediumShade DarkShade )
type ShadowStyleOption ¶
ShadowStyleOption represents shadow style options
type TextAlignment ¶
type TextAlignment int
TextAlignment represents text alignment options
const ( LeftAlign TextAlignment = iota CenterAlign RightAlign )
type ValidationError ¶
ValidationError represents a validation error for RenderOptions
func (*ValidationError) Error ¶
func (e *ValidationError) Error() string
type VerticalAlignment ¶
type VerticalAlignment int
VerticalAlignment represents the vertical alignment of a character or line.
const ( // AlignTop aligns the top of the character/line to the top of the canvas. AlignTop VerticalAlignment = iota // AlignMiddle aligns the middle of the character/line to the middle of the canvas. AlignMiddle // AlignBottom aligns the bottom of the character/line to the bottom of the canvas. AlignBottom )