PageRenderTime 53ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/smartpdf/code/include/FontMetrics.php

https://bitbucket.org/1blankz7/bibioteka
PHP | 299 lines | 165 code | 14 blank | 120 comment | 5 complexity | b19fd270b081f18e59abaf4e37b6ae2c MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /**
  3. * DOMPDF - PHP5 HTML to PDF renderer
  4. *
  5. * File: $RCSfile: font_metrics.cls.php,v $
  6. * Created on: 2004-06-02
  7. *
  8. * Copyright (c) 2004 - Benj Carson <benjcarson@digitaljunkies.ca>
  9. *
  10. * This library is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU Lesser General Public
  12. * License as published by the Free Software Foundation; either
  13. * version 2.1 of the License, or (at your option) any later version.
  14. *
  15. * This library is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public License
  21. * along with this library in the file LICENSE.LGPL; if not, write to the
  22. * Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  23. * 02111-1307 USA
  24. *
  25. * Alternatively, you may distribute this software under the terms of the
  26. * PHP License, version 3.0 or later. A copy of this license should have
  27. * been distributed with this file in the file LICENSE.PHP . If this is not
  28. * the case, you can obtain a copy at http://www.php.net/license/3_0.txt.
  29. *
  30. * The latest version of DOMPDF might be available at:
  31. * http://www.dompdf.com/
  32. *
  33. * @link http://www.dompdf.com/
  34. * @copyright 2004 Benj Carson
  35. * @author Benj Carson <benjcarson@digitaljunkies.ca>
  36. * @contributor Helmut Tischer <htischer@weihenstephan.org>
  37. * @package dompdf
  38. *
  39. * Changes
  40. * @contributor Helmut Tischer <htischer@weihenstephan.org>
  41. * @version 0.5.1.htischer.20090507
  42. * - On missing font on explicite font selection don't change subtype and don't return default font.
  43. * - On requesting default font and missing subtype, check similar subtypes, then any subtype, then normal. The last must exist.
  44. * - Add comments
  45. */
  46. /* $Id: font_metrics.cls.php 314 2010-09-14 11:35:41Z fabien.menager $ */
  47. require_once(DOMPDF_LIB_DIR . "/Cpdf.php");
  48. /**
  49. * Name of the font cache file
  50. *
  51. * This file must be writable by the webserver process only to update it
  52. * with save_font_families() after adding the .afm file references of a new font family
  53. * with Font_Metrics::save_font_families().
  54. * This is typically done only from command line with load_font.php on converting
  55. * ttf fonts to afm with an external tool referenced in the define _TTF2AFM
  56. *
  57. * Declared here because PHP5 prevents constants from being declared with expressions
  58. */
  59. if (!defined("__DOMPDF_FONT_CACHE_FILE")) {
  60. if (file_exists(DOMPDF_FONT_DIR . "dompdf_font_family_cache")) {
  61. define('__DOMPDF_FONT_CACHE_FILE', DOMPDF_FONT_DIR . "dompdf_font_family_cache");
  62. } else {
  63. define('__DOMPDF_FONT_CACHE_FILE', DOMPDF_FONT_DIR . "dompdf_font_family_cache.dist.php");
  64. }
  65. }
  66. /**
  67. * The font metrics class
  68. *
  69. * This class provides information about fonts and text. It can resolve
  70. * font names into actual installed font files, as well as determine the
  71. * size of text in a particular font and size.
  72. *
  73. * @static
  74. * @package dompdf
  75. */
  76. class FontMetrics {
  77. /**
  78. * @see __DOMPDF_FONT_CACHE_FILE
  79. */
  80. const CACHE_FILE = __DOMPDF_FONT_CACHE_FILE;
  81. /**
  82. * Underlying {@link Canvas} object to perform text size calculations
  83. *
  84. * @var Canvas
  85. */
  86. static protected $_pdf = null;
  87. /**
  88. * Array of font family names to font files
  89. *
  90. * Usually cached by the {@link load_font.php} script
  91. *
  92. * @var array
  93. */
  94. static protected $_font_lookup = array();
  95. /**
  96. * Class initialization
  97. *
  98. */
  99. static function init() {
  100. if (!self::$_pdf) {
  101. self::load_font_families();
  102. self::$_pdf = CanvasFactory::get_instance();
  103. }
  104. }
  105. /**
  106. * Calculates text size, in points
  107. *
  108. * @param string $text the text to be sized
  109. * @param string $font the desired font
  110. * @param float $size the desired font size
  111. * @param float $spacing word spacing, if any
  112. * @return float
  113. */
  114. static function get_text_width($text, $font, $size, $spacing = 0) {
  115. return self::$_pdf->get_text_width($text, $font, $size, $spacing);
  116. }
  117. /**
  118. * Calculates font height
  119. *
  120. * @param string $font
  121. * @param float $size
  122. * @return float
  123. */
  124. static function get_font_height($font, $size) {
  125. return self::$_pdf->get_font_height($font, $size);
  126. }
  127. /**
  128. * Resolves a font family & subtype into an actual font file
  129. *
  130. * Subtype can be one of 'normal', 'bold', 'italic' or 'bold_italic'. If
  131. * the particular font family has no suitable font file, the default font
  132. * ({@link DOMPDF_DEFAULT_FONT}) is used. The font file returned
  133. * is the absolute pathname to the font file on the system.
  134. *
  135. * @param string $family
  136. * @param string $subtype
  137. * @return string
  138. */
  139. static function get_font($family, $subtype = "normal") {
  140. /* Allow calling for various fonts in search path. Therefore not immediately
  141. * return replacement on non match.
  142. * Only when called with NULL try replacement.
  143. * When this is also missing there is really trouble.
  144. * If only the subtype fails, nevertheless return failure.
  145. * Only on checking the fallback font, check various subtypes on same font.
  146. */
  147. if ( $family ) {
  148. $family = str_replace( array("'", '"'), "", mb_strtolower($family));
  149. $subtype = mb_strtolower($subtype);
  150. if ( isset(self::$_font_lookup[$family][$subtype]) ) {
  151. return self::$_font_lookup[$family][$subtype];
  152. }
  153. return null;
  154. }
  155. $family = DOMPDF_DEFAULT_FONT;
  156. if ( isset(self::$_font_lookup[$family][$subtype]) ) {
  157. return self::$_font_lookup[$family][$subtype];
  158. }
  159. foreach ( self::$_font_lookup[$family] as $sub => $font ) {
  160. if (strpos($subtype, $sub) !== false) {
  161. return $font;
  162. }
  163. }
  164. if ($subtype !== "normal") {
  165. foreach ( self::$_font_lookup[$family] as $sub => $font ) {
  166. if ($sub !== "normal") {
  167. return $font;
  168. }
  169. }
  170. }
  171. $subtype = "normal";
  172. if ( isset(self::$_font_lookup[$family][$subtype]) ) {
  173. return self::$_font_lookup[$family][$subtype];
  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. self::$_font_lookup = require_once(self::CACHE_FILE);
  202. // If the font family cache is still in the old format
  203. if ( self::$_font_lookup === 1 ) {
  204. $cache_data = file_get_contents(self::CACHE_FILE);
  205. file_put_contents(self::CACHE_FILE, "<"."?php return $cache_data ?".">");
  206. self::$_font_lookup = require_once(self::CACHE_FILE);
  207. }
  208. }
  209. static function get_system_fonts() {
  210. $files = glob("/usr/share/fonts/truetype/*.ttf") +
  211. glob("/usr/share/fonts/truetype/*/*.ttf") +
  212. glob("/usr/share/fonts/truetype/*/*/*.ttf") +
  213. glob("C:\\Windows\\fonts\\*.ttf") +
  214. glob("C:\\WinNT\\fonts\\*.ttf") +
  215. glob("/mnt/c_drive/WINDOWS/Fonts/");
  216. new TTF_Info;
  217. $names = array();
  218. foreach($files as $file) {
  219. $info = getFontInfo($file);
  220. $info["path"] = $file;
  221. $type = $info[2];
  222. if (preg_match("/regular|normal|medium|book/i", $type)) {
  223. $type = "normal";
  224. }
  225. elseif (preg_match("/bold/i", $type)) {
  226. if (preg_match("/italic|oblique/i", $type)) {
  227. $type = "bold_italic";
  228. }
  229. else {
  230. $type = "bold";
  231. }
  232. }
  233. elseif (preg_match("/italic|oblique/i", $type)) {
  234. $type = "italic";
  235. }
  236. $names[mb_strtolower($info[1])][$type] = $file;
  237. }
  238. $keys = array_keys($names);
  239. /*$matches = array_intersect(array("times", "times new roman"), $keys);
  240. $names["serif"] = $names[reset($matches)];
  241. $matches = array_intersect(array("helvetica", "arial", "verdana"), $keys);
  242. $names["sans-serif"] = $names[reset($matches)];
  243. $matches = array_intersect(array("courier", "courier new"), $keys);
  244. $names["monospace"] = $names[reset($matches)];
  245. $names["fixed"] = $names[reset($matches)];*/
  246. return $names;
  247. }
  248. /**
  249. * Returns the current font lookup table
  250. *
  251. * @return array
  252. */
  253. static function get_font_families() {
  254. return self::$_font_lookup;
  255. }
  256. static function set_font_family($fontname, $entry) {
  257. self::$_font_lookup[mb_strtolower($fontname)] = $entry;
  258. }
  259. }
  260. FontMetrics::init();