function local_fonts_add_fontface_submit in @font-your-face 6.2
Implementation of hook_form_submit().
File
- modules/
local_fonts/ local_fonts.module, line 225
Code
function local_fonts_add_fontface_submit($form, &$form_state) {
$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_directory_path() . '/fontyourface/local_fonts';
$destination_directory = $local_fonts_directory . '/' . $font_path;
$destination_css_file = $destination_directory . '/stylesheet.css';
$css_path = file_directory_path() . '/fontyourface/local_fonts/' . $font_path . '/stylesheet.css';
$files = $_FILES['files']['name'];
$combined_sources = array();
$src = '';
if (file_check_directory($local_fonts_directory, FILE_CREATE_DIRECTORY) && file_check_directory($destination_directory, FILE_CREATE_DIRECTORY)) {
foreach ($files as $key => $filename) {
$format = $key;
// Everything but TTF matches the extension.
if ($format == 'ttf') {
$format = 'truetype';
}
// if
if (!empty($filename)) {
if ($file = file_save_upload($key, array(), $destination_directory . '/' . $filename . '.tmp', FILE_EXISTS_REPLACE)) {
// Copy the temporary file to permanent location.
if (file_copy($file, $destination_directory . '/' . $filename, FILE_EXISTS_REPLACE)) {
file_set_status($file, FILE_STATUS_PERMANENT);
if ($format == 'eot') {
// EOT goes on its own line and doesn't take a format.
// Because IE is a "special needs" browser.
$src .= ' src: url("' . $file->filename . '");' . "\n";
}
elseif ($format == 'svg') {
// Get the font ID from the SVG file.
$svg = file_get_contents($file->filepath);
$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];
}
// elseif
}
// if
$url = $file->filename . '#' . $hash;
$combined_sources[] = 'url("' . $url . '") format("' . $format . '")';
}
else {
$combined_sources[] = 'url("' . $file->filename . '") format("' . $format . '")';
}
// else
}
// if
}
// if
}
// if
}
// foreach
if (count($combined_sources) > 0) {
// Add a fake local font name first to prevent name conflicts with local fonts.
$src .= '' . implode(', ', $combined_sources) . ';' . "\n";
}
// if
$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_save_data($generated_css, $destination_css_file);
$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,
));
fontyourface_save_font($local_font);
drupal_set_message(t('@font added.', array(
'@font' => $font_family,
)));
}
// if
drupal_goto('admin/settings/fontyourface');
}