source-class-Com.Tecnick.Pdf.Graph.Base

It appears that you are using AdBlocking software. The cost of running this website is covered by advertisements. If you like it please feel free to a small amount of money to secure the future of this website.
  1: <?php
  2: /**
  3:  * Base.php
  4:  *
  5:  * @since       2011-05-23
  6:  * @category    Library
  7:  * @package     PdfGraph
  8:  * @author      Nicola Asuni <info@tecnick.com>
  9:  * @copyright   2011-2016 Nicola Asuni - Tecnick.com LTD
 10:  * @license     http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT)
 11:  * @link        https://github.com/tecnickcom/tc-lib-pdf-graph
 12:  *
 13:  * This file is part of tc-lib-pdf-graph software library.
 14:  */
 15: 
 16: namespace Com\Tecnick\Pdf\Graph;
 17: 
 18: use \Com\Tecnick\Color\Pdf as PdfColor;
 19: use \Com\Tecnick\Pdf\Encrypt\Encrypt;
 20: use \Com\Tecnick\Pdf\Graph\Exception as GraphException;
 21: 
 22: /**
 23:  * Com\Tecnick\Pdf\Graph\Base
 24:  *
 25:  * @since       2011-05-23
 26:  * @category    Library
 27:  * @package     PdfGraph
 28:  * @author      Nicola Asuni <info@tecnick.com>
 29:  * @copyright   2011-2016 Nicola Asuni - Tecnick.com LTD
 30:  * @license     http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT)
 31:  * @link        https://github.com/tecnickcom/tc-lib-pdf-graph
 32:  */
 33: abstract class Base
 34: {
 35:     /**
 36:      * Pi constant
 37:      * We use this instead of M_PI because HHVM has a different value.
 38:      *
 39:      * @var float
 40:      */
 41:     const MPI = 3.14159265358979323846264338327950288419716939937510;
 42: 
 43:     /**
 44:      * Current PDF object number
 45:      *
 46:      * @var int
 47:      */
 48:     protected $pon;
 49: 
 50:     /**
 51:      * Current page height
 52:      *
 53:      * @var float
 54:      */
 55:     protected $pageh = 0;
 56: 
 57:     /**
 58:      * Current page width
 59:      *
 60:      * @var float
 61:      */
 62:     protected $pagew = 0;
 63: 
 64:     /**
 65:      * Unit of measure conversion ratio
 66:      *
 67:      * @var float
 68:      */
 69:     protected $kunit = 1.0;
 70: 
 71:     /**
 72:      * Color object
 73:      *
 74:      * @var PdfColor
 75:      */
 76:     protected $col;
 77: 
 78:     /**
 79:      * Encrypt object
 80:      *
 81:      * @var Encrypt
 82:      */
 83:     protected $enc;
 84: 
 85:     /**
 86:      * True if we are in PDF/A mode.
 87:      *
 88:      * @var bool
 89:      */
 90:     protected $pdfa = false;
 91: 
 92:     /**
 93:      * Initialize
 94:      *
 95:      * @param float    $kunit  Unit of measure conversion ratio.
 96:      * @param float    $pagew  Page width.
 97:      * @param float    $pageh  Page height.
 98:      * @param PdfColor $color  Color object.
 99:      * @param bool     $pdfa   True if we are in PDF/A mode.
100:      */
101:     public function __construct($kunit, $pagew, $pageh, PdfColor $color, Encrypt $enc, $pdfa = false)
102:     {
103:         $this->setKUnit($kunit);
104:         $this->setPageWidth($pagew);
105:         $this->setPageHeight($pageh);
106:         $this->col = $color;
107:         $this->enc = $enc;
108:         $this->pdfa = (bool) $pdfa;
109:         $this->init();
110:     }
111: 
112:     /**
113:      * Returns current PDF object number
114:      *
115:      * @return int
116:      */
117:     public function getObjectNumber()
118:     {
119:         return $this->pon;
120:     }
121: 
122:     /**
123:      * Initialize objects
124:      */
125:     abstract public function init();
126: 
127:     /**
128:      * Set page height
129:      *
130:      * @param float  $pageh  Page height
131:      */
132:     public function setPageHeight($pageh)
133:     {
134:         $this->pageh = (float) $pageh;
135:         return $this;
136:     }
137: 
138:     /**
139:      * Set page width
140:      *
141:      * @param float  $pagew  Page width
142:      */
143:     public function setPageWidth($pagew)
144:     {
145:         $this->pagew = (float) $pagew;
146:         return $this;
147:     }
148: 
149:     /**
150:      * Set unit of measure conversion ratio.
151:      *
152:      * @param float  $kunit  Unit of measure conversion ratio.
153:      */
154:     public function setKUnit($kunit)
155:     {
156:         $this->kunit = (float) $kunit;
157:         return $this;
158:     }
159:     
160:     /**
161:      * Get the PDF output string for ExtGState
162:      *
163:      * @param int $pon Current PDF Object Number
164:      *
165:      * @return string PDF command
166:      */
167:     public function getOutExtGState($pon)
168:     {
169:         $this->pon = (int) $pon;
170:         $out = '';
171:         foreach ($this->extgstates as $idx => $ext) {
172:             $this->extgstates[$idx]['n'] = ++$this->pon;
173:             $out .= $this->pon.' 0 obj'."\n"
174:                 .'<< /Type /ExtGState';
175:             foreach ($ext['parms'] as $key => $val) {
176:                 if (is_numeric($val)) {
177:                     $val = sprintf('%F', $val);
178:                 } elseif ($val === true) {
179:                     $val = 'true';
180:                 } elseif ($val === false) {
181:                     $val = 'false';
182:                 }
183:                 $out .= ' /'.$key.' '.$val;
184:             }
185:             $out .= ' >>'."\n"
186:             .'endobj'."\n";
187:         }
188:         return $out;
189:     }
190: 
191:     /**
192:      * Get the PDF output string for ExtGState Resource Dictionary
193:      *
194:      * @return string PDF command
195:      */
196:     public function getOutExtGStateResources()
197:     {
198:         if ($this->pdfa || empty($this->extgstates)) {
199:             return '';
200:         }
201:         $out = ' /ExtGState <<';
202:         foreach ($this->extgstates as $key => $ext) {
203:             if (isset($ext['name'])) {
204:                 $out .= ' /'.$ext['name'];
205:             } else {
206:                 $out .= ' /GS'.$key;
207:             }
208:             $out .= ' '.$ext['n'].' 0 R';
209:         }
210:         $out .= ' >>'."\n";
211:         return $out;
212:     }
213:     
214:     /**
215:      * Get the PDF output string for Gradients Resource Dictionary
216:      *
217:      * @return string PDF command
218:      */
219:     public function getOutGradientResources()
220:     {
221:         if ($this->pdfa || empty($this->gradients)) {
222:             return '';
223:         }
224:         $grp = '';
225:         $grs = '';
226:         foreach ($this->gradients as $idx => $grad) {
227:             // gradient patterns
228:             $grp .= ' /p'.$idx.' '.$grad['pattern'].' 0 R';
229:             // gradient shadings
230:             $grs .= ' /Sh'.$idx.' '.$grad['id'].' 0 R';
231:         }
232:         return ' /Pattern <<'.$grp.' >>'."\n"
233:             .' /Shading <<'.$grs.' >>'."\n";
234:     }
235: 
236:     /**
237:      * Get the PDF output string for gradient colors and transparency
238:      *
239:      * @param array  $grad Array of gradient colors
240:      * @param string $type Type of output: 'color' or 'opacity'
241:      *
242:      * @return string PDF command
243:      */
244:     protected function getOutGradientCols($grad, $type)
245:     {
246:         if (($type == 'opacity') && !$grad['transparency']) {
247:             return '';
248:         }
249: 
250:         $out = '';
251: 
252:         if (($grad['type'] == 2) || ($grad['type'] == 3)) {
253:             $num_cols = count($grad['colors']);
254:             $lastcols = ($num_cols - 1);
255:             $funct = array(); // color and transparency objects
256:             $bounds = array();
257:             $encode = array();
258: 
259:             for ($idx = 1; $idx < $num_cols; ++$idx) {
260:                 $encode[] = '0 1';
261:                 if ($idx < $lastcols) {
262:                     $bounds[] = sprintf('%F ', $grad['colors'][$idx]['offset']);
263:                 }
264:                 $out .= ++$this->pon.' 0 obj'."\n"
265:                 .'<<'
266:                 .' /FunctionType 2'
267:                 .' /Domain [0 1]'
268:                 .' /C0 ['.$grad['colors'][($idx - 1)][$type].']'
269:                 .' /C1 ['.$grad['colors'][$idx][$type].']'
270:                 .' /N '.$grad['colors'][$idx]['exponent']
271:                 .' >>'."\n"
272:                 .'endobj'."\n";
273:                 $funct[] = $this->pon.' 0 R';
274:             }
275: 
276:             $out .= ++$this->pon.' 0 obj'."\n"
277:                 .'<<'
278:                 .' /FunctionType 3'
279:                 .' /Domain [0 1]'
280:                 .' /Functions ['.implode(' ', $funct).']'
281:                 .' /Bounds ['.implode(' ', $bounds).']'
282:                 .' /Encode ['.implode(' ', $encode).']'
283:                 .' >>'."\n"
284:                 .'endobj'."\n";
285:         }
286: 
287:         $out .= $this->getOutPatternObj($grad, $this->pon);
288:         return $out;
289:     }
290: 
291:     /**
292:      * Get the PDF output string for the pattern and shading object
293:      *
294:      * @param array  $grad    Array of gradient colors
295:      * @param int    $objref  Refrence object number
296:      *
297:      * @return string PDF command
298:      */
299:     protected function getOutPatternObj($grad, $objref)
300:     {
301:         // set shading object
302:         if ($grad['transparency']) {
303:             $grad['colspace'] = 'DeviceGray';
304:         }
305:         
306:         $objref = ++$this->pon;
307:         $out = $objref.' 0 obj'."\n"
308:             .'<<'
309:             .' /ShadingType '.$grad['type']
310:             .' /ColorSpace /'.$grad['colspace'];
311:         if (!empty($grad['background'])) {
312:             $out .= ' /Background ['.$grad['background']->getComponentsString().']';
313:         }
314:         if (!empty($grad['antialias'])) {
315:             $out .= ' /AntiAlias true';
316:         }
317:         if ($grad['type'] == 2) {
318:             $out .= ' '.sprintf(
319:                 '/Coords [%F %F %F %F]',
320:                 $grad['coords'][0],
321:                 $grad['coords'][1],
322:                 $grad['coords'][2],
323:                 $grad['coords'][3]
324:             )
325:                 .' /Domain [0 1]'
326:                 .' /Function '.$objref.' 0 R'
327:                 .' /Extend [true true]'
328:                 .' >>'."\n";
329:         } elseif ($grad['type'] == 3) {
330:             // x0, y0, r0, x1, y1, r1
331:             // the  radius of the inner circle is 0
332:             $out .= ' '.sprintf(
333:                 '/Coords [%F %F 0 %F %F %F]',
334:                 $grad['coords'][0],
335:                 $grad['coords'][1],
336:                 $grad['coords'][2],
337:                 $grad['coords'][3],
338:                 $grad['coords'][4]
339:             )
340:                 .' /Domain [0 1]'
341:                 .' /Function '.$objref.' 0 R'
342:                 .' /Extend [true true]'
343:                 .' >>'."\n";
344:         } elseif ($grad['type'] == 6) {
345:             $stream = $this->enc->encryptString($grad['stream'], $this->pon);
346:             $out .= ' /BitsPerCoordinate 16'
347:                 .' /BitsPerComponent 8'
348:                 .' /Decode[0 1 0 1 0 1 0 1 0 1]'
349:                 .' /BitsPerFlag 8'
350:                 .' /Length '.strlen($stream)
351:                 .' >>'."\n"
352:                 .' stream'."\n"
353:                 .$stream."\n"
354:                 .'endstream'."\n";
355:         }
356:         $out .= 'endobj'."\n";
357: 
358:         // pattern object
359:         $out .= ++$this->pon.' 0 obj'."\n"
360:             .'<<'
361:             .' /Type /Pattern'
362:             .' /PatternType 2'
363:             .' /Shading '.$objref.' 0 R'
364:             .' >>'."\n"
365:             .'endobj'
366:             ."\n";
367: 
368:         return $out;
369:     }
370: 
371:     /**
372:      * Get the PDF output string for gradient shaders
373:      *
374:      * @param int $pon Current PDF Object Number
375:      *
376:      * @return string PDF command
377:      */
378:     public function getOutGradientShaders($pon)
379:     {
380:         if ($this->pdfa || empty($this->gradients)) {
381:             return '';
382:         }
383: 
384:         $this->pon = (int) $pon;
385:         $idt = count($this->gradients); // index for transparency gradients
386: 
387:         $out = '';
388:         foreach ($this->gradients as $idx => $grad) {
389:             $out .= $this->getOutGradientCols($grad, 'color');
390:             $this->gradients[$idx]['id'] = ($this->pon - 1);
391:             $this->gradients[$idx]['pattern'] = $this->pon;
392: 
393:             $out .= $this->getOutGradientCols($grad, 'opacity');
394:             $idgs = ($idx + $idt);
395:             $this->gradients[$idgs]['id'] = ($this->pon - 1);
396:             $this->gradients[$idgs]['pattern'] = $this->pon;
397: 
398:             if ($grad['transparency']) {
399:                 $oid = ++$this->pon;
400:                 $pwidth = ($this->pagew * $this->kunit);
401:                 $pheight = ($this->pageh * $this->kunit);
402:                 $stream = 'q /a0 gs /Pattern cs /p'.$idgs.' scn 0 0 '.$pwidth.' '.$pheight.' re f Q';
403:                 $stream = gzcompress($stream);
404:                 $stream = $this->enc->encryptString($stream, $oid);
405:                 $rect = sprintf('%F %F', $pwidth, $pheight);
406: 
407:                 $out .= $oid.' 0 obj'."\n"
408:                     .'<<'
409:                     .' /Type /XObject'
410:                     .' /Subtype /Form'
411:                     .' /FormType 1'
412:                     .' /Filter /FlateDecode'
413:                     .' /Length '.strlen($stream)
414:                     .' /BBox [0 0 '.$rect.']'
415:                     .' /Group << /Type /Group /S /Transparency /CS /DeviceGray >>'
416:                     .' /Resources <<'
417:                     .' /ExtGState << /a0 << /ca 1 /CA 1 >>  >>'
418:                     .' /Pattern << /p'.$idgs.' '.$this->gradients[$idgs]['pattern'].' 0 R >>'
419:                     .' >>'
420:                     .' >>'."\n"
421:                     .' stream'."\n"
422:                     .$stream."\n"
423:                     .'endstream'."\n"
424:                     .'endobj'."\n";
425: 
426:                 // SMask
427:                 $objsm = ++$this->pon;
428:                 $out .= $objsm.' 0 obj'."\n"
429:                     .'<<'
430:                     .' /Type /Mask'
431:                     .' /S /Luminosity'
432:                     .' /G '.$oid.' 0 R'
433:                     .' >>'."\n"
434:                     .'endobj'."\n";
435: 
436:                 // ExtGState
437:                 $objext = ++$this->pon;
438:                 $out .= ++$objext.' 0 obj'."\n"
439:                     .'<<'
440:                     .' /Type /ExtGState'
441:                     .' /SMask '.$objsm.' 0 R'
442:                     .' /AIS false'
443:                     .' >>'."\n"
444:                     .'endobj'."\n";
445:                 $this->extgstates[] = array('n' => $objext, 'name' => 'TGS'.$idx);
446:             }
447:         }
448: 
449:         return $out;
450:     }
451: }
452: 
 

© 2004-2017 – Nicola Asuni - Tecnick.com - All rights reserved.
about - disclaimer - privacy