PageRenderTime 206ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/src/FontMetrics.php

https://gitlab.com/oritadeu/dompdf
PHP | 603 lines | 316 code | 74 blank | 213 comment | 23 complexity | 8753b3db71f45343d5746a2f60afe783 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. namespace Dompdf;
  11. use FontLib\Font;
  12. /**
  13. * The font metrics class
  14. *
  15. * This class provides information about fonts and text. It can resolve
  16. * font names into actual installed font files, as well as determine the
  17. * size of text in a particular font and size.
  18. *
  19. * @static
  20. * @package dompdf
  21. */
  22. class FontMetrics
  23. {
  24. /**
  25. * Name of the font cache file
  26. *
  27. * This file must be writable by the webserver process only to update it
  28. * with save_font_families() after adding the .afm file references of a new font family
  29. * with FontMetrics::saveFontFamilies().
  30. * This is typically done only from command line with load_font.php on converting
  31. * ttf fonts to ufm with php-font-lib.
  32. */
  33. const CACHE_FILE = "dompdf_font_family_cache.php";
  34. /**
  35. * @var Canvas
  36. * @deprecated
  37. */
  38. protected $pdf;
  39. /**
  40. * Underlying {@link Canvas} object to perform text size calculations
  41. *
  42. * @var Canvas
  43. */
  44. protected $canvas;
  45. /**
  46. * Array of font family names to font files
  47. *
  48. * Usually cached by the {@link load_font.php} script
  49. *
  50. * @var array
  51. */
  52. protected $fontLookup = array();
  53. /**
  54. * @var Options
  55. */
  56. private $options;
  57. /**
  58. * Class initialization
  59. */
  60. public function __construct(Canvas $canvas, Options $options)
  61. {
  62. $this->setCanvas($canvas);
  63. $this->setOptions($options);
  64. $this->loadFontFamilies();
  65. }
  66. /**
  67. * @deprecated
  68. */
  69. public function save_font_families()
  70. {
  71. $this->saveFontFamilies();
  72. }
  73. /**
  74. * Saves the stored font family cache
  75. *
  76. * The name and location of the cache file are determined by {@link
  77. * FontMetrics::CACHE_FILE}. This file should be writable by the
  78. * webserver process.
  79. *
  80. * @see Font_Metrics::load_font_families()
  81. */
  82. public function saveFontFamilies()
  83. {
  84. // replace the path to the DOMPDF font directories with the corresponding constants (allows for more portability)
  85. $cacheData = sprintf("<?php return array (%s", PHP_EOL);
  86. foreach ($this->fontLookup as $family => $variants) {
  87. $cacheData .= sprintf(" '%s' => array(%s", addslashes($family), PHP_EOL);
  88. foreach ($variants as $variant => $path) {
  89. $path = sprintf("'%s'", $path);
  90. $path = str_replace('\'' . $this->getOptions()->getFontDir() , '$fontDir . \'' , $path);
  91. $path = str_replace('\'' . $this->getOptions()->getRootDir() , '$rootDir . \'' , $path);
  92. $cacheData .= sprintf(" '%s' => %s,%s", $variant, $path, PHP_EOL);
  93. }
  94. $cacheData .= sprintf(" ),%s", PHP_EOL);
  95. }
  96. $cacheData .= ") ?>";
  97. file_put_contents($this->getCacheFile(), $cacheData);
  98. }
  99. /**
  100. * @deprecated
  101. */
  102. public function load_font_families()
  103. {
  104. $this->loadFontFamilies();
  105. }
  106. /**
  107. * Loads the stored font family cache
  108. *
  109. * @see save_font_families()
  110. */
  111. public function loadFontFamilies()
  112. {
  113. $fontDir = $this->getOptions()->getFontDir();
  114. $rootDir = $this->getOptions()->getRootDir();
  115. // FIXME: tempoarary define constants for cache files <= v0.6.2
  116. if (!defined("DOMPDF_DIR")) { define("DOMPDF_DIR", $rootDir); }
  117. if (!defined("DOMPDF_FONT_DIR")) { define("DOMPDF_FONT_DIR", $fontDir); }
  118. $file = $rootDir . "/lib/fonts/dompdf_font_family_cache.dist.php";
  119. $distFonts = require $file;
  120. // FIXME: temporary step for font cache created before the font cache fix
  121. if (is_readable($fontDir . DIRECTORY_SEPARATOR . "dompdf_font_family_cache")) {
  122. $oldFonts = require $fontDir . DIRECTORY_SEPARATOR . "dompdf_font_family_cache";
  123. // If the font family cache is still in the old format
  124. if ($oldFonts === 1) {
  125. $cacheData = file_get_contents($fontDir . DIRECTORY_SEPARATOR . "dompdf_font_family_cache");
  126. file_put_contents($fontDir . DIRECTORY_SEPARATOR . "dompdf_font_family_cache", "<" . "?php return $cacheData ?" . ">");
  127. $oldFonts = require $fontDir . DIRECTORY_SEPARATOR . "dompdf_font_family_cache";
  128. }
  129. $distFonts += $oldFonts;
  130. }
  131. if (!is_readable($this->getCacheFile())) {
  132. $this->fontLookup = $distFonts;
  133. return;
  134. }
  135. $cacheData = require $this->getCacheFile();
  136. // If the font family cache is still in the old format
  137. if ($cacheData === 1) {
  138. $cacheData = file_get_contents($this->getCacheFile());
  139. file_put_contents($this->getCacheFile(), "<" . "?php return $cacheData ?" . ">");
  140. $this->fontLookup = require $this->getCacheFile();
  141. }
  142. $this->fontLookup = array();
  143. foreach ($cacheData as $key => $value) {
  144. $this->fontLookup[stripslashes($key)] = $value;
  145. }
  146. // Merge provided fonts
  147. $this->fontLookup += $distFonts;
  148. }
  149. /**
  150. * @param array $files
  151. * @return array
  152. * @deprecated
  153. */
  154. public function install_fonts($files)
  155. {
  156. return $this->installFonts($files);
  157. }
  158. /**
  159. * @param array $files
  160. * @return array
  161. */
  162. public function installFonts(array $files)
  163. {
  164. $names = array();
  165. foreach ($files as $file) {
  166. $font = Font::load($file);
  167. $records = $font->getData("name", "records");
  168. $type = $this->getType($records[2]);
  169. $names[mb_strtolower($records[1])][$type] = $file;
  170. $font->close();
  171. }
  172. return $names;
  173. }
  174. /**
  175. * @param array $style
  176. * @param string $remote_file
  177. * @param resource $context
  178. * @return bool
  179. * @deprecated
  180. */
  181. public function register_font($style, $remote_file, $context = null)
  182. {
  183. return $this->registerFont($style, $remote_file);
  184. }
  185. /**
  186. * @param array $style
  187. * @param string $remoteFile
  188. * @param resource $context
  189. * @return bool
  190. */
  191. public function registerFont($style, $remoteFile, $context = null)
  192. {
  193. $fontDir = $this->getOptions()->getFontDir();
  194. $fontname = mb_strtolower($style["family"]);
  195. $families = $this->getFontFamilies();
  196. $entry = array();
  197. if (isset($families[$fontname])) {
  198. $entry = $families[$fontname];
  199. }
  200. $localFile = $fontDir . DIRECTORY_SEPARATOR . md5($remoteFile);
  201. $localTempFile = $this->options->get('tempDir') . "/" . md5($remoteFile);
  202. $cacheEntry = $localFile;
  203. $localFile .= ".ttf";
  204. $styleString = $this->getType("{$style['weight']} {$style['style']}");
  205. if ( !isset($entry[$styleString]) ) {
  206. $entry[$styleString] = $cacheEntry;
  207. // Download the remote file
  208. $remoteFileContent = @file_get_contents($remoteFile, null, $context);
  209. if (false === $remoteFileContent) {
  210. return false;
  211. }
  212. file_put_contents($localTempFile, $remoteFileContent);
  213. $font = Font::load($localTempFile);
  214. if (!$font) {
  215. unlink($localTempFile);
  216. return false;
  217. }
  218. $font->parse();
  219. $font->saveAdobeFontMetrics("$cacheEntry.ufm");
  220. $font->close();
  221. unlink($localTempFile);
  222. if ( !file_exists("$cacheEntry.ufm") ) {
  223. return false;
  224. }
  225. // Save the changes
  226. file_put_contents($localFile, file_get_contents($remoteFile, null, $context));
  227. if ( !file_exists($localFile) ) {
  228. unlink("$cacheEntry.ufm");
  229. return false;
  230. }
  231. $this->setFontFamily($fontname, $entry);
  232. $this->saveFontFamilies();
  233. }
  234. return true;
  235. }
  236. /**
  237. * @param $text
  238. * @param $font
  239. * @param $size
  240. * @param float $word_spacing
  241. * @param float $char_spacing
  242. * @return float
  243. * @deprecated
  244. */
  245. public function get_text_width($text, $font, $size, $word_spacing = 0.0, $char_spacing = 0.0)
  246. {
  247. //return self::$_pdf->get_text_width($text, $font, $size, $word_spacing, $char_spacing);
  248. return $this->getTextWidth($text, $font, $size, $word_spacing, $char_spacing);
  249. }
  250. /**
  251. * Calculates text size, in points
  252. *
  253. * @param string $text the text to be sized
  254. * @param string $font the desired font
  255. * @param float $size the desired font size
  256. * @param float $wordSpacing
  257. * @param float $charSpacing
  258. *
  259. * @internal param float $spacing word spacing, if any
  260. * @return float
  261. */
  262. public function getTextWidth($text, $font, $size, $wordSpacing = 0.0, $charSpacing = 0.0)
  263. {
  264. // @todo Make sure this cache is efficient before enabling it
  265. static $cache = array();
  266. if ($text === "") {
  267. return 0;
  268. }
  269. // Don't cache long strings
  270. $useCache = !isset($text[50]); // Faster than strlen
  271. $key = "$font/$size/$wordSpacing/$charSpacing";
  272. if ($useCache && isset($cache[$key][$text])) {
  273. return $cache[$key]["$text"];
  274. }
  275. $width = $this->getCanvas()->get_text_width($text, $font, $size, $wordSpacing, $charSpacing);
  276. if ($useCache) {
  277. $cache[$key][$text] = $width;
  278. }
  279. return $width;
  280. }
  281. /**
  282. * @param $font
  283. * @param $size
  284. * @return float
  285. * @deprecated
  286. */
  287. public function get_font_height($font, $size)
  288. {
  289. return $this->getFontHeight($font, $size);
  290. }
  291. /**
  292. * Calculates font height
  293. *
  294. * @param string $font
  295. * @param float $size
  296. *
  297. * @return float
  298. */
  299. public function getFontHeight($font, $size)
  300. {
  301. return $this->getCanvas()->get_font_height($font, $size);
  302. }
  303. /**
  304. * @param $family_raw
  305. * @param string $subtype_raw
  306. * @return string
  307. * @deprecated
  308. */
  309. public function get_font($family_raw, $subtype_raw = "normal")
  310. {
  311. return $this->getFont($family_raw, $subtype_raw);
  312. }
  313. /**
  314. * Resolves a font family & subtype into an actual font file
  315. * Subtype can be one of 'normal', 'bold', 'italic' or 'bold_italic'. If
  316. * the particular font family has no suitable font file, the default font
  317. * ({@link Options::defaultFont}) is used. The font file returned
  318. * is the absolute pathname to the font file on the system.
  319. *
  320. * @param string $familyRaw
  321. * @param string $subtypeRaw
  322. *
  323. * @return string
  324. */
  325. public function getFont($familyRaw, $subtypeRaw = "normal")
  326. {
  327. static $cache = array();
  328. if (isset($cache[$familyRaw][$subtypeRaw])) {
  329. return $cache[$familyRaw][$subtypeRaw];
  330. }
  331. /* Allow calling for various fonts in search path. Therefore not immediately
  332. * return replacement on non match.
  333. * Only when called with NULL try replacement.
  334. * When this is also missing there is really trouble.
  335. * If only the subtype fails, nevertheless return failure.
  336. * Only on checking the fallback font, check various subtypes on same font.
  337. */
  338. $subtype = strtolower($subtypeRaw);
  339. if ($familyRaw) {
  340. $family = str_replace(array("'", '"'), "", strtolower($familyRaw));
  341. if (isset($this->fontLookup[$family][$subtype])) {
  342. return $cache[$familyRaw][$subtypeRaw] = $this->fontLookup[$family][$subtype];
  343. }
  344. return null;
  345. }
  346. $family = "serif";
  347. if (isset($this->fontLookup[$family][$subtype])) {
  348. return $cache[$familyRaw][$subtypeRaw] = $this->fontLookup[$family][$subtype];
  349. }
  350. if (!isset($this->fontLookup[$family])) {
  351. return null;
  352. }
  353. $family = $this->fontLookup[$family];
  354. foreach ($family as $sub => $font) {
  355. if (strpos($subtype, $sub) !== false) {
  356. return $cache[$familyRaw][$subtypeRaw] = $font;
  357. }
  358. }
  359. if ($subtype !== "normal") {
  360. foreach ($family as $sub => $font) {
  361. if ($sub !== "normal") {
  362. return $cache[$familyRaw][$subtypeRaw] = $font;
  363. }
  364. }
  365. }
  366. $subtype = "normal";
  367. if (isset($family[$subtype])) {
  368. return $cache[$familyRaw][$subtypeRaw] = $family[$subtype];
  369. }
  370. return null;
  371. }
  372. /**
  373. * @param $family
  374. * @return null|string
  375. * @deprecated
  376. */
  377. public function get_family($family)
  378. {
  379. return $this->getFamily($family);
  380. }
  381. /**
  382. * @param string $family
  383. * @return null|string
  384. */
  385. public function getFamily($family)
  386. {
  387. $family = str_replace(array("'", '"'), "", mb_strtolower($family));
  388. if (isset($this->fontLookup[$family])) {
  389. return $this->fontLookup[$family];
  390. }
  391. return null;
  392. }
  393. /**
  394. * @param $type
  395. * @return string
  396. * @deprecated
  397. */
  398. public function get_type($type)
  399. {
  400. return $this->getType($type);
  401. }
  402. /**
  403. * @param string $type
  404. * @return string
  405. */
  406. public function getType($type)
  407. {
  408. if (preg_match("/bold/i", $type)) {
  409. if (preg_match("/italic|oblique/i", $type)) {
  410. $type = "bold_italic";
  411. } else {
  412. $type = "bold";
  413. }
  414. } elseif (preg_match("/italic|oblique/i", $type)) {
  415. $type = "italic";
  416. } else {
  417. $type = "normal";
  418. }
  419. return $type;
  420. }
  421. /**
  422. * @return array
  423. * @deprecated
  424. */
  425. public function get_system_fonts()
  426. {
  427. return $this->getSystemFonts();
  428. }
  429. /**
  430. * @return array
  431. */
  432. public function getSystemFonts()
  433. {
  434. $files = glob("/usr/share/fonts/truetype/*.ttf") +
  435. glob("/usr/share/fonts/truetype/*/*.ttf") +
  436. glob("/usr/share/fonts/truetype/*/*/*.ttf") +
  437. glob("C:\\Windows\\fonts\\*.ttf") +
  438. glob("C:\\WinNT\\fonts\\*.ttf") +
  439. glob("/mnt/c_drive/WINDOWS/Fonts/");
  440. return $this->installFonts($files);
  441. }
  442. /**
  443. * @return array
  444. * @deprecated
  445. */
  446. public function get_font_families()
  447. {
  448. return $this->getFontFamilies();
  449. }
  450. /**
  451. * Returns the current font lookup table
  452. *
  453. * @return array
  454. */
  455. public function getFontFamilies()
  456. {
  457. return $this->fontLookup;
  458. }
  459. /**
  460. * @param string $fontname
  461. * @param mixed $entry
  462. * @deprecated
  463. */
  464. public function set_font_family($fontname, $entry)
  465. {
  466. $this->setFontFamily($fontname, $entry);
  467. }
  468. /**
  469. * @param string $fontname
  470. * @param mixed $entry
  471. */
  472. public function setFontFamily($fontname, $entry)
  473. {
  474. $this->fontLookup[mb_strtolower($fontname)] = $entry;
  475. }
  476. /**
  477. * @return string
  478. */
  479. public function getCacheFile()
  480. {
  481. return $this->getOptions()->getFontDir() . DIRECTORY_SEPARATOR . self::CACHE_FILE;
  482. }
  483. /**
  484. * @param Options $options
  485. * @return $this
  486. */
  487. public function setOptions(Options $options)
  488. {
  489. $this->options = $options;
  490. return $this;
  491. }
  492. /**
  493. * @return Options
  494. */
  495. public function getOptions()
  496. {
  497. return $this->options;
  498. }
  499. /**
  500. * @param Canvas $canvas
  501. * @return $this
  502. */
  503. public function setCanvas(Canvas $canvas)
  504. {
  505. $this->pdf = $canvas;
  506. $this->canvas = $canvas;
  507. return $this;
  508. }
  509. /**
  510. * @return Canvas
  511. */
  512. public function getCanvas()
  513. {
  514. return $this->canvas;
  515. }
  516. }