reportlab: cropMarks more robust
authorrgbecker
Fri, 13 Feb 2009 12:39:57 +0000
changeset 3110 c5b325f692ab
parent 3109 e90108bdf932
child 3111 86a3158c50bd
reportlab: cropMarks more robust
src/reportlab/pdfgen/canvas.py
src/reportlab/platypus/doctemplate.py
--- a/src/reportlab/pdfgen/canvas.py	Thu Feb 12 18:04:27 2009 +0000
+++ b/src/reportlab/pdfgen/canvas.py	Fri Feb 13 12:39:57 2009 +0000
@@ -130,7 +130,8 @@
                  pageCompression=None,
                  invariant = None,
                  verbosity=0,
-                 encrypt=None):
+                 encrypt=None,
+                 cropMarks=None):
         """Create a canvas of a given size. etc.
 
         You may pass a file-like object to filename as an alternative to
@@ -138,7 +139,10 @@
         For more information about the encrypt parameter refer to the setEncrypt method.
         
         Most of the attributes are private - we will use set/get methods
-        as the preferred interface.  Default page size is A4."""
+        as the preferred interface.  Default page size is A4.
+        cropMarks may be True/False or an object with parameters borderWidth, markColor, markWidth
+        and markLength
+        """
         if pagesize is None: pagesize = rl_config.defaultPageSize
         if invariant is None: invariant = rl_config.invariant
         self._filename = filename
@@ -152,7 +156,7 @@
 
         #this is called each time a page is output if non-null
         self._onPage = None
-        self._cropMarks = None
+        self._cropMarks = cropMarks
 
         self._pagesize = pagesize
         self._pageRotation = 0
@@ -407,41 +411,42 @@
         cM = self._cropMarks
         code = self._code
         if cM:
-            mv = min(pageWidth,pageHeight)
+            mv = max(1,min(pageWidth,pageHeight))
             sf = min(1+1./mv,1.01)
-            bv = (sf-1)*mv*0.5
-            ml = getattr(cM,'markerLength',18)/sf
-            mw = getattr(cM,'markerWidth',0.5)
-            mc = getattr(cM,'markerColor',black)
-            bw = getattr(cM,'borderWidth',18)/sf
-            mlbw = ml+bw
-            cx0 = len(code)
-            self.saveState()
-            self.scale(sf,sf)
-            self.translate(mlbw,mlbw)
-            opw = pageWidth*sf
-            oph = pageHeight*sf
-            pageWidth = 2*mlbw + pageWidth*sf
-            pageHeight = 2*mlbw + pageHeight*sf
-            if ml and mc:
+            bw = max(0,getattr(cM,'borderWidth',36)/sf)
+            if bw:
+                bv = (sf-1)*mv*0.5
+                ml = min(bw,max(0,getattr(cM,'markLength',18)/sf))
+                mw = getattr(cM,'markWidth',0.5)
+                mc = getattr(cM,'markColor',black)
+                mg = bw-ml
+                cx0 = len(code)
                 self.saveState()
-                self.setStrokeColor(mc)
-                self.setLineWidth(mw)
-                self.lines([
-                    (bv,0-mlbw,bv,ml-mlbw),
-                    (opw-2*bv,0-mlbw,opw-2*bv,ml-mlbw),
-                    (bv,oph+bw,bv,oph+mlbw),
-                    (opw-2*bv,oph+bw,opw-2*bv,oph+mlbw),
-                    (-mlbw,bv,ml-mlbw,bv),
-                    (opw+bw,bv,opw+mlbw,bv),
-                    (-mlbw,oph-2*bv,ml-mlbw,oph-2*bv),
-                    (opw+bw,oph-2*bv,opw+mlbw,oph-2*bv),
-                    ])
+                self.scale(sf,sf)
+                self.translate(bw,bw)
+                opw = pageWidth*sf
+                oph = pageHeight*sf
+                pageWidth = 2*bw + pageWidth*sf
+                pageHeight = 2*bw + pageHeight*sf
+                if ml and mc:
+                    self.saveState()
+                    self.setStrokeColor(mc)
+                    self.setLineWidth(mw)
+                    self.lines([
+                        (bv,0-bw,bv,ml-bw),
+                        (opw-2*bv,0-bw,opw-2*bv,ml-bw),
+                        (bv,oph+mg,bv,oph+bw),
+                        (opw-2*bv,oph+mg,opw-2*bv,oph+bw),
+                        (-bw,bv,ml-bw,bv),
+                        (opw+mg,bv,opw+bw,bv),
+                        (-bw,oph-2*bv,ml-bw,oph-2*bv),
+                        (opw+mg,oph-2*bv,opw+bw,oph-2*bv),
+                        ])
+                    self.restoreState()
+                C = code[cx0:]
+                del code[cx0:]
+                code[0:0] = C
                 self.restoreState()
-            C = code[cx0:]
-            del code[cx0:]
-            code[0:0] = C
-            self.restoreState()
 
         code.append(' ')
         page = pdfdoc.PDFPage()
--- a/src/reportlab/platypus/doctemplate.py	Thu Feb 12 18:04:27 2009 +0000
+++ b/src/reportlab/platypus/doctemplate.py	Fri Feb 13 12:39:57 2009 +0000
@@ -345,7 +345,9 @@
                     '_pageBreakQuick':1,
                     'rotation':0,
                     '_debug':0,
-                    'encrypt': None}
+                    'encrypt': None,
+                    'cropMarks': None,
+                    }
     _invalidInitArgs = ()
     _firstPageTemplateIndex = 0
 
@@ -724,6 +726,7 @@
  
         getattr(self.canv,'setEncrypt',lambda x: None)(self.encrypt)
 
+        self.canv._cropMarks = self.cropMarks
         self.canv.setAuthor(self.author)
         self.canv.setTitle(self.title)
         self.canv.setSubject(self.subject)