commit-gnue
[Top][All Lists]
Advanced

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

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


From: jamest
Subject: r6032 - trunk/gnue-common/src/printing/barcodes
Date: Thu, 22 Jul 2004 21:31:44 -0500 (CDT)

Author: jamest
Date: 2004-07-22 21:31:43 -0500 (Thu, 22 Jul 2004)
New Revision: 6032

Modified:
   trunk/gnue-common/src/printing/barcodes/Base.py
   trunk/gnue-common/src/printing/barcodes/README
Log:
convert barcode to use reportlab renderers


Modified: trunk/gnue-common/src/printing/barcodes/Base.py
===================================================================
--- trunk/gnue-common/src/printing/barcodes/Base.py     2004-07-22 16:03:33 UTC 
(rev 6031)
+++ trunk/gnue-common/src/printing/barcodes/Base.py     2004-07-23 02:31:43 UTC 
(rev 6032)
@@ -27,6 +27,8 @@
 #
 import sys, os, string
 import Image, ImageDraw
+from reportlab.lib import colors 
+from reportlab.graphics.shapes import * 
 
 class InvalidBarcode(StandardError):
   pass
@@ -118,69 +120,51 @@
 
     lineHeight = self.calculateLineHeight(width)
 
+    
     ##
-    ## Vector-based PS/EPS output
+    ## Reportlab (PDF)
     ##
-    if format in ('eps'):
+    d = Drawing(width+1,lineHeight+(includeText and ( textSize+2 ) or 1 ))
+    
+    if includeText:
+      y = textSize + 1
+    else:
+      y = 0
 
-      # Write minimal EPS header
-      stream.write('%!PS-Adobe-3.0 EPSF-3.0\n')
-      stream.write('%%%%BoundingBox: 0 0 %s %s\n' % (width+1,
-         lineHeight+(includeText and ( textSize+2 ) or 1 )))
-      stream.write(psHeader)
+    strokes = {}
 
-      # Initialize the graphics
-      stream.write('gsave 0 setgray\n')
+    # Draw each bar
+    x = 0
+    for ch in code:
+      stroke, xmul, ymul = self.encodingMap[ch]
+      dx = lineWidth*xmul
+      dy = lineHeight*ymul
+      if stroke:
+        # So we won't cut off half the first bar...
+        if not x:
+          x = dx/2
 
-      if includeText:
-        y = textSize + 1
+        d.add(Rect(x+dx/2, y,dx,dy, fillColor=colors.black,strokeWidth=0)) 
+
+        x += dx
       else:
-        y = 0
+        x += spaceWidth*xmul
 
-      strokes = {}
 
-      # Draw each bar
-      x = 0
-      for ch in code:
-        stroke, xmul, ymul = self.encodingMap[ch]
-        dx = lineWidth*xmul
-        dy = lineHeight*ymul
-        if stroke:
+    # 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)
 
-          # So we won't cut off half the first bar...
-          if not x:
-            x = dx/2
-
-          # Store the strokes by their width, so we can
-          # more efficiently draw all the same-width lines
-          # at the same time.
-          try:
-            strks = strokes[dx]
-          except KeyError:
-            strks = strokes[dx] = []
-
-          # Draw the line
-          strks += ['%s %s Mt %s %s Lt' % (
-              x+dx/2, y, x+dx/2, y+dy)]
-          x += dx
-        else:
-          x += spaceWidth*xmul
-
-      # For each stroke, draw:
-      for stroke, set in strokes.items():
-        stream.write('%s setlinewidth newpath\n' % stroke)
-        stream.write(string.join(set,'\n'))
-        stream.write('\nstroke\n')
-
-      # Draw the text
-      if includeText:
-        stream.write('/Courier findfont %s scalefont setfont\n' % textSize)
-        stream.write('%s 0 moveto ' % (x/2))
-        stream.write('(%s) dup stringwidth pop 2 div neg 0 rmoveto show\n' % 
value)
-
-      stream.write("grestore\n%%Trailer\n%%EOF\n")
-
-
     ##
     ## Raster-based output using PIL
     ##
@@ -208,105 +192,9 @@
 #       image.save(stream, format)
 
     if closeFile:
