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

cap-height

v2.0.0

Published

Calculate the cap height of fonts loaded with Web Font Loader

Downloads

41

Readme

Cap height

Calculate the cap height of any font loaded with Web Font Loader. See the demo on CodePen.

What is the cap height?

The cap height is the height of a capital letter measured from the baseline.

Read more on Typography Deconstructed and on Wikipedia.

How is this useful?

The web lacks exquisite typography. Of course, its state is very good with the wide use of @font-face and the quality web font distribution services and foundries. However, setting type on the web still challenging. Several attempts and tools have risen in order to define a proper vertical rhythm and a baseline grid.

But most of them depend on a cap height ratio, which varies for every typeface and cannot be calculated with CSS. You have to rely on design software or font metrics from the foundries to find this out. This package addresses this issue by rendering loaded fonts on a canvas to measure the actual height of the glyphs, and thus calculating the cap-height/font-size ratio. Then you could easily apply this value to any tool you like.

Read more about the subject

Demo

See the demo on CodePen. Open the CodePen console to see the output.

Or run dist/index.html in your browser and open the developer tools. In the output of the console you should see a JSON object for each loaded font, with its properties. An additional --cap-height CSS property contains the holy grail of the web typography.

In the browser window a canvas will be displayed for each loaded font with the letter H.

This example loads Roboto Mono through Google Fonts; but you can use any module available in the Web Font Loader, like Typekit, Fondeck, Fonts.com or your own web fonts.

Check all the available modules: https://github.com/typekit/webfontloader#modules

How it works

When a @font-face font is loaded with the Web Font Loader, it creates a HTML canvas and draws the letter H with the appropriate CSS properties extracted from the Font Variation Description. Then it measures the actual pixel height of the drawn letter, and calculates the ratio between the height and the font-size.

WIP

This project is a work in progress. I’m planning to add more methods to calculate loaded fonts beyond the Web Font Loader, like the Font Face API, or simply by selecting a DOM element along with its CSS properties.

Installation

In a browser:

Include the dist/cap-height.js script in your document, then call the methods on the capHeight instance. Don’t forget to include the Web Font Loader.

<script src="https://unpkg.com/[email protected]"></script>
<script src="./cap-height.js"></script>

<script>
  capHeight.calculate({});
</script>

As a module:

$ npm install cap-height
import capHeight from "capHeight";

capHeight.calculate({});

API

calculate(properties, [text=H])

This is the main method which calculates the cap-height. The first argument is an object containing the font-style, font-weight, font-size, and font-family properties. If you omit any of these, the defaults in the example below will be used.

Warning: The font-size MUST be in pixels. If any other unit of unit-less number is passed, it will be evaluated in pixels. E.g. 2em or 2 will be computed to 2px.

The second argument is optional. It defines the text that will be used to measure the cap-height. The default is H.

The method returns the passed object along with a --cap-height CSS property, and any other omitted property.

capHeight.calculate({
  "font-style": "normal",
  "font-weight": 400,
  "font-size": "100px",
  "font-family": "serif"
}, "HI");

// Output
{
  "font-style": "normal",
  "font-weight": 400,
  "font-size": "100px",
  "font-family": "serif",
  "--cap-height": 0.66
}

fontActive(callback, text)

Event handler for the fontactive or active events of the Web Font Loader.

This method accepts a callback function, in which the calculated properties will be passed. The callback is executed after the Web Font Loader has fired the appropriate events and the calculations are completed.

Read about the Web Font Loader events: https://github.com/typekit/webfontloader#events

You can pass a string as an optional second argument after your callback, to define the text that will be used to calculate the cap-height. The default is H.

WebFontConfig = {
  {
    // define your font modules here
  },
  fontactive: capHeight.fontActive(function(properties) {
    console.log(properties);
  }, "HI");
};

setContainer(element)

If you want to see/debug the process, define a DOM element in which a canvas(es) of each rendered font will be displayed.

WebFontConfig = {
  // set the container while the fonts are loading
  loading: function() {
    capHeight.setContainer(document.body);
  }
}

Credits