/** * @param Text $element * * @return mixed[] [float $x, float $y, ZendAbstractFont $font] */ private function computeTextPos(Text $element, ZendAbstractFont $font) { $fontStyle = $element->getFontStyle(); $scale = $fontStyle->getSize() / (double) $font->getUnitsPerEm(); $x = $element->getX(); $y = $element->getY(); switch ($fontStyle->getHAlign()) { case FontStyle::HORIZONTAL_ALIGN_MIDDLE: $glyphNumbers = $font->glyphNumbersForCharacters(TextUtils::getCodes($element->getText())); $x -= 0.5 * $scale * array_sum($font->widthsForGlyphs($glyphNumbers)); break; case FontStyle::HORIZONTAL_ALIGN_RIGHT: $glyphNumbers = $font->glyphNumbersForCharacters(TextUtils::getCodes($element->getText())); $x -= $scale * array_sum($font->widthsForGlyphs($glyphNumbers)); break; } switch ($fontStyle->getVAlign()) { case FontStyle::VERTICAL_ALIGN_TOP: $y -= $scale * ($font->getAscent() - $font->getDescent()); break; case FontStyle::VERTICAL_ALIGN_CENTRAL: $y -= 0.5 * $scale * $font->getAscent(); break; case FontStyle::VERTICAL_ALIGN_BOTTOM: $y -= $scale * ($font->getDescent() + $font->getDescent()); break; } return [$x, $y]; }
/** * @param SimpleXMLElement $svg * @param Text $element * @param float $yBase * * @return SimpleXMLElement */ private function addText(SimpleXMLElement $svg, Text $element, $yBase) { $x = $element->getX(); $y = $yBase - $element->getY(); if ($element->getRotation() > 0) { $group = $svg->addChild('g'); $group->addAttribute("transform", "translate({$x}, {$y}) rotate({$element->getRotation()})"); $text = $group->addChild("text", $element->getText()); $text->addAttribute("x", 0); $text->addAttribute("y", 0); } else { $text = $svg->addChild("text", $element->getText()); $text->addAttribute("x", $x); $text->addAttribute("y", $y); } $this->addTextStyle($text, $element); return $text; }