Skip to content

Commit

Permalink
Add text shadow feature
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolaasuni committed Jan 26, 2024
1 parent fadedd4 commit f6907e3
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 14 deletions.
30 changes: 29 additions & 1 deletion examples/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -1031,9 +1031,37 @@

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

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

// Add text
$txt2 = $pdf->getTextLine(
'Link to https://tcpdf.org',
15,
($bbox['y'] + $bbox['height'] + $pdf->toUnit($bfont2['ascent'])),
0,
0,
0,
0,
0,
true,
false,
false,
'',
[
'xoffset' => 0.5,
'yoffset' => 0.5,
'opacity' => 0.5,
'mode' => 'Normal',
'color' => 'red',
],
);

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



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

$aoid = $pdf->setAnnotation(
$bbox['x'],
Expand Down
70 changes: 57 additions & 13 deletions src/Text.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@
* 'width': float,
* 'height': float,
* }
*
* @phpstan-type TextShadow array{
* 'xoffset': float,
* 'yoffset': float,
* 'opacity': float,
* 'mode': string,
* 'color': string,
* }
*/
abstract class Text extends \Com\Tecnick\Pdf\Cell
{
Expand All @@ -58,18 +66,19 @@ abstract class Text extends \Com\Tecnick\Pdf\Cell
/**
* Returns the PDF code to render a line of text.
*
* @param string $txt Text string to be processed.
* @param float $posx X position relative to the start of the current line.
* @param float $posy Y position relative to the start of the current line (font baseline).
* @param float $width Desired string width to force justification via word spacing (0 = automatic).
* @param float $strokewidth Stroke width.
* @param float $wordspacing Word spacing (use it only when width == 0).
* @param float $leading Leading.
* @param float $rise Text rise.
* @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 string $txt Text string to be processed.
* @param float $posx X position relative to the start of the current line.
* @param float $posy Y position relative to the start of the current line (font baseline).
* @param float $width Desired string width to force justification via word spacing (0 = automatic).
* @param float $strokewidth Stroke width.
* @param float $wordspacing Word spacing (use it only when width == 0).
* @param float $leading Leading.
* @param float $rise Text rise.
* @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 getTextLine(
string $txt,
Expand All @@ -84,6 +93,7 @@ public function getTextLine(
bool $stroke = false,
bool $clip = false,
string $forcedir = '',
?array $shadow = null,
): string {
if ($txt === '') {
return '';
Expand All @@ -92,7 +102,41 @@ public function getTextLine(
$ordarr = [];
$dim = [];
$this->prepareText($txt, $ordarr, $dim);
return $this->outTextLine(

$out = '';

if (!empty($shadow)) {
if ($shadow['xoffset'] < 0) {
$posx += $shadow['xoffset'];
}

if ($shadow['yoffset'] < 0) {
$posy += $shadow['yoffset'];
}

$out .= $this->graph->getStartTransform();
$out .= $this->color->getPdfColor($shadow['color'], false);
$out .= $this->graph->getAlpha($shadow['opacity'], $shadow['mode']);
$out .= $this->outTextLine(
$txt,
$ordarr,
$dim,
$posx + $shadow['xoffset'],
$posy + $shadow['yoffset'],
$width,
0,
$wordspacing,
$leading,
$rise,
true,
false,
false,
$forcedir
);
$out .= $this->graph->getStopTransform();
}

return $out . $this->outTextLine(
$txt,
$ordarr,
$dim,
Expand Down

0 comments on commit f6907e3

Please sign in to comment.