InvoiceText als Blocksatz darstellen
Bernd Wurst

Bernd Wurst commited on 2008-04-15 18:42:01
Zeige 1 geänderte Dateien mit 33 Einfügungen und 2 Löschungen.

... ...
@@ -53,6 +53,34 @@ def _splitToWidth(canvas, text, width, font, size):
53 53
   return lines
54 54
 
55 55
 
56
+def _drawJustifiedString(x, y, text, canvas, width, font, size):
57
+  text = text.strip()
58
+  if canvas.stringWidth(text, font, size) > width:
59
+    canvas.drawString(x, y, text)
60
+    # too long line, I cannot handle this
61
+    return
62
+  if not ' ' in text:
63
+    canvas.drawString(x, y, text)
64
+    # no space in there, nothing to justify
65
+    return
66
+  
67
+  words = [ '%s' % w for w in text.split(' ')]
68
+  words_width = 0.0
69
+  for word in words:
70
+    words_width += canvas.stringWidth(word, font, size)
71
+  
72
+  available_space = width - words_width
73
+  available_each = available_space / float((len(words) - 1))
74
+  from pprint import pprint
75
+  pprint( (width, words_width)) 
76
+ 
77
+  my_x = x
78
+  for idx in range(len(words)):
79
+    word = words[idx]
80
+    canvas.drawString(my_x, y, word)
81
+    my_x += canvas.stringWidth(word, font, size) + available_each
82
+  return
83
+
56 84
 
57 85
 def _PageWrap(canvas):
58 86
   '''Seitenumbruch'''
... ...
@@ -337,9 +365,12 @@ def InvoiceToPDF(iv):
337 365
             y -= line_height + 1
338 366
           y -= line_padding
339 367
           canvas.setFont(font, my_font_size)
340
-        for line in lines:
341
-          canvas.drawString(left, y-font_height, line)
368
+        for line in lines[:-1]:
369
+          _drawJustifiedString(left, y-font_height, line, canvas, right-left, font, my_font_size)
370
+          y -= line_height
371
+        canvas.drawString(left, y-font_height, lines[-1])
342 372
         y -= line_height
373
+        
343 374
         y -= line_padding*3
344 375
       left, right = leftcontent, rightcontent
345 376
     else:
346 377