Active
Project:
Dynamic Font Rendering
Version:
6.x-1.3
Component:
Code
Priority:
Normal
Category:
Feature request
Assigned:
Unassigned
Reporter:
Created:
16 Jun 2010 at 14:23 UTC
Updated:
28 Jun 2010 at 05:57 UTC
Hi,
I needed to put my fonts out of the files directory so I altered the module to provide a settings form to choose a directory.
dfont.module:
<?php
function dfont_init() {
$path = file_directory_path();
if ( ! file_exists( $path . '/dfont.css' ) ){
$file = fopen($path . '/dfont.css', 'w');
fwrite($file, '/* $Id: dfont.module,v 1.18 2010/05/04 21:12:39 vph Exp $ */');
fclose($file);
}
drupal_add_css( $path . '/dfont.css' );
if ( ! file_exists( $path . '/dfont.js' ) ){
$file = fopen($path . '/dfont.js', 'w');
fwrite($file, '/* $Id: dfont.module,v 1.18 2010/05/04 21:12:39 vph Exp $ */');
fclose($file);
}
drupal_add_js( $path . '/dfont.js' );
}
/**
* Implementation of hook_settings()
*
* @return unknown
*/
function dfont_settings() {
$form['dfont_directory'] = array(
'#title' => t('Fonts Directory'),
'#description' => t('Enter the directory where your fonts are located relative to site root with no trailing or preceding slash'),
'#type' => 'textfield',
'#default_value' => variable_get('dfont_directory', file_directory_path()),
);
return system_settings_form($form);
}
function dfont_menu() {
$items = array();
$items['admin/settings/dfont'] = array(
'title' => 'Dynamic Fonts',
'description' => 'dynamic font rendering',
'page callback' => 'dfont_admin',
'access arguments' => array('administer site configuration'),
'type' => MENU_NORMAL_ITEM,
);
$items['admin/settings/dfont/extra'] = array(
'title' => 'Dynamic Fonts extra settings',
'description' => 'Directory where your fonts are located',
'page callback' => 'drupal_get_form',
'page arguments' => array('dfont_settings'),
'access arguments' => array('administer site configuration'),
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
function dfont_filter($op, $delta = 0, $format = -1, $text = '', $cache_id = 0) {
switch ($op) {
case 'list':
return array(0 => t('Dynamic font filter'));
case 'description':
return t('Allows users to display dynamic fonts using tag dfont');
case 'prepare':
return $text;
case "process":
// example: [dfont=UVNThuTu|120%] ... [/dfont]
$patterns[0] = '/\[dfont=(df[0-9]+(m|l|x))\]/';
$patterns[1] = '/\[dfont=([a-zA-Z0-9_]+)\|(\d+(\%|pt|px))(\|(\d+\%)){0,1}\]/';
$patterns[2] = '/\[\/dfont\]/';
$replace[0] = "<div class=\"$1\">";
$replace[1] = "<div class=\"$1\" style=\"font-size: $2; line-height: $5;\">";
$replace[2] = "</div>";
$text = preg_replace($patterns, $replace, $text);
return $text;
default:
return $text;
}
}
function dfont_admin() {
$path = check_plain(variable_get('dfont_directory', file_directory_path()));
$dir = dir($path);
$font_list = array();
while (($file = $dir->read()) !== false) {
if($file != '.' && $file != '..'){
$ext = substr($file, strrpos($file, '.') + 1);
if ($ext == 'ttf' || $ext == 'TTF'){
$prefix = substr($file, 0, strrpos($file, '.'));
if ( ! file_exists( $path .'/'. $prefix . '.eot') ){
drupal_set_message($prefix . ".eot is missing", "error");
}
else {
$font_list[] = $file;
}
}
}
}
$dir->close();
return dfont_guide() . dfont_font_list($path, $dir, $font_list);
}
function dfont_font_list($path, $dir, $font_list){
$out = '<br><h1>Available fonts shown in 20pt:</h1><ul>';
$count = 0;
foreach ( $font_list as $file ){
$count++;
$prefix = substr($file, 0, strrpos($file, '.'));
$out .= "<li><h3>$prefix (df$count):</h3><blockquote><div class=\"df$count\" style=\"font-size:20pt;\">The quick brown fox jumps over the lazy dog</div></blockquote>";
$pre_form['df-file-' . $count] = array('#type' => 'hidden', '#value' => $file);
$pre_form['df-medium-' . $count] = array('#title' => t("Font-size,line-height for medium size of $prefix"),
'#type' => 'textfield',
'#default_value' => variable_get('df-medium-'.$count, "160%,100%"),
'#description' => t("Usage: [dfont=df$count"."m] ... [/dfont]")
);
$pre_form['df-large-' . $count] = array('#title' => t("Font-size,line-height for large size of $prefix"),
'#type' => 'textfield',
'#default_value' => variable_get('df-large-'.$count, "200%,105%"),
'#description' => t("Usage: [dfont=df$count"."l] ... [/dfont]")
);
$pre_form['df-xlarge-' . $count] = array('#title' => t("Font-size,line-height for xlarge size of $prefix"),
'#type' => 'textfield',
'#default_value' => variable_get('df-xlarge-'.$count, "300%,110%"),
'#description' => t("Usage: [dfont=df$count"."x] ... [/dfont]")
);
}
$pre_form['df-count'] = array('#type' => 'hidden', '#value' => $count);
$pre_form['df-h1'] = array('#title' => t("h1 preset"), '#type' => 'textfield',
'#default_value' => variable_get('df-h1', ''),
'#description' => t('empty or df1m or df1l or df1x etc.')
);
$pre_form['df-h2'] = array('#title' => t("h2 preset"), '#type' => 'textfield',
'#default_value' => variable_get('df-h2', ''),
'#description' => t('empty or df1m or df1l or df1x etc.')
);
$pre_form['df-h3'] = array('#title' => t("h3 preset"), '#type' => 'textfield',
'#default_value' => variable_get('df-h3', ''),
'#description' => t('empty or df1m or df1l or df1x etc.')
);
return $out . "</ul><h1>Presets:</h1>" . drupal_get_form('dfont_preset_form', $pre_form);
}
function dfont_preset_form(&$form_state, $pre_form){
$pre_form['submit'] = array('#type' => 'submit', '#value' => t('Generate Font Styles'));
return $pre_form;
}
function dfont_preset_form_submit($form, &$form_state) {
$css_file = fopen( file_directory_path() . '/dfont.css', 'w');
fwrite($css_file, '/* $Id: dfont.module,v 1.18 2010/05/04 21:12:39 vph Exp $ */' . "\n");
$values = $form_state['values'];
for ($i=1; $i<=$values['df-count']; $i++){
$file = $values['df-file-' . $i];
$m = $values['df-medium-' . $i];
$l = $values['df-large-' . $i];
$x = $values['df-xlarge-' . $i];
variable_set('df-medium-'.$i, $m);
variable_set('df-large-'.$i, $l);
variable_set('df-xlarge-'.$i, $x);
$prefix = substr($file, 0, strrpos($file, '.'));
fwrite($css_file, dfont_font_entry($m, $l, $x, $prefix, $file, $i));
}
fclose($css_file);
variable_set('df-h1', $values['df-h1']);
variable_set('df-h2', $values['df-h2']);
variable_set('df-h3', $values['df-h3']);
$js_file = fopen( file_directory_path() . '/dfont.js', 'w');
fwrite($js_file, '/* $Id: dfont.module,v 1.18 2010/05/04 21:12:39 vph Exp $ */' . "\n");
if($values['df-h1']) { $h1 = '$("h1").addClass("' . $values['df-h1'] .'"); '; }
if($values['df-h2']) { $h2 = '$("h2").addClass("' . $values['df-h2'] .'"); '; }
if($values['df-h3']) { $h3 = '$("h3").addClass("' . $values['df-h3'] .'"); '; }
$js = <<<EOD
if (Drupal.jsEnabled) {
$(document).ready(function () {
$h1 $h2 $h3
});
}
EOD;
fwrite($js_file, $js);
fclose($js_file);
$form_state['redirect'] = "admin/settings/dfont";
drupal_set_message("Font styles have been generated. Please refresh this page");
}
function dfont_font_entry($m, $l, $x, $prefix, $file, $count){
$medium = explode(',', $m);
$large = explode(',', $l);
$xlarge = explode(',', $x);
$size[0] = 'm'; $size[1] = 'l'; $size[2] = 'x';
$entry = <<<EOD
@font-face { font-family: $prefix; src: url('fonts/$prefix.eot'); src: local('$prefix'), url('fonts/$file') format('truetype'); } .$prefix , .df$count { font-family: $prefix; } .df$count$size[0] { font-family: $prefix; font-size: $medium[0]; line-height: $medium[1]; } .df$count$size[1] { font-family: $prefix; font-size: $large[0]; line-height: $large[1]; } .df$count$size[2] { font-family: $prefix; font-size: $xlarge[0]; line-height: $xlarge[1]; }
EOD;
return $entry . "\n";
}
function dfont_guide() {
$phrase = 'The quick brown fox jumps over the lazy dog';
$guide = "<h1>Installation:</h1><ol>";
$guide .= "<li>Choose a directory for your fonts ". l('here', 'admin/settings/dfont/extra') .". <strong>". file_directory_path() ."</strong> is set as default.</li>";
$guide .= "<li>Upload the .ttf and .eot files, for each font, to directory your fonts directory</li>";
$guide .= "<li>Scroll down, set presets and click <b>Generate Font Styles</b>.</li>";
$guide .= "<li>Enable dynamic font filter in <b><a href=\"" . base_path() . "admin/settings/filters\">admin/settings/filters</a></b>.</li>";
$guide .= "<li>Refresh this page to ensure your fonts are rendered below.</li></ol>";
$guide .= "<h1>Usage:</h1>Using the dfont filter, You can specify a font's full-name or its short name (dfx). You must specify font-size using percentage, pt, or px.";
$guide .= "<blockquote><font color=\"blue\">[dfont=GentI102|105%]</font> $phrase <font color=\"blue\">[/dfont]</font><br>";
$guide .= "<font color=\"blue\">[dfont=df1|16pt]</font> $phrase <font color=\"blue\">[/dfont]</font></blockquote>";
$guide .= "If you render a multi-line paragraph, you can optionally specify the line height in percentage.";
$guide .= "<blockquote><font color=\"blue\">[dfont=df1|120%|110%]</font> $phrase <font color=\"blue\">[/dfont]</font> (font size = 120%, line height = 110%)</blockquote>";
$guide .= "It might be more convenient to use the presets (m=medium, l=large, x=xlarge) for each font.";
$guide .= "<blockquote><font color=\"blue\">[dfont=df1m]</font> $phrase <font color=\"blue\">[/dfont]</font> (font df1 medium size)</blockquote>";
return $guide;
}
Sorry for not supplying a patch but Eclipse reformatted loads of stuff so the patch was super ugly.
cheers
Marcus
Comments
Comment #1
klonos+1 for this one! I haven't tested it yet, but I surely like the idea.
While at it, may I propose a
../sites/all/fontsas the default font directory. If that directory doesn't exist, it can gracefully fall back to../sites/all/filesafter spitting some message like so: 'The default fonts directory was not found under ../sites/all - reverting to ../sites/all/files'