<?
require_once('fpdf.php');
class PDF extends FPDF
{
//Cabecera de página
function Header()
{
    //Logos
	global $_1,$_2,$_3,$_4,$_5,$_6,$_7,$_8,$_9,$_10,$_11,$_12,$_13,$_14,$_15,$_16,$_17,$_18,$_19,$_20,$_21,$_22,$_23,$_24,$_25,$_26,$_27,$_28,$_29,$_30,$_95,$_31,$_32,$_33,$_34,$_35,$_36,$_37,$_38,$_39,$_40,$_41,$_42,$_43,$_44,$_45,$_46,$_47,$_48,$_49,$_50,$_51,$_52,$_53,$_54,$_55,$_56,$_57,$_58,$_59,$_60,$_61,$_62,$_63,$_64,$_65,$_66,$_67,$_68,$_69,$_70,$_71,$_72,$_73,$_74,$_75,$_76,$_77,$_78,$_79,$_80,$_81,$_82,$_83,$_84,$_85,$_86,$_87,$_88,$_89,$_90,$_91,$haydetalle,$elmasgrande,$w,$_90,$_91,$_92,$_93,$_94;
	$this->Image("logo_ahmsa.jpg",10,10,80);
	$this->SetFillColor(201, 201, 201);
	$this->SetX(120);
	$this->SetFont('Arial','B',7);
	$this->Cell(50,3,"Altos Hornos de México, S.A.B. de C.V.",0,1,'C');
	$this->SetFont('Arial','',7);
	$this->Cell(195,3,"Régimen Fiscal:",0,0,'R');
	$this->SetX(120);
	$this->Cell(50,3,"Planta y Domicilio Fiscal: Prol. Juárez Sin Número, Col. La Loma",0,1,'C');
	$this->Cell(225,3,"Régimen General de Ley Personas Morales",0,0,'R');
	$this->SetX(120);
	$this->Cell(50,3,"C.P. 25770 Apartado Postal 121",0,1,'C');
	$this->SetX(120);
	$this->Cell(50,3,"Monclova, Coah. Tel.: (866)649-36-00",0,1,'C');
	$this->SetX(120);
	$this->Cell(50,3,"R.F.C.: AHM-420706-U10",0,1,'C');
	$this->SetXY(250,10);
	$this->SetFont('Arial','B',7);
	$this->Cell(0,4,"$_3",1,2,'C',1);
	//$this->SetTextColor(255,0,0);
	$this->Cell(10,4,"SERIE",1,0,'C');
	$this->Cell(0,4,"FOLIO",1,1,'C');
	$this->SetX(250);
	$this->Cell(10,4,"$_45",1,0,'C');
	$this->Cell(0,4,"$_46",1,1,'C');
	$this->SetTextColor(0,0,0);
	$this->SetFont('Arial','',6.8);
	$this->SetXY(167,22);
	$this->Cell(18,4,"Número SAP",0,0,'L');
	$this->Cell(20,4,"$_15",1,0,'C');//Folio
	$this->Cell(18,4,"Folio Fiscal",0,0,'R');
	$this->Cell(46.4,4,"$_95",1,1,'C');//Folio
	$this->SetY(27);
	$x = $this->GetX();
	$y = $this->GetY();
	$this->SetFontSize(6);
	$this->MultiCell(120,3,"CLIENTE: $_4                            CLIENTE PROVEEDOR: $_84\n$_1\n$_7 $_10\n$_13, $_11, $_19\nR.F.C. $_6",1,'L');
	$this->Ln(2);
	$y2 = $this->GetY();
	$this->MultiCell(60,3,"CONSIGNATARIO: $_53\n$_55\n$_76\n$_78\n$_81\n$_90",0,'L');
	$ye1 = $this->GetY();
	$this->SetXY($x+60,$y2);
	$this->MultiCell(60,3,"CLIENTE FINAL: $_75\n$_56\n$_77\n$_89\n$_79\n$_91",0,'L');
	$ye2 = $this->GetY();
	$yemax = max($ye1,$ye2);
	$this->Rect(10,$y2,60,$yemax-$y2);
	$this->Rect(70,$y2,60,$yemax-$y2);
	$this->SetXY($x,$yemax+1);
	$this->Cell(120,4,"CONDICIÓN DE PAGO: $_83",1,1,'L');
//	$this->Cell(18,4,"Método de Pago: $_92",'LTB',0,'L');
//	$this->Cell(102,4,"Número de Cuenta de Pago: $_93",'TBR',0,'R');
	$this->SetXY($x+122,$y);
	$this->SetFont('Arial','B',6);
	$this->Cell(50,5,"DATOS DEL EMBARQUE",1,0,'C');
	$this->Cell(60,5,"DATOS INTERNOS",1,1,'C');
	$this->Image("rfc_ahmsa.jpg",244,$y,25,45);
	$this->SetXY($x+122,$y+5);
	$this->Cell(25,3,"NUM. ENTREGA",'LTR',0,'L');
	$this->Cell(25,3,"NUM. TRANSPORTE",'LTR',0,'L');
	$this->Cell(30,3,"",'LTR',0,'L');
	$this->Cell(30,3,"NUMERO",'LTR',1,'L');
	$this->SetXY($x+122,$y+8);
	$this->SetFont('Arial','',6);
	$this->Cell(25,3,"$_54",'LBR',0,'L');//NUMERO DE ENTREGA
	$this->Cell(25,3,"$_57",'LBR',0,'L');//NUMERO DE TRANSPORTE
	$this->Cell(30,3,"",'LBR',0,'L');//NADA
	$this->Cell(30,3,"$_58",'LBR',1,'L');//NUMERO
	$this->SetXY($x+122,$y+11);
	$this->SetFont('Arial','B',6);
	$this->Cell(25,3,"FECHA DE EXPEDICION",'LTR',0,'L');
	$this->Cell(25,3,"NUM. UNIDAD",'LTR',0,'L');
	$this->Cell(43,3,"LINEA TRANSPORTISTA",'LTR',0,'L');
	$this->Cell(17,3,"FECHA",'LTR',1,'L');
	$this->SetXY($x+122,$y+14);
	$this->SetFont('Arial','',6);
	$this->Cell(25,3,"$_59",'LBR',0,'L');//FECHA DE EMISION
	$this->Cell(25,3,"$_60",'LBR',0,'L');//NUMERO DE UNIDAD
	$this->Cell(43,3,"$_61",'LBR',0,'L');//LINEA TRANSPORTISTA
	$this->Cell(17,3,"$_62",'LBR',1,'L');//FECHA
	$this->SetXY($x+122,$y+17);
	$this->SetFont('Arial','B',6);
	$this->Cell(30,3,"VIA",'LTR',0,'L');
	$this->Cell(35,3,"CONDICION DE FLETE",'LTR',0,'L');
//	$this->Cell(25,3,"FECHA ENTREGA",'LTR',0,'L');
	$this->Cell(25,3,"",'LTR',0,'L');
	$this->Cell(20,3,"TIPO CAMBIO",'LTR',1,'L');
	$this->SetXY($x+122,$y+20);
	$this->SetFont('Arial','',6);
	$this->Cell(30,3,"$_63",'LBR',0,'L');//VIA
	$this->Cell(35,3,"$_64",'LBR',0,'L');//CONDICION DE FLETE
	$this->Cell(25,3,"$_65",'LBR',0,'L');//FECHA ENTREGA
	$this->Cell(20,3,"$_66",'LBR',1,'L');//TIPO DE CAMBIO
	$this->SetXY($x+122,$y+23);
	$this->SetFont('Arial','B',6);
	$this->Cell(45,3,"ADUANA",'LTR',0,'L');
	$this->Cell(20,3,"PESO REAL:",'LTR',0,'L');
	$this->Cell(45,3,"DESTINO",'LTR',0,'L');
	$this->SetXY($x+122,$y+26);
	$this->SetFont('Arial','',6);
	$this->Cell(45,3,"$_67",'LBR',0,'L');//ADUANA
	$this->Cell(20,3,"$_68",'LBR',0,'L');//PESO REAL
	$this->Cell(45,3,"$_69",'LBR',1,'L');//DESTINO
	$this->SetFont('Arial','B',6);
	$this->SetXY($x+122,$y+31);
	$this->Cell(110,3,"CONDICIÓN DE VENTA:",'LTR',1,'L');
	$this->SetFont('Arial','',6);
	$this->SetXY($x+122,$y+34);
	$this->Cell(110,4,"$_70",'LR',1,'L'); //CONDICIÓN DE VENTA
	$this->SetFont('Arial','B',6);
	$this->SetXY($x+122,$y+38);
	$this->Cell(15,3,"CENTRO:",'L',0,'L');
	$this->SetFont('Arial','',6);
	$this->Cell(95,3,"$_71",'R',1,'L');
	$this->SetFont('Arial','B',6);
	$this->SetXY($x+122,$y+41);
	$this->Cell(15,3,"ALMACÉN:",'LB',0,'L');
	$this->SetFont('Arial','',6);
	$this->Cell(95,3,"$_72",'BR',1,'L');
	$this->Ln(2);
	$this->SetFont('Arial','B',6);
	
	$temp1 = ($_73=="LBS")?"LIBRAS":"TONE-";
	$temp2 = ($_73=="LBS")?"":"LADAS";
	if($haydetalle){

	$t1 = array("","","","","","","","ESPESOR","ANCHO","","","","PRECIO","","");
	$t2 = array("","","","","PRODUCTO","GPO","","CALIBRE O","DIAMETRO","","VALORIZADO",$temp1,"UNITARIO","PRECIO","VALOR");
	$t3 = array("PEDIDO","PTDA","PZAS","PEDIDO DEL CLIENTE","CALIDAD O GRADO NORMA","ART.","PESO","PERALTE","O PATIN","LARGO","EN $_73",$temp2,"EN $_74","UNITARIO","MATERIAL");
	for($i = 0;$i<sizeof($w);$i++)
	{
	  $this->Cell($w[$i],3,$t1[$i],'LTR',0,'C');
	}
	$this->Ln();
	for($i = 0;$i<sizeof($w);$i++)
	{
	  $this->Cell($w[$i],3,$t2[$i],'LR',0,'C');
	}
	$this->Ln();
	for($i = 0;$i<sizeof($w);$i++)
	{
	  $this->Cell($w[$i],3,$t3[$i],'LBR',0,'C');
	}
	}
	$this->Ln();
	
		
	
}

//Pie de página

function Footer(){
	//Posición: a 1,5 cm del final
	global $_34,$_33,$_36,$_37,$_52,$_43,$haydetalle, $_98, $_97,$qrcode,$ambiente,$_50,$_51,$_99,$_2;
 
	if($haydetalle){
		$this->Line(10,$this->getY(),269.4,$this->getY());
	}
    $this->SetY(-40);
	
	$qrcode->displayFPDF($this,10,$this->getY(),0.8,array(255,255,255),array(0,0,0));
	$this->setX(43);
	$this->SetFillColor(201, 201, 201);
	$this->Cell(0,3,"CADENA ORIGINAL DEL COMPLEMENTO DE CERTIFICACIÓN DIGITAL DEL SAT" ,1,2,'L',1);
	$this->MultiCell(0,3,"$_50",1,'L');
	$this->setX(43);
	$this->Cell(0,3,"SELLO DIGITAL DEL CFD",1,2,'L',1);
	$this->MultiCell(0,3,"$_51",1,'L');
	$this->setX(43);
	$this->Cell(0,3,"SELLO DIGITAL DEL SAT",1,2,'L',1);
	$this->MultiCell(0,3,"$_99",1,'L');
	$this->Ln(1);
	$this->setX(43);
	if($_2!='FPP')
	$this->Cell(0,3,"REG. INDUSTRIA DEL HIERRO Y DEL ACERO NUM.1 PARA EFECTOS DE FACTURACIÓN RIGEN INVARIABLEMENTE LOS PESOS EN NUESTRAS BASCULAS",0,1,'L');

    //Arial italic 8
    $this->SetFont('Arial','I',6);
	$this->setX(43);
	$this->MultiCell(0,3,"ESTE DOCUMENTO ES UNA REPRESENTACIÓN IMPRESA DE UN CFDI.  NUMERO DE CERTIFICADO DEL EMISOR:$_37  NUMERO DE CERTIFICADO DEL SAT:$_98 \nFECHA Y HORA DE EMISION: $_43     FECHA Y HORA DE CERTIFICACION: $_97",0,'C');
    $this->Cell(0,4,'Página '.$this->PageNo().'/{nb}',0,0,'C');
	
	if($ambiente!="PRODUCCION"){
		$this->Rotate(45,10,300);
		$this->SetFont('Arial','B',30);
		$this->SetTextColor(150);
		$this->Cell(0,20,"DOCUMENTO DE PRUEBA",0,0,'C');
		$this->SetTextColor(0);
		$this->Rotate(0);
		$this->SetY(10);
	}
}

// Sets line style
    // Parameters:
    // - style: Line style. Array with keys among the following:
    //   . width: Width of the line in user units
    //   . cap: Type of cap to put on the line (butt, round, square). The difference between 'square' and 'butt' is that 'square' projects a flat end past the end of the line.
    //   . join: miter, round or bevel
    //   . dash: Dash pattern. Is 0 (without dash) or array with series of length values, which are the lengths of the on and off dashes.
    //           For example: (2) represents 2 on, 2 off, 2 on , 2 off ...
    //                        (2,1) is 2 on, 1 off, 2 on, 1 off.. etc
    //   . phase: Modifier of the dash pattern which is used to shift the point at which the pattern starts
    //   . color: Draw color. Array with components (red, green, blue)
    function SetLineStyle($style) {
        extract($style);
        if (isset($width)) {
            $width_prev = $this->LineWidth;
            $this->SetLineWidth($width);
            $this->LineWidth = $width_prev;
        }
        if (isset($cap)) {
            $ca = array('butt' => 0, 'round'=> 1, 'square' => 2);
            if (isset($ca[$cap]))
                $this->_out($ca[$cap] . ' J');
        }
        if (isset($join)) {
            $ja = array('miter' => 0, 'round' => 1, 'bevel' => 2);
            if (isset($ja[$join]))
                $this->_out($ja[$join] . ' j');
        }
        if (isset($dash)) {
            $dash_string = '';
            if ($dash) {
                if(ereg('^.+,', $dash))
                    $tab = explode(',', $dash);
                else
                    $tab = array($dash);
                $dash_string = '';
                foreach ($tab as $i => $v) {
                    if ($i > 0)
                        $dash_string .= ' ';
                    $dash_string .= sprintf('%.2f', $v);
                }
            }
            if (!isset($phase) || !$dash)
                $phase = 0;
            $this->_out(sprintf('[%s] %.2f d', $dash_string, $phase));
        }
        if (isset($color)) {
            list($r, $g, $b) = $color;
            $this->SetDrawColor($r, $g, $b);
        }
    }

