Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed font size when specified in configuration #26

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ PLUGINS_CONFIG = {
'with_text': True,
'text_fields': ['name', 'serial'],
'font': 'ArialMT',
'font_size': 12, # If the value is 0 or the line does not exist, then the text is automatically adjusted
'custom_text': 'Property of SomeCompany\ntel.8.800333554-CALL',
'text_location': 'up',
'qr_version': 1,
Expand Down
2 changes: 1 addition & 1 deletion netbox_qrcode/template_content.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def x_page(self):
if custom_text:
text.append(custom_text)
text = '\n'.join(text)
text_img = get_qr_text(qr_img.size, text, config.get('font'))
text_img = get_qr_text(qr_img.size, text, config.get('font'), config.get('font_size', 0))
qr_with_text = get_concat(qr_img, text_img, config.get('text_location', 'right'))

img = get_img_b64(qr_with_text)
Expand Down
31 changes: 23 additions & 8 deletions netbox_qrcode/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,29 @@ def get_img_b64(img):
return str(base64.b64encode(stream.getvalue()), encoding='ascii')


def get_qr_text(max_size, text, font='TahomaBold'):
font_size = 56
tmpimg = Image.new('L', max_size, 'white')
def get_qr_text(max_size, text, font='TahomaBold', font_size=0):

tmpimg = Image.new('P', max_size, 'white')
text_too_large = True
while text_too_large:

#If no Font Size in Config File, then Match the text to the QR Code
if font_size == 0:

font_size = 56

while text_too_large:
file_path = resource_stream(__name__, 'fonts/{}.ttf'.format(font))
try:
fnt = ImageFont.truetype(file_path, font_size)
except Exception:
fnt = ImageFont.load_default()

draw = ImageDraw.Draw(tmpimg)
w, h = draw.textsize(text, font=fnt)
if w < max_size[0] - 4 and h < max_size[1] - 4:
text_too_large = False
font_size -= 1
else:
file_path = resource_stream(__name__, 'fonts/{}.ttf'.format(font))
try:
fnt = ImageFont.truetype(file_path, font_size)
Expand All @@ -41,9 +59,6 @@ def get_qr_text(max_size, text, font='TahomaBold'):

draw = ImageDraw.Draw(tmpimg)
w, h = draw.textsize(text, font=fnt)
if w < max_size[0] - 4 and h < max_size[1] - 4:
text_too_large = False
font_size -= 1

img = Image.new('L', (w, h), 'white')
draw = ImageDraw.Draw(img)
Expand All @@ -63,7 +78,7 @@ def get_concat(im1, im2, direction='right'):
'Invalid direction "{}" (must be one of "left", "right", "up", or "down")'.format(direction)
)

dst = Image.new('L', (width, height), 'white')
dst = Image.new('P', (width, height), 'white')

if direction == 'right' or direction == 'left':
if im1.height > im2.height:
Expand Down