cufon.module in Cufón 6
Same filename and directory in other branches
Adds simple Cufón support to Drupal.
Please read README.txt for installation and configuration instructions.
File
cufon.moduleView source
<?php
/**
* @file
* Adds simple Cufón support to Drupal.
*
* Please read README.txt for installation and configuration instructions.
*/
/**
* Implementation of hook_perm().
*/
function cufon_perm() {
return array(
'administer cufon',
);
}
/**
* Implementation of hook_menu().
*/
function cufon_menu() {
$items = array();
$items['admin/settings/cufon'] = array(
'title' => 'Cufón Settings',
'description' => 'Adds cufon support to Drupal',
'access arguments' => array(
'administer cufon',
),
'page callback' => 'drupal_get_form',
'page arguments' => array(
'cufon_admin',
),
'file' => 'cufon.admin.inc',
);
return $items;
}
/**
* _cufon_find_fonts().
*
* Find all installed Cufon fonts.
*
* - <current_theme>
* - profiles/<profile>/libraries/cufon-fonts/
* - sites/all/libraries/cufon-fonts/
* - sites/<current_site>/libraries/cufon-fonts/
*
*/
function _cufon_find_fonts() {
global $language;
$pattern = '.*\\.js$';
$conf_path = conf_path();
// Search for fonts in this order:
// - theme folders (only enabled themes)
// - site library folder
// - if you want to use different fonts for different languages put them into <langcode>/ subfolder
// - default font MUST exist!
$files = array();
foreach (list_themes() as $theme) {
if ($theme->status === '0') {
continue;
}
$files += file_scan_directory(drupal_get_path('theme', $theme->name), $pattern);
}
$files += drupal_system_listing('\\.js$', 'libraries/cufon-fonts', 'name', 0);
$fonts = array();
foreach ($files as $file) {
// Check for language specific font
if (!empty($language)) {
$f = dirname($file->filename) . '/' . $language->language . '/' . $file->filename;
if (file_exists($f)) {
$file->uri = $f;
}
}
// Resolve font families...
$families = _cufon_get_font_families($file->filename);
if (empty($families)) {
continue;
}
foreach ($families as $family) {
// ... and keep complete file info if not set already
if (!isset($fonts[$family])) {
$fonts[$family] = $file;
}
}
}
return $fonts;
}
/**
* _cufon_get_font_families().
*
* Extracts "font-family" parameter from file contents
*
* Returns: array of found font families.
*/
function _cufon_get_font_families($file) {
$file_content = file_get_contents($file);
if (preg_match_all('/[\'"]font-family[\'"]\\s*:\\s*([\'"]([^"\\r\\n]+)[\'"])/', $file_content, $matches)) {
return $matches[2];
}
return array();
}
/**
* _cufon_parse_css_properties().
*
* Parses out all extra effects added to the selector. Example:
* CSS Format: <selector> { <effect>:<value>; <effect>:<value>; }
* h2 { textShadow:0 1px #fff; color:-linear-gradient(white, #990000); };
*/
function _cufon_parse_css_properties(&$selectors) {
foreach ($selectors as &$selector) {
$a = explode('{', $selector['selector']);
if (count($a) > 1) {
$new = array();
$new['selector'] = $a[0];
$new['options'] = $selector['options'];
$p = explode(';', str_replace(array(
' ',
'}',
), array(
' ',
'',
), $a[1]));
foreach ($p as $property) {
$parts = explode(':', $property);
if (trim($parts[0]) == '' || trim($parts[1]) == '') {
continue;
}
$new['options'][trim($parts[0])] = trim($parts[1]);
}
$selector = $new;
}
}
}
/**
* Implementation of hook_theme_registry_alter().
*/
function cufon_theme_registry_alter(&$theme_registry) {
if (isset($theme_registry['page'])) {
if (!empty($theme_registry['page']['preprocess functions'])) {
if ($key = array_search('cufon_preprocess_page', $theme_registry['page']['preprocess functions'])) {
unset($theme_registry['page']['preprocess functions'][$key]);
}
}
// Run before js is added before template_preprocess_page().
array_unshift($theme_registry['page']['preprocess functions'], 'cufon_preprocess_page');
}
}
/**
* Implementation of hook_preprocess_page().
*/
function cufon_preprocess_page(&$vars) {
$selectors = variable_get('cufon_selectors', array());
foreach ($selectors as $id => $selector) {
if (empty($selector['selector'])) {
unset($selectors[$id]);
}
}
// Include only used fonts.
foreach (_cufon_find_fonts() as $family => $font) {
foreach ($selectors as $selector) {
if ($family == $selector['options']['fontFamily']) {
drupal_add_js($font->filename, 'file', 'header');
break;
}
}
}
_cufon_parse_css_properties($selectors);
drupal_add_js(array(
'cufonSelectors' => $selectors,
), 'setting');
$path = drupal_get_path('module', 'cufon');
static $cufonLib;
// search in libraries folders for cufon-yui.js library.
$cufonLib = drupal_system_listing('cufon-yui.js', 'libraries');
if (!isset($cufonLib['cufon-yui'])) {
// try to load local cufon-yui - for backwards compatibility
if (file_exists($path . '/js/cufon-yui.js')) {
drupal_add_js($path . '/js/cufon-yui.js', 'module');
}
}
else {
// load from library
drupal_add_js($cufonLib['cufon-yui']->filename, 'module');
}
drupal_add_js($path . '/js/cufon-drupal.js', 'module', 'footer');
}
Functions
Name | Description |
---|---|
cufon_menu | Implementation of hook_menu(). |
cufon_perm | Implementation of hook_perm(). |
cufon_preprocess_page | Implementation of hook_preprocess_page(). |
cufon_theme_registry_alter | Implementation of hook_theme_registry_alter(). |
_cufon_find_fonts | _cufon_find_fonts(). |
_cufon_get_font_families | _cufon_get_font_families(). |
_cufon_parse_css_properties | _cufon_parse_css_properties(). |