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