Repository navigation
Left align text when center_text is false - #271
Closed
TheeCoderAhmed wants to merge 1 commit into
Closed
TheeCoderAhmed wants to merge 1 commit into
TheeCoderAhmed wants to merge 1 commit into
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #145
The docs say
center_text=Falseleft aligns the text. Instead, the text gets cut off at the left edge of the image.BaseWriter.rendermoves the text position to the start of the barcode whencenter_textis false, but both writers still anchor the text by its center (text-anchor:middleinSVGWriter,anchor="md"inImageWriter). 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").renderdecides this and stores it in a private attribute, so thepaint_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 whencenter_textis true.What I ran, on Python 3.12 with Pillow installed:
test_svg_center_text_false_left_aligns_textandtest_image_center_text_false_left_aligns_textfail 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 afterpytest tests: 60 passed, and 57 passed in a second environment without Pillowruff check barcode tests, andruff format --checkon the changed Python filesmypy barcodereports 8 errors both with and without this change, and none of them are on the lines I changed.tests/test_writers.pyadds no new onesNot 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.