lib/transforms/subsetFonts.js
function groupTextsByFontFamilyProps(
  textByPropsArray,
  availableFontFaceDeclarations
) {
  const snappedTexts = _.flatMapDeep(textByPropsArray, textAndProps => {
    const family = textAndProps.props['font-family'];
    if (family === undefined) {
      return [];
    }

    // Find all the families in the traced font-family that we have @font-face declarations for:
    const families = fontFamily
      .parse(family)
      .filter(family =>
        availableFontFaceDeclarations.some(
          fontFace =>
            fontFace['font-family'].toLowerCase() === family.toLowerCase()
        )
      );

    return families.map(family => {
      const activeFontFaceDeclaration = fontSnapper(
        availableFontFaceDeclarations,
        {
          ...textAndProps.props,
          'font-family': fontFamily.stringify([family])
        }
      );

      if (!activeFontFaceDeclaration) {
        return [];
      }

      const { relations, ...props } = activeFontFaceDeclaration;
      const fontUrl = getPreferredFontUrl(relations);

      return {
        text: textAndProps.text,
        props,
        fontRelations: relations,
        fontUrl
      };
    });
  }).filter(textByProps => textByProps && textByProps.fontUrl);

  const textsByFontUrl = _.groupBy(snappedTexts, 'fontUrl');

  return _.map(textsByFontUrl, (textsPropsArray, fontUrl) => {
    const texts = textsPropsArray.map(obj => obj.text);
    const fontFamilies = new Set(
      textsPropsArray.map(obj => obj.props['font-family'])
    );
    const pageText = _.uniq(texts.join(''))
      .sort()
      .join('');

    let smallestOriginalSize;
    let smallestOriginalFormat;
    for (const relation of textsPropsArray[0].fontRelations) {
      if (relation.to.isLoaded) {
        const size = relation.to.rawSrc.length;
        if (smallestOriginalSize === undefined || size < smallestOriginalSize) {
          smallestOriginalSize = size;
          smallestOriginalFormat = relation.to.type.toLowerCase();
        }
      }
    }

    return {
      smallestOriginalSize,
      smallestOriginalFormat,
      texts,
      pageText,
      text: pageText,
      props: { ...textsPropsArray[0].props },
      fontUrl,
      fontFamilies
    };
  });
}
function groupTextsByFontFamilyProps(
  textByPropsArray,
  availableFontFaceDeclarations
) {
  const snappedTexts = _.flatMapDeep(textByPropsArray, textAndProps => {
    const family = textAndProps.props['font-family'];
    if (family === undefined) {
      return [];
    }

    // Find all the families in the traced font-family that we have @font-face declarations for:
    const families = fontFamily
      .parse(family)
      .filter(family =>
        availableFontFaceDeclarations.some(
          fontFace =>
            fontFace['font-family'].toLowerCase() === family.toLowerCase()
        )
      );

    return families.map(family => {
      const activeFontFaceDeclaration = fontSnapper(
        availableFontFaceDeclarations,
        {
          ...textAndProps.props,
          'font-family': fontFamily.stringify([family])
        }
      );

      if (!activeFontFaceDeclaration) {
        return [];
      }

      const { relations, ...props } = activeFontFaceDeclaration;
      const fontUrl = getPreferredFontUrl(relations);

      return {
        text: textAndProps.text,
        props,
        fontRelations: relations,
        fontUrl
      };
    });
  }).filter(textByProps => textByProps && textByProps.fontUrl);

  const textsByFontUrl = _.groupBy(snappedTexts, 'fontUrl');

  return _.map(textsByFontUrl, (textsPropsArray, fontUrl) => {
    const texts = textsPropsArray.map(obj => obj.text);
    const fontFamilies = new Set(
      textsPropsArray.map(obj => obj.props['font-family'])
    );
    const pageText = _.uniq(texts.join(''))
      .sort()
      .join('');

    let smallestOriginalSize;
    let smallestOriginalFormat;
    for (const relation of textsPropsArray[0].fontRelations) {
      if (relation.to.isLoaded) {
        const size = relation.to.rawSrc.length;
        if (smallestOriginalSize === undefined || size < smallestOriginalSize) {
          smallestOriginalSize = size;
          smallestOriginalFormat = relation.to.type.toLowerCase();
        }
      }
    }

    return {
      smallestOriginalSize,
      smallestOriginalFormat,
      texts,
      pageText,
      text: pageText,
      props: { ...textsPropsArray[0].props },
      fontUrl,
      fontFamilies
    };
  });
}
function groupTextsByFontFamilyProps ( textByPropsArray , availableFontFaceDeclarations ) { const snappedTexts = _ . flatMapDeep ( textByPropsArray , textAndProps => { const family = textAndProps . props [ 'font-family' ] ; if ( family === undefined ) { return [ ] ; } // Find all the families in the traced font-family that we have @font-face declarations for: const families = fontFamily . parse ( family ) . filter ( family => availableFontFaceDeclarations . some ( fontFace => fontFace [ 'font-family' ] . toLowerCase ( ) === family . toLowerCase ( ) ) ) ; return families . map ( family => { const activeFontFaceDeclaration = fontSnapper ( availableFontFaceDeclarations , { ... textAndProps . props , 'font-family' : fontFamily . stringify ( [ family ] ) } ) ; if ( ! activeFontFaceDeclaration ) { return [ ] ; } const { relations , ... props } = activeFontFaceDeclaration ; const fontUrl = getPreferredFontUrl ( relations ) ; return { text : textAndProps . text , props , fontRelations : relations , fontUrl } ; } ) ; } ) . filter ( textByProps => textByProps && textByProps . fontUrl ) ; const textsByFontUrl = _ . groupBy ( snappedTexts , 'fontUrl' ) ; return _ . map ( textsByFontUrl , ( textsPropsArray , fontUrl ) => { const texts = textsPropsArray . map ( obj => obj . text ) ; const fontFamilies = new Set ( textsPropsArray . map ( obj => obj . props [ 'font-family' ] ) ) ; const pageText = _ . uniq ( texts . join ( '' ) ) . sort ( ) . join ( '' ) ; let smallestOriginalSize ; let smallestOriginalFormat ; for ( const relation of textsPropsArray [ 0 ] . fontRelations ) { if ( relation . to . isLoaded ) { const size = relation . to . rawSrc . length ; if ( smallestOriginalSize === undefined || size < smallestOriginalSize ) { smallestOriginalSize = size ; smallestOriginalFormat = relation . to . type . toLowerCase ( ) ; } } } return { smallestOriginalSize , smallestOriginalFormat , texts , pageText , text : pageText , props : { ... textsPropsArray [ 0 ] . props } , fontUrl , fontFamilies } ; } ) ; }
Takes the output of fontTracer
Takes the output of fontTracer