source: sandbox/filemanager/tp/dompdf/include/font_metrics.cls.php @ 1575

Revision 1575, 5.4 KB checked in by amuller, 14 years ago (diff)

Ticket #597 - Implentação, melhorias do modulo gerenciador de arquivos

Line 
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 * @package dompdf
37 * @version 0.5.1
38 */
39
40/* $Id: font_metrics.cls.php,v 1.6 2006/07/07 21:31:03 benjcarson Exp $ */
41
42require_once(DOMPDF_LIB_DIR . "/class.pdf.php");
43
44/**
45 * Name of the font cache file
46 *
47 * This file must be writable by the webserver process.  Declared here
48 * because PHP5 prevents constants from being declared with expressions
49 */
50define('__DOMPDF_FONT_CACHE_FILE', DOMPDF_FONT_DIR . "dompdf_font_family_cache");
51
52
53/**
54 * The font metrics class
55 *
56 * This class provides information about fonts and text.  It can resolve
57 * font names into actual installed font files, as well as determine the
58 * size of text in a particular font and size.
59 *
60 * @static
61 * @package dompdf
62 */
63class Font_Metrics {
64
65  /**
66   * @see __DOMPDF_FONT_CACHE_FILE
67   */
68  const CACHE_FILE = __DOMPDF_FONT_CACHE_FILE;
69 
70  /**
71   * Underlying {@link Cpdf} object to perform text size calculations
72   *
73   * @var Cpdf
74   */
75  static protected $_pdf = null;
76
77  /**
78   * Array of font family names to font files
79   *
80   * Usually cached by the {@link load_font.php} script
81   *
82   * @var array
83   */
84  static protected $_font_lookup = array();
85 
86 
87  /**
88   * Class initialization
89   *
90   */
91  static function init() {
92    if (!self::$_pdf) {
93      self::load_font_families();
94      self::$_pdf = Canvas_Factory::get_instance();
95    }
96  }
97
98  /**
99   * Calculates text size, in points
100   *
101   * @param string $text the text to be sized
102   * @param string $font the desired font
103   * @param float  $size the desired font size
104   * @param float  $spacing word spacing, if any
105   * @return float
106   */
107  static function get_text_width($text, $font, $size, $spacing = 0) {
108    return self::$_pdf->get_text_width($text, $font, $size, $spacing);
109  }
110
111  /**
112   * Calculates font height
113   *
114   * @param string $font
115   * @param float $size
116   * @return float
117   */
118  static function get_font_height($font, $size) {
119    return self::$_pdf->get_font_height($font, $size);
120  }
121
122  /**
123   * Resolves a font family & subtype into an actual font file
124   *
125   * Subtype can be one of 'normal', 'bold', 'italic' or 'bold_italic'.  If
126   * the particular font family has no suitable font file, the default font
127   * ({@link DOMPDF_DEFAULT_FONT}) is used.  The font file returned
128   * is the absolute pathname to the font file on the system.
129   *
130   * @param string $family
131   * @param string $subtype
132   * @return string
133   */
134  static function get_font($family, $subtype = "normal") {
135   
136    $family = str_replace( array("'", '"'), "", mb_strtolower($family));
137    $subtype = mb_strtolower($subtype);
138   
139    if ( !isset(self::$_font_lookup[$family]) )
140      $family = DOMPDF_DEFAULT_FONT;
141
142    if ( !in_array($subtype, array("normal", "bold", "italic", "bold_italic")) )
143      //throw new DOMPDF_Exception("Font subtype '$subtype' is unsupported.");
144      return self::$_font_lookup[DOMPDF_DEFAULT_FONT]["normal"];
145   
146    if ( !isset(self::$_font_lookup[$family][$subtype]) )
147      return null;
148   
149    return self::$_font_lookup[$family][$subtype];
150  }
151
152  /**
153   * Saves the stored font family cache
154   *
155   * The name and location of the cache file are determined by {@link
156   * Font_Metrics::CACHE_FILE}.  This file should be writable by the
157   * webserver process.
158   *
159   * @see Font_Metrics::load_font_families()
160   */
161  static function save_font_families() {
162
163    file_put_contents(self::CACHE_FILE, var_export(self::$_font_lookup, true));
164   
165  }
166
167  /**
168   * Loads the stored font family cache
169   *
170   * @see save_font_families()
171   */
172  static function load_font_families() {
173    if ( !is_readable(self::CACHE_FILE) )
174      return;
175
176    $data = file_get_contents(self::CACHE_FILE);
177
178    if ( $data != "" )
179      eval ('self::$_font_lookup = ' . $data . ";");
180
181  }
182
183  /**
184   * Returns the current font lookup table
185   *
186   * @return array
187   */
188  static function get_font_families() {
189    return self::$_font_lookup;
190  }
191
192  static function set_font_family($fontname, $entry) {
193    self::$_font_lookup[mb_strtolower($fontname)] = $entry;
194  }
195}
196
197Font_Metrics::init();
198
199?>
Note: See TracBrowser for help on using the repository browser.