<?php
include("_config/addCfg.php");
include("_includes/addInc.php");

/*
 * generates the left & top rulers.
 * 	== maybe should be separated in 2 separated rulers... for memory conservation on both the client and server side ==
 *
 * 	call it as	makeRuler.php?w=__&h=__
 * 		- w - the width opf the generated image
 * 		- h - the height of the final generated image
 *
*/




/*
 * function to convert a hex color string to a rgb array.
 * when $rgb is set, it will only extract that specific color.
*/
function hexrgb($hexstr,$rgb='') {
	$int = hexdec($hexstr);
	switch($rgb) {
		case "r":
			return 0xFF & $int >> 0x10;
		break;
		case "g":
			return 0xFF & ($int >> 0x8);
		break;
		case "b":
			return 0xFF & $int;
		break;
		default:
			return array(
				"r" => 0xFF & $int >> 0x10,
				"g" => 0xFF & ($int >> 0x8),
				"b" => 0xFF & $int
			);
		break;
	}
}

$vertical=($R['dir']=='Y');

$allowedDimensions=Array(
		100=>true,
		50=>true,
		10=>true,
		5=>true,
		1=>true,
	);
$allowedLineDimensions=$allowedDimensions;
	
// initially set the internal $w and $h
$w=$R['w'];
$h=$R['h'];

// only choose positive greater-than-zero values
$w=($w?max($w,0):1);
$h=($h?max($h,0):1);

/*
 * add ~41px to each side of the image.
 * The 26part comes from the default offset from the begginning of the image and the beginning of the document container
*/
$w+=round(26*1.3);
$h+=round(26*1.3);

// limit the grid-max-size - WARNING - 8192 may get reached preatty easy if using a BIG zoom level, and a multi-paged document.
$w=min(8192,$w);
$h=min(8192,$h);


$gridCt=($S[$thisSection]['page']['gridCt']?$S[$thisSection]['page']['gridCt']:$CFG['DTM']['module']['gridCt']);
$zoom=$S[$thisSection]['page']['zoom'];

if(mm2px(100)<300){
	$allowedDimensions[1]=false;
	
	if(mm2px(100)<200){
		$allowedDimensions[5]=false;
		$allowedLineDimensions[1]=false;
		
		if(mm2px(100)<100){
			$allowedDimensions[10]=false;
			$allowedLineDimensions[5]=false;
		}
	}
}

if($vertical){
	$w=26;
}else{
	$h=26;
}

// the outputFile. this will decide if a chached file will be used, or a new one will simply be served to the browser.
$outputFile="makeRuler_".($vertical?'Y':'X').".{$w}x{$h}@".mm2px(100);
$outputFile=readCachedFile($outputFile); // try to read and display the requested image from cache. script might end here if cached image is found


// if the cached image was found, then this point would not get reached
$image=imagecreate($w, $h);

$color='ffffff';
$colorWhite=imagecolorallocate($image, hexrgb($color,'r'), hexrgb($color,'g'), hexrgb($color,'b'));

$color='bfbfbf';
$colorGrey[2]=imagecolorallocate($image, hexrgb($color,'r'), hexrgb($color,'g'), hexrgb($color,'b'));

$color='808080';
$colorGrey[1]=imagecolorallocate($image, hexrgb($color,'r'), hexrgb($color,'g'), hexrgb($color,'b'));

$color='000000';
$colorGrey[0]=imagecolorallocate($image, hexrgb($color,'r'), hexrgb($color,'g'), hexrgb($color,'b'));

$color='000000';
$colorBlack=imagecolorallocate($image, hexrgb($color,'r'), hexrgb($color,'g'), hexrgb($color,'b'));

$units['mm']=$gridCt*$zoom;

$unit=$units['mm'];

$period=$unit;

$size['width']=px2mm($w);
$size['height']=px2mm($h);

$multimples=Array(
					'100'=>$colorGrey[0],
					'50'=>$colorGrey[0],
					'10'=>$colorGrey[0],
					'5'=>$colorGrey[1],
					'1'=>$colorGrey[2],
					);