    // Draws a line
    // Parameters:
    // - x1, y1: Start point
    // - x2, y2: End point
    // - style: Line style. Array like for SetLineStyle
    function Line($x1, $y1, $x2, $y2, $style = null) {
        if ($style)
            $this->SetLineStyle($style);
        parent::Line($x1, $y1, $x2, $y2);
    }

    // Draws a rectangle
    // Parameters:
    // - x, y: Top left corner
    // - w, h: Width and height
    // - style: Style of rectangle (draw and/or fill: D, F, DF, FD)
    // - border_style: Border style of rectangle. Array with some of this index
    //   . all: Line style of all borders. Array like for SetLineStyle
    //   . L: Line style of left border. null (no border) or array like for SetLineStyle
    //   . T: Line style of top border. null (no border) or array like for SetLineStyle
    //   . R: Line style of right border. null (no border) or array like for SetLineStyle
    //   . B: Line style of bottom border. null (no border) or array like for SetLineStyle
    // - fill_color: Fill color. Array with components (red, green, blue)
    function Rect($x, $y, $w, $h, $style = '', $border_style = null, $fill_color = null) {
        if (!(false === strpos($style, 'F')) && $fill_color) {
            list($r, $g, $b) = $fill_color;
            $this->SetFillColor($r, $g, $b);
        }
        switch ($style) {
            case 'F':
                $border_style = null;
                parent::Rect($x, $y, $w, $h, $style);
                break;
            case 'DF': case 'FD':
                if (!$border_style || isset($border_style['all'])) {
                    if (isset($border_style['all'])) {
                        $this->SetLineStyle($border_style['all']);
                        $border_style = null;
                    }
                } else
                    $style = 'F';
                parent::Rect($x, $y, $w, $h, $style);
                break;
            default:
                if (!$border_style || isset($border_style['all'])) {
                    if (isset($border_style['all']) && $border_style['all']) {
                        $this->SetLineStyle($border_style['all']);
                        $border_style = null;
                    }
                    parent::Rect($x, $y, $w, $h, $style);
                }
                break;
        }
        if ($border_style) {
            if (isset($border_style['L']) && $border_style['L'])
                $this->Line($x, $y, $x, $y + $h, $border_style['L']);
            if (isset($border_style['T']) && $border_style['T'])
                $this->Line($x, $y, $x + $w, $y, $border_style['T']);
            if (isset($border_style['R']) && $border_style['R'])
                $this->Line($x + $w, $y, $x + $w, $y + $h, $border_style['R']);
            if (isset($border_style['B']) && $border_style['B'])
                $this->Line($x, $y + $h, $x + $w, $y + $h, $border_style['B']);
        }
    }

