Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add table #36

Merged
merged 6 commits into from
Jan 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions CONTRIBUTE.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ layouts:
- [x] implement block
- [x] implement flow
- [x] implement grid
- [ ] implement table
- [x] implement table

layout blocks:

Expand All @@ -51,6 +51,7 @@ extend layout support:
- [ ] alignment for blocks
- [ ] column/row spans for grids, tables
- [ ] auto, contain, cover for content types
- [ ] top/right/bottom/left different weight borders

extend drawing support:

Expand All @@ -67,8 +68,6 @@ extend PDF support:

This is a large, long-lived project, and as such there are maintenance and architecture topics.

Topics:

Known bugs / issues:
- [ ] Should use new `enum` construct instead of `const`
- [ ] Images of size 0 lead to crash
Expand Down
6 changes: 3 additions & 3 deletions src/Frontend/Layout/AbstractBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
namespace PdfGenerator\Frontend\Layout;

use PdfGenerator\Frontend\Layout\Traits\BlockTrait;
use PdfGenerator\Frontend\LayoutEngine\AbstractBlockVisitor;
use PdfGenerator\Frontend\LayoutEngine\BlockVisitorInterface;

abstract class AbstractBlock
{
Expand All @@ -21,9 +21,9 @@ abstract class AbstractBlock
/**
* @template T
*
* @param AbstractBlockVisitor<T> $visitor
* @param BlockVisitorInterface<T> $visitor
*
* @return T
*/
abstract public function accept(AbstractBlockVisitor $visitor);
abstract public function accept(BlockVisitorInterface $visitor);
}
6 changes: 3 additions & 3 deletions src/Frontend/Layout/Block.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace PdfGenerator\Frontend\Layout;

use PdfGenerator\Frontend\LayoutEngine\AbstractBlockVisitor;
use PdfGenerator\Frontend\LayoutEngine\BlockVisitorInterface;

