root/trunk/RBRapier/RBRapier/Formats/SVG/SVGSkin/SVGItems/Styled.py

Revision 657, 4.2 kB (checked in by sholloway, 5 years ago)

*** empty log message ***

Line 
1 #!/usr/bin/env python
2 ##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3 ##~ License
4 ##~
5 ##- The RuneBlade Foundation library is intended to ease some
6 ##- aspects of writing intricate Jabber, XML, and User Interface (wxPython, etc.)
7 ##- applications, while providing the flexibility to modularly change the
8 ##- architecture. Enjoy.
9 ##~
10 ##~ Copyright (C) 2002  TechGame Networks, LLC.
11 ##~
12 ##~ This library is free software; you can redistribute it and/or
13 ##~ modify it under the terms of the BSD style License as found in the
14 ##~ LICENSE file included with this distribution.
15 ##~
16 ##~ TechGame Networks, LLC can be reached at:
17 ##~ 3578 E. Hartsel Drive #211
18 ##~ Colorado Springs, Colorado, USA, 80920
19 ##~
20 ##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
21
22 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
23 #~ Imports
24 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
25
26 import warnings
27 from RBFoundation.XMLUtilityFunctions import ColorStringToRGB
28 from Common import TransformableSVGItem
29
30 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
31 #~ Definitions
32 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
33
34 class StyledSVGItem(TransformableSVGItem):
35     def InterpretSettings(self, settings):
36         TransformableSVGItem.InterpretSettings(self, settings)
37
38         stylelist = self._asStyleList(settings.get('style', ''))
39         explicit_styles = [(n, settings[n].strip()) for n in self.explicit_styles_mapping if n in settings]
40         styledict = self._asStyleDict(stylelist, explicit_styles)
41         self.object.SetStyle(styledict)
42
43     #~ helpers ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
44
45     def _asString(self, value):
46         return str(value)
47
48     def _asColor(self, color):
49         """
50         Valid color strings:
51             color="none"
52             color="black" # constant color lookup
53             color="#ff0088"
54             color="#f08"
55             color="rgb(255, 0, 136)"
56             color="rgb(1.0, 0.0, 0.53125)"
57         """
58         if isinstance(color, (type(None), int, long, tuple)):
59             result = color
60         elif color == 'currentColor':
61             result = 'currentColor'
62         elif color.startswith('url'):
63             warnings.warn('TODO: implement color url')
64             result = None
65         elif not color:
66             result = None
67         else:
68             result = ColorStringToRGB(color)
69         return result
70
71     def _asOpacity(self, opacity):
72         result = float(opacity)
73         if not (0. <= result <= 1.):
74             raise ValueError, "Opacity value must be in the range of [0.0, 1.0], but was %r (%r)" % (opacity, result)
75         return result
76
77     def _asDashArray(self, dasharray):
78         if not isinstance(dasharray, (list, tuple)):
79             dasharray = dasharray.split(',') or dasharray.split()
80             dasharray = [x.strip() for x in dasharray]
81         else:
82             dasharray = list(dasharray)
83         if len(dasharray) & 1:
84             # dasharray is odd -- repeat to make it even, according to SVG book
85             dasharray *= 2
86         return dasharray
87
88     def _asStyleList(self, style):
89         result = []
90         for entry in filter(None, style.split(';')):
91             name, value = entry.split(':', 1)
92             result.append((name.strip(), value.strip()))
93         return result
94
95     def _asStyleDict(self, *stylesIn):
96         styledict = {}
97         map(styledict.update, map(dict, stylesIn))
98         mapping = self.explicit_styles_mapping
99         for name, value in styledict.iteritems():
100             mappingFn = mapping.get(name, None)
101             if mappingFn is not None:
102                 styledict[name] = mappingFn(self, value)
103         return styledict
104
105     #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
106     #~ Conversion Mappings
107     #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
108
109     explicit_styles_mapping = {
110         "color": _asColor,
111
112         "stroke": _asColor,
113         "stroke-opacity": _asOpacity,
114         #"stroke-width": TransformableSVGItem._asCoord,
115         #"stroke-dasharray": _asDashArray,
116         #"stroke-linecap": _asString,
117         #"stroke-linejoin": _asString,
118         #"stroke-meterlimit": _asString,
119
120         "fill": _asColor,
121         "fill-opacity": _asOpacity,
122         "fill-rule": _asString,
123         }
124
Note: See TracBrowser for help on using the browser.