-
Notifications
You must be signed in to change notification settings - Fork 1
/
model.go
586 lines (511 loc) · 13.1 KB
/
model.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
package rmtool
import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"io"
"strconv"
"strings"
"github.com/akeil/rmtool/internal/errors"
)
// TrashFolder is the ID whoch is used for the reMArkable trash folder.
// Items that have been soft-deleted have their parent ID set to this value.
const TrashFolder = "trash"
// NotebookType is used to distinguish betweeen documents and folders.
type NotebookType int
const (
DocumentType NotebookType = iota
CollectionType
)
// Orientation is the layout of a notebook page.
// It can be Portrait or Landscape.
type Orientation int
const (
Portrait Orientation = iota
Landscape
)
// FileType are the different types of supported content for a notebook.
type FileType int
const (
Notebook FileType = iota
Epub
Pdf
)
type LineHeight int
const (
LineHeightDefault LineHeight = -1
LineHeightSmall LineHeight = 100
LineHeightMedium LineHeight = 150
LineHeightLarge LineHeight = 200
)
type TextAlign int
const (
AlignLeft TextAlign = iota
AlignJustify
)
const blankTemplate = "Blank"
const maxLayers = 5
const defaultCoverPage = -1
// Content holds the data from the remarkable `.content` file.
// It describes the content for a notebook, specifically the sequence of pages.
// Collections have an empty content object.
type Content struct {
DummyDocument bool `json:"dummyDocument"`
ExtraMetadata ExtraMetadata `json:"extraMetadata"`
// FileType is the type of content (i.e. handwritten Notebook or PDF, EPUB).
FileType FileType `json:"fileType"`
// Orientation gives the base layout orientation.
// Individual pages can have a different orientation.
Orientation Orientation `json:"orientation"`
// PageCount is the number of pages in this notebooks.
PageCount int `json:"pageCount"`
// Pages is a list of page IDs in the correct order.
Pages []string `json:"pages"`
// CoverPageNumber is the page that should be used as the cover in the UI.
CoverPageNumber int `json:"coverPageNumber"`
// not sure if these are relevant
// FontName for EPUB, empty to use default (probably a list w/ supported font names)
FontName string `json:"fontName"`
// LineHeight always seems to be -1 / 150 / 200 / 100?
LineHeight LineHeight `json:"lineHeight"`
// MArgins are the page margins (left/right?) for EPUB and PDF files, default is 100 (180 for PDF?)
Margins int `json:"margins"`
// TextAlignment for EPUB, left or justify
TextAlignment TextAlign `json:"textAlignment"`
// TextScale for EPUB, default is 1.0,
TextScale float32 `json:"textScale"`
Transform Transform `json:"transform"`
}
func NewContent(f FileType) *Content {
return &Content{
DummyDocument: false,
ExtraMetadata: NewExtraMetadata(),
CoverPageNumber: defaultCoverPage,
FileType: f,
Orientation: Portrait,
PageCount: 0,
Pages: make([]string, 0),
// default values taken from a sample file
FontName: "",
LineHeight: LineHeightDefault,
Margins: 100,
TextAlignment: AlignLeft,
TextScale: 1.0,
Transform: NewTransform(),
}
}
func (c *Content) Validate() error {
switch c.FileType {
case Notebook, Pdf, Epub:
// ok
default:
return errors.NewValidationError("invalid file type %v", c.FileType)
}
switch c.Orientation {
case Portrait, Landscape: // ok
default:
return errors.NewValidationError("invalid orientation %v", c.Orientation)
}
if c.PageCount != len(c.Pages) {
return errors.NewValidationError("pageCount does not match number of pages %v != %v", c.PageCount, len(c.Pages))
}
// Cover page may be -1 (=not set)
// or an existing page
if c.CoverPageNumber != defaultCoverPage {
if c.CoverPageNumber < 1 || c.CoverPageNumber > c.PageCount {
return errors.NewValidationError("cover page %v is not an existing page", c.CoverPageNumber)
}
}
// TODO validate font names
// TODO validate LineHeight
// TODO validate Margins
// TODO: validate TextScale
switch c.TextAlignment {
case AlignLeft, AlignJustify:
// ok
default:
return errors.NewValidationError("invalid text align %v", c.TextAlignment)
}
return nil
}
type Transform struct {
// TODO: these might also be floats
// never seen anything other than identity transform with values set to 1 or 0
M11 int `json:"m11"`
M12 int `json:"m12"`
M13 int `json:"m13"`
M21 int `json:"m21"`
M22 int `json:"m22"`
M23 int `json:"m23"`
M31 int `json:"m31"`
M32 int `json:"m32"`
M33 int `json:"m33"`
}
func NewTransform() Transform {
return Transform{
M11: 1,
M22: 1,
M33: 1,
}
}
// PageMetadata holds the layer information for a single page.
type PageMetadata struct {
// Layers is the list of layers for a page.
Layers []LayerMetadata `json:"layers"`
}
func (p PageMetadata) Validate() error {
if p.Layers == nil {
return errors.NewValidationError("no layers defined")
}
if len(p.Layers) == 0 {
return errors.NewValidationError("no layers defined")
}
if len(p.Layers) > maxLayers {
return errors.NewValidationError("maximum number of layers exceeded")
}
for _, l := range p.Layers {
err := l.Validate()
if err != nil {
return err
}
}
return nil
}
// LayerMetadata describes one layer.
type LayerMetadata struct {
// Name is the display name for this layer.
Name string `json:"name"`
// TODO: visible y/n?
}
func (l LayerMetadata) Validate() error {
if l.Name == "" {
return errors.NewValidationError("layer name must not be empty")
}
return nil
}
func (n *NotebookType) UnmarshalJSON(b []byte) error {
var s string
err := json.Unmarshal(b, &s)
if err != nil {
return err
}
var nt NotebookType
switch s {
case "DocumentType":
nt = DocumentType
case "CollectionType":
nt = CollectionType
default:
return fmt.Errorf("invalid notebook type %q", s)
}
*n = nt
return nil
}
func (n NotebookType) MarshalJSON() ([]byte, error) {
var s string
switch n {
case DocumentType:
s = "DocumentType"
case CollectionType:
s = "CollectionType"
default:
return nil, fmt.Errorf("invalid notebook type %v", n)
}
buf := bytes.NewBufferString(`"`)
buf.WriteString(s)
buf.WriteString(`"`)
return buf.Bytes(), nil
}
func (o *Orientation) UnmarshalJSON(b []byte) error {
var s string
err := json.Unmarshal(b, &s)
if err != nil {
return err
}
var x Orientation
switch s {
case "portrait":
x = Portrait
case "landscape":
x = Landscape
default:
return fmt.Errorf("invalid notebook type %q", s)
}
*o = x
return nil
}
func (o Orientation) MarshalJSON() ([]byte, error) {
s := o.String()
if s == "UNKNOWN" {
return nil, fmt.Errorf("invalid notebook type %v", o)
}
buf := bytes.NewBufferString(`"`)
buf.WriteString(s)
buf.WriteString(`"`)
return buf.Bytes(), nil
}
func (o Orientation) String() string {
switch o {
case Portrait:
return "portrait"
case Landscape:
return "landscape"
default:
return "UNKNOWN"
}
}
func (f *FileType) UnmarshalJSON(b []byte) error {
var s string
err := json.Unmarshal(b, &s)
if err != nil {
return err
}
var ft FileType
switch s {
case "notebook":
ft = Notebook
case "epub":
ft = Epub
case "pdf":
ft = Pdf
default:
return fmt.Errorf("invalid file type %q", s)
}
*f = ft
return nil
}
func (f FileType) MarshalJSON() ([]byte, error) {
s := f.String()
if s == "UNKNOWN" {
return nil, fmt.Errorf("invalid file type %v", f)
}
buf := bytes.NewBufferString(`"`)
buf.WriteString(s)
buf.WriteString(`"`)
return buf.Bytes(), nil
}
func (f FileType) String() string {
switch f {
case Notebook:
return "notebook"
case Epub:
return "epub"
case Pdf:
return "pdf"
default:
return "UNKNOWN"
}
}
func (f FileType) Ext() string {
switch f {
case Epub:
return ".epub"
case Pdf:
return ".pdf"
default:
return ""
}
}
func (t *TextAlign) UnmarshalJSON(b []byte) error {
var s string
err := json.Unmarshal(b, &s)
if err != nil {
return err
}
var ta TextAlign
switch s {
case "left":
ta = AlignLeft
case "justify":
ta = AlignJustify
default:
return fmt.Errorf("invalid text align %q", s)
}
*t = ta
return nil
}
func (t TextAlign) MarshalJSON() ([]byte, error) {
s := t.String()
if s == "UNKNOWN" {
return nil, fmt.Errorf("invalid text align type %v", t)
}
buf := bytes.NewBufferString(`"`)
buf.WriteString(s)
buf.WriteString(`"`)
return buf.Bytes(), nil
}
func (t TextAlign) String() string {
switch t {
case AlignLeft:
return "left"
case AlignJustify:
return "justify"
default:
return "UNKNOWN"
}
}
func ReadPagedata(r io.Reader) ([]string, error) {
pd := make([]string, 0)
s := bufio.NewScanner(r)
for s.Scan() {
text := s.Text()
text = strings.TrimSpace(text)
pd = append(pd, text)
}
return pd, s.Err()
}
func WritePagedata(pd []string, w io.Writer) error {
for _, s := range pd {
_, err := w.Write([]byte(s + "\n"))
if err != nil {
return err
}
}
return nil
}
type ExtraMetadata struct {
LastBallpointColor string
LastBallpointSize intStr
LastBallpointv2Color string
LastBallpointv2Size intStr
LastBrushColor string
LastBrushThicknessScale intStr
LastCalligraphyColor string
LastCalligraphySize intStr
LastClearPageColor string
LastClearPageSize intStr
LastColor string
LastEraseSectionColor string
LastEraseSectionSize intStr
LastEraserColor string
LastEraserSize intStr
LastEraserThicknessScale intStr
LastEraserTool string //"Eraser"
LastFinelinerColor string
LastFinelinerSize intStr
LastFinelinerv2Color string
LastFinelinerv2Size intStr
LastHighlighterColor string
LastHighlighterSize intStr
LastHighlighterv2Color string
LastHighlighterv2Size intStr
LastMarkerColor string
LastMarkerSize intStr
LastMarkerv2Color string
LastMarkerv2Size intStr
LastPaintbrushColor string
LastPaintbrushSize intStr
LastPaintbrushv2Color string
LastPaintbrushv2Size intStr
LastPen string // Ballpointv2
LastPenColor string
LastPenThicknessScale intStr
LastPencil string // SharpPencil
LastPencilColor string
LastPencilSize intStr
LastPencilThicknessScale intStr
LastPencilv2Color string
LastPencilv2Size intStr
LastReservedPenColor string
LastReservedPenSize intStr
LastSelectionToolColor string
LastSelectionToolSize intStr
LastSharpPencilColor string
LastSharpPencilSize intStr
LastSharpPencilv2Color string
LastSharpPencilv2Size intStr
LastSolidPenColor string
LastSolidPenSize intStr
LastTool string // Ballpoint
LastUndefinedColor string
LastUndefinedSize intStr
LastZoomToolColor string
LastZoomToolSize intStr
ThicknessScale intStr
}
func NewExtraMetadata() ExtraMetadata {
// default values taken from a sample file
return ExtraMetadata{
LastBallpointColor: "Black",
LastBallpointSize: 2,
LastBallpointv2Color: "Black",
LastBallpointv2Size: 2,
LastBrushColor: "Black",
LastBrushThicknessScale: 2,
LastCalligraphyColor: "Black",
LastCalligraphySize: 2,
LastClearPageColor: "Black",
LastClearPageSize: 2,
LastColor: "Black",
LastEraseSectionColor: "Black",
LastEraseSectionSize: 2,
LastEraserColor: "Black",
LastEraserSize: 2,
LastEraserThicknessScale: 2,
LastEraserTool: "Eraser",
LastFinelinerColor: "Black",
LastFinelinerSize: 2,
LastFinelinerv2Color: "Black",
LastFinelinerv2Size: 2,
LastHighlighterColor: "Black",
LastHighlighterSize: 2,
LastHighlighterv2Color: "Black",
LastHighlighterv2Size: 2,
LastMarkerColor: "Black",
LastMarkerSize: 2,
LastMarkerv2Color: "Black",
LastMarkerv2Size: 2,
LastPaintbrushColor: "Black",
LastPaintbrushSize: 2,
LastPaintbrushv2Color: "Black",
LastPaintbrushv2Size: 2,
LastPen: "Ballpointv2",
LastPenColor: "Black",
LastPenThicknessScale: 2,
LastPencil: "SharpPencil",
LastPencilColor: "Black",
LastPencilSize: 2,
LastPencilThicknessScale: 2,
LastPencilv2Color: "Black",
LastPencilv2Size: 2,
LastReservedPenColor: "Black",
LastReservedPenSize: 2,
LastSelectionToolColor: "Black",
LastSelectionToolSize: 2,
LastSharpPencilColor: "Black",
LastSharpPencilSize: 2,
LastSharpPencilv2Color: "Black",
LastSharpPencilv2Size: 2,
LastSolidPenColor: "Black",
LastSolidPenSize: 2,
LastTool: "Ballpoint",
LastUndefinedColor: "Black",
LastUndefinedSize: 1,
LastZoomToolColor: "Black",
LastZoomToolSize: 2,
ThicknessScale: 2,
}
}
type intStr int
func (is *intStr) UnmarshalJSON(b []byte) error {
// expects a string lke this: 1607462787637
// with the last for digits containing nanoseconds.
var s string
err := json.Unmarshal(b, &s)
if err != nil {
return err
}
v, err := strconv.Atoi(s)
if err != nil {
return err
}
*is = intStr(v)
return nil
}
func (is intStr) MarshalJSON() ([]byte, error) {
buf := bytes.NewBufferString(`"`)
buf.WriteString(fmt.Sprintf("%v", is))
buf.WriteString(`"`)
return buf.Bytes(), nil
}