diff options
| author | Gertjan van den Burg <gertjanvandenburg@gmail.com> | 2020-02-23 15:59:17 +0000 |
|---|---|---|
| committer | Gertjan van den Burg <gertjanvandenburg@gmail.com> | 2020-02-23 15:59:17 +0000 |
| commit | e36c312a29ead22feaa32d6e975da2d30cb42a9d (patch) | |
| tree | ff7afeaa1641a0a587b02a04ecc6080d1ac2f688 | |
| parent | Merge branch 'feature/no_crop' (diff) | |
| parent | Merge branch 'master' into feature/right_align (diff) | |
| download | paper2remarkable-e36c312a29ead22feaa32d6e975da2d30cb42a9d.tar.gz paper2remarkable-e36c312a29ead22feaa32d6e975da2d30cb42a9d.zip | |
Merge branch 'feature/right_align'
| -rw-r--r-- | paper2remarkable/crop.py | 58 | ||||
| -rw-r--r-- | paper2remarkable/pdf_ops.py | 32 | ||||
| -rw-r--r-- | paper2remarkable/providers/_base.py | 8 | ||||
| -rw-r--r-- | paper2remarkable/ui.py | 16 |
4 files changed, 95 insertions, 19 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 0eda537..c68caab 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, no_crop=False, remarkable_dir="/", @@ -67,6 +68,8 @@ class Provider(metaclass=abc.ABCMeta): self.operations = [] elif center: self.operations = [("center", self.center_pdf)] + elif right: + self.operations = [("right", self.right_pdf)] else: self.operations = [("crop", self.crop_pdf)] @@ -92,6 +95,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 0c8ea91..a8218ec 100644 --- a/paper2remarkable/ui.py +++ b/paper2remarkable/ui.py @@ -53,10 +53,17 @@ def parse_args(): default="/", ) parser.add_argument( + "-r", + "--right", + help="Right align so the menu doesn't cover it", + action="store_true", + ) + parser.add_argument( '-k', '--no-crop', help="Don't crop the pdf file", - action="store_true") + action="store_true" + ) parser.add_argument( "-v", "--verbose", help="be verbose", action="store_true" @@ -114,9 +121,15 @@ 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 args.center and args.no_crop: exception("Can't center and not crop at the same time!") + if args.right_align and args.no_crop: + exception("Can't right align and not crop at the same time!") + if LocalFile.validate(args.input): # input is a local file provider = LocalFile @@ -139,6 +152,7 @@ def main(): upload=not args.no_upload, debug=args.debug, center=args.center, + right=args.right, blank=args.blank, no_crop=args.no_crop, remarkable_dir=args.remarkable_dir, |
