Skip to content

Commit

Permalink
Draft text cell
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolaasuni committed Mar 25, 2024
1 parent 84d9d3c commit abb2a2d
Show file tree
Hide file tree
Showing 4 changed files with 216 additions and 26 deletions.
52 changes: 49 additions & 3 deletions examples/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,20 @@
define('OUTPUT_FILE', '../target/example.pdf');

// define fonts directory
define('K_PATH_FONTS', '../vendor/tecnickcom/tc-lib-pdf-font/target/fonts/core/');
define('K_PATH_FONTS', '../vendor/tecnickcom/tc-lib-pdf-font/target/fonts');

// autoloader when using RPM or DEB package installation
//require ('/usr/share/php/Com/Tecnick/Pdf/autoload.php');

// main TCPDF object
$pdf = new \Com\Tecnick\Pdf\Tcpdf('mm', true, false, true, '');
$pdf = new \Com\Tecnick\Pdf\Tcpdf(
'mm', // string $unit = 'mm',
true, // bool $isunicode = true,
false, // bool $subsetfont = false,
true, // bool $compress = true,
'', // string $mode = '',
null, // ?ObjEncrypt $objEncrypt = null,
);

// ----------

Expand Down Expand Up @@ -1077,7 +1084,7 @@

// add a text column with automatic line breaking

$bfont3 = $pdf->font->insert($pdf->pon, 'times', 'I', 14);
$bfont3 = $pdf->font->insert($pdf->pon, 'times', '', 14);

$pdf->page->addContent($bfont3['out']);

Expand Down Expand Up @@ -1126,6 +1133,45 @@
null, // ?array $shadow = null,
);

// Text cell
$style_cell = [
'all' => [
'lineWidth' => 0.5,
'lineCap' => 'butt',
'lineJoin' => 'miter',
'miterLimit' => 0.5,
'dashArray' => [],
'dashPhase' => 0,
'lineColor' => 'red',
'fillColor' => 'yellow',
],
];

$pdf->setDefaultCellPadding(1,1,1,1);
$txtcell = $pdf->getTextCell(
'Lorem ipsum dolor sit amet', // string $txt,
20, // float $posx = 0,
60, // float $posy = 0,
0, // float $width = 0,
0, // float $height = 0,
'C', // string $valign = 'C',
'C', // string $halign = 'C',
null, // ?array $cell = null,
$style_cell, // array $styles = [],
0, // float $strokewidth = 0,
0, // float $wordspacing = 0,
0, // float $leading = 0,
0, // float $rise = 0,
false, // bool $justify = false,
true, // bool $fill = true,
false, // bool $stroke = false,
false, // bool $clip = false,
'', // string $forcedir = '',
null // ?array $shadow = null,
);
$pdf->page->addContent($txtcell);


// ----------

