1
0
Fork
You've already forked python-barcode
0

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)
Copy link

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)
Copy link

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 Branch/Tag specified
main
pre-commit-ci-update-config
extensibility
fix_sizes
feature/longer_guard_bar
ean-without-checksum
readme-formatting
cleaning
cleanup
srolin/master
michieldwitte/left-alignment-fix
v0.13.1
v0.13.0
v0.12.0
v0.11.0
v0.10.0
v0.9.0
v0.8.3
v0.8.2
v0.8.1
v0.8.0
0.8beta1
0.7
0.7beta4
0.7beta3
0.7beta2
v0.6
v0.6b3
v0.6b2
v0.6b1
v0.5.0
v0.5.0b1
v0.4.3
v0.4.2
v0.4.1
v0.4
v0.4b2
v0.4b1
v0.3
v0.3b1
v0.2.1
v0.1
Milestone
Clear milestone
No items
No milestone
Projects
Clear projects
No items
No project
Assignees
Clear assignees
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
Reference in a new issue
WhyNotHugo/python-barcode
No description provided.
Delete branch "%!s()"

Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?