capsize

repository·master·Indexed 23 days ago

https://github.com/seek-oss/capsize

A tool for making text sizing and layout predictable by using font metadata to trim whitespace above capital letters and below the baseline. It includes @capsizecss/core for generating styles, @capsizecss/metrics for system and Google font data, @capsizecss/unpack for extracting metrics from font files, and an official integration for vanilla-extract via @capsizecss/vanilla-extract.

Tokens
9.3K
Snippets
32
Records
57
Agent score
82%

What's inside capsize

  1. Access average character width by subset

    master

    The xWidthAvg metric represents the average character width. While the top-level xWidthAvg property defaults to the latin subset, you can access specific language subsets via the subsets field.

    Supported Subsets:

    • latin: English (based on Wikinews data)
    • thai: Thai (based on Wikinews data)
    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;
  2. Obtain Font Metrics

    master

    To use Capsize, you must provide fontMetrics. There are three primary ways to obtain them:

    1. Pre-packaged metrics: Install @capsizecss/metrics and import the specific font (e.g., import arialMetrics from '@capsizecss/metrics/arial').
    2. Extract from font files: Use @capsizecss/unpack to extract metrics directly from a font file using fromFile.
    3. Manual lookup: Use the Capsize website to find metrics by selecting a font and referencing the Metrics tab.
    import { fromFile } from '@capsizecss/unpack';
    
    const metrics = await fromFile(filePath);
  3. Extract font metrics using @capsizecss/unpack

    master

    The @capsizecss/unpack package provides several asynchronous methods to resolve font metrics from different data sources. All methods accept an optional options object.

    fromBuffer

    Takes a buffer and returns the resolved font metrics.

    import { fromBuffer } from '@capsizecss/unpack';
    
    const metrics = await fromBuffer(buffer);

    fromBlob

    Takes a file blob and returns the resolved font metrics.

    import { fromBlob } from '@capsizecss/unpack';
    
    const metrics = await fromBlob(file);

    fromUrl

    Takes a URL string and returns the resolved font metrics.

    import { fromUrl } from '@capsizecss/unpack';
    
    const metrics = await fromUrl(url);

    fromFile

    Takes a file path string and returns the resolved font metrics. Note that this is imported from @capsizecss/unpack/fs.

    import { fromFile } from '@capsizecss/unpack/fs';
    
    const metrics = await fromFile(filePath);
  4. Import specific font variants

    master

    You can import specific font weights and styles using a path convention that follows Google Fonts. The format is @capsizecss/metrics/{font-family}/{weight}{style}.

    Variant naming conventions:

    • Standalone weight or style (e.g., regular, italic)
    • Numeric weight (e.g., 100 to 900)
    • Combination (e.g., 700italic)

    Note:

    • Importing the top-most path (e.g., @capsizecss/metrics/arial) returns the regular variant.
    • For Google Fonts without a regular variant, the first available variant is returned.
    • Only available variants for a specific font are included.
    import arialRegular from '@capsizecss/metrics/arial/regular';
    import arialItalic from '@capsizecss/metrics/arial/italic';
    import arialBold from '@capsizecss/metrics/arial/700';
    import arialBoldItalic from '@capsizecss/metrics/arial/700italic';
  5. Install @capsizecss/unpack for custom fonts

    master

    If you are using a custom font or a font that is not included in the @capsizecss/metrics package, use @capsizecss/unpack. This package allows you to extract the necessary font metrics data either from a local file or via a URL.

    npm install @capsizecss/unpack
  6. Create responsive typography

    master

    You can create responsive typography by passing a vanilla-extract media query object as the second argument to createTextStyle. The first argument should be the base (mobile) typography definition.

    // Text.css.ts
    import { createTextStyle } from '@capsizecss/vanilla-extract';
    
    const fontMetrics = {
      capHeight: 700,
      ascent: 1058,
      descent: -291,
      lineGap: 0,
      unitsPerEm: 1000,
    };
    
    const textDefinitions = {
      mobile: { fontSize: 18, leading: 24, fontMetrics },
      tablet: { fontSize: 16, leading: 22, fontMetrics },
      desktop: { fontSize: 14, leading: 18, fontMetrics },
    };
    
    export const text = createTextStyle(textDefinitions.mobile, {
      '@media': {
        'screen and (min-width: 768px)': textDefinitions.tablet,
        'screen and (min-width: 1024px)': textDefinitions.desktop,
      },
    });
  7. Install @capsizecss/metrics for font data

    master

    Capsize provides the @capsizecss/metrics package to simplify the retrieval of font metrics. It contains the required data for both system fonts and Google fonts, allowing you to use Capsize without manually extracting font metrics.

    npm install @capsizecss/metrics