Get the font file path for a given font name. Args: font_name: Name of the font (e.g., 'Arial', 'Calibri') Returns: Path to the font file, or None if not found
(font_name: str)
| 278 | |
| 279 | @staticmethod |
| 280 | def get_font_path(font_name: str) -> Optional[str]: |
| 281 | """Get the font file path for a given font name. |
| 282 | |
| 283 | Args: |
| 284 | font_name: Name of the font (e.g., 'Arial', 'Calibri') |
| 285 | |
| 286 | Returns: |
| 287 | Path to the font file, or None if not found |
| 288 | """ |
| 289 | system = platform.system() |
| 290 | |
| 291 | # Common font file variations to try |
| 292 | font_variations = [ |
| 293 | font_name, |
| 294 | font_name.lower(), |
| 295 | font_name.replace(" ", ""), |
| 296 | font_name.replace(" ", "-"), |
| 297 | ] |
| 298 | |
| 299 | # Define font directories and extensions by platform |
| 300 | if system == "Darwin": # macOS |
| 301 | font_dirs = [ |
| 302 | "/System/Library/Fonts/", |
| 303 | "/Library/Fonts/", |
| 304 | "~/Library/Fonts/", |
| 305 | ] |
| 306 | extensions = [".ttf", ".otf", ".ttc", ".dfont"] |
| 307 | else: # Linux |
| 308 | font_dirs = [ |
| 309 | "/usr/share/fonts/truetype/", |
| 310 | "/usr/local/share/fonts/", |
| 311 | "~/.fonts/", |
| 312 | ] |
| 313 | extensions = [".ttf", ".otf"] |
| 314 | |
| 315 | # Try to find the font file |
| 316 | from pathlib import Path |
| 317 | |
| 318 | for font_dir in font_dirs: |
| 319 | font_dir_path = Path(font_dir).expanduser() |
| 320 | if not font_dir_path.exists(): |
| 321 | continue |
| 322 | |
| 323 | # First try exact matches |
| 324 | for variant in font_variations: |
| 325 | for ext in extensions: |
| 326 | font_path = font_dir_path / f"{variant}{ext}" |
| 327 | if font_path.exists(): |
| 328 | return str(font_path) |
| 329 | |
| 330 | # Then try fuzzy matching - find files containing the font name |
| 331 | try: |
| 332 | for file_path in font_dir_path.iterdir(): |
| 333 | if file_path.is_file(): |
| 334 | file_name_lower = file_path.name.lower() |
| 335 | font_name_lower = font_name.lower().replace(" ", "") |
| 336 | if font_name_lower in file_name_lower and any( |
| 337 | file_name_lower.endswith(ext) for ext in extensions |
no outgoing calls
no test coverage detected