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
|
# Copyright (C) 2009, 2010, 2011 Rickard Lindberg, Roger Lindberg
#
# This file is part of Timeline.
#
# Timeline 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 3 of the License, or
# (at your option) any later version.
#
# Timeline 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.
#
# You should have received a copy of the GNU General Public License
# along with Timeline. If not, see <http://www.gnu.org/licenses/>.
from types import UnicodeType
import wx
from pysvg.structure import *
from pysvg.core import *
from pysvg.text import *
from pysvg.shape import *
from pysvg.builders import *
from pysvg.filter import *
from timelinelib.db.objects.category import sort_categories
from timelinelib.drawing.utils import darken_color
OUTER_PADDING = 5 # Space between event boxes (pixels)
INNER_PADDING = 3 # Space inside event box to text (pixels)
BASELINE_PADDING = 15 # Extra space to move events away from baseline (pixels)
PERIOD_THRESHOLD = 20 # Periods smaller than this are drawn as events (pixels)
BALLOON_RADIUS = 12
DATA_INDICATOR_SIZE = 10
SMALL_FONT_SIZE = 9
MAJOR_STRIP_FONT_SIZE = 6
def export(path, scene, view_properties):
svgDrawer = SVGDrawingAlgorithm(path, scene, view_properties, shadow=True)
svgDrawer.draw()
svgDrawer.write(path)
class SVGDrawingAlgorithm(object):
# options: shadow=True|False
def __init__(self, path, scene, view_properties, **kwargs):
# store important data references
self.path = path
self.scene = scene
self.view_properties = view_properties
# SVG document size, maybe TODO
self.metrics = dict({'widthpx':1052, 'heightpx':744});
# SVG document handle
self.svg = svg(width="%dpx" % self.metrics['widthpx'], height="%dpx" % self.metrics['heightpx'])
# Fonts and pens we use when drawing
# SVG Text style
self.myHeaderStyle = StyleBuilder()
self.mySmallTextStyle = StyleBuilder()
self.myHeaderStyle.setFontFamily(fontfamily="Verdana")
self.mySmallTextStyle.setFontFamily(fontfamily="Verdana")
self.mySmallTextStyle.setFontSize('3')
self.myHeaderStyle.setFontSize('7')
self.mySmallTextStyle.setFilling("black")
self.myHeaderStyle.setFilling("black")
filterShadow = filter(x="-.3",y="-.5", width=1.9, height=1.9)
filtBlur = feGaussianBlur(stdDeviation="4")
filtBlur.set_in("SourceAlpha")
filtBlur.set_result("out1")
filtOffset = feOffset()
filtOffset.set_in("out1")
filtOffset.set_dx(4)
filtOffset.set_dy(-4)
filtOffset.set_result("out2")
filtMergeNode1 = feMergeNode()
filtMergeNode1.set_in("out2")
filtMergeNode2 = feMergeNode()
filtMergeNode2.set_in("SourceGraphic")
filtMerge = feMerge()
filtMerge.addElement(filtMergeNode1)
filtMerge.addElement(filtMergeNode2)
filterShadow.addElement(filtBlur) # here i get an error from python. It is not allowed to add a primitive filter
filterShadow.addElement(filtOffset)
filterShadow.addElement(filtMerge)
filterShadow.set_id("filterShadow")
d=defs()
d.addElement(filterShadow)
self.svg.addElement(d)
self.DATA_ICON_WIDTH = 5
# local flags
self.shadowFlag = False
# flag handling
for key in kwargs:
if key == 'shadow':
self.shadowFlag = kwargs[key]
def write(self, path):
"""
write the SVG code into the file with filename path. No
checking is done if file/path exists
"""
self.svg.save(path)
def draw(self):
"""
Implement the drawing interface.
"""
self._draw_period_selection()
self._draw_bg()
self._draw_events(self.view_properties)
self._draw_legend(self.view_properties, self._extract_categories())
def _draw_period_selection(self):
if not self.view_properties.period_selection:
return
start, end = self.view_properties.period_selection
start_x = self.scene.x_pos_for_time(start)
end_x = self.scene.x_pos_for_time(end)
self.dc.SetBrush(self.lightgrey_solid_brush)
self.dc.SetPen(wx.TRANSPARENT_PEN)
self.dc.DrawRectangle(start_x, 0,
end_x - start_x + 1, self.scene.height)
def _draw_bg(self):
"""
Draw major and minor strips, lines to all event boxes and baseline.
Both major and minor strips have divider lines and labels.
"""
myStyle = StyleBuilder()
myStyle.setStrokeDashArray((2,2))
myStyle.setFontFamily(fontfamily="Verdana")
myStyle.setFontSize("2em")
myStyle.setTextAnchor('left')
svgGroup = g()
self._draw_minor_strips(svgGroup, myStyle)
self._draw_major_strips(svgGroup, myStyle)
self._draw_divider_line(svgGroup)
self._draw_lines_to_non_period_events(svgGroup, self.view_properties)
self._draw_now_line(svgGroup)
self.svg.addElement(svgGroup)
def _draw_minor_strips(self, group, style):
for strip_period in self.scene.minor_strip_data:
self._draw_minor_strip_divider_line_at(group,strip_period.end_time)
self._draw_minor_strip_label(group, style, strip_period)
def _draw_minor_strip_divider_line_at(self, group, time):
x = self.scene.x_pos_for_time(time)
oh = ShapeBuilder()
line = oh.createLine(x,0,x,self.scene.height, strokewidth=0.5, stroke="lightgrey")
group.addElement(line)
# self.dc.SetPen(self.black_dashed_pen)
# self.dc.DrawLine(x, 0, x, self.scene.height)
def _draw_minor_strip_label(self, group, style, strip_period):
label = self.scene.minor_strip.label(strip_period.start_time)
# self.dc.SetFont(self.scene.minor_strip.get_font(strip_period))
# (tw, th) = self.dc.GetTextExtent(label)
middle = self.scene.x_pos_for_time(strip_period.start_time) + INNER_PADDING
# check for negative values
if middle < INNER_PADDING:
return
middley = self.scene.divider_y
# self.dc.DrawText(label, middle - tw / 2, middley - th)
# Label
myText = self._text(label, middle, middley)
myText.set_style(style)
group.addElement(myText)
def _draw_major_strips(self, group, style):
# self.dc.SetFont(self.header_font)
# self.dc.SetPen(self.grey_solid_pen)
oh = ShapeBuilder()
style.setStrokeDashArray("")
fontSize = MAJOR_STRIP_FONT_SIZE
style.setFontSize("%dem" % fontSize)
for tp in self.scene.major_strip_data:
# Divider line
x = self.scene.x_pos_for_time(tp.end_time)
line = oh.createLine(x,0,x,self.scene.height,strokewidth=0.5, stroke="grey")
# self.dc.DrawLine(x, 0, x, self.scene.height)
# Label
label = self.scene.major_strip.label(tp.start_time, True)
# (tw, th) = self.dc.GetTextExtent(label)
x = self.scene.x_pos_for_time(tp.start_time) + INNER_PADDING
# x = self.scene.x_pos_for_time(tp.mean_time()) - tw / 2
# If the label is not visible when it is positioned in the middle
# of the period, we move it so that as much of it as possible is
# visible without crossing strip borders.
# self.dc.DrawText(label, x, INNER_PADDING)
extra_vertical_padding = 0
if x - INNER_PADDING < 0:
x = INNER_PADDING
extra_vertical_padding = fontSize * 4
# since there is no function like textwidth() for SVG, just take into account that text can be overwritten
# do not perform a special handling for right border, SVG is unlimited
myText = self._text(label, x, fontSize*4+INNER_PADDING+extra_vertical_padding)
myText.set_style(style)
group.addElement(myText)
def _draw_divider_line(self, group):
# self.dc.SetPen(self.black_solid_pen)
oh = ShapeBuilder()
line = oh.createLine(0, self.scene.divider_y, self.scene.width,
self.scene.divider_y, strokewidth=0.5, stroke="grey")
group.addElement(line)
# self.dc.DrawLine(0, self.scene.divider_y, self.scene.width,
# self.scene.divider_y)
def _draw_lines_to_non_period_events(self, group, view_properties):
# self.dc.SetBrush(self.black_solid_brush)
for (event, rect) in self.scene.event_data:
if rect.Y < self.scene.divider_y:
x = self.scene.x_pos_for_time(event.mean_time())
y = rect.Y + rect.Height / 2
if view_properties.is_selected(event):
myStroke="red"
else:
myStroke="black"
oh = ShapeBuilder()
line = oh.createLine(x, y, x, self.scene.divider_y, stroke=myStroke)
group.addElement(line)
circle = oh.createCircle(x, self.scene.divider_y, 2)
group.addElement(circle)
# self.dc.DrawLine(x, y, x, self.scene.divider_y)
# self.dc.DrawCircle(x, self.scene.divider_y, 2)
def _draw_now_line(self, group):
x = self.scene.x_pos_for_now()
if x > 0 and x < self.scene.width:
oh = ShapeBuilder()
line = oh.createLine(x, 0, x, self.scene.height, stroke="darkred")
group.addElement(line)
# self.dc.SetPen(self.darkred_solid_pen)
# self.dc.DrawLine(x, 0, x, self.scene.height)
def _get_base_color(self, event):
if event.category:
base_color = event.category.color
else:
base_color = (200, 200, 200)
return base_color
def _get_border_color(self, event):
base_color = self._get_base_color(event)
border_color = darken_color(base_color)
return border_color
def _map_svg_color(self, color):
"""
map (r,g,b) color to svg string
"""
sColor = "#%02X%02X%02X" % color
return sColor
def _get_box_border_color(self, event):
border_color = self._get_border_color(event)
sColor = self._map_svg_color(border_color)
return sColor
def _get_box_color(self, event):
""" get the color of the event box """
base_color = self._get_base_color(event)
sColor = self._map_svg_color(base_color)
return sColor
def _get_box_indicator_color(self, event):
base_color = self._get_base_color(event)
darker_color = darken_color(base_color, 0.6)
sColor = self._map_svg_color(darker_color)
return sColor
def _legend_should_be_drawn(self, view_properties, categories):
return view_properties.show_legend and len(categories) > 0
def _extract_categories(self):
categories = []
for (event, rect) in self.scene.event_data:
cat = event.category
if cat and not cat in categories:
categories.append(cat)
return sort_categories(categories)
def _draw_legend(self, view_properties, categories):
"""
Draw legend for the given categories.
Box in lower right corner
Motivation for positioning in right corner:
SVG text cannot be centered since the text width cannot be calculated
and the first part of each event text is important.
ergo: text needs to be left aligned.
But then the probability is high that a lot of text is at the left
bottom
ergo: put the legend to the right.
+----------+
| Name O |
| Name O |
+----------+
"""
if self._legend_should_be_drawn(view_properties, categories):
num_categories = len(categories)
if num_categories == 0:
return
font_size = SMALL_FONT_SIZE
myStyle = StyleBuilder()
myStyle.setFontFamily(fontfamily="Verdana")
myStyle.setFontSize(font_size)
myStyle.setTextAnchor('left')
# reserve 15% for the legend
width = int(self.metrics['widthpx'] * 0.15)
item_height = font_size + OUTER_PADDING
height = num_categories *(item_height+INNER_PADDING)+2*OUTER_PADDING
# Draw big box
builder = ShapeBuilder()
x = self.metrics['widthpx'] - width - OUTER_PADDING
svgGroup = g()
box_rect = builder.createRect(x,
self.metrics['heightpx'] - height - OUTER_PADDING,
width, height,fill='white')
svgGroup.addElement(box_rect)
# Draw text and color boxes
cur_y = self.metrics['heightpx'] - height - OUTER_PADDING + INNER_PADDING
for cat in categories:
base_color = self._map_svg_color(cat.color)
border_color = self._map_svg_color(darken_color(cat.color))
color_box_rect = builder.createRect(x + OUTER_PADDING,
cur_y, item_height, item_height, fill=base_color,
stroke=border_color)
svgGroup.addElement(color_box_rect)
myText = self._svg_clipped_text(cat.name,
(x + OUTER_PADDING + INNER_PADDING+item_height,
cur_y, width-OUTER_PADDING-INNER_PADDING-item_height,
item_height ),
myStyle)
svgGroup.addElement(myText)
cur_y = cur_y + item_height + INNER_PADDING
self.svg.addElement(svgGroup)
def _draw_events(self, view_properties):
"""Draw all event boxes and the text inside them."""
myStyle = StyleBuilder()
myStyle.setFontFamily(fontfamily="Verdana")
myStyle.setFontSize("%d" % SMALL_FONT_SIZE)
myStyle.setTextAnchor('left')
oh = ShapeBuilder()
for (event, rect) in self.scene.event_data:
# Ensure that we can't draw outside rectangle
# have one group per event
svgGroup = g()
# Ensure that we can't draw content outside inner rectangle
boxColor = self._get_box_color(event)
boxBorderColor = self._get_box_border_color(event)
svgRect = oh.createRect(rect.X, rect.Y,
rect.GetWidth(), rect.GetHeight(),
stroke=boxBorderColor,
fill=boxColor )
if self.shadowFlag:
svgRect.set_filter("url(#filterShadow)")
svgGroup.addElement(svgRect)
if rect.Width > 0:
# Draw the text (if there is room for it)
svgGroup.addElement(self._svg_clipped_text(event.text, rect.Get(), myStyle))
# Draw data contents indicator
if event.has_data():
svgGroup.addElement(self._draw_contents_indicator(event, rect))
self.svg.addElement(svgGroup)
def _draw_contents_indicator(self, event, rect):
"""
The data contents indicator is a small triangle drawn in the upper
right corner of the event rectangle.
"""
corner_x = rect.X + rect.Width
polyPoints = "%d,%d %d,%d %d,%d" % \
(corner_x - DATA_INDICATOR_SIZE, rect.Y,
corner_x, rect.Y,
corner_x, rect.Y + DATA_INDICATOR_SIZE)
polyColor = self._get_box_indicator_color(event)
oh = ShapeBuilder()
indicator = oh.createPolygon(polyPoints,fill=polyColor,stroke=polyColor)
# TODO (low): Transparency ?
return indicator
def _svg_clipped_text(self, myString, rectTuple, myStyle):
myString = self._encode_unicode_text(myString)
# Put text,clipping into a SVG group
group=g()
rx, ry, width, height = rectTuple
text_x = rx + INNER_PADDING
text_y = ry + height - INNER_PADDING
# TODO: in SVG, negative value should be OK, but they
# are not drawn in Firefox. So add a special handling here
# however the root cause is the layout function for the recs
# which should be fixed not to have values < 0
if text_x < INNER_PADDING:
width = width - (INNER_PADDING-text_x)
text_x = INNER_PADDING
pathId = "path%d_%d" % (text_x, text_y)
p = path(pathData= "M %d %d H %d V %d H %d" % \
(rx, ry + height,
text_x+width-INNER_PADDING,
ry, rx))
clip = clipPath()
clip.addElement(p)
clip.set_id(pathId)
d=defs()
d.addElement(clip)
self.svg.addElement(d)
myText = text( myString, text_x, text_y )
myText.set_style(myStyle.getStyle())
myText.set_textLength(width-2*INNER_PADDING)
myText.set_lengthAdjust("spacingAndGlyphs")
group.set_clip_path("url(#%s)" % pathId)
group.addElement(myText)
return group
def _text(self, the_text, x, y):
encoded_text = self._encode_unicode_text(the_text)
return text(encoded_text, x, y)
def _encode_unicode_text(self, text):
if type(text) is UnicodeType:
return text.encode('ISO-8859-1')
else:
return text
|