-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsvglabel.py
99 lines (79 loc) · 3.5 KB
/
svglabel.py
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
# Copyright (C) 2006, 2007, 2008 One Laptop Per Child
#
# 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.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import gtk, pygtk
import rsvg
import cairo
import re
class SvgLabel(gtk.DrawingArea):
filename = ''
fill_color = ''
stroke_color = ''
background_color = ''
def __init__(self, filename, fill_color, stroke_color, pixbuf = False, background_color = '', request_x = 45, request_y = 45):
gtk.DrawingArea.__init__(self)
self.set_size_request(request_x, request_y)
self.filename = filename
self.background_color = background_color
self.fill_color = fill_color
self.stroke_color = stroke_color
self.modify_bg(gtk.STATE_NORMAL, gtk.gdk.color_parse(background_color))
if pixbuf:
self.pixbuf = pixbuf
else:
self.pixbuf = self._read_icon_data(self.filename, self.fill_color, self.stroke_color)
self.connect('expose-event', self._expose_cb)
def _expose_cb(self, widget, event):
widget.window.draw_pixbuf(None, self.pixbuf, 0, 0, 0, 0)
return False
def _read_icon_data(self, filename, fill_color, stroke_color):
icon_file = open(filename, 'r')
data = icon_file.read()
icon_file.close()
if fill_color:
entity = '<!ENTITY fill_color "%s">' % fill_color
data = re.sub('<!ENTITY fill_color .*>', entity, data)
if stroke_color:
entity = '<!ENTITY stroke_color "%s">' % stroke_color
data = re.sub('<!ENTITY stroke_color .*>', entity, data)
self.data_size = len(data)
return rsvg.Handle(data=data).get_pixbuf()
def set_color(self, fill_color, stroke_color):
self.fill_color = fill_color
self.stroke_color = stroke_color
self.pixmap = self._read_icon_data(self.filename, self.fill_color, self.stroke_color)
self.queue_draw()
def set_fill_color(self, fill_color):
self.fill_color = fill_color
self.pixmap = self._read_icon_data(self.filename, self.fill_color, self.stroke_color)
self.queue_draw()
def get_fill_color(self):
return self.fill_color
def set_stroke_color(self, stroke_color):
self.stroke_color = stroke_color
self.pixmap = self._read_icon_data(self.filename, self.fill_color, self.stroke_color)
self.queue_draw()
def get_stroke_color(self):
return self.stroke_color
def get_pixbuf(self):
return self.pixbuf
def set_pixbuf(self, pixbuf):
self.pixbuf = pixbuf
self.queue_draw()
def set_background(self, background_color):
self.background_color = background_color
self.modify_bg(gtk.STATE_NORMAL, gtk.gdk.color_parse(self.background_color))
self.queue_draw()