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

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:  * Transform.php
  4:  *
  5:  * @since       2011-05-23
  6:  * @category    Library
  7:  * @package     PdfGraph
  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-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\Transform
 22:  *
 23:  * @since       2011-05-23
 24:  * @category    Library
 25:  * @package     PdfGraph
 26:  * @author      Nicola Asuni <info@tecnick.com>
 27:  * @copyright   2011-2015 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 Transform extends \Com\Tecnick\Pdf\Graph\Style
 32: {
 33:     /**
 34:      * Array (stack) of Current Transformation Matrix (CTM),
 35:      * which maps user space coordinates used within a PDF content stream into output device coordinates.
 36:      *
 37:      * @var array
 38:      */
 39:     protected $ctm = array();
 40: 
 41:     /**
 42:      * Current ID for transformation matrix.
 43:      *
 44:      * @var int
 45:      */
 46:     protected $ctmid = -1;
 47: 
 48:     /**
 49:      * Returns the transformation stack.
 50:      *
 51:      * @return array
 52:      */
 53:     public function getTransformStack()
 54:     {
 55:         return $this->ctm;
 56:     }
 57: 
 58:     /**
 59:      * Returns the transformation stack index.
 60:      *
 61:      * @return int
 62:      */
 63:     public function getTransformIndex()
 64:     {
 65:         return $this->ctmid;
 66:     }
 67: 
 68:     /**
 69:      * Starts a 2D transformation saving current graphic state.
 70:      * This function must be called before calling transformation methods
 71:      *
 72:      * @return string
 73:      */
 74:     public function getStartTransform()
 75:     {
 76:         $this->saveStyleStaus();
 77:         $this->ctm[++$this->ctmid] = array();
 78:         return 'q'."\n";
 79:     }
 80: 
 81:     /**
 82:      * Stops a 2D tranformation restoring previous graphic state.
 83:      * This function must be called after calling transformation methods.
 84:      *
 85:      * @return string
 86:      */
 87:     public function getStopTransform()
 88:     {
 89:         if (!isset($this->ctm[$this->ctmid])) {
 90:             return '';
 91:         }
 92:         unset($this->ctm[$this->ctmid]);
 93:         --$this->ctmid;
 94:         $this->restoreStyleStaus();
 95:         return 'Q'."\n";
 96:     }
 97: 
 98:     /**
 99:      * Get the tranformation matrix (CTM) PDF string
100:      *
101:      * @param array $ctm Transformation matrix array.
102:      *
103:      * @return string
104:      */
105:     public function getTransformation($ctm)
106:     {
107:         $this->ctm[$this->ctmid][] = $ctm;
108:         return sprintf('%F %F %F %F %F %F cm'."\n", $ctm[0], $ctm[1], $ctm[2], $ctm[3], $ctm[4], $ctm[5]);
109:     }
110: 
111:     /**
112:      * Vertical and horizontal non-proportional Scaling.
113:      *
114:      * @param float $skx  Horizontal scaling factor.
115:      * @param float $sky  vertical scaling factor.
116:      * @param float $posx Abscissa of the scaling center.
117:      * @param float $posy Ordinate of the scaling center.
118:      *
119:      * @return string Transformation string
120:      */
121:     public function getScaling($skx, $sky, $posx, $posy)
122:     {
123:         if (($skx == 0) || ($sky == 0)) {
124:             throw new GraphException('Scaling factors must be different than zero');
125:         }
126:         $posy = (($this->pageh - $posy) * $this->kunit);
127:         $posx = ($posx * $this->kunit);
128:         $ctm = array($skx, 0, 0, $sky, ($posx * (1 - $skx)), ($posy * (1 - $sky)));
129:         return $this->getTransformation($ctm);
130:     }
131: 
132:     /**
133:      * Horizontal Scaling.
134:      *
135:      * @param float $skx  Horizontal scaling factor.
136:      * @param float $posx Abscissa of the scaling center.
137:      * @param float $posy Ordinate of the scaling center.
138:      *
139:      * @return string Transformation string
140:      */
141:     public function getHorizScaling($skx, $posx, $posy)
142:     {
143:         return $this->getScaling($skx, 1, $posx, $posy);
144:     }
145: 
146:     /**
147:      * Vertical Scaling.
148:      *
149:      * @param float $sky  vertical scaling factor.
150:      * @param float $posx Abscissa of the scaling center.
151:      * @param float $posy Ordinate of the scaling center.
152:      *
153:      * @return string Transformation string
154:      */
155:     public function getVertScaling($sky, $posx, $posy)
156:     {
157:         return $this->getScaling(1, $sky, $posx, $posy);
158:     }
159: 
160:     /**
161:      * Vertical and horizontal proportional Scaling.
162:      *
163:      * @param float $skf  Scaling factor.
164:      * @param float $posx Abscissa of the scaling center.
165:      * @param float $posy Ordinate of the scaling center.
166:      *
167:      * @return string Transformation string
168:      */
169:     public function getPropScaling($skf, $posx, $posy)
170:     {
171:         return $this->getScaling($skf, $skf, $posx, $posy);
172:     }
173: 
174:     /**
175:      * Rotation.
176:      *
177:      * @param float $angle Angle in degrees for counter-clockwise rotation.
178:      * @param float $posx Abscissa of the rotation center.
179:      * @param float $posy Ordinate of the rotation center.
180:      *
181:      * @return string Transformation string
182:      */
183:     public function getRotation($angle, $posx, $posy)
184:     {
185:         $posy = (($this->pageh - $posy) * $this->kunit);
186:         $posx = ($posx * $this->kunit);
187:         $ctm = array();
188:         $ctm[0] = cos($this->degToRad($angle));
189:         $ctm[1] = sin($this->degToRad($angle));
190:         $ctm[2] = -$ctm[1];
191:         $ctm[3] = $ctm[0];
192:         $ctm[4] = ($posx + ($ctm[1] * $posy) - ($ctm[0] * $posx));
193:         $ctm[5] = ($posy - ($ctm[0] * $posy) - ($ctm[1] * $posx));
194:         return $this->getTransformation($ctm);
195:     }
196: 
197:     /**
198:      * Horizontal Mirroring.
199:      *
200:      * @param float $posx Abscissa of the mirroring line.
201:      *
202:      * @return string Transformation string
203:      */
204:     public function getHorizMirroring($posx)
205:     {
206:         return $this->getScaling(-1, 1, $posx, 0);
207:     }
208: 
209:     /**
210:      * Verical Mirroring.
211:      *
212:      * @param float $posy Ordinate of the mirroring line.
213:      *
214:      * @return string Transformation string
215:      */
216:     public function getVertMirroring($posy)
217:     {
218:         return $this->getScaling(1, -1, 0, $posy);
219:     }
220: 
221:     /**
222:      * Point reflection mirroring.
223:      *
224:      * @param float $posx Abscissa of the mirroring point.
225:      * @param float $posy Ordinate of the mirroring point.
226:      *
227:      * @return string Transformation string
228:      */
229:     public function getPointMirroring($posx, $posy)
230:     {
231:         return $this->getScaling(-1, -1, $posx, $posy);
232:     }
233: 
234:     /**
235:      * Reflection against a straight line through point (x, y) with the gradient angle (angle).
236:      *
237:      * @param float $angle Gradient angle in degrees of the straight line.
238:      * @param float $posx  Abscissa of the mirroring point.
239:      * @param float $posy  Ordinate of the mirroring point.
240:      *
241:      * @return string Transformation string
242:      */
243:     public function getReflection($ang, $posx, $posy)
244:     {
245:         return $this->getScaling(-1, 1, $posx, $posy).$this->getRotation((-2 * ($ang - 90)), $posx, $posy);
246:     }
247: 
248:     /**
249:      * Translate graphic object horizontally and vertically.
250:      *
251:      * @param float $trx Movement to the right.
252:      * @param float $try Movement to the bottom.
253:      *
254:      * @return string Transformation string
255:      */
256:     public function getTranslation($trx, $try)
257:     {
258:         //calculate elements of transformation matrix
259:         $ctm = array(1, 0, 0, 1, ($trx * $this->kunit), (-$try * $this->kunit));
260:         return $this->getTransformation($ctm);
261:     }
262: 
263:     /**
264:      * Translate graphic object horizontally.
265:      *
266:      * @param float $trx Movement to the right.
267:      *
268:      * @return string Transformation string
269:      */
270:     public function getHorizTranslation($trx)
271:     {
272:         return $this->getTranslation($trx, 0);
273:     }
274: 
275:     /**
276:      * Translate graphic object vertically.
277:      *
278:      * @param float $try Movement to the bottom.
279:      *
280:      * @return string Transformation string
281:      */
282:     public function getVertTranslation($try)
283:     {
284:         return $this->getTranslation(0, $try);
285:     }
286: 
287:     /**
288:      * Skew.
289:      *
290:      * @param float $angx Angle in degrees between -90 (skew to the left) and 90 (skew to the right)
291:      * @param float $angy Angle in degrees between -90 (skew to the bottom) and 90 (skew to the top)
292:      * @param float $posx Abscissa of the skewing center.
293:      * @param float $posy Ordinate of the skewing center.
294:      *
295:      * @return string Transformation string
296:      */
297:     public function getSkewing($angx, $angy, $posx, $posy)
298:     {
299:         if (($angx <= -90) || ($angx >= 90) || ($angy <= -90) || ($angy >= 90)) {
300:             throw new GraphException('Angle values must be beweeen -90 and +90 degrees.');
301:         }
302:         $posy = (($this->pageh - $posy) * $this->kunit);
303:         $posx = ($posx * $this->kunit);
304:         $ctm = array();
305:         $ctm[0] = 1;
306:         $ctm[1] = tan($this->degToRad($angy));
307:         $ctm[2] = tan($this->degToRad($angx));
308:         $ctm[3] = 1;
309:         $ctm[4] = (-$ctm[2] * $posy);
310:         $ctm[5] = (-$ctm[1] * $posx);
311:         return $this->getTransformation($ctm);
312:     }
313: 
314:     /**
315:      * Skew horizontally.
316:      *
317:      * @param float $angx Angle in degrees between -90 (skew to the left) and 90 (skew to the right)
318:      * @param float $posx Abscissa of the skewing center.
319:      * @param float $posy Ordinate of the skewing center.
320:      *
321:      * @return string Transformation string
322:      */
323:     public function getHorizSkewing($angx, $posx, $posy)
324:     {
325:         return $this->getSkewing($angx, 0, $posx, $posy);
326:     }
327: 
328:     /**
329:      * Skew vertically.
330:      *
331:      * @param float $angy Angle in degrees between -90 (skew to the bottom) and 90 (skew to the top)
332:      * @param float $posx Abscissa of the skewing center.
333:      * @param float $posy Ordinate of the skewing center.
334:      *
335:      * @return string Transformation string
336:      */
337:     public function getVertSkewing($angy, $posx, $posy)
338:     {
339:         return $this->getSkewing(0, $angy, $posx, $posy);
340:     }
341: 
342:     /**
343:      * Get the product of two Tranformation Matrix.
344:      *
345:      * @param array $tma First  Tranformation Matrix.
346:      * @param array $tmb Second Tranformation Matrix.
347:      *
348:      * @return array CTM Transformation Matrix.
349:      */
350:     public function getCtmProduct($tma, $tmb)
351:     {
352:         return array(
353:             (((float) $tma[0] * (float) $tmb[0]) + ((float) $tma[2] * (float) $tmb[1])),
354:             (((float) $tma[1] * (float) $tmb[0]) + ((float) $tma[3] * (float) $tmb[1])),
355:             (((float) $tma[0] * (float) $tmb[2]) + ((float) $tma[2] * (float) $tmb[3])),
356:             (((float) $tma[1] * (float) $tmb[2]) + ((float) $tma[3] * (float) $tmb[3])),
357:             (((float) $tma[0] * (float) $tmb[4]) + ((float) $tma[2] * (float) $tmb[5]) + (float) $tma[4]),
358:             (((float) $tma[1] * (float) $tmb[4]) + ((float) $tma[3] * (float) $tmb[5]) + (float) $tma[5])
359:         );
360:     }
361: }
362: 
 

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