<?

# Needs FPDF Printing
require($FPDF_LOCATION.'/fpdf.php');

# Some printing configuration
$PRINT_MARGINS = .5; # In inches
$PRINT_TITLE_SIZE = 18; # In Points
$PRINT_NOTES_SIZE = 12; # In Points

function printPDF($xml, $outputPDF) {
	global $PRINT_MARGINS;
	global $PRINT_TITLE_SIZE;
	global $PRINT_NOTES_SIZE;

	$inputObj = $xml->getElementsByTagName('input')->item(0);

	$extent = split(' ',$inputObj->getAttribute('mapext'));

	$canvasObj = $xml->getElementsByTagName('canvas')->item(0);

	# G'head and get the map title
	$mapTitle = false;
	for($i = 0; $i < $canvasObj->childNodes->length && !$mapTitle; $i++) {
		$child = $canvasObj->childNodes->item($i);
		if($child->tagName == 'param') {
			if($child->getAttribute('name') == 'title') {
				$mapTitle = $child->getAttribute('value');
			}
		}
	}

	# Page size also takes care of orientation
	$pageSize = split(' ', $inputObj->getAttribute('pagesize'));

	$legends = ($inputObj->hasAttribute('legend') and $inputObj->getAttribute('legend') == 'yes');
	$northArrow = ($inputObj->hasAttribute('north') and $inputObj->getAttribute('north') == 'yes');
	$printQuality = ($inputObj->hasAttribute('quality') ? $inputObj->getAttribute('quality') : 1);

	$displayGutter = ($legends or $northArrow);

	# Setup the initial map size
	$mapWidth = $pageSize[0] - $PRINT_MARGINS*2;
	$mapHeight = $pageSize[1] - $PRINT_MARGINS*2;

	# Now intialize the PDF
	$pdf = new FPDF('L','in', array($pageSize[1], $pageSize[0]));
	$pdf->SetAutoPageBreak(false);
	$pdf->AddPage();
	$pdf->SetMargins($PRINT_MARGINS, $PRINT_MARGINS, $PRINT_MARGINS);
	$pdf->SetDrawColor(0,0,0);
	$pdf->SetFillColor(0,0,0);
	$pdf->SetX(0);
	$pdf->SetY(0);

	# Makes a nice inital page border
	$pdf->Rect($PRINT_MARGINS, $PRINT_MARGINS, 
		$pageSize[0]-$PRINT_MARGINS*2, 
		$pageSize[1]-$PRINT_MARGINS*2, 'D');


	# Check to see if we need to get some legends...
	$gutterWidth = 0;


	# Is there a title, if so, print a title bar
	$imageTop = $PRINT_MARGINS;
	if($mapTitle) {
		$imageTop += ($PRINT_TITLE_SIZE+2)/72;
		$pdf->Rect($PRINT_MARGINS, $PRINT_MARGINS, 
			$pageSize[0]-$PRINT_MARGINS*2, 
			($PRINT_TITLE_SIZE+2)/72, 'F');
		# adjust the map height	
		$mapHeight -= ($PRINT_TITLE_SIZE+2)/72;	

		$pdf->SetTextColor(255,255,255);
		$pdf->SetX($PRINT_MARGINS+2/72);
		$pdf->SetY($PRINT_MARGINS);
		$pdf->SetFont('Arial', '', $PRINT_TITLE_SIZE);
		$pdf->Cell(0,.25,$mapTitle,0,0,'L');
	}



	if($northArrow) {
		$image = imagecreatefromjpeg("resources/north.jpg");
		$gutterWidth = imagesx($image);
		$width = $gutterWidth/72;
		$height = imagesy($image)/72;
		$pdf->Image("resources/north.jpg", 
			$pageSize[0]-$PRINT_MARGINS-$width,
			$pageSize[1]-$height-$PRINT_MARGINS);
	}
	if($legends) {
		# Oh snap, we need legends
		$maps = $xml->getElementsByTagName('map');
		$legendTop = $imageTop+1/72;

		$imageArray = array();
		$legendCount = 0;
		for($m = 0; $m < $maps->length; $m++) {
			$map = $maps->item($m);
			$file = $map->getElementsByTagName('file')->item(0)->nodeValue;
			if((strpos($file, 'relative://') === false) and (strpos($file, 'http://') === false) and $file) {
				$mapfile = ms_newMapObj(getPath($file));
				$mapfile->setSize($mapWidth*72, $mapHeight*72);
				$mapfile->setExtent($extent[0], $extent[1], $extent[2], $extent[3]);
				if($pName == 'layer' or $pName == 'qlayer' or $pName == 'layers') {
					$layerNames = split(' ', $param->getAttribute('value'));
					# Occasionally "layers=" will be set to null
					# this checks to make sure that the value is an actual list.
					if(count($layerNames) > 0 and $layerNames[0] != "") {
						for($l = 0; $l < $mapfile->numlayers; $l++) {
							$layer = $mapfile->getLayer($l);
							$layerName = $layer->name;
							$layerOn = false;
							foreach($layerNames as $testName) {
								if($testName == $layerName or $layer->group == $layerName) {
									$layerOn = true;
								}
							}
							if($layerOn) {
								$layer->set("status", MS_DEFAULT);
							} else {
								$layer->set("status", MS_OFF);
							}
						}
					}
				}
				$mapfile->selectOutputFormat("JPEG");
				$image = $mapfile->prepareImage();
				$image = $mapfile->drawLegend();
				$image->saveImage($outputPDF."_".$legendCount."_legend.jpeg");

				$image = imagecreatefromjpeg($outputPDF."_".$legendCount."_legend.jpeg");
				if(ImageSX($image) > $gutterWidth) {
					$gutterWidth = ImageSX($image);
				}
				
				$legendCount++;
			}
		}
		for($i = 0; $i < $legendCount; $i++) {
			$image = imagecreatefromjpeg($outputPDF."_".$i."_legend.jpeg");
			if(ImageSY($image) > 5) {
				$pdf->Image($outputPDF."_".$i."_legend.jpeg",
					$pageSize[0]-$PRINT_MARGINS-$gutterWidth/72+1/72,
					$legendTop);

				$legendTop += ImageSY($image) / 72;
			}

		}
	}


	# Convert the gutter pixel width into inches
	# This also determines the mapwidth (in inches)
	$gutterWidth = ($gutterWidth + 1)/ 72;
	if($displayGutter) {
		# Draw the gutter border
		$pdf->Rect($pageSize[0]-$PRINT_MARGINS-$gutterWidth, 
			$PRINT_MARGINS, $gutterWidth, 
			$pageSize[1]-$PRINT_MARGINS*2, 'D');
		$mapWidth -= $gutterWidth;
	}

	# Image stand in
	$pdf->setFillColor(255,0,255);
	$imageRight = $pageSize[0]-$PRINT_MARGINS*2;
	if($displayGutter) {
		$imageRight -= $gutterWidth;
	}

	$mapBottom = $PRINT_MARGINS;

	$messages = $xml->getElementsByTagName('message');

	if($messages->length > 0) {
		$msg = "";
		for($m = 0; $m < $messages->length; $m++) {
			$message = $messages->item($m);
			$msg = $msg . $message->nodeValue;
		}
		$pdf->SetFont('Arial','', $PRINT_NOTES_SIZE);
		$pdf->SetTextColor(0,0,0);
		$pdf->SetX($PRINT_MARGINS+2/72);
		$pdf->SetY($pageSize[1]-$mapBottom-($PRINT_NOTES_SIZE+3)/72);
		$pdf->Cell(0,.25,$msg,0,0,'L');
		$mapBottom += $PRINT_NOTES_SIZE / 72;

		$pdf->Rect($PRINT_MARGINS, $pageSize[1]-$PRINT_MARGINS-$PRINT_NOTES_SIZE/72, 
			$pageSize[0]-$PRINT_MARGINS*2-$gutterWidth, 
			$PRINT_NOTES_SIZE / 72, 'D');

	}

	$imagePixelHeight = floor($pageSize[1]-$mapBottom-$imageTop-1/72)*72*$printQuality;
	$imagePixelWidth =  floor($imageRight-1/72)*72*$printQuality;
	
	renderImage($xml, $outputPDF.'.jpeg', $imagePixelWidth, $imagePixelHeight,
		$extent[0], $extent[1], $extent[2], $extent[3], $printQuality);

	$pdf->Image($outputPDF.'.jpeg', $PRINT_MARGINS+.5/72, $imageTop+.5/72,
		$imageRight-1/72, $pageSize[1]-$mapBottom-$imageTop-1/72);
	$pdf->Output($outputPDF.'.pdf');
}
?>
