PageRenderTime 47ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/branches/wi16-graphs/Classes/PHPExcel/Worksheet/BaseDrawing.php

#
PHP | 504 lines | 181 code | 53 blank | 270 comment | 22 complexity | cff15bbd2c3368a342842080ae977340 MD5 | raw file
Possible License(s): AGPL-1.0, LGPL-2.0, LGPL-2.1, GPL-3.0, LGPL-3.0
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2009 PHPExcel
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * @category PHPExcel
  22. * @package PHPExcel_Worksheet
  23. * @copyright Copyright (c) 2006 - 2009 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version ##VERSION##, ##DATE##
  26. */
  27. /** PHPExcel_IComparable */
  28. require_once 'PHPExcel/IComparable.php';
  29. /** PHPExcel_Worksheet */
  30. require_once 'PHPExcel/Worksheet.php';
  31. /** PHPExcel_Worksheet_Drawing_Shadow */
  32. require_once 'PHPExcel/Worksheet/Drawing/Shadow.php';
  33. /**
  34. * PHPExcel_Worksheet_BaseDrawing
  35. *
  36. * @category PHPExcel
  37. * @package PHPExcel_Worksheet
  38. * @copyright Copyright (c) 2006 - 2009 PHPExcel (http://www.codeplex.com/PHPExcel)
  39. */
  40. class PHPExcel_Worksheet_BaseDrawing implements PHPExcel_IComparable
  41. {
  42. /**
  43. * Image counter
  44. *
  45. * @var int
  46. */
  47. private static $_imageCounter = 0;
  48. /**
  49. * Image index
  50. *
  51. * @var int
  52. */
  53. private $_imageIndex = 0;
  54. /**
  55. * Name
  56. *
  57. * @var string
  58. */
  59. protected $_name;
  60. /**
  61. * Description
  62. *
  63. * @var string
  64. */
  65. protected $_description;
  66. /**
  67. * Worksheet
  68. *
  69. * @var PHPExcel_Worksheet
  70. */
  71. protected $_worksheet;
  72. /**
  73. * Coordinates
  74. *
  75. * @var string
  76. */
  77. protected $_coordinates;
  78. /**
  79. * Offset X
  80. *
  81. * @var int
  82. */
  83. protected $_offsetX;
  84. /**
  85. * Offset Y
  86. *
  87. * @var int
  88. */
  89. protected $_offsetY;
  90. /**
  91. * Width
  92. *
  93. * @var int
  94. */
  95. protected $_width;
  96. /**
  97. * Height
  98. *
  99. * @var int
  100. */
  101. protected $_height;
  102. /**
  103. * Proportional resize
  104. *
  105. * @var boolean
  106. */
  107. protected $_resizeProportional;
  108. /**
  109. * Rotation
  110. *
  111. * @var int
  112. */
  113. protected $_rotation;
  114. /**
  115. * Shadow
  116. *
  117. * @var PHPExcel_Worksheet_Drawing_Shadow
  118. */
  119. protected $_shadow;
  120. /**
  121. * Create a new PHPExcel_Worksheet_BaseDrawing
  122. */
  123. public function __construct()
  124. {
  125. // Initialise values
  126. $this->_name = '';
  127. $this->_description = '';
  128. $this->_worksheet = null;
  129. $this->_coordinates = 'A1';
  130. $this->_offsetX = 0;
  131. $this->_offsetY = 0;
  132. $this->_width = 0;
  133. $this->_height = 0;
  134. $this->_resizeProportional = true;
  135. $this->_rotation = 0;
  136. $this->_shadow = new PHPExcel_Worksheet_Drawing_Shadow();
  137. // Set image index
  138. self::$_imageCounter++;
  139. $this->_imageIndex = self::$_imageCounter;
  140. }
  141. /**
  142. * Destructor
  143. */
  144. public function __destruct()
  145. {
  146. $this->_worksheet = null;
  147. unset($this->_worksheet);
  148. }
  149. /**
  150. * Get image index
  151. *
  152. * @return int
  153. */
  154. public function getImageIndex() {
  155. return $this->_imageIndex;
  156. }
  157. /**
  158. * Get Name
  159. *
  160. * @return string
  161. */
  162. public function getName() {
  163. return $this->_name;
  164. }
  165. /**
  166. * Set Name
  167. *
  168. * @param string $pValue
  169. */
  170. public function setName($pValue = '') {
  171. $this->_name = $pValue;
  172. }
  173. /**
  174. * Get Description
  175. *
  176. * @return string
  177. */
  178. public function getDescription() {
  179. return $this->_description;
  180. }
  181. /**
  182. * Set Description
  183. *
  184. * @param string $pValue
  185. */
  186. public function setDescription($pValue = '') {
  187. $this->_description = $pValue;
  188. }
  189. /**
  190. * Get Worksheet
  191. *
  192. * @return PHPExcel_Worksheet
  193. */
  194. public function getWorksheet() {
  195. return $this->_worksheet;
  196. }
  197. /**
  198. * Set Worksheet
  199. *
  200. * @param PHPExcel_Worksheet $pValue
  201. * @param bool $pOverrideOld If a Worksheet has already been assigned, overwrite it and remove image from old Worksheet?
  202. * @throws Exception
  203. */
  204. public function setWorksheet(PHPExcel_Worksheet $pValue = null, $pOverrideOld = false) {
  205. if (is_null($this->_worksheet)) {
  206. // Add drawing to PHPExcel_Worksheet
  207. $this->_worksheet = $pValue;
  208. $this->_worksheet->getCell($this->_coordinates);
  209. $this->_worksheet->getDrawingCollection()->append($this);
  210. } else {
  211. if ($pOverrideOld) {
  212. // Remove drawing from old PHPExcel_Worksheet
  213. $iterator = $this->_worksheet->getDrawingCollection()->getIterator();
  214. while ($iterator->valid()) {
  215. if ($iterator->current()->getHashCode() == $this->getHashCode()) {
  216. $this->_worksheet->getDrawingCollection()->offsetUnset( $iterator->key() );
  217. $this->_worksheet = null;
  218. break;
  219. }
  220. }
  221. // Set new PHPExcel_Worksheet
  222. $this->setWorksheet($pValue);
  223. } else {
  224. throw new Exception("A PHPExcel_Worksheet has already been assigned. Drawings can only exist on one PHPExcel_Worksheet.");
  225. }
  226. }
  227. }
  228. /**
  229. * Get Coordinates
  230. *
  231. * @return string
  232. */
  233. public function getCoordinates() {
  234. return $this->_coordinates;
  235. }
  236. /**
  237. * Set Coordinates
  238. *
  239. * @param string $pValue
  240. */
  241. public function setCoordinates($pValue = 'A1') {
  242. $this->_coordinates = $pValue;
  243. }
  244. /**
  245. * Get OffsetX
  246. *
  247. * @return int
  248. */
  249. public function getOffsetX() {
  250. return $this->_offsetX;
  251. }
  252. /**
  253. * Set OffsetX
  254. *
  255. * @param int $pValue
  256. */
  257. public function setOffsetX($pValue = 0) {
  258. $this->_offsetX = $pValue;
  259. }
  260. /**
  261. * Get OffsetY
  262. *
  263. * @return int
  264. */
  265. public function getOffsetY() {
  266. return $this->_offsetY;
  267. }
  268. /**
  269. * Set OffsetY
  270. *
  271. * @param int $pValue
  272. */
  273. public function setOffsetY($pValue = 0) {
  274. $this->_offsetY = $pValue;
  275. }
  276. /**
  277. * Get Width
  278. *
  279. * @return int
  280. */
  281. public function getWidth() {
  282. return $this->_width;
  283. }
  284. /**
  285. * Set Width
  286. *
  287. * @param int $pValue
  288. */
  289. public function setWidth($pValue = 0) {
  290. // Resize proportional?
  291. if ($this->_resizeProportional && $pValue != 0) {
  292. $ratio = $this->_height / $this->_width;
  293. $this->_height = round($ratio * $pValue);
  294. }
  295. // Set width
  296. $this->_width = $pValue;
  297. }
  298. /**
  299. * Get Height
  300. *
  301. * @return int
  302. */
  303. public function getHeight() {
  304. return $this->_height;
  305. }
  306. /**
  307. * Set Height
  308. *
  309. * @param int $pValue
  310. */
  311. public function setHeight($pValue = 0) {
  312. // Resize proportional?
  313. if ($this->_resizeProportional && $pValue != 0) {
  314. $ratio = $this->_width / $this->_height;
  315. $this->_width = round($ratio * $pValue);
  316. }
  317. // Set height
  318. $this->_height = $pValue;
  319. }
  320. /**
  321. * Set width and height with proportional resize
  322. * @author Vincent@luo MSN:kele_100@hotmail.com
  323. * @param int $width
  324. * @param int $height
  325. * @example $objDrawing->setResizeProportional(true);
  326. * @example $objDrawing->setWidthAndHeight(160,120);
  327. */
  328. public function setWidthAndHeight($width = 0, $height = 0) {
  329. $xratio = $width / $this->_width;
  330. $yratio = $height / $this->_height;
  331. if ($this->_resizeProportional && !($width == 0 || $height == 0)) {
  332. if (($xratio * $this->_height) < $height) {
  333. $this->_height = ceil($xratio * $this->_height);
  334. $this->_width = $width;
  335. } else {
  336. $this->_width = ceil($yratio * $this->_width);
  337. $this->_height = $height;
  338. }
  339. }
  340. }
  341. /**
  342. * Get ResizeProportional
  343. *
  344. * @return boolean
  345. */
  346. public function getResizeProportional() {
  347. return $this->_resizeProportional;
  348. }
  349. /**
  350. * Set ResizeProportional
  351. *
  352. * @param boolean $pValue
  353. */
  354. public function setResizeProportional($pValue = true) {
  355. $this->_resizeProportional = $pValue;
  356. }
  357. /**
  358. * Get Rotation
  359. *
  360. * @return int
  361. */
  362. public function getRotation() {
  363. return $this->_rotation;
  364. }
  365. /**
  366. * Set Rotation
  367. *
  368. * @param int $pValue
  369. */
  370. public function setRotation($pValue = 0) {
  371. $this->_rotation = $pValue;
  372. }
  373. /**
  374. * Get Shadow
  375. *
  376. * @return PHPExcel_Worksheet_Drawing_Shadow
  377. */
  378. public function getShadow() {
  379. return $this->_shadow;
  380. }
  381. /**
  382. * Set Shadow
  383. *
  384. * @param PHPExcel_Worksheet_Drawing_Shadow $pValue
  385. * @throws Exception
  386. */
  387. public function setShadow(PHPExcel_Worksheet_Drawing_Shadow $pValue = null) {
  388. $this->_shadow = $pValue;
  389. }
  390. /**
  391. * Get hash code
  392. *
  393. * @return string Hash code
  394. */
  395. public function getHashCode() {
  396. return md5(
  397. $this->_name
  398. . $this->_description
  399. . $this->_worksheet->getHashCode()
  400. . $this->_coordinates
  401. . $this->_offsetX
  402. . $this->_offsetY
  403. . $this->_width
  404. . $this->_height
  405. . $this->_rotation
  406. . $this->_shadow->getHashCode()
  407. . __CLASS__
  408. );
  409. }
  410. /**
  411. * Hash index
  412. *
  413. * @var string
  414. */
  415. private $_hashIndex;
  416. /**
  417. * Get hash index
  418. *
  419. * Note that this index may vary during script execution! Only reliable moment is
  420. * while doing a write of a workbook and when changes are not allowed.
  421. *
  422. * @return string Hash index
  423. */
  424. public function getHashIndex() {
  425. return $this->_hashIndex;
  426. }
  427. /**
  428. * Set hash index
  429. *
  430. * Note that this index may vary during script execution! Only reliable moment is
  431. * while doing a write of a workbook and when changes are not allowed.
  432. *
  433. * @param string $value Hash index
  434. */
  435. public function setHashIndex($value) {
  436. $this->_hashIndex = $value;
  437. }
  438. /**
  439. * Implement PHP __clone to create a deep clone, not just a shallow copy.
  440. */
  441. public function __clone() {
  442. $vars = get_object_vars($this);
  443. foreach ($vars as $key => $value) {
  444. if (is_object($value)) {
  445. $this->$key = clone $value;
  446. } else {
  447. $this->$key = $value;
  448. }
  449. }
  450. }
  451. }