class Block extends AbstractBlock
{
Expand All @@ -35,11 +35,11 @@ public function cloneWithBlock(AbstractBlock $block): self
/**
* @template T
*
* @param AbstractBlockVisitor<T> $visitor
* @param BlockVisitorInterface<T> $visitor
*
* @return T
*/
public function accept(AbstractBlockVisitor $visitor): mixed
public function accept(BlockVisitorInterface $visitor): mixed
{
return $visitor->visitBlock($this);
}
Expand Down
6 changes: 3 additions & 3 deletions src/Frontend/Layout/ContentBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
namespace PdfGenerator\Frontend\Layout;

use PdfGenerator\Frontend\Content\AbstractContent;
use PdfGenerator\Frontend\LayoutEngine\AbstractBlockVisitor;
use PdfGenerator\Frontend\LayoutEngine\BlockVisitorInterface;

class ContentBlock extends AbstractBlock
{
Expand All @@ -36,11 +36,11 @@ public function cloneWithContent(?AbstractContent $content): self
/**
* @template T
*
* @param AbstractBlockVisitor<T> $visitor
* @param BlockVisitorInterface<T> $visitor
*
* @return T
*/
public function accept(AbstractBlockVisitor $visitor): mixed
public function accept(BlockVisitorInterface $visitor): mixed
{
return $visitor->visitContentBlock($this);
}
Expand Down
6 changes: 3 additions & 3 deletions src/Frontend/Layout/Flow.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

use PdfGenerator\Frontend\Content\AbstractContent;
use PdfGenerator\Frontend\Layout\Style\FlowDirection;
use PdfGenerator\Frontend\LayoutEngine\AbstractBlockVisitor;
use PdfGenerator\Frontend\LayoutEngine\BlockVisitorInterface;

class Flow extends AbstractBlock
{
Expand Down Expand Up @@ -72,11 +72,11 @@ public function getGap(): float
/**
* @template T
*
* @param AbstractBlockVisitor<T> $visitor
* @param BlockVisitorInterface<T> $visitor
*
* @return T
*/
public function accept(AbstractBlockVisitor $visitor): mixed
public function accept(BlockVisitorInterface $visitor): mixed
{
return $visitor->visitFlow($this);
}
Expand Down
38 changes: 6 additions & 32 deletions src/Frontend/Layout/Grid.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@

use PdfGenerator\Frontend\Layout\Parts\Row;
use PdfGenerator\Frontend\Layout\Style\ColumnSize;
use PdfGenerator\Frontend\LayoutEngine\AbstractBlockVisitor;
use PdfGenerator\Frontend\Layout\Traits\ColumnSizesTrait;
use PdfGenerator\Frontend\LayoutEngine\BlockVisitorInterface;

class Grid extends AbstractBlock
{
use ColumnSizesTrait;

/**
* @var Row[]
*/
Expand Down Expand Up @@ -65,44 +68,15 @@ public function getPerpendicularGap(): float
return $this->perpendicularGap;
}

/**
* @return (string|float|ColumnSize)[]
*/
public function getColumnSizes(): array
{
return $this->columnSizes;
}

/**
* @template T
*
* @param AbstractBlockVisitor<T> $visitor
* @param BlockVisitorInterface<T> $visitor
*
* @return T
*/
public function accept(AbstractBlockVisitor $visitor): mixed
public function accept(BlockVisitorInterface $visitor): mixed
{
return $visitor->visitGrid($this);
}

/**
* returns all column sizes for all columns used in the grid.
* if column size undefined for some column, defaults to AUTO.
*
* @return (float|string|ColumnSize)[]
*/
public function getNormalizedColumnSizes(): array
{
$maxColumn = max(...array_keys($this->getColumnSizes()));
foreach ($this->getRows() as $row) {
$maxColumn = max($maxColumn, ...array_keys($row->getColumns()));
}

$columnSizes = array_fill(0, $maxColumn + 1, ColumnSize::AUTO);
foreach ($this->getColumnSizes() as $index => $columnSize) {
$columnSizes[$index] = $columnSize;
}

return $columnSizes;
}
}
17 changes: 14 additions & 3 deletions src/Frontend/Layout/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@

use PdfGenerator\Frontend\Layout\Parts\Row;
use PdfGenerator\Frontend\Layout\Style\ColumnSize;
use PdfGenerator\Frontend\LayoutEngine\AbstractBlockVisitor;
use PdfGenerator\Frontend\Layout\Traits\ColumnSizesTrait;
use PdfGenerator\Frontend\LayoutEngine\BlockVisitorInterface;

class Table extends AbstractBlock
{
use ColumnSizesTrait;

/**
* @var Row[]
*/
Expand Down Expand Up @@ -64,6 +67,14 @@ public function getBody(): array
return $this->body;
}

/**
* @return Row[]
*/
public function getRows(): array
{
return array_merge($this->head, $this->body);
}

/**
* @param Row[] $body
*/
Expand All @@ -86,11 +97,11 @@ public function getColumnSizes(): array
/**
* @template T
*
* @param AbstractBlockVisitor<T> $visitor
* @param BlockVisitorInterface<T> $visitor
*
* @return T
*/
public function accept(AbstractBlockVisitor $visitor): mixed
public function accept(BlockVisitorInterface $visitor): mixed
{
return $visitor->visitTable($this);
}
Expand Down
46 changes: 46 additions & 0 deletions src/Frontend/Layout/Traits/ColumnSizesTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

/*
* This file is part of the famoser/pdf-generator project.
*
* (c) Florian Moser <git@famoser.ch>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace PdfGenerator\Frontend\Layout\Traits;

use PdfGenerator\Frontend\Layout\Style\ColumnSize;

trait ColumnSizesTrait
{
/**
* @return (string|float|ColumnSize)[]
*/
public function getColumnSizes(): array
{
return $this->columnSizes;
}

/**
* returns all column sizes for all columns used in the grid.
* if column size undefined for some column, defaults to AUTO.
*
* @return (float|string|ColumnSize)[]
*/
public function getNormalizedColumnSizes(): array
{
$maxColumn = max([0, ...array_keys($this->getColumnSizes())]);
foreach ($this->getRows() as $row) {
$maxColumn = max($maxColumn, ...array_keys($row->getColumns()));
}

$columnSizes = array_fill(0, $maxColumn + 1, ColumnSize::AUTO);
foreach ($this->getColumnSizes() as $index => $columnSize) {
$columnSizes[$index] = $columnSize;
}

return $columnSizes;
}
}
Loading