Example #1
0
 private function createTextObject(ZendResourceFont $font, $text, $encoding)
 {
     return new StringObject($font->encodeString($text, $encoding));
 }
Example #2
0
 /**
  * @param string $text
  * @param AnchorPath $p
  * @param FontStyle $fontStyle
  *
  * @return mixed[]
  * @throws \Exception
  */
 private function preparePathText($text, AnchorPath $p, ZendAbstractFont $font, FontStyle $fontStyle)
 {
     $fontScale = $fontStyle->getSize() / (double) $font->getUnitsPerEm();
     // split text into encoded characters and widths
     $chars = [];
     $widthSum = 0;
     foreach (TextUtils::getCodes($text) as $code) {
         $char = $font->encodeString(html_entity_decode('&#' . $code . ';', ENT_NOQUOTES, 'UTF-8'), 'UTF-8');
         $width = $font->widthForGlyph($font->glyphNumberForCharacter($code));
         $widthSum += $width;
         $chars[] = [$char, $fontScale * $width];
     }
     // compute start position
     switch ($fontStyle->getHAlign()) {
         case FontStyle::HORIZONTAL_ALIGN_MIDDLE:
             $startPos = ($p->getLen() - $fontScale * $widthSum) / 2.0;
             break;
         case FontStyle::HORIZONTAL_ALIGN_RIGHT:
             $startPos = $p->getLen() - $fontScale * $widthSum;
             break;
         case FontStyle::HORIZONTAL_ALIGN_LEFT:
         default:
             $startPos = 0;
     }
     // compute vertical shift
     switch ($fontStyle->getVAlign()) {
         case FontStyle::VERTICAL_ALIGN_TOP:
             $yShift = $fontScale * ($font->getAscent() - $font->getDescent());
             break;
         case FontStyle::VERTICAL_ALIGN_CENTRAL:
             $yShift = 0.5 * $fontScale * $font->getAscent();
             break;
         case FontStyle::VERTICAL_ALIGN_BOTTOM:
             $yShift = $fontScale * $font->getDescent();
             break;
         case FontStyle::VERTICAL_ALIGN_BASE:
         default:
             $yShift = 0;
     }
     return [$chars, $startPos, $yShift];
 }
Example #3
0
 /**
  * Draw a line of text at the specified position.
  *
  * @param string $text
  * @param float $x
  * @param float $y
  * @param string $charEncoding (optional) Character encoding of source text.
  *   Defaults to current locale.
  * @throws \ZendPdf\Exception\ExceptionInterface
  * @return \ZendPdf\Page
  */
 public function drawText($text, $x, $y, $charEncoding = '')
 {
     if ($this->_font === null) {
         throw new Exception\LogicException('Font has not been set');
     }
     $this->_addProcSet('Text');
     $textObj = new InternalType\StringObject($this->_font->encodeString($text, $charEncoding));
     $xObj = new InternalType\NumericObject($x);
     $yObj = new InternalType\NumericObject($y);
     $this->_contents .= "BT\n" . $xObj->toString() . ' ' . $yObj->toString() . " Td\n" . $textObj->toString() . " Tj\n" . "ET\n";
     return $this;
 }