Provider hooks

Provider hooks should be implemented by modules providing font data to @font-your-face.

hook_fontyourface_info()

This function notifies fontyourface that a provider exists, and provides a name and URL in an array.

function example_fontyourface_info() {

  return array(
    'name' => 'Example provider',
    'url' => 'http://example.com/',
    'base_path' => 'http://example.com/webfonts/family=',
  );

}

hook_fontyourface_import()

Most font providers import font information from another site. This hook is called to begin that import process. A batch process is recommended for any import process likely to take more than a few seconds.

fontyourface_save_font($font)

This should be called on import. It will handle updating fonts that were already imported (based on URL) so providers don't need to worry about that. A full font object should be passed. Example:

$font = new stdClass;
$font->name = 'Font name';
$font->url = 'http://example.com/font-name'; // This must be unique. It's the GUID.
$font->provider = 'example'; // This is the module name.
$font->metadata = serialize($metadata); // Anything can go in here
$font->css_family = "'FontFamily'";
$font->css_weight = 'bold'; // optional
$font->css_style = 'italic'; // optional
$font->foundry = 'Foundry, Inc.'; // optional
$font->foundry_url = 'http://example.com/foundry-inc'; // optional
$font->tags = array('serif', 'fun');

fontyourface_save_font($font);

template_preprocess_page()

Any provider that needs to add CSS or anything else to a page for specific fonts should implement this hook. The fontyourface module implements this and will have populated $vars['fontyourface'] with an array of active fonts. Providers should check the provider attribute of each font to see if action is required.

function example_preprocess_page(&$vars) {

  if (!empty($vars['fontyourface'])) {

    foreach ($vars['fontyourface'] as $active_font) {

      if ($active_font->provider == 'example') {

        // Do something here.

      }

    }

  }

}

fontyourface_add_css_in_preprocess(&$vars, $css_path, $type = 'public')

Because most providers will be adding CSS in preprocess, this function exists to simplify the process. $type can be public, private, or remote. public CSS is in the CSS files are within the files directory, private are anywhere else in Drupal, and remote are on an external server.

hook_fontyourface_preview($font, $text, $size)

This hook will be called when a preview of the font is needed. $size will be either a number, indicating pixel size, or 'all', indicating multiple sized should be shown. CSS font-family will not be applied to the returned HTML, so it should added with inline CSS.

function example_fontyourface_preview($font, $text = NULL, $size = 18) {

  $output = '';

  if ($text == NULL) {
    $text = $font->name;
  }

  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>';

    }

  }
  else {

    // Display single size.

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

  }

  return $output;

}

fontyourface_font_css($font)

This function is useful to get standard CSS for a given font, including font-family, font-weight, and font-style.

hook_fontyourface_enable($font)

Some providers need to do something when a font is being enabled, e.g. notify a remote API. If this hook returns FALSE, the font will not be enabled.

hook_fontyourface_disable($font)

Same as above, but for disabling.

hook_fontyourface_selector_update($font)

Called when a font's selector is changed.

hook_fontyourface_short_preview_text($font)

By default, fonts are previewed with AaGg. If a font supports non-Latin languages, it may be useful to change this text to a few characters in the appropriate language.

hook_fontyourface_status()

This hook allows providers to supply their own status text for screens that list provider status.

fontyourface_log($message, $arguments)

@font-your-face has an option to log detailed debugging info. This function will check if that's enabled and create an appropriate watchdog() message if it is.

Other API functions

fontyourface_font_registry($font = FALSE, $clear = FALSE)

This function is for activating a font on the current page without enabling it site-wide. It's used in the browse interface, for example. It will also return all fonts that are already active for the current page.

fontyourface_get_fonts($where = "enabled = 1 AND css_selector != ''", $order_by = 'name ASC')

This is for getting lists of fonts. The default parameters will return all enabled fonts.