1<?php 2 3/** 4 * This file is part of FPDI 5 * 6 * @package setasign\Fpdi 7 * @copyright Copyright (c) 2024 Setasign GmbH & Co. KG (https://www.setasign.com) 8 * @license http://opensource.org/licenses/mit-license The MIT License 9 */ 10 11namespace setasign\Fpdi; 12 13use setasign\Fpdi\Math\Matrix; 14use setasign\Fpdi\Math\Vector; 15 16/** 17 * A simple graphic state class which holds the current transformation matrix. 18 */ 19class GraphicsState 20{ 21 /** 22 * @var Matrix 23 */ 24 protected $ctm; 25 26 /** 27 * @param Matrix|null $ctm 28 */ 29 public function __construct(?Matrix $ctm = null) 30 { 31 if ($ctm === null) { 32 $ctm = new Matrix(); 33 } 34 35 $this->ctm = $ctm; 36 } 37 38 /** 39 * @param Matrix $matrix 40 * @return $this 41 */ 42 public function add(Matrix $matrix) 43 { 44 $this->ctm = $matrix->multiply($this->ctm); 45 return $this; 46 } 47 48 /** 49 * @param int|float $x 50 * @param int|float $y 51 * @param int|float $angle 52 * @return $this 53 */ 54 public function rotate($x, $y, $angle) 55 { 56 if (abs($angle) < 1e-5) { 57 return $this; 58 } 59 60 $angle = deg2rad($angle); 61 $c = cos($angle); 62 $s = sin($angle); 63 64 $this->add(new Matrix($c, $s, -$s, $c, $x, $y)); 65 66 return $this->translate(-$x, -$y); 67 } 68 69 /** 70 * @param int|float $shiftX 71 * @param int|float $shiftY 72 * @return $this 73 */ 74 public function translate($shiftX, $shiftY) 75 { 76 return $this->add(new Matrix(1, 0, 0, 1, $shiftX, $shiftY)); 77 } 78 79 /** 80 * @param int|float $scaleX 81 * @param int|float $scaleY 82 * @return $this 83 */ 84 public function scale($scaleX, $scaleY) 85 { 86 return $this->add(new Matrix($scaleX, 0, 0, $scaleY, 0, 0)); 87 } 88 89 /** 90 * @param Vector $vector 91 * @return Vector 92 */ 93 public function toUserSpace(Vector $vector) 94 { 95 return $vector->multiplyWithMatrix($this->ctm); 96 } 97} 98