~chroot64bit/zivios/gentoo-experimental

« back to all changes in this revision

Viewing changes to application/library/Zend/Pdf/Font.php

  • Committer: Mustafa A. Hashmi
  • Date: 2008-12-04 13:32:21 UTC
  • Revision ID: mhashmi@zivios.org-20081204133221-0nd1trunwevijj38
Inclusion of new installation framework with ties to zend layout and dojo layout

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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_FileParserDataSource */
 
22
require_once 'Zend/Pdf/FileParserDataSource.php';
 
23
 
 
24
/** Zend_Pdf_FileParserDataSource_File */
 
25
require_once 'Zend/Pdf/FileParserDataSource/File.php';
 
26
 
 
27
/** Zend_Pdf_FileParserDataSource_String */
 
28
require_once 'Zend/Pdf/FileParserDataSource/String.php';
 
29
 
 
30
/** Zend_Pdf_FileParser_Font_OpenType_TrueType */
 
31
require_once 'Zend/Pdf/FileParser/Font/OpenType/TrueType.php';
 
32
 
 
33
/** Zend_Pdf_Resource_Font_Simple_Parsed_TrueType */
 
34
require_once 'Zend/Pdf/Resource/Font/Simple/Parsed/TrueType.php';
 
35
 
 
36
/** Zend_Pdf_Resource_Font_Type0 */
 
37
require_once 'Zend/Pdf/Resource/Font/Type0.php';
 
38
 
 
39
/** Zend_Pdf_Resource_Font_CidFont_TrueType */
 
40
require_once 'Zend/Pdf/Resource/Font/CidFont/TrueType.php';
 
41
 
 
42
/** Zend_Pdf_Resource_Font_Simple_Standard_Courier */
 
43
require_once 'Zend/Pdf/Resource/Font/Simple/Standard/Courier.php';
 
44
 
 
45
/** Zend_Pdf_Resource_Font_Simple_Standard_CourierBold */
 
46
require_once 'Zend/Pdf/Resource/Font/Simple/Standard/CourierBold.php';
 
47
 
 
48
/** Zend_Pdf_Resource_Font_Simple_Standard_CourierBoldOblique */
 
49
require_once 'Zend/Pdf/Resource/Font/Simple/Standard/CourierBoldOblique.php';
 
50
 
 
51
/** Zend_Pdf_Resource_Font_Simple_Standard_CourierOblique */
 
52
require_once 'Zend/Pdf/Resource/Font/Simple/Standard/CourierOblique.php';
 
53
 
 
54
/** Zend_Pdf_Resource_Font_Simple_Standard_Helvetica */
 
55
require_once 'Zend/Pdf/Resource/Font/Simple/Standard/Helvetica.php';
 
56
 
 
57
/** Zend_Pdf_Resource_Font_Simple_Standard_HelveticaBold */
 
58
require_once 'Zend/Pdf/Resource/Font/Simple/Standard/HelveticaBold.php';
 
59
 
 
60
/** Zend_Pdf_Resource_Font_Simple_Standard_HelveticaBoldOblique */
 
61
require_once 'Zend/Pdf/Resource/Font/Simple/Standard/HelveticaBoldOblique.php';
 
62
 
 
63
/** Zend_Pdf_Resource_Font_Simple_Standard_HelveticaOblique */
 
64
require_once 'Zend/Pdf/Resource/Font/Simple/Standard/HelveticaOblique.php';
 
65
 
 
66
/** Zend_Pdf_Resource_Font_Simple_Standard_Symbol */
 
67
require_once 'Zend/Pdf/Resource/Font/Simple/Standard/Symbol.php';
 
68
 
 
69
/** Zend_Pdf_Resource_Font_Simple_Standard_TimesBold */
 
70
require_once 'Zend/Pdf/Resource/Font/Simple/Standard/TimesBold.php';
 
71
 
 
72
/** Zend_Pdf_Resource_Font_Simple_Standard_TimesBoldItalic */
 
73
require_once 'Zend/Pdf/Resource/Font/Simple/Standard/TimesBoldItalic.php';
 
74
 
 
75
/** Zend_Pdf_Resource_Font_Simple_Standard_TimesItalic */
 
