From 8b5dfb98a554ee4b4e5738190914a65265eed299 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Mon, 21 May 2012 17:12:32 +0530 Subject: [PATCH] Conversion pipeline: Remove the attempt to detect and autocorrect if text will go off the left edge of the page, as it was a rather crude heuristic. Also do not remove fake margins if the book uses negative text indents on the margined elements --- src/calibre/ebooks/oeb/transforms/flatcss.py | 14 ++------------ src/calibre/ebooks/oeb/transforms/page_margin.py | 16 +++++++++++++++- 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/src/calibre/ebooks/oeb/transforms/flatcss.py b/src/calibre/ebooks/oeb/transforms/flatcss.py index 43fec3985f..d58b64ac53 100644 --- a/src/calibre/ebooks/oeb/transforms/flatcss.py +++ b/src/calibre/ebooks/oeb/transforms/flatcss.py @@ -221,7 +221,7 @@ class CSSFlattener(object): value = 0.0 cssdict[property] = "%0.5fem" % (value / fsize) - def flatten_node(self, node, stylizer, names, styles, psize, item_id, left=0): + def flatten_node(self, node, stylizer, names, styles, psize, item_id): if not isinstance(node.tag, basestring) \ or namespace(node.tag) != XHTML_NS: return @@ -316,16 +316,6 @@ class CSSFlattener(object): if cssdict: if self.lineh and self.fbase and tag != 'body': self.clean_edges(cssdict, style, psize) - margin = asfloat(style['margin-left'], 0) - indent = asfloat(style['text-indent'], 0) - left += margin - if (left + indent) < 0: - try: - percent = (margin - indent) / style['width'] - cssdict['margin-left'] = "%d%%" % (percent * 100) - except ZeroDivisionError: - pass - left -= indent if 'display' in cssdict and cssdict['display'] == 'in-line': cssdict['display'] = 'inline' if self.unfloat and 'float' in cssdict \ @@ -378,7 +368,7 @@ class CSSFlattener(object): if 'style' in node.attrib: del node.attrib['style'] for child in node: - self.flatten_node(child, stylizer, names, styles, psize, item_id, left) + self.flatten_node(child, stylizer, names, styles, psize, item_id) def flatten_head(self, item, href, global_href): html = item.data diff --git a/src/calibre/ebooks/oeb/transforms/page_margin.py b/src/calibre/ebooks/oeb/transforms/page_margin.py index 9181c8fd4e..67ba490a81 100644 --- a/src/calibre/ebooks/oeb/transforms/page_margin.py +++ b/src/calibre/ebooks/oeb/transforms/page_margin.py @@ -32,6 +32,8 @@ class RemoveAdobeMargins(object): attr = 'margin-'+margin elem.attrib.pop(attr, None) +class NegativeTextIndent(Exception): + pass class RemoveFakeMargins(object): @@ -66,13 +68,25 @@ class RemoveFakeMargins(object): self.find_levels() for level in self.levels: - self.process_level(level) + try: + self.process_level(level) + except NegativeTextIndent: + self.log.debug('Negative text indent detected at level ' + ' %s, ignoring this level'%level) def get_margins(self, elem): cls = elem.get('class', None) if cls: style = self.selector_map.get('.'+cls, None) if style: + try: + ti = style['text-indent'] + except: + pass + else: + if ( (hasattr(ti, 'startswith') and ti.startswith('-')) or + isinstance(ti, (int, float)) and ti < 0): + raise NegativeTextIndent() return style.marginLeft, style.marginRight, style return '', '', None