Skip to content

Commit

Permalink
Add draft text methods and examples
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolaasuni committed Mar 25, 2024
1 parent 5b6510d commit 0316bff
Show file tree
Hide file tree
Showing 7 changed files with 776 additions and 61 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ PATHDEBPKG=$(TARGETDIR)/DEB
PATHBZ2PKG=$(TARGETDIR)/BZ2

# Default port number for the example server
PORT?=8000
PORT?=8971

# PHP binary
PHP=$(shell which php)
Expand Down Expand Up @@ -247,7 +247,7 @@ rpm:
# Start the development server
.PHONY: server
server:
$(PHP) -t example -S localhost:$(PORT)
$(PHP) -t examples -S localhost:$(PORT)

# Tag this GIT version
.PHONY: tag
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ Start a development server (requires PHP 8.0+) using the command:
make server
```

and point your browser to <http://localhost:8000/index.php>
and point your browser to <http://localhost:8971/index.php>


## Installation
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"tecnickcom/tc-lib-barcode": "^2.1",
"tecnickcom/tc-lib-color": "^2.0",
"tecnickcom/tc-lib-pdf-image": "^2.0",
"tecnickcom/tc-lib-pdf-font": "^2.1",
"tecnickcom/tc-lib-pdf-font": "^2.2",
"tecnickcom/tc-lib-file": "^2.0",
"tecnickcom/tc-lib-pdf-encrypt": "^2.1",
"tecnickcom/tc-lib-unicode-data": "^2.0",
Expand Down
111 changes: 107 additions & 4 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 @@ -1058,8 +1065,6 @@

$pdf->page->addContent($txt2);



// get the coordinates of the box containing the last added text string.
$bbox = $pdf->getLastTextBBox();

Expand All @@ -1075,6 +1080,104 @@
);
$pdf->page->addAnnotRef($aoid);

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

// add a text column with automatic line breaking

$bfont3 = $pdf->font->insert($pdf->pon, 'courier', '', 14);
$pdf->page->addContent($bfont3['out']);

$txt3 = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.'."\n".'Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.';

// single block of text
$txtbox = $pdf->getTextCol(
$txt3,
20, // float $posx = 0,
30, // float $posy = 0,
150, // float $width = 0,
15, // float $offset = 0,
1, // float $linespace = 0,
0, // float $strokewidth = 0,
0, // float $wordspacing = 0,
0, // float $leading = 0,
0, // float $rise = 0,
true, // bool $justify = false,
false, // bool $justifylast = false,
true, // bool $fill = true,
false, // bool $stroke = false,
false, // bool $clip = false,
'', // string $forcedir = '',
null, // ?array $shadow = null,
);
$pdf->page->addContent($txtbox);


$bfont4 = $pdf->font->insert($pdf->pon, 'dejavusans', '', 14);
$pdf->page->addContent($bfont4['out']);

// block of text between two page regions
$pdf->addTextCol(
$txt3,
20, // float $posx = 0,
275, // float $posy = 0,
150, // float $width = 0,
15, // float $offset = 0,
1, // float $linespace = 0,
0, // float $strokewidth = 0,
0, // float $wordspacing = 0,
0, // float $leading = 0,
0, // float $rise = 0,
true, // bool $justify = false,
false, // bool $justifylast = false,
true, // bool $fill = true,
false, // bool $stroke = false,
false, // bool $clip = false,
'', // string $forcedir = '',
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',
],
];

$bfont4 = $pdf->font->insert($pdf->pon, 'freeserif', 'I', 14);
$pdf->page->addContent($bfont4['out']);

$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);
}
}
65 changes: 42 additions & 23 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 All @@ -342,15 +346,17 @@ protected function cellTextHAlign(
}

/**
* Returns the top Y coordinate of the cell wrapping the text.
* Returns the baseline Y coordinate of the cell wrapping the text.
*
* @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,19 +413,30 @@ 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));
}

/**
* Sets the page context by adding the previous page font and graphic settings.
*
* @return void
*/
protected function setPageContext(): void
{
$this->page->addContent($this->font->getOutCurrentFont());
$this->page->addContent($this->graph->getStyle());
}
}
Loading

0 comments on commit 0316bff

Please sign in to comment.