From 51383c3ae072c3671aee6b328cfc87cd5be19bee Mon Sep 17 00:00:00 2001 From: Lucas Amaro Date: Sun, 27 Dec 2020 14:26:39 -0300 Subject: [PATCH] Add basic support to CalligraphyPen, simplify parsing by skipping tilt/pressure for now --- remarks/conversion/drawing.py | 16 +++-- remarks/conversion/parsing.py | 121 +++++++++------------------------- 2 files changed, 39 insertions(+), 98 deletions(-) diff --git a/remarks/conversion/drawing.py b/remarks/conversion/drawing.py index 3de5536..6421239 100644 --- a/remarks/conversion/drawing.py +++ b/remarks/conversion/drawing.py @@ -42,11 +42,11 @@ def draw_svg(data, dims={"x": RM_WIDTH, "y": RM_HEIGHT}, color=True): output += '" />\n' - output += "" # close stroke + output += "" # Close stroke - output += "" # close layer + output += "" # Close layer - # overlay it with a clickable rect for flipping pages + # Overlay it with a clickable rect for flipping pages output += ( f'' ) @@ -98,19 +98,21 @@ def draw_pdf(data, page, color=True, inplace=False): for seg_name, seg_data in segments.items(): seg_type = seg_name.split("_")[0] + # Highlights if seg_type == "Highlighter": for seg_rect in seg_data["rects"]: annot = page.addHighlightAnnot(seg_rect) - # TODO: setOpacity and setBorder don't seem to have any effect on HighlightAnnot - # maybe an issue related to https://github.com/pymupdf/PyMuPDF/issues/421 - # + # TODO: setOpacity and setBorder not working with HighlightAnnot + # maybe related to https://github.com/pymupdf/PyMuPDF/issues/421 # see also: https://pymupdf.readthedocs.io/en/latest/faq.html#how-to-add-and-modify-annotations + annot.setOpacity(seg_data["opacity"]) annot.setBorder(width=seg_data["stroke-width"]) annot.update() - else: # some kind of Scribble + # Scribbles + else: for seg_points in seg_data["points"]: color_array = fitz.utils.getColor(c[seg_data["color-code"]]) diff --git a/remarks/conversion/parsing.py b/remarks/conversion/parsing.py index 84c7d3e..d97e189 100644 --- a/remarks/conversion/parsing.py +++ b/remarks/conversion/parsing.py @@ -18,24 +18,16 @@ 3: "Marker", 16: "Marker", 6: "Eraser", + 8: "EraseArea", 7: "SharpPencil", 13: "SharpPencil", 1: "TiltPencil", 14: "TiltPencil", - 8: "EraseArea", 5: "Highlighter", 18: "Highlighter", + 21: "CalligraphyPen", } -# TODO: support Calligraphy pen (added in 2.3) -# https://support.remarkable.com/hc/en-us/articles/360013230697-Software-release-2-3 - - -# TODO: review stroke-width and opacity for all tools, -# especially the ones with pressure and tilting capabilities. -# As of July 2020 (version 2.2.0.48), the parameters below -# don't seem to match reMarkable's (on device) rendering - def get_adjusted_pdf_dims(pdf_width, pdf_height, scale): if (pdf_width / pdf_height) >= (RM_WIDTH / RM_HEIGHT): @@ -56,26 +48,32 @@ def get_pdf_to_device_ratio(pdf_width, pdf_height): pdf_aspect_ratio = pdf_width / pdf_height device_aspect_ratio = RM_WIDTH / RM_HEIGHT - # if PDF page is wider than reMarkable's aspect ratio, + # If PDF page is wider than reMarkable's aspect ratio, # use pdf_width as reference for the scale ratio. - # there should be no "leftover" (gap) on the horizontal + # There should be no "leftover" (gap) on the horizontal if pdf_aspect_ratio >= device_aspect_ratio: scale = pdf_width / RM_WIDTH # PDF page is narrower than reMarkable's a/r, # use pdf_height as reference for the scale ratio. - # there should be no "leftover" (gap) on the vertical + # There should be no "leftover" (gap) on the vertical else: scale = pdf_height / RM_HEIGHT return scale +# TODO: Review stroke-width and opacity for all tools + +# TODO: Add support for pressure and tilting as well +# for e.g. Paintbrush (Brush), CalligraphyPen, TiltPencil, etc + + def process_tool_meta(pen, dims, w, opc, cc): tool = RM_TOOLS[pen] # print(tool) - if tool == "Brush": + if tool == "Brush" or tool == "CalligraphyPen": pass elif tool == "Ballpoint" or tool == "Fineliner": w = 32 * w * w - 116 * w + 107 @@ -99,7 +97,7 @@ def process_tool_meta(pen, dims, w, opc, cc): else: raise ValueError("Found an unknown tool: {pen}") - w /= 2.3 # adjust for transformation to A4 + w /= 2.3 # Adjust to A4 meta = {} meta["pen-code"] = pen @@ -107,7 +105,7 @@ def process_tool_meta(pen, dims, w, opc, cc): name_code = f"{tool}_{pen}" - # w for stroke-width, opc for opacity + # Shorthands: w for stroke-width, opc for opacity return name_code, meta, w, opc @@ -132,12 +130,12 @@ def update_stroke_dict(st, tool, tool_meta): return st -def update_segment_dict(sg, seg_name, opacity, stroke_width): - sg[seg_name] = {} - sg[seg_name]["style"] = {} - sg[seg_name]["style"]["opacity"] = f"{opacity:.3f}" - sg[seg_name]["style"]["stroke-width"] = f"{stroke_width:.3f}" - sg[seg_name]["points"] = [] +def update_seg_dict(sg, name, opacity, stroke_width): + sg[name] = {} + sg[name]["style"] = {} + sg[name]["style"]["opacity"] = f"{opacity:.3f}" + sg[name]["style"]["stroke-width"] = f"{stroke_width:.3f}" + sg[name]["points"] = [] return sg @@ -186,7 +184,7 @@ def parse_rm_file(file_path, dims={"x": RM_WIDTH, "y": RM_HEIGHT}): output = {} output["layers"] = [] - for layer in range(nlayers): + for _ in range(nlayers): fmt = "