All files / src font-family.ts

78.18% Statements 43/55
100% Branches 0/0
0% Functions 0/2
78.18% Lines 43/55

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 551x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x       1x 1x 1x 1x       1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x       1x 1x 1x 1x      
import ttfMeta from './ttfmeta/lib/index.js';
 
/**
 * Extracts the font-family from an in-memory TTF or WOFF blob in `source`
 * parameter.
 *
 * @param source   In-memory TTF or WOFF font blob
 *
 * @throws Uncaught exceptions from ttfMeta.promise if the font file is invalid.
 *
 * @returns If the file is invalid or cannot be parsed, returns `null`,
 *          otherwise returns the font family as a string.
 */
export async function getFontFamily(source: Uint8Array) {
  /* c8 ignore next 3 */
  if(!source) {
    return null;
  }

  const buffer = Buffer.from(source);
  const font = await ttfMeta.promise(buffer);
  /* c8 ignore next 3 */
  if(!font) {
    return null;
  }

  return font.meta.property.find(prop => prop.name == 'font-family')?.text ?? null;
}
 
/**
 * Extracts the font-family from an in-memory TTF or WOFF blob in `source`
 * parameter.
 *
 * @param source   In-memory TTF or WOFF font blob
 *
 * @throws Uncaught exceptions from ttfMeta.promise if the font file is invalid.
 *
 * @returns If the file is invalid or cannot be parsed, returns `null`,
 *          otherwise returns the font family as a string.
 */
export function getFontFamilySync(source: Uint8Array) {
  /* c8 ignore next 3 */
  if(!source) {
    return null;
  }

  const buffer = Buffer.from(source);
  const font = ttfMeta.ttfInfoSync(buffer);
  /* c8 ignore next 3 */
  if(!font) {
    return null;
  }

  return font.meta.property.find(prop => prop.name == 'font-family')?.text ?? null;
}