2019-07-10 01:51:41 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
|
|
|
|
from gi.repository import GObject
|
2019-07-16 04:56:02 +02:00
|
|
|
|
2019-07-10 01:51:41 +02:00
|
|
|
import xdg.BaseDirectory
|
|
|
|
import svgwrite
|
|
|
|
import os
|
|
|
|
|
2019-07-18 05:02:29 +02:00
|
|
|
DATA_PATH = os.path.join(xdg.BaseDirectory.xdg_data_home, 'tuhi', 'svg')
|
2019-07-10 01:51:41 +02:00
|
|
|
|
|
|
|
|
|
|
|
class JsonSvg(GObject.Object):
|
2019-07-16 13:15:11 +02:00
|
|
|
def __init__(self, json, orientation, *args, **kwargs):
|
2019-07-10 01:51:41 +02:00
|
|
|
self.json = json
|
|
|
|
try:
|
|
|
|
os.mkdir(DATA_PATH)
|
|
|
|
except FileExistsError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
self.timestamp = json['timestamp']
|
|
|
|
self.filename = os.path.join(DATA_PATH, f'{self.timestamp}.svg')
|
2019-07-16 13:15:11 +02:00
|
|
|
self.orientation = orientation
|
2019-07-10 01:51:41 +02:00
|
|
|
self._convert()
|
|
|
|
|
|
|
|
def _convert(self):
|
|
|
|
js = self.json
|
|
|
|
dimensions = js['dimensions']
|
|
|
|
if dimensions == [0, 0]:
|
|
|
|
width, height = 100, 100
|
|
|
|
else:
|
|
|
|
# Original dimensions are too big for SVG Standard
|
|
|
|
# so we normalize them
|
|
|
|
width, height = dimensions[0] / 100, dimensions[1] / 100
|
|
|
|
|
2019-07-16 04:56:02 +02:00
|
|
|
if self.orientation in ['portrait', 'reverse-Portrait']:
|
2019-07-10 01:51:41 +02:00
|
|
|
size = (height, width)
|
|
|
|
else:
|
|
|
|
size = (width, height)
|
|
|
|
svg = svgwrite.Drawing(filename=self.filename, size=size)
|
|
|
|
g = svgwrite.container.Group(id='layer0')
|
|
|
|
for stroke_num, s in enumerate(js['strokes']):
|
|
|
|
|
|
|
|
points_with_sk_width = []
|
|
|
|
|
|
|
|
for p in s['points']:
|
|
|
|
|
|
|
|
x, y = p['position']
|
|
|
|
# Normalize coordinates too
|
|
|
|
x, y = x / 100, y / 100
|
|
|
|
|
2019-07-16 13:15:11 +02:00
|
|
|
if self.orientation == 'reverse-portrait':
|
2019-07-10 01:51:41 +02:00
|
|
|
x, y = y, width - x
|
2019-07-16 04:56:02 +02:00
|
|
|
elif self.orientation == 'portrait':
|
2019-07-10 01:51:41 +02:00
|
|
|
x, y = height - y, x
|
2019-07-16 13:15:11 +02:00
|
|
|
elif self.orientation == 'reverse-landscape':
|
2019-07-10 01:51:41 +02:00
|
|
|
x, y = width - x, height - y
|
|
|
|
|
|
|
|
delta = (p['pressure'] - 1000.0) / 1000.0
|
|
|
|
stroke_width = 0.4 + 0.20 * delta
|
|
|
|
points_with_sk_width.append((x, y, stroke_width))
|
|
|
|
|
2019-07-16 02:44:25 +02:00
|
|
|
lines = svgwrite.container.Group(id=f'strokes_{stroke_num}', stroke='black')
|
|
|
|
for i, (x, y, stroke_width) in enumerate(points_with_sk_width):
|
|
|
|
if i != 0:
|
|
|
|
xp, yp, stroke_width_p = points_with_sk_width[i - 1]
|
|
|
|
lines.add(
|
|
|
|
svg.line(
|
|
|
|
start=(xp, yp),
|
|
|
|
end=(x, y),
|
|
|
|
stroke_width=stroke_width,
|
|
|
|
style='fill:none'
|
2019-07-10 01:51:41 +02:00
|
|
|
)
|
2019-07-16 02:44:25 +02:00
|
|
|
)
|
2019-07-10 01:51:41 +02:00
|
|
|
g.add(lines)
|
|
|
|
|
|
|
|
svg.add(g)
|
|
|
|
svg.save()
|