PageRenderTime 24ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/dompdf/dompdf/include/font_metrics.cls.php

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