From 4a2de2c9058bd5d9c3d18a355e81215fa815bd48 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sun, 13 May 2012 08:40:57 +0530 Subject: [PATCH] Conversion: Allow setting negative page margins. A negative page margin means that calibre will not specify any page margin in the output document (for formats that support this) --- src/calibre/ebooks/conversion/plumber.py | 4 ++ src/calibre/ebooks/oeb/base.py | 2 +- src/calibre/ebooks/oeb/transforms/flatcss.py | 39 ++++++++++++-------- src/calibre/gui2/convert/page_setup.ui | 24 ++++++++++++ 4 files changed, 52 insertions(+), 17 deletions(-) diff --git a/src/calibre/ebooks/conversion/plumber.py b/src/calibre/ebooks/conversion/plumber.py index 615ea521d8..6c889cb259 100644 --- a/src/calibre/ebooks/conversion/plumber.py +++ b/src/calibre/ebooks/conversion/plumber.py @@ -343,21 +343,25 @@ OptionRecommendation(name='remove_fake_margins', OptionRecommendation(name='margin_top', recommended_value=5.0, level=OptionRecommendation.LOW, help=_('Set the top margin in pts. Default is %default. ' + 'Setting this to less than zero will cause no margin to be set. ' 'Note: 72 pts equals 1 inch')), OptionRecommendation(name='margin_bottom', recommended_value=5.0, level=OptionRecommendation.LOW, help=_('Set the bottom margin in pts. Default is %default. ' + 'Setting this to less than zero will cause no margin to be set. ' 'Note: 72 pts equals 1 inch')), OptionRecommendation(name='margin_left', recommended_value=5.0, level=OptionRecommendation.LOW, help=_('Set the left margin in pts. Default is %default. ' + 'Setting this to less than zero will cause no margin to be set. ' 'Note: 72 pts equals 1 inch')), OptionRecommendation(name='margin_right', recommended_value=5.0, level=OptionRecommendation.LOW, help=_('Set the right margin in pts. Default is %default. ' + 'Setting this to less than zero will cause no margin to be set. ' 'Note: 72 pts equals 1 inch')), OptionRecommendation(name='change_justification', diff --git a/src/calibre/ebooks/oeb/base.py b/src/calibre/ebooks/oeb/base.py index 252d5b34b3..7e76d4cc0d 100644 --- a/src/calibre/ebooks/oeb/base.py +++ b/src/calibre/ebooks/oeb/base.py @@ -966,7 +966,7 @@ class Manifest(object): data = data.cssText if isinstance(data, unicode): data = data.encode('utf-8') - return data + return data + b'\n' return str(data) def __unicode__(self): diff --git a/src/calibre/ebooks/oeb/transforms/flatcss.py b/src/calibre/ebooks/oeb/transforms/flatcss.py index 890c7db9ad..43fec3985f 100644 --- a/src/calibre/ebooks/oeb/transforms/flatcss.py +++ b/src/calibre/ebooks/oeb/transforms/flatcss.py @@ -157,10 +157,12 @@ class CSSFlattener(object): bs = body.get('style', '').split(';') bs.append('margin-top: 0pt') bs.append('margin-bottom: 0pt') - bs.append('margin-left : %fpt'%\ - float(self.context.margin_left)) - bs.append('margin-right : %fpt'%\ - float(self.context.margin_right)) + if float(self.context.margin_left) >= 0: + bs.append('margin-left : %gpt'%\ + float(self.context.margin_left)) + if float(self.context.margin_right) >= 0: + bs.append('margin-right : %gpt'%\ + float(self.context.margin_right)) bs.extend(['padding-left: 0pt', 'padding-right: 0pt']) if self.page_break_on_body: bs.extend(['page-break-before: always']) @@ -393,10 +395,11 @@ class CSSFlattener(object): l = etree.SubElement(head, XHTML('link'), rel='stylesheet', type=CSS_MIME, href=href) l.tail='\n' - href = item.relhref(global_href) - l = etree.SubElement(head, XHTML('link'), - rel='stylesheet', type=CSS_MIME, href=href) - l.tail = '\n' + if global_href: + href = item.relhref(global_href) + l = etree.SubElement(head, XHTML('link'), + rel='stylesheet', type=CSS_MIME, href=href) + l.tail = '\n' def replace_css(self, css): manifest = self.oeb.manifest @@ -413,14 +416,16 @@ class CSSFlattener(object): global_css = defaultdict(list) for item in self.oeb.spine: stylizer = self.stylizers[item] - stylizer.page_rule['margin-top'] = '%gpt'%\ - float(self.context.margin_top) - stylizer.page_rule['margin-bottom'] = '%gpt'%\ - float(self.context.margin_bottom) + if float(self.context.margin_top) >= 0: + stylizer.page_rule['margin-top'] = '%gpt'%\ + float(self.context.margin_top) + if float(self.context.margin_bottom) >= 0: + stylizer.page_rule['margin-bottom'] = '%gpt'%\ + float(self.context.margin_bottom) items = stylizer.page_rule.items() items.sort() css = ';\n'.join("%s: %s" % (key, val) for key, val in items) - css = '@page {\n%s\n}\n'%css + css = ('@page {\n%s\n}\n'%css) if items else '' rules = [r.cssText for r in stylizer.font_face_rules] raw = '\n\n'.join(rules) css += '\n\n' + raw @@ -429,9 +434,11 @@ class CSSFlattener(object): gc_map = {} manifest = self.oeb.manifest for css in global_css: - id_, href = manifest.generate('page_css', 'page_styles.css') - manifest.add(id_, href, CSS_MIME, data=cssutils.parseString(css, - validate=False)) + href = None + if css.strip(): + id_, href = manifest.generate('page_css', 'page_styles.css') + manifest.add(id_, href, CSS_MIME, data=cssutils.parseString(css, + validate=False)) gc_map[css] = href ans = {} diff --git a/src/calibre/gui2/convert/page_setup.ui b/src/calibre/gui2/convert/page_setup.ui index 0ea2093697..ee018628ac 100644 --- a/src/calibre/gui2/convert/page_setup.ui +++ b/src/calibre/gui2/convert/page_setup.ui @@ -109,12 +109,18 @@ + + No margin + pt 1 + + -1.000000000000000 + 200.000000000000000 @@ -132,12 +138,18 @@ + + No margin + pt 1 + + -1.000000000000000 + 200.000000000000000 @@ -155,12 +167,18 @@ + + No margin + pt 1 + + -1.000000000000000 + 200.000000000000000 @@ -178,12 +196,18 @@ + + No margin + pt 1 + + -1.000000000000000 + 200.000000000000000