You are here

function _google_webfont_loader_api_load_js_from_fontinfo in Webfont Loader 7

Same name and namespace in other branches
  1. 8 google_webfont_loader_api.module \_google_webfont_loader_api_load_js_from_fontinfo()
  2. 6 google_webfont_loader_api.module \_google_webfont_loader_api_load_js_from_fontinfo()

Parse a fontinfo description and create a javascript settings loader. This is the real meat of the functionality as fonts need to get added.

1 call to _google_webfont_loader_api_load_js_from_fontinfo()
_google_webfont_loader_api_load_font in ./google_webfont_loader_api.module
Loads font into the webfont stack.

File

./google_webfont_loader_api.module, line 127
Google Webfont Loader API primary file The designer/developer creates a set of packages (will use .fontinfo files created in a similar manner to a module or theme .info file) from which the site admin can then choose for their site. The fonts can…

Code

function _google_webfont_loader_api_load_js_from_fontinfo($font_info, $filepath) {
  $url = file_create_url($filepath);
  if (strpos(strrev($url), '/') !== 0) {
    $url .= '/';
  }
  $loader_js = array();

  // Load all the information from the font info file into the loader_js.
  // loader_js is a settings format recognized by the webfont loader.
  // You can specify your google fonts, typekit id, or custom fonts
  // (with their fontface css locations)
  foreach ($font_info as $key => $key_info) {
    $info = $key_info;
    if (!is_array($key_info)) {
      $info = array(
        $key_info,
      );
    }
    if ($key == 'google_families' && is_array($info)) {
      $loader_js['google'] = array(
        'families' => $info,
      );
    }
    if ($key == "typekit") {
      $loader_js['typekit'] = array(
        'id' => $info[0],
      );
    }
    if ($key == 'fontdeck') {
      $loader_js['fontdeck'] = array(
        'id' => $info[0],
      );
    }
    if ($key == 'monotype') {
      $loader_js['monotype'] = array(
        'projectId' => $info[0],
      );
    }

    // A custom style css must be specified if using custom families.
    if ($key == "custom_families" && is_array($font_info['custom_style_css'])) {
      $loader_js['custom'] = array(
        'families' => $info,
        'urls' => array(),
      );
      foreach ($font_info['custom_style_css'] as $custom_style) {
        if (file_uri_scheme($custom_style)) {
          $loader_js['custom']['urls'][] = file_create_url($custom_style);
        }
        else {
          $loader_js['custom']['urls'][] = $url . $custom_style;
        }
      }
    }

    // If specified, load and use custom test strings.
    if ($key == "custom_families" && isset($font_info['custom_testStrings']) && is_array($font_info['custom_testStrings'])) {
      foreach ($font_info['custom_testStrings'] as $string) {

        /* testString should be added to info file escaped like:
           custom_testStrings[] = '\\uE001\\uE002\\uE838' */
        $str = preg_replace_callback('/\\\\u([0-9a-fA-F]{4})/', function ($match) {
          return mb_convert_encoding(pack('H*', $match[1]), 'UTF-8', 'UTF-16BE');
        }, $string);
        $loader_js['custom']['testStrings'][$info[0]] = $str;
      }
    }
  }
  return google_webfont_loader_api_add_js($loader_js);
}