PageRenderTime 70ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/phpexcel/PHPExcel/Helper/HTML.php

http://github.com/moodle/moodle
PHP | 808 lines | 756 code | 45 blank | 7 comment | 20 complexity | ad5124b616693c73e1fe6f894bcb2e92 MD5 | raw file
Possible License(s): MIT, AGPL-3.0, MPL-2.0-no-copyleft-exception, LGPL-3.0, GPL-3.0, Apache-2.0, LGPL-2.1, BSD-3-Clause
  1. <?php
  2. class PHPExcel_Helper_HTML
  3. {
  4. protected static $colourMap = array(
  5. 'aliceblue' => 'f0f8ff',
  6. 'antiquewhite' => 'faebd7',
  7. 'antiquewhite1' => 'ffefdb',
  8. 'antiquewhite2' => 'eedfcc',
  9. 'antiquewhite3' => 'cdc0b0',
  10. 'antiquewhite4' => '8b8378',
  11. 'aqua' => '00ffff',
  12. 'aquamarine1' => '7fffd4',
  13. 'aquamarine2' => '76eec6',
  14. 'aquamarine4' => '458b74',
  15. 'azure1' => 'f0ffff',
  16. 'azure2' => 'e0eeee',
  17. 'azure3' => 'c1cdcd',
  18. 'azure4' => '838b8b',
  19. 'beige' => 'f5f5dc',
  20. 'bisque1' => 'ffe4c4',
  21. 'bisque2' => 'eed5b7',
  22. 'bisque3' => 'cdb79e',
  23. 'bisque4' => '8b7d6b',
  24. 'black' => '000000',
  25. 'blanchedalmond' => 'ffebcd',
  26. 'blue' => '0000ff',
  27. 'blue1' => '0000ff',
  28. 'blue2' => '0000ee',
  29. 'blue4' => '00008b',
  30. 'blueviolet' => '8a2be2',
  31. 'brown' => 'a52a2a',
  32. 'brown1' => 'ff4040',
  33. 'brown2' => 'ee3b3b',
  34. 'brown3' => 'cd3333',
  35. 'brown4' => '8b2323',
  36. 'burlywood' => 'deb887',
  37. 'burlywood1' => 'ffd39b',
  38. 'burlywood2' => 'eec591',
  39. 'burlywood3' => 'cdaa7d',
  40. 'burlywood4' => '8b7355',
  41. 'cadetblue' => '5f9ea0',
  42. 'cadetblue1' => '98f5ff',
  43. 'cadetblue2' => '8ee5ee',
  44. 'cadetblue3' => '7ac5cd',
  45. 'cadetblue4' => '53868b',
  46. 'chartreuse1' => '7fff00',
  47. 'chartreuse2' => '76ee00',
  48. 'chartreuse3' => '66cd00',
  49. 'chartreuse4' => '458b00',
  50. 'chocolate' => 'd2691e',
  51. 'chocolate1' => 'ff7f24',
  52. 'chocolate2' => 'ee7621',
  53. 'chocolate3' => 'cd661d',
  54. 'coral' => 'ff7f50',
  55. 'coral1' => 'ff7256',
  56. 'coral2' => 'ee6a50',
  57. 'coral3' => 'cd5b45',
  58. 'coral4' => '8b3e2f',
  59. 'cornflowerblue' => '6495ed',
  60. 'cornsilk1' => 'fff8dc',
  61. 'cornsilk2' => 'eee8cd',
  62. 'cornsilk3' => 'cdc8b1',
  63. 'cornsilk4' => '8b8878',
  64. 'cyan1' => '00ffff',
  65. 'cyan2' => '00eeee',
  66. 'cyan3' => '00cdcd',
  67. 'cyan4' => '008b8b',
  68. 'darkgoldenrod' => 'b8860b',
  69. 'darkgoldenrod1' => 'ffb90f',
  70. 'darkgoldenrod2' => 'eead0e',
  71. 'darkgoldenrod3' => 'cd950c',
  72. 'darkgoldenrod4' => '8b6508',
  73. 'darkgreen' => '006400',
  74. 'darkkhaki' => 'bdb76b',
  75. 'darkolivegreen' => '556b2f',
  76. 'darkolivegreen1' => 'caff70',
  77. 'darkolivegreen2' => 'bcee68',
  78. 'darkolivegreen3' => 'a2cd5a',
  79. 'darkolivegreen4' => '6e8b3d',
  80. 'darkorange' => 'ff8c00',
  81. 'darkorange1' => 'ff7f00',
  82. 'darkorange2' => 'ee7600',
  83. 'darkorange3' => 'cd6600',
  84. 'darkorange4' => '8b4500',
  85. 'darkorchid' => '9932cc',
  86. 'darkorchid1' => 'bf3eff',
  87. 'darkorchid2' => 'b23aee',
  88. 'darkorchid3' => '9a32cd',
  89. 'darkorchid4' => '68228b',
  90. 'darksalmon' => 'e9967a',
  91. 'darkseagreen' => '8fbc8f',
  92. 'darkseagreen1' => 'c1ffc1',
  93. 'darkseagreen2' => 'b4eeb4',
  94. 'darkseagreen3' => '9bcd9b',
  95. 'darkseagreen4' => '698b69',
  96. 'darkslateblue' => '483d8b',
  97. 'darkslategray' => '2f4f4f',
  98. 'darkslategray1' => '97ffff',
  99. 'darkslategray2' => '8deeee',
  100. 'darkslategray3' => '79cdcd',
  101. 'darkslategray4' => '528b8b',
  102. 'darkturquoise' => '00ced1',
  103. 'darkviolet' => '9400d3',
  104. 'deeppink1' => 'ff1493',
  105. 'deeppink2' => 'ee1289',
  106. 'deeppink3' => 'cd1076',
  107. 'deeppink4' => '8b0a50',
  108. 'deepskyblue1' => '00bfff',
  109. 'deepskyblue2' => '00b2ee',
  110. 'deepskyblue3' => '009acd',
  111. 'deepskyblue4' => '00688b',
  112. 'dimgray' => '696969',
  113. 'dodgerblue1' => '1e90ff',
  114. 'dodgerblue2' => '1c86ee',
  115. 'dodgerblue3' => '1874cd',
  116. 'dodgerblue4' => '104e8b',
  117. 'firebrick' => 'b22222',
  118. 'firebrick1' => 'ff3030',
  119. 'firebrick2' => 'ee2c2c',
  120. 'firebrick3' => 'cd2626',
  121. 'firebrick4' => '8b1a1a',
  122. 'floralwhite' => 'fffaf0',
  123. 'forestgreen' => '228b22',
  124. 'fuchsia' => 'ff00ff',
  125. 'gainsboro' => 'dcdcdc',
  126. 'ghostwhite' => 'f8f8ff',
  127. 'gold1' => 'ffd700',
  128. 'gold2' => 'eec900',
  129. 'gold3' => 'cdad00',
  130. 'gold4' => '8b7500',
  131. 'goldenrod' => 'daa520',
  132. 'goldenrod1' => 'ffc125',
  133. 'goldenrod2' => 'eeb422',
  134. 'goldenrod3' => 'cd9b1d',
  135. 'goldenrod4' => '8b6914',
  136. 'gray' => 'bebebe',
  137. 'gray1' => '030303',
  138. 'gray10' => '1a1a1a',
  139. 'gray11' => '1c1c1c',
  140. 'gray12' => '1f1f1f',
  141. 'gray13' => '212121',
  142. 'gray14' => '242424',
  143. 'gray15' => '262626',
  144. 'gray16' => '292929',
  145. 'gray17' => '2b2b2b',
  146. 'gray18' => '2e2e2e',
  147. 'gray19' => '303030',
  148. 'gray2' => '050505',
  149. 'gray20' => '333333',
  150. 'gray21' => '363636',
  151. 'gray22' => '383838',
  152. 'gray23' => '3b3b3b',
  153. 'gray24' => '3d3d3d',
  154. 'gray25' => '404040',
  155. 'gray26' => '424242',
  156. 'gray27' => '454545',
  157. 'gray28' => '474747',
  158. 'gray29' => '4a4a4a',
  159. 'gray3' => '080808',
  160. 'gray30' => '4d4d4d',
  161. 'gray31' => '4f4f4f',
  162. 'gray32' => '525252',
  163. 'gray33' => '545454',
  164. 'gray34' => '575757',
  165. 'gray35' => '595959',
  166. 'gray36' => '5c5c5c',
  167. 'gray37' => '5e5e5e',
  168. 'gray38' => '616161',
  169. 'gray39' => '636363',
  170. 'gray4' => '0a0a0a',
  171. 'gray40' => '666666',
  172. 'gray41' => '696969',
  173. 'gray42' => '6b6b6b',
  174. 'gray43' => '6e6e6e',
  175. 'gray44' => '707070',
  176. 'gray45' => '737373',
  177. 'gray46' => '757575',
  178. 'gray47' => '787878',
  179. 'gray48' => '7a7a7a',
  180. 'gray49' => '7d7d7d',
  181. 'gray5' => '0d0d0d',
  182. 'gray50' => '7f7f7f',
  183. 'gray51' => '828282',
  184. 'gray52' => '858585',
  185. 'gray53' => '878787',
  186. 'gray54' => '8a8a8a',
  187. 'gray55' => '8c8c8c',
  188. 'gray56' => '8f8f8f',
  189. 'gray57' => '919191',
  190. 'gray58' => '949494',
  191. 'gray59' => '969696',
  192. 'gray6' => '0f0f0f',
  193. 'gray60' => '999999',
  194. 'gray61' => '9c9c9c',
  195. 'gray62' => '9e9e9e',
  196. 'gray63' => 'a1a1a1',
  197. 'gray64' => 'a3a3a3',
  198. 'gray65' => 'a6a6a6',
  199. 'gray66' => 'a8a8a8',
  200. 'gray67' => 'ababab',
  201. 'gray68' => 'adadad',
  202. 'gray69' => 'b0b0b0',
  203. 'gray7' => '121212',
  204. 'gray70' => 'b3b3b3',
  205. 'gray71' => 'b5b5b5',
  206. 'gray72' => 'b8b8b8',
  207. 'gray73' => 'bababa',
  208. 'gray74' => 'bdbdbd',
  209. 'gray75' => 'bfbfbf',
  210. 'gray76' => 'c2c2c2',
  211. 'gray77' => 'c4c4c4',
  212. 'gray78' => 'c7c7c7',
  213. 'gray79' => 'c9c9c9',
  214. 'gray8' => '141414',
  215. 'gray80' => 'cccccc',
  216. 'gray81' => 'cfcfcf',
  217. 'gray82' => 'd1d1d1',
  218. 'gray83' => 'd4d4d4',
  219. 'gray84' => 'd6d6d6',
  220. 'gray85' => 'd9d9d9',
  221. 'gray86' => 'dbdbdb',
  222. 'gray87' => 'dedede',
  223. 'gray88' => 'e0e0e0',
  224. 'gray89' => 'e3e3e3',
  225. 'gray9' => '171717',
  226. 'gray90' => 'e5e5e5',
  227. 'gray91' => 'e8e8e8',
  228. 'gray92' => 'ebebeb',
  229. 'gray93' => 'ededed',
  230. 'gray94' => 'f0f0f0',
  231. 'gray95' => 'f2f2f2',
  232. 'gray97' => 'f7f7f7',
  233. 'gray98' => 'fafafa',
  234. 'gray99' => 'fcfcfc',
  235. 'green' => '00ff00',
  236. 'green1' => '00ff00',
  237. 'green2' => '00ee00',
  238. 'green3' => '00cd00',
  239. 'green4' => '008b00',
  240. 'greenyellow' => 'adff2f',
  241. 'honeydew1' => 'f0fff0',
  242. 'honeydew2' => 'e0eee0',
  243. 'honeydew3' => 'c1cdc1',
  244. 'honeydew4' => '838b83',
  245. 'hotpink' => 'ff69b4',
  246. 'hotpink1' => 'ff6eb4',
  247. 'hotpink2' => 'ee6aa7',
  248. 'hotpink3' => 'cd6090',
  249. 'hotpink4' => '8b3a62',
  250. 'indianred' => 'cd5c5c',
  251. 'indianred1' => 'ff6a6a',
  252. 'indianred2' => 'ee6363',
  253. 'indianred3' => 'cd5555',
  254. 'indianred4' => '8b3a3a',
  255. 'ivory1' => 'fffff0',
  256. 'ivory2' => 'eeeee0',
  257. 'ivory3' => 'cdcdc1',
  258. 'ivory4' => '8b8b83',
  259. 'khaki' => 'f0e68c',
  260. 'khaki1' => 'fff68f',
  261. 'khaki2' => 'eee685',
  262. 'khaki3' => 'cdc673',
  263. 'khaki4' => '8b864e',
  264. 'lavender' => 'e6e6fa',
  265. 'lavenderblush1' => 'fff0f5',
  266. 'lavenderblush2' => 'eee0e5',
  267. 'lavenderblush3' => 'cdc1c5',
  268. 'lavenderblush4' => '8b8386',
  269. 'lawngreen' => '7cfc00',
  270. 'lemonchiffon1' => 'fffacd',
  271. 'lemonchiffon2' => 'eee9bf',
  272. 'lemonchiffon3' => 'cdc9a5',
  273. 'lemonchiffon4' => '8b8970',
  274. 'light' => 'eedd82',
  275. 'lightblue' => 'add8e6',
  276. 'lightblue1' => 'bfefff',
  277. 'lightblue2' => 'b2dfee',
  278. 'lightblue3' => '9ac0cd',
  279. 'lightblue4' => '68838b',
  280. 'lightcoral' => 'f08080',
  281. 'lightcyan1' => 'e0ffff',
  282. 'lightcyan2' => 'd1eeee',
  283. 'lightcyan3' => 'b4cdcd',
  284. 'lightcyan4' => '7a8b8b',
  285. 'lightgoldenrod1' => 'ffec8b',
  286. 'lightgoldenrod2' => 'eedc82',
  287. 'lightgoldenrod3' => 'cdbe70',
  288. 'lightgoldenrod4' => '8b814c',
  289. 'lightgoldenrodyellow' => 'fafad2',
  290. 'lightgray' => 'd3d3d3',
  291. 'lightpink' => 'ffb6c1',
  292. 'lightpink1' => 'ffaeb9',
  293. 'lightpink2' => 'eea2ad',
  294. 'lightpink3' => 'cd8c95',
  295. 'lightpink4' => '8b5f65',
  296. 'lightsalmon1' => 'ffa07a',
  297. 'lightsalmon2' => 'ee9572',
  298. 'lightsalmon3' => 'cd8162',
  299. 'lightsalmon4' => '8b5742',
  300. 'lightseagreen' => '20b2aa',
  301. 'lightskyblue' => '87cefa',
  302. 'lightskyblue1' => 'b0e2ff',
  303. 'lightskyblue2' => 'a4d3ee',
  304. 'lightskyblue3' => '8db6cd',
  305. 'lightskyblue4' => '607b8b',
  306. 'lightslateblue' => '8470ff',
  307. 'lightslategray' => '778899',
  308. 'lightsteelblue' => 'b0c4de',
  309. 'lightsteelblue1' => 'cae1ff',
  310. 'lightsteelblue2' => 'bcd2ee',
  311. 'lightsteelblue3' => 'a2b5cd',
  312. 'lightsteelblue4' => '6e7b8b',
  313. 'lightyellow1' => 'ffffe0',
  314. 'lightyellow2' => 'eeeed1',
  315. 'lightyellow3' => 'cdcdb4',
  316. 'lightyellow4' => '8b8b7a',
  317. 'lime' => '00ff00',
  318. 'limegreen' => '32cd32',
  319. 'linen' => 'faf0e6',
  320. 'magenta' => 'ff00ff',
  321. 'magenta2' => 'ee00ee',
  322. 'magenta3' => 'cd00cd',
  323. 'magenta4' => '8b008b',
  324. 'maroon' => 'b03060',
  325. 'maroon1' => 'ff34b3',
  326. 'maroon2' => 'ee30a7',
  327. 'maroon3' => 'cd2990',
  328. 'maroon4' => '8b1c62',
  329. 'medium' => '66cdaa',
  330. 'mediumaquamarine' => '66cdaa',
  331. 'mediumblue' => '0000cd',
  332. 'mediumorchid' => 'ba55d3',
  333. 'mediumorchid1' => 'e066ff',
  334. 'mediumorchid2' => 'd15fee',
  335. 'mediumorchid3' => 'b452cd',
  336. 'mediumorchid4' => '7a378b',
  337. 'mediumpurple' => '9370db',
  338. 'mediumpurple1' => 'ab82ff',
  339. 'mediumpurple2' => '9f79ee',
  340. 'mediumpurple3' => '8968cd',
  341. 'mediumpurple4' => '5d478b',
  342. 'mediumseagreen' => '3cb371',
  343. 'mediumslateblue' => '7b68ee',
  344. 'mediumspringgreen' => '00fa9a',
  345. 'mediumturquoise' => '48d1cc',
  346. 'mediumvioletred' => 'c71585',
  347. 'midnightblue' => '191970',
  348. 'mintcream' => 'f5fffa',
  349. 'mistyrose1' => 'ffe4e1',
  350. 'mistyrose2' => 'eed5d2',
  351. 'mistyrose3' => 'cdb7b5',
  352. 'mistyrose4' => '8b7d7b',
  353. 'moccasin' => 'ffe4b5',
  354. 'navajowhite1' => 'ffdead',
  355. 'navajowhite2' => 'eecfa1',
  356. 'navajowhite3' => 'cdb38b',
  357. 'navajowhite4' => '8b795e',
  358. 'navy' => '000080',
  359. 'navyblue' => '000080',
  360. 'oldlace' => 'fdf5e6',
  361. 'olive' => '808000',
  362. 'olivedrab' => '6b8e23',
  363. 'olivedrab1' => 'c0ff3e',
  364. 'olivedrab2' => 'b3ee3a',
  365. 'olivedrab4' => '698b22',
  366. 'orange' => 'ffa500',
  367. 'orange1' => 'ffa500',
  368. 'orange2' => 'ee9a00',
  369. 'orange3' => 'cd8500',
  370. 'orange4' => '8b5a00',
  371. 'orangered1' => 'ff4500',
  372. 'orangered2' => 'ee4000',
  373. 'orangered3' => 'cd3700',
  374. 'orangered4' => '8b2500',
  375. 'orchid' => 'da70d6',
  376. 'orchid1' => 'ff83fa',
  377. 'orchid2' => 'ee7ae9',
  378. 'orchid3' => 'cd69c9',
  379. 'orchid4' => '8b4789',
  380. 'pale' => 'db7093',
  381. 'palegoldenrod' => 'eee8aa',
  382. 'palegreen' => '98fb98',
  383. 'palegreen1' => '9aff9a',
  384. 'palegreen2' => '90ee90',
  385. 'palegreen3' => '7ccd7c',
  386. 'palegreen4' => '548b54',
  387. 'paleturquoise' => 'afeeee',
  388. 'paleturquoise1' => 'bbffff',
  389. 'paleturquoise2' => 'aeeeee',
  390. 'paleturquoise3' => '96cdcd',
  391. 'paleturquoise4' => '668b8b',
  392. 'palevioletred' => 'db7093',
  393. 'palevioletred1' => 'ff82ab',
  394. 'palevioletred2' => 'ee799f',
  395. 'palevioletred3' => 'cd6889',
  396. 'palevioletred4' => '8b475d',
  397. 'papayawhip' => 'ffefd5',
  398. 'peachpuff1' => 'ffdab9',
  399. 'peachpuff2' => 'eecbad',
  400. 'peachpuff3' => 'cdaf95',
  401. 'peachpuff4' => '8b7765',
  402. 'pink' => 'ffc0cb',
  403. 'pink1' => 'ffb5c5',
  404. 'pink2' => 'eea9b8',
  405. 'pink3' => 'cd919e',
  406. 'pink4' => '8b636c',
  407. 'plum' => 'dda0dd',
  408. 'plum1' => 'ffbbff',
  409. 'plum2' => 'eeaeee',
  410. 'plum3' => 'cd96cd',
  411. 'plum4' => '8b668b',
  412. 'powderblue' => 'b0e0e6',
  413. 'purple' => 'a020f0',
  414. 'rebeccapurple' => '663399',
  415. 'purple1' => '9b30ff',
  416. 'purple2' => '912cee',
  417. 'purple3' => '7d26cd',
  418. 'purple4' => '551a8b',
  419. 'red' => 'ff0000',
  420. 'red1' => 'ff0000',
  421. 'red2' => 'ee0000',
  422. 'red3' => 'cd0000',
  423. 'red4' => '8b0000',
  424. 'rosybrown' => 'bc8f8f',
  425. 'rosybrown1' => 'ffc1c1',
  426. 'rosybrown2' => 'eeb4b4',
  427. 'rosybrown3' => 'cd9b9b',
  428. 'rosybrown4' => '8b6969',
  429. 'royalblue' => '4169e1',
  430. 'royalblue1' => '4876ff',
  431. 'royalblue2' => '436eee',
  432. 'royalblue3' => '3a5fcd',
  433. 'royalblue4' => '27408b',
  434. 'saddlebrown' => '8b4513',
  435. 'salmon' => 'fa8072',
  436. 'salmon1' => 'ff8c69',
  437. 'salmon2' => 'ee8262',
  438. 'salmon3' => 'cd7054',
  439. 'salmon4' => '8b4c39',
  440. 'sandybrown' => 'f4a460',
  441. 'seagreen1' => '54ff9f',
  442. 'seagreen2' => '4eee94',
  443. 'seagreen3' => '43cd80',
  444. 'seagreen4' => '2e8b57',
  445. 'seashell1' => 'fff5ee',
  446. 'seashell2' => 'eee5de',
  447. 'seashell3' => 'cdc5bf',
  448. 'seashell4' => '8b8682',
  449. 'sienna' => 'a0522d',
  450. 'sienna1' => 'ff8247',
  451. 'sienna2' => 'ee7942',
  452. 'sienna3' => 'cd6839',
  453. 'sienna4' => '8b4726',
  454. 'silver' => 'c0c0c0',
  455. 'skyblue' => '87ceeb',
  456. 'skyblue1' => '87ceff',
  457. 'skyblue2' => '7ec0ee',
  458. 'skyblue3' => '6ca6cd',
  459. 'skyblue4' => '4a708b',
  460. 'slateblue' => '6a5acd',
  461. 'slateblue1' => '836fff',
  462. 'slateblue2' => '7a67ee',
  463. 'slateblue3' => '6959cd',
  464. 'slateblue4' => '473c8b',
  465. 'slategray' => '708090',
  466. 'slategray1' => 'c6e2ff',
  467. 'slategray2' => 'b9d3ee',
  468. 'slategray3' => '9fb6cd',
  469. 'slategray4' => '6c7b8b',
  470. 'snow1' => 'fffafa',
  471. 'snow2' => 'eee9e9',
  472. 'snow3' => 'cdc9c9',
  473. 'snow4' => '8b8989',
  474. 'springgreen1' => '00ff7f',
  475. 'springgreen2' => '00ee76',
  476. 'springgreen3' => '00cd66',
  477. 'springgreen4' => '008b45',
  478. 'steelblue' => '4682b4',
  479. 'steelblue1' => '63b8ff',
  480. 'steelblue2' => '5cacee',
  481. 'steelblue3' => '4f94cd',
  482. 'steelblue4' => '36648b',
  483. 'tan' => 'd2b48c',
  484. 'tan1' => 'ffa54f',
  485. 'tan2' => 'ee9a49',
  486. 'tan3' => 'cd853f',
  487. 'tan4' => '8b5a2b',
  488. 'teal' => '008080',
  489. 'thistle' => 'd8bfd8',
  490. 'thistle1' => 'ffe1ff',
  491. 'thistle2' => 'eed2ee',
  492. 'thistle3' => 'cdb5cd',
  493. 'thistle4' => '8b7b8b',
  494. 'tomato1' => 'ff6347',
  495. 'tomato2' => 'ee5c42',
  496. 'tomato3' => 'cd4f39',
  497. 'tomato4' => '8b3626',
  498. 'turquoise' => '40e0d0',
  499. 'turquoise1' => '00f5ff',
  500. 'turquoise2' => '00e5ee',
  501. 'turquoise3' => '00c5cd',
  502. 'turquoise4' => '00868b',
  503. 'violet' => 'ee82ee',
  504. 'violetred' => 'd02090',
  505. 'violetred1' => 'ff3e96',
  506. 'violetred2' => 'ee3a8c',
  507. 'violetred3' => 'cd3278',
  508. 'violetred4' => '8b2252',
  509. 'wheat' => 'f5deb3',
  510. 'wheat1' => 'ffe7ba',
  511. 'wheat2' => 'eed8ae',
  512. 'wheat3' => 'cdba96',
  513. 'wheat4' => '8b7e66',
  514. 'white' => 'ffffff',
  515. 'whitesmoke' => 'f5f5f5',
  516. 'yellow' => 'ffff00',
  517. 'yellow1' => 'ffff00',
  518. 'yellow2' => 'eeee00',
  519. 'yellow3' => 'cdcd00',
  520. 'yellow4' => '8b8b00',
  521. 'yellowgreen' => '9acd32',
  522. );
  523. protected $face;
  524. protected $size;
  525. protected $color;
  526. protected $bold = false;
  527. protected $italic = false;
  528. protected $underline = false;
  529. protected $superscript = false;
  530. protected $subscript = false;
  531. protected $strikethrough = false;
  532. protected $startTagCallbacks = array(
  533. 'font' => 'startFontTag',
  534. 'b' => 'startBoldTag',
  535. 'strong' => 'startBoldTag',
  536. 'i' => 'startItalicTag',
  537. 'em' => 'startItalicTag',
  538. 'u' => 'startUnderlineTag',
  539. 'ins' => 'startUnderlineTag',
  540. 'del' => 'startStrikethruTag',
  541. 'sup' => 'startSuperscriptTag',
  542. 'sub' => 'startSubscriptTag',
  543. );
  544. protected $endTagCallbacks = array(
  545. 'font' => 'endFontTag',
  546. 'b' => 'endBoldTag',
  547. 'strong' => 'endBoldTag',
  548. 'i' => 'endItalicTag',
  549. 'em' => 'endItalicTag',
  550. 'u' => 'endUnderlineTag',
  551. 'ins' => 'endUnderlineTag',
  552. 'del' => 'endStrikethruTag',
  553. 'sup' => 'endSuperscriptTag',
  554. 'sub' => 'endSubscriptTag',
  555. 'br' => 'breakTag',
  556. 'p' => 'breakTag',
  557. 'h1' => 'breakTag',
  558. 'h2' => 'breakTag',
  559. 'h3' => 'breakTag',
  560. 'h4' => 'breakTag',
  561. 'h5' => 'breakTag',
  562. 'h6' => 'breakTag',
  563. );
  564. protected $stack = array();
  565. protected $stringData = '';
  566. protected $richTextObject;
  567. protected function initialise()
  568. {
  569. $this->face = $this->size = $this->color = null;
  570. $this->bold = $this->italic = $this->underline = $this->superscript = $this->subscript = $this->strikethrough = false;
  571. $this->stack = array();
  572. $this->stringData = '';
  573. }
  574. public function toRichTextObject($html)
  575. {
  576. $this->initialise();
  577. // Create a new DOM object
  578. $dom = new \DOMDocument;
  579. // Load the HTML file into the DOM object
  580. // Note the use of error suppression, because typically this will be an html fragment, so not fully valid markup
  581. $loaded = @$dom->loadHTML($html);
  582. // Discard excess white space
  583. $dom->preserveWhiteSpace = false;
  584. $this->richTextObject = new PHPExcel_RichText();;
  585. $this->parseElements($dom);
  586. // Clean any further spurious whitespace
  587. $this->cleanWhitespace();
  588. return $this->richTextObject;
  589. }
  590. protected function cleanWhitespace()
  591. {
  592. foreach ($this->richTextObject->getRichTextElements() as $key => $element) {
  593. $text = $element->getText();
  594. // Trim any leading spaces on the first run
  595. if ($key == 0) {
  596. $text = ltrim($text);
  597. }
  598. // Trim any spaces immediately after a line break
  599. $text = preg_replace('/\n */mu', "\n", $text);
  600. $element->setText($text);
  601. }
  602. }
  603. protected function buildTextRun()
  604. {
  605. $text = $this->stringData;
  606. if (trim($text) === '') {
  607. return;
  608. }
  609. $richtextRun = $this->richTextObject->createTextRun($this->stringData);
  610. if ($this->face) {
  611. $richtextRun->getFont()->setName($this->face);
  612. }
  613. if ($this->size) {
  614. $richtextRun->getFont()->setSize($this->size);
  615. }
  616. if ($this->color) {
  617. $richtextRun->getFont()->setColor(new PHPExcel_Style_Color('ff' . $this->color));
  618. }
  619. if ($this->bold) {
  620. $richtextRun->getFont()->setBold(true);
  621. }
  622. if ($this->italic) {
  623. $richtextRun->getFont()->setItalic(true);
  624. }
  625. if ($this->underline) {
  626. $richtextRun->getFont()->setUnderline(PHPExcel_Style_Font::UNDERLINE_SINGLE);
  627. }
  628. if ($this->superscript) {
  629. $richtextRun->getFont()->setSuperScript(true);
  630. }
  631. if ($this->subscript) {
  632. $richtextRun->getFont()->setSubScript(true);
  633. }
  634. if ($this->strikethrough) {
  635. $richtextRun->getFont()->setStrikethrough(true);
  636. }
  637. $this->stringData = '';
  638. }
  639. protected function rgbToColour($rgb)
  640. {
  641. preg_match_all('/\d+/', $rgb, $values);
  642. foreach ($values[0] as &$value) {
  643. $value = str_pad(dechex($value), 2, '0', STR_PAD_LEFT);
  644. }
  645. return implode($values[0]);
  646. }
  647. protected function colourNameLookup($rgb)
  648. {
  649. return self::$colourMap[$rgb];
  650. }
  651. protected function startFontTag($tag)
  652. {
  653. foreach ($tag->attributes as $attribute) {
  654. $attributeName = strtolower($attribute->name);
  655. $attributeValue = $attribute->value;
  656. if ($attributeName == 'color') {
  657. if (preg_match('/rgb\s*\(/', $attributeValue)) {
  658. $this->$attributeName = $this->rgbToColour($attributeValue);
  659. } elseif (strpos(trim($attributeValue), '#') === 0) {
  660. $this->$attributeName = ltrim($attributeValue, '#');
  661. } else {
  662. $this->$attributeName = $this->colourNameLookup($attributeValue);
  663. }
  664. } else {
  665. $this->$attributeName = $attributeValue;
  666. }
  667. }
  668. }
  669. protected function endFontTag()
  670. {
  671. $this->face = $this->size = $this->color = null;
  672. }
  673. protected function startBoldTag()
  674. {
  675. $this->bold = true;
  676. }
  677. protected function endBoldTag()
  678. {
  679. $this->bold = false;
  680. }
  681. protected function startItalicTag()
  682. {
  683. $this->italic = true;
  684. }
  685. protected function endItalicTag()
  686. {
  687. $this->italic = false;
  688. }
  689. protected function startUnderlineTag()
  690. {
  691. $this->underline = true;
  692. }
  693. protected function endUnderlineTag()
  694. {
  695. $this->underline = false;
  696. }
  697. protected function startSubscriptTag()
  698. {
  699. $this->subscript = true;
  700. }
  701. protected function endSubscriptTag()
  702. {
  703. $this->subscript = false;
  704. }
  705. protected function startSuperscriptTag()
  706. {
  707. $this->superscript = true;
  708. }
  709. protected function endSuperscriptTag()
  710. {
  711. $this->superscript = false;
  712. }
  713. protected function startStrikethruTag()
  714. {
  715. $this->strikethrough = true;
  716. }
  717. protected function endStrikethruTag()
  718. {
  719. $this->strikethrough = false;
  720. }
  721. protected function breakTag()
  722. {
  723. $this->stringData .= "\n";
  724. }
  725. protected function parseTextNode(DOMText $textNode)
  726. {
  727. $domText = preg_replace(
  728. '/\s+/u',
  729. ' ',
  730. str_replace(["\r", "\n"], ' ', $textNode->nodeValue)
  731. );
  732. $this->stringData .= $domText;
  733. $this->buildTextRun();
  734. }
  735. protected function handleCallback($element, $callbackTag, $callbacks)
  736. {
  737. if (isset($callbacks[$callbackTag])) {
  738. $elementHandler = $callbacks[$callbackTag];
  739. if (method_exists($this, $elementHandler)) {
  740. call_user_func(array($this, $elementHandler), $element);
  741. }
  742. }
  743. }
  744. protected function parseElementNode(DOMElement $element)
  745. {
  746. $callbackTag = strtolower($element->nodeName);
  747. $this->stack[] = $callbackTag;
  748. $this->handleCallback($element, $callbackTag, $this->startTagCallbacks);
  749. $this->parseElements($element);
  750. array_pop($this->stack);
  751. $this->handleCallback($element, $callbackTag, $this->endTagCallbacks);
  752. }
  753. protected function parseElements(DOMNode $element)
  754. {
  755. foreach ($element->childNodes as $child) {
  756. if ($child instanceof DOMText) {
  757. $this->parseTextNode($child);
  758. } elseif ($child instanceof DOMElement) {
  759. $this->parseElementNode($child);
  760. }
  761. }
  762. }
  763. }