mirror of
git://github.com/kovidgoyal/calibre.git
synced 2026-05-08 16:33:43 +02:00
Serialization of parsed fonts
This commit is contained in:
parent
bbaf9875cc
commit
bb4fe4d924
2 changed files with 20 additions and 1 deletions
|
|
@ -64,6 +64,24 @@ def one(x):
|
|||
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 serialize_font(font_dict):
|
||||
ans = []
|
||||
for x in 'style variant weight stretch'.split():
|
||||
val = font_dict.get('font-' + x)
|
||||
if val is not None:
|
||||
ans.append(val)
|
||||
val = font_dict.get('font-size')
|
||||
if val is not None:
|
||||
fs = val
|
||||
val = font_dict.get('line-height')
|
||||
if val is not None:
|
||||
fs += '/' + val
|
||||
ans.append(fs)
|
||||
val = font_dict.get('font-family')
|
||||
if val:
|
||||
ans.append(serialize_font_family(val))
|
||||
return ' '.join(ans)
|
||||
|
||||
def parse_font(css_string):
|
||||
# See https://www.w3.org/TR/css-fonts-3/#font-prop
|
||||
style = variant = weight = stretch = size = height = None
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
__license__ = 'GPL v3'
|
||||
__copyright__ = '2014, Kovid Goyal <kovid at kovidgoyal.net>'
|
||||
|
||||
from tinycss.fonts3 import CSSFonts3Parser, parse_font_family, parse_font
|
||||
from tinycss.fonts3 import CSSFonts3Parser, parse_font_family, parse_font, serialize_font
|
||||
from tinycss.tests import BaseTest
|
||||
|
||||
class TestFonts3(BaseTest):
|
||||
|
|
@ -52,6 +52,7 @@ def test_parse_font(self):
|
|||
def t(raw, **kw):
|
||||
q = {('line' if k == 'height' else 'font') + '-' + k:v for k, v in kw.iteritems()}
|
||||
self.ae(q, parse_font(raw))
|
||||
self.ae(q, parse_font(serialize_font(q)))
|
||||
t('caption', family=['sans-serif'])
|
||||
t('serif', family=['serif'])
|
||||
t('12pt/14pt sans-serif', size='12pt', height='14pt', family=['sans-serif'])
|
||||
|
|
|
|||
Loading…
Reference in a new issue