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.digitaljunkies.ca/dompdf
|
---|
32 | *
|
---|
33 | * @link http://www.digitaljunkies.ca/dompdf
|
---|
34 | * @copyright 2004 Benj Carson
|
---|
35 | * @author Benj Carson <benjcarson@digitaljunkies.ca>
|
---|
36 | * @contributor Helmut Tischer <htischer@weihenstephan.org>
|
---|
37 | * @package dompdf
|
---|
38 | * @version 0.5.1
|
---|
39 | *
|
---|
40 | * Changes
|
---|
41 | * @contributor Helmut Tischer <htischer@weihenstephan.org>
|
---|
42 | * @version 0.5.1.htischer.20090507
|
---|
43 | * - On missing font on explicite font selection don't change subtype and don't return default font.
|
---|
44 | * - On requesting default font and missing subtype, check similar subtypes, then any subtype, then normal. The last must exist.
|
---|
45 | * - Add comments
|
---|
46 | */
|
---|
47 |
|
---|
48 | /* $Id: font_metrics.cls.php 186 2009-10-19 22:42:06Z eclecticgeek@gmail.com $ */
|
---|
49 |
|
---|
50 | require_once(DOMPDF_LIB_DIR . "/class.pdf.php");
|
---|
51 |
|
---|
52 | /**
|
---|
53 | * Name of the font cache file
|
---|
54 | *
|
---|
55 | * This file must be writable by the webserver process only to update it
|
---|
56 | * with save_font_families() after adding the .afm file references of a new font family
|
---|
57 | * with Font_Metrics::save_font_families().
|
---|
58 | * This is typically done only from command line with load_font.php on converting
|
---|
59 | * ttf fonts to afm with an external tool referenced in the define _TTF2AFM
|
---|
60 | *
|
---|
61 | * Declared here because PHP5 prevents constants from being declared with expressions
|
---|
62 | */
|
---|
63 | if (file_exists(DOMPDF_FONT_DIR . "dompdf_font_family_cache")) {
|
---|
64 | define('__DOMPDF_FONT_CACHE_FILE', DOMPDF_FONT_DIR . "dompdf_font_family_cache");
|
---|
65 | } else {
|
---|
66 | define('__DOMPDF_FONT_CACHE_FILE', DOMPDF_FONT_DIR . "dompdf_font_family_cache.dist");
|
---|
67 | }
|
---|
68 |
|
---|
69 |
|
---|
70 | /**
|
---|
71 | * The font metrics class
|
---|
72 | *
|
---|
73 | * This class provides information about fonts and text. It can resolve
|
---|
74 | * font names into actual installed font files, as well as determine the
|
---|
75 | * size of text in a particular font and size.
|
---|
76 | *
|
---|
77 | * @static
|
---|
78 | * @package dompdf
|
---|
79 | */
|
---|
80 | class Font_Metrics {
|
---|
81 |
|
---|
82 | /**
|
---|
83 | * @see __DOMPDF_FONT_CACHE_FILE
|
---|
84 | */
|
---|
85 | const CACHE_FILE = __DOMPDF_FONT_CACHE_FILE;
|
---|
86 |
|
---|
87 | /**
|
---|
88 | * Underlying {@link Cpdf} object to perform text size calculations
|
---|
89 | *
|
---|
90 | * @var Cpdf
|
---|
91 | */
|
---|
92 | static protected $_pdf = null;
|
---|
93 |
|
---|
94 | /**
|
---|
95 | * Array of font family names to font files
|
---|
96 | *
|
---|
97 | * Usually cached by the {@link load_font.php} script
|
---|
98 | *
|
---|
99 | * @var array
|
---|
100 | */
|
---|
101 | static protected $_font_lookup = array();
|
---|
102 |
|
---|
103 |
|
---|
104 | /**
|
---|
105 | * Class initialization
|
---|
106 | *
|
---|
107 | */
|
---|
108 | static function init() {
|
---|
109 | if (!self::$_pdf) {
|
---|
110 | self::load_font_families();
|
---|
111 | self::$_pdf = Canvas_Factory::get_instance();
|
---|
112 | }
|
---|
113 | }
|
---|
114 |
|
---|
115 | /**
|
---|
116 | * Calculates text size, in points
|
---|
117 | *
|
---|
118 | * @param string $text the text to be sized
|
---|
119 | * @param string $font the desired font
|
---|
120 | * @param float $size the desired font size
|
---|
121 | * @param float $spacing word spacing, if any
|
---|
122 | * @return float
|
---|
123 | */
|
---|
124 | static function get_text_width($text, $font, $size, $spacing = 0) {
|
---|
125 | return self::$_pdf->get_text_width($text, $font, $size, $spacing);
|
---|
126 | }
|
---|
127 |
|
---|
128 | /**
|
---|
129 | * Calculates font height
|
---|
130 | *
|
---|
131 | * @param string $font
|
---|
132 | * @param float $size
|
---|
133 | * @return float
|
---|
134 | */
|
---|
135 | static function get_font_height($font, $size) {
|
---|
136 | return self::$_pdf->get_font_height($font, $size);
|
---|
137 | }
|
---|
138 |
|
---|
139 | /**
|
---|
140 | * Resolves a font family & subtype into an actual font file
|
---|
141 | *
|
---|
142 | * Subtype can be one of 'normal', 'bold', 'italic' or 'bold_italic'. If
|
---|
143 | * the particular font family has no suitable font file, the default font
|
---|
144 | * ({@link DOMPDF_DEFAULT_FONT}) is used. The font file returned
|
---|
145 | * is the absolute pathname to the font file on the system.
|
---|
146 | *
|
---|
147 | * @param string $family
|
---|
148 | * @param string $subtype
|
---|
149 | * @return string
|
---|
150 | */
|
---|
151 | static function get_font($family, $subtype = "normal") {
|
---|
152 |
|
---|
153 | /* Allow calling for various fonts in search path. Therefore not immediately
|
---|
154 | * return replacement on non match.
|
---|
155 | * Only when called with NULL try replacement.
|
---|
156 | * When this is also missing there is really trouble.
|
---|
157 | * If only the subtype fails, nevertheless return failure.
|
---|
158 | * Only on checking the fallback font, check various subtypes on same font.
|
---|
159 | */
|
---|
160 |
|
---|
161 | if ( $family ) {
|
---|
162 | $family = str_replace( array("'", '"'), "", mb_strtolower($family));
|
---|
163 | $subtype = mb_strtolower($subtype);
|
---|
164 |
|
---|
165 | if ( isset(self::$_font_lookup[$family][$subtype]) ) {
|
---|
166 | return self::$_font_lookup[$family][$subtype];
|
---|
167 | }
|
---|
168 | return null;
|
---|
169 | }
|
---|
170 |
|
---|
171 | $family = DOMPDF_DEFAULT_FONT;
|
---|
172 |
|
---|
173 | if ( isset(self::$_font_lookup[$family][$subtype]) ) {
|
---|
174 | return self::$_font_lookup[$family][$subtype];
|
---|
175 | }
|
---|
176 |
|
---|
177 | foreach ( self::$_font_lookup[$family] as $sub => $font ) {
|
---|
178 | if (strpos($subtype, $sub) !== false) {
|
---|
179 | return $font;
|
---|
180 | }
|
---|
181 | }
|
---|
182 |
|
---|
183 | if ($subtype != "normal") {
|
---|
184 | foreach ( self::$_font_lookup[$family] as $sub => $font ) {
|
---|
185 | if ($sub != "normal") {
|
---|
186 | return $font;
|
---|
187 | }
|
---|
188 | }
|
---|
189 | }
|
---|
190 |
|
---|
191 | $subtype = "normal";
|
---|
192 |
|
---|
193 | if ( isset(self::$_font_lookup[$family][$subtype]) ) {
|
---|
194 | return self::$_font_lookup[$family][$subtype];
|
---|
195 | }
|
---|
196 | return null;
|
---|
197 | }
|
---|
198 |
|
---|
199 | /**
|
---|
200 | * Saves the stored font family cache
|
---|
201 | *
|
---|
202 | * The name and location of the cache file are determined by {@link
|
---|
203 | * Font_Metrics::CACHE_FILE}. This file should be writable by the
|
---|
204 | * webserver process.
|
---|
205 | *
|
---|
206 | * @see Font_Metrics::load_font_families()
|
---|
207 | */
|
---|
208 | static function save_font_families() {
|
---|
209 |
|
---|
210 | file_put_contents(self::CACHE_FILE, var_export(self::$_font_lookup, true));
|
---|
211 |
|
---|
212 | }
|
---|
213 |
|
---|
214 | /**
|
---|
215 | * Loads the stored font family cache
|
---|
216 | *
|
---|
217 | * @see save_font_families()
|
---|
218 | */
|
---|
219 | static function load_font_families() {
|
---|
220 | if ( !is_readable(self::CACHE_FILE) )
|
---|
221 | return;
|
---|
222 |
|
---|
223 | $data = file_get_contents(self::CACHE_FILE);
|
---|
224 |
|
---|
225 | if ( $data != "" )
|
---|
226 | eval ('self::$_font_lookup = ' . $data . ";");
|
---|
227 | }
|
---|
228 |
|
---|
229 | /**
|
---|
230 | * Returns the current font lookup table
|
---|
231 | *
|
---|
232 | * @return array
|
---|
233 | */
|
---|
234 | static function get_font_families() {
|
---|
235 | return self::$_font_lookup;
|
---|
236 | }
|
---|
237 |
|
---|
238 | static function set_font_family($fontname, $entry) {
|
---|
239 | self::$_font_lookup[mb_strtolower($fontname)] = $entry;
|
---|
240 | }
|
---|
241 | }
|
---|
242 |
|
---|
243 | Font_Metrics::init();
|
---|
244 |
|
---|
245 | ?> |
---|