PageRenderTime 46ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/4gdevs/online-class-record-system
PHP | 382 lines | 210 code | 57 blank | 115 comment | 21 complexity | 947ff4c6f1009b120ea372810303bc53 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 = sprintf("<?php return array (%s", PHP_EOL);
  182. foreach (self::$_font_lookup as $family => $variants) {
  183. $cache_data .= sprintf(" '%s' => array(%s", addslashes($family), PHP_EOL);
  184. foreach ($variants as $variant => $path) {
  185. $path = sprintf("'%s'", $path);
  186. $path = str_replace('\'' . DOMPDF_FONT_DIR , 'DOMPDF_FONT_DIR . \'' , $path);
  187. $path = str_replace('\'' . DOMPDF_DIR , 'DOMPDF_DIR . \'' , $path);
  188. $cache_data .= sprintf(" '%s' => %s,%s", $variant, $path, PHP_EOL);
  189. }
  190. $cache_data .= sprintf(" ),%s", PHP_EOL);
  191. }
  192. $cache_data .= ") ?>";
  193. file_put_contents(self::CACHE_FILE, $cache_data);
  194. }
  195. /**
  196. * Loads the stored font family cache
  197. *
  198. * @see save_font_families()
  199. */
  200. static function load_font_families() {
  201. $dist_fonts = require_once DOMPDF_DIR . "/lib/fonts/dompdf_font_family_cache.dist.php";
  202. // FIXME: temporary step for font cache created before the font cache fix
  203. if ( is_readable( DOMPDF_FONT_DIR . "dompdf_font_family_cache" ) ) {
  204. $old_fonts = require_once DOMPDF_FONT_DIR . "dompdf_font_family_cache";
  205. // If the font family cache is still in the old format
  206. if ( $old_fonts === 1 ) {
  207. $cache_data = file_get_contents(DOMPDF_FONT_DIR . "dompdf_font_family_cache");
  208. file_put_contents(DOMPDF_FONT_DIR . "dompdf_font_family_cache", "<"."?php return $cache_data ?".">");
  209. $old_fonts = require_once DOMPDF_FONT_DIR . "dompdf_font_family_cache";
  210. }
  211. $dist_fonts += $old_fonts;
  212. }
  213. if ( !is_readable(self::CACHE_FILE) ) {
  214. self::$_font_lookup = $dist_fonts;
  215. return;
  216. }
  217. $cache_data = require_once self::CACHE_FILE;
  218. // If the font family cache is still in the old format
  219. if ( self::$_font_lookup === 1 ) {
  220. $cache_data = file_get_contents(self::CACHE_FILE);
  221. file_put_contents(self::CACHE_FILE, "<"."?php return $cache_data ?".">");
  222. $cache_data = require_once self::CACHE_FILE;
  223. }
  224. self::$_font_lookup = array();
  225. foreach ($cache_data as $key => $value) {
  226. self::$_font_lookup[stripslashes($key)] = $value;
  227. }
  228. // Merge provided fonts
  229. self::$_font_lookup += $dist_fonts;
  230. }
  231. static function get_type($type) {
  232. if (preg_match("/bold/i", $type)) {
  233. if (preg_match("/italic|oblique/i", $type)) {
  234. $type = "bold_italic";
  235. }
  236. else {
  237. $type = "bold";
  238. }
  239. }
  240. elseif (preg_match("/italic|oblique/i", $type)) {
  241. $type = "italic";
  242. }
  243. else {
  244. $type = "normal";
  245. }
  246. return $type;
  247. }
  248. static function install_fonts($files) {
  249. $names = array();
  250. foreach($files as $file) {
  251. $font = Font::load($file);
  252. $records = $font->getData("name", "records");
  253. $type = self::get_type($records[2]);
  254. $names[mb_strtolower($records[1])][$type] = $file;
  255. }
  256. return $names;
  257. }
  258. static function get_system_fonts() {
  259. $files = glob("/usr/share/fonts/truetype/*.ttf") +
  260. glob("/usr/share/fonts/truetype/*/*.ttf") +
  261. glob("/usr/share/fonts/truetype/*/*/*.ttf") +
  262. glob("C:\\Windows\\fonts\\*.ttf") +
  263. glob("C:\\WinNT\\fonts\\*.ttf") +
  264. glob("/mnt/c_drive/WINDOWS/Fonts/");
  265. return self::install_fonts($files);
  266. }
  267. /**
  268. * Returns the current font lookup table
  269. *
  270. * @return array
  271. */
  272. static function get_font_families() {
  273. return self::$_font_lookup;
  274. }
  275. static function set_font_family($fontname, $entry) {
  276. self::$_font_lookup[mb_strtolower($fontname)] = $entry;
  277. }
  278. static function register_font($style, $remote_file, $context = null) {
  279. $fontname = mb_strtolower($style["family"]);
  280. $families = Font_Metrics::get_font_families();
  281. $entry = array();
  282. if ( isset($families[$fontname]) ) {
  283. $entry = $families[$fontname];
  284. }
  285. $local_file = DOMPDF_FONT_DIR . md5($remote_file);
  286. $local_temp_file = DOMPDF_TEMP_DIR . "/" . md5($remote_file);
  287. $cache_entry = $local_file;
  288. $local_file .= ".ttf";
  289. $style_string = Font_Metrics::get_type("{$style['weight']} {$style['style']}");
  290. if ( !isset($entry[$style_string]) ) {
  291. $entry[$style_string] = $cache_entry;
  292. // Download the remote file
  293. file_put_contents($local_temp_file, file_get_contents($remote_file, null, $context));
  294. $font = Font::load($local_temp_file);
  295. if (!$font) {
  296. unlink($local_temp_file);
  297. return false;
  298. }
  299. $font->parse();
  300. $font->saveAdobeFontMetrics("$cache_entry.ufm");
  301. unlink($local_temp_file);
  302. if ( !file_exists("$cache_entry.ufm") ) {
  303. return false;
  304. }
  305. // Save the changes
  306. file_put_contents($local_file, file_get_contents($remote_file, null, $context));
  307. Font_Metrics::set_font_family($fontname, $entry);
  308. Font_Metrics::save_font_families();
  309. }
  310. return true;
  311. }
  312. }
  313. Font_Metrics::load_font_families();