76
require_once 'Zend/Pdf/Resource/Font/Simple/Standard/TimesItalic.php';
 
77
 
 
78
/** Zend_Pdf_Resource_Font_Simple_Standard_TimesRoman */
 
79
require_once 'Zend/Pdf/Resource/Font/Simple/Standard/TimesRoman.php';
 
80
 
 
81
/** Zend_Pdf_Resource_Font_Simple_Standard_ZapfDingbats */
 
82
require_once 'Zend/Pdf/Resource/Font/Simple/Standard/ZapfDingbats.php';
 
83
 
 
84
/** Zend_Pdf_Resource_Font_Extracted */
 
85
require_once 'Zend/Pdf/Resource/Font/Extracted.php';
 
86
 
 
87
 
 
88
/**
 
89
 * Abstract factory class which vends {@link Zend_Pdf_Resource_Font} objects.
 
90
 *
 
91
 * Font objects themselves are normally instantiated through the factory methods
 
92
 * {@link fontWithName()} or {@link fontWithPath()}.
 
93
 *
 
94
 * This class is also the home for font-related constants because the name of
 
95
 * the true base class ({@link Zend_Pdf_Resource_Font}) is not intuitive for the
 
96
 * end user.
 
97
 *
 
98
 * @package    Zend_Pdf
 
99
 * @subpackage Fonts
 
100
 * @copyright  Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
 
101
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 
102
 */
 
103
abstract class Zend_Pdf_Font
 
