/** * 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(); }
return intval($value); } ?> <a name="setup"> </a> <h2>Font manager</h2> <ul> <li style="list-style-image: url('images/star_02.gif');"><a href="#installed-fonts">Installed fonts</a></li> <li style="list-style-image: url('images/star_02.gif');"><a href="#install-fonts">Install new fonts</a></li> </ul> <h3 id="installed-fonts">Installed fonts</h3> <?php Font_Metrics::init(); $fonts = Font_Metrics::get_font_families(); $extensions = array("ttf", "afm", "afm.php", "ufm", "ufm.php"); ?> <button onclick="$('#clear-font-cache-message').load('controller.php?cmd=clear-font-cache', function(){ location.reload(); })">Clear font cache</button> <span id="clear-font-cache-message"></span> <table class="setup"> <tr> <th rowspan="2">Font family</th> <th rowspan="2">Variants</th> <th colspan="6">File versions</th> </tr> <tr> <th>TTF</th>
/** * Renders the HTML to PDF */ function render() { $this->save_locale(); $log_output_file = $this->get_option("log_output_file"); if ($log_output_file) { if (!file_exists($log_output_file) && is_writable(dirname($log_output_file))) { touch($log_output_file); } $this->_start_time = microtime(true); ob_start(); } //enable_mem_profile(); $this->_process_html(); $this->_css->apply_styles($this->_tree); // @page style rules : size, margins $page_styles = $this->_css->get_page_styles(); $base_page_style = $page_styles["base"]; unset($page_styles["base"]); foreach ($page_styles as $_page_style) { $_page_style->inherit($base_page_style); } if (is_array($base_page_style->size)) { $this->set_paper(array(0, 0, $base_page_style->size[0], $base_page_style->size[1])); } $this->_pdf = Canvas_Factory::get_instance($this, $this->_paper_size, $this->_paper_orientation); Font_Metrics::init($this->_pdf); if ($this->get_option("enable_font_subsetting") && $this->_pdf instanceof CPDF_Adapter) { foreach ($this->_tree->get_frames() as $frame) { $style = $frame->get_style(); $node = $frame->get_node(); // Handle text nodes if ($node->nodeName === "#text") { $this->_pdf->register_string_subset($style->font_family, $node->nodeValue); continue; } // Handle generated content (list items) if ($style->display === "list-item") { $chars = List_Bullet_Renderer::get_counter_chars($style->list_style_type); $this->_pdf->register_string_subset($style->font_family, $chars); continue; } // Handle other generated content (pseudo elements) // FIXME: This only captures the text of the stylesheet declaration, // not the actual generated content, and forces all possible counter // values. See notes in issue #750. if ($frame->get_node()->nodeName == "dompdf_generated") { // all possible counter values $chars = List_Bullet_Renderer::get_counter_chars('decimal'); $this->_pdf->register_string_subset($style->font_family, $chars); $chars = List_Bullet_Renderer::get_counter_chars('upper-alpha'); $this->_pdf->register_string_subset($style->font_family, $chars); $chars = List_Bullet_Renderer::get_counter_chars('lower-alpha'); $this->_pdf->register_string_subset($style->font_family, $chars); $chars = List_Bullet_Renderer::get_counter_chars('lower-greek'); $this->_pdf->register_string_subset($style->font_family, $chars); // the text of the stylesheet declaration $this->_pdf->register_string_subset($style->font_family, $style->content); continue; } } } $root = null; foreach ($this->_tree->get_frames() as $frame) { // Set up the root frame if (is_null($root)) { $root = Frame_Factory::decorate_root($this->_tree->get_root(), $this); continue; } // Create the appropriate decorators, reflowers & positioners. Frame_Factory::decorate_frame($frame, $this, $root); } // Add meta information $title = $this->_xml->getElementsByTagName("title"); if ($title->length) { $this->_pdf->add_info("Title", trim($title->item(0)->nodeValue)); } $metas = $this->_xml->getElementsByTagName("meta"); $labels = array("author" => "Author", "keywords" => "Keywords", "description" => "Subject"); foreach ($metas as $meta) { $name = mb_strtolower($meta->getAttribute("name")); $value = trim($meta->getAttribute("content")); if (isset($labels[$name])) { $this->_pdf->add_info($labels[$name], $value); continue; } if ($name === "dompdf.view" && $this->parse_default_view($value)) { $this->_pdf->set_default_view($this->_default_view, $this->_default_view_options); } } $root->set_containing_block(0, 0, $this->_pdf->get_width(), $this->_pdf->get_height()); $root->set_renderer(new Renderer($this)); // This is where the magic happens: $root->reflow(); // Clean up cached images Image_Cache::clear(); global $_dompdf_warnings, $_dompdf_show_warnings; if ($_dompdf_show_warnings) { echo '<b>DOMPDF Warnings</b><br><pre>'; foreach ($_dompdf_warnings as $msg) { echo $msg . "\n"; } echo $this->get_canvas()->get_cpdf()->messages; echo '</pre>'; flush(); } $this->restore_locale(); }
/** * Renders the HTML to PDF */ function render() { $this->save_locale(); if (DOMPDF_LOG_OUTPUT_FILE) { if (!file_exists(DOMPDF_LOG_OUTPUT_FILE) && is_writable(dirname(DOMPDF_LOG_OUTPUT_FILE))) { touch(DOMPDF_LOG_OUTPUT_FILE); } $this->_start_time = microtime(true); ob_start(); } // enable_mem_profile(); $this->_process_html(); $this->_css->apply_styles($this->_tree); // @page style rules : size, margins $page_styles = $this->_css->get_page_styles(); $base_page_style = $page_styles["base"]; unset($page_styles["base"]); foreach ($page_styles as $_page_style) { $_page_style->inherit($base_page_style); } if (is_array($base_page_style->size)) { $this->set_paper(array(0, 0, $base_page_style->size[0], $base_page_style->size[1])); } $this->_pdf = Canvas_Factory::get_instance($this->_paper_size, $this->_paper_orientation); Font_Metrics::init($this->_pdf); if (DOMPDF_ENABLE_FONTSUBSETTING && $this->_pdf instanceof CPDF_Adapter) { foreach ($this->_tree->get_frames() as $frame) { $style = $frame->get_style(); $node = $frame->get_node(); // Handle text nodes if ($node->nodeName === "#text") { $this->_pdf->register_string_subset($style->font_family, $node->nodeValue); continue; } // Handle generated content (list items) if ($style->display === "list-item") { $chars = List_Bullet_Renderer::get_counter_chars($style->list_style_type); $this->_pdf->register_string_subset($style->font_family, $chars); continue; } // TODO Handle other generated content (pseudo elements) } } $root = null; foreach ($this->_tree->get_frames() as $frame) { // Set up the root frame if (is_null($root)) { $root = Frame_Factory::decorate_root($this->_tree->get_root(), $this); continue; } // Create the appropriate decorators, reflowers & positioners. $deco = Frame_Factory::decorate_frame($frame, $this); $deco->set_root($root); // FIXME: handle generated content if ($frame->get_style()->display === "list-item") { // Insert a list-bullet frame $node = $this->_xml->createElement("bullet"); // arbitrary choice $b_f = new Frame($node); $parent_node = $frame->get_parent()->get_node(); if (!$parent_node->hasAttribute("dompdf-children-count")) { $xpath = new DOMXPath($this->_xml); $count = $xpath->query("li", $parent_node)->length; $parent_node->setAttribute("dompdf-children-count", $count); } if (!$parent_node->hasAttribute("dompdf-counter")) { $index = $parent_node->hasAttribute("start") ? $parent_node->getAttribute("start") - 1 : 0; } else { $index = $parent_node->getAttribute("dompdf-counter"); } $index++; $parent_node->setAttribute("dompdf-counter", $index); $node->setAttribute("dompdf-counter", $index); $style = $this->_css->create_style(); $style->display = "-dompdf-list-bullet"; $style->inherit($frame->get_style()); $b_f->set_style($style); $deco->prepend_child(Frame_Factory::decorate_frame($b_f, $this)); } } // Add meta information $title = $this->_xml->getElementsByTagName("title"); if ($title->length) { $this->_pdf->add_info("Title", trim($title->item(0)->nodeValue)); } $metas = $this->_xml->getElementsByTagName("meta"); $labels = array("author" => "Author", "keywords" => "Keywords", "description" => "Subject"); foreach ($metas as $meta) { $name = mb_strtolower($meta->getAttribute("name")); $value = trim($meta->getAttribute("content")); if (isset($labels[$name])) { $this->_pdf->add_info($labels[$name], $value); continue; } if ($name === "dompdf.view" && $this->parse_default_view($value)) { $this->_pdf->set_default_view($this->_default_view, $this->_default_view_options); } } $root->set_containing_block(0, 0, $this->_pdf->get_width(), $this->_pdf->get_height()); $root->set_renderer(new Renderer($this)); // This is where the magic happens: $root->reflow(); // Clean up cached images Image_Cache::clear(); global $_dompdf_warnings, $_dompdf_show_warnings; if ($_dompdf_show_warnings) { echo '<b>DOMPDF Warnings</b><br><pre>'; foreach ($_dompdf_warnings as $msg) { echo $msg . "\n"; } echo $this->get_canvas()->get_cpdf()->messages; echo '</pre>'; flush(); } $this->restore_locale(); }