Skip to content

Commit a62834d

Browse files
author
Alexander Roth
committed
Added multiline functionality for the used defined text.
1 parent c75556e commit a62834d

1 file changed

Lines changed: 24 additions & 16 deletions

File tree

barcode/writer.py

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ def __init__(self, initialize=None, paint_module=None, paint_text=None,
9494
self.text = ''
9595
self.human = '' # human readable text
9696
self.text_distance = 5
97+
self.text_line_distance = 1
9798
self.center_text = True
9899

99100
def calculate_size(self, modules_per_line, number_of_lines, dpi=300):
@@ -112,8 +113,10 @@ def calculate_size(self, modules_per_line, number_of_lines, dpi=300):
112113
"""
113114
width = 2 * self.quiet_zone + modules_per_line * self.module_width
114115
height = 2.0 + self.module_height * number_of_lines
116+
number_of_text_lines = self.text.count('\n') + 1
115117
if self.font_size and self.text:
116-
height += pt2mm(self.font_size) / 2 + self.text_distance
118+
height += pt2mm(self.font_size) / 2 * number_of_text_lines + self.text_distance
119+
height += self.text_line_distance * (number_of_text_lines - 1)
117120
return int(mm2px(width, dpi)), int(mm2px(height, dpi))
118121

119122
def save(self, filename, output):
@@ -261,21 +264,23 @@ def _create_module(self, xpos, ypos, width, color):
261264
self._group.appendChild(element)
262265

263266
def _create_text(self, xpos, ypos):
264-
element = self._document.createElement('text')
265-
attributes = dict(x=SIZE.format(xpos), y=SIZE.format(ypos),
266-
style='fill:{0};font-size:{1}pt;text-anchor:'
267-
'middle;'.format(self.foreground,
268-
self.font_size))
269-
_set_attributes(element, **attributes)
270267
# check option to override self.text with self.human (barcode as
271268
# human readable data, can be used to print own formats)
272269
if self.human != '':
273270
barcodetext = self.human
274271
else:
275-
barcodetext = self.text
276-
text_element = self._document.createTextNode(barcodetext)
277-
element.appendChild(text_element)
278-
self._group.appendChild(element)
272+
barcodetext = self.text
273+
for subtext in barcodetext.split('\n'):
274+
element = self._document.createElement('text')
275+
attributes = dict(x=SIZE.format(xpos), y=SIZE.format(ypos),
276+
style='fill:{0};font-size:{1}pt;text-anchor:'
277+
'middle;'.format(self.foreground,
278+
self.font_size))
279+
_set_attributes(element, **attributes)
280+
text_element = self._document.createTextNode(subtext)
281+
element.appendChild(text_element)
282+
self._group.appendChild(element)
283+
ypos += pt2mm(self.font_size) + self.text_line_distance
279284

280285
def _finish(self):
281286
if self.compress:
@@ -322,11 +327,14 @@ def _paint_module(self, xpos, ypos, width, color):
322327
self._draw.rectangle(size, outline=color, fill=color)
323328

324329
def _paint_text(self, xpos, ypos):
325-
font = ImageFont.truetype(FONT, self.font_size * 2)
326-
width, height = font.getsize(self.text)
327-
pos = (mm2px(xpos, self.dpi) - width // 2,
328-
mm2px(ypos, self.dpi) - height // 4)
329-
self._draw.text(pos, self.text, font=font, fill=self.foreground)
330+
for subtext in self.text.split('\n'):
331+
font = ImageFont.truetype(FONT, self.font_size * 2)
332+
width, height = font.getsize(subtext)
333+
# determine the maximum width of each line
334+
pos = (mm2px(xpos, self.dpi) - width // 2,
335+
mm2px(ypos, self.dpi) - height // 4)
336+
self._draw.text(pos, subtext, font=font, fill=self.foreground)
337+
ypos += pt2mm(self.font_size) / 2 + self.text_line_distance
330338

331339
def _finish(self):
332340
return self._image

0 commit comments

Comments
 (0)