View source
<?php
function google_fonts_api_fontyourface_info() {
$info = array(
'name' => 'Google',
'url' => 'http://code.google.com/webfonts',
);
return $info;
}
function google_fonts_api_fontyourface_preview($font) {
return '<span style="' . fontyourface_font_css($font) . ' font-size: 24px;">' . $font->name . '</span>';
}
function google_fonts_api_fontyourface_view($font, $text) {
$output = '';
$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>';
}
return $output;
}
function google_fonts_api_preprocess_page(&$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[$metadata['subset']] = $metadata['subset'];
if (!isset($paths[$path_parts[0]])) {
$paths[$path_parts[0]] = array();
}
if (count($path_parts) > 1) {
$paths[$path_parts[0]][] = $path_parts[1];
}
else {
$paths[$path_parts[0]][] = 'regular';
}
}
}
if (count($paths) > 0) {
$families = array();
foreach ($paths as $family => $variants) {
$families[] = $family . ':' . implode(',', $variants);
}
$base = 'http://fonts.googleapis.com/css?family=';
if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') {
$base = 'https://fonts.googleapis.com/css?family=';
}
$url = $base . implode('|', $families) . '&subset=' . implode(',', $subsets);
fontyourface_add_css_in_preprocess($vars, $url, 'remote');
if (module_exists('google_webfont_loader_api')) {
foreach ($families as $family) {
$font_info = array(
'name' => 'Google ' . $family,
'google_families' => array(
$family,
),
);
_google_webfont_loader_api_load_font($font_info);
}
}
}
}
}
function google_fonts_api_fontyourface_import() {
$success = TRUE;
$fonts = array();
$result = drupal_http_request('https://www.googleapis.com/webfonts/v1/webfonts?key=' . variable_get('google_fonts_api_key', 'AIzaSyBgeqKlFdYj3Y7VwmrEXnXzpnx5TfKXG4o'));
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);
$fonts = _google_fonts_api_convert_api_results($json_results->items);
}
foreach ($fonts as $font) {
if (!isset($font->tags)) {
$font->tags = array();
}
fontyourface_save_font($font);
}
return $success;
}
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:
if (is_numeric($json_font_variant)) {
$css_style = 'normal';
$css_weight = $json_font_variant;
}
elseif (is_numeric(substr($json_font_variant, 0, 3))) {
$css_style = substr($json_font_variant, 3);
$css_weight = substr($json_font_variant, 0, 3);
}
}
$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,
));
$fonts[$font_id]->tags = $json_font->subsets;
}
}
}
return $fonts;
}