| 1 |
#!/usr/bin/env python |
| 2 |
# vim:fileencoding=utf-8 |
| 3 |
|
| 4 |
|
| 5 |
__license__ = 'GPL v3' |
| 6 |
__copyright__ = '2014, Kovid Goyal <kovid at kovidgoyal.net>' |
| 7 |
|
| 8 |
|
| 9 |
import re |
| 10 |
|
| 11 |
from tinycss.css21 import CSS21Parser, ParseError |
| 12 |
|
| 13 |
from .tokenizer import tokenize_grouped |
| 14 |
|
| 15 |
|
| 16 |
def parse_font_family_tokens(tokens): |
| 17 |
families = [] |
| 18 |
current_family = '' |
| 19 |
|
| 20 |
def commit(): |
| 21 |
val = current_family.strip() |
| 22 |
if val: |
| 23 |
families.append(val) |
| 24 |
|
| 25 |
for token in tokens: |
| 26 |
if token.type == 'STRING': |
| 27 |
if current_family: |
| 28 |
commit() |
| 29 |
current_family = token.value |
| 30 |
elif token.type == 'DELIM': |
| 31 |
if token.value == ',': |
| 32 |
if current_family: |
| 33 |
commit() |
| 34 |
current_family = '' |
| 35 |
elif token.type == 'IDENT': |
| 36 |
current_family += ' ' + token.value |
| 37 |
if current_family: |
| 38 |
commit() |
| 39 |
return families |
| 40 |
|
| 41 |
|
| 42 |
def parse_font_family(css_string): |
| 43 |
return parse_font_family_tokens(tokenize_grouped(type('')(css_string).strip())) |
| 44 |
|
| 45 |
|
| 46 |
def serialize_single_font_family(x): |
| 47 |
xl = x.lower() |
| 48 |
if xl in GENERIC_FAMILIES: |
| 49 |
if xl == 'sansserif': |
| 50 |
xl = 'sans-serif' |
| 51 |
return xl |
| 52 |
if SIMPLE_NAME_PAT.match(x) is not None and not x.lower().startswith('and'): |
| 53 |
# css_parser dies if a font name starts with and |
| 54 |
return x |
| 55 |
return '"%s"' % x.replace('"', r'\"') |
| 56 |
|
| 57 |
|
| 58 |
def serialize_font_family(families): |
| 59 |
return ', '.join(map(serialize_single_font_family, families)) |
| 60 |
|
| 61 |
|
| 62 |
GLOBAL_IDENTS = frozenset('inherit initial unset normal'.split()) |
| 63 |
STYLE_IDENTS = frozenset('italic oblique'.split()) |
| 64 |
VARIANT_IDENTS = frozenset(('small-caps',)) |
| 65 |
WEIGHT_IDENTS = frozenset('bold bolder lighter'.split()) |
| 66 |
STRETCH_IDENTS = frozenset('ultra-condensed extra-condensed condensed semi-condensed semi-expanded expanded extra-expanded ultra-expanded'.split()) |
| 67 |
BEFORE_SIZE_IDENTS = STYLE_IDENTS | VARIANT_IDENTS | WEIGHT_IDENTS | STRETCH_IDENTS |
| 68 |
SIZE_IDENTS = frozenset('xx-small x-small small medium large x-large xx-large larger smaller'.split()) |
| 69 |
WEIGHT_SIZES = frozenset(map(int, '100 200 300 400 500 600 700 800 900'.split())) |
| 70 |
LEGACY_FONT_SPEC = frozenset('caption icon menu message-box small-caption status-bar'.split()) |
| 71 |
GENERIC_FAMILIES = frozenset('serif sans-serif sansserif cursive fantasy monospace'.split()) |
| 72 |
SIMPLE_NAME_PAT = re.compile(r'[a-zA-Z][a-zA-Z0-9_-]*$') |
| 73 |
|
| 74 |
|
| 75 |
def serialize_font(font_dict): |
| 76 |
ans = [] |
| 77 |
for x in 'style variant weight stretch'.split(): |
| 78 |
val = font_dict.get('font-' + x) |
| 79 |
if val is not None: |
| 80 |
ans.append(val) |
| 81 |
val = font_dict.get('font-size') |
| 82 |
if val is not None: |
| 83 |
fs = val |
| 84 |
val = font_dict.get('line-height') |
| 85 |
if val is not None: |
| 86 |
fs += '/' + val |
| 87 |
ans.append(fs) |
| 88 |
val = font_dict.get('font-family') |
| 89 |
if val: |
| 90 |
ans.append(serialize_font_family(val)) |
| 91 |
return ' '.join(ans) |
| 92 |
|
| 93 |
|
| 94 |
def parse_font(css_string): |
| 95 |
# See https://www.w3.org/TR/css-fonts-3/#font-prop |
| 96 |
style = variant = weight = stretch = size = height = None |
| 97 |
tokens = list(reversed(tuple(tokenize_grouped(type('')(css_string).strip())))) |
| 98 |
if tokens and tokens[-1].value in LEGACY_FONT_SPEC: |
| 99 |
return {'font-family':['sans-serif']} |
| 100 |
while tokens: |
| 101 |
tok = tokens.pop() |
| 102 |
if tok.type == 'STRING': |
| 103 |
tokens.append(tok) |
| 104 |
break |
| 105 |
if tok.type == 'INTEGER': |
| 106 |
if size is None: |
| 107 |
if weight is None and tok.value in WEIGHT_SIZES: |
| 108 |
weight = tok.as_css() |
| 109 |
continue |
| 110 |
break |
| 111 |
if height is None: |
| 112 |
height = tok.as_css() |
| 113 |
break |
| 114 |
break |
| 115 |
if tok.type == 'NUMBER': |
| 116 |
if size is not None and height is None: |
| 117 |
height = tok.as_css() |
| 118 |
break |
| 119 |
if tok.type == 'DELIM': |
| 120 |
if tok.value == '/' and size is not None and height is None: |
| 121 |
continue |
| 122 |
break |
| 123 |
if tok.type in ('DIMENSION', 'PERCENTAGE'): |
| 124 |
if size is None: |
| 125 |
size = tok.as_css() |
| 126 |
continue |
| 127 |
if height is None: |
| 128 |
height = tok.as_css() |
| 129 |
break |
| 130 |
if tok.type == 'IDENT': |
| 131 |
if tok.value in GLOBAL_IDENTS: |
| 132 |
if size is not None: |
| 133 |
if height is None: |
| 134 |
height = tok.value |
| 135 |
else: |
| 136 |
tokens.append(tok) |
| 137 |
break |
| 138 |
if style is None: |
| 139 |
style = tok.value |
| 140 |
elif variant is None: |
| 141 |
variant = tok.value |
| 142 |
elif weight is None: |
| 143 |
weight = tok.value |
| 144 |
elif stretch is None: |
| 145 |
stretch = tok.value |
| 146 |
elif size is None: |
| 147 |
size = tok.value |
| 148 |
elif height is None: |
| 149 |
height = tok.value |
| 150 |
break |
| 151 |
else: |
| 152 |
tokens.append(tok) |
| 153 |
break |
| 154 |
continue |
| 155 |
if tok.value in BEFORE_SIZE_IDENTS: |
| 156 |
if size is not None: |
| 157 |
break |
| 158 |
if tok.value in STYLE_IDENTS: |
| 159 |
style = tok.value |
| 160 |
elif tok.value in VARIANT_IDENTS: |
| 161 |
variant = tok.value |
| 162 |
elif tok.value in WEIGHT_IDENTS: |
| 163 |
weight = tok.value |
| 164 |
elif tok.value in STRETCH_IDENTS: |
| 165 |
stretch = tok.value |
| 166 |
elif tok.value in SIZE_IDENTS: |
| 167 |
size = tok.value |
| 168 |
else: |
| 169 |
tokens.append(tok) |
| 170 |
break |
| 171 |
families = parse_font_family_tokens(reversed(tokens)) |
| 172 |
ans = {} |
| 173 |
if style is not None: |
| 174 |
ans['font-style'] = style |
| 175 |
if variant is not None: |
| 176 |
ans['font-variant'] = variant |
| 177 |
if weight is not None: |
| 178 |
ans['font-weight'] = weight |
| 179 |
if stretch is not None: |
| 180 |
ans['font-stretch'] = stretch |
| 181 |
if size is not None: |
| 182 |
ans['font-size'] = size |
| 183 |
if height is not None: |
| 184 |
ans['line-height'] = height |
| 185 |
if families: |
| 186 |
ans['font-family'] = families |
| 187 |
return ans |
| 188 |
|
| 189 |
|
| 190 |
class FontFaceRule: |
| 191 |
|
| 192 |
at_keyword = '@font-face' |
| 193 |
__slots__ = 'declarations', 'line', 'column' |
| 194 |
|
| 195 |
def __init__(self, declarations, line, column): |
| 196 |
self.declarations = declarations |
| 197 |
self.line = line |
| 198 |
self.column = column |
| 199 |
|
| 200 |
def __repr__(self): |
| 201 |
return ('<{0.__class__.__name__} at {0.line}:{0.column}>' |
| 202 |
.format(self)) |
| 203 |
|
| 204 |
|
| 205 |
class CSSFonts3Parser(CSS21Parser): |
| 206 |
|
| 207 |
''' Parse @font-face rules from the CSS 3 fonts module ''' |
| 208 |
|
| 209 |
ALLOWED_CONTEXTS_FOR_FONT_FACE = {'stylesheet', '@media', '@page'} |
| 210 |
|
| 211 |
def __init__(self): |
| 212 |
super(CSSFonts3Parser, self).__init__() |
| 213 |
self.at_parsers['@font-face'] = self.parse_font_face_rule |
| 214 |
|
| 215 |
def parse_font_face_rule(self, rule, previous_rules, errors, context): |
| 216 |
if context not in self.ALLOWED_CONTEXTS_FOR_FONT_FACE: |
| 217 |
raise ParseError(rule, |
| 218 |
'@font-face rule not allowed in ' + context) |
| 219 |
if rule.body is None: |
| 220 |
raise ParseError(rule, |
| 221 |
'invalid {0} rule: missing block'.format(rule.at_keyword)) |
| 222 |
if rule.head: |
| 223 |
raise ParseError(rule, '{0} rule is not allowed to have content before the descriptor declaration'.format(rule.at_keyword)) |
| 224 |
declarations, decerrors = self.parse_declaration_list(rule.body) |
| 225 |
errors.extend(decerrors) |
| 226 |
return FontFaceRule(declarations, rule.line, rule.column) |