public function format(Nodes\Node $node, Document $document)
 {
     $boundary = $node->getBoundary();
     if (!$boundary->isClosed()) {
         list($x, $y) = $boundary->getFirstPoint()->toArray();
         $attributesSnapshot = $node->getAttributesSnapshot();
         $diffWidth = $node->getWidth() - $attributesSnapshot['width'];
         $width = $node->getWidth();
         $x += $width;
         $yEnd = $y - $node->getHeight();
         $boundary->setNext($x, $y)->setNext($x, $yEnd)->setNext($x - $width, $yEnd)->close();
         if ($node->hadAutoMargins()) {
             $node->translate(-$diffWidth / 2, 0);
         }
     }
 }
예제 #2
0
 public function format(Node $node, Document $document)
 {
     $children = $node->getChildren();
     $attributesSnapshot = $node->getAttributesSnapshot();
     $parentBottomYCoord = null;
     $positionCorrection = false;
     foreach ($children as $child) {
         $translateX = $translateY = 0;
         list($x, $y) = $child->getStartDrawingPoint();
         if ($this->hasFloat($child)) {
             $sibling = $this->getPreviousSiblingWithFloat($child, $child->getFloat());
             list($orgX, $orgY) = $child->getStartDrawingPoint();
             if (!$sibling) {
                 $previousSibling = $child->getPreviousSibling();
                 if ($previousSibling) {
                     $y = $previousSibling->getDiagonalPoint()->getY() - $previousSibling->getMarginBottom() - $child->getMarginTop() - $child->getPaddingTop();
                 }
             }
             $this->setNodesWithFloatPosition($child, $x, $y, $sibling);
             $translateX = $x - $orgX;
             $translateY = -($y - $orgY);
         } elseif ($positionCorrection) {
             $siblings = $child->getSiblings();
             $minYCoord = null;
             $previousSiblingWithMinBottomYCoord = null;
             foreach ($siblings as $sib) {
                 if ($sib === $child) {
                     break;
                 }
                 $previousSibling = $sib;
                 $bottomYCoord = $previousSibling->getDiagonalPoint()->getY() - $previousSibling->getMarginBottom();
                 if ($minYCoord === null || $bottomYCoord < $minYCoord) {
                     $minYCoord = $bottomYCoord;
                     $previousSiblingWithMinBottomYCoord = $previousSibling;
                 }
             }
             if ($minYCoord !== null) {
                 $translateY = -($minYCoord - $y - $child->getMarginTop() - $child->getPaddingTop());
                 if ($child->isInline() && $previousSiblingWithMinBottomYCoord->isInline()) {
                     $translateY -= $child instanceof Text ? $child->getLineHeightRecursively() : $child->getHeight();
                 }
             }
         }
         if ($translateX || $translateY) {
             $child->translate($translateX, $translateY);
         }
         $childBottomYCoord = $child->getDiagonalPoint()->getY() - $child->getMarginBottom();
         if ($parentBottomYCoord === null || $childBottomYCoord < $parentBottomYCoord) {
             $parentBottomYCoord = $childBottomYCoord;
         }
         if ($translateY) {
             $positionCorrection = true;
         }
     }
     if ($positionCorrection && $parentBottomYCoord && !$node->getAttribute('static-size')) {
         $parentTranslate = $node->getDiagonalPoint()->getY() - $parentBottomYCoord + $node->getPaddingBottom();
         $newHeight = $node->getHeight() + $parentTranslate;
         $oldHeight = isset($attributesSnapshot['height']) ? $attributesSnapshot['height'] : 0;
         if ($newHeight < $oldHeight) {
             $parentTranslate += $oldHeight - $newHeight;
             $newHeight = $oldHeight;
         }
         $node->setHeight($newHeight);
         $node->getBoundary()->pointTranslate(2, 0, $parentTranslate);
         $node->getBoundary()->pointTranslate(3, 0, $parentTranslate);
     }
 }