-      close(stream)
+      stream.close()
 
   # Line height is .15 * barcode width, but at least .25"
   # This is used by Code39, Interleaved 2 of 5, etc
   def _calculate15(self, width):
     return max(18, .15 * width)
-
-
-
-##
-## This fun stuff emulates setstrokeadjust if not present.
-## This mode forces all lines of the same width to actually
-## print at the same width, at the expense of whitespace.
-## If printing at 600dpi or more, this likely won't make a different.
-## It could at 300dpi or less, though.
-##
-psHeader = """\
-%%EndComments
-%%BeginProlog
-% automatic stroke adjustment already available?
-/Level2|DPS /setstrokeadjust where {pop true} {false} ifelse def
-Level2|DPS not { save } if % remove procset if not needed
-Level2|DPS % def interface for Level 2
-{
-%%BeginResource: procset Adobe_SpecialGraphics_L2DPS 1.0 0
-/GRinit {} def
-/Mt /moveto load def
-/Lt /lineto load def
-/Ct /curveto load def
-/Rt /rectstroke load def
-/SA /setstrokeadjust load def
-%%EndResource
-} if
-Level2|DPS not { restore } if
-Level2|DPS { save }if % remove procset from VM if not
-% needed
-Level2|DPS not { % emulations are needed
-%%BeginResource: procset Adobe_SpecialGraphics_L1 1.0 0
-/GRdict 5 dict def
-GRdict begin
-/Mt /moveto load def
-/Lt /lineto load def
-/Ct /curveto load def
-/Rt { % x y w h Rt - basic rectangle
-4 -2 roll moveto % bottom left hand corner
-dup 0 exch rlineto % to upper left
-exch 0 rlineto % to upper right
-neg 0 exch rlineto % to lower right
-closepath
-} bind def
-/SA { % true,false SA - enable/disable stroke adjustment
-{ mark countdictstack 2 sub
-{currentdict GRdict eq {SAdict begin exit}
-{currentdict end} ifelse} repeat
-counttomark {begin} repeat pop}
-{ mark countdictstack 2 sub
-{currentdict SAdict eq {end exit} {currentdict end} ifelse
-}
-repeat counttomark {begin} repeat pop
-} ifelse
-} bind def
-end % GRdict
-/GRinit { % put GRdict on stack under userdict
-countdictstack array dictstack cleardictstack
-dup 0 GRdict put { begin} forall
-} bind def
-/SAdict 10 dict def
-SAdict begin
-% snap user space location to device space pixel
-/snaptopixel { % x y snaptopixel sx sy
-transform
-.25 sub round .25 add exch
-.25 sub round .25 add exch
-itransform
-} bind def
-/Mt { % x y Mt - moveto
-snaptopixel moveto
-} bind def
-/Lt { % x y Lt - lineto
-snaptopixel lineto
-} bind def
-/Ct { % x1 y1 x2 y2 x3 y3 Ct - curveto
-snaptopixel curveto
-} bind def % note control points are not snapped
-/Rt { % x y w h Rt - basic rectangle
-4 -2 roll Mt % bottom left hand corner
-dtransform round exch round exch idtransform
-dup 0 exch rlineto % to upper left
-exch 0 rlineto % to upper right
-neg 0 exch rlineto % to lower right
-closepath
-} bind def
-end % SAdict
-%%EndResource
-} if
-Level2|DPS { restore }if
-%%EndProlog
-%%BeginSetup
-GRinit
-true SA
-%%EndSetup
-"""

Modified: trunk/gnue-common/src/printing/barcodes/README
===================================================================
--- trunk/gnue-common/src/printing/barcodes/README      2004-07-22 16:03:33 UTC 
(rev 6031)
+++ trunk/gnue-common/src/printing/barcodes/README      2004-07-23 02:31:43 UTC 
(rev 6032)
@@ -7,7 +7,7 @@
   # File Object:
   myfile = open('foo.eps','w')
   Code39().generate('123443',myfile,'eps')
-  myfile.close
+  myfile.close()
 
   # or string...
   Code39().generate('123456','foo2.eps','eps')





reply via email to

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