    // Draws a Bézier curve (the Bézier curve is tangent to the line between the control points at either end of the curve)
    // Parameters:
    // - x0, y0: Start point
    // - x1, y1: Control point 1
    // - x2, y2: Control point 2
    // - x3, y3: End point
    // - style: Style of rectangule (draw and/or fill: D, F, DF, FD)
    // - line_style: Line style for curve. Array like for SetLineStyle
    // - fill_color: Fill color. Array with components (red, green, blue)
    function Curve($x0, $y0, $x1, $y1, $x2, $y2, $x3, $y3, $style = '', $line_style = null, $fill_color = null) {
        if (!(false === strpos($style, 'F')) && $fill_color) {
            list($r, $g, $b) = $fill_color;
            $this->SetFillColor($r, $g, $b);
        }
        switch ($style) {
            case 'F':
                $op = 'f';
                $line_style = null;
                break;
            case 'FD': case 'DF':
                $op = 'B';
                break;
            default:
                $op = 'S';
                break;
        }
        if ($line_style)
            $this->SetLineStyle($line_style);

        $this->_Point($x0, $y0);
        $this->_Curve($x1, $y1, $x2, $y2, $x3, $y3);
        $this->_out($op);
    }

    // Draws an ellipse
    // Parameters:
    // - x0, y0: Center point
    // - rx, ry: Horizontal and vertical radius (if ry = 0, draws a circle)
    // - angle: Orientation angle (anti-clockwise)
    // - astart: Start angle
    // - afinish: Finish angle
    // - style: Style of ellipse (draw and/or fill: D, F, DF, FD, C (D + close))
    // - line_style: Line style for ellipse. Array like for SetLineStyle
    // - fill_color: Fill color. Array with components (red, green, blue)
    // - nSeg: Ellipse is made up of nSeg Bézier curves
    function Ellipse($x0, $y0, $rx, $ry = 0, $angle = 0, $astart = 0, $afinish = 360, $style = '', $line_style = null, $fill_color = null, $nSeg = 8) {
        if ($rx) {
            if (!(false === strpos($style, 'F')) && $fill_color) {
                list($r, $g, $b) = $fill_color;
                $this->SetFillColor($r, $g, $b);
            }
            switch ($style) {
                case 'F':
                    $op = 'f';
                    $line_style = null;
                    break;
                case 'FD': case 'DF':
                    $op = 'B';
                    break;
                case 'C':
                    $op = 's'; // small 's' means closing the path as well
                    break;
                default:
                    $op = 'S';
                    break;
            }
            if ($line_style)
                $this->SetLineStyle($line_style);
            if (!$ry)
                $ry = $rx;
            $rx *= $this->k;
            $ry *= $this->k;
            if ($nSeg < 2)
                $nSeg = 2;

            $astart = deg2rad((float) $astart);
            $afinish = deg2rad((float) $afinish);
            $totalAngle = $afinish - $astart;

            $dt = $totalAngle/$nSeg;
            $dtm = $dt/3;

            $x0 *= $this->k;
            $y0 = ($this->h - $y0) * $this->k;
            if ($angle != 0) {
                $a = -deg2rad((float) $angle);
                $this->_out(sprintf('q %.2f %.2f %.2f %.2f %.2f %.2f cm', cos($a), -1 * sin($a), sin($a), cos($a), $x0, $y0));
                $x0 = 0;
                $y0 = 0;
            }

            $t1 = $astart;
            $a0 = $x0 + ($rx * cos($t1));
            $b0 = $y0 + ($ry * sin($t1));
            $c0 = -$rx * sin($t1);
            $d0 = $ry * cos($t1);
            $this->_Point($a0 / $this->k, $this->h - ($b0 / $this->k));
            for ($i = 1; $i <= $nSeg; $i++) {
                // Draw this bit of the total curve
                $t1 = ($i * $dt) + $astart;
                $a1 = $x0 + ($rx * cos($t1));
                $b1 = $y0 + ($ry * sin($t1));
                $c1 = -$rx * sin($t1);
                $d1 = $ry * cos($t1);
                $this->_Curve(($a0 + ($c0 * $dtm)) / $this->k,
                            $this->h - (($b0 + ($d0 * $dtm)) / $this->k),
                            ($a1 - ($c1 * $dtm)) / $this->k,
                            $this->h - (($b1 - ($d1 * $dtm)) / $this->k),
                            $a1 / $this->k,
                            $this->h - ($b1 / $this->k));
                $a0 = $a1;
                $b0 = $b1;
                $c0 = $c1;
                $d0 = $d1;
            }
            $this->_out($op);
            if ($angle !=0)
                $this->_out('Q');
        }
    }

