Blame view
sources/3rdparty/phpdocx/pdf/include/font_metrics.cls.php
9.06 KB
|
31b7f2792
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 |
<?php
/**
* DOMPDF - PHP5 HTML to PDF renderer
*
* File: $RCSfile: font_metrics.cls.php,v $
* Created on: 2004-06-02
*
* Copyright (c) 2004 - Benj Carson <benjcarson@digitaljunkies.ca>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library in the file LICENSE.LGPL; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
* 02111-1307 USA
*
* Alternatively, you may distribute this software under the terms of the
* PHP License, version 3.0 or later. A copy of this license should have
* been distributed with this file in the file LICENSE.PHP . If this is not
* the case, you can obtain a copy at http://www.php.net/license/3_0.txt.
*
* The latest version of DOMPDF might be available at:
* http://www.dompdf.com/
*
* @link http://www.dompdf.com/
* @copyright 2004 Benj Carson
* @author Benj Carson <benjcarson@digitaljunkies.ca>
* @contributor Helmut Tischer <htischer@weihenstephan.org>
* @package dompdf
*
* Changes
* @contributor Helmut Tischer <htischer@weihenstephan.org>
* @version 0.5.1.htischer.20090507
* - On missing font on explicite font selection don't change subtype and don't return default font.
* - On requesting default font and missing subtype, check similar subtypes, then any subtype, then normal. The last must exist.
* - Add comments
*/
/* $Id: font_metrics.cls.php 355 2011-01-27 07:44:54Z fabien.menager $ */
require_once(DOMPDF_LIB_DIR . "/class.pdf.php");
/**
* Name of the font cache file
*
* This file must be writable by the webserver process only to update it
* with save_font_families() after adding the .afm file references of a new font family
* with Font_Metrics::save_font_families().
* This is typically done only from command line with load_font.php on converting
* ttf fonts to afm with an external tool referenced in the define _TTF2AFM
*
* Declared here because PHP5 prevents constants from being declared with expressions
*/
if (!defined("__DOMPDF_FONT_CACHE_FILE")) {
if (file_exists(DOMPDF_FONT_DIR . "dompdf_font_family_cache")) {
define('__DOMPDF_FONT_CACHE_FILE', DOMPDF_FONT_DIR . "dompdf_font_family_cache");
} else {
define('__DOMPDF_FONT_CACHE_FILE', DOMPDF_FONT_DIR . "dompdf_font_family_cache.dist.php");
}
}
/**
* The font metrics class
*
* This class provides information about fonts and text. It can resolve
* font names into actual installed font files, as well as determine the
* size of text in a particular font and size.
*
* @static
* @package dompdf
*/
class Font_Metrics {
/**
* @see __DOMPDF_FONT_CACHE_FILE
*/
const CACHE_FILE = __DOMPDF_FONT_CACHE_FILE;
/**
* Underlying {@link Canvas} object to perform text size calculations
*
* @var Canvas
*/
static protected $_pdf = null;
/**
* Array of font family names to font files
*
* Usually cached by the {@link load_font.php} script
*
* @var array
*/
static protected $_font_lookup = array();
/**
* Class initialization
*
*/
static function init() {
if (!self::$_pdf) {
self::load_font_families();
self::$_pdf = Canvas_Factory::get_instance();
}
}
/**
* Calculates text size, in points
*
* @param string $text the text to be sized
* @param string $font the desired font
* @param float $size the desired font size
* @param float $spacing word spacing, if any
* @return float
*/
static function get_text_width($text, $font, $size, $word_spacing = 0, $char_spacing = 0) {
return self::$_pdf->get_text_width($text, $font, $size, $word_spacing, $char_spacing);
}
/**
* Calculates font height
*
* @param string $font
* @param float $size
* @return float
*/
static function get_font_height($font, $size) {
return self::$_pdf->get_font_height($font, $size);
}
/**
* Resolves a font family & subtype into an actual font file
*
* Subtype can be one of 'normal', 'bold', 'italic' or 'bold_italic'. If
* the particular font family has no suitable font file, the default font
* ({@link DOMPDF_DEFAULT_FONT}) is used. The font file returned
* is the absolute pathname to the font file on the system.
*
* @param string $family
* @param string $subtype
* @return string
*/
static function get_font($family, $subtype = "normal") {
/* Allow calling for various fonts in search path. Therefore not immediately
* return replacement on non match.
* Only when called with NULL try replacement.
* When this is also missing there is really trouble.
* If only the subtype fails, nevertheless return failure.
* Only on checking the fallback font, check various subtypes on same font.
*/
if ( $family ) {
$family = str_replace( array("'", '"'), "", mb_strtolower($family));
$subtype = mb_strtolower($subtype);
if ( isset(self::$_font_lookup[$family][$subtype]) ) {
return self::$_font_lookup[$family][$subtype];
}
return null;
}
$family = DOMPDF_DEFAULT_FONT;
if ( isset(self::$_font_lookup[$family][$subtype]) ) {
return self::$_font_lookup[$family][$subtype];
}
foreach ( self::$_font_lookup[$family] as $sub => $font ) {
if (strpos($subtype, $sub) !== false) {
return $font;
}
}
if ($subtype !== "normal") {
foreach ( self::$_font_lookup[$family] as $sub => $font ) {
if ($sub !== "normal") {
return $font;
}
}
}
$subtype = "normal";
if ( isset(self::$_font_lookup[$family][$subtype]) ) {
return self::$_font_lookup[$family][$subtype];
}
return null;
}
/**
* Saves the stored font family cache
*
* The name and location of the cache file are determined by {@link
* Font_Metrics::CACHE_FILE}. This file should be writable by the
* webserver process.
*
* @see Font_Metrics::load_font_families()
*/
static function save_font_families() {
// replace the path to the DOMPDF font directory with "DOMPDF_FONT_DIR" (allows for more portability)
$cache_data = var_export(self::$_font_lookup, true);
$cache_data = str_replace('\''.DOMPDF_FONT_DIR , 'DOMPDF_FONT_DIR . \'' , $cache_data);
$cache_data = "<"."?php return $cache_data ?".">";
file_put_contents(self::CACHE_FILE, $cache_data);
}
/**
* Loads the stored font family cache
*
* @see save_font_families()
*/
static function load_font_families() {
if ( !is_readable(self::CACHE_FILE) )
return;
self::$_font_lookup = require_once(self::CACHE_FILE);
// If the font family cache is still in the old format
if ( self::$_font_lookup === 1 ) {
$cache_data = file_get_contents(self::CACHE_FILE);
file_put_contents(self::CACHE_FILE, "<"."?php return $cache_data ?".">");
self::$_font_lookup = require_once(self::CACHE_FILE);
}
}
static function get_system_fonts() {
$files = glob("/usr/share/fonts/truetype/*.ttf") +
glob("/usr/share/fonts/truetype/*/*.ttf") +
glob("/usr/share/fonts/truetype/*/*/*.ttf") +
glob("C:\\Windows\\fonts\\*.ttf") +
glob("C:\\WinNT\\fonts\\*.ttf") +
glob("/mnt/c_drive/WINDOWS/Fonts/");
new TTF_Info;
$names = array();
foreach($files as $file) {
$info = getFontInfo($file);
$info["path"] = $file;
$type = $info[2];
if (preg_match("/regular|normal|medium|book/i", $type)) {
$type = "normal";
}
elseif (preg_match("/bold/i", $type)) {
if (preg_match("/italic|oblique/i", $type)) {
$type = "bold_italic";
}
else {
$type = "bold";
}
}
elseif (preg_match("/italic|oblique/i", $type)) {
$type = "italic";
}
$names[mb_strtolower($info[1])][$type] = $file;
}
$keys = array_keys($names);
/*$matches = array_intersect(array("times", "times new roman"), $keys);
$names["serif"] = $names[reset($matches)];
$matches = array_intersect(array("helvetica", "arial", "verdana"), $keys);
$names["sans-serif"] = $names[reset($matches)];
$matches = array_intersect(array("courier", "courier new"), $keys);
$names["monospace"] = $names[reset($matches)];
$names["fixed"] = $names[reset($matches)];*/
return $names;
}
/**
* Returns the current font lookup table
*
* @return array
*/
static function get_font_families() {
return self::$_font_lookup;
}
static function set_font_family($fontname, $entry) {
self::$_font_lookup[mb_strtolower($fontname)] = $entry;
}
}
Font_Metrics::init();
|