mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Code to parse font family values
This commit is contained in:
parent
cedde63fe8
commit
553137015e
@ -8,6 +8,31 @@ __copyright__ = '2014, Kovid Goyal <kovid at kovidgoyal.net>'
|
|||||||
|
|
||||||
|
|
||||||
from tinycss.css21 import CSS21Parser, ParseError
|
from tinycss.css21 import CSS21Parser, ParseError
|
||||||
|
from .tokenizer import tokenize_grouped
|
||||||
|
|
||||||
|
|
||||||
|
def parse_font_family(css_string):
|
||||||
|
families = []
|
||||||
|
current_family = ''
|
||||||
|
def commit():
|
||||||
|
families.append(current_family.strip())
|
||||||
|
|
||||||
|
for token in tokenize_grouped(css_string.strip()):
|
||||||
|
if token.type == 'STRING':
|
||||||
|
if current_family:
|
||||||
|
commit()
|
||||||
|
current_family = token.value
|
||||||
|
elif token.type == 'DELIM':
|
||||||
|
if token.value == ',':
|
||||||
|
if current_family:
|
||||||
|
commit()
|
||||||
|
current_family = ''
|
||||||
|
elif token.type == 'IDENT':
|
||||||
|
current_family += ' ' + token.value
|
||||||
|
if current_family:
|
||||||
|
commit()
|
||||||
|
return families
|
||||||
|
|
||||||
|
|
||||||
class FontFaceRule(object):
|
class FontFaceRule(object):
|
||||||
|
|
||||||
|
@ -6,7 +6,7 @@ from __future__ import (unicode_literals, division, absolute_import,
|
|||||||
__license__ = 'GPL v3'
|
__license__ = 'GPL v3'
|
||||||
__copyright__ = '2014, Kovid Goyal <kovid at kovidgoyal.net>'
|
__copyright__ = '2014, Kovid Goyal <kovid at kovidgoyal.net>'
|
||||||
|
|
||||||
from tinycss.fonts3 import CSSFonts3Parser
|
from tinycss.fonts3 import CSSFonts3Parser, parse_font_family
|
||||||
from tinycss.tests import BaseTest
|
from tinycss.tests import BaseTest
|
||||||
|
|
||||||
class TestFonts3(BaseTest):
|
class TestFonts3(BaseTest):
|
||||||
@ -28,4 +28,22 @@ class TestFonts3(BaseTest):
|
|||||||
stylesheet = CSSFonts3Parser().parse_stylesheet('@font-face;')
|
stylesheet = CSSFonts3Parser().parse_stylesheet('@font-face;')
|
||||||
self.assert_errors(stylesheet.errors, ['missing block'])
|
self.assert_errors(stylesheet.errors, ['missing block'])
|
||||||
|
|
||||||
|
def test_parse_font_family(self):
|
||||||
|
' Test parsing of font-family values '
|
||||||
|
for raw, q in {
|
||||||
|
'"1as"': ['1as'],
|
||||||
|
'A B C, serif': ['A B C', 'serif'],
|
||||||
|
r'Red\/Black': ['Red/Black'],
|
||||||
|
'A B': ['A B'],
|
||||||
|
r'Ahem\!': ['Ahem!'],
|
||||||
|
r'"Ahem!"': ['Ahem!'],
|
||||||
|
'€42': ['€42'],
|
||||||
|
r'Hawaii\ 5-0': ['Hawaii 5-0'],
|
||||||
|
r'"X \"Y"': ['X "Y'],
|
||||||
|
'A B, C D, "E", serif': ['A B', 'C D', 'E', 'serif'],
|
||||||
|
}.iteritems():
|
||||||
|
self.ae(q, parse_font_family(raw))
|
||||||
|
for single in ('serif', 'sans-serif', 'A B C'):
|
||||||
|
self.ae([single], parse_font_family(single))
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user