The signwriter module allows you to use custom truetype fonts for headings. It does this by creating an image with the headings' text, and replacing the headings with the images.

There are several ways in which you can use signwriter:

"); break; } } /** * Implementation of hook_menu(). */ function signwriter_menu($may_cache) { $items = array(); if ($may_cache) { $items[] = array( 'path' => 'admin/settings/signwriter', 'title' => t('Signwriter'), 'description' => t('Manage Signwriter profiles, for custom font headings.'), 'callback' => 'signwriter_settings_page', 'access' => user_access('administer signwriter'), 'type' => MENU_NORMAL_ITEM); $items[] = array( 'path' => 'admin/settings/signwriter/profile/add', 'title' => t('Add profile'), 'description' => t('Add a new Signwriter profile.'), 'callback' => 'signwriter_profile_page', 'callback arguments' => null, 'type' => MENU_NORMAL_ITEM, 'weight' => -8); } else { if (arg(2) == 'signwriter') { foreach (signwriter_load_profiles() as $profile) { $items[] = array( 'path' => 'admin/settings/signwriter/profile/' . $profile->id, 'title' => $profile->name, 'description' => t('Manage the ') . $profile->name . t(' Signwriter profile.'), 'callback' => 'signwriter_profile_page', 'callback arguments' => array($profile), 'type' => MENU_NORMAL_ITEM,); $items[] = array( 'path' => 'admin/settings/signwriter/profile/' . $profile->id . '/delete', 'title' => "Delete $profile->name", 'description' => t('Delete the ') . $profile->name . t(' Signwriter profile'), 'callback' => 'signwriter_confirm_delete_profile_page', 'callback arguments' => array($profile), 'type' => MENU_NORMAL_ITEM,); } } } return $items; } /** * Implementation of hook_perm(). */ function signwriter_perm() { return array('administer signwriter'); } function signwriter_filter($op, $delta = 0, $format = -1, $text = '') { switch ($op) { case 'list': $profiles = signwriter_load_profiles(); $filters = array(); foreach ($profiles as $profile) { if (isset($profile->pattern) && !empty($profile->pattern)) { $filters[$profile->id] = $profile->name; } } return $filters; case 'description': $profile = signwriter_load_profile($delta); return t("Replaces anything matching the regular expression: %pattern with the image generated by the '%profilename' signwriter profile.", array('%pattern' => $profile->pattern, '%profilename' => $profile->name)); case "process": $profile = signwriter_load_profile($delta); return preg_replace($profile->pattern . 'e', 'signwriter_title_convert("$0", $profile)', $text); case 'no cache': return true; default: return $text; } } /** * Implementation of hook_filter_tips() */ function signwriter_filter_tips($delta, $format, $long = false) { $profile = signwriter_load_profile($delta); if ($long) { return t('The signwriter filter will replace headings matching the regular expression %pattern with images according to the settings of the signwriter profile %profilename.', array('%pattern' => $profile->pattern, '@profile-admin-url' => url('admin/settings/signwriter/profile/' . $profile->id), '%profilename' => $profile->name)); } else { return t("The signwriter filter '%profilename' is enabled.", array('%profilename' => $profile->name)); } } function _signwriter_db_fields() { return array('id', 'name', 'pattern', 'fontfile', 'fontsize', 'imagetype', 'background', 'foreground', 'transparent', 'bgimage', 'width', 'height', 'maxwidth', 'textalign', 'xoffset', 'yoffset'); } function _signwriter_profile_whereclause($profile) { if ($profile->id) return "WHERE id = '{$profile->id}'"; if ($profile->name) return "WHERE name = '{$profile->name}'"; if (is_numeric($profile)) return "WHERE id = '$profile'"; if (is_string($profile)) return "WHERE name = '$profile'"; return false; } /** * Delete a signwriter profile without asking for confirmation. * Returns the user to the main signwriter settings page. * * @param $profile * The profile to delete. This can be one of: * - a profile id * - a profile name * - a profile object with at least the name or id set */ function signwriter_delete_profile($profile) { if ($where = _signwriter_profile_whereclause($profile)) { db_query("DELETE FROM {signwriter} $where"); drupal_set_message(t("Deleted the '$profile->name' profile.")); } drupal_goto('admin/settings/signwriter'); } /** * Ask for user confirmation before deleting a profile. * * @param $profile * The profile to delete. This can be one of: * - a profile id * - a profile name * - a profile object with at least the name or id set */ function signwriter_confirm_delete_profile_page($profile) { print theme('page', drupal_get_form('signwriter_confirm_delete_profile_form', $profile)); } function signwriter_confirm_delete_profile_form($profile) { $profile = signwriter_load_profile($profile); $form['id'] = array('#type' => 'value', '#value' => $profile->id); $form['#submit'] = array('_signwriter_confirm_delete_profile_submit' => null); return confirm_form($form, t('Are you sure you want to delete the \'%title\' profile?', array('%title' => $profile->name)), 'admin/settings/signwriter', t('Deleting a profile cannot be undone.'), t('Delete'), t('Cancel')); } function _signwriter_confirm_delete_profile_submit($form_id, $form_values) { $profile = signwriter_load_profile($form_values['id']); return signwriter_delete_profile($profile); } /** * Save a profile or update a profile in the database. * * @param $profile * The profile object to save */ function signwriter_save_profile($profile) { $profile->is_new = false; if (empty($profile->id)) { $profile->is_new = true; $profile->id = db_next_id('{signwriter}_id'); } $fields = _signwriter_db_fields(); foreach ($fields as $fieldname) { if (!is_null($profile->$fieldname)) { $keys[] = $fieldname; $value_keywords[] = "'%s'"; $values[] = $profile->$fieldname; } } if (count($keys)) { if ($profile->is_new) { $keys = implode(', ', $keys); $value_keywords = implode(', ', $value_keywords); db_query("INSERT INTO {signwriter} ($keys) VALUES ($value_keywords)", $values); } else { $where = _signwriter_profile_whereclause($profile); foreach ($keys as $key) { $assignments[] = "$key = '%s'"; } $assignments = implode(', ', $assignments); db_query("UPDATE {signwriter} SET $assignments $where", $values); } } } /** * Load a profile from the database. * * @param $profile * The profile to load. This can be one of: * - a profile id * - a profile name * - a profile object with at least the name or id set * * @return * A signwriter profile object. */ function signwriter_load_profile($profile) { if ($where = _signwriter_profile_whereclause($profile)) { $fields = implode(', ', _signwriter_db_fields()); return db_fetch_object(db_query("SELECT $fields FROM {signwriter} $where")); } } /** * Load zero or more signwriter profiles. * * @param $profiles * Describes which profile(s) to load. If absent or null then load all * profiles. Otherwise this should be an array, each element of which should * be one of: * - a profile id * - a profile name * - a profile object with at least the name or id set */ function signwriter_load_profiles($profiles = null) { if (is_null($profiles)) { $profiles = array(); $results = db_query("SELECT %s FROM {signwriter}", implode(', ', _signwriter_db_fields())); while ($profile = db_fetch_object($results)) { $profiles[] = $profile; } } else { $results = array(); foreach ($profiles as $profile) { $results[] = signwriter_load_profile($profile); } $profiles = $results; } return $profiles; } /** * The signwriter profile settings form page. * * @param $p * The profile object to edit (optional). */ function signwriter_profile_page($p = null) { $output = drupal_get_form('_signwriter_profile_form', $p); print theme('page', $output); } function _signwriter_profile_form($p = null) { // TODO: add a preview, and maybe an 'Apply' button which submits the page // then returns to it, rather than back to the main signwriter settings page. $profileid = empty($p->id) ? null : $p->id; $form['id'] = array('#type' => 'value', '#value' => $profileid); $form['name'] = array( '#type' => 'textfield', '#title' => t('Profile Name'), '#required' => true, '#default_value' => $p->name, '#size' => 40, ); $form["pattern"] = array( '#type' => 'textfield', '#title' => 'Pattern to Match When Used as a Filter', '#default_value' => $p->pattern, '#description' => t("If this pattern is defined then this profile will be available as an input filter which you can enable on the %inputformats page. When the filter is enabled, anything matching this pattern will be replaced with a signwriter image. The pattern should be a perl regular expression. For example, to replace all headings, use: /<h.*?>.*?<\/h.*?>/s. To replace only h1 headings, use /<h1.*>.*?<\/h1>/s. To define a custom pseudo-html tag (such as <signwriter>), use: /<signwriter>.*?<\/signwriter>/", array('@input-formats-url' => url('admin/settings/filters'), '%inputformats' => 'Administer >> Site configuration >> Input formats')), '#size' => 20, ); $form["fontfile"] = array( '#type' => 'textfield', '#title' => t("Font"), '#default_value' => $p->fontfile, '#description' => t("This can either be the full system path to a .ttf file, or the name of a truetype font in your font path, without the .ttf extension. Signwriter includes your drupal base dir, your files directory, and the current theme directory in the font path."), '#required' => true, '#size' => 40, ); $form["fontsize"] = array( '#type' => 'textfield', '#title' => t("Font Size"), '#default_value' => _signwriter_get_val($p->fontsize, ''), '#description' => t("If you define 'Max Width' below then this size may be overridden in order to fit the text within the given width. Defaults to 20."), '#size' => 4, ); $form["imagetype"] = array( '#type' => 'select', '#title' => t("Image Type"), '#default_value' => $p->imagetype, '#description' => t("For transparency png is best, but it doesn't work in IE without hacks (see http://webfx.eae.net/dhtml/pngbehavior/pngbehavior.html for one such hack), so gif is a good alternative."), '#options' => signwriter_available_image_types(), ); $form["background"] = array( '#type' => 'textfield', '#title' => t("Background Colour"), '#default_value' => $p->background, '#description' => t("This should be six hexadecimal digits, such as ff0000 for red, 00ff00 for green, or 0000ff for blue. To avoid jagged fonts when using transparency, make sure that this colour is the same as the page background colour. If you are using a background image and transparency then this colour will be made transparent in the source background image."), '#size' => 6, '#maxlength' => 6, ); $form["foreground"] = array( '#type' => 'textfield', '#title' => t("Foreground Colour"), '#default_value' => $p->foreground, '#description' => t("The font colour of the text. This should be six hexadecimal digits, such as ff0000 for red, 00ff00 for green, or 0000ff for blue."), '#size' => 6, '#maxlength' => 6, ); $form["transparent"] = array( '#type' => 'checkbox', '#title' => t("Transparent"), '#default_value' => is_null($p->transparent) ? true : $p->transparent, '#description' => t("If enabled, then the 'Background Colour' will be made transparent in the generated image."), ); $form["bgimage"] = array( '#type' => 'textfield', '#title' => t("Background Image"), '#default_value' => $p->bgimage, '#description' => t("Path to the background image to use, relative to your drupal directory. Leave blank to not use a background image."), '#size' => 40, ); $form["width"] = array( '#type' => 'textfield', '#title' => "Width", '#default_value' => _signwriter_get_val($p->width, ''), '#description' => t("Set the width of the image in pixels. Leave blank to have the width automatically assigned, or if you're using a background image."), '#size' => 4, ); $form["height"] = array( '#type' => 'textfield', '#title' => "Height", '#default_value' => _signwriter_get_val($p->height, ''), '#description' => t("Set the height of the image in pixels. Leave blank to have the height automatically assigned, or if you're using a background image."), '#size' => 4, ); $form["maxwidth"] = array( '#type' => 'textfield', '#title' => "Max Width", '#default_value' => _signwriter_get_val($p->maxwidth, ''), '#description' => t("This value is in pixels. If it is set then the text size will be decreased so that the text fits within the given width. Leave this blank to have no maximum."), '#size' => 4, ); $form["textalign"] = array( '#type' => 'select', '#title' => t("Text Align"), '#default_value' => $p->textalign, '#options' => array('left' => 'left', 'center' => 'center', 'right' => 'right'), '#description' => t("Text Align only makes sense if your image is wider than the text. To make this happen either assign a background image, or set the width."), ); $form["xoffset"] = array( '#type' => 'textfield', '#title' => t("X Offset"), '#default_value' => _signwriter_get_val($p->xoffset, ''), '#description' => t("Adds to the distance from the left of the image to the start of the text (or from the right of the image in the case of right-aligned text)."), '#size' => 4, ); $form["yoffset"] = array( '#type' => 'textfield', '#title' => t("Y Offset"), '#default_value' => _signwriter_get_val($p->yoffset, ''), '#description' => t("Adds to the distance from the top of the image to the baseline of the text (or something like that)."), '#size' => 4, ); $form['submit'] = array('#type' => 'submit', '#value' => t('Save')); $form['#submit'] = array('_signwriter_profile_submit' => null); return $form; } function _signwriter_profile_submit($form_id, $form_values) { $profile = (object)$form_values; signwriter_save_profile($profile); drupal_set_message(t("Fontimage profile '{$profile->name}' saved.")); return 'admin/settings/signwriter'; } /** * The main admin>>settings>>signwriter page */ function signwriter_settings_page() { if (!function_exists('imagetypes')) { print theme('page', "It appears that you do not have the GD image library installed. GD is enabled by default in PHP >= 4.3, but can be enabled at compile time in earlier versions. If your php installation is on windows, try uncommenting the line which reads 'extension=php_gd2.dll' in your php.ini. For more information see php's Image library."); } else { $profiles = signwriter_load_profiles(); $rows = array(); foreach ($profiles as $profile) { $links[] = l($profile->name, 'admin/settings/signwriter/profile/'. $profile->id); $rows[] = array('name' => check_plain($profile->name), 'edit' => l(t('edit'), 'admin/settings/signwriter/profile/' . $profile->id), 'delete' => l(t('delete'), "admin/settings/signwriter/profile/$profile->id/delete")); } if (empty($rows)) { $rows[] = array(array('data' => t('No profiles'), 'colspan' => '3', 'class' => 'message')); } $rows[] = array(array('data' => l(t('Add a profile'), 'admin/settings/signwriter/profile/add'), 'colspan' => '3')); $header = array(array('data' => t('Profiles'), 'colspan' => '3')); $output = '

