mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Serialization of parsed fonts
This commit is contained in:
parent
bbaf9875cc
commit
bb4fe4d924
@ -64,6 +64,24 @@ LEGACY_FONT_SPEC = frozenset('caption icon menu message-box small-caption status
|
||||
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 @@ from __future__ import (unicode_literals, division, absolute_import,
|
||||
__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 @@ class TestFonts3(BaseTest):
|
||||
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…
x
Reference in New Issue
Block a user