Skip to content

Commit

Permalink
Add annotation support
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolaasuni committed Oct 3, 2023
1 parent 00cec2c commit f687e34
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 30 deletions.
23 changes: 12 additions & 11 deletions examples/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -933,17 +933,18 @@
$pdf->page->addContent($txt);

// // get the coordinates of the box containing the last added text string.
// $bbox = $pdf->getLastTextBBox();
// // set link
// $aoid = $pdf->setAnnotation(
// $bbox['llx'],
// $bbox['lly'],
// $bbox['urx'] - $bbox['llx'],
// $bbox['lly'] - $bbox['ury'],
// 'https://tcpdf.org',
// array('subtype'=>'Link')
// );
// $pdf->page->addAnnotRef($aoid);
$bbox = $pdf->getLastTextBBox();

// set link
$aoid = $pdf->setAnnotation(
$bbox['x'],
$bbox['y'],
$bbox['width'],
$bbox['height'],
'https://tcpdf.org',
array('subtype'=>'Link')
);
$pdf->page->addAnnotRef($aoid);

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

Expand Down
11 changes: 5 additions & 6 deletions src/Output.php
Original file line number Diff line number Diff line change
Expand Up @@ -818,12 +818,12 @@ protected function getOutAnnotations()
$annot = $this->annotation[$oid];
$annot['opt'] = array_change_key_case($annot['opt'], CASE_LOWER);
$out .= $this->getAnnotationRadiobuttonGroups($annot);
$orx = $this->userToPointsUnit($annot['x']);
$ory = $page['height'] - $this->userToPointsUnit(($annot['y'] + $annot['h']));
$width = $this->userToPointsUnit($annot['w']);
$height = $this->userToPointsUnit($annot['h']);
$orx = ($annot['x'] * $this->kunit);
$ory = ($page['pheight'] - (($annot['y'] + $annot['h']) * $this->kunit));
$width = ($annot['w'] * $this->kunit);
$height = ($annot['h'] * $this->kunit);
$rect = sprintf('%F %F %F %F', $orx, $ory, $orx+$width, $ory+$height);
$out .= $oid.' 0 R'."\n"
$out .= $oid.' 0 obj'."\n"
.'<<'
.' /Type /Annot'
.' /Subtype /'.$annot['opt']['subtype']
Expand Down Expand Up @@ -856,7 +856,6 @@ protected function getOutAnnotations()
}
}
}
//var_dump($out); exit; //DEBUG
return $out;
}

Expand Down
24 changes: 11 additions & 13 deletions src/Text.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ abstract class Text
{

/**
* Last text bounding box [llx, lly, urx, ury].
* Last text bounding box [x, y, width, height].
*
* @var array
*/
protected $lasttxtbbox = array(
'llx'=>0,
'lly'=>0,
'urx'=>0,
'ury'=>0
'x'=>0,
'y'=>0,
'width'=>0,
'height'=>0
);

/**
Expand Down Expand Up @@ -82,10 +82,10 @@ public function getTextLine(
$width = $width>0?$width:0;
$curfont = $this->font->getCurrentFont();
$this->lasttxtbbox = array(
'llx' => $posx,
'lly' => ($posy + $this->userToPointsUnit($curfont['descent'])),
'urx' => ($posx + $width),
'ury' => ($posy - $this->userToPointsUnit($curfont['ascent']))
'x' => $posx,
'y' => ($posy - $this->pointsToUserUnit($curfont['ascent'])),
'width' => $width,
'height' => ($this->pointsToUserUnit($curfont['ascent'] - $curfont['descent']))
);
$out = $this->getJustifiedString($txt, $width, $forcertl);
$out = $this->getOutTextPosXY($out, $posx, $posy, 'Td');
Expand All @@ -98,8 +98,6 @@ public function getTextLine(
$out = $this->getOutTextStateOperator($out, 'TL', $this->userToPointsUnit($leading));
$out = $this->getOutTextStateOperator($out, 'Ts', $this->userToPointsUnit($rise));
$out = $this->getOutTextObject($out);
//var_dump($out); //DEBUG
//var_dump($this->lasttxtbbox);
return $out;
}

Expand Down Expand Up @@ -142,7 +140,7 @@ protected function getJustifiedString($txt, $width = 0, $forcertl = false)
if ($width > 0) {
return $this->getOutTextStateOperator($txt, 'Tw', $spacewidth * $this->kunit);
}
$this->lasttxtbbox['urx'] += $this->pointsToUserUnit($dim['totwidth']);
$this->lasttxtbbox['width'] = $this->pointsToUserUnit($dim['totwidth']);
return $txt;
}
if ($this->font->isCurrentByteFont()) {
Expand All @@ -154,7 +152,7 @@ protected function getJustifiedString($txt, $width = 0, $forcertl = false)
}
$txt = $this->encrypt->escapeString($txt);
if ($width <= 0) {
$this->lasttxtbbox['urx'] += $this->pointsToUserUnit($dim['totwidth']);
$this->lasttxtbbox['width'] = $this->pointsToUserUnit($dim['totwidth']);
return $this->getOutTextShowing($txt, 'Tj');
}
$fontsize = $this->font->getCurrentFont()['size']?$this->font->getCurrentFont()['size']:1;
Expand Down

0 comments on commit f687e34

Please sign in to comment.