' . theme('table', $header, $rows) . '

'; $output .= '

' . drupal_get_form('signwriter_settings_form') . '

'; print theme('page', $output); } } function signwriter_settings_form() { $form = array(); $form['settings'] = array('#type' => 'fieldset', '#title' => t('Settings'), '#collapsible' => true, '#collapsed' => false); $form['settings']['cachedir'] = array( '#type' => 'textfield', '#title' => t('Cache Directory'), '#description' => t('This is the directory that signwriter will store its generated images in. It should be a path relative to the drupal base directory. The default is \'signwriter-cache\'. If your files directory is publicly accessible, then another good option would be \'files/signwriter-cache\'. Make sure that your webserver process is able to create and write to this directory. Files can be deleted from this directory at any time.'), '#default_value' => variable_get('signwriter_cachedir', 'signwriter-cache'), ); $form['settings']['fontpath'] = array( '#type' => 'textfield', '#title' => t('Font Search Path'), '#default_value' => variable_get('signwriter_fontpath', ''), '#description' => t('Add a : separated list of directories to search for your font files. Signwriter will automatically search the drupal directory, your files directory, and your current theme\'s directory.'), ); $form['submit'] = array('#type' => 'submit', '#value' => t('Save Settings')); return $form; } function signwriter_settings_form_submit($form_id, $form_values) { variable_set('signwriter_cachedir', $form_values['cachedir']); variable_set('signwriter_fontpath', $form_values['fontpath']); drupal_set_message(t('Signwriter settings updated.')); } function _signwriter_get_val($var, $default = null) { return empty($var) ? $default : $var; } /** * Generate a signwriter image using the given profile. * * @param $profile * The signwriter profile to use to render the image. The following fields * are required: * - $profile->text * The text to display. Can contain html entities. For example, & * will be displayed as & * - $profile->fontfile * Which font to use. This can be a system path to a .ttf file, or the * basename minus the .ttf extension of a .ttf font file in your font * path or your drupal files directory. * These fields are optional: * - $profile->fontsize * - $profile->foreground * - $profile->background * - $profile->width * - $profile->height * - $profile->maxwidth * - $profile->imagetype * - $profile->textalign * - $profile->transparent * - $profile->bgimage * - $profile->xoffset * - $profile->yoffset * * @return * The absolute url to the image. */ function signwriter_url($profile) { $htmltext = _signwriter_get_val($profile->text, ''); $text = html_entity_decode($htmltext, ENT_QUOTES); $fontfile = $profile->fontfile; $size = _signwriter_get_val($profile->fontsize, 20); $fg = (is_string($profile->foreground)) ? _signwriter_parse_colour($profile->foreground) : array(0, 0, 0); $bg = (is_string($profile->background)) ? _signwriter_parse_colour($profile->background) : array(255, 255, 255); $width = _signwriter_get_val($profile->width); $height = _signwriter_get_val($profile->height); $maxwidth = ($profile->maxwidth > 0) ? $profile->maxwidth : null; $imagetype = _signwriter_get_val($profile->imagetype, 'gif'); $cachedir = variable_get('signwriter_cachedir', 'signwriter-cache'); $align = _signwriter_get_val($profile->textalign, 'left'); $transparent = (isset($profile->transparent)) ? $profile->transparent : true; if ($profile->bgimage) { $backgroundimage = $profile->bgimage; $backgroundimagename = basename($backgroundimage); if (!($bgimagetype = signwriter_get_image_type($backgroundimagename))) { drupal_set_message("Signwriter: unsupported image type: $backgroundimage", 'error'); return ''; } } $x = _signwriter_get_val($profile->xoffset, 0); $y = _signwriter_get_val($profile->yoffset, 0); file_check_directory($cachedir, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS); // search drupal's base dir, files dir, and current theme dir, as well as user-supplied dirs for a font $fontpath = array('.', file_directory_path(), path_to_theme()); $userfontpath = variable_get('signwriter_fontpath', ''); if ($userfontpath != '') { array_push($fontpath, $userfontpath); } array_unshift($fontpath, '.'); $path_delimiter = (substr(PHP_OS, 0, 3) == 'WIN') ? ';' : ':'; putenv('GDFONTPATH=' . implode($path_delimiter, $fontpath)); $fontname = basename($fontfile); $filename = "size:$size-font:$fontname-width:$width-height:$height-maxwidth:$maxwidth-align:$align-transparent:$transparent-bgimage:$backgroundimagename-x:$x-y:$y-bg:{$bg[0]}-{$bg[1]}-{$bg[2]}-fg:{$fg[0]}-{$fg[1]}-{$fg[2]}.$imagetype"; $urlfilename = "text:$htmltext-" . $filename; $filename = "text:$text-" . $filename; //for shorter filenames that are still unique and repeatable (for caching) //TODO: fix question marks in text when md5 filenames aren't used $filename = $urlfilename = md5($filename) . '.' . $imagetype; $file = "$cachedir/$filename"; if (!is_readable($file)) { $angle = 0; // calculate the size of the text $box = imagettfbbox($size, $angle, $fontfile, $text); if (!$box) { drupal_set_message(t("Is your font set correctly in the %profilename signwriter profile?", array('%profilename' => $profile->name)), 'error'); return ''; } $textwidth = abs($box[2]) + abs($box[0]) + 5; // sometimes text is clipped. I don't know why, so I add 5px here... $textheight = abs($box[1]) + abs($box[7]); // if we exceed maxwidth, then use a smaller font size if ($maxwidth && ($textwidth > $maxwidth)) { $profile->fontsize = ($size * ($maxwidth / $textwidth)) - 0.5; // we take an extra 0.5 to avoid endless recursing due to actual font size not decreasing return signwriter_url($profile); } $width = $width ? $width : $textwidth + $x; $height = $height ? $height : $textheight + $y; // create the image if ($backgroundimage) { $imagefunction = 'imagecreatefrom' . $bgimagetype; $im = $imagefunction($backgroundimage); $width = imagesx($im); $height = imagesy($im); } else { $im = imagecreate($width, $height); } // align the text switch ($align) { case 'center': case 'centre': $x += ($width - $textwidth) / 2; break; case 'right': $x = $width - $textwidth - $x; break; } $background = imagecolorallocate($im, $bg[0], $bg[1], $bg[2]); $foreground = imagecolorallocate($im, $fg[0], $fg[1], $fg[2]); if ($transparent) { // the background is transparent, but it should be the same colour as // whatever the image is over, otherwise you will get jagged edges // (unless you're using png). imagecolortransparent($im, $background); } imagettftext($im, $size, $angle, $x, $y + abs($box[5]), $foreground, $fontfile, $text); $imagefunction = "image$imagetype"; $imagefunction($im, $file); imagedestroy($im); } return base_path() . $cachedir . '/' . $urlfilename; } /** * Turn a title into a signwriter image. * * @param $title * The title text. Can be wrapped in html tags. * @param $signwriter * The signwriter profile to use. Can be one of: * - a profile id * - a profile name * - a profile object with at least the name or id set * * @return * HTML text to replace the title. */ function signwriter_title_convert($title, $signwriter) { $title = _signwriter_strip_tags($title); preg_match('/(<.*?>)*([^<]*)(<.*?>)*/s', $title, $matches); $titletext = $matches[2]; $openingtags = $matches[1]; $closingtags = $matches[3]; return $openingtags . theme('signwriter_text_convert', $titletext, $signwriter) . $closingtags; } /** * Turn text into a signwriter image. * * @param $text * The text to display * @param $signwriter * The signwriter profile to use. Can be one of: * - a profile id * - a profile name * - a profile object with at least the name or id set * * @return * HTML text to replace the input text. */ function theme_signwriter_text_convert($text, $signwriter) { $text = _signwriter_strip_tags($text); $signwriter->text = $text; $imgsrc = signwriter_url($signwriter); return "$text$text"; } function _signwriter_strip_tags($text) { return preg_replace('/<\/?(i|b|em)>/', '', $text); } /** * Return a list of the image types available in this php install * * @return * An array in the form 'type' => 'type' (for convenient use in form select) */ function signwriter_available_image_types() { $types = imagetypes(); $return = array(); if ($types & IMG_GIF) $return['gif'] = 'gif'; if ($types & IMG_PNG) $return['png'] = 'png'; if ($types & IMG_JPG) $return['jpeg'] = 'jpeg'; if ($types & IMG_WBMP) $return['bmp'] = 'bmp'; if ($types & IMG_XPM) $return['xpm'] = 'xpm'; return $return; } /** * Determine the type of image from a filename extension. * * @param $imagename * The image filename. * * @return * A string representing the image type (gif, png, jpeg, bmp, or xpm), or * false if the image type is not recognised. */ function signwriter_get_image_type($imagename) { $types = signwriter_available_image_types(); $types['jpg'] = 'jpeg'; // synonym for jpeg foreach ($types as $extension => $imagetype) { if (preg_match("/.*$extension$/", $imagename)) return $imagetype; } return false; } /** * Parse a six letter colour code into a colour array. * * @param $str * A string of six hexadecimal digits. * * @return * An array in the form (red, green, blue), or false. */ function _signwriter_parse_colour($str) { if (strlen($str) == 6) { $red = intval(substr($str, 0, 2), 16); $green = intval(substr($str, 2, 2), 16); $blue = intval(substr($str, 4, 2), 16); return array($red, $green, $blue); } return false; }