Skip to content

Commit 5341366

Browse files
author
Hugo Osvaldo Barrera
authored
Merge pull request #48 from roth-a/master
Added multiline functionality for the user defined text.
2 parents 557742d + 7c26d2f commit 5341366

1 file changed

Lines changed: 27 additions & 15 deletions

File tree

barcode/writer.py

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ def __init__(self, initialize=None, paint_module=None, paint_text=None,
9090
self.text = ''
9191
self.human = '' # human readable text
9292
self.text_distance = 5
93+
self.text_line_distance = 1
9394
self.center_text = True
9495

9596
def calculate_size(self, modules_per_line, number_of_lines, dpi=300):
@@ -108,8 +109,10 @@ def calculate_size(self, modules_per_line, number_of_lines, dpi=300):
108109
"""
109110
width = 2 * self.quiet_zone + modules_per_line * self.module_width
110111
height = 2.0 + self.module_height * number_of_lines
112+
number_of_text_lines = len(self.text.splitlines())
111113
if self.font_size and self.text:
112-
height += pt2mm(self.font_size) / 2 + self.text_distance
114+
height += pt2mm(self.font_size) / 2 * number_of_text_lines + self.text_distance
115+
height += self.text_line_distance * (number_of_text_lines - 1)
113116
return int(mm2px(width, dpi)), int(mm2px(height, dpi))
114117

115118
def save(self, filename, output):
@@ -257,21 +260,27 @@ def _create_module(self, xpos, ypos, width, color):
257260
self._group.appendChild(element)
258261

259262
def _create_text(self, xpos, ypos):
260-
element = self._document.createElement('text')
261-
attributes = dict(x=SIZE.format(xpos), y=SIZE.format(ypos),
262-
style='fill:{0};font-size:{1}pt;text-anchor:'
263-
'middle;'.format(self.foreground,
264-
self.font_size))
265-
_set_attributes(element, **attributes)
266263
# check option to override self.text with self.human (barcode as
267264
# human readable data, can be used to print own formats)
268265
if self.human != '':
269266
barcodetext = self.human
270267
else:
271268
barcodetext = self.text
272-
text_element = self._document.createTextNode(barcodetext)
273-
element.appendChild(text_element)
274-
self._group.appendChild(element)
269+
for subtext in barcodetext.split('\n'):
270+
element = self._document.createElement('text')
271+
attributes = dict(
272+
x=SIZE.format(xpos),
273+
y=SIZE.format(ypos),
274+
style='fill:{0};font-size:{1}pt;text-anchor:middle;'.format(
275+
self.foreground,
276+
self.font_size,
277+
)
278+
)
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
275284

276285
def _finish(self):
277286
if self.compress:
@@ -318,11 +327,14 @@ def _paint_module(self, xpos, ypos, width, color):
318327
self._draw.rectangle(size, outline=color, fill=color)
319328

320329
def _paint_text(self, xpos, ypos):
321-
font = ImageFont.truetype(FONT, self.font_size * 2)
322-
width, height = font.getsize(self.text)
323-
pos = (mm2px(xpos, self.dpi) - width // 2,
324-
mm2px(ypos, self.dpi) - height // 4)
325-
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
326338

327339
def _finish(self):
328340
return self._image

0 commit comments

Comments
 (0)