From 64353e7d6cb2f97dcc10b0a73307924ecbe92b50 Mon Sep 17 00:00:00 2001 From: Gertjan van den Burg Date: Sun, 23 Feb 2020 15:50:58 +0000 Subject: Add option to right-align a file --- paper2remarkable/crop.py | 58 ++++++++++++++++++++++++++++++++----- paper2remarkable/pdf_ops.py | 32 +++++++++++++------- paper2remarkable/providers/_base.py | 8 ++++- paper2remarkable/ui.py | 10 +++++++ 4 files changed, 90 insertions(+), 18 deletions(-) diff --git a/paper2remarkable/crop.py b/paper2remarkable/crop.py index 2b6e086..02c6757 100644 --- a/paper2remarkable/crop.py +++ b/paper2remarkable/crop.py @@ -47,10 +47,7 @@ def find_offset_byte_line(line): class Cropper(object): def __init__( - self, - input_file=None, - output_file=None, - pdftoppm_path="pdftoppm", + self, input_file=None, output_file=None, pdftoppm_path="pdftoppm", ): if not input_file is None: self.input_file = os.path.abspath(input_file) @@ -67,6 +64,9 @@ class Cropper(object): def center(self, padding=15): return self.process_file(self.center_page, padding=padding) + def right(self, padding=15): + return self.process_file(self.right_page, padding=padding) + def process_file(self, page_func, *args, **kwargs): n = self.reader.getNumPages() for page_idx in range(n): @@ -81,13 +81,18 @@ class Cropper(object): logger.info("Processing pages ... (%i/%i)" % (n, n)) return 0 + def crop_page(self, page_idx, margins): + return self.process_page(page_idx, self.get_bbox, margins=margins) + def center_page(self, page_idx, padding): return self.process_page( page_idx, self.get_center_bbox, padding=padding ) - def crop_page(self, page_idx, margins): - return self.process_page(page_idx, self.get_bbox, margins=margins) + def right_page(self, page_idx, padding): + return self.process_page( + page_idx, self.get_right_bbox, padding=padding + ) def export_page(self, page_idx): """Helper function that exports a single page given by index """ @@ -216,7 +221,9 @@ class Cropper(object): ) left -= margins[0] + left = max(left, 0) top -= margins[1] + top = max(top, 0) right -= margins[2] bottom -= margins[3] @@ -252,7 +259,7 @@ class Cropper(object): # if the document is wider than the remarkable, we add top-padding to # center it, otherwise we add left-padding - x, y = 0, 0 + x = y = 0 if h_prime / w_prime < RM_HEIGHT / RM_WIDTH: y = ((RM_HEIGHT / RM_WIDTH) * w_prime - h_prime) / 2 else: @@ -260,3 +267,40 @@ class Cropper(object): margins = [padding + x, padding + y, padding, padding] return self.get_bbox(filename, margins=margins) + + def get_right_bbox(self, filename, padding=15): + """Get the bounding box that ensures the menu doesn't hide the text + """ + + bbox = self.get_bbox(filename, margins=0) + + h = bbox[3] - bbox[1] + w = bbox[2] - bbox[0] + + # Note, the menu width is about 12mm and the entire screen is about + # 156mm. This informs the width of the left padding we'll add. + menu_width = 12 / 156 * RM_WIDTH + + H = RM_HEIGHT + W = RM_WIDTH + + # TODO: This math is approximate. The goal is to get the page centered + # in the remaining space after taking the menu width into account, + # while also providing equal padding at the top and bottom. This seems + # to give too much padding on the left for some pages, but I'm not sure + # why. Pull requests welcome! + rho_rm = H / (W - menu_width) + rho_page = (h + 2 * padding) / (w + 2 * padding) + x = y = 0 + if rho_rm < rho_page: + x = -w - 2 * padding + (h + 2 * padding) * (W - menu_width) / H + elif rho_rm > rho_page: + y = -h - 2 * padding + H * (w + 2 * padding) / (W - menu_width) + + margins = [ + menu_width + x + padding, + padding + y, + padding, + padding, + ] + return self.get_bbox(filename, margins=margins) diff --git a/paper2remarkable/pdf_ops.py b/paper2remarkable/pdf_ops.py index 4c695c6..c7561e3 100644 --- a/paper2remarkable/pdf_ops.py +++ b/paper2remarkable/pdf_ops.py @@ -25,11 +25,7 @@ def crop_pdf(filepath, pdftoppm_path="pdftoppm"): logger.info("Cropping pdf file") cropped_file = os.path.splitext(filepath)[0] + "-crop.pdf" - cropper = Cropper( - filepath, - cropped_file, - pdftoppm_path=pdftoppm_path, - ) + cropper = Cropper(filepath, cropped_file, pdftoppm_path=pdftoppm_path,) status = cropper.crop(margins=15) if not status == 0: @@ -49,11 +45,7 @@ def center_pdf(filepath, pdftoppm_path="pdftoppm"): logger.info("Centering pdf file") centered_file = os.path.splitext(filepath)[0] + "-center.pdf" - cropper = Cropper( - filepath, - centered_file, - pdftoppm_path=pdftoppm_path, - ) + cropper = Cropper(filepath, centered_file, pdftoppm_path=pdftoppm_path,) status = cropper.center() if not status == 0: @@ -67,6 +59,26 @@ def center_pdf(filepath, pdftoppm_path="pdftoppm"): return centered_file +def right_pdf(filepath, pdftoppm_path="pdftoppm"): + """Right-align the pdf file on the reMarkable + """ + logger.info("Right-aligning pdf file") + righted_file = os.path.splitext(filepath)[0] + "-right.pdf" + + cropper = Cropper(filepath, righted_file, pdftoppm_path=pdftoppm_path) + status = cropper.right() + + if not status == 0: + logger.warning("Failed to right-align the pdf file at: %s" % filepath) + return filepath + if not os.path.exists(righted_file): + logger.warning( + "Can't find right-aligned file '%s' where expected" % righted_file + ) + return filepath + return righted_file + + def blank_pdf(filepath): """Add blank pages to PDF """ diff --git a/paper2remarkable/providers/_base.py b/paper2remarkable/providers/_base.py index bf8cdf5..b47fbae 100644 --- a/paper2remarkable/providers/_base.py +++ b/paper2remarkable/providers/_base.py @@ -15,7 +15,7 @@ import tempfile import time from ._info import Informer -from ..pdf_ops import crop_pdf, center_pdf, blank_pdf, shrink_pdf +from ..pdf_ops import crop_pdf, center_pdf, right_pdf, blank_pdf, shrink_pdf from ..utils import ( assert_file_is_pdf, download_url, @@ -36,6 +36,7 @@ class Provider(metaclass=abc.ABCMeta): upload=True, debug=False, center=False, + right=False, blank=False, remarkable_dir="/", rmapi_path="rmapi", @@ -65,6 +66,8 @@ class Provider(metaclass=abc.ABCMeta): self.operations = [("crop", self.crop_pdf)] if center: self.operations = [("center", self.center_pdf)] + if right: + self.operations = [("right", self.right_pdf)] if blank: self.operations.append(("blank", blank_pdf)) @@ -88,6 +91,9 @@ class Provider(metaclass=abc.ABCMeta): def center_pdf(self, filepath): return center_pdf(filepath, pdftoppm_path=self.pdftoppm_path) + def right_pdf(self, filepath): + return right_pdf(filepath, pdftoppm_path=self.pdftoppm_path) + def shrink_pdf(self, filepath): return shrink_pdf(filepath, gs_path=self.gs_path) diff --git a/paper2remarkable/ui.py b/paper2remarkable/ui.py index 2303603..835f044 100644 --- a/paper2remarkable/ui.py +++ b/paper2remarkable/ui.py @@ -52,6 +52,12 @@ def parse_args(): dest="remarkable_dir", default="/", ) + parser.add_argument( + "-r", + "--right", + help="Right align so the menu doesn't cover it", + action="store_true", + ) parser.add_argument( "-v", "--verbose", help="be verbose", action="store_true" ) @@ -108,6 +114,9 @@ def main(): args = parse_args() cookiejar = None + if args.center and args.right: + exception("Can't center and right align at the same time!") + if LocalFile.validate(args.input): # input is a local file provider = LocalFile @@ -130,6 +139,7 @@ def main(): upload=not args.no_upload, debug=args.debug, center=args.center, + right=args.right, blank=args.blank, remarkable_dir=args.remarkable_dir, rmapi_path=args.rmapi, -- cgit v1.2.3