commit-gnue
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

r6033 - trunk/gnue-common/src/printing/barcodes


From: jamest
Subject: r6033 - trunk/gnue-common/src/printing/barcodes
Date: Thu, 22 Jul 2004 22:07:07 -0500 (CDT)

Author: jamest
Date: 2004-07-22 22:07:07 -0500 (Thu, 22 Jul 2004)
New Revision: 6033

Modified:
   trunk/gnue-common/src/printing/barcodes/Base.py
   trunk/gnue-common/src/printing/barcodes/README
Log:
added support for returning reportlab Drawing objects from generate()


Modified: trunk/gnue-common/src/printing/barcodes/Base.py
===================================================================
--- trunk/gnue-common/src/printing/barcodes/Base.py     2004-07-23 02:31:43 UTC 
(rev 6032)
+++ trunk/gnue-common/src/printing/barcodes/Base.py     2004-07-23 03:07:07 UTC 
(rev 6033)
@@ -91,16 +91,114 @@
   ###
   ###
   ###
-  def generate(self, value, stream=sys.stdout,
+  def generate(self, value, stream=None,
                format='eps', includeText=None, textSize=7, dpi=300):
+    """
+    Generates the requested bar code either via a stream or as the 
+    requested object type.
+  
+    @param value:   The string to convert to a barcode
+    @param stream:  Optional argument of file name as a string, or any
+                    open file style object.
+    @param format: The format in which the output should be generated
+    
+                  Valid file formats include pdf, eps, svg and 
+                  will require the stream argument be provided.
+    
+                  Valid object formats include
+                    rldrawing: ReportLab Drawing object will be returned
+                                No stream argument is required.
+    @param includeText: Boolean.  If true then human readable text will
+                        be printed centered under the barcode.
+  
+    @param textSize: The point size of the human readable text.
+    @param dpi: The dots per inch at which the bitmap should be generated.  
+  
+    @return: None or a format dependent object.  Valid return values
+            eps : None
+            pdf : None
+            svg : None
+            rl  : ReportLab Drawing           
+    @rtype: misc
+    """
 
-    # If they passed a text name
+    assert (format in ('rl') or stream is not None)
+
+    d = self._generateDrawing(value, includeText, textSize, dpi)
+    
+    #
+    # Process formats that return value instead of write to a file
+    #      
+    if format == 'rl':
+      return d
+    
+    #
+    # A stream is required for the remaining formats
+    #
     if not hasattr(stream, 'write'):
       closeFile = True
       stream = open(stream,'w')
     else:
       closeFile = False
+                  
+    if format == 'pdf':
+      from reportlab.graphics import renderPDF 
+      renderPDF.drawToFile(d, stream, 'GNUe')      
+    elif format == 'eps':   
+      from reportlab.graphics import renderPS 
+      renderPS.drawToFile(d, stream)
+    elif format == 'svg':
+      from reportlab.graphics import renderSVG
+      renderSVG.drawToFile(d, stream)
 
+#
+# This code *should* be replaced with calls to renderPM
+# but that appears broken in the .debs 
+#
+    ##
+    ## Raster-based output using PIL  
+    ##
+#     elif format in ('png','tiff','ppm','xbm'):
+#       lineWidth = int(lineWidth * dpi/72+.5)   # 300dpi
+#       lineHeight = int(lineHeight * dpi/72+.5)  # 300dpi
+#       # Special case for PostNet
+#       lineHeight2 = int(lineHeight * .45+.5)
+#
+#       # Create a new monochrome image with a white backgint
+#       image = Image.new('1',(int(len(code)*lineWidth+.5),
+#          int(lineHeight+.5)), 1)
+#       draw = ImageDraw.Draw(image)
+#       offs = 0
+#       for ch in code:
+#         if ch == '1':
+#           draw.rectangle((offs,0,offs+lineWidth-1,lineHeight),
+#                           outline=0, fill=0)
+#         # Special case for PostNet
+#         elif ch == '2':
+#           draw.rectangle((offs,0,offs+lineWidth-1,lineHeight2),
+#                           outline=0, fill=0)
+#         offs += lineWidth
+#
+#       image.save(stream, format)
+
+    if closeFile:
+      stream.close()
+      
+  def _generateDrawing(self, value, includeText=None, textSize=7, dpi=300):
+    """
+    Generates a ReportLab Drawing object used by the renderers in generate() 
+  
+    @param value:   The string to convert to a barcode
+    @param includeText: Boolean.  If true then human readable text will
+                        be printed centered under the barcode.
+  
+    @param textSize: The point size of the human readable text.
+    @param dpi: The dots per inch at which the bitmap should be generated.  
+  
+    @return: ReportLab Drawing           
+    @rtype: misc
+    """
+    
     if includeText is None:
       includeText = self.defaultIncludeText
     code = self._buildBinary(value)
@@ -119,11 +217,7 @@
         width += spaceWidth*xmul
 
     lineHeight = self.calculateLineHeight(width)
-
     
-    ##
-    ## Reportlab (PDF)
-    ##
     d = Drawing(width+1,lineHeight+(includeText and ( textSize+2 ) or 1 ))
     
     if includeText:
@@ -131,8 +225,6 @@
     else:
       y = 0
 
-    strokes = {}
-
     # Draw each bar
     x = 0
     for ch in code:
@@ -142,58 +234,20 @@
       if stroke:
         # So we won't cut off half the first bar...
         if not x:
-          x = dx/2
+          x = dx/2.0
 
-        d.add(Rect(x+dx/2, y,dx,dy, fillColor=colors.black,strokeWidth=0)) 
+        d.add(Rect(x+dx/2.0, y,dx,dy, fillColor=colors.black,strokeWidth=0)) 
 
         x += dx
       else:
         x += spaceWidth*xmul
 
-
     # Draw the text
     if includeText:
-      d.add(String(x/2, textSize/2, value, fontSize=textSize, 
fontName="Courier",fillColor=colors.black,textAnchor="middle")) 
-      
-    if format == 'pdf':
-      from reportlab.graphics import renderPDF 
-      renderPDF.drawToFile(d, stream, 'GNUe')      
-    elif format == 'eps':   
-      from reportlab.graphics import renderPS 
-      renderPS.drawToFile(d, stream)
-    elif format == 'svg':
-      from reportlab.graphics import renderSVG
-      renderSVG.drawToFile(d, stream)
+      d.add(String(x/2.0, textSize/2.0, value, fontSize=textSize, 
fontName="Courier",fillColor=colors.black,textAnchor="middle")) 
 
-    ##
-    ## Raster-based output using PIL
-    ##
-#     elif format in ('png','tiff','ppm','xbm'):
-#       lineWidth = int(lineWidth * dpi/72+.5)   # 300dpi
-#       lineHeight = int(lineHeight * dpi/72+.5)  # 300dpi
-#       # Special case for PostNet
-#       lineHeight2 = int(lineHeight * .45+.5)
-#
-#       # Create a new monochrome image with a white backgint
-#       image = Image.new('1',(int(len(code)*lineWidth+.5),
-#          int(lineHeight+.5)), 1)
-#       draw = ImageDraw.Draw(image)
-#       offs = 0
-#       for ch in code:
-#         if ch == '1':
-#           draw.rectangle((offs,0,offs+lineWidth-1,lineHeight),
-#                           outline=0, fill=0)
-#         # Special case for PostNet
-#         elif ch == '2':
-#           draw.rectangle((offs,0,offs+lineWidth-1,lineHeight2),
-#                           outline=0, fill=0)
-#         offs += lineWidth
-#
-#       image.save(stream, format)
-
-    if closeFile:
-      stream.close()
-
+    return d        
+    
   # Line height is .15 * barcode width, but at least .25"
   # This is used by Code39, Interleaved 2 of 5, etc
   def _calculate15(self, width):

Modified: trunk/gnue-common/src/printing/barcodes/README
===================================================================
--- trunk/gnue-common/src/printing/barcodes/README      2004-07-23 02:31:43 UTC 
(rev 6032)
+++ trunk/gnue-common/src/printing/barcodes/README      2004-07-23 03:07:07 UTC 
(rev 6033)
@@ -12,6 +12,11 @@
   # or string...
   Code39().generate('123456','foo2.eps','eps')
 
+  # or get a report lab object  
+  object = Code39().generate('GNUE',format='rl')
+  from reportlab.graphics import renderPDF 
+  renderPDF.drawToFile(object, "output.pdf","GNUe Common Barcode Demo")      
+
 It has strong support for "eps". It somewhat supports
 "png", "tiff", and "xbm" via Python Imaging Library. 
 





reply via email to

[Prev in Thread] Current Thread [Next in Thread]