Shields: How to use new BadgeFactory export

Created on 13 Nov 2018  Â·  3Comments  Â·  Source: badges/shields

Maybe a dumb question, but I just upgraded the npm package from 1.3.0 to 2.0.0 and previously did not use badge.loadFont:

const badge = require("gh-badges"),
badge(
        {
          text: ["storybook", "up to date"],
          colorscheme: "orange",
          template: "flat"
        },
        function(svg, err) {
          fs.writeFileSync(folder + outputFile, svg);
        }
      );

Following the instructions here, I updated to:

const { BadgeFactory } = require("gh-badges");
const bf = new BadgeFactory();
const format = {
        text: ["storybook", "up to date"],
        colorscheme: "orange",
        template: "flat"
      };
      const svg = bf.create(format);

Which produces this error:

TypeError: Cannot destructure propertyfontPathof 'undefined' or 'null'.

I'm not sure how the old code worked, but it clearly did not require a font path before. How can I use badges without a font path like I previously did?

npm-package question

All 3 comments

Prior to 1.3.0 it looks like the interface looked like this:

var badge = require('gh-badges');
badge({ text: [ "build", "passed" ], colorscheme: "green" },
  function(svg, err) {
    // svg is a String… of your badge.
  });

It looked for Verdana.ttf in the directory containing Shields for accurate width computation, and if it didn't find it, would fall back to Helvetica-Bold for approximate widths.

1.3.0 added an optional step, allowing you to specify the path to Verdana:

// Optional step, to have accurate text width computation.
badge.loadFont('/path/to/Verdana.ttf', function(err) {
  badge({ text: ["build", "passed"], colorscheme: "green", template: "flat" },
    function(svg, err) {
      // svg is a String of your badge.
    });
});

In 2.0.0 there is no default font path. If you want to use Helvetica, you can pass 'Helvetica' for either fontPath or defaultFontPath:

const bf = new BadgeFactory({ fallbackFontPath: 'Helvetica' });

We could make that the default in 2.1.0. Though I've also proposed a change in #2311 that will eliminate the need for fonts entirely, which is probably a better path.

Thank you! I was going crazy thinking I had to have a local font file or something. Worked like a charm.

This information has been added to the changelog in #2300.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kerolloz picture kerolloz  Â·  3Comments

korenyoni picture korenyoni  Â·  3Comments

najeeb-ur-rehman picture najeeb-ur-rehman  Â·  3Comments

Fazendaaa picture Fazendaaa  Â·  3Comments

irgolic picture irgolic  Â·  3Comments