    // Draws a circle
    // Parameters:
    // - x0, y0: Center point
    // - r: Radius
    // - astart: Start angle
    // - afinish: Finish angle
    // - style: Style of circle (draw and/or fill) (D, F, DF, FD, C (D + close))
    // - line_style: Line style for circle. Array like for SetLineStyle
    // - fill_color: Fill color. Array with components (red, green, blue)
    // - nSeg: Ellipse is made up of nSeg Bézier curves
    function Circle($x0, $y0, $r, $astart = 0, $afinish = 360, $style = '', $line_style = null, $fill_color = null, $nSeg = 8) {
        $this->Ellipse($x0, $y0, $r, 0, 0, $astart, $afinish, $style, $line_style, $fill_color, $nSeg);
    }

    // Draws a polygon
    // Parameters:
    // - p: Points. Array with values x0, y0, x1, y1,..., x(np-1), y(np - 1)
    // - style: Style of polygon (draw and/or fill) (D, F, DF, FD)
    // - line_style: Line style. Array with one of this index
    //   . all: Line style of all lines. Array like for SetLineStyle
    //   . 0..np-1: Line style of each line. Item is 0 (not line) or like for SetLineStyle
    // - fill_color: Fill color. Array with components (red, green, blue)
    function Polygon($p, $style = '', $line_style = null, $fill_color = null) {
        $np = count($p) / 2;
        if (!(false === strpos($style, 'F')) && $fill_color) {
            list($r, $g, $b) = $fill_color;
            $this->SetFillColor($r, $g, $b);
        }
        switch ($style) {
            case 'F':
                $line_style = null;
                $op = 'f';
                break;
            case 'FD': case 'DF':
                $op = 'B';
                break;
            default:
                $op = 'S';
                break;
        }
        $draw = true;
        if ($line_style)
            if (isset($line_style['all']))
                $this->SetLineStyle($line_style['all']);
            else { // 0 .. (np - 1), op = {B, S}
                $draw = false;
                if ('B' == $op) {
                    $op = 'f';
                    $this->_Point($p[0], $p[1]);
                    for ($i = 2; $i < ($np * 2); $i = $i + 2)
                        $this->_Line($p[$i], $p[$i + 1]);
                    $this->_Line($p[0], $p[1]);
                    $this->_out($op);
                }
                $p[$np * 2] = $p[0];
                $p[($np * 2) + 1] = $p[1];
                for ($i = 0; $i < $np; $i++)
                    if (!empty($line_style[$i]))
                        $this->Line($p[$i * 2], $p[($i * 2) + 1], $p[($i * 2) + 2], $p[($i * 2) + 3], $line_style[$i]);
            }

        if ($draw) {
            $this->_Point($p[0], $p[1]);
            for ($i = 2; $i < ($np * 2); $i = $i + 2)
                $this->_Line($p[$i], $p[$i + 1]);
            $this->_Line($p[0], $p[1]);
            $this->_out($op);
        }
    }

