You are here

google_fonts_api.module in @font-your-face 6.2

File

modules/google_fonts_api/google_fonts_api.module
View source
<?php

/**
 * Implements hook_fontyourface_info().
 */
function google_fonts_api_fontyourface_info() {
  $info = array(
    'name' => 'Google',
    'url' => 'http://code.google.com/webfonts',
  );
  return $info;
}

// google_fonts_api_fontyourface_info

/**
 * Implements hook_fontyourface_preview().
 */
function google_fonts_api_fontyourface_preview($font) {
  return '<span style="' . fontyourface_font_css($font) . ' font-size: 24px;">' . $font->name . '</span>';
}

// google_fonts_api_fontyourface_preview

/**
 * Implements hook_fontyourface_view().
 */
function google_fonts_api_fontyourface_view($font, $text) {
  $output = '';
  $sizes = array(
    32,
    24,
    18,
    14,
    12,
    10,
  );
  foreach ($sizes as $size) {
    $output .= '<div style="' . fontyourface_font_css($font) . ' font-size: ' . $size . 'px; line-height: ' . $size . 'px;">' . $text . '</div>';
  }

  // foreach
  return $output;
}

// google_fonts_api_fontyourface_preview

/**
 * Implements template_preprocess_page().
 */
function google_fonts_api_preprocess_page(&$vars) {
  if (!empty($vars['fontyourface'])) {
    $paths = array();
    $subsets = array();
    foreach ($vars['fontyourface'] as $used_font) {
      if ($used_font->provider == 'google_fonts_api') {
        $metadata = unserialize($used_font->metadata);
        $path_parts = explode(':', $metadata['path']);
        $subsets[$metadata['subset']] = $metadata['subset'];
        if (!isset($paths[$path_parts[0]])) {
          $paths[$path_parts[0]] = array();
        }

        // if
        if (count($path_parts) > 1) {
          $paths[$path_parts[0]][] = $path_parts[1];
        }
        else {
          $paths[$path_parts[0]][] = 'regular';
        }

        // else
      }

      // if
    }

    // foreach
    if (count($paths) > 0) {
      $families = array();
      foreach ($paths as $family => $variants) {
        $families[] = $family . ':' . implode(',', $variants);
      }

      // foreach
      $base = 'http://fonts.googleapis.com/css?family=';
      if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') {
        $base = 'https://fonts.googleapis.com/css?family=';
      }

      // if
      $url = $base . implode('|', $families) . '&amp;subset=' . implode(',', $subsets);
      fontyourface_add_css_in_preprocess($vars, $url, 'remote');
      if (module_exists('google_webfont_loader_api')) {
        foreach ($families as $family) {
          $font_info = array(
            'name' => 'Google ' . $family,
            'google_families' => array(
              $family,
            ),
          );
          _google_webfont_loader_api_load_font($font_info);
        }

        // foreach
      }

      // if
    }

    // if
  }

  // if
}

// google_fonts_api_preprocess_page

/**
 * Implements hook_fontyourface_import().
 */
function google_fonts_api_fontyourface_import() {

  // Return the JSON object with all available fonts
  // For now, it uses the API key of BarisW (co-maintainer of this module)
  // This key is limited to 10.000 requests per day, which should
  // be sufficient as it is only used when selecting fonts in the
  // admin interface. After that, the fonts are cached in Drupal.
  $success = TRUE;
  $fonts = array();
  $result = drupal_http_request('https://www.googleapis.com/webfonts/v1/webfonts?key=' . variable_get('google_fonts_api_key', 'AIzaSyBgeqKlFdYj3Y7VwmrEXnXzpnx5TfKXG4o'));
  if ($result->code != 200) {
    $success = FALSE;
    drupal_set_message(t('The list of Google Fonts could not be fetched. Verify that your server can connect the Google Servers (https://www.googleapis.com). Error: %error', array(
      '%error' => $result->error,
    )), 'error');
  }
  elseif (isset($result->data)) {
    $json_results = json_decode($result->data);
    $fonts = _google_fonts_api_convert_api_results($json_results->items);
  }

  //elseif
  foreach ($fonts as $font) {
    if (!isset($font->tags)) {
      $font->tags = array();
    }

    // if
    fontyourface_save_font($font);
  }

  // foreach
  return $success;
}

// google_fonts_api_fontyourface_import

/**
 * Converts the Google Fonts API JSON results to a generic Fonts object array
 *
 * @param $json_font_list: Array of Font objects
 *   
 */
function _google_fonts_api_convert_api_results($json_font_list) {
  $fonts = array();
  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 numberic 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);
            }
        }

        //switch
        $fonts[$font_id] = new stdClass();
        $fonts[$font_id]->name = $font_id;
        $fonts[$font_id]->url = 'http://www.google.com/webfonts/family?family=' . $json_font->family . '&subset=' . $json_font_subset . '#' . $json_font_variant;
        $fonts[$font_id]->provider = 'google_fonts_api';
        $fonts[$font_id]->css_family = $json_font->family;
        $fonts[$font_id]->css_style = $css_style;
        $fonts[$font_id]->css_weight = $css_weight;
        $fonts[$font_id]->foundry = '';
        $fonts[$font_id]->foundry_url = '';
        $fonts[$font_id]->license = '';
        $fonts[$font_id]->license_url = '';
        $fonts[$font_id]->metadata = serialize(array(
          'path' => $json_font->family . ':' . $json_font_variant,
          'subset' => $json_font_subset,
        ));
        $fonts[$font_id]->tags = $json_font->subsets;
      }

      //foreach
    }

    //foreach
  }

  //foreach
  return $fonts;
}

// _google_fonts_api_convert_api_results

Functions

Namesort descending Description
google_fonts_api_fontyourface_import Implements hook_fontyourface_import().
google_fonts_api_fontyourface_info Implements hook_fontyourface_info().
google_fonts_api_fontyourface_preview Implements hook_fontyourface_preview().
google_fonts_api_fontyourface_view Implements hook_fontyourface_view().
google_fonts_api_preprocess_page Implements template_preprocess_page().
_google_fonts_api_convert_api_results Converts the Google Fonts API JSON results to a generic Fonts object array