-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCell.php
More file actions
644 lines (575 loc) · 19.4 KB
/
Copy pathCell.php
File metadata and controls
644 lines (575 loc) · 19.4 KB
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
<?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 license@zend.com so we can send you a copy immediately.
*
* @package Zend_Pdf
* @copyright Copyright (c) 2005-2007 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
/* Changes:
2008-12-02 Dominik Deobald <dominik DOT deobald AT interdose DOT com>
- Applied some changes to make it at least a little bit more UTF-8 compatible.
Umlauts were missing when a line had to be wordwrapped.
2009-01-12 Dominik Deobald <dominik DOT deobald AT interdose DOT com>
- Fixed bug in WordWrap where last word would just fit into a row,
but the following space would not. Resulted in infinite loop.
- Cleaned up my previous changes concerning char encodings. "Sections"
contain encoding information, so no additional parameter is required.
*/
/** Zend_Pdf_Exception */
require_once 'Zend/Pdf/Exception.php';
/** Zend_Pdf_Resource_Font */
require_once 'Zend/Pdf/Resource/Font.php';
/** Zend_Pdf_Page*/
require_once 'Zend/Pdf/Page.php';
/** Zend_Pdf_Cmap */
require_once 'Zend/Pdf/Cmap.php';
/** Zend_Pdf_Color */
require_once 'Zend/Pdf/Color.php';
class Zend_Pdf_Cell {
const ALIGN_LEFT=0;
const ALIGN_RIGHT=1;
const ALIGN_CENTER=2;
const ALIGN_JUSTIFY=3;
const POSITION_LEFT=0;
const POSITION_RIGHT=1;
const POSITION_CENTER_X=2;
const POSITION_CENTER_Y=4;
const POSITION_TOP=8;
const POSITION_BOTTOM=16;
/**
* Width of the cell
*/
private $_width;
/**
* Height of the cell
*/
private $_height;
/**
* Upper left X coordinate
*/
private $_x;
/**
* Upper left Y coordinate
*/
private $_y;
/**
* Current page for the cell to belong to.
*/
private $_page;
/**
* How the cell should be positioned on the page
*/
private $_position;
/**
* 3 diminsional array that stores the text in the cell.
* The first diminsion is for the line number.
* The second diminsion is for the section number.
* The third diminsion is the properties for that section.
*
* The only properties (3rd diminsion) valid for a non-zero section (second diminsion)
* are text, font and encoding.
*/
private $_text;
/**
* Keeps track of the current line number
*/
private $_lineNumber;
/**
* Keeps track of the current section
*/
private $_section;
/**
* Current font that is being used.
*/
private $_font;
/**
* Current font's height
*/
private $_fontSize;
/**
* When we want to auto-calculate the height, this is the
* height's current value.
*/
private $_autoHeight;
/**
* When we want to aut-calculate the width, this is the
* width's current value.
*/
private $_autoWidth;
/**
* Border around the cell.
*
* Defaults to BORDER_NONE.
*/
private $_border;
/**
* Creates a new cell.
*
* @param Zend_Pdf_Page $page Page for the cell to belong to.
* @param constant $position What type of position to have
* @param int $width Width of the cell. Provide 0 for auto-width.
* @param int $height Height of the cell. Provide 0 for auto-height.
*/
public function __construct($page,$position=Zend_Pdf_Cell::POSITION_LEFT,$width=0,$height=0) {
$this->setPage($page);
$this->_lineNumber=0;
$this->_section=0;
$this->_font=$page->getFont();
$this->_fontSize=$page->getFontSize();
$this->_initializeLine();
$this->_initializeBorder();
$this->setPosition($position);
$this->setWidth($width);
$this->setHeight($height);
}
private function _initializeBorder() {
$this->_border['pattern']=Zend_Pdf_Page::LINE_DASHING_SOLID;
$this->_border['size']=0;
$this->_border['color']=new Zend_Pdf_Color_RGB(0,0,0);
}
/**
* Wraps the section if needed.
*
* This function explodes the section's text, and adds one word at a time until it goes over
* the boundry. Once it goes over the boundry, it wraps the rest of the text up into its
* own text section and calls addText. If the current section had been wrapped, then
* this function returns true, otherwise it returns false.
*
* @param array $section Section of text to wrap.
* @return boolean True if it wrapped text, false if it did not.
*/
private function _wordWrap($section) {
if ($this->isAutoWidth()) {
$maxWidth=$this->_page->getWidth();
} else {
$maxWidth=$this->_width;
}
$lineWidth=$this->_text[$this->_lineNumber]['width'];
//if adding this section over flows borders
if ($lineWidth+$section['width'] > $maxWidth) {
//section of text is greater than our box's diminsions
$splitSection=explode(' ',$section['text']);
$maxTextSection=$this->_makeTextSection(array_shift($splitSection), $section['encoding']);
$spaceWidth=$this->_getTextWidth($this->_makeTextSection(' '));
while($maxTextSection['text']!=null && $lineWidth + $maxTextSection['width'] < $maxWidth) {
$this->addText($maxTextSection['text'], null, 0, $section['encoding']);
$lineWidth=$this->_text[$this->_lineNumber]['width'];
if ($lineWidth + $spaceWidth < $maxWidth) {
$this->addText(' ');
$lineWidth += $spaceWidth;
}
$maxTextSection=$this->_makeTextSection(array_shift($splitSection), $section['encoding']);
}
$this->newLine();
$restOfText=implode(' ',$splitSection);
$this->addText($maxTextSection['text'].' '.$restOfText, null, 0, $section['encoding']);
return true;
}
return false;
}
/**
* Turns a string of text into a text section
*
* @param string $text String of text to turn into a section.
* @return array A text section array.
*/
private function _makeTextSection($text, $charEncoding='ISO-8859-1') {
$section=array();
$section['text']=$text;
$section['encoding']=$charEncoding;
$section['font']=$this->_font;
$section['fontSize']=$this->_fontSize;
$section['width']=$this->_getTextWidth($section);
return $section;
}
/**
* Adds more text to the cell. This will create a new section of text.
*
* The text is not written to the PDF until write() is called.
*
* If this section is not the first section, then the alignment, x and y variables are ignored.
*
* @param string $text Text to add to the section
* @param mixed $alignment (Optional) How to align the text in the cell. If no alignment is
* specified, then it uses the previous line's alignment. If this is the first line in the
* cell, then it uses Zend_Pdf_Cell::ALIGN_LEFT as default.
* @param int $x (Optional) Offset of X from the relative position of this line in the cell.
* Defaults to 0
* @param string $charEncoding (Optional) Encoding of this particular section of text.
* Defaults to current locale.
*/
public function addText($text, $alignment=null, $offset=0, $charEncoding='ISO-8859-1') {
//Set the alignment
if ($alignment!==null) {
$this->_text[$this->_lineNumber]['alignment']=$alignment;
} else {
$this->_text[$this->_lineNumber]['alignment'];
if ($this->_lineNumber==0) {
if ($this->_text[$this->_lineNumber]['alignment']==null) {
//defaults to left alignment
$this->_text[$this->_lineNumber]['alignment']=Zend_Pdf_Cell::ALIGN_LEFT;
}
//we have word wrapped on the first line, so don't do anything
} else {
//else defaults to previous line's alignment
$this->_text[$this->_lineNumber]['alignment']=$this->_text[$this->_lineNumber-1]['alignment'];
}
}
$section=$this->_makeTextSection($text,$charEncoding);
if ($this->_wordWrap($section)) {
return; //word wrap has already taken care of calling addText
}
$this->_text[$this->_lineNumber][$this->_section]['text']=$section['text'];
$this->_text[$this->_lineNumber][$this->_section]['encoding']=$section['encoding'];
$this->_text[$this->_lineNumber][$this->_section]['font']=$section['font'];
$this->_text[$this->_lineNumber][$this->_section]['fontSize']=$section['fontSize'];
$this->_text[$this->_lineNumber][$this->_section]['width']=$section['width'];
//if we haven't set a width, do so and initialize it to the width of this piece of text,
//otherwise just add the width to this piece of text.
if (isset($this->_text[$this->_lineNumber]['width'])) {
$this->_text[$this->_lineNumber]['width']+=$this->_text[$this->_lineNumber][$this->_section]['width'];
} else {
$this->_text[$this->_lineNumber]['width']=$this->_text[$this->_lineNumber][$this->_section]['width'];
}
$this->_text[$this->_lineNumber]['x']=$offset;
//calculate the max width of the cell if we have an auto-size width cell
if ($this->isAutoWidth()) {
$this->_autoWidth = max($this->_text[$this->_lineNumber]['width'],$this->_autoWidth);
}
//calculate the max height of this line if we have an auto-size height cell
if ($this->isAutoHeight()) {
if (isset($this->_text[$this->_lineNumber]['height'])) {
$this->_text[$this->_lineNumber]['height'] = max($this->_text[$this->_lineNumber]['height'], ($this->_font->getLineHeight()/$this->_font->getUnitsPerEm())*$this->_fontSize);
$this->_autoHeight=max($this->_autoHeight,$this->_text[$this->_lineNumber]['height']);
} else {
$this->_text[$this->_lineNumber]['height']=($this->_font->getLineHeight()/$this->_font->getUnitsPerEm())*$this->_fontSize;
$this->_autoHeight=($this->_font->getLineHeight()/$this->_font->getUnitsPerEm())*$this->_fontSize;
}
}
$this->_section++;
}
/**
* Initialize a new line in the cell.
*
* @return void
*/
private function _initializeLine() {
$this->_text[$this->_lineNumber]['x']=0;
$this->_text[$this->_lineNumber]['width']=0;
$this->_text[$this->_lineNumber]['height']=0;
$this->_text[$this->_lineNumber]['alignment']=null;
}
/**
* Adds a new line to the cell.
*/
public function newLine() {
$this->_section=0;
$this->_lineNumber++;
$this->_text[$this->_lineNumber]=array();
$this->_text[$this->_lineNumber][0]['text']='';
$this->_text[$this->_lineNumber][0]['encoding']='ISO-8859-1';
$this->_text[$this->_lineNumber][0]['font']=$this->_font;
$this->_text[$this->_lineNumber][0]['fontSize']=$this->_fontSize;
$this->_text[$this->_lineNumber][0]['width']=0;
$this->_initializeLine();
$this->_text[$this->_lineNumber]['alignment']=$this->_text[$this->_lineNumber-1]['alignment'];
//add the last cell's height to the auto height if we have an auto-height box.
if ($this->isAutoHeight()) {
$this->_autoHeight+=$this->_text[$this->_lineNumber-1]['height'];
}
}
/**
* Returns the width of the cell, without the border
*
* @return int Width of cell, in pixels, without the border.
*/
public function getWidth() {
if ($this->isAutoWidth()) {
return $this->_autoWidth;
} else {
return $this->_width;
}
}
/**
* Returns the height of the cell, without the border
*
* @return int Width of cell, in pixels, without the border.
*/
public function getHeight() {
if ($this->isAutoWidth()) {
return $this->_autoHeight;
} else {
return $this->_height;
}
}
public function getPosition() {
return $this->_position;
}
public function getPage() {
return $this->_page;
}
/**
* Returns the type of border.
*
* Currently only a solid border is supported.
*
* @return int Pattern of border
*/
public function getBorderPattern() {
return $this->_border['pattern'];
}
/**
* Returns the size in pixels of the border around the cell.
*
* @return int Size of the border, in pixels.
*/
public function getBorderSize() {
return $this->_border['size'];
}
/**
* Returns the color of the border
*
* @return Zend_Pdf_Color Color of the border.
*/
public function getBorderColor() {
return $this->_border['color'];
}
/**
* Sets the border around the cell.
*
* A border may be placed around the cell. If the border is greater than
* one pixel in width, then the border grows outwards away from the cell.
*
* For Example:
*
* If you create a cell with a width of 100 pixels, and you have a border of
* 10 pixels, then the total width of the cell is 120 pixels.
*
* @todo Implement more borders.
* @param int $pattern Type of border to draw.
* @param int $size Size of border, in pixels.
* @param Zend_Pdf_Color $color Color for the border. Defaults to
* Zend_Pdf_Color_RGB(0,0,0)
*/
public function setBorder($size=1,$pattern=null,$color=null) {
if ($pattern===null) {
$this->_border['pattern']=Zend_Pdf_Page::LINE_DASHING_SOLID;
} else {
$this->_border['pattern']=$pattern;
}
$this->_border['size']=$size;
if ($color===null) {
$this->_border['color']=new Zend_Pdf_Color_RGB(0,0,0);
}
}
/**
* Sets the location of where the cell's upper left corner should be placed.
* If the alignment is set, then x and y are offsets to the alignment.
*
* @param int $x X offset for the cell.
* @param int $y Y offset for the cell.
* @param mixed $alignment (Optional) How to align the cell with the X and Y as offsets.
* Defaults to none.
*/
public function setLocation($x, $y, $alignment=Zend_Pdf_Cell::ALIGN_LEFT) {
$this->_position=$alignment;
$this->_x=$x;
$this->_y=$y;
}
/**
* Changes the current section's font to the one specified.
*
* @param Zend_Pdf_Font $font Font of the current section.
*/
public function setFont($font, $size) {
$this->_font=$font;
$this->_fontSize=$size;
}
/**
* Sets the current section's encoding to the encoding specified.
*
* @param string $encoding Character encoding of the text.
*/
public function setCharEncoding($encoding) {
$this->_text[$this->_lineNumber][$this->_section]['encoding']=$encoding;
}
public function setPage($page) {
$this->_page=$page;
}
/**
* Sets the maximum height of the cell, without the border.
*
* @todo Decide what to do if size of text exceeds cell height.
* @param int $height Maximum height of the cell, in pixels.
*/
public function setHeight($height) {
$this->_height=$height;
$this->_autoHeight=$height;
}
/**
* Sets the maximum width of the cell, without the border.
*
* If text is added that exceeds the width of the cell, then the text
* will be wrapped at a space.
*
* @todo Decide what to do if one word exceeds border width.
* @param int $width Maximum width of the cell, in pixels.
*/
public function setWidth($width) {
$this->_width=$width;
$this->_autoWidth=$width;
}
public function setPosition($position) {
$this->_position=$position;
}
/**
* Returns true if this cell is using auto width
*
* @return boolean If this cell is set to be auto-width
*/
public function isAutoWidth() {
if ($this->_width==0) {
return true;
} else {
return false;
}
}
/**
* Returns true if the cell is using auto height
*
* @return boolean If this cell is set to be auto height.
*/
public function isAutoHeight() {
if ($this->_height==0) {
return true;
} else {
return false;
}
}
/**
* Draws the cell to the PDF page.
*
* This function will parse the internal $_text field and draw the cell to the PDF page.
*/
public function write() {
if (!($this->_page instanceof Zend_Pdf_Page)) {
throw new Zend_Pdf_Exception("The PDF page that the cell is attempting to write to is not a valid page.");
}
if (!($this->_font instanceof Zend_Pdf_Resource_Font)) {
throw new Zend_Pdf_Exception('No font has been set');
}
if ($this->isAutoHeight()) {
$this->_height=$this->_autoHeight;
}
if ($this->isAutoWidth()) {
$this->_width=$this->_autoWidth;
}
//positions of the cell's box
//initalize the diminsions to defaults
$top=$this->_y;
$left=$this->_x;
$right=$left+$this->getWidth();
$bottom=$top+$this->getHeight();
if ($this->_position & Zend_Pdf_Cell::POSITION_BOTTOM) {
$top=$this->getHeight();
$bottom=$top+$this->getHeight();
}
if ($this->_position & Zend_Pdf_Cell::POSITION_CENTER_X) {
$left=$this->_page->getWidth()/2 - $this->getWidth()/2 + $this->_x;
$right=$left+$this->getWidth();
}
if ($this->_position & Zend_Pdf_Cell::POSITION_CENTER_Y) {
$top=$this->_page->getHeight()/2 + $this->getHeight()/2 - $this->_y;
$bottom=$top-$this->getHeight();
}
if ($this->_position & Zend_Pdf_Cell::POSITION_TOP ) {
$top=$this->_page->getHeight();
$bottom=$top+$this->getHeight();
}
if ($this->_position & Zend_Pdf_Cell::POSITION_RIGHT) {
$left=$this->_page->getWidth() - $this->getWidth();
$right=$left+$this->getWidth();
}
$currentY=$top;
//save the page's font so we can put it back after writing the cell
$pageFont=$this->_page->getFont();
$fontSize=$this->_page->getFontSize();
//restore old size and font
$this->_page->setFont($pageFont,$fontSize);
//draw the border
if ($this->_border['size']>0) {
$style=new Zend_Pdf_Style();
$style->setLineColor($this->getBorderColor());
$style->setFillColor(new Zend_Pdf_Color_RGB(255,255,255));
$style->setLineDashingPattern($this->getBorderPattern());
$this->_page->setStyle($style);
$this->_page->drawRectangle($right,$top,$left,$bottom);
$style->setFillColor(new Zend_Pdf_Color_RGB(0,0,0));
$this->_page->setStyle($style);
}
//draw every section of every page.
for ($i=0;$i<count($this->_text);$i++) {
$currentX=0;
switch ($this->_text[$i]['alignment']) {
case Zend_Pdf_Cell::ALIGN_RIGHT:
$currentX=$right - $this->_text[$i]['width'];
break;
case Zend_Pdf_Cell::ALIGN_CENTER:
$currentX=($right-$left)/2+$left-$this->_text[$i]['width']/2;
break;
case Zend_Pdf_Cell::ALIGN_JUSTIFY:
//@todo
break;
default:
$currentX=$left;
break;
}
//add the offset
$currentX+=$this->_text[$i]['x'];
$currentY-=$this->_text[$i]['height'];
//count() - 4 because of the 4 properties to this text.
for ($j=0;$j<count($this->_text[$i])-4;$j++) {
$this->_page->setFont($this->_text[$i][$j]['font'],$this->_text[$i][$j]['fontSize']);
$this->_page->drawText($this->_text[$i][$j]['text'],$currentX,$currentY,$this->_text[$i][$j]['encoding']);
$currentX+=$this->_text[$i][$j]['width'];
}
}
}
/**
* Returns the total width in points of the string using the specified font and
* size.
*
* This is not the most efficient way to perform this calculation. I'm
* concentrating optimization efforts on the upcoming layout manager class.
* Similar calculations exist inside the layout manager class, but widths are
* generally calculated only after determining line fragments.
*
* @link http://devzone.zend.com/article/2525-Zend_Pdf-tutorial#comments-2535
* @link http://stackoverflow.com/questions/1283555/zend-pdf-calculating-length-of-text-string-in-current-font-for-line-wrapping
* @param array $textSection A section of text that has the font, character encoding and text.
* @return float Number of PDF units wide the text should be.
*/
private function _getTextWidth($textSection) {
$drawingString = iconv($textSection['encoding'], 'UTF-16BE//IGNORE', $textSection['text']);
$characters = array();
for ($i = 0; $i < strlen($drawingString); $i++) {
$characters[] = (ord($drawingString[$i++]) << 8 ) | ord($drawingString[$i]);
}
$glyphs = $textSection['font']->glyphNumbersForCharacters($characters);
$widths = $textSection['font']->widthsForGlyphs($glyphs);
$stringWidth = (array_sum($widths) / $textSection['font']->getUnitsPerEm()) * $textSection['fontSize'];
echo $drawingString, ' - ', $stringWidth, '<br>';
return $stringWidth;
}
}