    // Draws a regular polygon
    // Parameters:
    // - x0, y0: Center point
    // - r: Radius of circumscribed circle
    // - ns: Number of sides
    // - angle: Orientation angle (anti-clockwise)
    // - circle: Draw circumscribed circle or not
    // - style: Style of polygon (draw and/or fill) (D, F, DF, FD)
    // - line_style: Line style. Array with one of this index
    //   . all: Line style of all lines. Array like for SetLineStyle
    //   . 0..ns-1: Line style of each line. Item is 0 (not line) or like for SetLineStyle
    // - fill_color: Fill color. Array with components (red, green, blue)
    // - circle_style: Style of circumscribed circle (draw and/or fill) (D, F, DF, FD) (if draw)
    // - circle_line_style: Line style for circumscribed circle. Array like for SetLineStyle (if draw)
    // - circle_fill_color: Fill color for circumscribed circle. Array with components (red, green, blue) (if draw fill circle)
    function RegularPolygon($x0, $y0, $r, $ns, $angle = 0, $circle = false, $style = '', $line_style = null, $fill_color = null, $circle_style = '', $circle_line_style = null, $circle_fill_color = null) {
        if ($ns < 3)
            $ns = 3;
        if ($circle)
            $this->Circle($x0, $y0, $r, 0, 360, $circle_style, $circle_line_style, $circle_fill_color);
        $p = null;
        for ($i = 0; $i < $ns; $i++) {
            $a = $angle + ($i * 360 / $ns);
            $a_rad = deg2rad((float) $a);
            $p[] = $x0 + ($r * sin($a_rad));
            $p[] = $y0 + ($r * cos($a_rad));
        }
        $this->Polygon($p, $style, $line_style, $fill_color);
    }

