Skip to content

Commit

Permalink
Basic exporting
Browse files Browse the repository at this point in the history
  • Loading branch information
tatjam committed Sep 25, 2022
1 parent 6425de6 commit 9858b95
Show file tree
Hide file tree
Showing 10 changed files with 119 additions and 39 deletions.
3 changes: 1 addition & 2 deletions nim.cfg
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
--threads:on
-d:release
--threads:on
2 changes: 1 addition & 1 deletion presets/66d6d990-2fd8-4e31-8260-a53c41a71429.json

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion src/document/brush.nim
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ type RemarkableColor* = enum
HIGHLIGHT_PINK,
HIGHLIGHT_GREEN,
BLUE,
RED
RED,
HIGHLIGHT_OVERLAP

# Includes rmhacks tools
type RemarkableTool* = enum
Expand Down
19 changes: 19 additions & 0 deletions src/document/brushes/fineliner.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import nimPDF/nimPDF
import ../lines

# color is already set in stroke and fill color
proc draw_fineliner*(x: Stroke, to: var PDF, scale: float) =
to.saveState()

# Values found by guesstimation
to.setLineWidth(scale * 0.4)
to.setLineCap(ROUND_END)
to.setLineJoin(MITER_JOIN)
to.setMiterLimit(1.0)
to.moveTo(x.segments[0].x * SCALE_FACTOR, x.segments[0].y * SCALE_FACTOR)
for i in 1..(x.segments.len - 1):
let next = x.segments[i]
to.lineTo(next.x * SCALE_FACTOR, next.y * SCALE_FACTOR)
to.stroke()

to.restoreState()
20 changes: 20 additions & 0 deletions src/document/brushes/highlighter.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import nimPDF/nimPDF
import ../lines

# color is already set in stroke and fill color
proc draw_highlighter*(x: Stroke, to: var PDF, scale: float) =
to.saveState()

# TODO: Doesn't quite match remarkable results
to.setLineWidth(scale * 3.0)
to.setLineCap(SQUARE_END)
to.setLineJoin(BEVEL_JOIN)
to.setAlpha(0.5)
to.setBlendMode(BM_OVERLAY)
to.moveTo(x.segments[0].x * SCALE_FACTOR, x.segments[0].y * SCALE_FACTOR)
for i in 1..(x.segments.len - 1):
let next = x.segments[i]
to.lineTo(next.x * SCALE_FACTOR, next.y * SCALE_FACTOR)
to.stroke()

to.restoreState()
10 changes: 9 additions & 1 deletion src/document/document.nim
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import lines
import std/json
import std/streams
import preset
import std/tables
import std/options
import eminim
import uuids

type Document* = ref object
path: string
Expand All @@ -15,9 +19,12 @@ type Document* = ref object


# Assumes all needed files are downloaded
proc generate*(path: string): Document =
proc generate*(path: string, preset: Preset): Document =
result = new(Document)
result.path = path

result.preset = preset

let contents = parseFile("./retmp/data/" & path & ".content")
let pages = contents["pages"]
for page in pages.items:
Expand All @@ -26,6 +33,7 @@ proc generate*(path: string): Document =
# PDF files may not actually have the files if the page is empty, we generate
# it anyway as we will superimpose the pdf files later
if strm.isNil:
echo "What in the world?"
discard
else:
npage = load_page(strm).get()
Expand Down
39 changes: 30 additions & 9 deletions src/document/lines.nim
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,21 @@ import brush
export options
import nimPDF/nimPDF
import preset
import std/tables

# in millimeters
let SCREEN_WIDTH* = (1404.0 / 226.0) * 25.4
let SCREEN_HEIGHT* = (1872.0 / 226.0) * 25.4
let SCALE_FACTOR* = 25.4 / 226.0

type Segment* = object
x, y, speed, direction, width, pressure: float32
x*, y*, speed*, direction*, width*, pressure*: float32

type Stroke* = object
pen: RemarkableTool
color: RemarkableColor
width: float32
segments: seq[Segment]
width*: float32
segments*: seq[Segment]

type Layer* = object
strokes: seq[Stroke]
Expand Down Expand Up @@ -72,13 +73,33 @@ proc load_page*(file: FileStream): Option[Page] =

return some(page)

import brushes/fineliner
import brushes/highlighter

proc draw*(x: Stroke, to: var PDF, preset: Preset) =
to.setStrokeColor(initRGB("black"))
to.moveTo(x.segments[0].x * SCALE_FACTOR, x.segments[0].y * SCALE_FACTOR)
for i in 1..(x.segments.len - 1):
let next = x.segments[i]
to.lineTo(next.x * SCALE_FACTOR, next.y * SCALE_FACTOR)
to.stroke()
let tool_sets = preset.tool_settings[x.pen]
if tool_sets.does_export and preset.color_exports[x.color]:
var color: Color
if tool_sets.color_map.hasKey(x.color):
color = tool_sets.color_map[x.color]
else:
color = preset.color_map[x.color]

to.setStrokeColor(color.red.float / 255.0, color.green.float / 255.0, color.blue.float / 255.0)

case tool_sets.draw_mode:
of BRUSH: discard
of PENCIL: discard
of BALLPOINT: discard
of MARKER: discard
of FINELINER: x.draw_fineliner(to, tool_sets.draw_scale)
of HIGHLIGHTER: x.draw_highlighter(to, tool_sets.draw_scale)
of ERASER: discard
of MECHANICAL: discard
of ERASER_AREA: discard
of CALLIGRAPHY: discard
of UNKNOWN: discard