// get PDF document as raw string
Expand Down
2 changes: 1 addition & 1 deletion src/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,6 @@ public function toYPoints(float $usr, float $pageh = -1): float
public function toYUnit(float $pnt, float $pageh = -1): float
{
$pageh = $pageh >= 0 ? $pageh : $this->page->getPage()['pheight'];
return ($pageh - $this->toUnit($pnt));
return $this->toUnit($pageh - $pnt);
}
}
52 changes: 30 additions & 22 deletions src/Cell.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,10 +163,12 @@ protected function adjustMinCellPadding(
* Returns the minimum cell height in points for the current font.
*
* @param string $align Text vertical alignment inside the cell:
* - T=top; - C=center; - B=bottom; -
* A=center-on-font-ascent; -
* L=center-on-font-baseline; -
* D=center-on-font-descent.
* - T=top;
* - C=center;
* - B=bottom;
* - A=center-on-font-ascent;
* - L=center-on-font-baseline;
* - D=center-on-font-descent.
* @param ?TCellDef $cell Optional to overwrite cell parameters for padding, margin etc.
*/
protected function cellMinHeight(
Expand Down Expand Up @@ -279,10 +281,12 @@ protected function cellHPos(
*
* @param float $pheight Cell height in internal points.
* @param string $align Text vertical alignment inside the cell:
* - T=top; - C=center; - B=bottom; -
* A=center-on-font-ascent; -
* L=center-on-font-baseline; -
* D=center-on-font-descent.
* - T=top;
* - C=center;
* - B=bottom;
* - A=center-on-font-ascent;
* - L=center-on-font-baseline;
* - D=center-on-font-descent.
* @param ?TCellDef $cell Optional to overwrite cell parameters for padding, margin etc.
*/
protected function cellTextVAlign(
Expand Down Expand Up @@ -317,7 +321,7 @@ protected function cellTextVAlign(
*
* @param float $pwidth Cell width in internal points.
* @param float $txtpwidth Text width in internal points.
* @param string $align Text vertical alignment inside the cell: L=left; C=center; R=right.
* @param string $align Text horizontal alignment inside the cell: L=left; C=center; R=right.
* @param ?TCellDef $cell Optional to overwrite cell parameters for padding, margin etc.
*/
protected function cellTextHAlign(
Expand Down Expand Up @@ -347,10 +351,12 @@ protected function cellTextHAlign(
* @param float $txty Text baseline top Y coordinate in internal points.
* @param float $pheight Cell height in internal points.
* @param string $align Text vertical alignment inside the cell:
* - T=top; - C=center; - B=bottom; -
* A=center-on-font-ascent; -
* L=center-on-font-baseline; -
* D=center-on-font-descent.
* - T=top;
* - C=center;
* - B=bottom;
* - A=center-on-font-ascent;
* - L=center-on-font-baseline;
* - D=center-on-font-descent.
* @param ?TCellDef $cell Optional to overwrite cell parameters for padding, margin etc.
*/
protected function cellVPosFromText(
Expand All @@ -368,7 +374,7 @@ protected function cellVPosFromText(
* @param float $txtx Text left X coordinate in internal points.
* @param float $pwidth Cell width in internal points.
* @param float $txtpwidth Text width in internal points.
* @param string $align Text vertical alignment inside the cell: L=left; C=center; R=right.
* @param string $align Text horizontal alignment inside the cell: L=left; C=center; R=right.
* @param ?TCellDef $cell Optional to overwrite cell parameters for padding, margin etc.
*/
protected function cellHPosFromText(
Expand All @@ -387,10 +393,12 @@ protected function cellHPosFromText(
* @param float $pnty Cell top Y coordinate in internal points.
* @param float $pheight Cell height in internal points.
* @param string $align Text vertical alignment inside the cell:
* - T=top; - C=center; - B=bottom; -
* A=center-on-font-ascent; -
* L=center-on-font-baseline; -
* D=center-on-font-descent.
* - T=top;
* - C=center;
* - B=bottom;
* - A=center-on-font-ascent;
* - L=center-on-font-baseline;
* - D=center-on-font-descent.
* @param ?TCellDef $cell Optional to overwrite cell parameters for padding, margin etc.
*/
protected function textVPosFromCell(
Expand All @@ -405,20 +413,20 @@ protected function textVPosFromCell(
/**
* Returns the left X coordinate of the text inside the cell.
*
* @param float $txtx Text left X coordinate in internal points.
* @param float $pntx Cell left X coordinate in internal points.
* @param float $pwidth Cell width in internal points.
* @param float $txtpwidth Text width in internal points.
* @param string $align Text vertical alignment inside the cell: L=left; C=center; R=right.
* @param string $align Text horizontal alignment inside the cell: L=left; C=center; R=right.
* @param ?TCellDef $cell Optional to overwrite cell parameters for padding, margin etc.
*/
protected function textHPosFromCell(
float $txtx,
float $pntx,
float $pwidth,
float $txtpwidth,
string $align = 'L',
?array $cell = null
): float {
return ($txtx + $this->cellTextHAlign($pwidth, $txtpwidth, $align, $cell));
return ($pntx + $this->cellTextHAlign($pwidth, $txtpwidth, $align, $cell));
}

/**
Expand Down
136 changes: 136 additions & 0 deletions src/Text.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
* @link https://github.com/tecnickcom/tc-lib-pdf
*
* @phpstan-import-type TTextDims from \Com\Tecnick\Pdf\Font\Stack
* @phpstan-import-type StyleDataOpt from \Com\Tecnick\Pdf\Cell
* @phpstan-import-type TCellDef from \Com\Tecnick\Pdf\Cell
*
* @phpstan-type TextBBox array{
* 'x': float,
Expand Down Expand Up @@ -73,6 +75,140 @@ abstract class Text extends \Com\Tecnick\Pdf\Cell
'height' => 0,
];

/**
* Returns the PDF code to render a single text line inside a rectangular cell.
*
* @param string $txt Text string to be processed.
* @param float $posx Abscissa of upper-left corner.
* @param float $posy Ordinate of upper-left corner.
* @param float $width Width.
* @param float $height Height.
* @param string $valign Text vertical alignment inside the cell: T=top; C=center; B=bottom.
* @param string $halign Text horizontal alignment inside the cell: L=left; C=center; R=right.
* @param ?TCellDef $cell Optional to overwrite cell parameters for padding, margin etc.
* @param array<int, StyleDataOpt> $styles Cell border styles (see: getCurrentStyleArray).
* @param float $strokewidth Stroke width.
* @param float $wordspacing Word spacing (use it only when justify == false).
* @param float $leading Leading.
* @param float $rise Text rise.
* @param bool $justify If true justify te text via word spacing.
* @param bool $fill If true fills the text.
* @param bool $stroke If true stroke the text.
* @param bool $clip If true activate clipping mode.
* @param string $forcedir If 'R' forces RTL, if 'L' forces LTR.
* @param ?TextShadow $shadow Text shadow parameters.
*/
public function getTextCell(
string $txt,
float $posx = 0,
float $posy = 0,
float $width = 0,
float $height = 0,
string $valign = 'C',
string $halign = 'C',
?array $cell = null,
array $styles = [],
float $strokewidth = 0,
float $wordspacing = 0,
float $leading = 0,
float $rise = 0,
bool $justify = false,
bool $fill = true,
bool $stroke = false,
bool $clip = false,
string $forcedir = '',
?array $shadow = null,
): string {
if ($txt === '') {
return '';
}

$ordarr = [];
$dim = [];
$this->prepareText($txt, $ordarr, $dim, $forcedir);
$txt_pwidth = $dim['totwidth'];

$cell_pheight = $this->toPoints($height);
$cell = $this->adjustMinCellPadding($styles, $cell);
if ($height <= 0) {
$cell_pheight = $this->cellMinHeight($valign, $cell);
}

$cell_pwidth = $this->toPoints($width);
if ($width <= 0) {
$cell_pwidth = $this->cellMinWidth($txt_pwidth, $halign, $cell);
}

$pntx = $this->toPoints($posx);
$pnty = $this->toYPoints($posy);

$cell_pntx = $this->cellHPos($pntx, $cell_pwidth, 'L', $cell);
$cell_pnty = $this->cellVPos($pnty, $cell_pheight, 'T', $cell);

$txt_pntx = $this->textHPosFromCell(
$cell_pntx,
$cell_pwidth,
$txt_pwidth,
$halign,
$cell
);

$curfont = $this->font->getCurrentFont();
$fontascent = $this->toUnit($curfont['ascent']);

$txt_pnty = $this->textVPosFromCell(
$cell_pnty,
$cell_pheight,
$valign,
$cell
);

$txt_out = $this->getOutTextLine(
$txt,
$ordarr,
$dim,
$this->toUnit($txt_pntx),
$this->toYUnit($txt_pnty),
($justify ? $this->toUnit($txt_pwidth) : 0),
$strokewidth,
$wordspacing,
$leading,
$rise,
$fill,
$stroke,
$clip,
$shadow,
);

$cell_out = $this->graph->getStartTransform();

$cell_mode = empty($styles['all']['fillColor']) ? 's' : 'b';

if (count($styles) <= 1) {
$cell_out .= $this->graph->getBasicRect(
$this->toUnit($cell_pntx),
$this->toYUnit($cell_pnty),
$this->toUnit($cell_pwidth),
$this->toUnit($cell_pheight),
$cell_mode,
(empty($styles['all']) ? [] : $styles['all']),
);
} else {
$cell_out .= $this->graph->getRect(
$this->toUnit($cell_pntx),
$this->toYUnit($cell_pnty),
$this->toUnit($cell_pwidth),
$this->toUnit($cell_pheight),
$cell_mode,
$styles,
);
}

$cell_out .= $this->graph->getStopTransform();

return $cell_out . $txt_out;
}

/**
* Add a text column with automatic line, page and region breaks.
*
Expand Down

0 comments on commit abb2a2d

Please sign in to comment.