    // Draws a star polygon
    // Parameters:
    // - x0, y0: Center point
    // - r: Radius of circumscribed circle
    // - nv: Number of vertices
    // - ng: Number of gaps (ng % nv = 1 => regular polygon)
    // - angle: Orientation angle (anti-clockwise)
    // - circle: Draw circumscribed circle or not
    // - style: Style of polygon (draw and/or fill) (D, F, DF, FD)
    // - line_style: Line style. Array with one of this index
    //   . all: Line style of all lines. Array like for SetLineStyle
    //   . 0..n-1: Line style of each line. Item is 0 (not line) or like for SetLineStyle
    // - fill_color: Fill color. Array with components (red, green, blue)
    // - circle_style: Style of circumscribed circle (draw and/or fill) (D, F, DF, FD) (if draw)
    // - circle_line_style: Line style for circumscribed circle. Array like for SetLineStyle (if draw)
    // - circle_fill_color: Fill color for circumscribed circle. Array with components (red, green, blue) (if draw fill circle)
    function StarPolygon($x0, $y0, $r, $nv, $ng, $angle = 0, $circle = false, $style = '', $line_style = null, $fill_color = null, $circle_style = '', $circle_line_style = null, $circle_fill_color = null) {
        if ($nv < 2)
            $nv = 2;
        if ($circle)
            $this->Circle($x0, $y0, $r, 0, 360, $circle_style, $circle_line_style, $circle_fill_color);
        $p2 = null;
        $visited = null;
        for ($i = 0; $i < $nv; $i++) {
            $a = $angle + ($i * 360 / $nv);
            $a_rad = deg2rad((float) $a);
            $p2[] = $x0 + ($r * sin($a_rad));
            $p2[] = $y0 + ($r * cos($a_rad));
            $visited[] = false;
        }
        $p = null;
        $i = 0;
        do {
            $p[] = $p2[$i * 2];
            $p[] = $p2[($i * 2) + 1];
            $visited[$i] = true;
            $i += $ng;
            $i %= $nv;
        } while (!$visited[$i]);
        $this->Polygon($p, $style, $line_style, $fill_color);
    }

