Skip to content

Left align text when center_text is false - #271

Closed
TheeCoderAhmed wants to merge 1 commit into
WhyNotHugo:mainfrom
TheeCoderAhmed:fix/center-text-false-left-align
Closed

TheeCoderAhmed wants to merge 1 commit into
WhyNotHugo:mainfrom
TheeCoderAhmed:fix/center-text-false-left-align

Conversation

@TheeCoderAhmed

Copy link
Copy Markdown

Fixes #145

The docs say center_text=False left aligns the text. Instead, the text gets cut off at the left edge of the image.

BaseWriter.render moves the text position to the start of the barcode when center_text is false, but both writers still anchor the text by its center (text-anchor:middle in SVGWriter, anchor="md" in ImageWriter). So half of the text lands over the quiet zone, and anything wider than that is cut off.

The writers now anchor the text at its start in that case (text-anchor:start, anchor="ld"). render decides this and stores it in a private attribute, so the paint_text(xpos, ypos) callback signature stays the same for custom writers.

Text blocks between EAN guards stay centered even with center_text=False, since their positions are computed as centers. Nothing changes when center_text is true.

What I ran, on Python 3.12 with Pillow installed:

  • the new test_svg_center_text_false_left_aligns_text and test_image_center_text_false_left_aligns_text fail before the change and pass after it. The other two new tests cover the default and the guard bar case, and they pass both before and after
  • pytest tests: 60 passed, and 57 passed in a second environment without Pillow
  • ruff check barcode tests, and ruff format --check on the changed Python files
  • mypy barcode reports 8 errors both with and without this change, and none of them are on the lines I changed. tests/test_writers.py adds no new ones

Not run: tox or other Python versions. I checked the SVG output by its attributes and the PNG output by its pixel positions, and I did not open the SVG in a browser.

The writers docs say that center_text=False left aligns the text. The
position was moved to the start of the barcode, but both writers kept
anchoring the text by its center, so half of it ended up over the quiet
zone and long text was cut off at the left edge.

Anchor the text at its start in that case. Text blocks between EAN guards
are positioned by their center, so they stay centered.
@WhyNotHugo WhyNotHugo closed this Nov 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

2 participants