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)

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)

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 milestone
No project
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
No description provided.