styleHTML.class.php 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456
  1. <?php
  2. /**
  3. * Logiciel : HTML2PDF - classe styleHTML
  4. *
  5. * Convertisseur HTML => PDF, utilise fpdf de Olivier PLATHEY
  6. * Distribu� sous la licence LGPL.
  7. *
  8. * @author Laurent MINGUET <webmaster@spipu.net>
  9. * @version 3.24 - 05/08/2009
  10. */
  11. if (!defined('__CLASS_STYLEHTML__'))
  12. {
  13. define('__CLASS_STYLEHTML__', true);
  14. class styleHTML
  15. {
  16. var $css = array(); // tableau des CSS
  17. var $css_keys = array(); // tableau des clefs CSS, pour l'ordre d'execution
  18. var $value = array(); // valeurs actuelles
  19. var $table = array(); // tableau d'empilement pour historisation des niveaux
  20. var $pdf = null; // r�f�rence au PDF parent
  21. var $htmlColor = array(); // liste des couleurs HTML
  22. var $onlyLeft = false; // indique si on est dans un sous HTML et qu'on bloque � gauche
  23. /**
  24. * Constructeur
  25. *
  26. * @param &pdf r�f�rence � l'objet HTML2PDF parent
  27. * @return null
  28. */
  29. function styleHTML(&$pdf)
  30. {
  31. $this->init(); // initialisation
  32. $this->pdf = &$pdf;
  33. }
  34. /**
  35. * Initialisation du style
  36. *
  37. * @return null
  38. */
  39. function init()
  40. {
  41. $color = array();
  42. $color['AliceBlue'] = '#F0F8FF';
  43. $color['AntiqueWhite'] = '#FAEBD7';
  44. $color['Aqua'] = '#00FFFF';
  45. $color['Aquamarine'] = '#7FFFD4';
  46. $color['Azure'] = '#F0FFFF';
  47. $color['Beige'] = '#F5F5DC';
  48. $color['Bisque'] = '#FFE4C4';
  49. $color['Black'] = '#000000';
  50. $color['BlanchedAlmond'] = '#FFEBCD';
  51. $color['Blue'] = '#0000FF';
  52. $color['BlueViolet'] = '#8A2BE2';
  53. $color['Brown'] = '#A52A2A';
  54. $color['BurlyWood'] = '#DEB887';
  55. $color['CadetBlue'] = '#5F9EA0';
  56. $color['Chartreuse'] = '#7FFF00';
  57. $color['Chocolate'] = '#D2691E';
  58. $color['Coral'] = '#FF7F50';
  59. $color['CornflowerBlue'] = '#6495ED';
  60. $color['Cornsilk'] = '#FFF8DC';
  61. $color['Crimson'] = '#DC143C';
  62. $color['Cyan'] = '#00FFFF';
  63. $color['DarkBlue'] = '#00008B';
  64. $color['DarkCyan'] = '#008B8B';
  65. $color['DarkGoldenRod'] = '#B8860B';
  66. $color['DarkGray'] = '#A9A9A9';
  67. $color['DarkGrey'] = '#A9A9A9';
  68. $color['DarkGreen'] = '#006400';
  69. $color['DarkKhaki'] = '#BDB76B';
  70. $color['DarkMagenta'] = '#8B008B';
  71. $color['DarkOliveGreen'] = '#556B2F';
  72. $color['Darkorange'] = '#FF8C00';
  73. $color['DarkOrchid'] = '#9932CC';
  74. $color['DarkRed'] = '#8B0000';
  75. $color['DarkSalmon'] = '#E9967A';
  76. $color['DarkSeaGreen'] = '#8FBC8F';
  77. $color['DarkSlateBlue'] = '#483D8B';
  78. $color['DarkSlateGray'] = '#2F4F4F';
  79. $color['DarkSlateGrey'] = '#2F4F4F';
  80. $color['DarkTurquoise'] = '#00CED1';
  81. $color['DarkViolet'] = '#9400D3';
  82. $color['DeepPink'] = '#FF1493';
  83. $color['DeepSkyBlue'] = '#00BFFF';
  84. $color['DimGray'] = '#696969';
  85. $color['DimGrey'] = '#696969';
  86. $color['DodgerBlue'] = '#1E90FF';
  87. $color['FireBrick'] = '#B22222';
  88. $color['FloralWhite'] = '#FFFAF0';
  89. $color['ForestGreen'] = '#228B22';
  90. $color['Fuchsia'] = '#FF00FF';
  91. $color['Gainsboro'] = '#DCDCDC';
  92. $color['GhostWhite'] = '#F8F8FF';
  93. $color['Gold'] = '#FFD700';
  94. $color['GoldenRod'] = '#DAA520';
  95. $color['Gray'] = '#808080';
  96. $color['Grey'] = '#808080';
  97. $color['Green'] = '#008000';
  98. $color['GreenYellow'] = '#ADFF2F';
  99. $color['HoneyDew'] = '#F0FFF0';
  100. $color['HotPink'] = '#FF69B4';
  101. $color['IndianRed'] = '#CD5C5C';
  102. $color['Indigo'] = '#4B0082';
  103. $color['Ivory'] = '#FFFFF0';
  104. $color['Khaki'] = '#F0E68C';
  105. $color['Lavender'] = '#E6E6FA';
  106. $color['LavenderBlush'] = '#FFF0F5';
  107. $color['LawnGreen'] = '#7CFC00';
  108. $color['LemonChiffon'] = '#FFFACD';
  109. $color['LightBlue'] = '#ADD8E6';
  110. $color['LightCoral'] = '#F08080';
  111. $color['LightCyan'] = '#E0FFFF';
  112. $color['LightGoldenRodYellow'] = '#FAFAD2';
  113. $color['LightGray'] = '#D3D3D3';
  114. $color['LightGrey'] = '#D3D3D3';
  115. $color['LightGreen'] = '#90EE90';
  116. $color['LightPink'] = '#FFB6C1';
  117. $color['LightSalmon'] = '#FFA07A';
  118. $color['LightSeaGreen'] = '#20B2AA';
  119. $color['LightSkyBlue'] = '#87CEFA';
  120. $color['LightSlateGray'] = '#778899';
  121. $color['LightSlateGrey'] = '#778899';
  122. $color['LightSteelBlue'] = '#B0C4DE';
  123. $color['LightYellow'] = '#FFFFE0';
  124. $color['Lime'] = '#00FF00';
  125. $color['LimeGreen'] = '#32CD32';
  126. $color['Linen'] = '#FAF0E6';
  127. $color['Magenta'] = '#FF00FF';
  128. $color['Maroon'] = '#800000';
  129. $color['MediumAquaMarine'] = '#66CDAA';
  130. $color['MediumBlue'] = '#0000CD';
  131. $color['MediumOrchid'] = '#BA55D3';
  132. $color['MediumPurple'] = '#9370D8';
  133. $color['MediumSeaGreen'] = '#3CB371';
  134. $color['MediumSlateBlue'] = '#7B68EE';
  135. $color['MediumSpringGreen'] = '#00FA9A';
  136. $color['MediumTurquoise'] = '#48D1CC';
  137. $color['MediumVioletRed'] = '#C71585';
  138. $color['MidnightBlue'] = '#191970';
  139. $color['MintCream'] = '#F5FFFA';
  140. $color['MistyRose'] = '#FFE4E1';
  141. $color['Moccasin'] = '#FFE4B5';
  142. $color['NavajoWhite'] = '#FFDEAD';
  143. $color['Navy'] = '#000080';
  144. $color['OldLace'] = '#FDF5E6';
  145. $color['Olive'] = '#808000';
  146. $color['OliveDrab'] = '#6B8E23';
  147. $color['Orange'] = '#FFA500';
  148. $color['OrangeRed'] = '#FF4500';
  149. $color['Orchid'] = '#DA70D6';
  150. $color['PaleGoldenRod'] = '#EEE8AA';
  151. $color['PaleGreen'] = '#98FB98';
  152. $color['PaleTurquoise'] = '#AFEEEE';
  153. $color['PaleVioletRed'] = '#D87093';
  154. $color['PapayaWhip'] = '#FFEFD5';
  155. $color['PeachPuff'] = '#FFDAB9';
  156. $color['Peru'] = '#CD853F';
  157. $color['Pink'] = '#FFC0CB';
  158. $color['Plum'] = '#DDA0DD';
  159. $color['PowderBlue'] = '#B0E0E6';
  160. $color['Purple'] = '#800080';
  161. $color['Red'] = '#FF0000';
  162. $color['RosyBrown'] = '#BC8F8F';
  163. $color['RoyalBlue'] = '#4169E1';
  164. $color['SaddleBrown'] = '#8B4513';
  165. $color['Salmon'] = '#FA8072';
  166. $color['SandyBrown'] = '#F4A460';
  167. $color['SeaGreen'] = '#2E8B57';
  168. $color['SeaShell'] = '#FFF5EE';
  169. $color['Sienna'] = '#A0522D';
  170. $color['Silver'] = '#C0C0C0';
  171. $color['SkyBlue'] = '#87CEEB';
  172. $color['SlateBlue'] = '#6A5ACD';
  173. $color['SlateGray'] = '#708090';
  174. $color['SlateGrey'] = '#708090';
  175. $color['Snow'] = '#FFFAFA';
  176. $color['SpringGreen'] = '#00FF7F';
  177. $color['SteelBlue'] = '#4682B4';
  178. $color['Tan'] = '#D2B48C';
  179. $color['Teal'] = '#008080';
  180. $color['Thistle'] = '#D8BFD8';
  181. $color['Tomato'] = '#FF6347';
  182. $color['Turquoise'] = '#40E0D0';
  183. $color['Violet'] = '#EE82EE';
  184. $color['Wheat'] = '#F5DEB3';
  185. $color['White'] = '#FFFFFF';
  186. $color['WhiteSmoke'] = '#F5F5F5';
  187. $color['Yellow'] = '#FFFF00';
  188. $color['YellowGreen'] = '#9ACD32';
  189. $this->htmlColor = array();
  190. foreach($color as $key => $val) $this->htmlColor[strtolower($key)] = $val;
  191. unset($color);
  192. $this->table = array();
  193. $this->value = array();
  194. $this->initStyle();
  195. // initialisation des styles sans h�ritages
  196. $this->resetStyle();
  197. }
  198. function initStyle()
  199. {
  200. $this->value['id_balise'] = 'body'; // balise
  201. $this->value['id_name'] = null; // name
  202. $this->value['id_id'] = null; // id
  203. $this->value['id_class'] = null; // class
  204. $this->value['id_lst'] = array('*'); // lst de dependance
  205. $this->value['mini-size'] = 1.; // rapport de taille sp�cifique aux sup, sub
  206. $this->value['mini-decal'] = 0; // rapport de position sp�cifique aux sup, sub
  207. $this->value['font-family'] = 'Arial';
  208. $this->value['font-bold'] = false;
  209. $this->value['font-italic'] = false;
  210. $this->value['font-underline'] = false;
  211. $this->value['font-overline'] = false;
  212. $this->value['font-linethrough'] = false;
  213. $this->value['font-size'] = $this->ConvertToMM('10pt');
  214. $this->value['text-indent'] = 0;
  215. $this->value['text-align'] = 'left';
  216. $this->value['vertical-align'] = 'middle';
  217. $this->value['line-height'] = 'normal';
  218. $this->value['position'] = null;
  219. $this->value['x'] = null;
  220. $this->value['y'] = null;
  221. $this->value['width'] = 0;
  222. $this->value['height'] = 0;
  223. $this->value['top'] = null;
  224. $this->value['right'] = null;
  225. $this->value['bottom'] = null;
  226. $this->value['left'] = null;
  227. $this->value['float'] = null;
  228. $this->value['display'] = null;
  229. $this->value['color'] = array(0, 0, 0);
  230. $this->value['background'] = array('color' => null, 'image' => null, 'position' => null, 'repeat' => null);
  231. $this->value['border'] = array();
  232. $this->value['padding'] = array();
  233. $this->value['margin'] = array();
  234. $this->value['margin-auto'] = false;
  235. $this->value['list-style-type'] = '';
  236. $this->value['list-style-image'] = '';
  237. $this->value['xc'] = null;
  238. $this->value['yc'] = null;
  239. }
  240. /**
  241. * Initialisation des styles sans h�ritages
  242. *
  243. * @param string balise HTML
  244. * @return null
  245. */
  246. function resetStyle($balise = '')
  247. {
  248. $collapse = isset($this->value['border']['collapse']) ? $this->value['border']['collapse'] : false;
  249. if (!in_array($balise, array('tr', 'td', 'th'))) $collapse = false;
  250. $this->value['position'] = null;
  251. $this->value['x'] = null;
  252. $this->value['y'] = null;
  253. $this->value['width'] = 0;
  254. $this->value['height'] = 0;
  255. $this->value['top'] = null;
  256. $this->value['right'] = null;
  257. $this->value['bottom'] = null;
  258. $this->value['left'] = null;
  259. $this->value['float'] = null;
  260. $this->value['display'] = null;
  261. $this->value['background'] = array('color' => null, 'image' => null, 'position' => null, 'repeat' => null);
  262. $this->value['border'] = array(
  263. 't' => $this->readBorder('none'),
  264. 'r' => $this->readBorder('none'),
  265. 'b' => $this->readBorder('none'),
  266. 'l' => $this->readBorder('none'),
  267. 'radius' => array(0, 0),
  268. 'collapse' => $collapse,
  269. );
  270. $this->value['margin'] = array(
  271. 't' => 0,
  272. 'r' => 0,
  273. 'b' => 0,
  274. 'l' => 0
  275. );
  276. $this->value['margin-auto'] = false;
  277. if (in_array($balise, array('div')))
  278. $this->value['vertical-align'] = 'top';
  279. if (in_array($balise, array('ul', 'li')))
  280. {
  281. $this->value['list-style-type'] = '';
  282. $this->value['list-style-image'] = '';
  283. }
  284. if (!in_array($balise, array('tr', 'td')))
  285. {
  286. $this->value['padding'] = array(
  287. 't' => 0,
  288. 'r' => 0,
  289. 'b' => 0,
  290. 'l' => 0
  291. );
  292. }
  293. else
  294. {
  295. $this->value['padding'] = array(
  296. 't' => $this->ConvertToMM('1px'),
  297. 'r' => $this->ConvertToMM('1px'),
  298. 'b' => $this->ConvertToMM('1px'),
  299. 'l' => $this->ConvertToMM('1px')
  300. );
  301. }
  302. if ($balise=='hr')
  303. {
  304. $this->value['border'] = array(
  305. 't' => $this->readBorder('solid 1px #000000'),
  306. 'r' => $this->readBorder('solid 1px #000000'),
  307. 'b' => $this->readBorder('solid 1px #000000'),
  308. 'l' => $this->readBorder('solid 1px #000000'),
  309. 'radius' => array(0, 0),
  310. 'collapse' => false,
  311. );
  312. $this->ConvertBackground('#FFFFFF', $this->value['background']);
  313. }
  314. $this->value['xc'] = null;
  315. $this->value['yc'] = null;
  316. }
  317. /**
  318. * Initialisation de la font PDF
  319. *
  320. * @return null
  321. */
  322. function FontSet()
  323. {
  324. $b = ($this->value['font-bold'] ? 'B' : '');
  325. $i = ($this->value['font-italic'] ? 'I' : '');
  326. $u = ($this->value['font-underline'] ? 'U' : '');
  327. // taille en mm, � ramener en pt
  328. $size = $this->value['font-size'];
  329. $size = 72 * $size / 25.4;
  330. $this->pdf->setOverline($this->value['font-overline']);
  331. $this->pdf->setLinethrough($this->value['font-linethrough']);
  332. // application de la fonte
  333. $this->pdf->SetFont($this->value['font-family'], $b.$i.$u, $this->value['mini-size']*$size);
  334. $this->pdf->SetTextColor($this->value['color'][0],$this->value['color'][1], $this->value['color'][2]);
  335. if ($this->value['background']['color'])
  336. $this->pdf->SetFillColor($this->value['background']['color'][0],$this->value['background']['color'][1], $this->value['background']['color'][2]);
  337. else
  338. $this->pdf->SetFillColor(255);
  339. }
  340. /**
  341. * Monter d'un niveau dans l'historisation
  342. *
  343. * @return null
  344. */
  345. function save()
  346. {
  347. $this->table[count($this->table)] = $this->value;
  348. }
  349. /**
  350. * Descendre d'un niveau dans l'historisation
  351. *
  352. * @return null
  353. */
  354. function load()
  355. {
  356. if (count($this->table))
  357. {
  358. $this->value = $this->table[count($this->table)-1];
  359. unset($this->table[count($this->table)-1]);
  360. }
  361. }
  362. function restorePosition(&$current_x, &$current_y)
  363. {
  364. if ($this->value['y']==$current_y) $current_y = $this->value['yc'];
  365. }
  366. function setPosition(&$current_x, &$current_y)
  367. {
  368. $this->value['xc'] = $current_x;
  369. $this->value['yc'] = $current_y;
  370. if ($this->value['position']=='relative' || $this->value['position']=='absolute')
  371. {
  372. if ($this->value['right']!==null)
  373. {
  374. $x = $this->getLastWidth(true) - $this->value['right'] - $this->value['width'];
  375. if ($this->value['margin']['r']) $x-= $this->value['margin']['r'];
  376. }
  377. else
  378. {
  379. $x = $this->value['left'];
  380. if ($this->value['margin']['l']) $x+= $this->value['margin']['l'];
  381. }
  382. if ($this->value['bottom']!==null)
  383. {
  384. $y = $this->getLastHeight(true) - $this->value['bottom'] - $this->value['height'];
  385. if ($this->value['margin']['b']) $y-= $this->value['margin']['b'];
  386. }
  387. else
  388. {
  389. $y = $this->value['top'];
  390. if ($this->value['margin']['t']) $y+= $this->value['margin']['t'];
  391. }
  392. if ($this->value['position']=='relative')
  393. {
  394. $this->value['x'] = $current_x + $x;
  395. $this->value['y'] = $current_y + $y;
  396. }
  397. else
  398. {
  399. $this->value['x'] = $this->getLastAbsoluteX()+$x;
  400. $this->value['y'] = $this->getLastAbsoluteY()+$y;
  401. }
  402. }
  403. else
  404. {
  405. $this->value['x'] = $current_x;
  406. $this->value['y'] = $current_y;
  407. if ($this->value['margin']['l']) $this->value['x']+= $this->value['margin']['l'];
  408. if ($this->value['margin']['t']) $this->value['y']+= $this->value['margin']['t'];
  409. }
  410. $current_x = $this->value['x'];
  411. $current_y = $this->value['y'];
  412. }
  413. /**
  414. * Analyse un tableau de style provenant du parseurHTML
  415. *
  416. * @param string nom de la balise
  417. * @param array tableau de style
  418. * @return null
  419. */
  420. function analyse($balise, &$param)
  421. {
  422. // preparation
  423. $balise = strtolower($balise);
  424. $id = isset($param['id']) ? strtolower(trim($param['id'])) : null; if (!$id) $id = null;
  425. $name = isset($param['name']) ? strtolower(trim($param['name'])) : null; if (!$name) $name = null;
  426. // lecture de la propriete classe
  427. $class = array();
  428. $tmp = isset($param['class']) ? preg_replace('/[\s]+/', ' ', strtolower($param['class'])) : '';
  429. $tmp = explode(' ', $tmp);
  430. foreach($tmp as $k => $v)
  431. {
  432. $v = trim($v);
  433. if ($v) $class[] = $v;
  434. }
  435. // identification de la balise et des styles direct qui pourraient lui �tre appliqu�s
  436. $this->value['id_balise'] = $balise;
  437. $this->value['id_name'] = $name;
  438. $this->value['id_id'] = $id;
  439. $this->value['id_class'] = $class;
  440. $this->value['id_lst'] = array();
  441. $this->value['id_lst'][] = '*';
  442. $this->value['id_lst'][] = $balise;
  443. if (count($class))
  444. {
  445. foreach($class as $v)
  446. {
  447. $this->value['id_lst'][] = '*.'.$v;
  448. $this->value['id_lst'][] = '.'.$v;
  449. $this->value['id_lst'][] = $balise.'.'.$v;
  450. }
  451. }
  452. if ($id)
  453. {
  454. $this->value['id_lst'][] = '*#'.$id;
  455. $this->value['id_lst'][] = '#'.$id;
  456. $this->value['id_lst'][] = $id.'#'.$id;
  457. }
  458. // style CSS
  459. $styles = $this->getFromCSS();
  460. // on ajoute le style propre � la balise
  461. $styles = array_merge($styles, $param['style']);
  462. if (isset($param['allwidth']) && !isset($styles['width'])) $styles['width'] = '100%';
  463. // mise � zero des styles non h�rit�s
  464. $this->resetStyle($balise);
  465. // interpreration des nouvelles valeurs
  466. $correct_width = false;
  467. $no_width = true;
  468. foreach($styles as $nom => $val)
  469. {
  470. switch($nom)
  471. {
  472. case 'font-family':
  473. $val = explode(',', $val);
  474. $val = trim($val[0]);
  475. if ($val) $this->value['font-family'] = $val;
  476. break;
  477. case 'font-weight':
  478. $this->value['font-bold'] = ($val=='bold');
  479. break;
  480. case 'font-style':
  481. $this->value['font-italic'] = ($val=='italic');
  482. break;
  483. case 'text-decoration':
  484. $val = explode(' ', $val);
  485. $this->value['font-underline'] = (in_array('underline', $val));
  486. $this->value['font-overline'] = (in_array('overline', $val));
  487. $this->value['font-linethrough'] = (in_array('line-through', $val));
  488. break;
  489. case 'text-indent':
  490. $this->value['text-indent'] = $this->ConvertToMM($val);
  491. break;
  492. case 'font-size':
  493. $val = $this->ConvertToMM($val, $this->value['font-size']);
  494. if ($val) $this->value['font-size'] = $val;
  495. break;
  496. case 'color':
  497. $res = null;
  498. $this->value['color'] = $this->ConvertToRVB($val, $res);
  499. if ($balise=='hr')
  500. {
  501. $this->value['border']['l']['color'] = $this->value['color'];
  502. $this->value['border']['t']['color'] = $this->value['color'];
  503. $this->value['border']['r']['color'] = $this->value['color'];
  504. $this->value['border']['b']['color'] = $this->value['color'];
  505. }
  506. break;
  507. case 'text-align':
  508. $this->value['text-align'] = $val;
  509. break;
  510. case 'vertical-align':
  511. $this->value['vertical-align'] = $val;
  512. break;
  513. case 'width':
  514. $this->value['width'] = $this->ConvertToMM($val, $this->getLastWidth());
  515. if ($this->value['width'] && substr($val, -1)=='%') $correct_width=true;
  516. $no_width = false;
  517. break;
  518. case 'height':
  519. $this->value['height'] = $this->ConvertToMM($val, $this->getLastHeight());
  520. break;
  521. case 'line-height':
  522. if (preg_match('/^[0-9\.]+$/isU', $val)) $val = floor($val*100).'%';
  523. $this->value['line-height'] = $val;
  524. break;
  525. case 'padding':
  526. $val = explode(' ', $val);
  527. foreach($val as $k => $v)
  528. {
  529. $v = trim($v);
  530. if ($v!='') $val[$k] = $v;
  531. else unset($val[$k]);
  532. }
  533. $val = array_values($val);
  534. if (count($val)!=4)
  535. {
  536. $val = $this->ConvertToMM($val[0], 0);
  537. $this->value['padding']['t'] = $val;
  538. $this->value['padding']['r'] = $val;
  539. $this->value['padding']['b'] = $val;
  540. $this->value['padding']['l'] = $val;
  541. }
  542. else
  543. {
  544. $this->value['padding']['t'] = $this->ConvertToMM($val[0], 0);
  545. $this->value['padding']['r'] = $this->ConvertToMM($val[1], 0);
  546. $this->value['padding']['b'] = $this->ConvertToMM($val[2], 0);
  547. $this->value['padding']['l'] = $this->ConvertToMM($val[3], 0);
  548. }
  549. break;
  550. case 'padding-top':
  551. $this->value['padding']['t'] = $this->ConvertToMM($val, 0);
  552. break;
  553. case 'padding-right':
  554. $this->value['padding']['r'] = $this->ConvertToMM($val, 0);
  555. break;
  556. case 'padding-bottom':
  557. $this->value['padding']['b'] = $this->ConvertToMM($val, 0);
  558. break;
  559. case 'padding-left':
  560. $this->value['padding']['l'] = $this->ConvertToMM($val, 0);
  561. break;
  562. case 'margin':
  563. if ($val=='auto')
  564. {
  565. $this->value['margin-auto'] = true;
  566. break;
  567. }
  568. $val = explode(' ', $val);
  569. foreach($val as $k => $v)
  570. {
  571. $v = trim($v);
  572. if ($v!='') $val[$k] = $v;
  573. else unset($val[$k]);
  574. }
  575. $val = array_values($val);
  576. if (count($val)!=4)
  577. {
  578. $val = $this->ConvertToMM($val[0], 0);
  579. $this->value['margin']['t'] = $val;
  580. $this->value['margin']['r'] = $val;
  581. $this->value['margin']['b'] = $val;
  582. $this->value['margin']['l'] = $val;
  583. }
  584. else
  585. {
  586. $this->value['margin']['t'] = $this->ConvertToMM($val[0], 0);
  587. $this->value['margin']['r'] = $this->ConvertToMM($val[1], 0);
  588. $this->value['margin']['b'] = $this->ConvertToMM($val[2], 0);
  589. $this->value['margin']['l'] = $this->ConvertToMM($val[3], 0);
  590. }
  591. break;
  592. case 'margin-top':
  593. $this->value['margin']['t'] = $this->ConvertToMM($val, 0);
  594. break;
  595. case 'margin-right':
  596. $this->value['margin']['r'] = $this->ConvertToMM($val, 0);
  597. break;
  598. case 'margin-bottom':
  599. $this->value['margin']['b'] = $this->ConvertToMM($val, 0);
  600. break;
  601. case 'margin-left':
  602. $this->value['margin']['l'] = $this->ConvertToMM($val, 0);
  603. break;
  604. case 'border':
  605. $val = $this->readBorder($val);
  606. $this->value['border']['t'] = $val;
  607. $this->value['border']['r'] = $val;
  608. $this->value['border']['b'] = $val;
  609. $this->value['border']['l'] = $val;
  610. break;
  611. case 'border-style':
  612. $val = explode(' ', $val);
  613. foreach($val as $val_k => $val_v)
  614. if (!in_array($val_v, array('solid', 'dotted', 'dashed')))
  615. $val[$val_k] = null;
  616. $this->duplicateBorder($val);
  617. if ($val[0]) $this->value['border']['t']['type'] = $val[0];
  618. if ($val[1]) $this->value['border']['r']['type'] = $val[1];
  619. if ($val[2]) $this->value['border']['b']['type'] = $val[2];
  620. if ($val[3]) $this->value['border']['l']['type'] = $val[3];
  621. break;
  622. case 'border-top-style':
  623. if (in_array($val, array('solid', 'dotted', 'dashed')))
  624. $this->value['border']['t']['type'] = $val;
  625. break;
  626. case 'border-right-style':
  627. if (in_array($val, array('solid', 'dotted', 'dashed')))
  628. $this->value['border']['r']['type'] = $val;
  629. break;
  630. case 'border-bottom-style':
  631. if (in_array($val, array('solid', 'dotted', 'dashed')))
  632. $this->value['border']['b']['type'] = $val;
  633. break;
  634. case 'border-left-style':
  635. if (in_array($val, array('solid', 'dotted', 'dashed')))
  636. $this->value['border']['l']['type'] = $val;
  637. break;
  638. case 'border-color':
  639. $res = false;
  640. $val = preg_replace('/,[\s]+/', ',', $val);
  641. $val = explode(' ', $val);
  642. foreach($val as $val_k => $val_v)
  643. {
  644. $val[$val_k] = $this->ConvertToRVB($val_v, $res);
  645. if (!$res) $val[$val_k] = null;
  646. }
  647. $this->duplicateBorder($val);
  648. if (is_array($val[0])) $this->value['border']['t']['color'] = $val[0];
  649. if (is_array($val[1])) $this->value['border']['r']['color'] = $val[1];
  650. if (is_array($val[2])) $this->value['border']['b']['color'] = $val[2];
  651. if (is_array($val[3])) $this->value['border']['l']['color'] = $val[3];
  652. break;
  653. case 'border-top-color':
  654. $res = false;
  655. $val = $this->ConvertToRVB($val, $res);
  656. if ($res) $this->value['border']['t']['color'] = $val;
  657. break;
  658. case 'border-right-color':
  659. $res = false;
  660. $val = $this->ConvertToRVB($val, $res);
  661. if ($res) $this->value['border']['r']['color'] = $val;
  662. break;
  663. case 'border-bottom-color':
  664. $res = false;
  665. $val = $this->ConvertToRVB($val, $res);
  666. if ($res) $this->value['border']['b']['color'] = $val;
  667. break;
  668. case 'border-left-color':
  669. $res = false;
  670. $val = $this->ConvertToRVB($val, $res);
  671. if ($res) $this->value['border']['l']['color'] = $val;
  672. break;
  673. case 'border-width':
  674. $val = explode(' ', $val);
  675. foreach($val as $val_k => $val_v)
  676. {
  677. $val[$val_k] = $this->ConvertToMM($val_v, 0);
  678. }
  679. $this->duplicateBorder($val);
  680. if ($val[0]) $this->value['border']['t']['width'] = $val[0];
  681. if ($val[1]) $this->value['border']['r']['width'] = $val[1];
  682. if ($val[2]) $this->value['border']['b']['width'] = $val[2];
  683. if ($val[3]) $this->value['border']['l']['width'] = $val[3];
  684. break;
  685. case 'border-top-width':
  686. $val = $this->ConvertToMM($val, 0);;
  687. if ($val) $this->value['border']['t']['width'] = $val;
  688. break;
  689. case 'border-right-width':
  690. $val = $this->ConvertToMM($val, 0);;
  691. if ($val) $this->value['border']['r']['width'] = $val;
  692. break;
  693. case 'border-bottom-width':
  694. $val = $this->ConvertToMM($val, 0);;
  695. if ($val) $this->value['border']['b']['width'] = $val;
  696. break;
  697. case 'border-left-width':
  698. $val = $this->ConvertToMM($val, 0);;
  699. if ($val) $this->value['border']['l']['width'] = $val;
  700. break;
  701. case 'border-collapse':
  702. if ($balise=='table') $this->value['border']['collapse'] = ($val=='collapse');
  703. break;
  704. case 'border-radius':
  705. // nettoyage des valeurs
  706. $val = explode(' ', $val);
  707. foreach($val as $k => $v)
  708. {
  709. $v = trim($v);
  710. if ($v)
  711. {
  712. $v = $this->ConvertToMM($v, 0);
  713. if ($v) $val[$k] = $v;
  714. else unset($val[$k]);
  715. }
  716. else unset($val[$k]);
  717. }
  718. $val = array_values($val);
  719. if (!isset($val[1]) && isset($val[0])) $val[1] = $val[0];
  720. if (count($val)==2)
  721. $this->value['border']['radius'] = array($val[0], $val[1]);
  722. break;
  723. case 'border-top':
  724. $this->value['border']['t'] = $this->readBorder($val);
  725. break;
  726. case 'border-right':
  727. $this->value['border']['r'] = $this->readBorder($val);
  728. break;
  729. case 'border-bottom':
  730. $this->value['border']['b'] = $this->readBorder($val);
  731. break;
  732. case 'border-left':
  733. $this->value['border']['l'] = $this->readBorder($val);
  734. break;
  735. case 'background-color':
  736. $this->value['background']['color'] = $this->ConvertBackgroundColor($val);
  737. break;
  738. case 'background-image':
  739. $this->value['background']['image'] = $this->ConvertBackgroundImage($val);
  740. break;
  741. case 'background-position':
  742. $res = null;
  743. $this->value['background']['position'] = $this->ConvertBackgroundPosition($val, $res);
  744. break;
  745. case 'background-repeat':
  746. $this->value['background']['repeat'] = $this->ConvertBackgroundRepeat($val);
  747. break;
  748. case 'background':
  749. $this->ConvertBackground($val, $this->value['background']);
  750. break;
  751. case 'position':
  752. if ($val=='absolute') $this->value['position'] = 'absolute';
  753. else if ($val=='relative') $this->value['position'] = 'relative';
  754. else $this->value['position'] = null;
  755. break;
  756. case 'float':
  757. if ($val=='left') $this->value['float'] = 'left';
  758. else if ($val=='right') $this->value['float'] = 'right';
  759. else $this->value['float'] = null;
  760. break;
  761. case 'display':
  762. if ($val=='inline') $this->value['display'] = 'inline';
  763. else if ($val=='block') $this->value['display'] = 'block';
  764. else if ($val=='none') $this->value['display'] = 'none';
  765. else $this->value['display'] = null;
  766. break;
  767. case 'top':
  768. case 'bottom':
  769. case 'left':
  770. case 'right':
  771. $this->value[$nom] = $val;
  772. break;
  773. case 'list-style':
  774. case 'list-style-type':
  775. case 'list-style-image':
  776. if ($nom=='list-style') $nom = 'list-style-type';
  777. $this->value[$nom] = $val;
  778. break;
  779. default:
  780. break;
  781. }
  782. }
  783. if ($this->onlyLeft) $this->value['text-align'] = 'left';
  784. // correction de la largeur pour correspondre au mod�le de boite quick
  785. if ($no_width && in_array($balise, array('div')) && $this->value['position']!='absolute')
  786. {
  787. $this->value['width'] = $this->getLastWidth();
  788. $this->value['width']-= $this->value['margin']['l'] + $this->value['margin']['r'];
  789. }
  790. else
  791. {
  792. if ($correct_width)
  793. {
  794. if (!in_array($balise, array('table', 'div', 'hr')))
  795. {
  796. $this->value['width']-= $this->value['padding']['l'] + $this->value['padding']['r'];
  797. $this->value['width']-= $this->value['border']['l']['width'] + $this->value['border']['r']['width'];
  798. }
  799. if (in_array($balise, array('th', 'td')))
  800. {
  801. $this->value['width']-= $this->ConvertToMM(isset($param['cellspacing']) ? $param['cellspacing'] : '2px');
  802. }
  803. if ($this->value['width']<0) $this->value['width']=0;
  804. }
  805. else
  806. {
  807. if ($this->value['width'])
  808. {
  809. if ($this->value['border']['l']['width']) $this->value['width'] += $this->value['border']['l']['width'];
  810. if ($this->value['border']['r']['width']) $this->value['width'] += $this->value['border']['r']['width'];
  811. if ($this->value['padding']['l']) $this->value['width'] += $this->value['padding']['l'];
  812. if ($this->value['padding']['r']) $this->value['width'] += $this->value['padding']['r'];
  813. }
  814. }
  815. }
  816. if ($this->value['height'])
  817. {
  818. if ($this->value['border']['b']['width']) { $this->value['height'] += $this->value['border']['b']['width']; }
  819. if ($this->value['border']['t']['width']) { $this->value['height'] += $this->value['border']['t']['width']; }
  820. if ($this->value['padding']['b']) $this->value['height'] += $this->value['padding']['b'];
  821. if ($this->value['padding']['t']) $this->value['height'] += $this->value['padding']['t'];
  822. }
  823. if ($this->value['top']!=null) $this->value['top'] = $this->ConvertToMM($this->value['top'], $this->getLastHeight(true));
  824. if ($this->value['bottom']!=null) $this->value['bottom'] = $this->ConvertToMM($this->value['bottom'], $this->getLastHeight(true));
  825. if ($this->value['left']!=null) $this->value['left'] = $this->ConvertToMM($this->value['left'], $this->getLastWidth(true));
  826. if ($this->value['right']!=null) $this->value['right'] = $this->ConvertToMM($this->value['right'], $this->getLastWidth(true));
  827. if ($this->value['top'] && $this->value['bottom'] && $this->value['height']) $this->value['bottom'] = null;
  828. if ($this->value['left'] && $this->value['right'] && $this->value['width']) $this->value['right'] = null;
  829. }
  830. /**
  831. * R�cup�ration de la hauteur de ligne courante
  832. *
  833. * @return float hauteur en mm
  834. */
  835. function getLineHeight()
  836. {
  837. $val = $this->value['line-height'];
  838. if ($val=='normal') $val = '108%';
  839. return $this->ConvertToMM($val, $this->value['font-size']);
  840. }
  841. /**
  842. * R�cup�ration de la largeur de l'objet parent
  843. *
  844. * @return float largeur
  845. */
  846. function getLastWidth($mode = false)
  847. {
  848. for($k=count($this->table); $k>0; $k--)
  849. {
  850. if ($this->table[$k-1]['width'])
  851. {
  852. $w = $this->table[$k-1]['width'];
  853. if ($mode)
  854. {
  855. $w+= $this->table[$k-1]['border']['l']['width'] + $this->table[$k-1]['padding']['l']+0.02;
  856. $w+= $this->table[$k-1]['border']['r']['width'] + $this->table[$k-1]['padding']['r']+0.02;
  857. }
  858. return $w;
  859. }
  860. }
  861. return $this->pdf->w - $this->pdf->lMargin - $this->pdf->rMargin;
  862. }
  863. /**
  864. * R�cup�ration de la hauteur de l'objet parent
  865. *
  866. * @return float hauteur
  867. */
  868. function getLastHeight($mode = false)
  869. {
  870. for($k=count($this->table); $k>0; $k--)
  871. {
  872. if ($this->table[$k-1]['height'])
  873. {
  874. $h = $this->table[$k-1]['height'];
  875. if ($mode)
  876. {
  877. $h+= $this->table[$k-1]['border']['t']['width'] + $this->table[$k-1]['padding']['t']+0.02;
  878. $h+= $this->table[$k-1]['border']['b']['width'] + $this->table[$k-1]['padding']['b']+0.02;
  879. }
  880. return $h;
  881. }
  882. }
  883. return $this->pdf->h - $this->pdf->tMargin - $this->pdf->bMargin;
  884. }
  885. function getFloat()
  886. {
  887. if ($this->value['float']=='left') return 'left';
  888. if ($this->value['float']=='right') return 'right';
  889. return null;
  890. }
  891. function getLastAbsoluteX()
  892. {
  893. for($k=count($this->table); $k>0; $k--)
  894. {
  895. if ($this->table[$k-1]['x'] && $this->table[$k-1]['position']) return $this->table[$k-1]['x'];
  896. }
  897. return $this->pdf->lMargin;
  898. }
  899. function getLastAbsoluteY()
  900. {
  901. for($k=count($this->table); $k>0; $k--)
  902. {
  903. if ($this->table[$k-1]['y'] && $this->table[$k-1]['position']) return $this->table[$k-1]['y'];
  904. }
  905. return $this->pdf->tMargin;
  906. }
  907. /**
  908. * R�cup�ration des propri�t�s CSS de la balise en cours
  909. *
  910. * @return array() tableau des propri�t�s CSS
  911. */
  912. function getFromCSS()
  913. {
  914. $styles = array(); // style � appliquer
  915. $getit = array(); // styles � r�cuperer
  916. // identification des styles direct, et ceux des parents
  917. $lst = array();
  918. $lst[] = $this->value['id_lst'];
  919. for($i=count($this->table)-1; $i>=0; $i--) $lst[] = $this->table[$i]['id_lst'];
  920. // identification des styles � r�cuperer
  921. foreach($this->css_keys as $key => $num)
  922. if ($this->getReccursiveStyle($key, $lst))
  923. $getit[$key] = $num;
  924. // si des styles sont � recuperer
  925. if (count($getit))
  926. {
  927. // on les r�cup�re, mais dans l'odre de d�finition, afin de garder les priorit�s
  928. asort($getit);
  929. foreach($getit as $key => $val) $styles = array_merge($styles, $this->css[$key]);
  930. }
  931. return $styles;
  932. }
  933. /**
  934. * Identification des styles � r�cuperer, en fonction de la balise et de ses parents
  935. *
  936. * @param string clef CSS � analyser
  937. * @param array() tableau des styles direct, et ceux des parents
  938. * @param string prochaine etape
  939. * @return boolean clef autoris�e ou non
  940. */
  941. function getReccursiveStyle($key, $lst, $next = null)
  942. {
  943. // si propchaine etape, on construit les valeurs
  944. if ($next!==null)
  945. {
  946. if ($next) $key = trim(substr($key, 0, -strlen($next))); // on el�ve cette etape
  947. unset($lst[0]);
  948. if (!count($lst)) return false; // pas d'etape possible
  949. $lst = array_values($lst);
  950. }
  951. // pour chaque style direct possible de l'etape en cours
  952. foreach($lst[0] as $nom)
  953. {
  954. if ($key==$nom) return true; // si la clef conrrespond => ok
  955. if (substr($key, -strlen(' '.$nom))==' '.$nom && $this->getReccursiveStyle($key, $lst, $nom)) return true; // si la clef est la fin, on analyse ce qui pr�c�de
  956. }
  957. // si on est pas � la premiere etape, on doit analyse toutes les sous etapes
  958. if ($next!==null && $this->getReccursiveStyle($key, $lst, '')) return true;
  959. // aucun style trouv�
  960. return false;
  961. }
  962. /**
  963. * Analyse d'une propri�t� Border
  964. *
  965. * @param string propri�t� border
  966. * @return array() propri�t� d�cod�e
  967. */
  968. function readBorder($val)
  969. {
  970. $none = array('type' => 'none', 'width' => 0, 'color' => array(0, 0, 0));
  971. // valeurs par d�fault
  972. $type = 'solid';
  973. $width = $this->ConvertToMM('1pt');
  974. $color = array(0, 0, 0);
  975. // nettoyage des valeurs
  976. $val = explode(' ', $val);
  977. foreach($val as $k => $v)
  978. {
  979. $v = trim($v);
  980. if ($v) $val[$k] = $v;
  981. else unset($val[$k]);
  982. }
  983. $val = array_values($val);
  984. // identification des valeurs
  985. $res = null;
  986. foreach($val as $key)
  987. {
  988. if ($key=='none' || $key=='hidden') return $none;
  989. if ($this->ConvertToMM($key)!==null) $width = $this->ConvertToMM($key);
  990. else if (in_array($key, array('solid', 'dotted', 'dashed'))) $type = $key;
  991. else
  992. {
  993. $tmp = $this->ConvertToRVB($key, $res);
  994. if ($res) $color = $tmp;
  995. }
  996. }
  997. if (!$width) return $none;
  998. return array('type' => $type, 'width' => $width, 'color' => $color);
  999. }
  1000. function duplicateBorder(&$val)
  1001. {
  1002. if (count($val)==1)
  1003. {
  1004. $val[1] = $val[0];
  1005. $val[2] = $val[0];
  1006. $val[3] = $val[0];
  1007. }
  1008. else if (count($val)==2)
  1009. {
  1010. $val[2] = $val[0];
  1011. $val[3] = $val[1];
  1012. }
  1013. else if (count($val)==3)
  1014. {
  1015. $val[3] = $val[1];
  1016. }
  1017. }
  1018. function ConvertBackground($stl, &$res)
  1019. {
  1020. // Image
  1021. $text = '/url\(([^)]*)\)/isU';
  1022. if (preg_match($text, $stl, $match))
  1023. {
  1024. $res['image'] = $this->ConvertBackgroundImage($match[0]);
  1025. $stl = preg_replace($text, '', $stl);
  1026. $stl = preg_replace('/[\s]+/', ' ', $stl);
  1027. }
  1028. // protection des espaces
  1029. $stl = preg_replace('/,[\s]+/', ',', $stl);
  1030. $lst = explode(' ', $stl);
  1031. $pos = '';
  1032. foreach($lst as $val)
  1033. {
  1034. $ok = false;
  1035. $color = $this->ConvertToRVB($val, $ok);
  1036. if ($ok)
  1037. {
  1038. $res['color'] = $color;
  1039. }
  1040. else if ($val=='transparent')
  1041. {
  1042. $res['color'] = null;
  1043. }
  1044. else
  1045. {
  1046. $repeat = $this->ConvertBackgroundRepeat($val);
  1047. if ($repeat)
  1048. {
  1049. $res['repeat'] = $repeat;
  1050. }
  1051. else
  1052. {
  1053. $pos.= ($pos ? ' ' : '').$val;
  1054. }
  1055. }
  1056. }
  1057. if ($pos)
  1058. {
  1059. $pos = $this->ConvertBackgroundPosition($pos, $ok);
  1060. if ($ok) $res['position'] = $pos;
  1061. }
  1062. }
  1063. function ConvertBackgroundColor($val)
  1064. {
  1065. $res = null;
  1066. if ($val=='transparent') return null;
  1067. else return $this->ConvertToRVB($val, $res);
  1068. }
  1069. function ConvertBackgroundImage($val)
  1070. {
  1071. if ($val=='none')
  1072. return null;
  1073. else if (preg_match('/^url\(([^)]*)\)$/isU', $val, $match))
  1074. return $match[1];
  1075. else
  1076. return null;
  1077. }
  1078. function ConvertBackgroundPosition($val, &$res)
  1079. {
  1080. $val = explode(' ', $val);
  1081. if (count($val)<2)
  1082. {
  1083. if (!$val[0]) return null;
  1084. $val[1] = 'center';
  1085. }
  1086. if (count($val)>2) return null;
  1087. $x = 0;
  1088. $y = 0;
  1089. $res = true;
  1090. if ($val[0]=='left') $x = '0%';
  1091. else if ($val[0]=='center') $x = '50%';
  1092. else if ($val[0]=='right') $x = '100%';
  1093. else if ($val[0]=='top') $y = '0%';
  1094. else if ($val[0]=='bottom') $y = '100%';
  1095. else if (preg_match('/^[-]?[0-9\.]+%$/isU', $val[0])) $x = $val[0];
  1096. else if ($this->ConvertToMM($val[0])) $x = $this->ConvertToMM($val[0]);
  1097. else $res = false;
  1098. if ($val[1]=='left') $x = '0%';
  1099. else if ($val[1]=='right') $x = '100%';
  1100. else if ($val[1]=='top') $y = '0%';
  1101. else if ($val[1]=='center') $y = '50%';
  1102. else if ($val[1]=='bottom') $y = '100%';
  1103. else if (preg_match('/^[-]?[0-9\.]+%$/isU', $val[1])) $y = $val[1];
  1104. else if ($this->ConvertToMM($val[1])) $y = $this->ConvertToMM($val[1]);
  1105. else $res = false;
  1106. $val[0] = $x;
  1107. $val[1] = $y;
  1108. return $val;
  1109. }
  1110. function ConvertBackgroundRepeat($val)
  1111. {
  1112. switch($val)
  1113. {
  1114. case 'repeat':
  1115. return array(true, true);
  1116. case 'repeat-x':
  1117. return array(true, false);
  1118. case 'repeat-y':
  1119. return array(false, true);
  1120. case 'no-repeat':
  1121. return array(false, false);
  1122. }
  1123. return null;
  1124. }
  1125. /**
  1126. * Convertir une longueur en mm
  1127. *
  1128. * @param string longueur, avec unit�, � convertir
  1129. * @param float longueur du parent
  1130. * @return float longueur exprim�e en mm
  1131. */
  1132. function ConvertToMM($val, $old=0.)
  1133. {
  1134. $val = trim($val);
  1135. if (preg_match('/^[0-9\.\-]+$/isU', $val)) $val.= 'px';
  1136. if (preg_match('/^[0-9\.\-]+px$/isU', $val)) $val = 25.4/96. * str_replace('px', '', $val);
  1137. else if (preg_match('/^[0-9\.\-]+pt$/isU', $val)) $val = 25.4/72. * str_replace('pt', '', $val);
  1138. else if (preg_match('/^[0-9\.\-]+in$/isU', $val)) $val = 25.4 * str_replace('in', '', $val);
  1139. else if (preg_match('/^[0-9\.\-]+mm$/isU', $val)) $val = 1.*str_replace('mm', '', $val);
  1140. else if (preg_match('/^[0-9\.\-]+%$/isU', $val)) $val = 1.*$old*str_replace('%', '', $val)/100.;
  1141. else $val = null;
  1142. return $val;
  1143. }
  1144. /**
  1145. * D�composition d'un code couleur HTML
  1146. *
  1147. * @param string couleur au format CSS
  1148. * @return array(r, v, b) couleur exprim� par ses comporantes R, V, B, de 0 � 255.
  1149. */
  1150. function ConvertToRVB($val, &$res)
  1151. {
  1152. $val = trim($val);
  1153. $res = true;
  1154. if (strtolower($val)=='transparent') return array(null, null, null);
  1155. if (isset($this->htmlColor[strtolower($val)])) $val = $this->htmlColor[strtolower($val)];
  1156. if (preg_match('/rgb\([\s]*([0-9%]+)[\s]*,[\s]*([0-9%]+)[\s]*,[\s]*([0-9%]+)[\s]*\)/isU', $val, $match))
  1157. {
  1158. $r =$match[1]; if (substr($r, -1)=='%') $r = floor(255*substr($r, 0, -1)/100);
  1159. $v =$match[2]; if (substr($v, -1)=='%') $v = floor(255*substr($v, 0, -1)/100);
  1160. $b =$match[3]; if (substr($b, -1)=='%') $b = floor(255*substr($b, 0, -1)/100);
  1161. }
  1162. else if (strlen($val)==7 && substr($val, 0, 1)=='#')
  1163. {
  1164. $r = hexdec(substr($val, 1, 2));
  1165. $v = hexdec(substr($val, 3, 2));
  1166. $b = hexdec(substr($val, 5, 2));
  1167. }
  1168. else if (strlen($val)==4 && substr($val, 0, 1)=='#')
  1169. {
  1170. $r = hexdec(substr($val, 1, 1).substr($val, 1, 1));
  1171. $v = hexdec(substr($val, 2, 1).substr($val, 2, 1));
  1172. $b = hexdec(substr($val, 3, 1).substr($val, 3, 1));
  1173. }
  1174. else
  1175. {
  1176. $r=0;
  1177. $v=0;
  1178. $b=0;
  1179. $res = false;
  1180. }
  1181. return array(floor($r), floor($v), floor($b));
  1182. }
  1183. /**
  1184. * Analyser une feuille de style
  1185. *
  1186. * @param string code CSS
  1187. * @return null
  1188. */
  1189. function analyseStyle(&$code)
  1190. {
  1191. // on remplace tous les espaces, tab, \r, \n, par des espaces uniques
  1192. $code = preg_replace('/[\s]+/', ' ', $code);
  1193. // on enl�ve les commentaires
  1194. $code = preg_replace('/\/\*.*?\*\//s', '', $code);
  1195. // on analyse chaque style
  1196. preg_match_all('/([^{}]+){([^}]*)}/isU', $code, $match);
  1197. for($k=0; $k<count($match[0]); $k++)
  1198. {
  1199. // noms
  1200. $noms = strtolower(trim($match[1][$k]));
  1201. // style, s�par� par des; => on remplie le tableau correspondant
  1202. $styles = trim($match[2][$k]);
  1203. $styles = explode(';', $styles);
  1204. $stl = array();
  1205. foreach($styles as $style)
  1206. {
  1207. $tmp = explode(':', $style);
  1208. if (count($tmp)>1)
  1209. {
  1210. $cod = $tmp[0]; unset($tmp[0]); $tmp = implode(':', $tmp);
  1211. $stl[trim(strtolower($cod))] = trim($tmp);
  1212. }
  1213. }
  1214. // d�composition des noms par les ,
  1215. $noms = explode(',', $noms);
  1216. foreach($noms as $nom)
  1217. {
  1218. $nom = trim($nom);
  1219. // Si il a une fonction sp�cifique, comme :hover => on zap
  1220. if (strpos($nom, ':')!==false) continue;
  1221. if (!isset($this->css[$nom]))
  1222. $this->css[$nom] = $stl;
  1223. else
  1224. $this->css[$nom] = array_merge($this->css[$nom], $stl);
  1225. }
  1226. }
  1227. $this->css_keys = array_flip(array_keys($this->css));
  1228. }
  1229. /**
  1230. * Extraction des feuille de style du code HTML
  1231. *
  1232. * @param string code HTML
  1233. * @return null
  1234. */
  1235. function readStyle(&$html)
  1236. {
  1237. $style = ' ';
  1238. // extraction des balises link, et suppression de celles-ci dans le code HTML
  1239. preg_match_all('/<link([^>]*)>/isU', $html, $match);
  1240. $html = preg_replace('/<link[^>]*>/isU', '', $html);
  1241. $html = preg_replace('/<\/link[^>]*>/isU', '', $html);
  1242. // analyse de chaque balise
  1243. foreach($match[1] as $code)
  1244. {
  1245. $tmp = array();
  1246. // lecture des param�tres du type nom=valeur
  1247. $prop = '([a-zA-Z0-9_]+)=([^"\'\s>]+)';
  1248. preg_match_all('/'.$prop.'/is', $code, $match);
  1249. for($k=0; $k<count($match[0]); $k++)
  1250. $tmp[trim(strtolower($match[1][$k]))] = trim($match[2][$k]);
  1251. // lecture des param�tres du type nom="valeur"
  1252. $prop = '([a-zA-Z0-9_]+)=["]([^"]*)["]';
  1253. preg_match_all('/'.$prop.'/is', $code, $match);
  1254. for($k=0; $k<count($match[0]); $k++)
  1255. $tmp[trim(strtolower($match[1][$k]))] = trim($match[2][$k]);
  1256. // lecture des param�tres du type nom='valeur'
  1257. $prop = "([a-zA-Z0-9_]+)=[']([^']*)[']";
  1258. preg_match_all('/'.$prop.'/is', $code, $match);
  1259. for($k=0; $k<count($match[0]); $k++)
  1260. $tmp[trim(strtolower($match[1][$k]))] = trim($match[2][$k]);
  1261. // si de type text/css => on garde
  1262. if (isset($tmp['type']) && strtolower($tmp['type'])=='text/css' && isset($tmp['href']))
  1263. {
  1264. $content = @file_get_contents($tmp['href']);
  1265. $url = $tmp['href'];
  1266. if (strpos($url, 'http://')!==false)
  1267. {
  1268. $url = str_replace('http://', '', $url);
  1269. $url = explode('/', $url);
  1270. $url_main = 'http://'.$url[0].'/';
  1271. $url_self = $url; unset($url_self[count($url_self)-1]); $url_self = 'http://'.implode('/', $url_self).'/';
  1272. $content = preg_replace('/url\(([^\\\\][^)]*)\)/isU', 'url('.$url_self.'$1)', $content);
  1273. $content = preg_replace('/url\((\\\\[^)]*)\)/isU', 'url('.$url_main.'$1)', $content);
  1274. }
  1275. $style.= $content."\n";
  1276. }
  1277. }
  1278. // extraction des balises style, et suppression de celles-ci dans le code HTML
  1279. preg_match_all('/<style[^>]*>(.*)<\/style[^>]*>/isU', $html, $match);
  1280. $html = preg_replace('/<style[^>]*>(.*)<\/style[^>]*>/isU', '', $html);
  1281. // analyse de chaque balise
  1282. foreach($match[1] as $code)
  1283. {
  1284. $code = str_replace('<!--', '', $code);
  1285. $code = str_replace('-->', '', $code);
  1286. $style.= $code."\n";
  1287. }
  1288. $this->analyseStyle($style);
  1289. }
  1290. }
  1291. }
  1292. ?>