You are here

google_fonts_api.module in @font-your-face 7.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',
    'base_path' => 'http://www.google.com/webfonts/family?family=',
  );
  return $info;
}

// google_fonts_api_fontyourface_info

/**
 * Implements hook_fontyourface_preview().
 */
function google_fonts_api_fontyourface_preview($font, $text = NULL, $size = 18) {
  $output = '';
  if ($text == NULL) {
    $text = $font->name;
  }

  // if
  if ($size == 'all') {

    // Display variety of sizes.
    $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
  }
  else {

    // Display single size.
    $output = '<span style="' . fontyourface_font_css($font) . ' font-size: ' . $size . 'px; line-height: ' . $size . 'px;">' . $text . '</span>';
  }

  // else
  return $output;
}

// google_fonts_api_fontyourface_preview

/**
 * Implements template_preprocess_html().
 */
function google_fonts_api_preprocess_html(&$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[$path_parts[0]][$metadata['subset']] = $metadata['subset'];
        $all_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]] = $path_parts[1];
        }
        else {
          $paths[$path_parts[0]]['regular'] = 'regular';
        }

        // else
      }

      // if
    }

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

      // foreach
      if (module_exists('google_webfont_loader_api')) {
        foreach ($families as $family => $family_with_variants) {
          $font_info = array(
            'name' => 'Google ' . $family,
            'google_families' => array(
              $family_with_variants . ':' . implode(',', $subsets[$family]),
            ),
          );
          _google_webfont_loader_api_load_font($font_info);
        }

        // foreach
      }
      else {
        $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) . '&subset=' . implode(',', $all_subsets);
        fontyourface_add_css_in_preprocess($vars, $url, 'remote');
      }

      // else
    }

    // if
  }

  // if
}

// google_fonts_api_preprocess_html

/**
 * 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'));
  fontyourface_log('drupal_http_request response: @response', array(
    '@response' => print_r($result, TRUE),
  ));
  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);
    fontyourface_log('google_fonts_api_fontyourface_import JSON: @json', array(
      '@json' => print_r($json_results, TRUE),
    ));
    $fonts = _google_fonts_api_convert_api_results($json_results->items);
    fontyourface_log('google_fonts_api_fontyourface_import fonts: @fonts', array(
      '@fonts' => print_r($fonts, TRUE),
    ));
  }

  //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

/**
 * Implements hook_views_api().
 */
function google_fonts_api_views_api() {
  if (module_exists('fontyourface_ui')) {
    return array(
      'api' => 2,
      'path' => drupal_get_path('module', 'google_fonts_api') . '/views',
    );
  }

  // if
}

// google_fonts_api_views_api

/**
 * 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,
        ));
        $tag_object = new StdClass();
        $tag_object->type = 'subset';
        $tag_object->name = $json_font_subset;
        $fonts[$font_id]->tags[] = $tag_object;
      }

      //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_preprocess_html Implements template_preprocess_html().
google_fonts_api_views_api Implements hook_views_api().
_google_fonts_api_convert_api_results Converts the Google Fonts API JSON results to a generic Fonts object array