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

@capsizecss/metrics

v3.0.0

Published

Font metrics library for system and Google fonts

Downloads

122,599

Readme

@capsizecss/metrics

Font metrics library for system and Google fonts.

npm install @capsizecss/metrics

Usage

Import the metrics for your chosen font to pass them directly into capsize.

import { createStyleObject } from '@capsizecss/core';
import arialMetrics from '@capsizecss/metrics/arial';

const capsizeStyles = createStyleObject({
  fontSize: 16,
  leading: 24,
  fontMetrics: arialMetrics,
});

In addition to common system fonts, Google fonts are also supported.

import { createStyleObject } from '@capsizecss/core';
import lobsterMetrics from '@capsizecss/metrics/lobster';

const capsizeStyles = createStyleObject({
  fontSize: 16,
  leading: 24,
  fontMetrics: lobsterMetrics,
});

Font metrics

The font metrics object returned contains the following properties if available:

| Property | Type | Description | | ---------- | ------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | familyName | string | The font family name as authored by font creator | | category | string | The style of the font: serif, sans-serif, monospace, display, or handwriting. | | capHeight | number | The height of capital letters above the baseline | | ascent | number | The height of the ascenders above baseline | | descent | number | The descent of the descenders below baseline | | lineGap | number | The amount of space included between lines | | unitsPerEm | number | The size of the font’s internal coordinate grid | | xHeight | number | The height of the main body of lower case letters above baseline | | xWidthAvg | number | The average width of character glyphs in the font for the selected unicode subset. Calculated based on character frequencies in written text (see below), falling back to the built in xAvgCharWidth from the OS/2 table. | | subsets | {[subset]: { xWidthAvg: number }} | A lookup of the xWidthAvg metric by subset (see supported subsets below) |

How xWidthAvg is calculated

The xWidthAvg metric is derived from character frequencies in written language. The value takes a weighted average of character glyph widths in the font, falling back to the built in xAvgCharWidth from the OS/2 table if the glyph width is not available.

The purpose of this metric is to support generating CSS metric overrides (e.g. ascent-override, size-adjust, etc) for fallback fonts, enabling inference of average line lengths so that a fallback font can be scaled to better align with a web font. This can be done either manually or using createFontStack.

For this technique to be effective, the metric factors in a character frequency weightings as observed in written language, using “abstracts” from Wikinews articles as a data source. Below is the source analysed for each supported subset:

| Subset | Language | | ------- | -------------------------------------------- | | latin | English (source) | | thai | Thai (source) |

[!TIP] Need support for a different unicode subset? Either create an issue or follow the steps outlined in the generate-weightings script in the unpack package and open a PR.

For more information on how to access the metrics for different subsets, see the subsets section below.

Subsets

The top level xWidthAvg metric represents the average character width for the latin subset. However, the xWidthAvg for each supported subset is available explicitly within the subsets field.

For example:

import arial from '@capsizecss/metrics/arial';

const xWidthAvgDefault = arial.xWidthAvg;
const xWidthAvgLatin = arial.subsets.latin.xWidthAvg; // Same as above
const xWidthAvgThai = arial.subsets.thai.xWidthAvg;

Supporting APIs

fontFamilyToCamelCase

A helper function to support tooling that needs to convert the font family name to the correct casing for the relevant metrics import.

import { fontFamilyToCamelCase } from '@capsizecss/metrics';

const familyName = fontFamilyToCamelCase('--apple-system'); // => `appleSystem`
const metrics = await import(`@capsizecss/metrics/${familyName}`);

entireMetricsCollection

Provides the entire metrics collection as a JSON object, keyed by font family name.


⚠️ CAUTION: Importing this will result in a large JSON structure being pulled into your project!

It is not recommended to use this client side.


import { entireMetricsCollection } from '@capsizecss/metrics/entireMetricsCollection';

const metrics = entireMetricsCollection['arial'];

Thanks

  • SEEK for giving us the space to do interesting work.

License

MIT.