From f6907e3c1ac77228aadba185627998dd4eeaddf6 Mon Sep 17 00:00:00 2001 From: nicolaasuni Date: Fri, 26 Jan 2024 15:42:20 +0000 Subject: [PATCH] Add text shadow feature --- examples/index.php | 30 +++++++++++++++++++- src/Text.php | 70 +++++++++++++++++++++++++++++++++++++--------- 2 files changed, 86 insertions(+), 14 deletions(-) diff --git a/examples/index.php b/examples/index.php index 0e9a2c0..4318259 100644 --- a/examples/index.php +++ b/examples/index.php @@ -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'], diff --git a/src/Text.php b/src/Text.php index ee93800..4fd8ebf 100644 --- a/src/Text.php +++ b/src/Text.php @@ -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 { @@ -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, @@ -84,6 +93,7 @@ public function getTextLine( bool $stroke = false, bool $clip = false, string $forcedir = '', + ?array $shadow = null, ): string { if ($txt === '') { return ''; @@ -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,