WhyNotHugo / python-barcode

㊙️ Create standard barcodes with Python. No external dependencies. 100% Organic Python.
http://python-barcode.rtfd.io/
MIT License
573 stars 123 forks source link

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

Open toxicantidote opened 2 years ago

toxicantidote commented 2 years ago

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'))
toxicantidote commented 2 years ago

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

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

WhyNotHugo commented 2 years ago

Can you try 0.14.0? There were a few fixes related to positioning and sizes in that release.

toxicantidote commented 2 years ago

I have updated to the 0.14.0 wheel from the releases page and the issue still persists as originally described.

Confirmed barcode.version returns '0.14.0'