You are here

function _google_fonts_api_convert_api_results in @font-your-face 8.3

Same name and namespace in other branches
  1. 6.2 modules/google_fonts_api/google_fonts_api.module \_google_fonts_api_convert_api_results()
  2. 7.2 modules/google_fonts_api/google_fonts_api.module \_google_fonts_api_convert_api_results()

Converts the Google Fonts API JSON results to a generic Fonts object array.

Parameters

array $json_font_list: Array of Google Font objects.

Return value

array Array of objects compatible with Fontyourface interface.

1 call to _google_fonts_api_convert_api_results()
google_fonts_api_fontyourface_import in modules/google_fonts_api/google_fonts_api.module
Implements hook_fontyourface_import().

File

modules/google_fonts_api/google_fonts_api.module, line 183
Google Fonts module file.

Code

function _google_fonts_api_convert_api_results(array $json_font_list) {
  $fonts = [];
  foreach ($json_font_list as $json_font) {
    foreach ($json_font->variants as $json_font_variant) {
      foreach ($json_font->subsets as $json_font_subset) {
        $font_id = $json_font->family . ' ' . $json_font_variant . ' (' . $json_font_subset . ')';
        switch ($json_font_variant) {
          case 'regular':
            $css_style = 'normal';
            $css_weight = 'normal';
            break;
          case 'italic':
            $css_style = 'italic';
            $css_weight = 'normal';
            break;
          case 'bold':
            $css_style = 'normal';
            $css_weight = 'bold';
            break;
          case 'bolditalic':
            $css_style = 'italic';
            $css_weight = 'bold';
            break;
          default:

            // For all other cases (eg 400 or 400italic).
            if (is_numeric($json_font_variant)) {

              // Variant is a number, like 400.
              $css_style = 'normal';
              $css_weight = $json_font_variant;
            }
            elseif (is_numeric(substr($json_font_variant, 0, 3))) {

              // Variant is a combined string of number and string, like
              // 400italic.
              // The numeric part is always three characters long, so we can
              // split it easily.
              $css_style = substr($json_font_variant, 3);
              $css_weight = substr($json_font_variant, 0, 3);
            }
        }
        $font = new stdClass();
        $font->name = $font_id;
        $font->url = 'https://www.google.com/webfonts/family?family=' . $json_font->family . '&subset=' . $json_font_subset . '#' . $json_font_variant;
        $font->provider = 'google_fonts_api';
        $font->css_family = $json_font->family;
        $font->css_style = $css_style;
        $font->css_weight = $css_weight;
        $font->designer = '';
        $font->designer_url = '';
        $font->foundry = '';
        $font->foundry_url = '';
        $font->license = '';
        $font->license_url = '';
        $font->classification = [
          $json_font->category,
        ];
        $font->language = [
          $json_font_subset,
        ];
        $font->metadata = [
          'path' => $json_font->family . ':' . $json_font_variant,
          'subset' => $json_font_subset,
        ];
        $fonts[] = $font;
      }
    }
  }
  return $fonts;
}