Converter.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. <?php
  2. /**
  3. * This file is part of PHPWord - A pure PHP library for reading and writing
  4. * word processing documents.
  5. *
  6. * PHPWord is free software distributed under the terms of the GNU Lesser
  7. * General Public License version 3 as published by the Free Software Foundation.
  8. *
  9. * For the full copyright and license information, please read the LICENSE
  10. * file that was distributed with this source code. For the full list of
  11. * contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
  12. *
  13. * @see https://github.com/PHPOffice/PHPWord
  14. * @copyright 2010-2018 PHPWord contributors
  15. * @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
  16. */
  17. namespace PhpOffice\PhpWord\Shared;
  18. /**
  19. * Common converter functions
  20. */
  21. class Converter
  22. {
  23. const INCH_TO_CM = 2.54;
  24. const INCH_TO_TWIP = 1440;
  25. const INCH_TO_PIXEL = 96;
  26. const INCH_TO_POINT = 72;
  27. const INCH_TO_PICA = 6;
  28. const PIXEL_TO_EMU = 9525;
  29. const DEGREE_TO_ANGLE = 60000;
  30. /**
  31. * Convert centimeter to twip
  32. *
  33. * @param float $centimeter
  34. * @return float
  35. */
  36. public static function cmToTwip($centimeter = 1)
  37. {
  38. return $centimeter / self::INCH_TO_CM * self::INCH_TO_TWIP;
  39. }
  40. /**
  41. * Convert centimeter to inch
  42. *
  43. * @param float $centimeter
  44. * @return float
  45. */
  46. public static function cmToInch($centimeter = 1)
  47. {
  48. return $centimeter / self::INCH_TO_CM;
  49. }
  50. /**
  51. * Convert centimeter to pixel
  52. *
  53. * @param float $centimeter
  54. * @return float
  55. */
  56. public static function cmToPixel($centimeter = 1)
  57. {
  58. return $centimeter / self::INCH_TO_CM * self::INCH_TO_PIXEL;
  59. }
  60. /**
  61. * Convert centimeter to point
  62. *
  63. * @param float $centimeter
  64. * @return float
  65. */
  66. public static function cmToPoint($centimeter = 1)
  67. {
  68. return $centimeter / self::INCH_TO_CM * self::INCH_TO_POINT;
  69. }
  70. /**
  71. * Convert centimeter to EMU
  72. *
  73. * @param float $centimeter
  74. * @return float
  75. */
  76. public static function cmToEmu($centimeter = 1)
  77. {
  78. return round($centimeter / self::INCH_TO_CM * self::INCH_TO_PIXEL * self::PIXEL_TO_EMU);
  79. }
  80. /**
  81. * Convert inch to twip
  82. *
  83. * @param float $inch
  84. * @return float
  85. */
  86. public static function inchToTwip($inch = 1)
  87. {
  88. return $inch * self::INCH_TO_TWIP;
  89. }
  90. /**
  91. * Convert inch to centimeter
  92. *
  93. * @param float $inch
  94. * @return float
  95. */
  96. public static function inchToCm($inch = 1)
  97. {
  98. return $inch * self::INCH_TO_CM;
  99. }
  100. /**
  101. * Convert inch to pixel
  102. *
  103. * @param float $inch
  104. * @return float
  105. */
  106. public static function inchToPixel($inch = 1)
  107. {
  108. return $inch * self::INCH_TO_PIXEL;
  109. }
  110. /**
  111. * Convert inch to point
  112. *
  113. * @param float $inch
  114. * @return float
  115. */
  116. public static function inchToPoint($inch = 1)
  117. {
  118. return $inch * self::INCH_TO_POINT;
  119. }
  120. /**
  121. * Convert inch to EMU
  122. *
  123. * @param float $inch
  124. * @return int
  125. */
  126. public static function inchToEmu($inch = 1)
  127. {
  128. return round($inch * self::INCH_TO_PIXEL * self::PIXEL_TO_EMU);
  129. }
  130. /**
  131. * Convert pixel to twip
  132. *
  133. * @param float $pixel
  134. * @return float
  135. */
  136. public static function pixelToTwip($pixel = 1)
  137. {
  138. return $pixel / self::INCH_TO_PIXEL * self::INCH_TO_TWIP;
  139. }
  140. /**
  141. * Convert pixel to centimeter
  142. *
  143. * @param float $pixel
  144. * @return float
  145. */
  146. public static function pixelToCm($pixel = 1)
  147. {
  148. return $pixel / self::INCH_TO_PIXEL * self::INCH_TO_CM;
  149. }
  150. /**
  151. * Convert pixel to point
  152. *
  153. * @param float $pixel
  154. * @return float
  155. */
  156. public static function pixelToPoint($pixel = 1)
  157. {
  158. return $pixel / self::INCH_TO_PIXEL * self::INCH_TO_POINT;
  159. }
  160. /**
  161. * Convert pixel to EMU
  162. *
  163. * @param float $pixel
  164. * @return int
  165. */
  166. public static function pixelToEmu($pixel = 1)
  167. {
  168. return round($pixel * self::PIXEL_TO_EMU);
  169. }
  170. /**
  171. * Convert point to twip unit
  172. *
  173. * @param float $point
  174. * @return float
  175. */
  176. public static function pointToTwip($point = 1)
  177. {
  178. return $point / self::INCH_TO_POINT * self::INCH_TO_TWIP;
  179. }
  180. /**
  181. * Convert point to pixel
  182. *
  183. * @param float $point
  184. * @return float
  185. */
  186. public static function pointToPixel($point = 1)
  187. {
  188. return $point / self::INCH_TO_POINT * self::INCH_TO_PIXEL;
  189. }
  190. /**
  191. * Convert point to EMU
  192. *
  193. * @param float $point
  194. * @return float
  195. */
  196. public static function pointToEmu($point = 1)
  197. {
  198. return round($point / self::INCH_TO_POINT * self::INCH_TO_PIXEL * self::PIXEL_TO_EMU);
  199. }
  200. /**
  201. * Convert point to cm
  202. *
  203. * @param float $point
  204. * @return float
  205. */
  206. public static function pointToCm($point = 1)
  207. {
  208. return $point / self::INCH_TO_POINT * self::INCH_TO_CM;
  209. }
  210. /**
  211. * Convert EMU to pixel
  212. *
  213. * @param float $emu
  214. * @return float
  215. */
  216. public static function emuToPixel($emu = 1)
  217. {
  218. return round($emu / self::PIXEL_TO_EMU);
  219. }
  220. /**
  221. * Convert pica to point
  222. *
  223. * @param float $pica
  224. * @return float
  225. */
  226. public static function picaToPoint($pica = 1)
  227. {
  228. return $pica / self::INCH_TO_PICA * self::INCH_TO_POINT;
  229. }
  230. /**
  231. * Convert degree to angle
  232. *
  233. * @param float $degree
  234. * @return int
  235. */
  236. public static function degreeToAngle($degree = 1)
  237. {
  238. return (int) round($degree * self::DEGREE_TO_ANGLE);
  239. }
  240. /**
  241. * Convert angle to degrees
  242. *
  243. * @param float $angle
  244. * @return int
  245. */
  246. public static function angleToDegree($angle = 1)
  247. {
  248. return round($angle / self::DEGREE_TO_ANGLE);
  249. }
  250. /**
  251. * Convert colorname as string to RGB
  252. *
  253. * @param string $value color name
  254. * @return string color as hex RGB string, or original value if unknown
  255. */
  256. public static function stringToRgb($value)
  257. {
  258. switch ($value) {
  259. case \PhpOffice\PhpWord\Style\Font::FGCOLOR_YELLOW:
  260. return 'FFFF00';
  261. case \PhpOffice\PhpWord\Style\Font::FGCOLOR_LIGHTGREEN:
  262. return '90EE90';
  263. case \PhpOffice\PhpWord\Style\Font::FGCOLOR_CYAN:
  264. return '00FFFF';
  265. case \PhpOffice\PhpWord\Style\Font::FGCOLOR_MAGENTA:
  266. return 'FF00FF';
  267. case \PhpOffice\PhpWord\Style\Font::FGCOLOR_BLUE:
  268. return '0000FF';
  269. case \PhpOffice\PhpWord\Style\Font::FGCOLOR_RED:
  270. return 'FF0000';
  271. case \PhpOffice\PhpWord\Style\Font::FGCOLOR_DARKBLUE:
  272. return '00008B';
  273. case \PhpOffice\PhpWord\Style\Font::FGCOLOR_DARKCYAN:
  274. return '008B8B';
  275. case \PhpOffice\PhpWord\Style\Font::FGCOLOR_DARKGREEN:
  276. return '006400';
  277. case \PhpOffice\PhpWord\Style\Font::FGCOLOR_DARKMAGENTA:
  278. return '8B008B';
  279. case \PhpOffice\PhpWord\Style\Font::FGCOLOR_DARKRED:
  280. return '8B0000';
  281. case \PhpOffice\PhpWord\Style\Font::FGCOLOR_DARKYELLOW:
  282. return '8B8B00';
  283. case \PhpOffice\PhpWord\Style\Font::FGCOLOR_DARKGRAY:
  284. return 'A9A9A9';
  285. case \PhpOffice\PhpWord\Style\Font::FGCOLOR_LIGHTGRAY:
  286. return 'D3D3D3';
  287. case \PhpOffice\PhpWord\Style\Font::FGCOLOR_BLACK:
  288. return '000000';
  289. }
  290. return $value;
  291. }
  292. /**
  293. * Convert HTML hexadecimal to RGB
  294. *
  295. * @param string $value HTML Color in hexadecimal
  296. * @return array Value in RGB
  297. */
  298. public static function htmlToRgb($value)
  299. {
  300. if ($value[0] == '#') {
  301. $value = substr($value, 1);
  302. } else {
  303. $value = self::stringToRgb($value);
  304. }
  305. if (strlen($value) == 6) {
  306. list($red, $green, $blue) = array($value[0] . $value[1], $value[2] . $value[3], $value[4] . $value[5]);
  307. } elseif (strlen($value) == 3) {
  308. list($red, $green, $blue) = array($value[0] . $value[0], $value[1] . $value[1], $value[2] . $value[2]);
  309. } else {
  310. return false;
  311. }
  312. $red = ctype_xdigit($red) ? hexdec($red) : 0;
  313. $green = ctype_xdigit($green) ? hexdec($green) : 0;
  314. $blue = ctype_xdigit($blue) ? hexdec($blue) : 0;
  315. return array($red, $green, $blue);
  316. }
  317. /**
  318. * Transforms a size in CSS format (eg. 10px, 10px, ...) to points
  319. *
  320. * @param string $value
  321. * @return float
  322. */
  323. public static function cssToPoint($value)
  324. {
  325. if ($value == '0') {
  326. return 0;
  327. }
  328. $matches = array();
  329. if (preg_match('/^[+-]?([0-9]+\.?[0-9]*)?(px|em|ex|%|in|cm|mm|pt|pc)$/i', $value, $matches)) {
  330. $size = $matches[1];
  331. $unit = $matches[2];
  332. switch ($unit) {
  333. case 'pt':
  334. return $size;
  335. case 'px':
  336. return self::pixelToPoint($size);
  337. case 'cm':
  338. return self::cmToPoint($size);
  339. case 'mm':
  340. return self::cmToPoint($size / 10);
  341. case 'in':
  342. return self::inchToPoint($size);
  343. case 'pc':
  344. return self::picaToPoint($size);
  345. }
  346. }
  347. return null;
  348. }
  349. /**
  350. * Transforms a size in CSS format (eg. 10px, 10px, ...) to twips
  351. *
  352. * @param string $value
  353. * @return float
  354. */
  355. public static function cssToTwip($value)
  356. {
  357. return self::pointToTwip(self::cssToPoint($value));
  358. }
  359. /**
  360. * Transforms a size in CSS format (eg. 10px, 10px, ...) to pixel
  361. *
  362. * @param string $value
  363. * @return float
  364. */
  365. public static function cssToPixel($value)
  366. {
  367. return self::pointToPixel(self::cssToPoint($value));
  368. }
  369. /**
  370. * Transforms a size in CSS format (eg. 10px, 10px, ...) to cm
  371. *
  372. * @param string $value
  373. * @return float
  374. */
  375. public static function cssToCm($value)
  376. {
  377. return self::pointToCm(self::cssToPoint($value));
  378. }
  379. /**
  380. * Transforms a size in CSS format (eg. 10px, 10px, ...) to emu
  381. *
  382. * @param string $value
  383. * @return float
  384. */
  385. public static function cssToEmu($value)
  386. {
  387. return self::pointToEmu(self::cssToPoint($value));
  388. }
  389. }