/**
  * @param Path $path
  * @param float $length
  * @param Anchor[] $anchors
  * 
  * @dataProvider anchorPathProvider
  */
 public function testAnchorPath(Path $path, $length, array $anchors)
 {
     $anchorPath = new AnchorPath($path);
     $this->assertSame($length == 0, $anchorPath->isEmpty());
     $this->assertSame($length, $anchorPath->getLen());
     $a = [];
     foreach ($anchors as $pos => $anchor) {
         $a[$pos] = $anchorPath->getAnchor((double) $pos);
     }
     $this->assertEquals($anchors, $a);
 }
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];
 }