How to use the fontkit.create function in fontkit

To help you get started, we’ve selected a few fontkit examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github assetgraph / assetgraph / lib / transforms / subsetFonts.js View on Github external
fontUsage.props['font-display'] = fontDisplay;
        }
      }
    }

    // Generate codepoint sets for original font, the used subset and the unused subset
    for (const htmlAssetTextWithProps of htmlAssetTextsWithProps) {
      for (const fontUsage of htmlAssetTextWithProps.fontUsages) {
        const originalFont = assetGraph.findAssets({
          url: fontUsage.fontUrl
        })[0];
        if (originalFont.isLoaded) {
          let originalCodepoints;
          try {
            // Guard against 'Unknown font format' errors
            originalCodepoints = fontkit.create(originalFont.rawSrc)
              .characterSet;
          } catch (err) {}
          if (originalCodepoints) {
            const usedCodepoints = fontUsage.text
              .split('')
              .map(c => c.codePointAt(0));
            const unusedCodepoints = originalCodepoints.filter(
              n => !usedCodepoints.includes(n)
            );

            fontUsage.codepoints = {
              original: originalCodepoints,
              used: usedCodepoints,
              unused: unusedCodepoints
            };
          }
github Munter / subfont / lib / subsetFonts.js View on Github external
htmlAsset: htmlAsset.urlOrDescription,
        fontUsages: fontUsages
      }))
    };
  }

  // Generate subsets:
  await getSubsetsForFontUsage(assetGraph, htmlAssetTextsWithProps, formats);

  // Warn about missing glyphs
  const missingGlyphsErrors = [];

  for (const { htmlAsset, fontUsages } of htmlAssetTextsWithProps) {
    for (const fontUsage of fontUsages) {
      if (fontUsage.subsets) {
        const characterSet = fontkit.create(Object.values(fontUsage.subsets)[0])
          .characterSet;

        for (const char of [...fontUsage.pageText]) {
          // Turns out that browsers don't mind that these are missing:
          if (char === '\t' || char === '\n') {
            continue;
          }

          const codePoint = char.codePointAt(0);

          const isMissing = !characterSet.includes(codePoint);

          if (isMissing) {
            let location;
            const charIdx = htmlAsset.text.indexOf(char);
github assetgraph / assetgraph / lib / util / fonts / downloadGoogleFonts.js View on Github external
});

      fontRelation.node.each(decl => {
        if (decl.prop !== 'src') {
          result[decl.prop] = decl.value;
        }
      });

      return [fontRelation.to, fontRelation.format];
    })
  );

  if (!('unicode-range' in result)) {
    const font = result.src[0][0];
    result['unicode-range'] = unicodeRange(
      fontkit.create(font.rawSrc).characterSet
    );
  }

  result['font-display'] = fontDisplay;

  // Output font face declaration object as CSS
  const declarationStrings = [];

  for (const [property, value] of Object.entries(result)) {
    if (property !== 'src') {
      declarationStrings.push(`  ${property}: ${value};`);
    }
  }

  const sources = result.src.map(([font, format]) => {
    return `url('${font.dataUrl}') format('${format}')`;
github qti3e / slye / core / three / font.ts View on Github external
private async ensure(): Promise {
    if (this.font) return;
    const data = await this.file.load();
    const buffer = new Buffer(data); // WTF! I don't like Buffer.
    this.font = fontkit.create(buffer);
  }
github assetgraph / assetgraph / lib / transforms / subsetFonts.js View on Github external
};
          }
        }
      }
    }

    // Generate subsets:
    await getSubsetsForFontUsage(assetGraph, htmlAssetTextsWithProps, formats);

    // Warn about missing glyphs
    const missingGlyphsErrors = [];

    for (const { htmlAsset, fontUsages } of htmlAssetTextsWithProps) {
      for (const fontUsage of fontUsages) {
        if (fontUsage.subsets) {
          const characterSet = fontkit.create(fontUsage.subsets.woff)
            .characterSet;

          for (const char of [...fontUsage.pageText]) {
            // Turns out that browsers don't mind that these are missing:
            if (char === '\t' || char === '\n') {
              continue;
            }

            const codePoint = char.codePointAt(0);

            const isMissing = !characterSet.includes(codePoint);

            if (isMissing) {
              let location;
              const charIdx = htmlAsset.text.indexOf(char);
github jest-community / jest-snapshots-svg / src / font-loader.ts View on Github external
export const addFontToSvg = (fontPath: string, style: FontStyle = {}) => {
  const resolvedPath = require.resolve(fontPath)
  const fontFile = fs.readFileSync(resolvedPath)
  const font = fontkit.create(fontFile, style.postscriptName)
  const key = getFontKey(font, style)
  svgFonts[key] = { path: resolvedPath, style: getFontStyle(font, style) }
  if (font.fonts) {
    font.fonts.forEach(f => addFont(f, { fontFamily: style.fontFamily }))
  } else {
    addFont(font, style)
  }
}
github foliojs / pdfkit / lib / font_factory.js View on Github external
static open(document, src, family, id) {
    let font;
    if (typeof src === 'string') {
      if (StandardFont.isStandardFont(src)) {
        return new StandardFont(document, src, id);
      }

      src = fs.readFileSync(src);
    }
    if (Buffer.isBuffer(src)) {
      font = fontkit.create(src, family);
    } else if (src instanceof Uint8Array) {
      font = fontkit.create(new Buffer(src), family);
    } else if (src instanceof ArrayBuffer) {
      font = fontkit.create(new Buffer(new Uint8Array(src)), family);
    }

    if (font == null) {
      throw new Error('Not a supported font format or standard PDF font.');
    }

    return new EmbeddedFont(document, font, id);
  }
}
github jest-community / jest-snapshots-svg / src / font-loader.ts View on Github external
export const loadFont = (fontFile, style: FontStyle = {}) => {
  const font = fontkit.create(fontFile, style.postscriptName)
  if (font.fonts) {
    font.fonts.forEach(f => addFont(f, { fontFamily: style.fontFamily }))
  } else {
    addFont(font, style)
  }
}
github foliojs / pdfkit / lib / font_factory.js View on Github external
static open(document, src, family, id) {
    let font;
    if (typeof src === 'string') {
      if (StandardFont.isStandardFont(src)) {
        return new StandardFont(document, src, id);
      }

      src = fs.readFileSync(src);
    }
    if (Buffer.isBuffer(src)) {
      font = fontkit.create(src, family);
    } else if (src instanceof Uint8Array) {
      font = fontkit.create(new Buffer(src), family);
    } else if (src instanceof ArrayBuffer) {
      font = fontkit.create(new Buffer(new Uint8Array(src)), family);
    }

    if (font == null) {
      throw new Error('Not a supported font format or standard PDF font.');
    }

    return new EmbeddedFont(document, font, id);
  }
}

fontkit

An advanced font engine for Node and the browser

MIT
Latest version published 4 months ago

Package Health Score

78 / 100
Full package analysis