104
{
 
105
  /**** Class Constants ****/
 
106
 
 
107
 
 
108
  /* Font Types */
 
109
 
 
110
    /**
 
111
     * Unknown font type.
 
112
     */
 
113
    const TYPE_UNKNOWN = 0;
 
114
 
 
115
    /**
 
116
     * One of the standard 14 PDF fonts.
 
117
     */
 
118
    const TYPE_STANDARD = 1;
 
119
 
 
120
    /**
 
121
     * A PostScript Type 1 font.
 
122
     */
 
123
    const TYPE_TYPE_1 = 2;
 
124
 
 
125
    /**
 
126
     * A TrueType font or an OpenType font containing TrueType outlines.
 
127
     */
 
128
    const TYPE_TRUETYPE = 3;
 
129
 
 
130
    /**
 
131
     * Type 0 composite font.
 
132
     */
 
133
    const TYPE_TYPE_0 = 4;
 
134
    
 
135
    /**
 
136
     * CID font containing a PostScript Type 1 font.
 
137
     * These fonts are used only to construct Type 0 composite fonts and can't be used directly
 
138
     */
 
139
    const TYPE_CIDFONT_TYPE_0 = 5;
 
140
 
 
141
    /**
 
142
     * CID font containing a TrueType font or an OpenType font containing TrueType outlines.
 
143
     * These fonts are used only to construct Type 0 composite fonts and can't be used directly
 
144
     */
 
145
    const TYPE_CIDFONT_TYPE_2 = 6;
 
146
    
 
147
 
 
148
  /* Names of the Standard 14 PDF Fonts */
 
149
 
 
150
    /**
 
151
     * Name of the standard PDF font Courier.
 
152
     */
 
153
    const FONT_COURIER = 'Courier';
 
154
 
 
155
    /**
 
156
     * Name of the bold style of the standard PDF font Courier.
 
157
     */
 
158
    const FONT_COURIER_BOLD = 'Courier-Bold';
 
159
 
 
160
    /**
 
161
     * Name of the italic style of the standard PDF font Courier.
 
162
     */
 
163
    const FONT_COURIER_OBLIQUE = 'Courier-Oblique';
 
164
 
 
165
    /**
 
166
     * Convenience constant for a common misspelling of
 
167
     * {@link FONT_COURIER_OBLIQUE}.
 
168
     */
 
169
    const FONT_COURIER_ITALIC = 'Courier-Oblique';
 
170
 
 
171
    /**
 
172
     * Name of the bold and italic style of the standard PDF font Courier.
 
173
     */
 
174
    const FONT_COURIER_BOLD_OBLIQUE = 'Courier-BoldOblique';
 
175
 
 
176
    /**
 
177
     * Convenience constant for a common misspelling of
 
178
     * {@link FONT_COURIER_BOLD_OBLIQUE}.
 
179
     */
 
180
    const FONT_COURIER_BOLD_ITALIC = 'Courier-BoldOblique';
 
181
 
 
182
    /**
 
183
     * Name of the standard PDF font Helvetica.
 
184
     */
 
185
    const FONT_HELVETICA = 'Helvetica';
 
186
 
 
187
    /**
 
188
     * Name of the bold style of the standard PDF font Helvetica.
 
189
     */
 
190
    const FONT_HELVETICA_BOLD = 'Helvetica-Bold';
 
191
 
 
192
    /**
 
193
     * Name of the italic style of the standard PDF font Helvetica.
 
194
     */
 
195
    const FONT_HELVETICA_OBLIQUE = 'Helvetica-Oblique';
 
196
 
 
197
    /**
 
198
     * Convenience constant for a common misspelling of
 
199
     * {@link FONT_HELVETICA_OBLIQUE}.
 
200
     */
 
201
    const FONT_HELVETICA_ITALIC = 'Helvetica-Oblique';
 
202
 
 
203
    /**
 
204
     * Name of the bold and italic style of the standard PDF font Helvetica.
 
205
     */
 
206
    const FONT_HELVETICA_BOLD_OBLIQUE = 'Helvetica-BoldOblique';
 
207
 
 
208
    /**
 
209
     * Convenience constant for a common misspelling of
 
210
     * {@link FONT_HELVETICA_BOLD_OBLIQUE}.
 
211
     */
 
212
    const FONT_HELVETICA_BOLD_ITALIC = 'Helvetica-BoldOblique';
 
213
 
 
214
    /**
 
215
     * Name of the standard PDF font Symbol.
 
216
     */
 
217
    const FONT_SYMBOL = 'Symbol';
 
218
 
 
219
    /**
 
220
     * Name of the standard PDF font Times.
 
221
     */
 
222
    const FONT_TIMES_ROMAN = 'Times-Roman';
 
223
 
 
224
    /**
 
225
     * Convenience constant for a common misspelling of
 
226
     * {@link FONT_TIMES_ROMAN}.
 
227
     */
 
228
    const FONT_TIMES = 'Times-Roman';
 
229
 
 
230
    /**
 
231
     * Name of the bold style of the standard PDF font Times.
 
232
     */
 
233
    const FONT_TIMES_BOLD = 'Times-Bold';
 
234
 
 
235
    /**
 
236
     * Name of the italic style of the standard PDF font Times.
 
237
     */
 
238
    const FONT_TIMES_ITALIC = 'Times-Italic';
 
239
 
 
240
    /**
 
241
     * Name of the bold and italic style of the standard PDF font Times.
 
242
     */
 
243
    const FONT_TIMES_BOLD_ITALIC = 'Times-BoldItalic';
 
244
 
 
245
    /**
 
246
     * Name of the standard PDF font Zapf Dingbats.
 
247
     */
 
248
    const FONT_ZAPFDINGBATS = 'ZapfDingbats';
 
249
 
 
250
 
 
251
  /* Font Name String Types */
 
252
 
 
253
    /**
 
254
     * Full copyright notice for the font.
 
255
     */
 
256
    const NAME_COPYRIGHT =  0;
 
257
 
 
258
    /**
 
259
     * Font family name. Used to group similar styles of fonts together.
 
260
     */
 
261
    const NAME_FAMILY =  1;
 
262
 
 
263
    /**
 
264
     * Font style within the font family. Examples: Regular, Italic, Bold, etc.
 
265
     */
 
266
    const NAME_STYLE =  2;
 
267
 
 
268
    /**
 
269
     * Unique font identifier.
 
270
     */
 
271
    const NAME_ID =  3;
 
272
 
 
273
    /**
 
274
     * Full font name. Usually a combination of the {@link NAME_FAMILY} and
 
275
     * {@link NAME_STYLE} strings.
 
276
     */
 
277
    const NAME_FULL =  4;
 
278
 
 
279
    /**
 
280
     * Version number of the font.
 
281
     */
 
282
    const NAME_VERSION =  5;
 
283
 
 
284
    /**
 
285
     * PostScript name for the font. This is the name used to identify fonts
 
286
     * internally and within the PDF file.
 
287
     */
 
288
    const NAME_POSTSCRIPT =  6;
 
289
 
 
290
    /**
 
291
     * Font trademark notice. This is distinct from the {@link NAME_COPYRIGHT}.
 
292
     */
 
293
    const NAME_TRADEMARK =  7;
 
294
 
 
295
    /**
 
296
     * Name of the font manufacturer.
 
297
     */
 
298
    const NAME_MANUFACTURER =  8;
 
299
 
 
300
    /**
 
301
     * Name of the designer of the font.
 
302
     */
 
303
    const NAME_DESIGNER =  9;
 
304
 
 
305
    /**
 
306
     * Description of the font. May contain revision information, usage
 
307
     * recommendations, features, etc.
 
308
     */
 
309
    const NAME_DESCRIPTION = 10;
 
310
 
 
311
    /**
 
312
     * URL of the font vendor. Some fonts may contain a unique serial number
 
313
     * embedded in this URL, which is used for licensing.
 
314
     */
 
315
    const NAME_VENDOR_URL = 11;
 
316
 
 
317
    /**
 
318
     * URL of the font designer ({@link NAME_DESIGNER}).
 
319
     */
 
320
    const NAME_DESIGNER_URL = 12;
 
321
 
 
322
    /**
 
323
     * Plain language licensing terms for the font.
 
324
     */
 
325
    const NAME_LICENSE = 13;
 
326
 
 
327
    /**
 
328
     * URL of more detailed licensing information for the font.
 
329
     */
 
330
    const NAME_LICENSE_URL = 14;
 
331
 
 
332
    /**
 
333
     * Preferred font family. Used by some fonts to work around a Microsoft
 
334
     * Windows limitation where only four fonts styles can share the same
 
335
     * {@link NAME_FAMILY} value.
 
336
     */
 
337
    const NAME_PREFERRED_FAMILY = 16;
 
338
 
 
339
    /**
 
340
     * Preferred font style. A more descriptive string than {@link NAME_STYLE}.
 
341
     */
 
342
    const NAME_PREFERRED_STYLE = 17;
 
343
 
 
344
    /**
 
345
     * Suggested text to use as a representative sample of the font.
 
346
     */
 
347
    const NAME_SAMPLE_TEXT = 19;
 
348
 
 
349
    /**
 
350
     * PostScript CID findfont name.
 
351
     */
 
352
    const NAME_CID_NAME = 20;
 
353
 
 
354
 
 
355
  /* Font Weights */
 
356
 
 
357
    /**
 
358
     * Thin font weight.
 
359
     */
 
360
    const WEIGHT_THIN = 100;
 
361
 
 
362
    /**
 
363
     * Extra-light (Ultra-light) font weight.
 
364
     */
 
365
    const WEIGHT_EXTRA_LIGHT = 200;
 
366
 
 
367
    /**
 
368
     * Light font weight.
 
369
     */
 
370
    const WEIGHT_LIGHT = 300;
 
371
 
 
372
    /**
 
373
     * Normal (Regular) font weight.
 
374
     */
 
375
    const WEIGHT_NORMAL = 400;
 
376
 
 
377
    /**
 
378
     * Medium font weight.
 
379
     */
 
380
    const WEIGHT_MEDIUM = 500;
 
381
 
 
382
    /**
 
383
     * Semi-bold (Demi-bold) font weight.
 
384
     */
 
385
    const WEIGHT_SEMI_BOLD = 600;
 
386
 
 
387
    /**
 
388
     * Bold font weight.
 
389
     */
 
390
    const WEIGHT_BOLD = 700;
 
391
 
 
392
    /**
 
393
     * Extra-bold (Ultra-bold) font weight.
 
394
     */
 
395
    const WEIGHT_EXTRA_BOLD = 800;
 
396
 
 
397
    /**
 
398
     * Black (Heavy) font weight.
 
399
     */
 
400
    const WEIGHT_BLACK = 900;
 
401
 
 
402
 
 
403
  /* Font Widths */
 
404
 
 
405
    /**
 
406
     * Ultra-condensed font width. Typically 50% of normal.
 
407
     */
 
408
    const WIDTH_ULTRA_CONDENSED = 1;
 
409
 
 
410
    /**
 
411
     * Extra-condensed font width. Typically 62.5% of normal.
 
412
     */
 
413
    const WIDTH_EXTRA_CONDENSED = 2;
 
414
 
 
415
    /**
 
416
     * Condensed font width. Typically 75% of normal.
 
417
     */
 
418
    const WIDTH_CONDENSED = 3;
 
419
 
 
420
    /**
 
421
     * Semi-condensed font width. Typically 87.5% of normal.
 
422
     */
 
423
    const WIDTH_SEMI_CONDENSED = 4;
 
424
 
 
425
    /**
 
426
     * Normal (Medium) font width.
 
427
     */
 
428
    const WIDTH_NORMAL = 5;
 
429
 
 
430
    /**
 
431
     * Semi-expanded font width. Typically 112.5% of normal.
 
432
     */
 
433
    const WIDTH_SEMI_EXPANDED = 6;
 
434
 
 
435
    /**
 
436
     * Expanded font width. Typically 125% of normal.
 
437
     */
 
438
    const WIDTH_EXPANDED = 7;
 
439
 
 
440
    /**
 
441
     * Extra-expanded font width. Typically 150% of normal.
 
442
     */
 
443
    const WIDTH_EXTRA_EXPANDED = 8;
 
444
 
 
445
    /**
 
446
     * Ultra-expanded font width. Typically 200% of normal.
 
447
     */
 
448
    const WIDTH_ULTRA_EXPANDED = 9;
 
449
 
 
450
 
 
451
  /* Font Embedding Options */
 
452
 
 
453
    /**
 
454
     * Do not embed the font in the PDF document.
 
455
     */
 
456
    const EMBED_DONT_EMBED = 0x01;
 
457
 
 
458
    /**
 
459
     * Embed, but do not subset the font in the PDF document.
 
460
     */
 
461
    const EMBED_DONT_SUBSET = 0x02;
 
462
 
 
463
    /**
 
464
     * Embed, but do not compress the font in the PDF document.
 
465
     */
 
466
    const EMBED_DONT_COMPRESS = 0x04;
 
467
 
 
468
    /**
 
469
     * Suppress the exception normally thrown if the font cannot be embedded
 
470
     * due to its copyright bits being set.
 
471
     */
 
472
    const EMBED_SUPPRESS_EMBED_EXCEPTION = 0x08;
 
473
 
 
474
 
 
475
 
 
476
  /**** Class Variables ****/
 
477
 
 
478
 
 
479
    /**
 
480
     * Array whose keys are the unique PostScript names of instantiated fonts.
 
481
     * The values are the font objects themselves.
 
482
     * @var array
 
483
     */
 
484
    private static $_fontNames = array();
 
485
 
 
486
    /**
 
487
     * Array whose keys are the md5 hash of the full paths on disk for parsed
 
488
     * fonts. The values are the font objects themselves.
 
489
     * @var array
 
490
     */
 
491
    private static $_fontFilePaths = array();
 
492
 
 
493
 
 
494
 
 
495
  /**** Public Interface ****/
 
496
 
 
497
 
 
498
  /* Factory Methods */
 
499
 
 
500
    /**
 
501
     * Returns a {@link Zend_Pdf_Resource_Font} object by full name.
 
502
     *
 
503
     * This is the preferred method to obtain one of the standard 14 PDF fonts.
 
504
     *
 
505
     * The result of this method is cached, preventing unnecessary duplication
 
506
     * of font objects. Repetitive calls for a font with the same name will
 
507
     * return the same object.
 
508
     *
 
509
     * The $embeddingOptions parameter allows you to set certain flags related
 
510
     * to font embedding. You may combine options by OR-ing them together. See
 
511
     * the EMBED_ constants defined in {@link Zend_Pdf_Font} for the list of
 
512
     * available options and their descriptions. Note that this value is only
 
513
     * used when creating a font for the first time. If a font with the same
 
514
     * name already exists, you will get that object and the options you specify
 
515
     * here will be ignored. This is because fonts are only embedded within the
 
516
     * PDF file once.
 
517
     *
 
518
     * If the font name supplied does not match the name of a previously
 
519
     * instantiated object and it is not one of the 14 standard PDF fonts, an
 
520
     * exception will be thrown.
 
521
     *
 
522
     * @param string $name Full PostScript name of font.
 
523
     * @param integer $embeddingOptions (optional) Options for font embedding.
 
524
     * @return Zend_Pdf_Resource_Font
 
525
     * @throws Zend_Pdf_Exception
 
526
     */
 
527
    public static function fontWithName($name, $embeddingOptions = 0)
 
528
        {
 
529
        /* First check the cache. Don't duplicate font objects.
 
530
         */
 
531
        if (isset(Zend_Pdf_Font::$_fontNames[$name])) {
 
532
            return Zend_Pdf_Font::$_fontNames[$name];
 
533
        }
 
534
 
 
535
        /**
 
536
         * @todo It would be cool to be able to have a mapping of font names to
 
537
         *   file paths in a configuration file for frequently used custom
 
538
         *   fonts. This would allow a user to use custom fonts without having
 
539
         *   to hard-code file paths all over the place. Table this idea until
 
540
         *   {@link Zend_Config} is ready.
 
541
         */
 
542
 
 
543
        /* Not an existing font and no mapping in the config file. Check to see
 
544
         * if this is one of the standard 14 PDF fonts.
 
545
         */
 
546
        switch ($name) {
 
547
            case Zend_Pdf_Font::FONT_COURIER:
 
548
                $font = new Zend_Pdf_Resource_Font_Simple_Standard_Courier();
 
549
                break;
 
550
 
 
551
            case Zend_Pdf_Font::FONT_COURIER_BOLD:
 
552
                $font = new Zend_Pdf_Resource_Font_Simple_Standard_CourierBold();
 
553
                break;
 
554
 
 
555
            case Zend_Pdf_Font::FONT_COURIER_OBLIQUE:
 
556
                $font = new Zend_Pdf_Resource_Font_Simple_Standard_CourierOblique();
 
557
                break;
 
558
 
 
559
            case Zend_Pdf_Font::FONT_COURIER_BOLD_OBLIQUE:
 
560
                $font = new Zend_Pdf_Resource_Font_Simple_Standard_CourierBoldOblique();
 
561
                break;
 
562
 
 
563
            case Zend_Pdf_Font::FONT_HELVETICA:
 
564
                $font = new Zend_Pdf_Resource_Font_Simple_Standard_Helvetica();
 
565
                break;
 
566
 
 
567
            case Zend_Pdf_Font::FONT_HELVETICA_BOLD:
 
568
                $font = new Zend_Pdf_Resource_Font_Simple_Standard_HelveticaBold();
 
569
                break;
 
570
 
 
571
            case Zend_Pdf_Font::FONT_HELVETICA_OBLIQUE:
 
572
                $font = new Zend_Pdf_Resource_Font_Simple_Standard_HelveticaOblique();
 
573
                break;
 
574
 
 
575
            case Zend_Pdf_Font::FONT_HELVETICA_BOLD_OBLIQUE:
 
576
                $font = new Zend_Pdf_Resource_Font_Simple_Standard_HelveticaBoldOblique();
 
577
                break;
 
578
 
 
579
            case Zend_Pdf_Font::FONT_SYMBOL:
 
580
                $font = new Zend_Pdf_Resource_Font_Simple_Standard_Symbol();
 
581
                break;
 
582
 
 
583
            case Zend_Pdf_Font::FONT_TIMES_ROMAN:
 
584
                $font = new Zend_Pdf_Resource_Font_Simple_Standard_TimesRoman();
 
585
                break;
 
586
 
 
587
            case Zend_Pdf_Font::FONT_TIMES_BOLD:
 
588
                $font = new Zend_Pdf_Resource_Font_Simple_Standard_TimesBold();
 
589
                break;
 
590
 
 
591
            case Zend_Pdf_Font::FONT_TIMES_ITALIC:
 
592
                $font = new Zend_Pdf_Resource_Font_Simple_Standard_TimesItalic();
 
593
                break;
 
594
 
 
595
            case Zend_Pdf_Font::FONT_TIMES_BOLD_ITALIC:
 
596
                $font = new Zend_Pdf_Resource_Font_Simple_Standard_TimesBoldItalic();
 
597
                break;
 
598
 
 
599
            case Zend_Pdf_Font::FONT_ZAPFDINGBATS:
 
600
                $font = new Zend_Pdf_Resource_Font_Simple_Standard_ZapfDingbats();
 
601
                break;
 
602
 
 
603
            default:
 
604
                throw new Zend_Pdf_Exception("Unknown font name: $name",
 
605
                                             Zend_Pdf_Exception::BAD_FONT_NAME);
 
606
        }
 
607
 
 
608
        /* Add this new font to the cache array and return it for use.
 
609
         */
 
610
        Zend_Pdf_Font::$_fontNames[$name] = $font;
 
611
        return $font;
 
612
    }
 
613
 
 
614
    /**
 
615
     * Returns a {@link Zend_Pdf_Resource_Font} object by file path.
 
616
     *
 
617
     * The result of this method is cached, preventing unnecessary duplication
 
618
     * of font objects. Repetitive calls for the font with the same path will
 
619
     * return the same object.
 
620
     *
 
621
     * The $embeddingOptions parameter allows you to set certain flags related
 
622
     * to font embedding. You may combine options by OR-ing them together. See
 
623
     * the EMBED_ constants defined in {@link Zend_Pdf_Font} for the list of
 
624
     * available options and their descriptions. Note that this value is only
 
625
     * used when creating a font for the first time. If a font with the same
 
626
     * name already exists, you will get that object and the options you specify
 
627
     * here will be ignored. This is because fonts are only embedded within the
 
628
     * PDF file once.
 
629
     *
 
630
     * If the file path supplied does not match the path of a previously
 
631
     * instantiated object or the font type cannot be determined, an exception
 
632
     * will be thrown.
 
633
     *
 
634
     * @param string $filePath Full path to the font file.
 
635
     * @param integer $embeddingOptions (optional) Options for font embedding.
 
636
     * @return Zend_Pdf_Resource_Font
 
637
     * @throws Zend_Pdf_Exception
 
638
     */
 
639
    public static function fontWithPath($filePath, $embeddingOptions = 0)
 
640
    {
 
641
        /* First check the cache. Don't duplicate font objects.
 
642
         */
 
643
        $filePathKey = md5($filePath);
 
644
        if (isset(Zend_Pdf_Font::$_fontFilePaths[$filePathKey])) {
 
645
            return Zend_Pdf_Font::$_fontFilePaths[$filePathKey];
 
646
        }
 
647
 
 
648
        /* Create a file parser data source object for this file. File path and
 
649
         * access permission checks are handled here.
 
650
         */
 
651
        $dataSource = new Zend_Pdf_FileParserDataSource_File($filePath);
 
652
 
 
653
        /* Attempt to determine the type of font. We can't always trust file
 
654
         * extensions, but try that first since it's fastest.
 
655
         */
 
656
        $fileExtension = strtolower(pathinfo($filePath, PATHINFO_EXTENSION));
 
657
 
 
658
        /* If it turns out that the file is named improperly and we guess the
 
659
         * wrong type, we'll get null instead of a font object.
 
660
         */
 
661
        switch ($fileExtension) {
 
662
            case 'ttf':
 
663
                $font = Zend_Pdf_Font::_extractTrueTypeFont($dataSource, $embeddingOptions);
 
664
                break;
 
665
 
 
666
            default:
 
667
                /* Unrecognized extension. Try to determine the type by actually
 
668
                 * parsing it below.
 
669
                 */
 
670
                $font = null;
 
671
                break;
 
672
        }
 
673
 
 
674
 
 
675
        if (is_null($font)) {
 
676
            /* There was no match for the file extension or the extension was
 
677
             * wrong. Attempt to detect the type of font by actually parsing it.
 
678
             * We'll do the checks in order of most likely format to try to
 
679
             * reduce the detection time.
 
680
             */
 
681
 
 
682
            // OpenType
 
683
 
 
684
            // TrueType
 
685
            if ((is_null($font)) && ($fileExtension != 'ttf')) {
 
686
                $font = Zend_Pdf_Font::_extractTrueTypeFont($dataSource, $embeddingOptions);
 
687
            }
 
688
 
 
689
            // Type 1 PostScript
 
690
 
 
691
            // Mac OS X dfont
 
692
 
 
693
            // others?
 
694
        }
 
695
 
 
696
 
 
697
        /* Done with the data source object.
 
698
         */
 
699
        $dataSource = null;
 
700
 
 
701
        if (! is_null($font)) {
 
702
            /* Parsing was successful. Add this font instance to the cache arrays
 
703
             * and return it for use.
 
704
             */
 
705
            $fontName = $font->getFontName(Zend_Pdf_Font::NAME_POSTSCRIPT, '', '');
 
706
            Zend_Pdf_Font::$_fontNames[$fontName] = $font;
 
707
            $filePathKey = md5($filePath);
 
708
            Zend_Pdf_Font::$_fontFilePaths[$filePathKey] = $font;
 
709
            return $font;
 
710
 
 
711
        } else {
 
712
            /* The type of font could not be determined. Give up.
 
713
             */
 
714
            throw new Zend_Pdf_Exception("Cannot determine font type: $filePath",
 
715
                                         Zend_Pdf_Exception::CANT_DETERMINE_FONT_TYPE);
 
716
         }
 
717
 
 
718
    }
 
719
 
 
720
 
 
721
 
 
722
  /**** Internal Methods ****/
 
723
 
 
724
 
 
725
  /* Font Extraction Methods */
 
726
 
 
727
    /**
 
728
     * Attempts to extract a TrueType font from the data source.
 
729
     *
 
730
     * If the font parser throws an exception that suggests the data source
 
731
     * simply doesn't contain a TrueType font, catches it and returns null. If
 
732
     * an exception is thrown that suggests the TrueType font is corrupt or
 
733
     * otherwise unusable, throws that exception. If successful, returns the
 
734
     * font object.
 
735
     *
 
736
     * @param Zend_Pdf_FileParserDataSource $dataSource
 
737
     * @param integer $embeddingOptions Options for font embedding.
 
738
     * @return Zend_Pdf_Resource_Font_OpenType_TrueType May also return null if
 
739
     *   the data source does not appear to contain a TrueType font.
 
740
     * @throws Zend_Pdf_Exception
 
741
     */
 
742
    protected static function _extractTrueTypeFont($dataSource, $embeddingOptions)
 
743
    {
 
744
        try {
 
745
            $fontParser = new Zend_Pdf_FileParser_Font_OpenType_TrueType($dataSource);
 
746
            
 
747
            $fontParser->parse();
 
748
            if ($fontParser->isAdobeLatinSubset) {
 
749
                $font = new Zend_Pdf_Resource_Font_Simple_Parsed_TrueType($fontParser, $embeddingOptions);
 
750
            } else {
 
751
                /* Use Composite Type 0 font which supports Unicode character mapping */
 
752
                $cidFont = new Zend_Pdf_Resource_Font_CidFont_TrueType($fontParser, $embeddingOptions);
 
753
                $font    = new Zend_Pdf_Resource_Font_Type0($cidFont);
 
754
            }
 
755
        } catch (Zend_Pdf_Exception $exception) {
 
756
            /* The following exception codes suggest that this isn't really a
 
757
             * TrueType font. If we caught such an exception, simply return
 
758
             * null. For all other cases, it probably is a TrueType font but has
 
759
             * a problem; throw the exception again.
 
760
             */
 
761
            $fontParser = null;
 
762
            switch ($exception->getCode()) {
 
763
                case Zend_Pdf_Exception::WRONG_FONT_TYPE:    // break intentionally omitted
 
764
                case Zend_Pdf_Exception::BAD_TABLE_COUNT:    // break intentionally omitted
 
765
                case Zend_Pdf_Exception::BAD_MAGIC_NUMBER:
 
766
                    return null;
 
767
 
 
768
                default:
 
769
                    throw $exception;
 
770
            }
 
771
        }
 
772
        return $font;
 
773
    }
 
774
 
 
775
}