1<?php
2/*
3This script is supposed to be used together with the HTML2FPDF.php class
4Copyright (C) 2004-2005 Renato Coelho
5*/
6
7function ConvertColor($color="#000000"){
8//returns an associative array (keys: R,G,B) from html code (e.g. #3FE5AA)
9
10  //W3C approved color array (disabled)
11  //static $common_colors = array('black'=>'#000000','silver'=>'#C0C0C0','gray'=>'#808080', 'white'=>'#FFFFFF','maroon'=>'#800000','red'=>'#FF0000','purple'=>'#800080','fuchsia'=>'#FF00FF','green'=>'#008000','lime'=>'#00FF00','olive'=>'#808000','yellow'=>'#FFFF00','navy'=>'#000080', 'blue'=>'#0000FF','teal'=>'#008080','aqua'=>'#00FFFF');
12  //All color names array
13  static $common_colors = array('antiquewhite'=>'#FAEBD7','aquamarine'=>'#7FFFD4','beige'=>'#F5F5DC','black'=>'#000000','blue'=>'#0000FF','brown'=>'#A52A2A','cadetblue'=>'#5F9EA0','chocolate'=>'#D2691E','cornflowerblue'=>'#6495ED','crimson'=>'#DC143C','darkblue'=>'#00008B','darkgoldenrod'=>'#B8860B','darkgreen'=>'#006400','darkmagenta'=>'#8B008B','darkorange'=>'#FF8C00','darkred'=>'#8B0000','darkseagreen'=>'#8FBC8F','darkslategray'=>'#2F4F4F','darkviolet'=>'#9400D3','deepskyblue'=>'#00BFFF','dodgerblue'=>'#1E90FF','firebrick'=>'#B22222','forestgreen'=>'#228B22','gainsboro'=>'#DCDCDC','gold'=>'#FFD700','gray'=>'#808080','green'=>'#008000','greenyellow'=>'#ADFF2F','hotpink'=>'#FF69B4','indigo'=>'#4B0082','khaki'=>'#F0E68C','lavenderblush'=>'#FFF0F5','lemonchiffon'=>'#FFFACD','lightcoral'=>'#F08080','lightgoldenrodyellow'=>'#FAFAD2','lightgreen'=>'#90EE90','lightsalmon'=>'#FFA07A','lightskyblue'=>'#87CEFA','lightslategray'=>'#778899','lightyellow'=>'#FFFFE0','limegreen'=>'#32CD32','magenta'=>'#FF00FF','mediumaquamarine'=>'#66CDAA','mediumorchid'=>'#BA55D3','mediumseagreen'=>'#3CB371','mediumspringgreen'=>'#00FA9A','mediumvioletred'=>'#C71585','mintcream'=>'#F5FFFA','moccasin'=>'#FFE4B5','navy'=>'#000080','olive'=>'#808000','orange'=>'#FFA500','orchid'=>'#DA70D6','palegreen'=>'#98FB98','palevioletred'=>'#D87093','peachpuff'=>'#FFDAB9','pink'=>'#FFC0CB','powderblue'=>'#B0E0E6','red'=>'#FF0000','royalblue'=>'#4169E1','salmon'=>'#FA8072','seagreen'=>'#2E8B57','sienna'=>'#A0522D','skyblue'=>'#87CEEB','slategray'=>'#708090','springgreen'=>'#00FF7F','tan'=>'#D2B48C','thistle'=>'#D8BFD8','turquoise'=>'#40E0D0','violetred'=>'#D02090','white'=>'#FFFFFF','yellow'=>'#FFFF00');
14  //http://www.w3schools.com/css/css_colornames.asp
15  if ( ($color{0} != '#') and ( strstr($color,'(') === false ) ) $color = $common_colors[strtolower($color)];
16
17  if ($color{0} == '#') //case of #nnnnnn or #nnn
18  {
19  	$cor = strtoupper($color);
20  	if (strlen($cor) == 4) // Turn #RGB into #RRGGBB
21  	{
22	 	  $cor = "#" . $cor{1} . $cor{1} . $cor{2} . $cor{2} . $cor{3} . $cor{3};
23	  }
24	  $R = substr($cor, 1, 2);
25	  $vermelho = hexdec($R);
26	  $V = substr($cor, 3, 2);
27	  $verde = hexdec($V);
28	  $B = substr($cor, 5, 2);
29	  $azul = hexdec($B);
30	  $color = array();
31	  $color['R']=$vermelho;
32	  $color['G']=$verde;
33	  $color['B']=$azul;
34  }
35  else //case of RGB(r,g,b)
36  {
37  	$color = str_replace("rgb(",'',$color); //remove �rgb(�
38  	$color = str_replace("RGB(",'',$color); //remove �RGB(� -- PHP < 5 does not have str_ireplace
39  	$color = str_replace(")",'',$color); //remove �)�
40    $cores = explode(",", $color);
41    $color = array();
42	  $color['R']=$cores[0];
43	  $color['G']=$cores[1];
44	  $color['B']=$cores[2];
45  }
46  if (empty($color)) return array('R'=>255,'G'=>255,'B'=>255);
47  else return $color; // array['R']['G']['B']
48}
49
50function ConvertSize($size=5,$maxsize=0){
51// Depends of maxsize value to make % work properly. Usually maxsize == pagewidth
52  //Identify size (remember: we are using 'mm' units here)
53  if ( stristr($size,'px') ) $size *= 0.2645; //pixels
54  elseif ( stristr($size,'cm') ) $size *= 10; //centimeters
55  elseif ( stristr($size,'mm') ) $size += 0; //millimeters
56  elseif ( stristr($size,'in') ) $size *= 25.4; //inches
57  elseif ( stristr($size,'pc') ) $size *= 38.1/9; //PostScript picas
58  elseif ( stristr($size,'pt') ) $size *= 25.4/72; //72dpi
59  elseif ( stristr($size,'%') )
60  {
61  	$size += 0; //make "90%" become simply "90"
62  	$size *= $maxsize/100;
63  }
64  else $size *= 0.2645; //nothing == px
65
66  return $size;
67}
68
69function value_entity_decode($html)
70{
71//replace each value entity by its respective char
72  preg_match_all('|&#(.*?);|',$html,$temparray);
73  foreach($temparray[1] as $val) $html = str_replace("&#".$val.";",chr($val),$html);
74  return $html;
75}
76
77function lesser_entity_decode($html)
78{
79  //supports the most used entity codes
80 	$html = str_replace("&nbsp;"," ",$html);
81 	$html = str_replace("&amp;","&",$html);
82 	$html = str_replace("&lt;","<",$html);
83 	$html = str_replace("&gt;",">",$html);
84 	$html = str_replace("&laquo;","�",$html);
85 	$html = str_replace("&raquo;","�",$html);
86 	$html = str_replace("&para;","�",$html);
87 	$html = str_replace("&euro;","�",$html);
88 	$html = str_replace("&trade;","�",$html);
89 	$html = str_replace("&copy;","�",$html);
90 	$html = str_replace("&reg;","�",$html);
91 	$html = str_replace("&plusmn;","�",$html);
92 	$html = str_replace("&tilde;","~",$html);
93 	$html = str_replace("&circ;","^",$html);
94 	$html = str_replace("&quot;",'"',$html);
95 	$html = str_replace("&permil;","�",$html);
96 	$html = str_replace("&Dagger;","�",$html);
97 	$html = str_replace("&dagger;","�",$html);
98  return $html;
99}
100
101function AdjustHTML($html,$usepre=true)
102{
103//Try to make the html text more manageable (turning it into XHTML)
104
105  //Remove javascript code from HTML (should not appear in the PDF file)
106  $regexp = '|<script.*?</script>|si';
107  $html = preg_replace($regexp,'',$html);
108
109 	$html = str_replace("\r\n","\n",$html); //replace carriagereturn-linefeed-combo by a simple linefeed
110 	$html = str_replace("\f",'',$html); //replace formfeed by nothing
111	$html = str_replace("\r",'',$html); //replace carriage return by nothing
112 	if ($usepre) //used to keep \n on content inside <pre> and inside <textarea>
113 	{
114    // Preserve '\n's in content between the tags <pre> and </pre>
115  	$regexp = '#<pre(.*?)>(.+?)</pre>#si';
116  	$thereispre = preg_match_all($regexp,$html,$temp);
117    // Preserve '\n's in content between the tags <textarea> and </textarea>
118  	$regexp2 = '#<textarea(.*?)>(.+?)</textarea>#si';
119  	$thereistextarea = preg_match_all($regexp2,$html,$temp2);
120  	$html = str_replace("\n",' ',$html); //replace linefeed by spaces
121  	$html = str_replace("\t",' ',$html); //replace tabs by spaces
122	  $regexp3 = '#\s{2,}#s'; // turn 2+ consecutive spaces into one
123	  $html = preg_replace($regexp3,' ',$html);
124   	$iterator = 0;
125  	while($thereispre) //Recover <pre attributes>content</pre>
126  	{
127      $temp[2][$iterator] = str_replace("\n","<br>",$temp[2][$iterator]);
128    	$html = preg_replace($regexp,'<erp'.$temp[1][$iterator].'>'.$temp[2][$iterator].'</erp>',$html,1);
129    	$thereispre--;
130    	$iterator++;
131    }
132    $iterator = 0;
133    while($thereistextarea) //Recover <textarea attributes>content</textarea>
134	  {
135      $temp2[2][$iterator] = str_replace(" ","&nbsp;",$temp2[2][$iterator]);
136    	$html = preg_replace($regexp2,'<aeratxet'.$temp2[1][$iterator].'>'.trim($temp2[2][$iterator]).'</aeratxet>',$html,1);
137    	$thereistextarea--;
138    	$iterator++;
139    }
140    //Restore original tag names
141    $html = str_replace("<erp","<pre",$html);
142    $html = str_replace("</erp>","</pre>",$html);
143    $html = str_replace("<aeratxet","<textarea",$html);
144    $html = str_replace("</aeratxet>","</textarea>",$html);
145  // (the code above might slowdown overall performance?)
146  } //end of if($usepre)
147  else
148  {
149  	$html = str_replace("\n",' ',$html); //replace linefeed by spaces
150  	$html = str_replace("\t",' ',$html); //replace tabs by spaces
151	  $regexp = '/\\s{2,}/s'; // turn 2+ consecutive spaces into one
152  	$html = preg_replace($regexp,' ',$html);
153  }
154  // remove redundant <br>'s before </div>, avoiding huge leaps between text blocks
155  // such things appear on computer-generated HTML code
156	$regexp = '/(<br[ \/]?[\/]?>)+?<\/div>/si'; //<?//fix PSPAD highlight bug
157	$html = preg_replace($regexp,'</div>',$html);
158	return $html;
159}
160
161function dec2alpha($valor,$toupper="true"){
162// returns a string from A-Z to AA-ZZ to AAA-ZZZ
163// OBS: A = 65 ASCII TABLE VALUE
164  if (($valor < 1)  || ($valor > 18278)) return "?"; //supports 'only' up to 18278
165  $c1 = $c2 = $c3 = '';
166  if ($valor > 702) // 3 letters (up to 18278)
167    {
168      $c1 = 65 + floor(($valor-703)/676);
169      $c2 = 65 + floor((($valor-703)%676)/26);
170      $c3 = 65 + floor((($valor-703)%676)%26);
171    }
172  elseif ($valor > 26) // 2 letters (up to 702)
173  {
174      $c1 = (64 + (int)(($valor-1) / 26));
175      $c2 = (64 + (int)($valor % 26));
176      if ($c2 == 64) $c2 += 26;
177  }
178  else // 1 letter (up to 26)
179  {
180      $c1 = (64 + $valor);
181  }
182  $alpha = chr($c1);
183  if ($c2 != '') $alpha .= chr($c2);
184  if ($c3 != '') $alpha .= chr($c3);
185  if (!$toupper) $alpha = strtolower($alpha);
186  return $alpha;
187}
188
189function dec2roman($valor,$toupper=true){
190//returns a string as a roman numeral
191  if (($valor >= 5000) || ($valor < 1)) return "?"; //supports 'only' up to 4999
192  $aux = (int)($valor/1000);
193  if ($aux!==0)
194  {
195    $valor %= 1000;
196    while($aux!==0)
197    {
198    	$r1 .= "M";
199    	$aux--;
200    }
201  }
202  $aux = (int)($valor/100);
203  if ($aux!==0)
204  {
205    $valor %= 100;
206    switch($aux){
207    	case 3: $r2="C";
208    	case 2: $r2.="C";
209    	case 1: $r2.="C"; break;
210  	  case 9: $r2="CM"; break;
211  	  case 8: $r2="C";
212  	  case 7: $r2.="C";
213    	case 6: $r2.="C";
214      case 5: $r2="D".$r2; break;
215      case 4: $r2="CD"; break;
216      default: break;
217	  }
218  }
219  $aux = (int)($valor/10);
220  if ($aux!==0)
221  {
222    $valor %= 10;
223    switch($aux){
224    	case 3: $r3="X";
225    	case 2: $r3.="X";
226    	case 1: $r3.="X"; break;
227    	case 9: $r3="XC"; break;
228    	case 8: $r3="X";
229    	case 7: $r3.="X";
230  	  case 6: $r3.="X";
231      case 5: $r3="L".$r3; break;
232      case 4: $r3="XL"; break;
233      default: break;
234    }
235  }
236  switch($valor){
237  	case 3: $r4="I";
238  	case 2: $r4.="I";
239  	case 1: $r4.="I"; break;
240  	case 9: $r4="IX"; break;
241  	case 8: $r4="I";
242    case 7: $r4.="I";
243    case 6: $r4.="I";
244    case 5: $r4="V".$r4; break;
245    case 4: $r4="IV"; break;
246    default: break;
247  }
248  $roman = $r1.$r2.$r3.$r4;
249  if (!$toupper) $roman = strtolower($roman);
250  return $roman;
251}
252
253?>