proc draw*(x: Page, to: var PDF, preset: Preset) =
for layer in x.layers:
Expand Down
27 changes: 15 additions & 12 deletions src/document/preset.nim
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ import std/streams
import std/os
import brush

export Color

type ToolSettings* = ref object
does_export: bool
draw_scale: float
does_export*: bool
draw_scale*: float
# Non present entries use the color_map
color_map: Table[RemarkableColor, Color]
color_map*: Table[RemarkableColor, Color]
# What does this tool draw like? To use custom drawings or remap stuff
draw_mode: RemarkableTool
draw_mode*: RemarkableTool

proc def_tool_settings*(tool: RemarkableTool): ToolSettings =
ToolSettings(does_export: true, draw_scale: 1.0, color_map: initTable[RemarkableColor, Color](), draw_mode: tool)
Expand All @@ -38,13 +40,14 @@ proc def_preset*(): Preset =
result.icon = "📓"
result.icon_tint = rgb(255, 255, 255, 255)
result.color_map[BLACK] = rgb(0, 0, 0, 255)
result.color_map[GRAY] = rgb(0, 0, 0, 255)
result.color_map[WHITE] = rgb(0, 0, 0, 255)
result.color_map[HIGHLIGHT_YELLOW] = rgb(0, 0, 0, 255)
result.color_map[HIGHLIGHT_PINK] = rgb(0, 0, 0, 255)
result.color_map[HIGHLIGHT_PINK] = rgb(0, 0, 0, 255)
result.color_map[BLUE] = rgb(0, 0, 0, 255)
result.color_map[RED] = rgb(0, 0, 0, 255)
result.color_map[GRAY] = rgb(128, 128, 128, 255)
result.color_map[WHITE] = rgb(255, 255, 255, 255)
result.color_map[HIGHLIGHT_YELLOW] = rgb(255, 245, 168, 128)
result.color_map[HIGHLIGHT_PINK] = rgb(255, 168, 254, 128)
result.color_map[HIGHLIGHT_GREEN] = rgb(167, 255, 172, 255)
result.color_map[HIGHLIGHT_OVERLAP] = rgb(0, 0, 0, 60)
result.color_map[BLUE] = rgb(60, 89, 202, 255)
result.color_map[RED] = rgb(202, 32, 32, 255)
for color in RemarkableColor:
result.color_exports[color] = true
for tool in RemarkableTool:
Expand Down Expand Up @@ -73,4 +76,4 @@ proc storeJson*(s: Stream; m: byte) =
proc save_preset*(preset: Preset) =
let file = newFileStream("./presets/" & preset.uuid & ".json", fmWrite)
file.storeJson(preset)
file.close()
file.close()
20 changes: 13 additions & 7 deletions src/gui/base.nim
Original file line number Diff line number Diff line change
@@ -1,25 +1,31 @@
import nigui
import ../document/preset

load_presets()

import weave

init(Weave)

import ../worker/worker
import ../document/document
import std/tables

import notebooks_view
import uuids

let def = def_preset()
def.save_preset()

#[let x = download("4c6d28dc-72d8-41b8-bd39-d1988eea482d")
load_presets()

let x = download("fa906f70-e0e7-4492-ac6a-1b735b2f251c", all_presets[parseUUID("66d6d990-2fd8-4e31-8260-a53c41a71429")])
let doc = sync(x)
doc.generate_pdf("output.pdf")]#
doc.generate_pdf("output.pdf")


let y = download("d4bd814c-dc0c-4352-b3bd-e37e8b6576d1")
#[let y = download("d4bd814c-dc0c-4352-b3bd-e37e8b6576d1")
let doc2 = sync(y)
doc2.generate_pdf("output-pdf.pdf")
doc2.generate_pdf("output-pdf.pdf")]#

import notebooks_view

let MARGINS* = 8
let STATUS_SIZE* = 28
Expand Down
15 changes: 9 additions & 6 deletions src/worker/worker.nim
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import deques
import std/[os, osproc, options, json]

import ../document/document
import ../document/preset

export preset

# returns true if failed, if ext = "/", then we are copying a folder
proc download(path: string, ext: string): bool =
Expand All @@ -24,7 +27,7 @@ proc download(path: string, ext: string): bool =
# multiple scp instances can run without problem, so we spawn as many as needed
# (Note that this will always work with directories!)
# May return nil!
proc download_worker(path: string): Document =
proc download_worker(path: string, preset: Preset): Document =
# We first download the .content to investigate other needed files
#[if download(path, ".content"):
return nil
Expand All @@ -39,10 +42,10 @@ proc download_worker(path: string): Document =
# Download all pages
if download(path, "/"):
return nil]#

return nil
]#
# We generate the document from this, this is also an "expensive" operation
let doc = generate(path)
let doc = generate(path, preset)

# And the contents file
#removeFile("./retmp/data/" & path & ".contents")
Expand All @@ -53,5 +56,5 @@ proc download_worker(path: string): Document =
return doc


proc download*(path: string): Flowvar[Document] =
return spawn download_worker(path)
proc download*(path: string, preset: Preset): Flowvar[Document] =
return spawn download_worker(path, preset)

0 comments on commit 9858b95

Please sign in to comment.