PageRenderTime 62ms CodeModel.GetById 34ms RepoModel.GetById 1ms app.codeStats 0ms

/common/libraries/plugin/phpexcel/PHPExcel/Worksheet/BaseDrawing.php

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