From 6dc53379ee4e41f9198eaa517a53d262467f066f Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 30 Oct 2019 09:42:07 +0530 Subject: [PATCH] Make code to parse PDF page sizes re-useable --- src/calibre/ebooks/pdf/image_writer.py | 38 ++++++++++++++------------ 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/src/calibre/ebooks/pdf/image_writer.py b/src/calibre/ebooks/pdf/image_writer.py index 971174d4ea..2682c9c0dc 100644 --- a/src/calibre/ebooks/pdf/image_writer.py +++ b/src/calibre/ebooks/pdf/image_writer.py @@ -40,6 +40,26 @@ class PDFMetadata(object): # {{{ # Page layout {{{ +def parse_pdf_page_size(spec, unit='inch', dpi=72.0): + width, sep, height = spec.lower().partition('x') + if height: + try: + width = float(width.replace(',', '.')) + height = float(height.replace(',', '.')) + except Exception: + pass + else: + if unit == 'devicepixel': + factor = 72.0 / dpi + else: + factor = { + 'point':1.0, 'inch':inch, 'cicero':cicero, + 'didot':didot, 'pica':pica, 'millimeter':mm, + 'centimeter':cm + }.get(unit, 1.0) + return QPageSize(QSize(factor*width, factor*height), matchPolicy=QPageSize.ExactMatch) + + def get_page_size(opts, for_comic=False): use_profile = opts.use_profile_size and opts.output_profile.short_name != 'default' and opts.output_profile.width <= 9999 if use_profile: @@ -53,23 +73,7 @@ def get_page_size(opts, for_comic=False): else: page_size = None if opts.custom_size is not None: - width, sep, height = opts.custom_size.partition('x') - if height: - try: - width = float(width.replace(',', '.')) - height = float(height.replace(',', '.')) - except: - pass - else: - if opts.unit == 'devicepixel': - factor = 72.0 / opts.output_profile.dpi - else: - factor = { - 'point':1.0, 'inch':inch, 'cicero':cicero, - 'didot':didot, 'pica':pica, 'millimeter':mm, - 'centimeter':cm - }[opts.unit] - page_size = QPageSize(QSize(factor*width, factor*height), matchPolicy=QPageSize.ExactMatch) + page_size = parse_pdf_page_size(opts.custom_size, opts.unit, opts.output_profile.dpi) if page_size is None: page_size = QPageSize(getattr(QPageSize, opts.paper_size.capitalize())) return page_size