npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

node-font-face-generator

v0.1.9

Published

Generate language/browser dependent @font-face CSS declarations

Downloads

106

Readme

node-font-face-generator

A node module to generate locale/browser dependent @font-face CSS declarations.

Usage

  1. Include node-font-face-generator in a node module.
const css_generator = require("node-font-face-generator");
  1. Set up your configuration. Call .setup with configuration.

Possible options:

  • fonts
  • localeToUrlKeys
  • urlModifier
  • host

fonts is an Object that holds a dictionary of fonts.

font_config = {
  "OpenSansRegular": {
    "fontFamily": "Open Sans",
    "fontStyle": "normal",
    "fontWeight": "400",
    "formats": [ {
        "type": "local",
        "url": "Open Sans"            // TrueType name (for most OSs)
      }, {
        "type": "local",
        "url": "Open Sans-Regular"    // PostScript name (for OSX)
      }, {
        "type": "embedded-opentype",
        "url": "/fonts/OpenSans-Regular.eot"
      }, {
        "type": "woff",
        "url": {
          "latin": "/fonts/OpenSans-Regular-latin.woff",
          "cyrillic": "/fonts/OpenSans-Regular-cyrillic.woff",
          "default": "/fonts/OpenSans-Regular-default.woff",
          "chinese": "/fonts/OpenSans-Regular-chinese.woff",
        }
      }, {
        "type": "truetype",
        "url": {
          "latin": "/fonts/OpenSans-Regular-latin.ttf",
          "default": "/fonts/OpenSans-Regular-default.ttf"
        }
      }, {
        "type": "svg",
        "url": {
          "latin": "/fonts/OpenSans-Regular-latin.svg",
          "default": "/fonts/OpenSans-Regular-default.svg"
        },
        "id": "opensans-regular"
      } ],
    // font specific locale to URL keys. Locales defined here override
    // the generic localeToUrlKeys passed in as configuration to
    // .setup.
    "localeToUrlKeys": {
      "cz": "chinese"
    }
  }
};

A font have multiple, locale specific URLs. For example, Latin based locales can be specified under the latin url, Russian under cyrillic, and Greek under greek. If multiple urls are defined, the fallback locale default must be defined.

localeToUrlKeys is an optional object that holds a dictionary of locales to urls. localeToUrlKeys kicks in if a locale cannot be directly found in the url list specified for a font. For example:

localeToUrlKeys = {
  "en":    "english",   // will match for en, en-US, en-UK, en-CA, ...
  "es":    "spanish",   // will match for es, es-MX, en-AR, en-*
  "fr"     "french",
  "ru":    "russian",
  "ro":    "romanian",
  "bg":    "bulgarian",
  "jp":    "japanese"
};

localeToUrlKeys may be defined generically for all fonts via the setup function, or as an alternative, for each individual font. The rules for how a locale matches to a URL are:

  • first, look in the list of URLs specified for a font to see if the locale is available.
  • next, look in the list of URLs specified for a font to see if the baseLocale is available.
  • next, look in the font specific localeToUrlKeys to see if the locale is specified.
  • next, look in the font specific localeToUrlKeys to see if the baseLocale is specified.
  • next, look in the generic localeToUrlKeys to see if the locale is specified.
  • next, look in the generic localeToUrlKeys to see if the baseLocale is specified.
  • next, look in DefaultUrlKeys to see if the locale is specified.
  • next, look in DefaultUrlKeys to see if the baseLocale is specified.
  • finally, fall back to default.

DefaultUrlKeys are a base set of localeToUrlKeys that are compatible with Google Font Directory's subset.pl utility. The list is found in lib/locale_to_url_keys.js

urlModifier is an optional function that is called to modify each URL. This is useful for caching/cache busting. Function must return a string.

host is an optional string used to specify a host where fonts are located. Useful if fonts are located on a CDN.

  1. Call the setup function with the configuration objects.
css_generator.setup({
  fonts: font_config,
  localeToUrlKeys: localeToUrlKeys
});
  1. When CSS for a custom font-face is needed, call get_font_css with the configuration and a callback. The callback follows node convention and will be called with two parameters when complete. The first parameter is any error that is thrown (or null), the second is the CSS (or null).
var css = css_generator.get_font_css({
  ua: getUserAgent(),
  locale: getUsersLocale(),
  fonts: ["OpenSansRegular"]
}, function(err, css) {
  if (err) {
    // handle the error
    ...
  }
  else if (css) {
    // do something with the CSS
  }
  else {
    // this should never ever happen
  }
});
  1. It is possible to generate one @font-face with declarations for all browsers using ua: 'all'. This is useful to create CSS for inclusion in a larger CSS file as part of a build script.
var css = css_generator.get_font_css({
  ua: 'all',
  locale: getUsersLocale(),
  fonts: ["OpenSansRegular"]
}, function(err, css) {
  if (err) {
    // handle the error
    ...
  }
  else if (css) {
    // This will be the css with fonts declared for all browsers.
  }
  else {
    // this should never ever happen
  }
});
  1. Do what you will with the CSS. Write it out to a .CSS file in a build script or handle it in an HTTP request.

Author:

Getting involved:

I am happy to review submissions!

License:

This software is available under version 2.0 of the MPL:

https://www.mozilla.org/MPL/