Skip to content

Commit

Permalink
combined_pdf: draw annotations in-place, if possible
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasrla committed Dec 9, 2020
1 parent 68efd27 commit 99ecc38
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"layers": [
{
"name": "Layer 1"
}
]
}
Binary file not shown.
1 change: 1 addition & 0 deletions remarks/conversion/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
get_adjusted_pdf_dims,
get_rescaled_device_dims,
rescale_parsed_data,
get_ann_max_bound,
)

from .drawing import draw_svg, draw_pdf
Expand Down
23 changes: 23 additions & 0 deletions remarks/conversion/parsing.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import struct

import shapely.geometry as geom # Shapely

# reMarkable defaults
RM_WIDTH = 1404
RM_HEIGHT = 1872
Expand Down Expand Up @@ -317,3 +319,24 @@ def rescale_parsed_data(parsed_data, scale):
)

return parsed_data


def get_ann_max_bound(parsed_data):
# https://shapely.readthedocs.io/en/stable/manual.html#LineString
# https://shapely.readthedocs.io/en/stable/manual.html#MultiLineString
# https://shapely.readthedocs.io/en/stable/manual.html#object.bounds

collection = []

for strokes in parsed_data["layers"]:
for _, st_value in strokes["strokes"].items():
for _, sg_value in st_value["segments"].items():
for points in sg_value["points"]:
line = geom.LineString(
[(float(p[0]), float(p[1])) for p in points]
)
collection.append(line)

(minx, miny, maxx, maxy) = geom.MultiLineString(collection).bounds

return (maxx, maxy)
17 changes: 14 additions & 3 deletions remarks/remarks.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
get_adjusted_pdf_dims,
get_rescaled_device_dims,
rescale_parsed_data,
get_ann_max_bound,
)
from .conversion.drawing import draw_svg, draw_pdf
from .conversion.text import md_from_blocks, is_text_extractable
Expand Down Expand Up @@ -157,13 +158,23 @@ def run_remarks(
)

if combined_pdf:
pdf_src.insertPDF(ann_doc, start_at=page_idx)
pdf_src.deletePage(page_idx + 1)
x_max, y_max = get_ann_max_bound(parsed_data)
ann_outside = (x_max > pdf_w_adj) or (y_max > pdf_h_adj)

# If there are annotations outside the original PDF page limits,
# insert the ann_page that we have created from scratch
if ann_outside:
pdf_src.insertPDF(ann_doc, start_at=page_idx)
pdf_src.deletePage(page_idx + 1)

# Else, draw annotations in the original PDF page (in-place)
# to preserve links (and also the original page size)
else:
draw_pdf(parsed_data, pdf_src[page_idx])

ann_doc.close()

if combined_pdf:
pdf_src.save(f"{output_dir}/{name} _remarks.pdf")

pdf_src.close()

0 comments on commit 99ecc38

Please sign in to comment.