Full Flavour Behaviour!

FPDF Rounded Corners
June 12th @ 10:33am

FPDF is great but it's missing some functions, notably rounded corners on rectangles. So here's mine.

I actually had to bust out some trig to do this, for the first time in years. It works by drawing a large number of lines from the point you specify inwards to the boundary of an imagined quarter circle. Enough lines are drawn to give the impression of a solid shape which will mask the sharp corner of your image or table cell.

$pdf is the instance of FPDF with which you are working. $x and $y are the co-ordinates of the corner and $r is the radius of the rounded corner you want.

The $cnr variable should be from 1 to 4, with 1 being the lower right and then progressing clockwise, 4 being top right.

Hope it's useful to somebody!

function corner ($pdf, $cnr, $x, $y, $r) {
	
	// white to match document background
	$pdf->SetDrawColor(255,255,255);
	
	// define quad sectors in radians
	for ($i=0;$i<=2;$i+=0.5) {
		$lim[] = $i*pi();
	}
	
	// find x and y of virtual circle origin
	$d = $r-0.2; // you get neater ends if it's a little bit offset
	$ox = $x + ($cnr>1&&$cnr<4 ? $d : -$d);
	$oy = $y + ($cnr>2 ? +$d : -$d);
	
	$step = 1/($r*10); // more lines if it's larger
	for ($a=$lim[$cnr-1];$a<=$lim[$cnr]+$step;$a+=$step) {
		$yy = $r*sin($a);
		$xx = $r*cos($a);
		$pdf->Line($x,$y,$ox+$xx,$oy+$yy);
	}
	
}

Comment on this entry

Don't miss..

Other Carl sites

Photo galleries