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

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:  * Style.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\Pdf\Graph\Exception as GraphException;
 19: 
 20: /**
 21:  * Com\Tecnick\Pdf\Graph\Style
 22:  *
 23:  * @since       2011-05-23
 24:  * @category    Library
 25:  * @package     PdfGraph
 26:  * @author      Nicola Asuni <info@tecnick.com>
 27:  * @copyright   2011-2016 Nicola Asuni - Tecnick.com LTD
 28:  * @license     http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT)
 29:  * @link        https://github.com/tecnickcom/tc-lib-pdf-graph
 30:  */
 31: abstract class Style extends \Com\Tecnick\Pdf\Graph\Base
 32: {
 33:     /**
 34:      * Stack containing style data
 35:      *
 36:      * @var array
 37:      */
 38:     protected $style = array();
 39: 
 40:     /**
 41:      * Stack index
 42:      *
 43:      * @var int
 44:      */
 45:     protected $styleid = -1;
 46: 
 47:     /**
 48:      * Array of restore points (style ID)
 49:      *
 50:      * @var array
 51:      */
 52:     protected $stylemark = array(0);
 53: 
 54:     /**
 55:      * Unit of measure conversion ratio
 56:      *
 57:      * @var float
 58:      */
 59:     protected $kunit = 1.0;
 60: 
 61:     /**
 62:      * Map values for lineCap
 63:      *
 64:      * @var array
 65:      */
 66:     protected static $linecapmap = array(0 => 0, 1 => 1, 2 => 2, 'butt' => 0, 'round'=> 1, 'square' => 2);
 67: 
 68:     /**
 69:      * Map values for lineJoin
 70:      *
 71:      * @var array
 72:      */
 73:     protected static $linejoinmap = array(0 => 0, 1 => 1, 2 => 2, 'miter' => 0, 'round' => 1, 'bevel' => 2);
 74: 
 75:     /**
 76:      * Map path paint operators
 77:      *
 78:      * @var array
 79:      */
 80:     protected static $ppopmap = array(
 81:         'S'    => 'S',
 82:         'D'    => 'S',
 83:         's'    => 's',
 84:         'h S'  => 's',
 85:         'd'    => 's',
 86:         'f'    => 'f',
 87:         'F'    => 'f',
 88:         'h f'  => 'h f',
 89:         'f*'   => 'f*',
 90:         'F*'   => 'f*',
 91:         'h f*' => 'h f*',
 92:         'B'    => 'B',
 93:         'FD'   => 'B',
 94:         'DF'   => 'B',
 95:         'B*'   => 'B*',
 96:         'F*D'  => 'B*',
 97:         'DF*'  => 'B*',
 98:         'b'    => 'b',
 99:         'h B'  => 'b',
100:         'fd'   => 'b',
101:         'df'   => 'b',
102:         'b*'   => 'b*',
103:         'h B*' => 'b*',
104:         'f*d'  => 'b*',
105:         'df*'  => 'b*',
106:         'W n'  => 'W n',
107:         'CNZ'  => 'W n',
108:         'W* n' => 'W* n',
109:         'CEO'  => 'W* n',
110:         'h'    => 'h',
111:         'n'    => 'n'
112:     );
113: 
114:     /**
115:      * Array of transparency objects and parameters.
116:      *
117:      * @var array
118:      */
119:     protected $extgstates = array();
120: 
121:     /**
122:      * Initialize default style
123:      */
124:     public function init()
125:     {
126:         $this->style[++$this->styleid] = array(
127:             'lineWidth'  => (1.0 / $this->kunit),  // line thickness in user units
128:             'lineCap'    => 'butt',                // shape of the endpoints for any open path that is stroked
129:             'lineJoin'   => 'miter',               // shape of joints between connected segments of a stroked path
130:             'miterLimit' => (10.0 / $this->kunit), // maximum length of mitered line joins for stroked paths
131:             'dashArray'  => array(),               // lengths of alternating dashes and gaps
132:             'dashPhase'  => 0,                     // distance  at which to start the dash
133:             'lineColor'  => 'black',               // line (drawing) color
134:             'fillColor'  => 'black',               // background (filling) color
135:         );
136:         return $this;
137:     }
138: 
139:     /**
140:      * Add a new style
141:      *
142:      * @param array $style       Style to add.
143:      * @param bool  $inheritlast If true inherit missing values from the last style.
144:      *
145:      * @return string PDF style string
146:      */
147:     public function add(array $style = array(), $inheritlast = false)
148:     {
149:         if ($inheritlast) {
150:             $style = array_merge($this->style[$this->styleid], $style);
151:         }
152:         $this->style[++$this->styleid] = $style;
153:         return $this->getStyle();
154:     }
155: 
156:     /**
157:      * Remove and return last style
158:      *
159:      * @return string PDF style string
160:      */
161:     public function pop()
162:     {
163:         if ($this->styleid <= 0) {
164:             throw new GraphException('The style stack is empty');
165:         }
166:         $style = $this->getStyle();
167:         unset($this->style[$this->styleid]);
168:         --$this->styleid;
169:         return $style;
170:     }
171: 
172:     /**
173:      * Save the current style ID to be restored later
174:      */
175:     public function saveStyleStaus()
176:     {
177:         $this->stylemark[] = $this->styleid;
178:     }
179: 
180:     /**
181:      * Restore the saved style status
182:      */
183:     public function restoreStyleStaus()
184:     {
185:         $this->styleid = array_pop($this->stylemark);
186:         $this->style = array_slice($this->style, 0, ($this->styleid + 1), true);
187:     }
188: 
189:     /**
190:      * Returns the last style array
191:      *
192:      * @return array
193:      */
194:     public function getCurrentStyleArray()
195:     {
196:         return $this->style[$this->styleid];
197:     }
198: 
199:     /**
200:      * Returns the last set value of the specified property
201:      *
202:      * @param string $property Property to search.
203:      * @param mixed  default   Default value to return in case the property is not found.
204:      *
205:      * @return mixed Property value or $default in case the property is not found
206:      */
207:     public function getLastStyleProperty($property, $default = null)
208:     {
209:         for ($idx = $this->styleid; $idx >= 0; --$idx) {
210:             if (isset($this->style[$idx][$property])) {
211:                 return $this->style[$idx][$property];
212:             }
213:         }
214:         return $default;
215:     }
216: 
217:     /**
218:      * Returns the value of th especified item from the last inserted style
219:      *
220:      * @return mixed
221:      */
222:     public function getCurrentStyleItem($item)
223:     {
224:         if (!isset($this->style[$this->styleid][$item])) {
225:             throw new GraphException('The '.$item.' value is not set in the current style');
226:         }
227:         return $this->style[$this->styleid][$item];
228:     }
229:     /**
230:      * Returns the PDF string of the last style added.
231:      *
232:      * @return string
233:      */
234:     public function getStyle()
235:     {
236:         return $this->getStyleCmd($this->style[$this->styleid]);
237:     }
238: 
239:     /**
240:      * Returns the PDF string of the specified style.
241:      *
242:      * @param array $style Style to represent.
243:      *
244:      * @return string
245:      */
246:     public function getStyleCmd(array $style)
247:     {
248:         $out = '';
249:         if (isset($style['lineWidth'])) {
250:             $out .= sprintf('%F w'."\n", ((float) $style['lineWidth'] * $this->kunit));
251:         }
252:         $out .= $this->getLineModeCmd($style);
253:         if (isset($style['lineColor'])) {
254:             $out .= $this->col->getPdfColor($style['lineColor'], true);
255:         }
256:         if (isset($style['fillColor'])) {
257:             $out .= $this->col->getPdfColor($style['fillColor'], false);
258:         }
259:         return $out;
260:     }
261: 
262:     /**
263:      * Returns the PDF string of the specified line style
264:      *
265:      * @param array $style Style to represent.
266:      *
267:      * @return string
268:      */
269:     protected function getLineModeCmd(array $style)
270:     {
271:         $out = '';
272:         if (isset($style['lineCap']) && isset(self::$linecapmap[$style['lineCap']])) {
273:             $out .= self::$linecapmap[$style['lineCap']].' J'."\n";
274:         }
275: 
276:         if (isset($style['lineJoin']) && isset(self::$linejoinmap[$style['lineJoin']])) {
277:             $out .= self::$linejoinmap[$style['lineJoin']].' j'."\n";
278:         }
279: 
280:         if (isset($style['miterLimit'])) {
281:             $out .= sprintf('%F M'."\n", ((float) $style['miterLimit'] * $this->kunit));
282:         }
283: 
284:         if (!empty($style['dashArray'])) {
285:             $dash = array();
286:             foreach ($style['dashArray'] as $val) {
287:                 $dash[] = sprintf('%F', ((float) $val * $this->kunit));
288:             }
289:             $out .= sprintf('[%s] %F d'."\n", implode(' ', $dash), ((float) $style['dashPhase'] * $this->kunit));
290:         }
291:         return $out;
292:     }
293: 
294:     /**
295:      * Get the Path-Painting Operators.
296:      *
297:      * @param string $mode Mode of rendering. Possible values are:
298:      *   - S or D: Stroke the path.
299:      *   - s or d: Close and stroke the path.
300:      *   - f or F: Fill the path, using the nonzero winding number rule to determine the region to fill.
301:      *   - f* or F*: Fill the path, using the even-odd rule to determine the region to fill.
302:      *   - B or FD or DF: Fill and then stroke the path,
303:      *         using the nonzero winding number rule to determine the region to fill.
304:      *   - B* or F*D or DF*: Fill and then stroke the path,
305:      *         using the even-odd rule to determine the region to fill.
306:      *   - b or fd or df: Close, fill, and then stroke the path,
307:      *         using the nonzero winding number rule to determine the region to fill.
308:      *   - b or f*d or df*: Close, fill, and then stroke the path,
309:      *         using the even-odd rule to determine the region to fill.
310:      *   - CNZ: Clipping mode using the even-odd rule to determine which regions lie inside the clipping path.
311:      *   - CEO: Clipping mode using the nonzero winding number rule to determine
312:      *          which regions lie inside the clipping path
313:      *   - n: End the path object without filling or stroking it.
314:      * @param string $default Default style
315:      *
316:      * @return string
317:      */
318:     public function getPathPaintOp($mode, $default = 'S')
319:     {
320:         if (!empty(self::$ppopmap[$mode])) {
321:             return self::$ppopmap[$mode]."\n";
322:         }
323:         if (!empty(self::$ppopmap[$default])) {
324:             return self::$ppopmap[$default]."\n";
325:         }
326:         return '';
327:     }
328: 
329:     /**
330:      * Returns true if the specified path paint operator includes the filling option
331:      *
332:      * @param string $mode Path paint operator (mode of rendering).
333:      *
334:      * @return bool
335:      */
336:     public function isFillingMode($mode)
337:     {
338:         return (!empty(self::$ppopmap[$mode])
339:             && in_array(self::$ppopmap[$mode], array('f', 'f*', 'B', 'B*', 'b', 'b*'))
340:         );
341:     }
342: 
343:     /**
344:      * Returns true if the specified mode includes the stroking option
345:      *
346:      * @param string $mode Path paint operator (mode of rendering).
347:      *
348:      * @return bool
349:      */
350:     public function isStrokingMode($mode)
351:     {
352:         return (!empty(self::$ppopmap[$mode])
353:             && in_array(self::$ppopmap[$mode], array('S', 's', 'B', 'B*', 'b', 'b*'))
354:         );
355:     }
356: 
357:     /**
358:      * Returns true if the specified mode includes "closing the path" option
359:      *
360:      * @param string $mode Path paint operator (mode of rendering).
361:      *
362:      * @return bool
363:      */
364:     public function isClosingMode($mode)
365:     {
366:         return (!empty(self::$ppopmap[$mode])
367:             && in_array(self::$ppopmap[$mode], array('s', 'b', 'b*'))
368:         );
369:     }
370: 
371:     /**
372:      * Remove the Close option from the specified Path paint operator.
373:      *
374:      * @param string $mode Path paint operator (mode of rendering).
375:      *
376:      * @return string
377:      */
378:     public function getModeWithoutClose($mode)
379:     {
380:         $map = array('s' => 'S', 'b' => 'B', 'b*' => 'B*');
381:         if (!empty(self::$ppopmap[$mode]) && isset($map[self::$ppopmap[$mode]])) {
382:             return $map[self::$ppopmap[$mode]];
383:         }
384:         return $mode;
385:     }
386: 
387:     /**
388:      * Remove the Fill option from the specified Path paint operator.
389:      *
390:      * @param string $mode Path paint operator (mode of rendering).
391:      *
392:      * @return string
393:      */
394:     public function getModeWithoutFill($mode)
395:     {
396:         $map = array('f' => '', 'f*' => '', 'B' => 'S', 'B*' => 'S', 'b' => 's', 'b*' => 's');
397:         if (!empty(self::$ppopmap[$mode]) && isset($map[self::$ppopmap[$mode]])) {
398:             return $map[self::$ppopmap[$mode]];
399:         }
400:         return $mode;
401:     }
402: 
403:     /**
404:      * Remove the Stroke option from the specified Path paint operator.
405:      *
406:      * @param string $mode Path paint operator (mode of rendering).
407:      *
408:      * @return string
409:      */
410:     public function getModeWithoutStroke($mode)
411:     {
412:         $map = array('S' => '', 's' => 'h', 'B' => 'f', 'B*' => 'f*', 'b' => 'h f', 'b*' => 'h f*');
413:         if (!empty(self::$ppopmap[$mode]) && isset($map[self::$ppopmap[$mode]])) {
414:             return $map[self::$ppopmap[$mode]];
415:         }
416:         return $mode;
417:     }
418: 
419:     /**
420:      * Add transparency parameters to the current extgstate
421:      *
422:      * @param array $parms parameters
423:      *
424:      * @return string PDF command
425:      */
426:     public function getExtGState($parms)
427:     {
428:         if ($this->pdfa) {
429:             return '';
430:         }
431: 
432:         $gsx = (count($this->extgstates) + 1);
433:         // check if this ExtGState already exist
434:         foreach ($this->extgstates as $idx => $ext) {
435:             if ($ext['parms'] == $parms) {
436:                 $gsx = $idx;
437:                 break;
438:             }
439:         }
440:         if (empty($this->extgstates[$gsx])) {
441:             $this->extgstates[$gsx] = array('parms' => $parms);
442:         }
443:         return '/GS'.$gsx.' gs'."\n";
444:     }
445: }
446: 
 

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