forked from zendframework/zendframework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Page.php
1764 lines (1515 loc) · 58.4 KB
/
Page.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* @category Zend
* @package Zend_PDF
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id$
*/
/**
* @namespace
*/
namespace Zend\Pdf;
/**
* PDF Page
*
* @uses \Zend\Pdf\PdfDocument
* @uses \Zend\Pdf\ObjectFactory\ElementFactory
* @uses \Zend\Pdf\InternalType
* @uses \Zend\Pdf\Exception
* @uses \Zend\Pdf\Resource\Font
* @package Zend_PDF
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Page
{
/**** Class Constants ****/
/* Page Sizes */
/**
* Size representing an A4 page in portrait (tall) orientation.
*/
const SIZE_A4 = '595:842:';
/**
* Size representing an A4 page in landscape (wide) orientation.
*/
const SIZE_A4_LANDSCAPE = '842:595:';
/**
* Size representing a US Letter page in portrait (tall) orientation.
*/
const SIZE_LETTER = '612:792:';
/**
* Size representing a US Letter page in landscape (wide) orientation.
*/
const SIZE_LETTER_LANDSCAPE = '792:612:';
/* Shape Drawing */
/**
* Stroke the path only. Do not fill.
*/
const SHAPE_DRAW_STROKE = 0;
/**
* Fill the path only. Do not stroke.
*/
const SHAPE_DRAW_FILL = 1;
/**
* Fill and stroke the path.
*/
const SHAPE_DRAW_FILL_AND_STROKE = 2;
/* Shape Filling Methods */
/**
* Fill the path using the non-zero winding rule.
*/
const FILL_METHOD_NON_ZERO_WINDING = 0;
/**
* Fill the path using the even-odd rule.
*/
const FILL_METHOD_EVEN_ODD = 1;
/* Line Dash Types */
/**
* Solid line dash.
*/
const LINE_DASHING_SOLID = 0;
/**
* Page dictionary (refers to an inderect \Zend\Pdf\InternalType\DictionaryObject object).
*
* @var \Zend\Pdf\InternalType\DictionaryObject
* | \Zend\Pdf\InternalType\IndirectObject
* | \Zend\Pdf\InternalType\IndirectObjectReference
*/
protected $_pageDictionary;
/**
* PDF objects factory.
*
* @var \Zend\Pdf\ObjectFactory
*/
protected $_objFactory = null;
/**
* Flag which signals, that page is created separately from any PDF document or
* attached to anyone.
*
* @var boolean
*/
protected $_attached;
/**
* Stream of the drawing instructions.
*
* @var string
*/
protected $_contents = '';
/**
* Current style
*
* @var \Zend\Pdf\Style
*/
protected $_style = null;
/**
* Counter for the "Save" operations
*
* @var integer
*/
protected $_saveCount = 0;
/**
* Safe Graphics State semafore
*
* If it's false, than we can't be sure Graphics State is restored withing
* context of previous contents stream (ex. drawing coordinate system may be rotated).
* We should encompass existing content with save/restore GS operators
*
* @var boolean
*/
protected $_safeGS;
/**
* Current font
*
* @var \Zend\Pdf\Resource\Font\AbstractFont
*/
protected $_font = null;
/**
* Current font size
*
* @var float
*/
protected $_fontSize;
/**
* Object constructor.
* Constructor signatures:
*
* 1. Load PDF page from a parsed PDF file.
* Object factory is created by PDF parser.
* ---------------------------------------------------------
* new Zend_PDF_Page(\Zend\Pdf\InternalType\DictionaryObject $pageDict,
* \Zend\Pdf\InternalType\ObjectFactory $factory);
* ---------------------------------------------------------
*
* 2. Clone PDF page.
* New page is created in the same context as source page. Object factory is shared.
* Thus it will be attached to the document, but need to be placed into Zend_Pdf::$pages array
* to be included into output.
* ---------------------------------------------------------
* new Zend_PDF_Page(Zend_PDF_Page $page);
* ---------------------------------------------------------
*
* 3. Create new page with a specified pagesize.
* If $factory is null then it will be created and page must be attached to the document to be
* included into output.
* ---------------------------------------------------------
* new Zend_PDF_Page(string $pagesize, \Zend\Pdf\ObjectFactory $factory = null);
* ---------------------------------------------------------
*
* 4. Create new page with a specified pagesize (in default user space units).
* If $factory is null then it will be created and page must be attached to the document to be
* included into output.
* ---------------------------------------------------------
* new Zend_PDF_Page(numeric $width, numeric $height,
* \Zend\Pdf\ObjectFactory $factory = null);
* ---------------------------------------------------------
*
*
* @param mixed $param1
* @param mixed $param2
* @param mixed $param3
* @throws \Zend\Pdf\Exception
*/
public function __construct($param1, $param2 = null, $param3 = null)
{
if ($param1 instanceof InternalType\IndirectObjectReference &&
$param1->getType() == InternalType\AbstractTypeObject::TYPE_DICTIONARY &&
$param2 instanceof ObjectFactory &&
$param3 === null
) {
$this->_pageDictionary = $param1;
$this->_objFactory = $param2;
$this->_attached = true;
$this->_safeGS = false;
return;
} else if ($param1 instanceof Page && $param2 === null && $param3 === null) {
// Clone existing page.
// Let already existing content and resources to be shared between pages
// We don't give existing content modification functionality, so we don't need "deep copy"
$this->_objFactory = $param1->_objFactory;
$this->_attached = &$param1->_attached;
$this->_safeGS = false;
$this->_pageDictionary = $this->_objFactory->newObject(new InternalType\DictionaryObject());
foreach ($param1->_pageDictionary->getKeys() as $key) {
if ($key == 'Contents') {
// Clone Contents property
$this->_pageDictionary->Contents = new InternalType\ArrayObject();
if ($param1->_pageDictionary->Contents->getType() != InternalType\AbstractTypeObject::TYPE_ARRAY) {
// Prepare array of content streams and add existing stream
$this->_pageDictionary->Contents->items[] = $param1->_pageDictionary->Contents;
} else {
// Clone array of the content streams
foreach ($param1->_pageDictionary->Contents->items as $srcContentStream) {
$this->_pageDictionary->Contents->items[] = $srcContentStream;
}
}
} else {
$this->_pageDictionary->$key = $param1->_pageDictionary->$key;
}
}
return;
} else if (is_string($param1) &&
($param2 === null || $param2 instanceof ObjectFactory) &&
$param3 === null) {
if ($param2 !== null) {
$this->_objFactory = $param2;
} else {
$this->_objFactory = ObjectFactory\ElementFactory::createFactory(1);
}
$this->_attached = false;
$this->_safeGS = true; /** New page created. That's users App responsibility to track GS changes */
switch (strtolower($param1)) {
case 'a4':
$param1 = self::SIZE_A4;
break;
case 'a4-landscape':
$param1 = self::SIZE_A4_LANDSCAPE;
break;
case 'letter':
$param1 = self::SIZE_LETTER;
break;
case 'letter-landscape':
$param1 = self::SIZE_LETTER_LANDSCAPE;
break;
default:
// should be in "x:y" or "x:y:" form
}
$pageDim = explode(':', $param1);
if(count($pageDim) == 2 || count($pageDim) == 3) {
$pageWidth = $pageDim[0];
$pageHeight = $pageDim[1];
} else {
/**
* @todo support of user defined pagesize notations, like:
* "210x297mm", "595x842", "8.5x11in", "612x792"
*/
throw new Exception('Wrong pagesize notation.');
}
/**
* @todo support of pagesize recalculation to "default user space units"
*/
} else if (is_numeric($param1) && is_numeric($param2) &&
($param3 === null || $param3 instanceof ObjectFactory)) {
if ($param3 !== null) {
$this->_objFactory = $param3;
} else {
$this->_objFactory = ObjectFactory\ElementFactory::createFactory(1);
}
$this->_attached = false;
$this->_safeGS = true; /** New page created. That's users App responsibility to track GS changes */
$pageWidth = $param1;
$pageHeight = $param2;
} else {
throw new Exception('Unrecognized method signature, wrong number of arguments or wrong argument types.');
}
$this->_pageDictionary = $this->_objFactory->newObject(new InternalType\DictionaryObject());
$this->_pageDictionary->Type = new InternalType\NameObject('Page');
$this->_pageDictionary->LastModified = new InternalType\StringObject(PdfDocument::pdfDate());
$this->_pageDictionary->Resources = new InternalType\DictionaryObject();
$this->_pageDictionary->MediaBox = new InternalType\ArrayObject();
$this->_pageDictionary->MediaBox->items[] = new InternalType\NumericObject(0);
$this->_pageDictionary->MediaBox->items[] = new InternalType\NumericObject(0);
$this->_pageDictionary->MediaBox->items[] = new InternalType\NumericObject($pageWidth);
$this->_pageDictionary->MediaBox->items[] = new InternalType\NumericObject($pageHeight);
$this->_pageDictionary->Contents = new InternalType\ArrayObject();
}
/**
* Clone operator
*
* @throws \Zend\Pdf\Exception
*/
public function __clone()
{
throw new Exception('Cloning \Zend\Pdf\Page object using \'clone\' keyword is not supported. Use \'new Zend_PDF_Page($srcPage)\' syntax');
}
/**
* Attach resource to the page
*
* @param string $type
* @param \Zend\Pdf\Resource\AbstractResource $resource
* @return string
*/
protected function _attachResource($type, Resource\AbstractResource $resource)
{
// Check that Resources dictionary contains appropriate resource set
if ($this->_pageDictionary->Resources->$type === null) {
$this->_pageDictionary->Resources->touch();
$this->_pageDictionary->Resources->$type = new InternalType\DictionaryObject();
} else {
$this->_pageDictionary->Resources->$type->touch();
}
// Check, that resource is already attached to resource set.
$resObject = $resource->getResource();
foreach ($this->_pageDictionary->Resources->$type->getKeys() as $ResID) {
if ($this->_pageDictionary->Resources->$type->$ResID === $resObject) {
return $ResID;
}
}
$idCounter = 1;
do {
$newResName = $type[0] . $idCounter++;
} while ($this->_pageDictionary->Resources->$type->$newResName !== null);
$this->_pageDictionary->Resources->$type->$newResName = $resObject;
$this->_objFactory->attach($resource->getFactory());
return $newResName;
}
/**
* Add procedureSet to the Page description
*
* @param string $procSetName
*/
protected function _addProcSet($procSetName)
{
// Check that Resources dictionary contains ProcSet entry
if ($this->_pageDictionary->Resources->ProcSet === null) {
$this->_pageDictionary->Resources->touch();
$this->_pageDictionary->Resources->ProcSet = new InternalType\ArrayObject();
} else {
$this->_pageDictionary->Resources->ProcSet->touch();
}
foreach ($this->_pageDictionary->Resources->ProcSet->items as $procSetEntry) {
if ($procSetEntry->value == $procSetName) {
// Procset is already included into a ProcSet array
return;
}
}
$this->_pageDictionary->Resources->ProcSet->items[] = new InternalType\NameObject($procSetName);
}
/**
* Retrive PDF file reference to the page
*
* @internal
* @return \Zend\Pdf\InternalType\DictionaryObject
*/
public function getPageDictionary()
{
return $this->_pageDictionary;
}
/**
* Dump current drawing instructions into the content stream.
*
* @todo Don't forget to close all current graphics operations (like path drawing)
*
* @throws \Zend\Pdf\Exception
*/
public function flush()
{
if ($this->_saveCount != 0) {
throw new Exception('Saved graphics state is not restored');
}
if ($this->_contents == '') {
return;
}
if ($this->_pageDictionary->Contents->getType() != InternalType\AbstractTypeObject::TYPE_ARRAY) {
/**
* It's a stream object.
* Prepare Contents page attribute for update.
*/
$this->_pageDictionary->touch();
$currentPageContents = $this->_pageDictionary->Contents;
$this->_pageDictionary->Contents = new InternalType\ArrayObject();
$this->_pageDictionary->Contents->items[] = $currentPageContents;
} else {
$this->_pageDictionary->Contents->touch();
}
if ((!$this->_safeGS) && (count($this->_pageDictionary->Contents->items) != 0)) {
/**
* Page already has some content which is not treated as safe.
*
* Add save/restore GS operators
*/
$this->_addProcSet('PDF');
$newContentsArray = new InternalType\ArrayObject();
$newContentsArray->items[] = $this->_objFactory->newStreamObject(" q\n");
foreach ($this->_pageDictionary->Contents->items as $contentStream) {
$newContentsArray->items[] = $contentStream;
}
$newContentsArray->items[] = $this->_objFactory->newStreamObject(" Q\n");
$this->_pageDictionary->touch();
$this->_pageDictionary->Contents = $newContentsArray;
$this->_safeGS = true;
}
$this->_pageDictionary->Contents->items[] =
$this->_objFactory->newStreamObject($this->_contents);
$this->_contents = '';
}
/**
* Prepare page to be rendered into PDF.
*
* @todo Don't forget to close all current graphics operations (like path drawing)
*
* @param \Zend\Pdf\ObjectFactory $objFactory
* @throws \Zend\Pdf\Exception
*/
public function render(ObjectFactory $objFactory)
{
$this->flush();
if ($objFactory === $this->_objFactory) {
// Page is already attached to the document.
return;
}
if ($this->_attached) {
throw new Exception('Page is attached to one documen, but rendered in context of another.');
/**
* @todo Page cloning must be implemented here instead of exception.
* PDF objects (ex. fonts) can be shared between pages.
* Thus all referenced objects, which can be modified, must be cloned recursively,
* to avoid producing wrong object references in a context of source PDF.
*/
//...
} else {
$objFactory->attach($this->_objFactory);
}
}
/**
* Set fill color.
*
* @param \Zend\Pdf\Color $color
* @return \Zend\Pdf\Page
*/
public function setFillColor(Color $color)
{
$this->_addProcSet('PDF');
$this->_contents .= $color->instructions(false);
return $this;
}
/**
* Set line color.
*
* @param \Zend\Pdf\Color $color
* @return \Zend\Pdf\Page
*/
public function setLineColor(Color $color)
{
$this->_addProcSet('PDF');
$this->_contents .= $color->instructions(true);
return $this;
}
/**
* Set line width.
*
* @param float $width
* @return \Zend\Pdf\Page
*/
public function setLineWidth($width)
{
$this->_addProcSet('PDF');
$widthObj = new InternalType\NumericObject($width);
$this->_contents .= $widthObj->toString() . " w\n";
return $this;
}
/**
* Set line dashing pattern
*
* Pattern is an array of floats: array(on_length, off_length, on_length, off_length, ...)
* Phase is shift from the beginning of line.
*
* @param array $pattern
* @param array $phase
* @return \Zend\Pdf\Page
*/
public function setLineDashingPattern($pattern, $phase = 0)
{
$this->_addProcSet('PDF');
if ($pattern === self::LINE_DASHING_SOLID) {
$pattern = array();
$phase = 0;
}
$dashPattern = new InternalType\ArrayObject();
$phaseEleemnt = new InternalType\NumericObject($phase);
foreach ($pattern as $dashItem) {
$dashElement = new InternalType\NumericObject($dashItem);
$dashPattern->items[] = $dashElement;
}
$this->_contents .= $dashPattern->toString() . ' '
. $phaseEleemnt->toString() . " d\n";
return $this;
}
/**
* Set current font.
*
* @param \Zend\Pdf\Resource\Font\AbstractFont $font
* @param float $fontSize
* @return \Zend\Pdf\Page
*/
public function setFont(Resource\Font\AbstractFont $font, $fontSize)
{
$this->_addProcSet('Text');
$fontName = $this->_attachResource('Font', $font);
$this->_font = $font;
$this->_fontSize = $fontSize;
$fontNameObj = new InternalType\NameObject($fontName);
$fontSizeObj = new InternalType\NumericObject($fontSize);
$this->_contents .= $fontNameObj->toString() . ' ' . $fontSizeObj->toString() . " Tf\n";
return $this;
}
/**
* Set the style to use for future drawing operations on this page
*
* @param \Zend\Pdf\Style $style
* @return \Zend\Pdf\Page
*/
public function setStyle(Style $style)
{
$this->_style = $style;
$this->_addProcSet('Text');
$this->_addProcSet('PDF');
if ($style->getFont() !== null) {
$this->setFont($style->getFont(), $style->getFontSize());
}
$this->_contents .= $style->instructions($this->_pageDictionary->Resources);
return $this;
}
/**
* Set the transparancy
*
* $alpha == 0 - transparent
* $alpha == 1 - opaque
*
* Transparency modes, supported by PDF:
* Normal (default), Multiply, Screen, Overlay, Darken, Lighten, ColorDodge, ColorBurn, HardLight,
* SoftLight, Difference, Exclusion
*
* @param float $alpha
* @param string $mode
* @throws \Zend\Pdf\Exception
* @return \Zend\Pdf\Page
*/
public function setAlpha($alpha, $mode = 'Normal')
{
if (!in_array($mode, array('Normal', 'Multiply', 'Screen', 'Overlay', 'Darken', 'Lighten', 'ColorDodge',
'ColorBurn', 'HardLight', 'SoftLight', 'Difference', 'Exclusion'))) {
throw new Exception('Unsupported transparency mode.');
}
if (!is_numeric($alpha) || $alpha < 0 || $alpha > 1) {
throw new Exception('Alpha value must be numeric between 0 (transparent) and 1 (opaque).');
}
$this->_addProcSet('Text');
$this->_addProcSet('PDF');
$resources = $this->_pageDictionary->Resources;
// Check if Resources dictionary contains ExtGState entry
if ($resources->ExtGState === null) {
$resources->touch();
$resources->ExtGState = new InternalType\DictionaryObject();
} else {
$resources->ExtGState->touch();
}
$idCounter = 1;
do {
$gStateName = 'GS' . $idCounter++;
} while ($resources->ExtGState->$gStateName !== null);
$gStateDictionary = new InternalType\DictionaryObject();
$gStateDictionary->Type = new InternalType\NameObject('ExtGState');
$gStateDictionary->BM = new InternalType\NameObject($mode);
$gStateDictionary->CA = new InternalType\NumericObject($alpha);
$gStateDictionary->ca = new InternalType\NumericObject($alpha);
$resources->ExtGState->$gStateName = $this->_objFactory->newObject($gStateDictionary);
$gStateNameObj = new InternalType\NameObject($gStateName);
$this->_contents .= $gStateNameObj->toString() . " gs\n";
return $this;
}
/**
* Get current font.
*
* @return \Zend\Pdf\Resource\Font\AbstractFont $font
*/
public function getFont()
{
return $this->_font;
}
/**
* Extract resources attached to the page
*
* This method is not intended to be used in userland, but helps to optimize some document wide operations
*
* returns array of \Zend\Pdf\InternalType\DictionaryObject objects
*
* @internal
* @return array
*/
public function extractResources()
{
return $this->_pageDictionary->Resources;
}
/**
* Extract fonts attached to the page
*
* returns array of Zend_PDF_Resource_Font_Extracted objects
*
* @return array
* @throws \Zend\Pdf\Exception
*/
public function extractFonts()
{
if ($this->_pageDictionary->Resources->Font === null) {
// Page doesn't have any font attached
// Return empty array
return array();
}
$fontResources = $this->_pageDictionary->Resources->Font;
$fontResourcesUnique = array();
foreach ($fontResources->getKeys() as $fontResourceName) {
$fontDictionary = $fontResources->$fontResourceName;
if (! ($fontDictionary instanceof InternalType\IndirectObjectReference ||
$fontDictionary instanceof InternalType\IndirectObject) ) {
throw new Exception('Font dictionary has to be an indirect object or object reference.');
}
$fontResourcesUnique[spl_object_hash($fontDictionary->getObject())] = $fontDictionary;
}
$fonts = array();
foreach ($fontResourcesUnique as $resourceId => $fontDictionary) {
try {
// Try to extract font
$extractedFont = new Resource\Font\Extracted($fontDictionary);
$fonts[$resourceId] = $extractedFont;
} catch (Exception $e) {
if ($e->getMessage() != 'Unsupported font type.') {
throw new Exception($e->getMessage(), $e->getCode(), $e);
}
}
}
return $fonts;
}
/**
* Extract font attached to the page by specific font name
*
* $fontName should be specified in UTF-8 encoding
*
* @return \Zend\Pdf\Resource\Font\Extracted|null
* @throws \Zend\Pdf\Exception
*/
public function extractFont($fontName)
{
if ($this->_pageDictionary->Resources->Font === null) {
// Page doesn't have any font attached
return null;
}
$fontResources = $this->_pageDictionary->Resources->Font;
$fontResourcesUnique = array();
foreach ($fontResources->getKeys() as $fontResourceName) {
$fontDictionary = $fontResources->$fontResourceName;
if (! ($fontDictionary instanceof InternalType\IndirectObjectReference ||
$fontDictionary instanceof InternalType\IndirectObject) ) {
throw new Exception('Font dictionary has to be an indirect object or object reference.');
}
$resourceId = spl_object_hash($fontDictionary->getObject());
if (isset($fontResourcesUnique[$resourceId])) {
continue;
} else {
// Mark resource as processed
$fontResourcesUnique[$resourceId] = 1;
}
if ($fontDictionary->BaseFont->value != $fontName) {
continue;
}
try {
// Try to extract font
return new Resource\Font\Extracted($fontDictionary);
} catch (Exception $e) {
if ($e->getMessage() != 'Unsupported font type.') {
throw new Exception($e->getMessage(), $e->getCode(), $e);
}
// Continue searhing font with specified name
}
}
return null;
}
/**
* Get current font size
*
* @return float $fontSize
*/
public function getFontSize()
{
return $this->_fontSize;
}
/**
* Return the style, applied to the page.
*
* @return \Zend\Pdf\Style|null
*/
public function getStyle()
{
return $this->_style;
}
/**
* Save the graphics state of this page.
* This takes a snapshot of the currently applied style, position, clipping area and
* any rotation/translation/scaling that has been applied.
*
* @todo check for the open paths
* @throws \Zend\Pdf\Exception - if a save is performed with an open path
* @return \Zend\Pdf\Page
*/
public function saveGS()
{
$this->_saveCount++;
$this->_addProcSet('PDF');
$this->_contents .= " q\n";
return $this;
}
/**
* Restore the graphics state that was saved with the last call to saveGS().
*
* @throws \Zend\Pdf\Exception - if there is no previously saved state
* @return \Zend\Pdf\Page
*/
public function restoreGS()
{
if ($this->_saveCount-- <= 0) {
throw new Exception('Restoring graphics state which is not saved');
}
$this->_contents .= " Q\n";
return $this;
}
/**
* Intersect current clipping area with a circle.
*
* @param float $x
* @param float $y
* @param float $radius
* @param float $startAngle
* @param float $endAngle
* @return \Zend\Pdf\Page
*/
public function clipCircle($x, $y, $radius, $startAngle = null, $endAngle = null)
{
$this->clipEllipse($x - $radius, $y - $radius,
$x + $radius, $y + $radius,
$startAngle, $endAngle);
return $this;
}
/**
* Intersect current clipping area with a polygon.
*
* Method signatures:
* drawEllipse($x1, $y1, $x2, $y2);
* drawEllipse($x1, $y1, $x2, $y2, $startAngle, $endAngle);
*
* @todo process special cases with $x2-$x1 == 0 or $y2-$y1 == 0
*
* @param float $x1
* @param float $y1
* @param float $x2
* @param float $y2
* @param float $startAngle
* @param float $endAngle
* @return \Zend\Pdf\Page
*/
public function clipEllipse($x1, $y1, $x2, $y2, $startAngle = null, $endAngle = null)
{
$this->_addProcSet('PDF');
if ($x2 < $x1) {
$temp = $x1;
$x1 = $x2;
$x2 = $temp;
}
if ($y2 < $y1) {
$temp = $y1;
$y1 = $y2;
$y2 = $temp;
}
$x = ($x1 + $x2)/2.;
$y = ($y1 + $y2)/2.;
$xC = new InternalType\NumericObject($x);
$yC = new InternalType\NumericObject($y);
if ($startAngle !== null) {
if ($startAngle != 0) { $startAngle = fmod($startAngle, M_PI*2); }
if ($endAngle != 0) { $endAngle = fmod($endAngle, M_PI*2); }
if ($startAngle > $endAngle) {
$endAngle += M_PI*2;
}
$clipPath = $xC->toString() . ' ' . $yC->toString() . " m\n";
$clipSectors = (int)ceil(($endAngle - $startAngle)/M_PI_4);
$clipRadius = max($x2 - $x1, $y2 - $y1);
for($count = 0; $count <= $clipSectors; $count++) {
$pAngle = $startAngle + ($endAngle - $startAngle)*$count/(float)$clipSectors;
$pX = new InternalType\NumericObject($x + cos($pAngle)*$clipRadius);
$pY = new InternalType\NumericObject($y + sin($pAngle)*$clipRadius);
$clipPath .= $pX->toString() . ' ' . $pY->toString() . " l\n";
}
$this->_contents .= $clipPath . "h\nW\nn\n";
}
$xLeft = new InternalType\NumericObject($x1);
$xRight = new InternalType\NumericObject($x2);
$yUp = new InternalType\NumericObject($y2);
$yDown = new InternalType\NumericObject($y1);
$xDelta = 2*(M_SQRT2 - 1)*($x2 - $x1)/3.;
$yDelta = 2*(M_SQRT2 - 1)*($y2 - $y1)/3.;
$xr = new InternalType\NumericObject($x + $xDelta);
$xl = new InternalType\NumericObject($x - $xDelta);
$yu = new InternalType\NumericObject($y + $yDelta);
$yd = new InternalType\NumericObject($y - $yDelta);
$this->_contents .= $xC->toString() . ' ' . $yUp->toString() . " m\n"
. $xr->toString() . ' ' . $yUp->toString() . ' '
. $xRight->toString() . ' ' . $yu->toString() . ' '
. $xRight->toString() . ' ' . $yC->toString() . " c\n"
. $xRight->toString() . ' ' . $yd->toString() . ' '
. $xr->toString() . ' ' . $yDown->toString() . ' '
. $xC->toString() . ' ' . $yDown->toString() . " c\n"
. $xl->toString() . ' ' . $yDown->toString() . ' '
. $xLeft->toString() . ' ' . $yd->toString() . ' '
. $xLeft->toString() . ' ' . $yC->toString() . " c\n"
. $xLeft->toString() . ' ' . $yu->toString() . ' '
. $xl->toString() . ' ' . $yUp->toString() . ' '
. $xC->toString() . ' ' . $yUp->toString() . " c\n"
. "h\nW\nn\n";
return $this;
}
/**
* Intersect current clipping area with a polygon.
*
* @param array $x - array of float (the X co-ordinates of the vertices)
* @param array $y - array of float (the Y co-ordinates of the vertices)
* @param integer $fillMethod
* @return \Zend\Pdf\Page
*/
public function clipPolygon($x, $y, $fillMethod = self::FILL_METHOD_NON_ZERO_WINDING)
{
$this->_addProcSet('PDF');
$firstPoint = true;
foreach ($x as $id => $xVal) {
$xObj = new InternalType\NumericObject($xVal);
$yObj = new InternalType\NumericObject($y[$id]);
if ($firstPoint) {
$path = $xObj->toString() . ' ' . $yObj->toString() . " m\n";
$firstPoint = false;
} else {