Location: PHPKode > projects > OpenBizMap > openbizmap/mydata/script/sys_html2ps.txt
<?php

//converts mso:html into ps
  //IN $template_content
  //IN $header_content
  //IN $footer_content
  //OUT $ps_template_content


//ps
$ps_header_content='';
$ps_footer_content='';
$ps_body_content='';

$ps_command_content='';
$ps_images_content='';
$img_cnt=0;

for($processing_step=0;$processing_step<3;$processing_step++)
{
  $content_array=array();
  $settings_array=array();
  
  if($processing_step==0)
    $html_content=$header_content;
  elseif($processing_step==1)
    $html_content=$footer_content;
  else
    $html_content=$template_content;
    
  $ps_content='';  
  
  $html_content=str_replace('<',' <',$html_content);
  $html_content=str_replace('>',' >',$html_content);
  
  //main tokenizer
  $content_array_counter=0;
  $tok=strtok($html_content,'>');
  while($tok)
  {
    $tok=strtok('<');
    $content_array[$content_array_counter].=$tok;
    
    $tok=strtok('>');
    $settings_array[$content_array_counter].=$tok;
      
    $content_array_counter++;
  }
  
  //marks
  $mark_body=0;
  $mark_open_table=0;
  $mark_close_table=0;
  $mark_open_row=0;
  $mark_close_row=0;
  $mark_open_col=0;
  $mark_close_col=0;
  $mark_reading_col=0;
  $mark_font=0;
  $mark_paragraph_open=0;
  $mark_paragraph_close=0;
  $mark_page_margins=0;
  
  //aligns
  $align='0';
  
  //page
  $page_margins='70.9 70.9 70.9 70.9 page_margins';
  
  //border
  $border=0;
  
  //font
  $font='<f30>';
  $mark_bold=0;
  $mark_base_font=0;
  
  //main output loop
  $content_row_column=0;
  $content_array_sizeof=sizeof($content_array);
  for($content_array_ii=0;$content_array_ii<$content_array_sizeof;$content_array_ii++)
  {
    $settings=trim($settings_array[$content_array_ii]);
    
    //----------- SETTINGS -----------------------------------------------------------
    if($settings!='')
    {
    
      //marks
      if(substr($settings,0,4)=='body')
        $mark_body=1;
      elseif(substr($settings,0,4)=='head')
        $mark_body=0;
      elseif(substr($settings,0,2)=='tr')
        $mark_open_row=1;
      elseif(substr($settings,0,3)=='/tr')
        $mark_close_row=1;
      elseif(substr($settings,0,2)=='td')
        $mark_open_col=1;
      elseif(substr($settings,0,3)=='/td')
        $mark_close_col=1;

      
      if(substr($settings,0,4)=='img ')
      {
        $s_pos=strpos($settings,'src=');
        $e_pos=strpos($settings,'"',$s_pos+6);
        $img_src=substr($settings,$s_pos,$e_pos-$s_pos);
        $img_src=str_replace('src="','',$img_src);
        
        $img_size=getimagesize($img_src);
        $img_width=$img_size[0];
        $img_height=$img_size[1];
        
        $img_cnt++;
        $image_name='image_'.$img_cnt;
        
        $ps_images_content.="
          /$image_name {  
          %size
          $img_width $img_height
          %resize
          $img_width $img_height
          %image
          (
        ";  
        
        $img_content=file_get_contents($img_src);
        $img_sizeof=strlen($img_content);
        for($ii=0;$ii<$img_sizeof;$ii++)
        {
          $ps_images_content.=strtoupper(substr('00'.dechex(ord(substr($img_content,$ii,1))),-2));
          
          if($ii>0 and ((2*($ii+1))%64)==0)
            $ps_images_content.=CRLF;
        }
        $ps_images_content.=') show_image } def'.CRLF;
        
        $ps_command_content.="(<$image_name>) command eq \{$image_name /inline_processing 1 def} if".CRLF;
        
        //$content.="<$image_name>";
        $content_array[$content_array_ii]=trim($content_array[$content_array_ii])." <$image_name> ";
      }  

      //aligns
      if(strpos($settings,'align=right')>0 or strpos($settings,'style=\'text-align:right\'')>0)
        $align=2;
      elseif(strpos($settings,'align=center')>0 or strpos($settings,'style=\'text-align:center\'')>0)
        $align=1;
        
      //width
      if(strpos($settings,'width:')>0)
      {
        $s_pos=strpos($settings,'width:');
        $e_pos=strpos($settings,';',$s_pos);
        $width=substr($settings,$s_pos,$e_pos-$s_pos);
        $width=str_replace('width:','',$width);
        $width=str_replace('pt','',$width);
      }
      
      //border
      if(strpos($settings,'border=0')>0)
        $border=0;
      elseif(strpos($settings,'border=1')>0)
        $border=1;
      
      if(strpos($settings,'border:solid')>0)
      {
        $border_style=15;
        
        if(strpos($settings,'border-top:none')>0)
          $border_style-=1;
        if(strpos($settings,'border-right:none')>0)
          $border_style-=2;
        if(strpos($settings,'border-bottom:none')>0)
          $border_style-=4;
        if(strpos($settings,'border-left:none')>0)
          $border_style-=8;
      }
      else
      {
        $border_style=0;
        
        if(strpos($settings,'border-top:solid')>0)
          $border_style+=1;
        if(strpos($settings,'border-right:solid')>0)
          $border_style+=2;
        if(strpos($settings,'border-bottom:solid')>0)
          $border_style+=4;
        if(strpos($settings,'border-left:solid')>0)
          $border_style+=8;
      }
      
      if($border_style>0)  
        $border=$border_style;
        
        
      //P
      if(substr($settings,0,3)=='/p ')
        $mark_paragraph_close=1;
      
      if(substr($settings,0,2)=='p ')
      {
        $font=$base_font;
        $mark_paragraph_open=1;
      }

      //page style
      if(!$mark_page_margins and $processing_step==2 and strpos($settings,'@page ')>0) //only in body
      {
        $f_pos=strpos($settings,'@page ');
        $s_pos=strpos($settings,'margin:',$f_pos);
        $e_pos=strpos($settings,';',$s_pos);
        $page_margins=substr($settings,$s_pos,$e_pos-$s_pos);
        $page_margins=str_replace('margin:','',$page_margins);
        $page_margins=str_replace('pt','',$page_margins);
        $page_margins=str_replace('cm',' cm',$page_margins);
        
        $page_margins.=' page_margins';
         
        $mark_page_margins=1;
      }

      //font 
      if(!$mark_base_font and strpos($settings,'font-size:')>0)
      {
        $s_pos=strpos($settings,'font-size:');
        $e_pos=strpos($settings,'.',$s_pos);
        $font_size=substr($settings,$s_pos,$e_pos-$s_pos);
        $font_size=str_replace('font-size:','',$font_size);
        
        $base_font='<f'.(10*($font_size-7)).'> ';
        
        $mark_base_font=1;
      }
      
      $mark_font=0;
      
      if(strpos($settings,'font-size:')>0)
      {
        $s_pos=strpos($settings,'font-size:');
        $e_pos=strpos($settings,'.',$s_pos);
        $font_size=substr($settings,$s_pos,$e_pos-$s_pos);
        $font_size=str_replace('font-size:','',$font_size);
        
        $font='<f'.(10*($font_size-7)).'> ';
        
        $mark_font=1;
      }
            
      if(strpos($settings,'font-family:')>0)
      {
        if(strpos($settings,'font-family:Courier')>0) $font_family='Courier';
        if(strpos($settings,'font-family:Arial')>0) $font_family='Helvetica';
        
        $mark_font=1;
      }

      if(substr($settings,0,2)=='b ' or substr($settings,0,2)=='b'."\r")
        $mark_bold=1;
        
      if($settings=='/b')
        $mark_bold=0;
      
      //font mapping
      if(substr($settings,0,2)=='h1') $font='<f90> ';
      if(substr($settings,0,2)=='h2') $font='<f90> ';
      if(substr($settings,0,2)=='h3') $font='<f90> ';
    
      //echo 'SETTINGS::'.htmlspecialchars($settings_array[$content_array_ii]); br();
    }
    
    //----------- CONTENT -----------------------------------------------------------
    if(trim($content_array[$content_array_ii])!='')
    {
      $content=$content_array[$content_array_ii];
      //echo 'CONTENT::'.htmlspecialchars($content); br(); br();
      
      //if($font!='')
      $font=substr($font,0,3).$mark_bold.'> ';
      $content=''.$font.''.str_replace('&nbsp;',' ',$content);
      
      if($mark_body)
      {
        
        if($mark_font)
        {
          $ps_content.="
            %font
            /$font_family $font_size font_def
          ";
          $mark_font=0;
        }
        
        if($mark_open_col)
          $mark_reading_col=1;
        
        if($mark_close_row)
        {
          $ps_content.="
            %<tr>
            %show_row
              %columns
          ";
          
          $sizeof=$content_row_column+1;//sizeof($content_row_array);
          $column_end=0;
          for($ii=0;$ii<$sizeof;$ii++)
          {
            $column_start=$column_end;
            $column_end=$column_start+$content_row_array[$ii]['width'];
            
            $ps_content.="
              ".(0+$content_row_array[$ii]['align'])." ".(0+$content_row_array[$ii]['border'])." ".(0+$column_start)." ".(0+$column_end)." (".str_replace('(','\(',str_replace(')','\)',$content_row_array[$ii]['content']))." )
            ";
          }
          $ps_content.="
              %col count
                ".$sizeof."
            show_row
            %</tr>
          ";
          
          $mark_reading_col=0;
          $content_row_column=0;
          $content_row_array=array();
        }
        
        if($mark_close_col)
          $content_row_column++;
          
        if($mark_open_row)
          $content_row_column=0;
          
        if($mark_open_col or $mark_reading_col)
        {
          //$content=str_replace(CRLF,' <br/> ',$content);
          
          if(!$mark_open_col and ($mark_paragraph_open or $mark_paragraph_open))
            $content=' <br/> '.$content;
            
          //if($mark_open_col) 
          //  $content=$content.' <br/> ';
          
          $content_row_array[$content_row_column]['content'].=$content;
          $content_row_array[$content_row_column]['align']=$align;
          $content_row_array[$content_row_column]['width']=$width;
          $content_row_array[$content_row_column]['border']=$border;
          
          $content='';
        }
        
        if(!$mark_reading_col and strlen($content)>0)
        {
          $ps_content.="
            %show_row
            %columns
              ".$align." 0 0 cm 100 cm (".str_replace('(','\(',str_replace(')','\)',$content))." )
            %col count
              1
            show_row
          ";
        }
          
      }
      
      //reset marks
      $mark_open_table=0;
      $mark_close_table=0;
      $mark_open_row=0;
      $mark_close_row=0;
      $mark_open_col=0;
      $mark_close_col=0;
      $mark_paragraph_open=0;
      $mark_paragraph_close=0;
      
      //reset aligns
      $align='0';
  
      //reset border
      $border_style=0;
      
      //reset font
      $font=$base_font;
    }
  
  }
  //--------------------------------------------------------------------------------


  if($processing_step==0)
    $ps_header_content=$ps_content;
  elseif($processing_step==1)
    $ps_footer_content=$ps_content;
  else
    $ps_body_content=$ps_content;

}

$ps_font_name='Helvetica';
$ps_font_content='';

for($ii=1;$ii<10;$ii++)
{
  $ps_font_content.='  actual_font_number '.$ii.'0 eq {/'.$ps_font_name.' '.($ii+7).' font_def} if'.CRLF;
  $ps_font_content.='  actual_font_number '.$ii.'1 eq {/'.$ps_font_name.'-Bold '.($ii+7).' font_def} if'.CRLF;
  $ps_command_content.='  (<f'.$ii.'0>) command eq {'.$ii.'0 font  /inline_processing 1 def} if'.CRLF;
  $ps_command_content.='  (<f'.$ii.'1>) command eq {'.$ii.'1 font  /inline_processing 1 def} if'.CRLF;
}


  $ps_function_filename=$_innerSystemPath.'/storm/include/psbase.txt';
  $ps_function_content=file_get_contents($ps_function_filename);
  $ps_template_content=$ps_function_content;
    
  $ps_template_content=str_replace('%----------- PAGEMARGINS_PLACEHOLDER -----------------------',$page_margins,$ps_template_content);
  $ps_template_content=str_replace('%----------- FONT_PLACEHOLDER -----------------------',$ps_font_content,$ps_template_content);
  $ps_template_content=str_replace('%----------- COMMAND_PLACEHOLDER -----------------------',$ps_command_content,$ps_template_content);
  $ps_template_content=str_replace('%----------- IMAGE_PLACEHOLDER -----------------------',$ps_images_content,$ps_template_content);
  
  $ps_template_content=str_replace('%----------- HEADER_PLACEHOLDER -----------------------',$ps_header_content,$ps_template_content);
  $ps_template_content=str_replace('%----------- FOOTER_PLACEHOLDER -----------------------',$ps_footer_content,$ps_template_content);
  $ps_template_content=str_replace('%----------- BODY_PLACEHOLDER -----------------------',$ps_body_content,$ps_template_content);

?>
Return current item: OpenBizMap