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

Changing font functionality #104

Open
opened 2021年01月27日 15:07:54 +01:00 by jkissl · 1 comment
jkissl commented 2021年01月27日 15:07:54 +01:00 (Migrated from github.com)
Copy link

Looking through the documentation and the code in I found the font_path argument that either defaults to a supplied font or can be passed a path to a different font. I've tried several different variations of passing a font to it and even modified writer.py directly to use a different font. Neither of those had any effect on the font that appears below the barcode. Can you provide an example of changing the font?

Looking through the documentation and the code in I found the font_path argument that either defaults to a supplied font or can be passed a path to a different font. I've tried several different variations of passing a font to it and even modified writer.py directly to use a different font. Neither of those had any effect on the font that appears below the barcode. Can you provide an example of changing the font?
kmBlaine commented 2021年01月29日 23:33:20 +01:00 (Migrated from github.com)
Copy link

I encountered this as well behavior as well. It only applies if you are writing SVGs though. If you write raster files (PNGs, JPGs, etc), the font_path option does exactly what you expect.

Looking at the SVGWriter class in class in barcode/writer.py it's pretty obvious why:

class SVGWriter(BaseWriter):
 # ...
 def _create_text(self, xpos, ypos):
 # ...
 attributes = {
 "x": SIZE.format(xpos),
 "y": SIZE.format(ypos),
 #
 # the 'font-family' attribute used by XHTML docs to render font is not set
 #
 "style": "fill:{};font-size:{}pt;text-anchor:middle;".format(
 self.foreground,
 self.font_size,
 ),
 }

As hacky workaround since in my use case I needed bunch of arbitrarily scalable barcodes with non-ambiguous font (a lot of fonts 0 and capitol O look identical), I just had it write the font_path option as the XHTML font-family option:

class SVGWriter(BaseWriter):
 # ...
 def _create_text(self, xpos, ypos):
 # ...
 attributes = {
 "x": SIZE.format(xpos),
 "y": SIZE.format(ypos),
 "style": "fill:{};font-size:{}pt;text-anchor:middle;font-family:'{}'".format(
 self.foreground,
 self.font_size,
 self.font_path
 ),
 }

CAUTION: I am not clear on how that would interact with the SVG sizing system in the package so I didn't commit and PR this change. I wanted to take a closer look and do this more smartly if need be. Regardless, I am more than happy to work with the team on giving SVG barcodes font customizability.

I encountered this as well behavior as well. It only applies if you are writing SVGs though. If you write raster files (PNGs, JPGs, etc), the `font_path` option does exactly what you expect. Looking at the `SVGWriter` class in class in [barcode/writer.py](https://github.com/WhyNotHugo/python-barcode/blob/master/barcode/writer.py) it's pretty obvious why: ``` class SVGWriter(BaseWriter): # ... def _create_text(self, xpos, ypos): # ... attributes = { "x": SIZE.format(xpos), "y": SIZE.format(ypos), # # the 'font-family' attribute used by XHTML docs to render font is not set # "style": "fill:{};font-size:{}pt;text-anchor:middle;".format( self.foreground, self.font_size, ), } ``` As hacky workaround since in my use case I needed bunch of arbitrarily scalable barcodes with non-ambiguous font (a lot of fonts `0` and capitol `O` look identical), I just had it write the `font_path` option as the XHTML `font-family` option: ``` class SVGWriter(BaseWriter): # ... def _create_text(self, xpos, ypos): # ... attributes = { "x": SIZE.format(xpos), "y": SIZE.format(ypos), "style": "fill:{};font-size:{}pt;text-anchor:middle;font-family:'{}'".format( self.foreground, self.font_size, self.font_path ), } ``` CAUTION: I am not clear on how that would interact with the SVG sizing system in the package so I didn't commit and PR this change. I wanted to take a closer look and do this more smartly if need be. Regardless, I am more than happy to work with the team on giving SVG barcodes font customizability.
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#104
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?