svg2ttf

repository·master·Indexed 19 days ago

https://github.com/fontello/svg2ttf

A utility for converting SVG fonts into the TTF (TrueType Font) format, optimized for generating iconic fonts. It provides both a CLI for direct file conversion and an API to convert SVG font strings into TTF byte buffers with configurable metadata such as copyright, description, and versioning.

Tokens
1.3K
Snippets
5
Records
5
Agent score
69%

What's inside svg2ttf

  1. Install and use svg2ttf via CLI

    master

    You can install svg2ttf globally via npm and use it to convert an SVG font file into a TTF file directly from your terminal.

    Installation:

    npm install -g svg2ttf

    Usage: Provide the source SVG file and the target TTF file path as arguments.

    svg2ttf fontello.svg fontello.ttf
    npm install -g svg2ttf
    svg2ttf fontello.svg fontello.ttf
  2. Use the svg2ttf API

    master

    The svg2ttf function converts an SVG font string into a TTF byte buffer.

    Signature: svg2ttf(svgFontString, options) -> buf

    Parameters:

    • svgFontString: The raw SVG font content as a string.
    • options: An object to configure font metadata:
      • copyright: (optional) Copyright string.
      • description: (optional) Description string.
      • ts: (optional) Unix timestamp (in seconds) to override the creation time.
      • url: (optional) Manufacturer URL.
      • version: (optional) Font version string (e.g., Version x.y or x.y).

    Return Value: Returns a buf object (an internal byte buffer similar to DataView). To get the actual TTF content, access the buffer property, which is a Uint8Array or Array.

    var fs = require('fs');
    var svg2ttf = require('svg2ttf');
    
    // Convert SVG to TTF buffer
    var ttf = svg2ttf(fs.readFileSync('myfont.svg', 'utf8'), {});
    
    // Write the buffer content to a file
    fs.writeFileSync('myfont.ttf', new Buffer(ttf.buffer));
  3. Use the svg2ttf function to convert SVG to TTF

    master

    The svg2ttf function converts an SVG font string into a TTF (TrueType Font) buffer. It accepts an SVG font string and an optional configuration object to customize font metadata, metrics, and versioning.

    Parameters:

    • svgString (String): The input SVG font string.
    • options (Object): Configuration for the generated font.

    Returns:

    • Buffer: A buffer containing the generated TTF content.

    Common Options:

    • id: Font ID.
    • familyname: Font family name.
    • subfamilyname: Font subfamily name (defaults to 'Regular').
    • fullname: Full font name.
    • copyright: Copyright metadata.
    • description: Font description.
    • url: Font URL.
    • version: A string representing the version (must match the pattern ^(\d+[.]\d+)$ or include the prefix Version ).
    • ts: A Unix timestamp (seconds) to set createdDate and modifiedDate.

    Errors:

    • Throws an error if version is not a string.
    • Throws an error if version does not match the expected version format.
    const svg2ttf = require('svg2ttf');
    
    const svgString = '<svg>...</svg>'; // Your SVG font string
    const options = {
      id: 'my-font',
      familyname: 'My Font',
      fullname: 'My Font Regular',
      version: '1.0',
      copyright: '© 2023 My Company'
    };
    
    try {
      const ttfBuffer = svg2ttf(svgString, options);
      // Use the buffer (e.g., write to file)
    } catch (err) {
      console.error('Failed to generate TTF:', err.message);
    }
  4. Configure svg2ttf CLI metadata options

    master

    When using the svg2ttf CLI, you can override the default font metadata using the following flags:

    FlagLong FlagDescription
    -c--copyrightSet the copyright text
    -d--descriptionOverride the default description text
    --ts(none)Override font creation time (Unix time stamp, integer)
    -u--urlOverride the default manufacturer URL
    --vs(none)Override the font version string (e.g., "x.y" or "Version x.y")
    -v--versionDisplay the current version of the tool
    -h--helpDisplay help information
    -c, --copyright      Copyright text
    -d, --description    Override default description text
    --ts                 Override font creation time (Unix time stamp)
    -u, --url            Override default manufacturer url
    --vs                 Override default font version string (Version 1.0), can be "x.y" or "Version x.y"
  5. Use the svg2ttf CLI to convert SVG to TTF

    master

    The svg2ttf CLI tool allows you to convert an SVG font file into a TTF font file from your terminal. The basic usage requires an input file and an output file path.

    Basic Syntax:

    svg2ttf <input.svg> <output.ttf>
    svg2ttf input.svg output.ttf