/** * Installs a new font family * * This function maps a font-family name to a font. It tries to locate the * bold, italic, and bold italic versions of the font as well. Once the * files are located, ttf versions of the font are copied to the fonts * directory. Changes to the font lookup table are saved to the cache. * * @param string $fontname the font-family name * @param string $normal the filename of the normal face font subtype * @param string $bold the filename of the bold face font subtype * @param string $italic the filename of the italic face font subtype * @param string $bold_italic the filename of the bold italic face font subtype */ function install_font_family($fontname, $normal, $bold = null, $italic = null, $bold_italic = null) { Font_Metrics::init(); // Check if the base filename is readable if (!is_readable($normal)) { throw new DOMPDF_Exception("Unable to read '{$normal}'."); } $dir = dirname($normal); $basename = basename($normal); $last_dot = strrpos($basename, '.'); if ($last_dot !== false) { $file = substr($basename, 0, $last_dot); $ext = strtolower(substr($basename, $last_dot)); } else { $file = $basename; $ext = ''; } if (!in_array($ext, array(".ttf", ".otf"))) { throw new DOMPDF_Exception("Unable to process fonts of type '{$ext}'."); } // Try $file_Bold.$ext etc. $path = "{$dir}/{$file}"; $patterns = array("bold" => array("_Bold", "b", "B", "bd", "BD"), "italic" => array("_Italic", "i", "I"), "bold_italic" => array("_Bold_Italic", "bi", "BI", "ib", "IB")); foreach ($patterns as $type => $_patterns) { if (!isset(${$type}) || !is_readable(${$type})) { foreach ($_patterns as $_pattern) { if (is_readable("{$path}{$_pattern}{$ext}")) { ${$type} = "{$path}{$_pattern}{$ext}"; break; } } if (is_null(${$type})) { echo "Unable to find {$type} face file.\n"; } } } $fonts = compact("normal", "bold", "italic", "bold_italic"); $entry = array(); // Copy the files to the font directory. foreach ($fonts as $var => $src) { if (is_null($src)) { $entry[$var] = DOMPDF_FONT_DIR . mb_substr(basename($normal), 0, -4); continue; } // Verify that the fonts exist and are readable if (!is_readable($src)) { throw new DOMPDF_Exception("Requested font '{$src}' is not readable"); } $dest = DOMPDF_FONT_DIR . basename($src); if (!is_writeable(dirname($dest))) { throw new DOMPDF_Exception("Unable to write to destination '{$dest}'."); } echo "Copying {$src} to {$dest}...\n"; if (!copy($src, $dest)) { throw new DOMPDF_Exception("Unable to copy '{$src}' to '{$dest}'"); } $entry_name = mb_substr($dest, 0, -4); echo "Generating Adobe Font Metrics for {$entry_name}...\n"; $font_obj = Font::load($dest); $font_obj->saveAdobeFontMetrics("{$entry_name}.ufm"); $entry[$var] = $entry_name; } // Store the fonts in the lookup table Font_Metrics::set_font_family($fontname, $entry); // Save the changes Font_Metrics::save_font_families(); }
static function register_font($style, $remote_file) { $fontname = mb_strtolower($style["family"]); $families = Font_Metrics::get_font_families(); $entry = array(); if (isset($families[$fontname])) { $entry = $families[$fontname]; } $local_file = DOMPDF_FONT_DIR . md5($remote_file); $cache_entry = $local_file; $local_file .= ".ttf"; $style_string = Font_Metrics::get_type("{$style['weight']} {$style['style']}"); if (!isset($entry[$style_string])) { $entry[$style_string] = $cache_entry; Font_Metrics::set_font_family($fontname, $entry); // Download the remote file if (!is_file($local_file)) { file_put_contents($local_file, file_get_contents($remote_file)); } $font = Font::load($local_file); if (!$font) { return false; } $font->parse(); $font->saveAdobeFontMetrics("{$cache_entry}.ufm"); // Save the changes Font_Metrics::save_font_families(); } return true; }
/** * Installs a new font family * * This function maps a font-family name to a font. It tries to locate the * bold, italic, and bold italic versions of the font as well. Once the * files are located, ttf versions of the font are copied to the fonts * directory. Changes to the font lookup table are saved to the cache. * * @param string $fontname the font-family name * @param string $normal the filename of the normal face font subtype * @param string $bold the filename of the bold face font subtype * @param string $italic the filename of the italic face font subtype * @param string $bold_italic the filename of the bold italic face font subtype */ function install_font_family($fontname, $normal, $bold = null, $italic = null, $bold_italic = null) { // Check if the base filename is readable if (!is_readable($normal)) { throw new DOMPDF_Exception("Unable to read '{$normal}'."); } $dir = dirname($normal); list($file, $ext) = explode(".", basename($normal), 2); // subtract extension // Try $file_Bold.$ext etc. $ext = ".{$ext}"; if (!isset($bold) || !is_readable($bold)) { $bold = $dir . "/" . $file . "_Bold" . $ext; if (!is_readable($bold)) { // Try $file . "b" $bold = $dir . "/" . $file . "b" . $ext; if (!is_readable($bold)) { // Try $file . "B" $bold = $dir . "/" . $file . "B" . $ext; if (!is_readable($bold)) { $bold = null; } } } } if (is_null($bold)) { echo "Unable to find bold face file.\n"; } if (!isset($italic) || !is_readable($italic)) { $italic = $dir . "/" . $file . "_Italic" . $ext; if (!is_readable($italic)) { // Try $file . "i" $italic = $dir . "/" . $file . "i" . $ext; if (!is_readable($italic)) { // Try $file . "I" $italic = $dir . "/" . $file . "I" . $ext; if (!is_readable($italic)) { $italic = null; } } } } if (is_null($italic)) { echo "Unable to find italic face file.\n"; } if (!isset($bold_italic) || !is_readable($bold_italic)) { $bold_italic = $dir . "/" . $file . "_Bold_Italic" . $ext; if (!is_readable($bold_italic)) { // Try $file . "bi" $bold_italic = $dir . "/" . $file . "bi" . $ext; if (!is_readable($bold_italic)) { // Try $file . "BI" $bold_italic = $dir . "/" . $file . "BI" . $ext; if (!is_readable($bold_italic)) { // Try $file . "ib" $bold_italic = $dir . "/" . $file . "ib" . $ext; if (!is_readable($bold_italic)) { // Try $file . "IB" $bold_italic = $dir . "/" . $file . "IB" . $ext; if (!is_readable($bold_italic)) { $bold_italic = null; } } } } } } if (is_null($bold_italic)) { echo "Unable to find bold italic face file.\n"; } $fonts = compact("normal", "bold", "italic", "bold_italic"); $entry = array(); if (strtolower($ext) === ".pfb" || strtolower($ext) === ".ttf" || strtolower($ext) === ".otf") { // Copy the files to the font directory. foreach ($fonts as $var => $src) { if (is_null($src)) { $entry[$var] = DOMPDF_FONT_DIR . basename($normal); continue; } // Verify that the fonts exist and are readable if (!is_readable($src)) { throw new User_DOMPDF_Exception("Requested font '{$pathname}' is not readable"); } $dest = DOMPDF_FONT_DIR . basename($src); if (!is_writeable(dirname($dest))) { throw new User_DOMPDF_Exception("Unable to write to destination '{$dest}'."); } echo "Copying {$src} to {$dest}...\n"; if (!copy($src, $dest)) { throw new DOMPDF_Exception("Unable to copy '{$src}' to '" . DOMPDF_FONT_DIR . "{$dest}'."); } $entry[$var] = $dest; } } else { throw new DOMPDF_Exception("Unable to process fonts of type '{$ext}'."); } // If the extension is a ttf, try and convert the fonts to afm too if (mb_strtolower($ext) === ".ttf" || strtolower($ext) == ".otf") { foreach ($fonts as $var => $font) { if (is_null($font)) { $entry[$var] = DOMPDF_FONT_DIR . mb_substr(basename($normal), 0, -4); continue; } $dest = DOMPDF_FONT_DIR . mb_substr(basename($font), 0, -4); echo "Generating .afm for {$font}...\n"; echo "Command: " . _TTF2AFM . " " . escapeshellarg($font) . " " . escapeshellarg($dest) . "\n"; exec(_TTF2AFM . " " . escapeshellarg($font) . " " . escapeshellarg($dest) . " &> /dev/null", $output, $ret); $entry[$var] = $dest; } } // FIXME: how to generate afms from pfb? // Store the fonts in the lookup table Font_Metrics::set_font_family(strtolower($fontname), $entry); // Save the changes Font_Metrics::save_font_families(); }
/** * Installs a new font family * * This function maps a font-family name to a font. It tries to locate the * bold, italic, and bold italic versions of the font as well. Once the * files are located, ttf versions of the font are copied to the fonts * directory. Changes to the font lookup table are saved to the cache. * * @param string $fontname the font-family name * @param string $normal the filename of the normal face font subtype * @param string $bold the filename of the bold face font subtype * @param string $italic the filename of the italic face font subtype * @param string $bold_italic the filename of the bold italic face font subtype */ function install_font_family($fontname, $normal, $bold = null, $italic = null, $bold_italic = null) { // Check if the base filename is readable if (!is_readable($normal)) { throw new DOMPDF_Exception("Unable to read '{$normal}'."); } $dir = dirname($normal); $basename = basename($normal); $last_dot = strrpos($basename, '.'); if ($last_dot !== false) { $file = substr($basename, 0, $last_dot); $ext = strtolower(substr($basename, $last_dot)); } else { $file = $basename; $ext = ''; } // Try $file_Bold.$ext etc. $path = "{$dir}/{$file}"; $patterns = array("bold" => array("_Bold", "b", "B", "bd", "BD"), "italic" => array("_Italic", "i", "I"), "bold_italic" => array("_Bold_Italic", "bi", "BI", "ib", "IB")); foreach ($patterns as $type => $_patterns) { if (!isset(${$type}) || !is_readable(${$type})) { foreach ($_patterns as $_pattern) { if (is_readable("{$path}{$_pattern}{$ext}")) { ${$type} = "{$path}{$_pattern}{$ext}"; break; } } if (is_null(${$type})) { echo "Unable to find {$type} face file.\n"; } } } $fonts = compact("normal", "bold", "italic", "bold_italic"); $entry = array(); if ($ext === ".pfb" || $ext === ".ttf" || $ext === ".otf") { // Copy the files to the font directory. foreach ($fonts as $var => $src) { if (is_null($src)) { $entry[$var] = DOMPDF_FONT_DIR . basename($normal); continue; } // Verify that the fonts exist and are readable if (!is_readable($src)) { throw new DOMPDF_Exception("Requested font '{$pathname}' is not readable"); } $dest = DOMPDF_FONT_DIR . basename($src); if (!is_writeable(dirname($dest))) { throw new DOMPDF_Exception("Unable to write to destination '{$dest}'."); } echo "Copying {$src} to {$dest}...\n"; if (!copy($src, $dest)) { throw new DOMPDF_Exception("Unable to copy '{$src}' to '" . DOMPDF_FONT_DIR . "{$dest}'."); } $entry[$var] = $dest; } } else { throw new DOMPDF_Exception("Unable to process fonts of type '{$ext}'."); } // If the extension is a ttf, try and convert the fonts to afm too if ($ext === ".ttf" || $ext === ".otf") { foreach ($fonts as $var => $font) { if (is_null($font)) { $entry[$var] = DOMPDF_FONT_DIR . mb_substr(basename($normal), 0, -4); continue; } $dest = DOMPDF_FONT_DIR . mb_substr(basename($font), 0, -4); $stdout = strpos(PHP_OS, "WIN") === false ? " >/dev/null" : " 2>&1"; $command = _TTF2AFM . " " . escapeshellarg($font) . " " . escapeshellarg($dest) . $stdout; echo "Generating .afm for {$font}...\n"; //echo $command . "\n"; exec($command, $output, $ret); $entry[$var] = $dest; } } // FIXME: how to generate afms from pfb? // Store the fonts in the lookup table Font_Metrics::set_font_family($fontname, $entry); // Save the changes Font_Metrics::save_font_families(); }