classes.c10_font
1from typing import TYPE_CHECKING 2import drawBot 3import colorama 4from loguru import logger 5from icecream import ic 6 7from lib import fonts as libFonts 8 9# Avoid circular import issues, hide from runtime, but keep for type checking 10if TYPE_CHECKING: 11 from .c11_family import KFamily 12 13 14class KFont: 15 """ 16 Represents a single font and provides methods for font analysis and manipulation. 17 18 Usually grouped into `classes.c11_family.KFamily`. 19 """ 20 21 fullName: str 22 """Full name of font: `Stabil Grotesk 400 Regular`""" 23 familyName: str 24 """Family name of font: `Stabil Grotesk`""" 25 styleName: str 26 """Style name of font: `Regular`""" 27 shortName: str 28 """Short name of font: `400 Regular`""" 29 styleNumber: int 30 """Style number of font: `400`""" 31 32 def __init__(self, path: str, family: "KFamily" = None): 33 """ 34 Initialize a KFont instance. 35 36 Args: 37 path: Path to the font file. 38 family: Family this font belongs to (optional). See `classes.c11_family.KFamily`. 39 """ 40 self.path = path 41 """File system path to the font file""" 42 43 if family: 44 self.family = family 45 46 names = libFonts.parseNameObject(path, separate=True) 47 for [name, value] in names.items(): 48 setattr(self, name, value) 49 50 def __str__(self) -> str: 51 """Return a string representation of the KFont instance.""" 52 return f"{colorama.Fore.BLACK}{colorama.Back.LIGHTGREEN_EX}KFont{colorama.Style.RESET_ALL} {self.fullName}" 53 54 @property 55 def fontType(self): 56 """ 57 Get the font type based on the style number. 58 59 Returns: 60 The font type as determined by `lib.fonts.getFontType`. 61 """ 62 try: 63 return libFonts.getFontType(self.styleNumber) 64 except: 65 logger.warning("[KFont] Error getting font type!") 66 67 def isType(self, criteria: libFonts.FontType) -> bool: 68 """ 69 Check if the font matches given `lib.fonts.FontType` criteria: 70 71 ``` 72 upright \\d0 73 italic \\d5 74 display [^4-6] 75 text [4-6] 76 thin [1-3] 77 thick [7-9] 78 ``` 79 80 Args: 81 criteria: The font type to check (e.g., 'upright', 'italic', etc.). 82 83 Returns: 84 True if the font matches the criteria, False otherwise. 85 """ 86 return criteria in self.fontType 87 88 def getSibling(self) -> "KFont": 89 """ 90 Get the sibling font with a related style number. 91 92 Example: 93 `400` => `450` 94 95 Returns: 96 The sibling KFont instance if found, otherwise None. 97 """ 98 prefix = self.styleNumber[0] 99 suffix = "5" if self.isType("upright") else "0" 100 # Adjust to correct length: 40 => 45, 400 => 450 101 adjusted = (prefix + suffix).ljust(len(self.styleNumber), "0") 102 siblingNumber = int(adjusted) 103 try: 104 return self.family.getFonts(criteria=siblingNumber) 105 except: 106 logger.warning("[KFont] Error getting sibling!") 107 108 def getWeightClass(self, numeric=False) -> str | int: 109 """ 110 Get the weight class of the font. 111 112 Example: 113 `400` => `4` 114 115 Args: 116 numeric: If True, return as integer; as string otherwise. 117 118 Returns: 119 The weight class as string or integer. 120 """ 121 numberString = self.styleNumber[0] 122 return int(numberString) if numeric else numberString 123 124 def activate(self): 125 """ 126 Activate this font for use in DrawBot. 127 128 Returns: 129 The KFont instance (self). 130 """ 131 logger.trace("Activating KFont: {}", self.fullName) 132 drawBot.font(self.path) 133 return self
class
KFont:
15class KFont: 16 """ 17 Represents a single font and provides methods for font analysis and manipulation. 18 19 Usually grouped into `classes.c11_family.KFamily`. 20 """ 21 22 fullName: str 23 """Full name of font: `Stabil Grotesk 400 Regular`""" 24 familyName: str 25 """Family name of font: `Stabil Grotesk`""" 26 styleName: str 27 """Style name of font: `Regular`""" 28 shortName: str 29 """Short name of font: `400 Regular`""" 30 styleNumber: int 31 """Style number of font: `400`""" 32 33 def __init__(self, path: str, family: "KFamily" = None): 34 """ 35 Initialize a KFont instance. 36 37 Args: 38 path: Path to the font file. 39 family: Family this font belongs to (optional). See `classes.c11_family.KFamily`. 40 """ 41 self.path = path 42 """File system path to the font file""" 43 44 if family: 45 self.family = family 46 47 names = libFonts.parseNameObject(path, separate=True) 48 for [name, value] in names.items(): 49 setattr(self, name, value) 50 51 def __str__(self) -> str: 52 """Return a string representation of the KFont instance.""" 53 return f"{colorama.Fore.BLACK}{colorama.Back.LIGHTGREEN_EX}KFont{colorama.Style.RESET_ALL} {self.fullName}" 54 55 @property 56 def fontType(self): 57 """ 58 Get the font type based on the style number. 59 60 Returns: 61 The font type as determined by `lib.fonts.getFontType`. 62 """ 63 try: 64 return libFonts.getFontType(self.styleNumber) 65 except: 66 logger.warning("[KFont] Error getting font type!") 67 68 def isType(self, criteria: libFonts.FontType) -> bool: 69 """ 70 Check if the font matches given `lib.fonts.FontType` criteria: 71 72 ``` 73 upright \\d0 74 italic \\d5 75 display [^4-6] 76 text [4-6] 77 thin [1-3] 78 thick [7-9] 79 ``` 80 81 Args: 82 criteria: The font type to check (e.g., 'upright', 'italic', etc.). 83 84 Returns: 85 True if the font matches the criteria, False otherwise. 86 """ 87 return criteria in self.fontType 88 89 def getSibling(self) -> "KFont": 90 """ 91 Get the sibling font with a related style number. 92 93 Example: 94 `400` => `450` 95 96 Returns: 97 The sibling KFont instance if found, otherwise None. 98 """ 99 prefix = self.styleNumber[0] 100 suffix = "5" if self.isType("upright") else "0" 101 # Adjust to correct length: 40 => 45, 400 => 450 102 adjusted = (prefix + suffix).ljust(len(self.styleNumber), "0") 103 siblingNumber = int(adjusted) 104 try: 105 return self.family.getFonts(criteria=siblingNumber) 106 except: 107 logger.warning("[KFont] Error getting sibling!") 108 109 def getWeightClass(self, numeric=False) -> str | int: 110 """ 111 Get the weight class of the font. 112 113 Example: 114 `400` => `4` 115 116 Args: 117 numeric: If True, return as integer; as string otherwise. 118 119 Returns: 120 The weight class as string or integer. 121 """ 122 numberString = self.styleNumber[0] 123 return int(numberString) if numeric else numberString 124 125 def activate(self): 126 """ 127 Activate this font for use in DrawBot. 128 129 Returns: 130 The KFont instance (self). 131 """ 132 logger.trace("Activating KFont: {}", self.fullName) 133 drawBot.font(self.path) 134 return self
Represents a single font and provides methods for font analysis and manipulation.
Usually grouped into classes.c11_family.KFamily.
KFont(path: str, family: classes.c11_family.KFamily = None)
33 def __init__(self, path: str, family: "KFamily" = None): 34 """ 35 Initialize a KFont instance. 36 37 Args: 38 path: Path to the font file. 39 family: Family this font belongs to (optional). See `classes.c11_family.KFamily`. 40 """ 41 self.path = path 42 """File system path to the font file""" 43 44 if family: 45 self.family = family 46 47 names = libFonts.parseNameObject(path, separate=True) 48 for [name, value] in names.items(): 49 setattr(self, name, value)
Initialize a KFont instance.
Arguments:
- path: Path to the font file.
- family: Family this font belongs to (optional). See
classes.c11_family.KFamily.
fontType
55 @property 56 def fontType(self): 57 """ 58 Get the font type based on the style number. 59 60 Returns: 61 The font type as determined by `lib.fonts.getFontType`. 62 """ 63 try: 64 return libFonts.getFontType(self.styleNumber) 65 except: 66 logger.warning("[KFont] Error getting font type!")
Get the font type based on the style number.
Returns:
The font type as determined by
lib.fonts.getFontType.
def
isType( self, criteria: Literal['upright', 'italic', 'display', 'text', 'thin', 'thick', 'any']) -> bool:
68 def isType(self, criteria: libFonts.FontType) -> bool: 69 """ 70 Check if the font matches given `lib.fonts.FontType` criteria: 71 72 ``` 73 upright \\d0 74 italic \\d5 75 display [^4-6] 76 text [4-6] 77 thin [1-3] 78 thick [7-9] 79 ``` 80 81 Args: 82 criteria: The font type to check (e.g., 'upright', 'italic', etc.). 83 84 Returns: 85 True if the font matches the criteria, False otherwise. 86 """ 87 return criteria in self.fontType
Check if the font matches given lib.fonts.FontType criteria:
upright \d0
italic \d5
display [^4-6]
text [4-6]
thin [1-3]
thick [7-9]
Arguments:
- criteria: The font type to check (e.g., 'upright', 'italic', etc.).
Returns:
True if the font matches the criteria, False otherwise.
89 def getSibling(self) -> "KFont": 90 """ 91 Get the sibling font with a related style number. 92 93 Example: 94 `400` => `450` 95 96 Returns: 97 The sibling KFont instance if found, otherwise None. 98 """ 99 prefix = self.styleNumber[0] 100 suffix = "5" if self.isType("upright") else "0" 101 # Adjust to correct length: 40 => 45, 400 => 450 102 adjusted = (prefix + suffix).ljust(len(self.styleNumber), "0") 103 siblingNumber = int(adjusted) 104 try: 105 return self.family.getFonts(criteria=siblingNumber) 106 except: 107 logger.warning("[KFont] Error getting sibling!")
Get the sibling font with a related style number.
Example:
400=>450
Returns:
The sibling KFont instance if found, otherwise None.
def
getWeightClass(self, numeric=False) -> str | int:
109 def getWeightClass(self, numeric=False) -> str | int: 110 """ 111 Get the weight class of the font. 112 113 Example: 114 `400` => `4` 115 116 Args: 117 numeric: If True, return as integer; as string otherwise. 118 119 Returns: 120 The weight class as string or integer. 121 """ 122 numberString = self.styleNumber[0] 123 return int(numberString) if numeric else numberString
Get the weight class of the font.
Example:
400=>4
Arguments:
- numeric: If True, return as integer; as string otherwise.
Returns:
The weight class as string or integer.
def
activate(self):
125 def activate(self): 126 """ 127 Activate this font for use in DrawBot. 128 129 Returns: 130 The KFont instance (self). 131 """ 132 logger.trace("Activating KFont: {}", self.fullName) 133 drawBot.font(self.path) 134 return self
Activate this font for use in DrawBot.
Returns:
The KFont instance (self).