PageRenderTime 45ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/common/libraries/plugin/phpexcel/PHPExcel/Shared/Excel5.php

https://bitbucket.org/renaatdemuynck/chamilo
PHP | 343 lines | 146 code | 48 blank | 149 comment | 26 complexity | 1fb4e525f945faec177c0f4617289071 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_Shared
  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_Shared_Excel5
  29. *
  30. * @category PHPExcel
  31. * @package PHPExcel_Shared
  32. * @copyright Copyright (c) 2006 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
  33. */
  34. class PHPExcel_Shared_Excel5
  35. {
  36. /**
  37. * Get the width of a column in pixels. We use the relationship y = ceil(7x) where
  38. * x is the width in intrinsic Excel units (measuring width in number of normal characters)
  39. * This holds for Arial 10
  40. *
  41. * @param PHPExcel_Worksheet $sheet The sheet
  42. * @param integer $col The column
  43. * @return integer The width in pixels
  44. */
  45. public static function sizeCol($sheet, $col = 'A')
  46. {
  47. // default font of the workbook
  48. $font = $sheet->getParent()->getDefaultStyle()->getFont();
  49. $columnDimensions = $sheet->getColumnDimensions();
  50. // first find the true column width in pixels (uncollapsed and unhidden)
  51. if (isset($columnDimensions[$col]) and $columnDimensions[$col]->getWidth() != - 1)
  52. {
  53. // then we have column dimension with explicit width
  54. $columnDimension = $columnDimensions[$col];
  55. $width = $columnDimension->getWidth();
  56. $pixelWidth = PHPExcel_Shared_Drawing :: cellDimensionToPixels($width, $font);
  57. }
  58. else
  59. if ($sheet->getDefaultColumnDimension()->getWidth() != - 1)
  60. {
  61. // then we have default column dimension with explicit width
  62. $defaultColumnDimension = $sheet->getDefaultColumnDimension();
  63. $width = $defaultColumnDimension->getWidth();
  64. $pixelWidth = PHPExcel_Shared_Drawing :: cellDimensionToPixels($width, $font);
  65. }
  66. else
  67. {
  68. // we don't even have any default column dimension. Width depends on default font
  69. $pixelWidth = PHPExcel_Shared_Font :: getDefaultColumnWidthByFont($font, true);
  70. }
  71. // now find the effective column width in pixels
  72. if (isset($columnDimensions[$col]) and ! $columnDimensions[$col]->getVisible())
  73. {
  74. $effectivePixelWidth = 0;
  75. }
  76. else
  77. {
  78. $effectivePixelWidth = $pixelWidth;
  79. }
  80. return $effectivePixelWidth;
  81. }
  82. /**
  83. * Convert the height of a cell from user's units to pixels. By interpolation
  84. * the relationship is: y = 4/3x. If the height hasn't been set by the user we
  85. * use the default value. If the row is hidden we use a value of zero.
  86. *
  87. * @param PHPExcel_Worksheet $sheet The sheet
  88. * @param integer $row The row index (1-based)
  89. * @return integer The width in pixels
  90. */
  91. public static function sizeRow($sheet, $row = 1)
  92. {
  93. // default font of the workbook
  94. $font = $sheet->getParent()->getDefaultStyle()->getFont();
  95. $rowDimensions = $sheet->getRowDimensions();
  96. // first find the true row height in pixels (uncollapsed and unhidden)
  97. if (isset($rowDimensions[$row]) and $rowDimensions[$row]->getRowHeight() != - 1)
  98. {
  99. // then we have a row dimension
  100. $rowDimension = $rowDimensions[$row];
  101. $rowHeight = $rowDimension->getRowHeight();
  102. $pixelRowHeight = (int) ceil(4 * $rowHeight / 3); // here we assume Arial 10
  103. }
  104. else
  105. if ($sheet->getDefaultRowDimension()->getRowHeight() != - 1)
  106. {
  107. // then we have a default row dimension with explicit height
  108. $defaultRowDimension = $sheet->getDefaultRowDimension();
  109. $rowHeight = $defaultRowDimension->getRowHeight();
  110. $pixelRowHeight = PHPExcel_Shared_Drawing :: pointsToPixels($rowHeight);
  111. }
  112. else
  113. {
  114. // we don't even have any default row dimension. Height depends on default font
  115. $pointRowHeight = PHPExcel_Shared_Font :: getDefaultRowHeightByFont($font);
  116. $pixelRowHeight = PHPExcel_Shared_Font :: fontSizeToPixels($pointRowHeight);
  117. }
  118. // now find the effective row height in pixels
  119. if (isset($rowDimensions[$row]) and ! $rowDimensions[$row]->getVisible())
  120. {
  121. $effectivePixelRowHeight = 0;
  122. }
  123. else
  124. {
  125. $effectivePixelRowHeight = $pixelRowHeight;
  126. }
  127. return $effectivePixelRowHeight;
  128. }
  129. /**
  130. * Get the horizontal distance in pixels between two anchors
  131. * The distanceX is found as sum of all the spanning columns widths minus correction for the two offsets
  132. *
  133. * @param PHPExcel_Worksheet $sheet
  134. * @param string $startColumn
  135. * @param integer $startOffset Offset within start cell measured in 1/1024 of the cell width
  136. * @param string $endColumn
  137. * @param integer $endOffset Offset within end cell measured in 1/1024 of the cell width
  138. * @return integer Horizontal measured in pixels
  139. */
  140. public static function getDistanceX(PHPExcel_Worksheet $sheet, $startColumn = 'A', $startOffsetX = 0, $endColumn = 'A', $endOffsetX = 0)
  141. {
  142. $distanceX = 0;
  143. // add the widths of the spanning columns
  144. $startColumnIndex = PHPExcel_Cell :: columnIndexFromString($startColumn) - 1; // 1-based
  145. $endColumnIndex = PHPExcel_Cell :: columnIndexFromString($endColumn) - 1; // 1-based
  146. for($i = $startColumnIndex; $i <= $endColumnIndex; ++ $i)
  147. {
  148. $distanceX += self :: sizeCol($sheet, PHPExcel_Cell :: stringFromColumnIndex($i));
  149. }
  150. // correct for offsetX in startcell
  151. $distanceX -= (int) floor(self :: sizeCol($sheet, $startColumn) * $startOffsetX / 1024);
  152. // correct for offsetX in endcell
  153. $distanceX -= (int) floor(self :: sizeCol($sheet, $endColumn) * (1 - $endOffsetX / 1024));
  154. return $distanceX;
  155. }
  156. /**
  157. * Get the vertical distance in pixels between two anchors
  158. * The distanceY is found as sum of all the spanning rows minus two offsets
  159. *
  160. * @param PHPExcel_Worksheet $sheet
  161. * @param string $startRow (1-based)
  162. * @param integer $startOffset Offset within start cell measured in 1/256 of the cell height
  163. * @param string $endRow (1-based)
  164. * @param integer $endOffset Offset within end cell measured in 1/256 of the cell height
  165. * @return integer Vertical distance measured in pixels
  166. */
  167. public static function getDistanceY(PHPExcel_Worksheet $sheet, $startRow = 1, $startOffsetY = 0, $endRow = 1, $endOffsetY = 0)
  168. {
  169. $distanceY = 0;
  170. // add the widths of the spanning rows
  171. for($row = $startRow; $row <= $endRow; ++ $row)
  172. {
  173. $distanceY += self :: sizeRow($sheet, $row);
  174. }
  175. // correct for offsetX in startcell
  176. $distanceY -= (int) floor(self :: sizeRow($sheet, $startRow) * $startOffsetY / 256);
  177. // correct for offsetX in endcell
  178. $distanceY -= (int) floor(self :: sizeRow($sheet, $endRow) * (1 - $endOffsetY / 256));
  179. return $distanceY;
  180. }
  181. /**
  182. * Convert 1-cell anchor coordinates to 2-cell anchor coordinates
  183. * This function is ported from PEAR Spreadsheet_Writer_Excel with small modifications
  184. *
  185. * Calculate the vertices that define the position of the image as required by
  186. * the OBJ record.
  187. *
  188. * +------------+------------+
  189. * | A | B |
  190. * +-----+------------+------------+
  191. * | |(x1,y1) | |
  192. * | 1 |(A1)._______|______ |
  193. * | | | | |
  194. * | | | | |
  195. * +-----+----| BITMAP |-----+
  196. * | | | | |
  197. * | 2 | |______________. |
  198. * | | | (B2)|
  199. * | | | (x2,y2)|
  200. * +---- +------------+------------+
  201. *
  202. * Example of a bitmap that covers some of the area from cell A1 to cell B2.
  203. *
  204. * Based on the width and height of the bitmap we need to calculate 8 vars:
  205. * $col_start, $row_start, $col_end, $row_end, $x1, $y1, $x2, $y2.
  206. * The width and height of the cells are also variable and have to be taken into
  207. * account.
  208. * The values of $col_start and $row_start are passed in from the calling
  209. * function. The values of $col_end and $row_end are calculated by subtracting
  210. * the width and height of the bitmap from the width and height of the
  211. * underlying cells.
  212. * The vertices are expressed as a percentage of the underlying cell width as
  213. * follows (rhs values are in pixels):
  214. *
  215. * x1 = X / W *1024
  216. * y1 = Y / H *256
  217. * x2 = (X-1) / W *1024
  218. * y2 = (Y-1) / H *256
  219. *
  220. * Where: X is distance from the left side of the underlying cell
  221. * Y is distance from the top of the underlying cell
  222. * W is the width of the cell
  223. * H is the height of the cell
  224. *
  225. * @param PHPExcel_Worksheet $sheet
  226. * @param string $coordinates E.g. 'A1'
  227. * @param integer $offsetX Horizontal offset in pixels
  228. * @param integer $offsetY Vertical offset in pixels
  229. * @param integer $width Width in pixels
  230. * @param integer $height Height in pixels
  231. * @return array
  232. */
  233. public static function oneAnchor2twoAnchor($sheet, $coordinates, $offsetX, $offsetY, $width, $height)
  234. {
  235. list($column, $row) = PHPExcel_Cell :: coordinateFromString($coordinates);
  236. $col_start = PHPExcel_Cell :: columnIndexFromString($column) - 1;
  237. $row_start = $row - 1;
  238. $x1 = $offsetX;
  239. $y1 = $offsetY;
  240. // Initialise end cell to the same as the start cell
  241. $col_end = $col_start; // Col containing lower right corner of object
  242. $row_end = $row_start; // Row containing bottom right corner of object
  243. // Zero the specified offset if greater than the cell dimensions
  244. if ($x1 >= self :: sizeCol($sheet, PHPExcel_Cell :: stringFromColumnIndex($col_start)))
  245. {
  246. $x1 = 0;
  247. }
  248. if ($y1 >= self :: sizeRow($sheet, $row_start + 1))
  249. {
  250. $y1 = 0;
  251. }
  252. $width = $width + $x1 - 1;
  253. $height = $height + $y1 - 1;
  254. // Subtract the underlying cell widths to find the end cell of the image
  255. while ($width >= self :: sizeCol($sheet, PHPExcel_Cell :: stringFromColumnIndex($col_end)))
  256. {
  257. $width -= self :: sizeCol($sheet, PHPExcel_Cell :: stringFromColumnIndex($col_end));
  258. ++ $col_end;
  259. }
  260. // Subtract the underlying cell heights to find the end cell of the image
  261. while ($height >= self :: sizeRow($sheet, $row_end + 1))
  262. {
  263. $height -= self :: sizeRow($sheet, $row_end + 1);
  264. ++ $row_end;
  265. }
  266. // Bitmap isn't allowed to start or finish in a hidden cell, i.e. a cell
  267. // with zero height or width.
  268. if (self :: sizeCol($sheet, PHPExcel_Cell :: stringFromColumnIndex($col_start)) == 0)
  269. {
  270. return;
  271. }
  272. if (self :: sizeCol($sheet, PHPExcel_Cell :: stringFromColumnIndex($col_end)) == 0)
  273. {
  274. return;
  275. }
  276. if (self :: sizeRow($sheet, $row_start + 1) == 0)
  277. {
  278. return;
  279. }
  280. if (self :: sizeRow($sheet, $row_end + 1) == 0)
  281. {
  282. return;
  283. }
  284. // Convert the pixel values to the percentage value expected by Excel
  285. $x1 = $x1 / self :: sizeCol($sheet, PHPExcel_Cell :: stringFromColumnIndex($col_start)) * 1024;
  286. $y1 = $y1 / self :: sizeRow($sheet, $row_start + 1) * 256;
  287. $x2 = ($width + 1) / self :: sizeCol($sheet, PHPExcel_Cell :: stringFromColumnIndex($col_end)) * 1024; // Distance to right side of object
  288. $y2 = ($height + 1) / self :: sizeRow($sheet, $row_end + 1) * 256; // Distance to bottom of object
  289. $startCoordinates = PHPExcel_Cell :: stringFromColumnIndex($col_start) . ($row_start + 1);
  290. $endCoordinates = PHPExcel_Cell :: stringFromColumnIndex($col_end) . ($row_end + 1);
  291. $twoAnchor = array('startCoordinates' => $startCoordinates, 'startOffsetX' => $x1, 'startOffsetY' => $y1,
  292. 'endCoordinates' => $endCoordinates, 'endOffsetX' => $x2, 'endOffsetY' => $y2);
  293. return $twoAnchor;
  294. }
  295. }