Use the new font parsing code for normalize_css

This commit is contained in:
Kovid Goyal
2016-06-30 12:25:28 +05:30
parent b0b7a59afd
commit bbaf9875cc
2 changed files with 33 additions and 48 deletions
+16
View File
@@ -7,6 +7,8 @@ __license__ = 'GPL v3'
__copyright__ = '2014, Kovid Goyal <kovid at kovidgoyal.net>'
import re
from future_builtins import map
from tinycss.css21 import CSS21Parser, ParseError
from .tokenizer import tokenize_grouped
@@ -38,6 +40,18 @@ def parse_font_family_tokens(tokens):
def parse_font_family(css_string):
return parse_font_family_tokens(tokenize_grouped(type('')(css_string).strip()))
def serialize_font_family(families):
def one(x):
xl = x.lower()
if xl in GENERIC_FAMILIES:
if xl == 'sansserif':
xl = 'sans-serif'
return xl
if SIMPLE_NAME_PAT.match(x) is not None:
return x
return '"%s"' % x.replace('"', r'\"')
return ', '.join(map(one, families))
GLOBAL_IDENTS = frozenset('inherit initial unset normal'.split())
STYLE_IDENTS = frozenset('italic oblique'.split())
VARIANT_IDENTS = frozenset(('small-caps',))
@@ -47,6 +61,8 @@ BEFORE_SIZE_IDENTS = STYLE_IDENTS | VARIANT_IDENTS | WEIGHT_IDENTS | STRETCH_IDE
SIZE_IDENTS = frozenset('xx-small x-small small medium large x-large xx-large larger smaller'.split())
WEIGHT_SIZES = frozenset(map(int, '100 200 300 400 500 600 700 800 900'.split()))
LEGACY_FONT_SPEC = frozenset('caption icon menu message-box small-caption status-bar'.split())
GENERIC_FAMILIES = frozenset('serif sans-serif sansserif cursive fantasy monospace'.split())
SIMPLE_NAME_PAT = re.compile(r'[a-zA-Z][a-zA-Z0-9_-]*$')
def parse_font(css_string):
# See https://www.w3.org/TR/css-fonts-3/#font-prop