private function designateLinesOfWords(Node $node) { $currentPoint = $node->getFirstPoint(); $partsOfLine = array(); $yTranslation = 0; $line = new Line($node, 0, $yTranslation); foreach ($node->getChildren() as $textNode) { $words = $textNode->getWords(); $wordsSizes = $textNode->getWordsSizes(); $currentWordLine = array(); $currentWidthOfLine = 0; $numberOfWords = count($words); $first = true; $lineWidth = 0; foreach ($words as $index => $word) { $wordSize = $wordsSizes[$index]; $newLineWidth = $currentWidthOfLine + $wordSize; $endXCoord = $newLineWidth + $currentPoint->getX(); $maxLineXCoord = $this->getMaxXCoord($node); $isEndOfLine = $endXCoord > $maxLineXCoord; if ($isEndOfLine) { if ($currentWordLine) { $partOfLine = new LinePart($currentWordLine, $currentWidthOfLine, $currentPoint->getX() - $node->getFirstPoint()->getX(), $textNode); $partsOfLine[] = $partOfLine; $line->addParts($partsOfLine); $node->addLine($line); $yTranslation += $line->getHeight(); $line = new Line($node, 0, $yTranslation); $partsOfLine = array(); $currentWidthOfLine = 0; $currentWordLine = array(); } else { $line->addParts($partsOfLine); $node->addLine($line); $yTranslation += $line->getHeight(); $line = new Line($node, 0, $yTranslation); $partsOfLine = array(); } $currentPoint = Point::getInstance($node->getFirstPoint()->getX(), 0); } $currentWidthOfLine = $currentWidthOfLine + $wordSize; $currentWordLine[] = $word; } if ($currentWordLine) { $partOfLine = new LinePart($currentWordLine, $currentWidthOfLine, $currentPoint->getX() - $node->getFirstPoint()->getX(), $textNode); $partsOfLine[] = $partOfLine; $currentPoint = $currentPoint->translate($currentWidthOfLine, 0); } } if ($partsOfLine) { $yTranslation += $line->getHeight(); $line = new Line($node, 0, $yTranslation); $line->addParts($partsOfLine); $node->addLine($line); } }
/** * @test */ public function justifyLine() { $paragraphWidth = 100; $paragraph = $this->getMockBuilder('PHPPdf\\Core\\Node\\Paragraph')->setMethods(array('getWidth', 'getRecurseAttribute', 'getParentPaddingLeft', 'getParentPaddingRight'))->getMock(); $paragraph->expects($this->atLeastOnce())->method('getWidth')->will($this->returnValue($paragraphWidth)); $paragraph->expects($this->atLeastOnce())->method('getParentPaddingLeft')->will($this->returnValue(0)); $paragraph->expects($this->atLeastOnce())->method('getParentPaddingRight')->will($this->returnValue(0)); $paragraph->expects($this->atLeastOnce())->method('getWidth')->will($this->returnValue($paragraphWidth)); $paragraph->expects($this->atLeastOnce())->method('getRecurseAttribute')->with('text-align')->will($this->returnValue(Node::ALIGN_JUSTIFY)); $line = new Line($paragraph, 0, 0); $linePartSizes = array(30, 50); $numberOfWordsPerPart = 5; $numberOfSpaces = count($linePartSizes) * $numberOfWordsPerPart - 1; $totalWidth = array_sum($linePartSizes); $expectedWordSpacing = ($paragraphWidth - $totalWidth) / $numberOfSpaces; foreach ($linePartSizes as $width) { $linePart = $this->getMockBuilder('PHPPdf\\Core\\Node\\Paragraph\\LinePart')->setMethods(array('setWordSpacing', 'getWidth', 'getNumberOfWords'))->disableOriginalConstructor()->getMock(); $linePart->expects($this->atLeastOnce())->method('getWidth')->will($this->returnValue($width)); $linePart->expects($this->once())->method('setWordSpacing')->with($expectedWordSpacing); $linePart->expects($this->atLeastOnce())->method('getNumberOfWords')->will($this->returnValue($numberOfWordsPerPart)); $line->addPart($linePart); } $line->format(); }
public function __construct(Point $firstPoint, $height) { $paragraph = new Paragraph(); $paragraph->getBoundary()->setNext($firstPoint); parent::__construct($paragraph, 0, 0); $this->height = $height; }
/** * _____________________________ * | | * | ______________| * |_____________| | <- breaking here * | | * |____________________________| * * @test */ public function breaking() { $x = 0; $y = 500; $width = 500; $height = 500; $firstPoint = Point::getInstance($x, $y); $paragraphParent = new Container(); $paragraphParent->setWidth($width); $paragraphParent->setHeight($height); $paragraph = new Paragraph(); $paragraph->setParent($paragraphParent); $text1 = new Text(); $text1->getBoundary()->setNext($firstPoint)->setNext($firstPoint->translate($width, 0))->setNext($firstPoint->translate($width, 200))->setNext($firstPoint->translate($width / 2, 200))->setNext($firstPoint->translate($width / 2, 250))->setNext($firstPoint->translate(0, 250))->close(); $text2 = new Text(); $text2->getBoundary()->setNext($firstPoint->translate($width / 2, 200))->setNext($firstPoint->translate($width, 200))->setNext($firstPoint->translate($width, 500))->setNext($firstPoint->translate(0, 500))->setNext($firstPoint->translate(0, 250))->setNext($firstPoint->translate($width / 2, 250))->close(); $text1->setWidth(500); $text1->setHeight(250); $text2->setWidth(500); $text2->setHeight(300); $text1->setAttribute('line-height', 100); $text2->setAttribute('line-height', 100); for ($i = 0; $i < 2; $i++) { $line = new Line($paragraph, 0, $i * 100); $part = new LinePart('', 500, 0, $text1); $line->addPart($part); $paragraph->addLine($line); } $line = new Line($paragraph, 0, 200); $line->addPart(new LinePart('', $width / 2, 0, $text1)); $line->addPart(new LinePart('', $width / 2, 250, $text2)); $paragraph->addLine($line); for ($i = 0; $i < 2; $i++) { $line = new Line($paragraph, 0, ($i + 3) * 100); $part = new LinePart('', 500, 0, $text2); $line->addPart($part); $paragraph->addLine($line); } $paragraph->add($text1); $paragraph->add($text2); $paragraph->getBoundary()->setNext($firstPoint)->setNext($firstPoint->translate($width, 0))->setNext($firstPoint->translate($width, 500))->setNext($firstPoint->translate(0, 500))->close(); $paragraph->setHeight(500); $paragraphProduct = $paragraph->breakAt(225); $this->assertEquals(200, $paragraph->getHeight()); $this->assertEquals(200, $paragraph->getFirstPoint()->getY() - $paragraph->getDiagonalPoint()->getY()); $this->assertEquals(300, $paragraphProduct->getHeight()); $this->assertEquals(300, $paragraphProduct->getFirstPoint()->getY() - $paragraphProduct->getDiagonalPoint()->getY()); $this->assertTrue($paragraphProduct !== null); $this->assertEquals(200, $paragraph->getHeight()); $this->assertEquals(2, count($paragraph->getLines())); $this->assertEquals(3, count($paragraphProduct->getLines())); $this->assertEquals(1, count($paragraph->getChildren())); $this->assertEquals(2, count($paragraphProduct->getChildren())); foreach ($paragraphProduct->getLines() as $i => $line) { $this->assertEquals($i * 100, $line->getYTranslation()); foreach ($line->getParts() as $part) { // $this->assertTrue($part->getText() !== $text1); // $this->assertTrue($part->getText() !== $text2); } } }