    // Draws a rounded rectangle
    // Parameters:
    // - x, y: Top left corner
    // - w, h: Width and height
    // - r: Radius of the rounded corners
    // - round_corner: Draws rounded corner or not. String with a 0 (not rounded i-corner) or 1 (rounded i-corner) in i-position. Positions are, in order and begin to 0: top left, top right, bottom right and bottom left
    // - style: Style of rectangle (draw and/or fill) (D, F, DF, FD)
    // - border_style: Border style of rectangle. Array like for SetLineStyle
    // - fill_color: Fill color. Array with components (red, green, blue)
    function RoundedRect($x, $y, $w, $h, $r, $round_corner = '1111', $style = '', $border_style = null, $fill_color = null) {
        if ('0000' == $round_corner) // Not rounded
            $this->Rect($x, $y, $w, $h, $style, $border_style, $fill_color);
        else { // Rounded
            if (!(false === strpos($style, 'F')) && $fill_color) {
                list($red, $g, $b) = $fill_color;
                $this->SetFillColor($red, $g, $b);
            }
            switch ($style) {
                case 'F':
                    $border_style = null;
                    $op = 'f';
                    break;
                case 'FD': case 'DF':
                    $op = 'B';
                    break;
                default:
                    $op = 'S';
                    break;
            }
            if ($border_style)
                $this->SetLineStyle($border_style);

            $MyArc = 4 / 3 * (sqrt(2) - 1);

            $this->_Point($x + $r, $y);
            $xc = $x + $w - $r;
            $yc = $y + $r;
            $this->_Line($xc, $y);
            if ($round_corner[0])
                $this->_Curve($xc + ($r * $MyArc), $yc - $r, $xc + $r, $yc - ($r * $MyArc), $xc + $r, $yc);
            else
                $this->_Line($x + $w, $y);

            $xc = $x + $w - $r ;
            $yc = $y + $h - $r;
            $this->_Line($x + $w, $yc);

            if ($round_corner[1])
                $this->_Curve($xc + $r, $yc + ($r * $MyArc), $xc + ($r * $MyArc), $yc + $r, $xc, $yc + $r);
            else
                $this->_Line($x + $w, $y + $h);

            $xc = $x + $r;
            $yc = $y + $h - $r;
            $this->_Line($xc, $y + $h);
            if ($round_corner[2])
                $this->_Curve($xc - ($r * $MyArc), $yc + $r, $xc - $r, $yc + ($r * $MyArc), $xc - $r, $yc);
            else
                $this->_Line($x, $y + $h);

            $xc = $x + $r;
            $yc = $y + $r;
            $this->_Line($x, $yc);
            if ($round_corner[3])
                $this->_Curve($xc - $r, $yc - ($r * $MyArc), $xc - ($r * $MyArc), $yc - $r, $xc, $yc - $r);
            else {
                $this->_Line($x, $y);
                $this->_Line($x + $r, $y);
            }
            $this->_out($op);
        }
    }

