A pure-Python library for reading and converting SVG files to ReportLab Graphics drawings
Font registration and lookup system for proper text rendering in converted SVG files. This module provides comprehensive font management capabilities with automatic font discovery and ReportLab integration.
Register fonts for use in SVG text rendering, with support for custom font files and automatic ReportLab integration.
def register_font(font_name, font_path=None, weight='normal', style='normal', rlgFontName=None):
"""
Register a font for use in SVG conversion.
Parameters:
- font_name: str - Font family name (e.g., 'Arial', 'Times New Roman')
- font_path: str, optional - Path to font file (.ttf, .otf). If None, system font discovery is attempted
- weight: str - Font weight ('normal', 'bold', or numeric weight)
- style: str - Font style ('normal', 'italic')
- rlgFontName: str, optional - Custom ReportLab font name override
Returns:
tuple: (font_name, exact_match) - ReportLab font name and boolean indicating exact match, or (None, False) if registration failed
"""Usage Examples:
from svglib.svglib import register_font
# Register system font by name
font_name, exact = register_font('Arial')
# Register font with specific weight and style
font_name, exact = register_font('Arial', weight='bold', style='italic')
# Register custom font file
font_name, exact = register_font('CustomFont', font_path='/path/to/custom_font.ttf')
# Register with custom ReportLab name
font_name, exact = register_font('MyFont', font_path='/fonts/myfont.ttf', rlgFontName='MyCustomRLFont')Find registered fonts by family name, weight, and style for use in text rendering.
def find_font(font_name, weight='normal', style='normal'):
"""
Find a registered font by family name, weight, and style.
Parameters:
- font_name: str - Font family name
- weight: str - Font weight ('normal', 'bold', or numeric)
- style: str - Font style ('normal', 'italic')
Returns:
tuple: (font_name, exact_match) - ReportLab font name and boolean indicating exact match, or (None, False) if not found
"""Usage Examples:
from svglib.svglib import find_font
# Find normal weight font
rl_font, exact = find_font('Arial')
# Find bold italic font
rl_font, exact = find_font('Times New Roman', weight='bold', style='italic')
# Check if font exists
rl_font, exact = find_font('CustomFont')
if rl_font:
print(f"CustomFont is available as {rl_font}, exact match: {exact}")Register complete font families with multiple variants through the FontMap class.
Usage Example:
from svglib.fonts import get_global_font_map
# Register complete font family via FontMap instance
font_map = get_global_font_map()
font_map.register_font_family(
'MyFamily',
normal='/fonts/myfamily-regular.ttf',
bold='/fonts/myfamily-bold.ttf',
italic='/fonts/myfamily-italic.ttf',
bolditalic='/fonts/myfamily-bolditalic.ttf'
)Access the global font mapping system for advanced font management.
def get_global_font_map():
"""
Get the global FontMap instance.
Returns:
FontMap: Global font mapping instance
"""Main class for managing font mappings between SVG font names and ReportLab fonts.
class FontMap:
"""
Managing the mapping of SVG font names to ReportLab fonts and registering
them in ReportLab.
"""
def __init__(self):
"""Initialize FontMap with default fonts."""
def register_font_family(self, family, normal, bold=None, italic=None, bolditalic=None):
"""
Register an entire font family with different variants.
Parameters:
- family: str - Font family name
- normal: str - Path to normal weight font file
- bold: str, optional - Path to bold weight font file
- italic: str, optional - Path to italic style font file
- bolditalic: str, optional - Path to bold italic font file
"""
@staticmethod
def build_internal_name(family, weight='normal', style='normal'):
"""
Build internal font name from family, weight, and style.
Parameters:
- family: str - Font family name
- weight: str - Font weight
- style: str - Font style
Returns:
str: Internal font name (e.g., 'Arial-BoldItalic')
"""
@staticmethod
def guess_font_filename(basename, weight='normal', style='normal', extension='ttf'):
"""
Guess font filename based on family, weight, and style.
Parameters:
- basename: str - Base font name
- weight: str - Font weight
- style: str - Font style
- extension: str - Font file extension
Returns:
str: Guessed filename
"""
def use_fontconfig(self, font_name, weight='normal', style='normal'):
"""
Use fontconfig to find and register fonts on Unix-like systems.
Parameters:
- font_name: str - Font family name
- weight: str - Font weight
- style: str - Font style
Returns:
tuple: (font_path, success) - Path to font file and success status
"""
def register_default_fonts(self):
"""Register default ReportLab fonts in the font map."""Usage Example:
from svglib.fonts import get_global_font_map, FontMap
# Get global font map
font_map = get_global_font_map()
# Create custom font map
custom_map = FontMap()
# Build internal font names
internal_name = FontMap.build_internal_name('Arial', 'bold', 'italic')
# Returns: 'Arial-BoldItalic'Tuple of standard ReportLab font names available by default.
STANDARD_FONT_NAMES = (
'Times-Roman', 'Times-Italic', 'Times-Bold', 'Times-BoldItalic',
'Helvetica', 'Helvetica-Oblique', 'Helvetica-Bold', 'Helvetica-BoldOblique',
'Courier', 'Courier-Oblique', 'Courier-Bold', 'Courier-BoldOblique',
'Symbol', 'ZapfDingbats',
)Default values used for font properties when not specified.
DEFAULT_FONT_NAME = "Helvetica"
DEFAULT_FONT_WEIGHT = 'normal'
DEFAULT_FONT_STYLE = 'normal'
DEFAULT_FONT_SIZE = 12Usage Example:
from svglib.fonts import (
STANDARD_FONT_NAMES, DEFAULT_FONT_NAME,
DEFAULT_FONT_WEIGHT, DEFAULT_FONT_STYLE, DEFAULT_FONT_SIZE
)
# Check if font is standard
font_name = 'Helvetica-Bold'
if font_name in STANDARD_FONT_NAMES:
print(f"{font_name} is a standard ReportLab font")
# Use defaults
font_size = DEFAULT_FONT_SIZE # 12
font_weight = DEFAULT_FONT_WEIGHT # 'normal'Install with Tessl CLI
npx tessl i tessl/pypi-svglib