PageRenderTime 34ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/include/font_metrics.cls.php

https://github.com/dwagner2301/dompdf
PHP | 351 lines | 187 code | 52 blank | 112 comment | 22 complexity | bea952f03ba78978108a7246e5534ff1 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /**
  3. * @package dompdf
  4. * @link http://dompdf.github.com/
  5. * @author Benj Carson <benjcarson@digitaljunkies.ca>
  6. * @author Helmut Tischer <htischer@weihenstephan.org>
  7. * @author Fabien MĂŠnager <fabien.menager@gmail.com>
  8. * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
  9. */
  10. require_once DOMPDF_LIB_DIR . "/class.pdf.php";
  11. /**
  12. * Name of the font cache file
  13. *
  14. * This file must be writable by the webserver process only to update it
  15. * with save_font_families() after adding the .afm file references of a new font family
  16. * with Font_Metrics::save_font_families().
  17. * This is typically done only from command line with load_font.php on converting
  18. * ttf fonts to ufm with php-font-lib.
  19. *
  20. * Declared here because PHP5 prevents constants from being declared with expressions
  21. */
  22. if (!defined("__DOMPDF_FONT_CACHE_FILE")) {
  23. if (file_exists(DOMPDF_FONT_DIR . "dompdf_font_family_cache")) {
  24. define('__DOMPDF_FONT_CACHE_FILE', DOMPDF_FONT_DIR . "dompdf_font_family_cache");
  25. }
  26. else {
  27. define('__DOMPDF_FONT_CACHE_FILE', DOMPDF_FONT_DIR . "dompdf_font_family_cache.dist.php");
  28. }
  29. }
  30. /**
  31. * The font metrics class
  32. *
  33. * This class provides information about fonts and text. It can resolve
  34. * font names into actual installed font files, as well as determine the
  35. * size of text in a particular font and size.
  36. *
  37. * @static
  38. * @package dompdf
  39. */
  40. class Font_Metrics {
  41. /**
  42. * @see __DOMPDF_FONT_CACHE_FILE
  43. */
  44. const CACHE_FILE = __DOMPDF_FONT_CACHE_FILE;
  45. /**
  46. * Underlying {@link Canvas} object to perform text size calculations
  47. *
  48. * @var Canvas
  49. */
  50. static protected $_pdf = null;
  51. /**
  52. * Array of font family names to font files
  53. *
  54. * Usually cached by the {@link load_font.php} script
  55. *
  56. * @var array
  57. */
  58. static protected $_font_lookup = array();
  59. /**
  60. * Class initialization
  61. *
  62. */
  63. static function init(Canvas $canvas = null) {
  64. if (!self::$_pdf) {
  65. if (!$canvas) {
  66. $canvas = Canvas_Factory::get_instance(new DOMPDF());
  67. }
  68. self::$_pdf = $canvas;
  69. }
  70. }
  71. /**
  72. * Calculates text size, in points
  73. *
  74. * @param string $text the text to be sized
  75. * @param string $font the desired font
  76. * @param float $size the desired font size
  77. * @param float $word_spacing
  78. * @param float $char_spacing
  79. *
  80. * @internal param float $spacing word spacing, if any
  81. * @return float
  82. */
  83. static function get_text_width($text, $font, $size, $word_spacing = 0.0, $char_spacing = 0.0) {
  84. //return self::$_pdf->get_text_width($text, $font, $size, $word_spacing, $char_spacing);
  85. // @todo Make sure this cache is efficient before enabling it
  86. static $cache = array();
  87. if ( $text === "" ) {
  88. return 0;
  89. }
  90. // Don't cache long strings
  91. $use_cache = !isset($text[50]); // Faster than strlen
  92. $key = "$font/$size/$word_spacing/$char_spacing";
  93. if ( $use_cache && isset($cache[$key][$text]) ) {
  94. return $cache[$key]["$text"];
  95. }
  96. $width = self::$_pdf->get_text_width($text, $font, $size, $word_spacing, $char_spacing);
  97. if ( $use_cache ) {
  98. $cache[$key][$text] = $width;
  99. }
  100. return $width;
  101. }
  102. /**
  103. * Calculates font height
  104. *
  105. * @param string $font
  106. * @param float $size
  107. * @return float
  108. */
  109. static function get_font_height($font, $size) {
  110. return self::$_pdf->get_font_height($font, $size);
  111. }
  112. /**
  113. * Resolves a font family & subtype into an actual font file
  114. * Subtype can be one of 'normal', 'bold', 'italic' or 'bold_italic'. If
  115. * the particular font family has no suitable font file, the default font
  116. * ({@link DOMPDF_DEFAULT_FONT}) is used. The font file returned
  117. * is the absolute pathname to the font file on the system.
  118. *
  119. * @param string $family_raw
  120. * @param string $subtype_raw
  121. *
  122. * @return string
  123. */
  124. static function get_font($family_raw, $subtype_raw = "normal") {
  125. static $cache = array();
  126. if ( isset($cache[$family_raw][$subtype_raw]) ) {
  127. return $cache[$family_raw][$subtype_raw];
  128. }
  129. /* Allow calling for various fonts in search path. Therefore not immediately
  130. * return replacement on non match.
  131. * Only when called with NULL try replacement.
  132. * When this is also missing there is really trouble.
  133. * If only the subtype fails, nevertheless return failure.
  134. * Only on checking the fallback font, check various subtypes on same font.
  135. */
  136. $subtype = strtolower($subtype_raw);
  137. if ( $family_raw ) {
  138. $family = str_replace( array("'", '"'), "", strtolower($family_raw));
  139. if ( isset(self::$_font_lookup[$family][$subtype]) ) {
  140. return $cache[$family_raw][$subtype_raw] = self::$_font_lookup[$family][$subtype];
  141. }
  142. return null;
  143. }
  144. $family = "serif";
  145. if ( isset(self::$_font_lookup[$family][$subtype]) ) {
  146. return $cache[$family_raw][$subtype_raw] = self::$_font_lookup[$family][$subtype];
  147. }
  148. if ( !isset(self::$_font_lookup[$family]) ) {
  149. return null;
  150. }
  151. $family = self::$_font_lookup[$family];
  152. foreach ( $family as $sub => $font ) {
  153. if (strpos($subtype, $sub) !== false) {
  154. return $cache[$family_raw][$subtype_raw] = $font;
  155. }
  156. }
  157. if ($subtype !== "normal") {
  158. foreach ( $family as $sub => $font ) {
  159. if ($sub !== "normal") {
  160. return $cache[$family_raw][$subtype_raw] = $font;
  161. }
  162. }
  163. }
  164. $subtype = "normal";
  165. if ( isset($family[$subtype]) ) {
  166. return $cache[$family_raw][$subtype_raw] = $family[$subtype];
  167. }
  168. return null;
  169. }
  170. static function get_family($family) {
  171. $family = str_replace( array("'", '"'), "", mb_strtolower($family));
  172. if ( isset(self::$_font_lookup[$family]) ) {
  173. return self::$_font_lookup[$family];
  174. }
  175. return null;
  176. }
  177. /**
  178. * Saves the stored font family cache
  179. *
  180. * The name and location of the cache file are determined by {@link
  181. * Font_Metrics::CACHE_FILE}. This file should be writable by the
  182. * webserver process.
  183. *
  184. * @see Font_Metrics::load_font_families()
  185. */
  186. static function save_font_families() {
  187. // replace the path to the DOMPDF font directory with "DOMPDF_FONT_DIR" (allows for more portability)
  188. $cache_data = var_export(self::$_font_lookup, true);
  189. $cache_data = str_replace('\''.DOMPDF_FONT_DIR , 'DOMPDF_FONT_DIR . \'' , $cache_data);
  190. $cache_data = "<"."?php return $cache_data ?".">";
  191. file_put_contents(self::CACHE_FILE, $cache_data);
  192. }
  193. /**
  194. * Loads the stored font family cache
  195. *
  196. * @see save_font_families()
  197. */
  198. static function load_font_families() {
  199. if ( !is_readable(self::CACHE_FILE) ) {
  200. return;
  201. }
  202. self::$_font_lookup = require_once self::CACHE_FILE;
  203. // If the font family cache is still in the old format
  204. if ( self::$_font_lookup === 1 ) {
  205. $cache_data = file_get_contents(self::CACHE_FILE);
  206. file_put_contents(self::CACHE_FILE, "<"."?php return $cache_data ?".">");
  207. self::$_font_lookup = require_once self::CACHE_FILE;
  208. }
  209. }
  210. static function get_type($type) {
  211. if (preg_match("/bold/i", $type)) {
  212. if (preg_match("/italic|oblique/i", $type)) {
  213. $type = "bold_italic";
  214. }
  215. else {
  216. $type = "bold";
  217. }
  218. }
  219. elseif (preg_match("/italic|oblique/i", $type)) {
  220. $type = "italic";
  221. }
  222. else {
  223. $type = "normal";
  224. }
  225. return $type;
  226. }
  227. static function install_fonts($files) {
  228. $names = array();
  229. foreach($files as $file) {
  230. $font = Font::load($file);
  231. $records = $font->getData("name", "records");
  232. $type = self::get_type($records[2]);
  233. $names[mb_strtolower($records[1])][$type] = $file;
  234. }
  235. return $names;
  236. }
  237. static function get_system_fonts() {
  238. $files = glob("/usr/share/fonts/truetype/*.ttf") +
  239. glob("/usr/share/fonts/truetype/*/*.ttf") +
  240. glob("/usr/share/fonts/truetype/*/*/*.ttf") +
  241. glob("C:\\Windows\\fonts\\*.ttf") +
  242. glob("C:\\WinNT\\fonts\\*.ttf") +
  243. glob("/mnt/c_drive/WINDOWS/Fonts/");
  244. return self::install_fonts($files);
  245. }
  246. /**
  247. * Returns the current font lookup table
  248. *
  249. * @return array
  250. */
  251. static function get_font_families() {
  252. return self::$_font_lookup;
  253. }
  254. static function set_font_family($fontname, $entry) {
  255. self::$_font_lookup[mb_strtolower($fontname)] = $entry;
  256. }
  257. static function register_font($style, $remote_file) {
  258. $fontname = mb_strtolower($style["family"]);
  259. $families = Font_Metrics::get_font_families();
  260. $entry = array();
  261. if ( isset($families[$fontname]) ) {
  262. $entry = $families[$fontname];
  263. }
  264. $local_file = DOMPDF_FONT_DIR . md5($remote_file);
  265. $cache_entry = $local_file;
  266. $local_file .= ".ttf";
  267. $style_string = Font_Metrics::get_type("{$style['weight']} {$style['style']}");
  268. if ( !isset($entry[$style_string]) ) {
  269. $entry[$style_string] = $cache_entry;
  270. Font_Metrics::set_font_family($fontname, $entry);
  271. // Download the remote file
  272. if ( !is_file($local_file) ) {
  273. file_put_contents($local_file, file_get_contents($remote_file));
  274. }
  275. $font = Font::load($local_file);
  276. if (!$font) {
  277. return false;
  278. }
  279. $font->parse();
  280. $font->saveAdobeFontMetrics("$cache_entry.ufm");
  281. // Save the changes
  282. Font_Metrics::save_font_families();
  283. }
  284. return true;
  285. }
  286. }
  287. Font_Metrics::load_font_families();