    /* PRIVATE METHODS */

    // Sets a draw point
    // Parameters:
    // - x, y: Point
    function _Point($x, $y) {
        $this->_out(sprintf('%.2f %.2f m', $x * $this->k, ($this->h - $y) * $this->k));
    }

    // Draws a line from last draw point
    // Parameters:
    // - x, y: End point
    function _Line($x, $y) {
        $this->_out(sprintf('%.2f %.2f l', $x * $this->k, ($this->h - $y) * $this->k));
    }

    // Draws a Bézier curve from last draw point
    // Parameters:
    // - x1, y1: Control point 1
    // - x2, y2: Control point 2
    // - x3, y3: End point
    function _Curve($x1, $y1, $x2, $y2, $x3, $y3) {
        $this->_out(sprintf('%.2f %.2f %.2f %.2f %.2f %.2f c', $x1 * $this->k, ($this->h - $y1) * $this->k, $x2 * $this->k, ($this->h - $y2) * $this->k, $x3 * $this->k, ($this->h - $y3) * $this->k));
    }
	
	var $angle=0;

function Rotate($angle,$x=-1,$y=-1)
{
    if($x==-1)
        $x=$this->x;
    if($y==-1)
        $y=$this->y;
    if($this->angle!=0)
        $this->_out('Q');
    $this->angle=$angle;
    if($angle!=0)
    {
        $angle*=M_PI/180;
        $c=cos($angle);
        $s=sin($angle);
        $cx=$x*$this->k;
        $cy=($this->h-$y)*$this->k;
        $this->_out(sprintf('q %.5f %.5f %.5f %.5f %.2f %.2f cm 1 0 0 1 %.2f %.2f cm',$c,$s,-$s,$c,$cx,$cy,-$cx,-$cy));
    }
}
}
?>