$border=25;
$heights=Array(
					'100'=>$border*0.40 ,
					'50'=>$border*0.40 ,
					'10'=>$border*0.40 ,
					'5'=> $border*0.60 ,
					'1'=> $border*0.80 ,
					);

$fontColor=Array(
					'100'=>$colorGrey[0],
					'50'=>$colorGrey[1],
					'10'=>$colorGrey[2],
					'5'=>0,
					'1'=>0,
					);

$offsetX=25;
$offsetY=-2;

$dimensionX=25;
$dimensionY=24;

imagefill($image, 0, 0,$colorWhite);


/******* draw the horizontal ruler ****************************************************/
if(!$vertical){
	imageline($image, $offsetX+0, 0, $offsetX+0, $border, $colorBlack);
	imageline($image, 0, $dimensionY, $offsetX, $dimensionY, $colorBlack);
	
	imageline($image, $offsetX+0, $border-1, $offsetX+$w, $border-1, $colorBlack);

	for($i=0;$i<$size['width'];$i++){
		$x1=round($offsetX+($i*$period));
		$x2=$x1;
		$y1=0;
		$y2=$dimensionY-2;
	
		foreach($multimples as $index=>$multiple){
			if($i % $index == 0){
				$currentPoint=sprintf("%.0f",$i/10);
				$y1=$heights[$index];
				if($fontColor[$index] && $currentPoint!=$lastPoint && $allowedDimensions[$index]){
					if($currentPoint==0){
						$textWidth=1*5;
						imagestring($image,4,$x1-10,9,$currentPoint,$fontColor[10]);
					}else{
						$textWidth=strlen($currentPoint)*5;
						imagestring($image,2,round($x1-$textWidth/2),-2,$currentPoint,$fontColor[$index]);
					}
				}
				if($index%10==0)$y2=$dimensionY;
				
				if($allowedLineDimensions[$index]){
					imageline($image, $x1, $y1, $x2, $y2, $multimples[$index]);
				}
				break;
			}
			$coloridx++;
		}
	}
}

if($vertical){
	/******* draw the vertical ruler ****************************************************/
	//imageline($image, 0, $offsetY, $offsetX, $offsetY, $colorBlack);
	
	imageline($image, $offsetX, $offsetY, $offsetX, $offsetY+$h, $colorBlack);
	
	for($i=0;$i<$size['height'];$i++){
		$x2=$offsetX-2;
		$y1=round($offsetY+($i*$period));
		$y2=$y1;
	
		foreach($multimples as $index=>$multiple){
			if($i % $index == 0){
				$currentPoint=sprintf("%.0f",$i/10);
				$x1=$heights[$index];
				if($fontColor[$index] && $currentPoint!=$lastPoint && $allowedDimensions[$index]){
					if($currentPoint==0){
						$textWidth=1*5;
						imagestring($image,4,$x1+6,$y1-15,$currentPoint,$fontColor[10]);
					}else{
						$textWidth=strlen($currentPoint)*5;
						imagestringup($image,2,round($x1-$dimensionX/2),round($y1+$textWidth/2),$currentPoint,$fontColor[$index]);
					}
				}
				if($index%10==0)$x2=$dimensionX;
				
				if($allowedLineDimensions[$index]){
					imageline($image, $x1, $y1, $x2, $y2, $multimples[$index]);
				}
				break;
			}
			$coloridx++;
		}
	}
}

// save the image to cache and stream it to the browser.
switch($CFG['DTM']['imageCache']['output']){
	case 'png':
		if($CFG['DTM']['imageCache']['enabled'])
			imagepng($image,$outputFile);

		header("Content-type: image/png");
		imagepng($image);
	break;
	case 'jpg':
		if($CFG['DTM']['imageCache']['enabled'])
			imagejpeg($image,$outputFile,65);

		header("Content-type: image/jpeg");
		imagejpeg($image);
	break;
	case 'gif':
		if($CFG['DTM']['imageCache']['enabled'])
			imagegif($image,$outputFile);

		header("Content-type: image/gif");
		imagegif($image);
	break;
}

imagedestroy($image);

