renderSVG add support for font attributes
authorrgbecker
Tue, 11 Dec 2012 13:52:45 +0000
changeset 3631 faec46a34c20
parent 3630 0302fc9eb428
child 3632 401542b8679b
renderSVG add support for font attributes
src/reportlab/graphics/renderSVG.py
src/reportlab/lib/utils.py
src/reportlab/lib/validators.py
--- a/src/reportlab/graphics/renderSVG.py	Thu Dec 06 16:12:38 2012 +0000
+++ b/src/reportlab/graphics/renderSVG.py	Tue Dec 11 13:52:45 2012 +0000
@@ -17,7 +17,7 @@
 from reportlab.graphics.shapes import STATE_DEFAULTS, Path, UserNode
 from reportlab.graphics.shapes import * # (only for test0)
 from reportlab import rl_config
-from reportlab.lib.utils import getStringIO
+from reportlab.lib.utils import getStringIO, RLFontName
 
 from xml.dom import getDOMImplementation
 
@@ -27,9 +27,9 @@
 cos = math.cos
 pi = math.pi
 
-AREA_STYLES = 'stroke-width stroke-linecap stroke fill stroke-dasharray'
-LINE_STYLES = 'stroke-width stroke-linecap stroke stroke-dasharray'
-TEXT_STYLES = 'font-family font-size'
+AREA_STYLES = 'stroke-width stroke-linecap stroke fill stroke-dasharray'.split()
+LINE_STYLES = 'stroke-width stroke-linecap stroke stroke-dasharray'.split()
+TEXT_STYLES = 'font-family font-weight font-style font-variant font-size'.split()
 
 ### top-level user function ###
 def drawToString(d, showBoundary=rl_config.showBoundary):
@@ -234,12 +234,12 @@
 
         return stringWidth(s, font, fontSize)
 
-    def _formatStyle(self, include='', exclude='',**kwds):
+    def _formatStyle(self, include=[], exclude='',**kwds):
         style = self.style.copy()
         style.update(kwds)
         keys = style.keys()
         if include:
-            keys = [k for k in keys if k in include.split()]
+            keys = [k for k in keys if k in include]
         if exclude:
             exclude = exclude.split()
             items = [k+': '+str(style[k]) for k in keys if k not in exclude]
@@ -338,9 +338,18 @@
 
     def setFont(self, font, fontSize):
         if self._font != font or self._fontSize != fontSize:
-            self._font, self._fontSize = (font, fontSize)
-            self.style['font-family'] = font
-            self.style['font-size'] = '%spx' % fontSize
+            self._font = font
+            self._fontSize = fontSize
+            style = self.style
+            for k in TEXT_STYLES:
+                if k in style:
+                    del style[k]
+            if isinstance(font,RLFontName):
+                for k,v in font.svgAttrs.iteritems():
+                    style['font-'+k] = v
+            if 'font-family' not in style:
+                style['font-family'] = font
+            style['font-size'] = '%spx' % fontSize
 
     def _add_link(self, dom_object, link_info) :
         assert isinstance(link_info, dict)
--- a/src/reportlab/lib/utils.py	Thu Dec 06 16:12:38 2012 +0000
+++ b/src/reportlab/lib/utils.py	Tue Dec 11 13:52:45 2012 +0000
@@ -1174,3 +1174,14 @@
         self = str.__new__(cls,value)
         self.__inc = inc
         return self
+
+class RLFontName(str):
+    '''allows specification of the properties of a font using a dictionary of extra attributes
+    eg fontName = RLFontName('proxima-nova-bold',
+                    svgAttrs=dict(family='"proxima-nova"',weight='bold'))
+    '''
+    def __new__(cls,v,**kwds):
+        self = str.__new__(cls,v)
+        for k,v in kwds.iteritems():
+            setattr(self,k,v)
+        return self
--- a/src/reportlab/lib/validators.py	Thu Dec 06 16:12:38 2012 +0000
+++ b/src/reportlab/lib/validators.py	Tue Dec 11 13:52:45 2012 +0000
@@ -64,7 +64,7 @@
 
 class _isString(Validator):
     def test(self,x):
-        return type(x) in (StringType, UnicodeType)
+        return isinstance(x,(str,unicode))
 
 class _isCodec(Validator):
     def test(self,x):