Skip to content

Commit b75561d

Browse files
committed
Fix PyCSS font parser
1 parent 40ca697 commit b75561d

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

lglpy/timeline/drawable/css.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
- line-width: 1.5; (Default 1.0)
3939
- line-dash: 2,4; (Default none)
4040
- font-color: #01234567; (Default none)
41-
- font-face: name; (Default Consolas,sans)
41+
- font-face: name; (Default Consolas,monospace)
4242
- font-size: 11; (Default 10)
4343
4444
Colors can be RGB or RGBA, but note that transparent colors are much more
@@ -180,6 +180,17 @@ class CSSStylesheet(dict):
180180
re.VERBOSE
181181
)
182182

183+
re_str_decl = re.compile(
184+
r'''
185+
^\s* # Start a new line ignore whitespace
186+
(font-face):\s* # Mandatory name
187+
"(.*?)" # Double-quoted string
188+
;\s* # Closing ; immediately afterwards
189+
$
190+
''',
191+
re.VERBOSE
192+
)
193+
183194
def __init__(
184195
self, css_file: Optional[str] = None,
185196
css_string: Optional[str] = None):
@@ -279,6 +290,13 @@ def parse_string(self, css_string: str, file_name: Optional[str]):
279290
node[key] = dash_value
280291
continue
281292

293+
# Handle node field declarations for strings
294+
if match := self.re_str_decl.match(line):
295+
key = match.group(1)
296+
for node in current_nodes:
297+
node[key] = CSSFont(match.group(2))
298+
continue
299+
282300
# If we get here this line is an unknown so raise an error
283301
raise ValueError()
284302

@@ -370,7 +388,7 @@ class CSSFont(tuple):
370388
tuples. Created instances will not be instances of CSSFont.
371389
'''
372390

373-
def __new__(cls, face: str = 'sans'):
391+
def __new__(cls, face: str):
374392
'''
375393
Create a new CSSFont tuple.
376394
@@ -422,7 +440,7 @@ class CSSNode(dict):
422440
if os.name == 'nt':
423441
DEFAULT_FONT = 'Consolas'
424442
else:
425-
DEFAULT_FONT = 'sans'
443+
DEFAULT_FONT = 'monospace'
426444

427445
DEFAULT_STYLES = {
428446
'fill-color': CSSColor('none'),

lglpy/timeline/drawable/drawable.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
FONT = ('Consolas', cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_NORMAL)
5151
''' Constant code for Windows font style'''
5252
else:
53-
FONT = ('sans', cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_NORMAL)
53+
FONT = ('monospace', cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_NORMAL)
5454
''' Constant code for Linux font style'''
5555

5656

0 commit comments

Comments
 (0)