Skip to content

Commit cf57e0f

Browse files
committed
Add option to specify the font to use
The bundled font may not always be the right choice for a given barcode, so I've added an option for the user to specify the font to use. Simply pass the full path to the font file using the font_path option.
1 parent 04b6e5a commit cf57e0f

File tree

3 files changed

+6
-2
lines changed

3 files changed

+6
-2
lines changed

barcode/writer.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ def create_svg_object():
4949
SIZE = '{0:.3f}mm'
5050
COMMENT = 'Autogenerated with python-barcode {0}'.format(version)
5151
PATH = os.path.dirname(os.path.abspath(__file__))
52-
FONT = os.path.join(PATH, 'DejaVuSansMono.ttf')
5352

5453

5554
class BaseWriter(object):
@@ -87,6 +86,7 @@ def __init__(
8786
}
8887
self.module_width = 10
8988
self.module_height = 10
89+
self.font_path = os.path.join(PATH, 'fonts', 'DejaVuSansMono.ttf')
9090
self.font_size = 10
9191
self.quiet_zone = 6.5
9292
self.background = 'white'
@@ -352,8 +352,8 @@ def _paint_module(self, xpos, ypos, width, color):
352352
self._draw.rectangle(size, outline=color, fill=color)
353353

354354
def _paint_text(self, xpos, ypos):
355+
font = ImageFont.truetype(self.font_path, self.font_size * 2)
355356
for subtext in self.text.split('\n'):
356-
font = ImageFont.truetype(FONT, self.font_size * 2)
357357
width, height = font.getsize(subtext)
358358
# determine the maximum width of each line
359359
pos = (

docs/source/writers/index.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ Common Options:
3131
(last) barcode module in mm as *float*.
3232
Defaults to **6.5**.
3333

34+
:font_path:
35+
Path to the font file to be used. Defaults to **DejaVuSansMono** (which
36+
is bundled with this package).
37+
3438
:font_size:
3539
Font size of the text under the barcode in pt as *integer*.
3640
Defaults to **10**.

0 commit comments

Comments
 (0)