PageRenderTime 24ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/sites/all/modules/contrib/civicrm/vendor/dompdf/dompdf/src/FontMetrics.php

https://gitlab.com/virtualrealms/d7civicrm
PHP | 534 lines | 274 code | 65 blank | 195 comment | 21 complexity | 682dac011d8682829bd996639a322604 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. if (!is_readable($this->getCacheFile())) {
  121. $this->fontLookup = $distFonts;
  122. return;
  123. }
  124. $cacheData = require $this->getCacheFile();
  125. $this->fontLookup = array();
  126. if (is_array($this->fontLookup)) {
  127. foreach ($cacheData as $key => $value) {
  128. $this->fontLookup[stripslashes($key)] = $value;
  129. }
  130. }
  131. // Merge provided fonts
  132. $this->fontLookup += $distFonts;
  133. }
  134. /**
  135. * @param array $style
  136. * @param string $remote_file
  137. * @param resource $context
  138. * @return bool
  139. * @deprecated
  140. */
  141. public function register_font($style, $remote_file, $context = null)
  142. {
  143. return $this->registerFont($style, $remote_file);
  144. }
  145. /**
  146. * @param array $style
  147. * @param string $remoteFile
  148. * @param resource $context
  149. * @return bool
  150. */
  151. public function registerFont($style, $remoteFile, $context = null)
  152. {
  153. $fontDir = $this->getOptions()->getFontDir();
  154. $fontname = mb_strtolower($style["family"]);
  155. $families = $this->getFontFamilies();
  156. $entry = array();
  157. if (isset($families[$fontname])) {
  158. $entry = $families[$fontname];
  159. }
  160. $localFile = $fontDir . DIRECTORY_SEPARATOR . md5($remoteFile);
  161. $localTempFile = $this->options->get('tempDir') . "/" . md5($remoteFile);
  162. $cacheEntry = $localFile;
  163. $localFile .= ".".strtolower(pathinfo($remoteFile,PATHINFO_EXTENSION));
  164. $styleString = $this->getType("{$style['weight']} {$style['style']}");
  165. if ( !isset($entry[$styleString]) ) {
  166. $entry[$styleString] = $cacheEntry;
  167. // Download the remote file
  168. list($remoteFileContent, $http_response_header) = @Helpers::getFileContent($remoteFile, $context);
  169. if (false === $remoteFileContent) {
  170. return false;
  171. }
  172. file_put_contents($localTempFile, $remoteFileContent);
  173. $font = Font::load($localTempFile);
  174. if (!$font) {
  175. unlink($localTempFile);
  176. return false;
  177. }
  178. $font->parse();
  179. $font->saveAdobeFontMetrics("$cacheEntry.ufm");
  180. $font->close();
  181. unlink($localTempFile);
  182. if ( !file_exists("$cacheEntry.ufm") ) {
  183. return false;
  184. }
  185. // Save the changes
  186. file_put_contents($localFile, $remoteFileContent);
  187. if ( !file_exists($localFile) ) {
  188. unlink("$cacheEntry.ufm");
  189. return false;
  190. }
  191. $this->setFontFamily($fontname, $entry);
  192. $this->saveFontFamilies();
  193. }
  194. return true;
  195. }
  196. /**
  197. * @param $text
  198. * @param $font
  199. * @param $size
  200. * @param float $word_spacing
  201. * @param float $char_spacing
  202. * @return float
  203. * @deprecated
  204. */
  205. public function get_text_width($text, $font, $size, $word_spacing = 0.0, $char_spacing = 0.0)
  206. {
  207. //return self::$_pdf->get_text_width($text, $font, $size, $word_spacing, $char_spacing);
  208. return $this->getTextWidth($text, $font, $size, $word_spacing, $char_spacing);
  209. }
  210. /**
  211. * Calculates text size, in points
  212. *
  213. * @param string $text the text to be sized
  214. * @param string $font the desired font
  215. * @param float $size the desired font size
  216. * @param float $wordSpacing
  217. * @param float $charSpacing
  218. *
  219. * @internal param float $spacing word spacing, if any
  220. * @return float
  221. */
  222. public function getTextWidth($text, $font, $size, $wordSpacing = 0.0, $charSpacing = 0.0)
  223. {
  224. // @todo Make sure this cache is efficient before enabling it
  225. static $cache = array();
  226. if ($text === "") {
  227. return 0;
  228. }
  229. // Don't cache long strings
  230. $useCache = !isset($text[50]); // Faster than strlen
  231. $key = "$font/$size/$wordSpacing/$charSpacing";
  232. if ($useCache && isset($cache[$key][$text])) {
  233. return $cache[$key]["$text"];
  234. }
  235. $width = $this->getCanvas()->get_text_width($text, $font, $size, $wordSpacing, $charSpacing);
  236. if ($useCache) {
  237. $cache[$key][$text] = $width;
  238. }
  239. return $width;
  240. }
  241. /**
  242. * @param $font
  243. * @param $size
  244. * @return float
  245. * @deprecated
  246. */
  247. public function get_font_height($font, $size)
  248. {
  249. return $this->getFontHeight($font, $size);
  250. }
  251. /**
  252. * Calculates font height
  253. *
  254. * @param string $font
  255. * @param float $size
  256. *
  257. * @return float
  258. */
  259. public function getFontHeight($font, $size)
  260. {
  261. return $this->getCanvas()->get_font_height($font, $size);
  262. }
  263. /**
  264. * @param $family_raw
  265. * @param string $subtype_raw
  266. * @return string
  267. * @deprecated
  268. */
  269. public function get_font($family_raw, $subtype_raw = "normal")
  270. {
  271. return $this->getFont($family_raw, $subtype_raw);
  272. }
  273. /**
  274. * Resolves a font family & subtype into an actual font file
  275. * Subtype can be one of 'normal', 'bold', 'italic' or 'bold_italic'. If
  276. * the particular font family has no suitable font file, the default font
  277. * ({@link Options::defaultFont}) is used. The font file returned
  278. * is the absolute pathname to the font file on the system.
  279. *
  280. * @param string $familyRaw
  281. * @param string $subtypeRaw
  282. *
  283. * @return string
  284. */
  285. public function getFont($familyRaw, $subtypeRaw = "normal")
  286. {
  287. static $cache = array();
  288. if (isset($cache[$familyRaw][$subtypeRaw])) {
  289. return $cache[$familyRaw][$subtypeRaw];
  290. }
  291. /* Allow calling for various fonts in search path. Therefore not immediately
  292. * return replacement on non match.
  293. * Only when called with NULL try replacement.
  294. * When this is also missing there is really trouble.
  295. * If only the subtype fails, nevertheless return failure.
  296. * Only on checking the fallback font, check various subtypes on same font.
  297. */
  298. $subtype = strtolower($subtypeRaw);
  299. if ($familyRaw) {
  300. $family = str_replace(array("'", '"'), "", strtolower($familyRaw));
  301. if (isset($this->fontLookup[$family][$subtype])) {
  302. return $cache[$familyRaw][$subtypeRaw] = $this->fontLookup[$family][$subtype];
  303. }
  304. return null;
  305. }
  306. $family = "serif";
  307. if (isset($this->fontLookup[$family][$subtype])) {
  308. return $cache[$familyRaw][$subtypeRaw] = $this->fontLookup[$family][$subtype];
  309. }
  310. if (!isset($this->fontLookup[$family])) {
  311. return null;
  312. }
  313. $family = $this->fontLookup[$family];
  314. foreach ($family as $sub => $font) {
  315. if (strpos($subtype, $sub) !== false) {
  316. return $cache[$familyRaw][$subtypeRaw] = $font;
  317. }
  318. }
  319. if ($subtype !== "normal") {
  320. foreach ($family as $sub => $font) {
  321. if ($sub !== "normal") {
  322. return $cache[$familyRaw][$subtypeRaw] = $font;
  323. }
  324. }
  325. }
  326. $subtype = "normal";
  327. if (isset($family[$subtype])) {
  328. return $cache[$familyRaw][$subtypeRaw] = $family[$subtype];
  329. }
  330. return null;
  331. }
  332. /**
  333. * @param $family
  334. * @return null|string
  335. * @deprecated
  336. */
  337. public function get_family($family)
  338. {
  339. return $this->getFamily($family);
  340. }
  341. /**
  342. * @param string $family
  343. * @return null|string
  344. */
  345. public function getFamily($family)
  346. {
  347. $family = str_replace(array("'", '"'), "", mb_strtolower($family));
  348. if (isset($this->fontLookup[$family])) {
  349. return $this->fontLookup[$family];
  350. }
  351. return null;
  352. }
  353. /**
  354. * @param $type
  355. * @return string
  356. * @deprecated
  357. */
  358. public function get_type($type)
  359. {
  360. return $this->getType($type);
  361. }
  362. /**
  363. * @param string $type
  364. * @return string
  365. */
  366. public function getType($type)
  367. {
  368. if (preg_match("/bold/i", $type)) {
  369. if (preg_match("/italic|oblique/i", $type)) {
  370. $type = "bold_italic";
  371. } else {
  372. $type = "bold";
  373. }
  374. } elseif (preg_match("/italic|oblique/i", $type)) {
  375. $type = "italic";
  376. } else {
  377. $type = "normal";
  378. }
  379. return $type;
  380. }
  381. /**
  382. * @return array
  383. * @deprecated
  384. */
  385. public function get_font_families()
  386. {
  387. return $this->getFontFamilies();
  388. }
  389. /**
  390. * Returns the current font lookup table
  391. *
  392. * @return array
  393. */
  394. public function getFontFamilies()
  395. {
  396. return $this->fontLookup;
  397. }
  398. /**
  399. * @param string $fontname
  400. * @param mixed $entry
  401. * @deprecated
  402. */
  403. public function set_font_family($fontname, $entry)
  404. {
  405. $this->setFontFamily($fontname, $entry);
  406. }
  407. /**
  408. * @param string $fontname
  409. * @param mixed $entry
  410. */
  411. public function setFontFamily($fontname, $entry)
  412. {
  413. $this->fontLookup[mb_strtolower($fontname)] = $entry;
  414. }
  415. /**
  416. * @return string
  417. */
  418. public function getCacheFile()
  419. {
  420. return $this->getOptions()->getFontDir() . DIRECTORY_SEPARATOR . self::CACHE_FILE;
  421. }
  422. /**
  423. * @param Options $options
  424. * @return $this
  425. */
  426. public function setOptions(Options $options)
  427. {
  428. $this->options = $options;
  429. return $this;
  430. }
  431. /**
  432. * @return Options
  433. */
  434. public function getOptions()
  435. {
  436. return $this->options;
  437. }
  438. /**
  439. * @param Canvas $canvas
  440. * @return $this
  441. */
  442. public function setCanvas(Canvas $canvas)
  443. {
  444. $this->canvas = $canvas;
  445. // Still write deprecated pdf for now. It might be used by a parent class.
  446. $this->pdf = $canvas;
  447. return $this;
  448. }
  449. /**
  450. * @return Canvas
  451. */
  452. public function getCanvas()
  453. {
  454. return $this->canvas;
  455. }
  456. }