PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/branches/v1.7.2/Classes/PHPExcel/Shared/Drawing.php

#
PHP | 183 lines | 67 code | 18 blank | 98 comment | 14 complexity | 5f7f1fb55e31a24bc8ee0f5ccd4154cc 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 - 2010 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 - 2010 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 root */
  28. if (!defined('PHPEXCEL_ROOT')) {
  29. /**
  30. * @ignore
  31. */
  32. define('PHPEXCEL_ROOT', dirname(__FILE__) . '/../../');
  33. }
  34. /** PHPExcel_Shared_String */
  35. require_once PHPEXCEL_ROOT . 'PHPExcel/Shared/Font.php';
  36. /**
  37. * PHPExcel_Shared_Drawing
  38. *
  39. * @category PHPExcel
  40. * @package PHPExcel_Shared
  41. * @copyright Copyright (c) 2006 - 2010 PHPExcel (http://www.codeplex.com/PHPExcel)
  42. */
  43. class PHPExcel_Shared_Drawing
  44. {
  45. /**
  46. * Convert pixels to EMU
  47. *
  48. * @param int $pValue Value in pixels
  49. * @return int Value in EMU
  50. */
  51. public static function pixelsToEMU($pValue = 0) {
  52. return round($pValue * 9525);
  53. }
  54. /**
  55. * Convert EMU to pixels
  56. *
  57. * @param int $pValue Value in EMU
  58. * @return int Value in pixels
  59. */
  60. public static function EMUToPixels($pValue = 0) {
  61. if ($pValue != 0) {
  62. return round($pValue / 9525);
  63. } else {
  64. return 0;
  65. }
  66. }
  67. /**
  68. * Convert pixels to column width. Exact algorithm not known.
  69. * By inspection of a real Excel file using Calibri 11, one finds 1000px ~ 142.85546875
  70. * This gives a conversion factor of 7. Also, we assume that pixels and font size are proportional.
  71. *
  72. * @param int $pValue Value in pixels
  73. * @param PHPExcel_Style_Font $pDefaultFont Default font of the workbook
  74. * @return int Value in cell dimension
  75. */
  76. public static function pixelsToCellDimension($pValue = 0, PHPExcel_Style_Font $pDefaultFont) {
  77. // Font name and size
  78. $name = $pDefaultFont->getName();
  79. $size = $pDefaultFont->getSize();
  80. if (isset(PHPExcel_Shared_Font::$defaultColumnWidths[$name][$size])) {
  81. // Exact width can be determined
  82. $colWidth = $pValue
  83. * PHPExcel_Shared_Font::$defaultColumnWidths[$name][$size]['width']
  84. / PHPExcel_Shared_Font::$defaultColumnWidths[$name][$size]['px'];
  85. } else {
  86. // We don't have data for this particular font and size, use approximation by
  87. // extrapolating from Calibri 11
  88. $colWidth = $pValue * 11
  89. * PHPExcel_Shared_Font::$defaultColumnWidths['Calibri'][11]['width']
  90. / PHPExcel_Shared_Font::$defaultColumnWidths['Calibri'][11]['px'] / $size;
  91. }
  92. return $colWidth;
  93. }
  94. /**
  95. * Convert column width from (intrinsic) Excel units to pixels
  96. *
  97. * @param float $pValue Value in cell dimension
  98. * @param PHPExcel_Style_Font $pDefaultFont Default font of the workbook
  99. * @return int Value in pixels
  100. */
  101. public static function cellDimensionToPixels($pValue = 0, PHPExcel_Style_Font $pDefaultFont) {
  102. // Font name and size
  103. $name = $pDefaultFont->getName();
  104. $size = $pDefaultFont->getSize();
  105. if (isset(PHPExcel_Shared_Font::$defaultColumnWidths[$name][$size])) {
  106. // Exact width can be determined
  107. $colWidth = $pValue
  108. * PHPExcel_Shared_Font::$defaultColumnWidths[$name][$size]['px']
  109. / PHPExcel_Shared_Font::$defaultColumnWidths[$name][$size]['width'];
  110. } else {
  111. // We don't have data for this particular font and size, use approximation by
  112. // extrapolating from Calibri 11
  113. $colWidth = $pValue * $size
  114. * PHPExcel_Shared_Font::$defaultColumnWidths['Calibri'][11]['px']
  115. / PHPExcel_Shared_Font::$defaultColumnWidths['Calibri'][11]['width'] / 11;
  116. }
  117. // Round pixels to closest integer
  118. $colWidth = (int) round($colWidth);
  119. return $colWidth;
  120. }
  121. /**
  122. * Convert pixels to points
  123. *
  124. * @param int $pValue Value in pixels
  125. * @return int Value in points
  126. */
  127. public static function pixelsToPoints($pValue = 0) {
  128. return $pValue * 0.67777777;
  129. }
  130. /**
  131. * Convert points to pixels
  132. *
  133. * @param int $pValue Value in points
  134. * @return int Value in pixels
  135. */
  136. public static function pointsToPixels($pValue = 0) {
  137. if ($pValue != 0) {
  138. return (int) ceil($pValue * 1.333333333);
  139. } else {
  140. return 0;
  141. }
  142. }
  143. /**
  144. * Convert degrees to angle
  145. *
  146. * @param int $pValue Degrees
  147. * @return int Angle
  148. */
  149. public static function degreesToAngle($pValue = 0) {
  150. return (int)round($pValue * 60000);
  151. }
  152. /**
  153. * Convert angle to degrees
  154. *
  155. * @param int $pValue Angle
  156. * @return int Degrees
  157. */
  158. public static function angleToDegrees($pValue = 0) {
  159. if ($pValue != 0) {
  160. return round($pValue / 60000);
  161. } else {
  162. return 0;
  163. }
  164. }
  165. }