View source
<?php
function local_fonts_fontyourface_info() {
return array(
'name' => t('Local Fonts'),
'url' => 'http://drupal.org/project/fontyourface#local',
);
}
function local_fonts_menu() {
$items = array();
$items['admin/config/fontyourface/local_fonts'] = array(
'title' => 'Import Local Font',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'local_fonts_add_form',
),
'access arguments' => array(
'administer @font-your-face',
),
);
$items['admin/appearance/fontyourface/local_fonts/delete/%'] = array(
'title' => 'Delete Local Font',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'local_fonts_delete_form',
5,
),
'access arguments' => array(
'administer @font-your-face',
),
'type' => MENU_CALLBACK,
);
return $items;
}
function local_fonts_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == 'fontyourface_settings_form') {
$form['providers']['local_fonts_import']['import'] = array(
'#type' => 'item',
'#markup' => l(t('Import Local Font'), 'admin/config/fontyourface/local_fonts'),
);
}
elseif ($form_id == 'fontyourface_admin_edit_form') {
$form['buttons']['delete'] = array(
'#type' => 'submit',
'#value' => t('Delete font record'),
'#submit' => array(
'local_fonts_delete_button_submit',
),
);
}
}
function local_fonts_delete_button_submit($form, &$form_state) {
$destination = '';
if (isset($_GET['destination'])) {
$destination = drupal_get_destination();
unset($_GET['destination']);
}
$form_state['redirect'] = array(
'admin/appearance/fontyourface/local_fonts/delete/' . $form['fid']['#value'],
$destination,
);
}
function local_fonts_add_form($form, $form_state) {
$form = array();
$form['local_fonts']['information'] = array(
'#type' => 'fieldset',
'#title' => t('Font Information'),
'#collapsible' => FALSE,
);
$form['local_fonts']['information']['css_family'] = array(
'#type' => 'textfield',
'#title' => t('CSS Font Family'),
'#required' => TRUE,
);
$form['local_fonts']['information']['css_style'] = array(
'#type' => 'select',
'#title' => t('CSS Font Style'),
'#options' => array(
'normal' => t('Normal'),
'italic' => t('Italic'),
'oblique' => t('Oblique'),
),
'#required' => TRUE,
);
$form['local_fonts']['information']['css_weight'] = array(
'#type' => 'select',
'#title' => t('CSS Font Weight'),
'#options' => array(
'normal' => t('Normal'),
'bold' => t('Bold'),
'bolder' => t('Bolder'),
'lighter' => t('Lighter'),
'100' => t('100'),
'200' => t('200'),
'300' => t('300'),
'400' => t('400'),
'500' => t('500'),
'600' => t('600'),
'700' => t('700'),
'800' => t('800'),
'900' => t('900'),
),
'#required' => TRUE,
);
$form['local_fonts']['upload']['eot'] = array(
'#type' => 'file',
'#title' => t('EOT font file'),
'#description' => t('Embedded Open Type. This format is supported by IE 5-8.'),
);
$form['local_fonts']['upload']['ttf'] = array(
'#type' => 'file',
'#title' => t('TTF font file'),
'#description' => t('Raw TrueType file, designed to look good on-screen. This format is supported by Webkit/Safari 3.1+, Safari Mobile iOS 4.2+, Opera 10+, Mozilla/Firefox 3.5+, and Chrome 4+'),
);
$form['local_fonts']['upload']['woff'] = array(
'#type' => 'file',
'#title' => t('WOFF font file'),
'#description' => t('Web-only font format that uses gzip compression. This format is supported by IE9+, FF3.6+, Chrome 5+.'),
);
$form['local_fonts']['upload']['svg'] = array(
'#type' => 'file',
'#title' => t('SVG font file'),
'#description' => t('An XML format required by iOS devices before version 4.2. Currently supported by iOS 4.1 and below, and Opera 10+.'),
);
$form = confirm_form($form, t('Import Local Font'), 'admin/config/fontyourface', '');
$form['#attributes'] = array(
'enctype' => 'multipart/form-data',
);
return $form;
}
function local_fonts_add_form_validate($form, &$form_state) {
$values = $form_state['values'];
$font_path = strtr(preg_replace('#[^a-zA-Z0-9]+#', ' ', $values['css_family']), ' ', '_');
$font_style = $values['css_style'];
$font_weight = $values['css_weight'];
$local_fonts_directory = dirname($_SERVER['SCRIPT_FILENAME']) . '/' . file_stream_wrapper_get_instance_by_scheme('public')
->getDirectoryPath() . '/fontyourface/local_fonts';
$destination_directory = $local_fonts_directory . '/' . $font_path;
$file_uploaded = FALSE;
if (file_prepare_directory($local_fonts_directory, FILE_CREATE_DIRECTORY) && file_prepare_directory($destination_directory, FILE_CREATE_DIRECTORY)) {
$files = $_FILES['files']['name'];
foreach ($files as $key => $filename) {
$file_object = new stdClass();
$file_object->filename = $filename;
if (!empty($file_object->filename)) {
$validation = file_validate_extensions($file_object, $key);
if (is_array($validation) && count($validation) > 0) {
form_set_error($key, implode(', ', $validation));
}
else {
$file_uploaded = TRUE;
}
}
}
}
else {
form_set_error('files', t('Error creating file directory.'));
}
if (!$file_uploaded) {
form_set_error('files', t('Upload at least one font file to go with the font family.'));
}
}
function local_fonts_add_form_submit($form, &$form_state) {
$stream_wrappers = file_get_stream_wrappers();
if ($stream_wrappers['public']) {
$scheme = 'public';
}
$values = $form_state['values'];
$font_path = strtr(preg_replace('#[^a-zA-Z0-9]+#', ' ', $values['css_family']), ' ', '_') . '-' . $values['css_style'] . '-' . $values['css_weight'];
$font_family = $values['css_family'];
$font_style = $values['css_style'];
$font_weight = $values['css_weight'];
$local_fonts_directory = dirname($_SERVER['SCRIPT_FILENAME']) . '/' . file_stream_wrapper_get_instance_by_scheme('public')
->getDirectoryPath() . '/fontyourface/local_fonts';
$destination_directory = $local_fonts_directory . '/' . $font_path;
$validators['file_validate_extensions'][] = 'eot ttf svg woff css';
$files = $_FILES['files']['name'];
$combined_sources = array();
$src = '';
if (file_prepare_directory($local_fonts_directory, FILE_CREATE_DIRECTORY) && file_prepare_directory($destination_directory, FILE_CREATE_DIRECTORY)) {
$destination_directory = $scheme . '://' . 'fontyourface/local_fonts/' . $font_path;
$destination_css_file = $destination_directory . '/stylesheet.css';
$css_path = file_stream_wrapper_get_instance_by_scheme('public')
->getDirectoryPath() . '/fontyourface/local_fonts/' . $font_path . '/stylesheet.css';
foreach ($files as $key => $filename) {
$format = $key;
if ($format == 'ttf') {
$format = 'truetype';
}
if (!empty($filename)) {
if ($file = file_save_upload($key, $validators, $destination_directory, FILE_EXISTS_REPLACE)) {
$file->status = FILE_STATUS_PERMANENT;
$file = file_save($file);
if ($format == 'eot') {
$src .= ' src: url("' . $file->filename . '");' . "\n";
}
elseif ($format == 'svg') {
$svg = file_get_contents($file->uri);
$hash = '';
if (preg_match('#<font([^>]+)>#i', $svg, $font_tag_match)) {
if (preg_match('#id="([^"]+)"#i', $font_tag_match[1], $id_match)) {
$hash = $id_match[1];
}
elseif (preg_match("#id='([^']+)'#i", $font_tag_match[1], $id_match)) {
$hash = $id_match[1];
}
}
$url = $file->filename . '#' . $hash;
$combined_sources[] = 'url("' . $url . '") format("' . $format . '")';
}
else {
$combined_sources[] = 'url("' . $file->filename . '") format("' . $format . '")';
}
}
}
}
if (count($combined_sources) > 0) {
$src .= '' . implode(', ', $combined_sources) . ';' . "\n";
}
$generated_css = '@font-face {' . "\n";
$generated_css .= " font-family: '" . $font_family . "';" . "\n";
$generated_css .= $src;
$generated_css .= ' font-style: ' . $font_style . ';' . "\n";
$generated_css .= ' font-weight: ' . $font_weight . ';' . "\n";
$generated_css .= '}' . "\n";
$css_file = file_unmanaged_save_data($generated_css, $destination_css_file, FILE_EXISTS_REPLACE);
$local_font = new stdClass();
$local_font->name = $font_family . ' ' . $font_style . ' ' . $font_weight;
$local_font->url = 'http://localhost/#' . md5($local_font->name);
$local_font->css_family = "'" . $font_family . "'";
$local_font->css_style = $font_style;
$local_font->css_weight = $font_weight;
$local_font->provider = 'local_fonts';
$local_font->metadata = serialize(array(
'path' => $css_path,
));
$local_font->tags = array();
fontyourface_save_font($local_font);
drupal_set_message(t('@font added.', array(
'@font' => $font_family,
)));
}
drupal_goto('admin/config/fontyourface');
}
function local_fonts_delete_form($form, $form_state, $fid) {
$font = fontyourface_get_font($fid);
$form = array(
'fid' => array(
'#type' => 'value',
'#value' => $fid,
),
'confirm' => array(
'#type' => 'fieldset',
'#title' => t('Confirm delete of @title?', array(
'@title' => $font->name,
)),
array(
'#type' => 'submit',
'#value' => 'Delete font record',
),
array(
'#type' => 'submit',
'#value' => 'Cancel',
),
),
);
return $form;
}
function local_fonts_delete_form_submit($form, &$form_state) {
if ($form_state['input']['op'] == t('Cancel')) {
drupal_goto('admin/appearance/fontyourface/edit/' . $form['fid']['#value']);
}
else {
$font = fontyourface_get_font($form['fid']['#value']);
local_fonts_delete_font($font);
drupal_set_message(t('@font deleted.', array(
'@font' => $font->name,
)));
drupal_goto('admin/appearance/fontyourface');
}
}
function local_fonts_delete_font($font) {
$metadata = unserialize($font->metadata);
$directory = dirname($metadata['path']);
$files = scandir($directory);
foreach ($files as $file) {
if ($file != '.' && $file != '..') {
unlink($directory . '/' . $file);
}
}
rmdir($directory);
db_delete('fontyourface_font')
->condition('fid', $font->fid)
->execute();
fontyourface_delete_unused_tags();
return TRUE;
}
function local_fonts_fontyourface_preview($font) {
return '<span style="' . fontyourface_font_css($font) . ' font-size: 24px;">' . $font->name . '</span>';
}
function local_fonts_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 local_fonts_preprocess_page(&$vars) {
if (!empty($vars['fontyourface'])) {
foreach ($vars['fontyourface'] as $used_font) {
if ($used_font->provider == 'local_fonts') {
$metadata = unserialize($used_font->metadata);
fontyourface_add_css_in_preprocess($vars, $metadata['path'], FALSE);
}
}
}
}