PageRenderTime 124ms CodeModel.GetById 80ms app.highlight 6ms RepoModel.GetById 34ms app.codeStats 1ms

/library/Zend/Pdf/Resource/Font/Simple.php

https://bitbucket.org/baruffaldi/website-2008-computer-shopping-3
PHP | 275 lines | 69 code | 23 blank | 183 comment | 9 complexity | f406f8fec3a59fd798e43b03bc49a8c3 MD5 | raw file
  1<?php
  2/**
  3 * Zend Framework
  4 *
  5 * LICENSE
  6 *
  7 * This source file is subject to the new BSD license that is bundled
  8 * with this package in the file LICENSE.txt.
  9 * It is also available through the world-wide-web at this URL:
 10 * http://framework.zend.com/license/new-bsd
 11 * If you did not receive a copy of the license and are unable to
 12 * obtain it through the world-wide-web, please send an email
 13 * to license@zend.com so we can send you a copy immediately.
 14 *
 15 * @package    Zend_Pdf
 16 * @subpackage Fonts
 17 * @copyright  Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
 18 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 19 */
 20
 21/** Zend_Pdf_Resource_Font */
 22require_once 'Zend/Pdf/Resource/Font.php';
 23
 24/** Zend_Pdf_Cmap */
 25require_once 'Zend/Pdf/Cmap.php';
 26
 27
 28
 29/**
 30 * Adobe PDF Simple fonts implementation
 31 * 
 32 * PDF simple fonts functionality is presented by Adobe Type 1 
 33 * (including standard PDF Type1 built-in fonts) and TrueType fonts support.
 34 *
 35 * Both fonts have the following properties:
 36 * - Glyphs in the font are selected by single-byte character codes obtained from a
 37 *   string that is shown by the text-showing operators. Logically, these codes index
 38 *   into a table of 256 glyphs; the mapping from codes to glyphs is called the font’s
 39 *   encoding.
 40 *   PDF specification provides a possibility to specify any user defined encoding in addition
 41 *   to the standard built-in encodings: Standard-Encoding, MacRomanEncoding, WinAnsiEncoding, 
 42 *   and PDFDocEncoding, but Zend_Pdf simple fonts implementation operates only with 
 43 *   Windows ANSI encoding (except Symbol and ZapfDingbats built-in fonts).
 44 *
 45 * - Each glyph has a single set of metrics, including a horizontal displacement or
 46 *   width. That is, simple fonts support only horizontal writing mode.
 47 *
 48 * 
 49 * The code in this class is common to both types. However, you will only deal
 50 * directly with subclasses.
 51 * 
 52 * Font objects should be normally be obtained from the factory methods
 53 * {@link Zend_Pdf_Font::fontWithName} and {@link Zend_Pdf_Font::fontWithPath}.
 54 *
 55 * @package    Zend_Pdf
 56 * @subpackage Fonts
 57 * @copyright  Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
 58 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 59 */
 60abstract class Zend_Pdf_Resource_Font_Simple extends Zend_Pdf_Resource_Font
 61{
 62    /**
 63     * Object representing the font's cmap (character to glyph map).
 64     * @var Zend_Pdf_Cmap
 65     */
 66    protected $_cmap = null;
 67
 68	/**
 69     * Array containing the widths of each of the glyphs contained in the font.
 70     *
 71     * Keys are integers starting from 0, which coresponds to Zend_Pdf_Cmap::MISSING_CHARACTER_GLYPH.
 72     *
 73     * Font character map may contain gaps for actually used glyphs, nevertheless glyphWidths array
 74     * contains widths for all glyphs even they are unused.
 75     *
 76     * @var array
 77     */
 78    protected $_glyphWidths = null;
 79
 80    /**
 81     * Width for glyphs missed in the font
 82     * 
 83     * Note: Adobe PDF specfication (V1.4 - V1.6) doesn't define behavior for rendering
 84     * characters missed in the standard PDF fonts (such us 0x7F (DEL) Windows ANSI code)
 85     * Adobe Font Metrics files doesn't also define metrics for "missed glyph".
 86     * We provide character width as "0" for this case, but actually it depends on PDF viewer
 87     * implementation. 
 88     *
 89     * @var integer
 90     */
 91    protected $_missingGlyphWidth = 0;
 92
 93
 94	/**** Public Interface ****/
 95
 96
 97  /* Object Lifecycle */
 98
 99    /**
100     * Object constructor
101     *
102     */
103    public function __construct()
104    {
105        parent::__construct();
106
107        /**
108         * @todo
109         * It's easy to add other encodings support now (Standard-Encoding, MacRomanEncoding, 
110         * PDFDocEncoding, MacExpertEncoding, Symbol, and ZapfDingbats).
111         * Steps for the implementation:
112         * - completely describe all PDF single byte encodings in the documentation
113         * - implement non-WinAnsi encodings processing into encodeString()/decodeString() methods
114         * 
115         * These encodings will be automatically supported for standard builtin PDF fonts as well 
116         * as for external fonts.
117         */
118        $this->_resource->Encoding = new Zend_Pdf_Element_Name('WinAnsiEncoding');
119    }
120
121    /**
122     * Returns an array of glyph numbers corresponding to the Unicode characters.
123     *
124     * If a particular character doesn't exist in this font, the special 'missing
125     * character glyph' will be substituted.
126     *
127     * See also {@link glyphNumberForCharacter()}.
128     *
129     * @param array $characterCodes Array of Unicode character codes (code points).
130     * @return array Array of glyph numbers.
131     */
132    public function glyphNumbersForCharacters($characterCodes)
133    {
134    	return $this->_cmap->glyphNumbersForCharacters($characterCodes);
135    }
136
137    /**
138     * Returns the glyph number corresponding to the Unicode character.
139     *
140     * If a particular character doesn't exist in this font, the special 'missing
141     * character glyph' will be substituted.
142     *
143     * See also {@link glyphNumbersForCharacters()} which is optimized for bulk
144     * operations.
145     *
146     * @param integer $characterCode Unicode character code (code point).
147     * @return integer Glyph number.
148     */
149    public function glyphNumberForCharacter($characterCode)
150    {
151        return $this->_cmap->glyphNumberForCharacter($characterCode);
152    }
153
154    /**
155     * Returns a number between 0 and 1 inclusive that indicates the percentage
156     * of characters in the string which are covered by glyphs in this font.
157     *
158     * Since no one font will contain glyphs for the entire Unicode character
159     * range, this method can be used to help locate a suitable font when the
160     * actual contents of the string are not known.
161     *
162     * Note that some fonts lie about the characters they support. Additionally,
163     * fonts don't usually contain glyphs for control characters such as tabs
164     * and line breaks, so it is rare that you will get back a full 1.0 score.
165     * The resulting value should be considered informational only.
166     *
167     * @param string $string
168     * @param string $charEncoding (optional) Character encoding of source text.
169     *   If omitted, uses 'current locale'.
170     * @return float
171     */
172    public function getCoveredPercentage($string, $charEncoding = '')
173    {
174        /* Convert the string to UTF-16BE encoding so we can match the string's
175         * character codes to those found in the cmap.
176         */
177        if ($charEncoding != 'UTF-16BE') {
178            $string = iconv($charEncoding, 'UTF-16BE', $string);
179        }
180
181        $charCount = iconv_strlen($string, 'UTF-16BE');
182        if ($charCount == 0) {
183            return 0;
184        }
185
186        /* Fetch the covered character code list from the font's cmap.
187         */
188        $coveredCharacters = $this->_cmap->getCoveredCharacters();
189
190        /* Calculate the score by doing a lookup for each character.
191         */
192        $score = 0;
193        $maxIndex = strlen($string);
194        for ($i = 0; $i < $maxIndex; $i++) {
195            /**
196             * @todo Properly handle characters encoded as surrogate pairs.
197             */
198            $charCode = (ord($string[$i]) << 8) | ord($string[++$i]);
199            /* This could probably be optimized a bit with a binary search...
200             */
201            if (in_array($charCode, $coveredCharacters)) {
202                $score++;
203            }
204        }
205        return $score / $charCount;
206    }
207
208    /**
209     * Returns the widths of the glyphs.
210     *
211     * The widths are expressed in the font's glyph space. You are responsible
212     * for converting to user space as necessary. See {@link unitsPerEm()}.
213     *
214     * See also {@link widthForGlyph()}.
215     *
216     * @param array &$glyphNumbers Array of glyph numbers.
217     * @return array Array of glyph widths (integers).
218     */
219    public function widthsForGlyphs($glyphNumbers)
220    {
221        $widths = array();
222        foreach ($glyphNumbers as $key => $glyphNumber) {
223            if (!isset($this->_glyphWidths[$glyphNumber])) {
224                $widths[$key] = $this->_missingGlyphWidth;
225            } else {
226                $widths[$key] = $this->_glyphWidths[$glyphNumber];
227            }
228        }
229        return $widths;
230    }
231
232    /**
233     * Returns the width of the glyph.
234     *
235     * Like {@link widthsForGlyphs()} but used for one glyph at a time.
236     *
237     * @param integer $glyphNumber
238     * @return integer
239     */
240    public function widthForGlyph($glyphNumber)
241    {
242        if (!isset($this->_glyphWidths[$glyphNumber])) {
243            return $this->_missingGlyphWidth;
244        }
245        return $this->_glyphWidths[$glyphNumber];
246    }
247
248    /**
249     * Convert string to the font encoding.
250     * 
251     * The method is used to prepare string for text drawing operators 
252     *
253     * @param string $string
254     * @param string $charEncoding Character encoding of source text.
255     * @return string
256     */
257    public function encodeString($string, $charEncoding)
258    {
259        return iconv($charEncoding, 'CP1252//IGNORE', $string);
260    }
261
262    /**
263     * Convert string from the font encoding.
264     *
265     * The method is used to convert strings retrieved from existing content streams
266     *
267     * @param string $string
268     * @param string $charEncoding Character encoding of resulting text.
269     * @return string
270     */
271    public function decodeString($string, $charEncoding)
272    {
273        return iconv('CP1252', $charEncoding, $string);
274    }
275}