public function format(Node $node, Document $document)
 {
     $parent = $node->getParent();
     if ($node->getWidth() === null && !$node->isInline() && $node->getFloat() === Node::FLOAT_NONE) {
         $parentWidth = $parent->getWidthWithoutPaddings();
         $marginLeft = $node->getMarginLeft();
         $marginRight = $node->getMarginRight();
         $node->setWidth($parentWidth - ($marginLeft + $marginRight));
         $node->setRelativeWidth('100%');
     } elseif ($node->isInline()) {
         $node->setWidth(0);
     }
     if ($node->getHeight() === null) {
         $node->setHeight(0);
     }
     $paddingLeft = $node->getPaddingLeft();
     $paddingRight = $node->getPaddingRight();
     $paddingTop = $node->getPaddingTop();
     $paddingBottom = $node->getPaddingBottom();
     $prefferedWidth = $node->getRealWidth() + $paddingLeft + $paddingRight;
     $parent = $node->getParent();
     $parentWidth = $parent ? $parent->getWidthWithoutPaddings() : null;
     if ($parent && $parentWidth < $prefferedWidth) {
         $prefferedWidth = $parentWidth;
     }
     $node->setWidth($prefferedWidth);
     $node->setHeight($node->getRealHeight() + $paddingTop + $paddingBottom);
 }
 public function format(Nodes\Node $node, Document $document)
 {
     $minX = $maxX = $minY = $maxY = null;
     foreach ($node->getChildren() as $child) {
         $firstPoint = $child->getFirstPoint();
         $diagonalPoint = $child->getDiagonalPoint();
         $childMinX = $firstPoint->getX() - $child->getMarginLeft();
         $childMaxX = $diagonalPoint->getX() + $child->getMarginRight();
         $childMinY = $diagonalPoint->getY() - $child->getMarginBottom();
         $childMaxY = $firstPoint->getY() + $child->getMarginTop();
         $maxX = $this->changeValueIfIsLess($maxX, $childMaxX);
         $maxY = $this->changeValueIfIsLess($maxY, $childMaxY);
         $minX = $this->changeValueIfIsGreater($minX, $childMinX);
         $minY = $this->changeValueIfIsGreater($minY, $childMinY);
     }
     $paddingVertical = $node->getPaddingTop() + $node->getPaddingBottom();
     $paddingHorizontal = $node->getPaddingLeft() + $node->getPaddingRight();
     $realHeight = $paddingVertical + ($maxY - $minY);
     $realWidth = $paddingHorizontal + ($maxX - $minX);
     if ($realHeight > $node->getHeight()) {
         $node->setHeight($realHeight);
     }
     if ($node->isInline() || $realWidth > $node->getWidth()) {
         $node->setWidth($realWidth);
     }
 }
Ejemplo n.º 3
0
 private function setNodesWithFloatPosition(Node $node, &$preferredXCoord, &$preferredYCoord, Node $previousSiblingWithTheSameFloat = null)
 {
     $sibling = $previousSiblingWithTheSameFloat;
     $parent = $node->getParent();
     if ($sibling) {
         $originalPreferredXCoord = $preferredXCoord;
         if ($node->getFloat() === Node::FLOAT_LEFT) {
             $preferredXCoord = $sibling->getDiagonalPoint()->getX() + $sibling->getMarginRight() + $node->getMarginLeft() + $node->getPaddingLeft();
         } else {
             $preferredXCoord = $sibling->getFirstPoint()->getX() - $sibling->getMarginLeft() - $node->getMarginRight() - $node->getWidth() - $node->getPaddingRight();
         }
         list(, $preferredYCoord) = $sibling->getStartDrawingPoint();
         if ($preferredXCoord < $parent->getFirstPoint()->getX() || $preferredXCoord + $node->getWidth() > $parent->getDiagonalPoint()->getX()) {
             $overflowed = true;
         } else {
             $dummyBoundary = $this->createBoundary($node, $preferredXCoord, $preferredYCoord);
             $siblings = $node->getSiblings();
             $overflowed = false;
             foreach ($siblings as $sib) {
                 if ($sib === $node) {
                     break;
                 }
                 if ($dummyBoundary->intersects($sib->getBoundary())) {
                     $overflowed = true;
                     break;
                 }
             }
         }
         if ($overflowed) {
             $preferredYCoord = $sibling->getDiagonalPoint()->getY() - $node->getPaddingTop() - ($sibling->getMarginBottom() + $node->getMarginTop());
             $preferredXCoord = $this->correctXCoordWithParent($node, $sibling);
         }
     } else {
         $previousSibling = $node->getPreviousSibling();
         $preferredXCoord = $this->correctXCoordWithParent($node);
         if ($previousSibling && $previousSibling->getFloat() !== Node::FLOAT_NONE) {
             $yCoord = $preferredYCoord + $previousSibling->getHeight() + $node->getMarginTop() + $previousSibling->getMarginBottom();
             $boundary = $this->createBoundary($node, $preferredXCoord, $yCoord);
             if (!$boundary->intersects($previousSibling->getBoundary())) {
                 $preferredYCoord += $previousSibling->getHeight() + $node->getMarginTop() + $previousSibling->getMarginBottom();
             }
         }
     }
 }