font_metrics.cls.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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 216 2010-03-11 22:49:18Z ryan.masten $ */
  47. require_once(DOMPDF_LIB_DIR . "/class.pdf.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 (file_exists(DOMPDF_FONT_DIR . "dompdf_font_family_cache")) {
  60. define('__DOMPDF_FONT_CACHE_FILE', DOMPDF_FONT_DIR . "dompdf_font_family_cache");
  61. } else {
  62. define('__DOMPDF_FONT_CACHE_FILE', DOMPDF_FONT_DIR . "dompdf_font_family_cache.dist");
  63. }
  64. /**
  65. * The font metrics class
  66. *
  67. * This class provides information about fonts and text. It can resolve
  68. * font names into actual installed font files, as well as determine the
  69. * size of text in a particular font and size.
  70. *
  71. * @static
  72. * @package dompdf
  73. */
  74. class Font_Metrics {
  75. /**
  76. * @see __DOMPDF_FONT_CACHE_FILE
  77. */
  78. const CACHE_FILE = __DOMPDF_FONT_CACHE_FILE;
  79. /**
  80. * Underlying {@link Cpdf} object to perform text size calculations
  81. *
  82. * @var Cpdf
  83. */
  84. static protected $_pdf = null;
  85. /**
  86. * Array of font family names to font files
  87. *
  88. * Usually cached by the {@link load_font.php} script
  89. *
  90. * @var array
  91. */
  92. static protected $_font_lookup = array();
  93. /**
  94. * Class initialization
  95. *
  96. */
  97. static function init() {
  98. if (!self::$_pdf) {
  99. self::load_font_families();
  100. self::$_pdf = Canvas_Factory::get_instance();
  101. }
  102. }
  103. /**
  104. * Calculates text size, in points
  105. *
  106. * @param string $text the text to be sized
  107. * @param string $font the desired font
  108. * @param float $size the desired font size
  109. * @param float $spacing word spacing, if any
  110. * @return float
  111. */
  112. static function get_text_width($text, $font, $size, $spacing = 0) {
  113. return self::$_pdf->get_text_width($text, $font, $size, $spacing);
  114. }
  115. /**
  116. * Calculates font height
  117. *
  118. * @param string $font
  119. * @param float $size
  120. * @return float
  121. */
  122. static function get_font_height($font, $size) {
  123. return self::$_pdf->get_font_height($font, $size);
  124. }
  125. /**
  126. * Resolves a font family & subtype into an actual font file
  127. *
  128. * Subtype can be one of 'normal', 'bold', 'italic' or 'bold_italic'. If
  129. * the particular font family has no suitable font file, the default font
  130. * ({@link DOMPDF_DEFAULT_FONT}) is used. The font file returned
  131. * is the absolute pathname to the font file on the system.
  132. *
  133. * @param string $family
  134. * @param string $subtype
  135. * @return string
  136. */
  137. static function get_font($family, $subtype = "normal") {
  138. /* Allow calling for various fonts in search path. Therefore not immediately
  139. * return replacement on non match.
  140. * Only when called with NULL try replacement.
  141. * When this is also missing there is really trouble.
  142. * If only the subtype fails, nevertheless return failure.
  143. * Only on checking the fallback font, check various subtypes on same font.
  144. */
  145. if ( $family ) {
  146. $family = str_replace( array("'", '"'), "", mb_strtolower($family));
  147. $subtype = mb_strtolower($subtype);
  148. if ( isset(self::$_font_lookup[$family][$subtype]) ) {
  149. return self::$_font_lookup[$family][$subtype];
  150. }
  151. return null;
  152. }
  153. $family = DOMPDF_DEFAULT_FONT;
  154. if ( isset(self::$_font_lookup[$family][$subtype]) ) {
  155. return self::$_font_lookup[$family][$subtype];
  156. }
  157. foreach ( self::$_font_lookup[$family] as $sub => $font ) {
  158. if (strpos($subtype, $sub) !== false) {
  159. return $font;
  160. }
  161. }
  162. if ($subtype !== "normal") {
  163. foreach ( self::$_font_lookup[$family] as $sub => $font ) {
  164. if ($sub !== "normal") {
  165. return $font;
  166. }
  167. }
  168. }
  169. $subtype = "normal";
  170. if ( isset(self::$_font_lookup[$family][$subtype]) ) {
  171. return self::$_font_lookup[$family][$subtype];
  172. }
  173. return null;
  174. }
  175. /**
  176. * Saves the stored font family cache
  177. *
  178. * The name and location of the cache file are determined by {@link
  179. * Font_Metrics::CACHE_FILE}. This file should be writable by the
  180. * webserver process.
  181. *
  182. * @see Font_Metrics::load_font_families()
  183. */
  184. static function save_font_families() {
  185. file_put_contents(self::CACHE_FILE, var_export(self::$_font_lookup, true));
  186. }
  187. /**
  188. * Loads the stored font family cache
  189. *
  190. * @see save_font_families()
  191. */
  192. static function load_font_families() {
  193. if ( !is_readable(self::CACHE_FILE) )
  194. return;
  195. $data = file_get_contents(self::CACHE_FILE);
  196. if ( $data != "" )
  197. eval ('self::$_font_lookup = ' . $data . ";");
  198. }
  199. /**
  200. * Returns the current font lookup table
  201. *
  202. * @return array
  203. */
  204. static function get_font_families() {
  205. return self::$_font_lookup;
  206. }
  207. static function set_font_family($fontname, $entry) {
  208. self::$_font_lookup[mb_strtolower($fontname)] = $entry;
  209. }
  210. }
  211. Font_Metrics::init();