View source
<?php
function google_fonts_api_fontyourface_info() {
$info = array(
'name' => 'Google',
'url' => 'http://code.google.com/webfonts',
'base_path' => 'http://www.google.com/webfonts/family?family=',
);
return $info;
}
function google_fonts_api_fontyourface_preview($font, $text = NULL, $size = 18) {
$output = '';
if ($text == NULL) {
$text = $font->name;
}
if ($size == 'all') {
$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 {
$output = '<span style="' . fontyourface_font_css($font) . ' font-size: ' . $size . 'px; line-height: ' . $size . 'px;">' . $text . '</span>';
}
return $output;
}
function google_fonts_api_preprocess_html(&$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[$path_parts[0]][$metadata['subset']] = $metadata['subset'];
$all_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]] = $path_parts[1];
}
else {
$paths[$path_parts[0]]['regular'] = 'regular';
}
}
}
if (count($paths) > 0) {
$families = array();
foreach ($paths as $family => $variants) {
$families[$family] = urlencode($family) . ':' . implode(',', $variants);
}
if (module_exists('google_webfont_loader_api')) {
foreach ($families as $family => $family_with_variants) {
$font_info = array(
'name' => 'Google ' . $family,
'google_families' => array(
$family_with_variants . ':' . implode(',', $subsets[$family]),
),
);
_google_webfont_loader_api_load_font($font_info);
}
}
else {
$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(',', $all_subsets);
fontyourface_add_css_in_preprocess($vars, $url, 'remote');
}
}
}
}
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'));
fontyourface_log('drupal_http_request response: @response', array(
'@response' => print_r($result, TRUE),
));
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);
fontyourface_log('google_fonts_api_fontyourface_import JSON: @json', array(
'@json' => print_r($json_results, TRUE),
));
$fonts = _google_fonts_api_convert_api_results($json_results->items);
fontyourface_log('google_fonts_api_fontyourface_import fonts: @fonts', array(
'@fonts' => print_r($fonts, TRUE),
));
}
foreach ($fonts as $font) {
if (!isset($font->tags)) {
$font->tags = array();
}
fontyourface_save_font($font);
}
return $success;
}
function google_fonts_api_views_api() {
if (module_exists('fontyourface_ui')) {
return array(
'api' => 2,
'path' => drupal_get_path('module', 'google_fonts_api') . '/views',
);
}
}
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,
));
$tag_object = new StdClass();
$tag_object->type = 'subset';
$tag_object->name = $json_font_subset;
$fonts[$font_id]->tags[] = $tag_object;
}
}
}
return $fonts;
}