You are here

function cufon_init in Cufón 7.2

Implementation of hook_init().

File

./cufon.module, line 78
Adds simple Cufón support to Drupal.

Code

function cufon_init() {
  global $user;
  static $cufonLib;
  $remote_url = variable_get('cufon_remote_url', '');

  // If a remote URL is used and it's a valid absolute URL, use that.
  if (strlen($remote_url) > 0) {
    if (valid_url($remote_url, TRUE)) {
      drupal_add_js($remote_url, 'external');
    }
    else {
      drupal_set_message(t('<strong>Cufón:</strong> You have provided an invalid absolute URL to Cufón library. Cufón has not been loaded.'), 'warning');
    }
  }
  else {

    // No remote URL used, so search for a local JS file.
    //
    // Search in theme folder first for cufon-yui.js
    $cufonLib = file_scan_directory(path_to_theme(), '/^cufon-yui.js$/', array(
      'key' => 'name',
    ));
    if (empty($cufonLib)) {

      // Search in module folder for cufon-yui.js
      $cufonLib = file_scan_directory(drupal_get_path('module', 'cufon'), '/^cufon-yui.js$/', array(
        'key' => 'name',
      ));
    }
    if (empty($cufonLib)) {

      // Search in libraries folders for cufon-yui.js
      $cufonLib = drupal_system_listing('/^cufon-yui.js$/', 'libraries/cufon', 'name', 0);
    }
    if (empty($cufonLib)) {

      // Include cufon library
      drupal_add_js($cufonLib['cufon-yui']->uri, 'file');
    }
    if (!isset($cufonLib['cufon-yui'])) {

      // Show message only to cufon administrator(s).
      if (user_access('administer cufon')) {
        drupal_set_message(t('<strong>Cufón:</strong> You must download <a href="http://cufon.shoqolate.com/js/cufon-yui.js">cufon-yui.js</a> and install it to <code>libraries/cufon</code> folder.'), 'error');
      }
      return;
    }
    else {

      // Include cufon library
      drupal_add_js($cufonLib['cufon-yui']->uri, 'file');
    }
  }
  $selectors = variable_get('cufon_selectors', array());

  // Remove disabled selectors
  foreach ($selectors as $id => $selector) {
    if (empty($selector['options']['enabled'])) {
      unset($selectors[$id]);
    }
  }

  // Include only used fonts.
  foreach (_cufon_find_fonts() as $family => $font) {
    foreach ($selectors as $selector) {
      if (!empty($selector['options']['autoDetect']) || $family == $selector['options']['fontFamily']) {
        drupal_add_js($font->uri, array(
          'type' => 'file',
          'scope' => 'header',
        ));
        break;
      }
    }
  }

  // Add settings
  drupal_add_js(array(
    'cufonSelectors' => $selectors,
  ), array(
    'type' => 'setting',
  ));

  // Add behaviour(s)
  drupal_add_js(drupal_get_path('module', 'cufon') . '/js/cufon-drupal.js');

  // Add Cufon.now() to footer but before any other scripts
  drupal_add_js('Cufon.now();', array(
    'type' => 'inline',
    'scope' => 'footer',
    'weight' => -99,
  ));
}