Conversion: Add support for the CSS3 rem font size unit

This commit is contained in:
Kovid Goyal 2013-09-05 10:49:45 +05:30
parent 58ac84cb57
commit d7a206c563
3 changed files with 10 additions and 4 deletions

View File

@ -133,7 +133,8 @@ def render_html(path_to_html, width=590, height=750, as_xhtml=True):
from PyQt4.QtWebKit import QWebPage
from PyQt4.Qt import QEventLoop, QPalette, Qt, QUrl, QSize
from calibre.gui2 import is_ok_to_use_qt
if not is_ok_to_use_qt(): return None
if not is_ok_to_use_qt():
return None
path_to_html = os.path.abspath(path_to_html)
with CurrentDir(os.path.dirname(path_to_html)):
page = QWebPage()
@ -214,9 +215,9 @@ def calibre_cover(title, author_string, series_string=None,
if cleanup:
os.remove(font_path)
UNIT_RE = re.compile(r'^(-*[0-9]*[.]?[0-9]*)\s*(%|em|ex|en|px|mm|cm|in|pt|pc)$')
UNIT_RE = re.compile(r'^(-*[0-9]*[.]?[0-9]*)\s*(%|em|ex|en|px|mm|cm|in|pt|pc|rem)$')
def unit_convert(value, base, font, dpi):
def unit_convert(value, base, font, dpi, body_font_size=12):
' Return value in pts'
if isinstance(value, (int, long, float)):
return value
@ -250,6 +251,8 @@ def unit_convert(value, base, font, dpi):
result = value * 2.8346456693
elif unit == 'cm':
result = value * 28.346456693
elif unit == 'rem':
result = value * body_font_size
return result
def generate_masthead(title, output_path=None, width=600, height=60):

View File

@ -210,6 +210,7 @@ class Stylizer(object):
if self.profile is None:
# Just in case the default profile is removed in the future :)
self.profile = opts.output_profile
self.body_font_size = self.profile.fbase
self.logger = oeb.logger
item = oeb.manifest.hrefs[path]
basename = os.path.basename(path)
@ -630,7 +631,7 @@ class Style(object):
base = self.width
if not font and font != 0:
font = self.fontSize
return unit_convert(value, base, font, self._profile.dpi)
return unit_convert(value, base, font, self._profile.dpi, body_font_size=self._stylizer.body_font_size)
def pt_to_px(self, value):
return (self._profile.dpi / 72.0) * value

View File

@ -310,6 +310,8 @@ class CSSFlattener(object):
except:
font_size = self.sbase if self.sbase is not None else \
self.context.source.fbase
if tag == 'body' and isinstance(font_size, (int, float)):
stylizer.body_font_size = font_size
if 'align' in node.attrib:
if tag != 'img':
cssdict['text-align'] = node.attrib['align']