Drawings can now do asString(format)
authorandy_robinson
Tue, 18 Mar 2003 00:01:09 +0000
changeset 1862 11f6cd4b0f07
parent 1861 033ed93da811
child 1863 086d685ad0cb
Drawings can now do asString(format)
reportlab/graphics/samples/runall.py
reportlab/graphics/shapes.py
--- a/reportlab/graphics/samples/runall.py	Mon Mar 17 16:59:26 2003 +0000
+++ b/reportlab/graphics/samples/runall.py	Tue Mar 18 00:01:09 2003 +0000
@@ -1,30 +1,38 @@
 # runs all the GUIedit charts in this directory - 
 # makes a PDF sample for eaxh existing chart type
+import sys
+import glob
+import string
+import inspect
+import types
+
+def moduleClasses(mod):
+    def P(obj, m=mod.__name__, CT=types.ClassType):
+        return (type(obj)==CT and obj.__module__==m)
+    try:
+        return inspect.getmembers(mod, P)[0][1]
+    except:
+        return None
+
+def getclass(f):
+    return moduleClasses(__import__(f))
+
+def run(format):
+    allfiles = glob.glob('*.py')
+    allfiles.sort()
+    for fn in allfiles:
+        f = string.split(fn, '.')[0]
+        c = getclass(f)
+        if c != None:
+            print c.__name__
+            try:
+                c().save(formats=[format],outDir='.',fnRoot=c.__name__)
+            except:
+                print " COULDN'T CREATE '%s.%s'!" % (c.__name__, format)
 
 if __name__ == "__main__":
-    def run():
-        import glob, string
-        def moduleClasses(mod):
-            import inspect, types
-            def P(obj, m=mod.__name__, CT=types.ClassType):
-                return (type(obj)==CT and obj.__module__==m)
-            try:
-                return inspect.getmembers(mod, P)[0][1]
-            except:
-                return None
-        def getclass(f):
-            return moduleClasses(__import__(f))
-
-        allfiles = glob.glob('*.py')
-        allfiles.sort()
-        for fn in allfiles:
-            f = string.split(fn, '.')[0]
-            c = getclass(f)
-            if c != None:
-                print c.__name__
-                try:
-                    c().save(formats=['pdf'],outDir='.',fnRoot=c.__name__)
-                except:
-                    print " COULDN'T CREATE '%s.pdf'!" % c.__name__
-
-    run()
\ No newline at end of file
+    if len(sys.argv) < 2:
+        print 'usage: runall.py FORMAT'
+        print '  (where format one of pdf,gif,eps,png etc.)'
+    else:
+        run(sys.argv[1])
\ No newline at end of file
--- a/reportlab/graphics/shapes.py	Mon Mar 17 16:59:26 2003 +0000
+++ b/reportlab/graphics/shapes.py	Tue Mar 18 00:01:09 2003 +0000
@@ -1,11 +1,11 @@
 #copyright ReportLab Inc. 2001
 #see license.txt for license details
 #history http://cvs.sourceforge.net/cgi-bin/cvsweb.cgi/reportlab/graphics/shapes.py?cvsroot=reportlab
-#$Header: /tmp/reportlab/reportlab/graphics/shapes.py,v 1.86 2003/02/24 13:26:07 rgbecker Exp $
+#$Header: /tmp/reportlab/reportlab/graphics/shapes.py,v 1.87 2003/03/18 00:01:03 andy_robinson Exp $
 """
 core of the graphics library - defines Drawing and Shapes
 """
-__version__=''' $Id: shapes.py,v 1.86 2003/02/24 13:26:07 rgbecker Exp $ '''
+__version__=''' $Id: shapes.py,v 1.87 2003/03/18 00:01:03 andy_robinson Exp $ '''
 
 import string, os, sys
 from math import pi, cos, sin, tan
@@ -548,8 +548,10 @@
         return self._copy(apply(Group,args,kw))
 
     def save(self, formats=None, verbose=None, fnRoot=None, outDir=None, title=''):
+        """Saves copies of self in desired location and formats.
+
+        Multiple formats can be supported in one call"""
         from reportlab import rl_config
-        "Saves copies of self in desired location and formats"
         ext = ''
         if not fnRoot:
             fnRoot = getattr(self,'fileNamePattern',(self.__class__.__name__+'%03d'))
@@ -642,6 +644,30 @@
             self.saveLogger(fnroot,ext)
         return ext and fnroot+ext[1:] or ''
 
+
+    def asString(self, format, verbose=None):
+        """Converts to an 8 bit string in given format."""
+        assert format in ['pdf','ps','eps','gif','png','jpg','jpeg','tiff','tif','py'], 'Unknown file format "%s"' % format
+        from reportlab import rl_config
+        #verbose = verbose is not None and (verbose,) or (getattr(self,'verbose',verbose),)[0]
+        if format == 'pdf':
+            from reportlab.graphics import renderPDF
+            return renderPDF.drawToString(self)
+        elif format in ['gif','png','tif','jpg']:
+            from reportlab.graphics import renderPM
+            return renderPM.drawToString(self, fmt=format)
+        elif format == 'eps':
+            from rlextra.graphics import renderPS_SEP
+            return renderPS_SEP.drawToString(self,
+                                preview = getattr(self,'preview',1),
+                                showBoundary=getattr(self,'showBorder',rl_config.showBoundary))
+        elif format == 'ps':
+            from reportlab.graphics import renderPS
+            return renderPS.drawToString(self, showBoundary=getattr(self,'showBorder',rl_config.showBoundary))
+        elif format == 'py':
+            return self._renderPy()
+
+
 class _DrawingEditorMixin:
     '''This is a mixin to provide functionality for edited drawings'''
     def _add(self,obj,value,name=None,validate=None,desc=None,pos=None):