source-class-Com.Tecnick.Pdf.Page.Settings

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:  * Settings.php
  4:  *
  5:  * @since       2011-05-23
  6:  * @category    Library
  7:  * @package     PdfPage
  8:  * @author      Nicola Asuni <info@tecnick.com>
  9:  * @copyright   2011-2015 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-page
 12:  *
 13:  * This file is part of tc-lib-pdf-page software library.
 14:  */
 15: 
 16: namespace Com\Tecnick\Pdf\Page;
 17: 
 18: use \Com\Tecnick\Color\Pdf as Color;
 19: use \Com\Tecnick\Pdf\Page\Exception as PageException;
 20: 
 21: /**
 22:  * Com\Tecnick\Pdf\Page\Settings
 23:  *
 24:  * @since       2011-05-23
 25:  * @category    Library
 26:  * @package     PdfPage
 27:  * @author      Nicola Asuni <info@tecnick.com>
 28:  * @copyright   2011-2015 Nicola Asuni - Tecnick.com LTD
 29:  * @license     http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT)
 30:  * @link        https://github.com/tecnickcom/tc-lib-pdf-page
 31:  *
 32:  * @SuppressWarnings(PHPMD)
 33:  */
 34: abstract class Settings extends \Com\Tecnick\Pdf\Page\Box
 35: {
 36:     /**
 37:      * Epsilon precision used to compare floating point values
 38:      */
 39:     const EPS = 0.0001;
 40: 
 41:     /**
 42:      * Sanitize or set the page modification time.
 43:      *
 44:      * @param array $data Page data
 45:      */
 46:     public function sanitizePageNumber(array &$data)
 47:     {
 48:         if (!empty($data['num'])) {
 49:             $data['num'] = max(0, intval($data['num']));
 50:         }
 51:     }
 52: 
 53:     /**
 54:      * Sanitize or set the page modification time.
 55:      *
 56:      * @param array $data Page data
 57:      */
 58:     public function sanitizeTime(array &$data)
 59:     {
 60:         if (empty($data['time'])) {
 61:             $data['time'] = time();
 62:         } else {
 63:             $data['time'] = max(0, intval($data['time']));
 64:         }
 65:     }
 66: 
 67:     /**
 68:      * Sanitize or set the page group
 69:      *
 70:      * @param array $data Page data
 71:      */
 72:     public function sanitizeGroup(array &$data)
 73:     {
 74:         if (empty($data['group'])) {
 75:             $data['group'] = 0;
 76:         } else {
 77:             $data['group'] = max(0, intval($data['group']));
 78:         }
 79:     }
 80: 
 81:     /**
 82:      * Sanitize or set the page content.
 83:      *
 84:      * @param array $data Page data
 85:      */
 86:     public function sanitizeContent(array &$data)
 87:     {
 88:         if (empty($data['content'])) {
 89:             $data['content'] = array('');
 90:         } else {
 91:             $data['content'] = array((string)$data['content']);
 92:         }
 93:     }
 94: 
 95:     /**
 96:      * Sanitize or set the annotation references
 97:      *
 98:      * @param array $data Page data
 99:      */
100:     public function sanitizeAnnotRefs(array &$data)
101:     {
102:         if (empty($data['annotrefs'])) {
103:             $data['annotrefs'] = array();
104:         }
105:     }
106: 
107:     /**
108:      * Sanitize or set the page rotation.
109:      * The number of degrees by which the page shall be rotated clockwise when displayed or printed.
110:      * The value shall be a multiple of 90.
111:      *
112:      * @param array $data Page data
113:      */
114:     public function sanitizeRotation(array &$data)
115:     {
116:         if (empty($data['rotation']) || (($data['rotation'] % 90) != 0)) {
117:             $data['rotation'] = 0;
118:         } else {
119:             $data['rotation'] = intval($data['rotation']);
120:         }
121:     }
122: 
123:     /**
124:      * Sanitize or set the page preferred zoom (magnification) factor.
125:      *
126:      * @param array $data Page data
127:      */
128:     public function sanitizeZoom(array &$data)
129:     {
130:         if (empty($data['zoom'])) {
131:             $data['zoom'] = 1;
132:         } else {
133:             $data['zoom'] = floatval($data['zoom']);
134:         }
135:     }
136: 
137:     /**
138:      * Sanitize or set the page transitions.
139:      *
140:      * @param array $data Page data
141:      */
142:     public function sanitizeTransitions(array &$data)
143:     {
144:         if (empty($data['transition'])) {
145:             return;
146:         }
147:         // display duration before advancing page
148:         if (empty($data['transition']['Dur'])) {
149:             unset($data['transition']['Dur']);
150:         } else {
151:             $data['transition']['Dur'] = max(0, floatval($data['transition']['Dur']));
152:         }
153:         // transition style
154:         $styles = array(
155:             'Split',
156:             'Blinds',
157:             'Box',
158:             'Wipe',
159:             'Dissolve',
160:             'Glitter',
161:             'R',
162:             'Fly',
163:             'Push',
164:             'Cover',
165:             'Uncover',
166:             'Fade'
167:         );
168:         if (empty($data['transition']['S']) || !in_array($data['transition']['S'], $styles)) {
169:             $data['transition']['S'] = 'R';
170:         }
171:         // duration of the transition effect, in seconds
172:         if (!isset($data['transition']['D'])) {
173:             $data['transition']['D'] = 1;
174:         } else {
175:             $data['transition']['D'] = intval($data['transition']['D']);
176:         }
177:         // dimension in which the specified transition effect shall occur
178:         if (empty($data['transition']['Dm'])
179:             || !in_array($data['transition']['S'], array('Split', 'Blinds'))
180:             || !in_array($data['transition']['Dm'], array('H', 'V'))
181:         ) {
182:             unset($data['transition']['Dm']);
183:         }
184:         // direction of motion for the specified transition effect
185:         if (empty($data['transition']['M'])
186:             || !in_array($data['transition']['S'], array('Split', 'Box', 'Fly'))
187:             || !in_array($data['transition']['M'], array('I', 'O'))
188:         ) {
189:             unset($data['transition']['M']);
190:         }
191:         // direction in which the specified transition effect shall moves
192:         if (empty($data['transition']['Di'])
193:             || !in_array($data['transition']['S'], array('Wipe', 'Glitter', 'Fly', 'Cover', 'Uncover', 'Push'))
194:             || !in_array($data['transition']['Di'], array('None', 0, 90, 180, 270, 315))
195:             || (in_array($data['transition']['Di'], array(90, 180)) && ($data['transition']['S'] != 'Wipe'))
196:             || (($data['transition']['Di'] == 315) && ($data['transition']['S'] != 'Glitter'))
197:             || (($data['transition']['Di'] == 'None') && ($data['transition']['S'] != 'Fly'))
198:         ) {
199:             unset($data['transition']['Di']);
200:         }
201:         // starting or ending scale at which the changes shall be drawn
202:         if (isset($data['transition']['SS'])) {
203:             $data['transition']['SS'] = floatval($data['transition']['SS']);
204:         }
205:         // If true, the area that shall be flown in is rectangular and opaque
206:         if (empty($data['transition']['B'])) {
207:             $data['transition']['B'] = false;
208:         } else {
209:             $data['transition']['B'] = true;
210:         }
211:     }
212: 
213:     /**
214:      * Sanitize or set the page margins
215:      *
216:      * @param array $data Page data
217:      */
218:     public function sanitizeMargins(array &$data)
219:     {
220:         if (empty($data['margin'])) {
221:             $data['margin'] = array();
222:             if (empty($data['width']) || empty($data['height'])) {
223:                 list($data['width'], $data['height'], $data['orientation']) = $this->getPageFormatSize('A4', 'P');
224:                 $data['width'] /= $this->kunit;
225:                 $data['height'] /= $this->kunit;
226:             }
227:         }
228:         $margins = array(
229:             'PL' => $data['width'],
230:             'PR' => $data['width'],
231:             'PT' => $data['height'],
232:             'HB' => $data['height'],
233:             'CT' => $data['height'],
234:             'CB' => $data['height'],
235:             'FT' => $data['height'],
236:             'PB' => $data['height']
237:         );
238:         foreach ($margins as $type => $max) {
239:             if (empty($data['margin'][$type])) {
240:                 $data['margin'][$type] = 0;
241:             } else {
242:                 $data['margin'][$type] = min(max(0, floatval($data['margin'][$type])), $max);
243:             }
244:         }
245:         $data['margin']['PR'] = min($data['margin']['PR'], ($data['width'] - $data['margin']['PL']));
246:         $data['margin']['HB'] = max($data['margin']['HB'], $data['margin']['PT']);
247:         $data['margin']['CT'] = max($data['margin']['CT'], $data['margin']['HB']);
248:         $data['margin']['CB'] = min($data['margin']['CB'], ($data['height'] - $data['margin']['CT']));
249:         $data['margin']['FT'] = min($data['margin']['FT'], $data['margin']['CB']);
250:         $data['margin']['PB'] = min($data['margin']['PB'], $data['margin']['FT']);
251: 
252:         $data['ContentWidth'] = ($data['width'] - $data['margin']['PL'] - $data['margin']['PR']);
253:         $data['ContentHeight'] = ($data['height'] - $data['margin']['CT'] - $data['margin']['CB']);
254:         $data['HeaderHeight'] = ($data['margin']['HB'] - $data['margin']['PT']);
255:         $data['FooterHeight'] = ($data['margin']['FT'] - $data['margin']['PB']);
256:     }
257: 
258:     /**
259:      * Sanitize or set the page regions (columns)
260:      *
261:      * @param array $data Page data
262:      */
263:     public function sanitizeRegions(array &$data)
264:     {
265:         if (!empty($data['columns'])) {
266:             // set eaual columns
267:             $data['region'] = array();
268:             $width = ($data['ContentWidth'] / $data['columns']);
269:             for ($idx = 0; $idx < $data['columns']; ++$idx) {
270:                 $data['region'][] = array(
271:                     'RX'  => ($data['margin']['PL'] + ($idx * $width)),
272:                     'RY'  => $data['margin']['CT'],
273:                     'RW'  => $width,
274:                     'RH'  => $data['ContentHeight'],
275:                 );
276:             }
277:         }
278:         if (empty($data['region'])) {
279:             // default single region
280:             $data['region'] = array(
281:                 array(
282:                     'RX'  => $data['margin']['PL'],
283:                     'RY'  => $data['margin']['CT'],
284:                     'RW'  => $data['ContentWidth'],
285:                     'RH'  => $data['ContentHeight'],
286:                 )
287:             );
288:         }
289:         $data['columns'] = 0; // count the number of regions
290:         foreach ($data['region'] as $key => $val) {
291:             // region width
292:             $data['region'][$key]['RW'] = min(max(0, floatval($val['RW'])), $data['ContentWidth']);
293:             // horizontal coordinate of the top-left corner
294:             $data['region'][$key]['RX'] = min(
295:                 max(0, floatval($val['RX'])),
296:                 ($data['width'] - $data['margin']['PR'] - $val['RW'])
297:             );
298:             // distance of the region right side from the left page edge
299:             $data['region'][$key]['RL'] = ($val['RX'] + $val['RW']);
300:             // distance of the region right side from the right page edge
301:             $data['region'][$key]['RR'] = ($data['width'] - $val['RX'] - $val['RW']);
302:             // region height
303:             $data['region'][$key]['RH'] = min(max(0, floatval($val['RH'])), $data['ContentHeight']);
304:             // vertical coordinate of the top-left corner
305:             $data['region'][$key]['RY'] = min(
306:                 max(0, floatval($val['RY'])),
307:                 ($data['height'] - $data['margin']['CB'] - $val['RH'])
308:             );
309:             // distance of the region bottom side from the top page edge
310:             $data['region'][$key]['RT'] = ($val['RY'] + $val['RH']);
311:             // distance of the region bottom side from the bottom page edge
312:             $data['region'][$key]['RB'] = ($data['height'] - $val['RY'] - $val['RH']);
313: 
314:             // initialize cursor position inside the region
315:             $data['region'][$key]['x'] = $data['region'][$key]['RX'];
316:             $data['region'][$key]['y'] = $data['region'][$key]['RY'];
317: 
318:             ++$data['columns'];
319:         }
320: 
321:         if (!isset($data['autobreak'])) {
322:             $data['autobreak'] = true;
323:         }
324:     }
325: 
326:     /**
327:      * Sanitize or set the page boxes containing the page boundaries.
328:      *
329:      * @param array $data Page data
330:      */
331:     public function sanitizeBoxData(array &$data)
332:     {
333:         if (empty($data['box'])) {
334:             if (empty($data['pwidth']) || empty($data['pheight'])) {
335:                 list($data['pwidth'], $data['pheight'], $data['orientation']) = $this->getPageFormatSize('A4', 'P');
336:             }
337:             $data['box'] = $this->setPageBoxes($data['pwidth'], $data['pheight']);
338:         } else {
339:             if (!empty($data['format']) && ($data['format'] == 'MediaBox')) {
340:                 $data['format'] = '';
341:                 $data['width'] = abs($data['box']['MediaBox']['urx'] - $data['box']['MediaBox']['llx']) / $this->kunit;
342:                 $data['height'] = abs($data['box']['MediaBox']['ury'] - $data['box']['MediaBox']['lly']) / $this->kunit;
343:                 $this->sanitizePageFormat($data);
344:             }
345:             if (empty($data['box']['MediaBox'])) {
346:                 $data['box'] = $this->setBox($data['box'], 'MediaBox', 0, 0, $data['pwidth'], $data['pheight']);
347:             }
348:             if (empty($data['box']['CropBox'])) {
349:                 $data['box'] = $this->setBox(
350:                     $data['box'],
351:                     'CropBox',
352:                     $data['box']['MediaBox']['llx'],
353:                     $data['box']['MediaBox']['lly'],
354:                     $data['box']['MediaBox']['urx'],
355:                     $data['box']['MediaBox']['ury']
356:                 );
357:             }
358:             if (empty($data['box']['BleedBox'])) {
359:                 $data['box'] = $this->setBox(
360:                     $data['box'],
361:                     'BleedBox',
362:                     $data['box']['CropBox']['llx'],
363:                     $data['box']['CropBox']['lly'],
364:                     $data['box']['CropBox']['urx'],
365:                     $data['box']['CropBox']['ury']
366:                 );
367:             }
368:             if (empty($data['box']['TrimBox'])) {
369:                 $data['box'] = $this->setBox(
370:                     $data['box'],
371:                     'TrimBox',
372:                     $data['box']['CropBox']['llx'],
373:                     $data['box']['CropBox']['lly'],
374:                     $data['box']['CropBox']['urx'],
375:                     $data['box']['CropBox']['ury']
376:                 );
377:             }
378:             if (empty($data['box']['ArtBox'])) {
379:                 $data['box'] = $this->setBox(
380:                     $data['box'],
381:                     'ArtBox',
382:                     $data['box']['CropBox']['llx'],
383:                     $data['box']['CropBox']['lly'],
384:                     $data['box']['CropBox']['urx'],
385:                     $data['box']['CropBox']['ury']
386:                 );
387:             }
388:         }
389:         $orientation = $this->getPageOrientation(
390:             abs($data['box']['MediaBox']['urx'] - $data['box']['MediaBox']['llx']),
391:             abs($data['box']['MediaBox']['ury'] - $data['box']['MediaBox']['lly'])
392:         );
393:         if (empty($data['orientation'])) {
394:             $data['orientation'] = $orientation;
395:         } elseif ($data['orientation'] != $orientation) {
396:             $data['box'] = $this->swapCoordinates($data['box']);
397:         }
398:     }
399: 
400:     /**
401:      * Sanitize or set the page format
402:      *
403:      * @param array $data Page data
404:      */
405:     public function sanitizePageFormat(array &$data)
406:     {
407:         if (empty($data['orientation'])) {
408:             $data['orientation'] = '';
409:         }
410:         if (!empty($data['format'])) {
411:             list($data['pwidth'], $data['pheight'], $data['orientation']) = $this->getPageFormatSize(
412:                 $data['format'],
413:                 $data['orientation']
414:             );
415:             $data['width'] = ($data['pwidth'] / $this->kunit);
416:             $data['height'] = ($data['pheight'] / $this->kunit);
417:         } else {
418:             $data['format'] = 'CUSTOM';
419:             if (empty($data['width']) || empty($data['height'])) {
420:                 if (empty($data['box']['MediaBox'])) {
421:                     // default page format
422:                     $data['format'] = 'A4';
423:                     $data['orientation'] = 'P';
424:                     return $this->sanitizePageFormat($data);
425:                 }
426:                 $data['format'] = 'MediaBox';
427:                 return;
428:             } else {
429:                 list($data['width'], $data['height'], $data['orientation']) = $this->getPageOrientedSize(
430:                     $data['width'],
431:                     $data['height'],
432:                     $data['orientation']
433:                 );
434:             }
435:         }
436:         // convert values in points
437:         $data['pwidth'] = ($data['width'] * $this->kunit);
438:         $data['pheight'] = ($data['height'] * $this->kunit);
439:     }
440: }
441: 
 

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