center_text=False cuts off text at left when justifying barcode text in SVG output #145

Open
opened 2022-02-09 05:16:17 +01:00 by toxicantidote · 1 comment
toxicantidote commented 2022-02-09 05:16:17 +01:00 (Migrated from github.com)

Barcode version: 0.13.1-py3
Python version: 3.7.3 (64 bit)
Platform: Windows 10 (20H2)

If the center_text=False option is used when writing a code128 (and possibly others) barcode in SVG format, the text under the barcode is justified beyond the left of the SVG. It appears that the justification is based on the center of the the SVG text, rather than its left edge.

I am using this in an application that embeds these SVG images within a table in a webpage, and for the same code (97660) they appear as below:
barcode-bug

A simplified extract of the code section being used to generate the SVG is below, however it uses the same writer options as my production code. I suspect that changes to the quiet_zone option are not being accounted for in text positioning.

code = 97660
## imports
import barcode
import io
## make a file-like object to write to
handle = io.BytesIO()
writer = barcode.writer.SVGWriter()
## no doctype, just svg tags
writer.with_doctype = False
## create the barcode
c128 = barcode.get_barcode('code128', str(code), writer = writer)
## render it, saving the data to the file-like object. pass in options
## to specify the sizing and dimensions of the barcode
c128.write(handle, options = {'text_distance': 4.0, 'module_height': 4.0, 'font_size': 8, 'quiet_zone': 1.0, 'module_width': 0.15})
## seek back to the start of the file-like object
handle.seek(0)
## get the binary image data
data = handle.read()
## close the file-like object
handle.close()
## print the SVG data as text
print(data.decode('utf-8', errors = 'ignore'))
Barcode version: 0.13.1-py3 Python version: 3.7.3 (64 bit) Platform: Windows 10 (20H2) If the `center_text=False` option is used when writing a code128 (and possibly others) barcode in SVG format, the text under the barcode is justified beyond the left of the SVG. It appears that the justification is based on the center of the the SVG text, rather than its left edge. I am using this in an application that embeds these SVG images within a table in a webpage, and for the same code (97660) they appear as below: ![barcode-bug](https://user-images.githubusercontent.com/23189273/153119732-03f537a7-eb66-48b7-a49a-d599d89d8e27.png) A simplified extract of the code section being used to generate the SVG is below, however it uses the same writer options as my production code. I suspect that changes to the `quiet_zone` option are not being accounted for in text positioning. ``` code = 97660 ## imports import barcode import io ## make a file-like object to write to handle = io.BytesIO() writer = barcode.writer.SVGWriter() ## no doctype, just svg tags writer.with_doctype = False ## create the barcode c128 = barcode.get_barcode('code128', str(code), writer = writer) ## render it, saving the data to the file-like object. pass in options ## to specify the sizing and dimensions of the barcode c128.write(handle, options = {'text_distance': 4.0, 'module_height': 4.0, 'font_size': 8, 'quiet_zone': 1.0, 'module_width': 0.15}) ## seek back to the start of the file-like object handle.seek(0) ## get the binary image data data = handle.read() ## close the file-like object handle.close() ## print the SVG data as text print(data.decode('utf-8', errors = 'ignore')) ```
toxicantidote commented 2022-02-09 05:23:39 +01:00 (Migrated from github.com)

Looked at the code. I see quiet_zone appears to be accounted for after all.

Also in case it's relevant. browser is Chrome.

Looked at the code. I see `quiet_zone` appears to be accounted for after all. Also in case it's relevant. browser is Chrome.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
WhyNotHugo/python-barcode#145
No description provided.