Fix font height computation. Implement truncate and '...' on overflow.

This commit is contained in:
dave 2010-04-03 08:48:20 +00:00
parent a6887423d1
commit 7616982ff2

View File

@ -169,34 +169,40 @@ class pdf extends TCPDF {
$x = $this->getX();
$y = $this->getY();
$orig_fs = $this->getFontSizePt();
$add_ellipses = false;
$fontsize = $orig_fs;
while(1) {
$lines = $this->_fitcell_lines($w, $h, $txt, $fontsize);
$label_h = $this->cMargin * 2 + ($this->getFontSize($fontsize) * count($lines));
// echo "h=$h; fs=".$this->getFontSize($fontsize).", lines=".count($lines).", total=$label_h\n";
$cell_height = $this->cMargin * 2 + $this->FontSize;
$label_h = $cell_height * count($lines);
if($label_h <= $h) {
/* It fits! */
break;
}
/* else, it doesn't fit */
if($on_overflow == 'scale') {
/* reduce the font size and try again */
$fontsize -= 0.5;
$this->setFontSize($fontsize);
// echo "retry...\n";
continue;
} else if($on_overflow == '...') {
/* truncate, add ... */
echo "Not implemented: FitCell::on_overflow=...";
exit;
} else {
/* truncate */
echo "Not implemented: FitCell::on_overflow=truncate";
exit;
}
/* If it doesn't fit, and we're not scaling, it must
* be a truncate. Compute the number of lines that
* can be displayed */
$display_lines = floor($h / $cell_height);
/* Adjust height */
$label_h -= (count($lines) - $display_lines) * $cell_height;
/* truncate */
$lines = array_slice($lines, 0, $display_lines);
if($on_overflow == '...') $add_ellipses = true;
break;
}
/* SetX, find Y based on alignment */
@ -212,23 +218,29 @@ class pdf extends TCPDF {
break;
}
/* Fontsize will be correctly set here */
/* Cell( float $w, [float $h = 0], [string $txt = ''], [mixed $border = 0],
[int $ln = 0], [string $align = ''], [int $fill = 0], [mixed $link = ''],
[int $stretch = 0], [boolean $ignore_min_height = false]) */
$this->setFontSize($fontsize);
foreach($lines as $l) {
$this->Cell($w, 0, $l, 0, 2, $align, 0, 0, 0, false);
}
/* Restore original fontsize */
$this->setFontSize($orig_fs);
if($add_ellipses) {
/* Only use fontsize so the '...' is really close to the lower right. */
$this->SetXY($x, $y + $h - $cell_height);
$this->Cell($w, 0, '...', 0, 0, 'R');
}
/* Restore original location */
$this->SetXY($x,$y);
/* Restore original fontsize */
$this->setFontSize($orig_fs);
/* Deal with the border and ln, leaving x,y wherever $ln
* tells us to */
$this->Cell($w, $h, '', $border, $ln);
$this->Cell($w, $h, '', $border, $ln, 'R');
}
function GetFontList()
@ -327,11 +339,13 @@ class pdf extends TCPDF {
}
function output()
function output($filename='', $dest='I')
{
$filename=strtolower($this->subject);
$filename=ereg_replace("[^a-z0-9]","_",$filename);
parent::Output($filename.".pdf", "I");
if($filename == '') {
$filename=strtolower($this->subject);
$filename=ereg_replace("[^a-z0-9]","_",$filename).'.pdf';
}
parent::Output($filename, $dest);
}
/* align = left, center, right
@ -384,5 +398,4 @@ class pdf extends TCPDF {
{
$this->Rect($this->lMargin + $x, $this->tMargin + $y, $w, $h);
}
}