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

0.1.40 #34

Merged
merged 8 commits into from
Aug 16, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
root = true

[*]
charset = utf-8
indent_style = tab
indent_size = 4
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
max_line_length = 120


[**/*.json]
charset = utf-8
indent_style = space
indent_size = 2
end_of_line = lf
trim_trailing_whitespace = true
14 changes: 9 additions & 5 deletions .php_cs.dist
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
$config = \PhpCsFixer\Config::create()
->setRiskyAllowed(true)
->setIndent("\t")
->setUsingCache(false)
->setCacheFile(__DIR__ . '/.php-cs-fixer.cache')
->setRules([
'@PHP56Migration' => true,
'@PHPUnit60Migration:risky' => true,
'@PHP74Migration' => true,
'@PHP74Migration:risky' => true,
'@PHP80Migration' => true,
'@PHP80Migration:risky' => true,
'@PhpCsFixer' => true,
'@PhpCsFixer:risky' => true,
'list_syntax' => ['syntax' => 'short'],
'@PSR2' => true,
'list_syntax' => ['syntax' => 'short'],
'align_multiline_comment' => true,
'array_syntax' => ['syntax' => 'short'],
'blank_line_after_namespace' => true,
Expand All @@ -27,6 +29,7 @@ $config = \PhpCsFixer\Config::create()
],
'explicit_string_variable' => false,
'declare_equal_normalize' => true,
'declare_strict_types' => false,
'dir_constant' => false,
'doctrine_annotation_braces' => true,
'doctrine_annotation_indentation' => true,
Expand Down Expand Up @@ -145,13 +148,14 @@ $config = \PhpCsFixer\Config::create()
'switch_case_space' => true,
'ternary_operator_spaces' => true,
'ternary_to_null_coalescing' => true,
'trailing_comma_in_multiline_array' => false,
'trailing_comma_in_multiline' => ['after_heredoc' => false, 'elements' => []],
'trim_array_spaces' => true,
'unary_operator_spaces' => true,
'visibility_required' => [
'property',
'method',
],
'void_return' => false,
'whitespace_after_comma_in_array' => true,
])
->setFinder(PhpCsFixer\Finder::create()
Expand Down
12 changes: 6 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,19 @@
}
],
"require": {
"php": ">=7.3",
"php": ">=7.4",
"ext-mbstring": "*",
"ext-dom": "*",
"ext-libxml": "*",
"ext-bcmath": "*",
"ext-zlib": "*",
"ext-gd": "*",
"ext-filter": "*",
"phenx/php-font-lib": "^0.5.1",
"guzzlehttp/guzzle": "^7.0",
"composer/ca-bundle": "^1.1",
"milon/barcode": "^8.0.0",
"sabberworm/php-css-parser": "^8.3"
"phenx/php-font-lib": "^0.5",
"guzzlehttp/guzzle": "^7",
"composer/ca-bundle": "^1",
"milon/barcode": "^9",
"sabberworm/php-css-parser": "^8"
},
"autoload": {
"psr-4": {
Expand Down
5 changes: 1 addition & 4 deletions lib/Catalog.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,7 @@ public function render(): string
{
return implode("\n", [
$this->getRawId() . ' obj',
'<<',
' /Type /Catalog',
' /Pages ' . $this->document->getPagesObject()->getReference(),
'>>',
'<</Type /Catalog/Pages ' . $this->document->getPagesObject()->getReference() . '>>',
'endobj',
]);
}
Expand Down
12 changes: 6 additions & 6 deletions lib/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -688,7 +688,7 @@ public function fixPageNumbers()
*/
protected function getDocumentHeader(): string
{
return "%PDF-1.7\n%âăĎÓ\n";
return "%PDF-1.4\n%âăĎÓ\n";
}

/**
Expand Down Expand Up @@ -893,11 +893,11 @@ public function render(): string
$trailer = (new \YetiForcePDF\Objects\Trailer())
->setDocument($this)->setRootObject($this->catalog)->setSize($objectSize);
$this->buffer .= $trailer->render() . "\n";
$this->buffer .= implode("\n", [
'startxref',
$offset,
'',
]);
// $this->buffer .= implode("\n", [
// 'startxref',
// $offset,
// '',
// ]);
$this->buffer .= $this->getDocumentFooter();
$this->removeObject($trailer);
return $this->buffer;
Expand Down
59 changes: 30 additions & 29 deletions lib/Layout/TableBox.php
Original file line number Diff line number Diff line change
Expand Up @@ -294,36 +294,37 @@ public function setUpWidths(string $availableSpace)
{
foreach ($this->getChildren() as $rowGroup) {
foreach ($rowGroup->getChildren() as $row) {
$columns = $row->getChildren();
foreach ($columns as $columnIndex => $column) {
$cell = $column->getFirstChild();
$cellStyle = $cell->getStyle();
$columnInnerWidth = $cell->getDimensions()->getMaxWidth();
$styleWidth = $column->getStyle()->getRules('width');
$this->contentWidths[$columnIndex] = Math::max($this->contentWidths[$columnIndex] ?? '0', $columnInnerWidth);
$minColumnWidth = $cell->getDimensions()->getMinWidth();
if ($column->getColSpan() > 1) {
$minColumnWidth = Math::div($minColumnWidth, (string) $column->getColSpan());
}
$this->minWidths[$columnIndex] = Math::max($this->minWidths[$columnIndex] ?? '0', $minColumnWidth);
if ('auto' !== $styleWidth && false === strpos($styleWidth, '%')) {
if ($columns = $row->getChildren()) {
foreach ($columns as $columnIndex => $column) {
$cell = $column->getFirstChild();
$cellStyle = $cell->getStyle();
$columnInnerWidth = $cell->getDimensions()->getMaxWidth();
$styleWidth = $column->getStyle()->getRules('width');
$this->contentWidths[$columnIndex] = Math::max($this->contentWidths[$columnIndex] ?? '0', $columnInnerWidth);
$minColumnWidth = $cell->getDimensions()->getMinWidth();
if ($column->getColSpan() > 1) {
$styleWidth = Math::div($styleWidth, (string) $column->getColSpan());
$minColumnWidth = Math::div($minColumnWidth, (string) $column->getColSpan());
}
$preferred = Math::max($styleWidth, $minColumnWidth);
$this->minWidths[$columnIndex] = $preferred;
} elseif (strpos($styleWidth, '%') > 0) {
$preferred = Math::max($this->preferredWidths[$columnIndex] ?? '0', $columnInnerWidth);
$this->percentages[$columnIndex] = Math::max($this->percentages[$columnIndex] ?? '0', trim($styleWidth, '%'));
} else {
$preferred = Math::max($this->preferredWidths[$columnIndex] ?? '0', $columnInnerWidth);
$this->minWidths[$columnIndex] = Math::max($this->minWidths[$columnIndex] ?? '0', $minColumnWidth);
if ('auto' !== $styleWidth && false === strpos($styleWidth, '%')) {
if ($column->getColSpan() > 1) {
$styleWidth = Math::div($styleWidth, (string) $column->getColSpan());
}
$preferred = Math::max($styleWidth, $minColumnWidth);
$this->minWidths[$columnIndex] = $preferred;
} elseif (strpos($styleWidth, '%') > 0) {
$preferred = Math::max($this->preferredWidths[$columnIndex] ?? '0', $columnInnerWidth);
$this->percentages[$columnIndex] = Math::max($this->percentages[$columnIndex] ?? '0', trim($styleWidth, '%'));
} else {
$preferred = Math::max($this->preferredWidths[$columnIndex] ?? '0', $columnInnerWidth);
}
$this->preferredWidths[$columnIndex] = $preferred;
}
$this->preferredWidths[$columnIndex] = $preferred;
$this->borderWidth = Math::add($this->borderWidth, $cellStyle->getHorizontalBordersWidth());
$this->minWidth = Math::add($this->minWidth, $this->minWidths[$columnIndex]);
$this->contentWidth = Math::add($this->contentWidth, $this->contentWidths[$columnIndex]);
$this->preferredWidth = Math::add($this->preferredWidth, $this->preferredWidths[$columnIndex]);
}
$this->borderWidth = Math::add($this->borderWidth, $cellStyle->getHorizontalBordersWidth());
$this->minWidth = Math::add($this->minWidth, $this->minWidths[$columnIndex]);
$this->contentWidth = Math::add($this->contentWidth, $this->contentWidths[$columnIndex]);
$this->preferredWidth = Math::add($this->preferredWidth, $this->preferredWidths[$columnIndex]);
}
}
if ('collapse' !== $this->getParent()->getStyle()->getRules('border-collapse')) {
Expand Down Expand Up @@ -358,9 +359,9 @@ protected function setUpSizingTypes()
foreach ($this->getChildren() as $rowGroup) {
foreach ($rowGroup->getChildren() as $row) {
foreach ($row->getChildren() as $columnIndex => $column) {
if ('percent' === $columnSizingTypes[$columnIndex]) {
if (isset($columnSizingTypes[$columnIndex]) && 'percent' === $columnSizingTypes[$columnIndex]) {
$this->percentColumns[$columnIndex][] = $column;
} elseif ('pixel' === $columnSizingTypes[$columnIndex]) {
} elseif (isset($columnSizingTypes[$columnIndex]) && 'pixel' === $columnSizingTypes[$columnIndex]) {
$this->pixelColumns[$columnIndex][] = $column;
} else {
$this->autoColumns[$columnIndex][] = $column;
Expand Down Expand Up @@ -1287,7 +1288,7 @@ public function measureHeight(bool $afterPageDividing = false)
}
// column that is spanned with more than 1 row must have height that is equal to all spanned rows height
foreach ($rows as $rowIndex => $row) {
$currentRowMax = $maxRowHeights[$rowGroupIndex][$rowIndex];
$currentRowMax = $maxRowHeights[$rowGroupIndex][$rowIndex] ?? '0';
foreach ($row->getChildren() as $column) {
$rowSpan = $column->getRowSpan();
if ($rowSpan > 1) {
Expand Down
2 changes: 0 additions & 2 deletions lib/Math.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ public static function add(string ...$numbers)
foreach ($numbers as $number) {
$result = bcadd($result, $number, static::$scale);
}

return $result;
}

Expand Down Expand Up @@ -172,7 +171,6 @@ public static function max(string ...$numbers)
foreach ($numbers as $number) {
$result = 1 === bccomp($number, $result, static::$scale) ? $number : $result;
}

return $result;
}

Expand Down
3 changes: 2 additions & 1 deletion lib/Objects/Basic/DictionaryObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@ public function render(): string
foreach ($this->values as $value) {
$values[] = ' ' . implode(' ', $value);
}
$values[] = ">>\nendobj";
$values[] = '>>';
$values[] = 'endobj';
return implode("\n", $values);
}
}
9 changes: 2 additions & 7 deletions lib/Objects/Basic/StreamObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,10 @@ public function render(): string
if ('FlateDecode' === $this->filter) {
$stream = gzcompress($stream);
}
$filter = $this->filter ? ' /Filter /' . $this->filter : '';
$filter = $this->filter ? '/Filter /' . $this->filter : '';
return implode("\n", [
$this->getRawId() . ' obj',
'<<',
' /Length ' . mb_strlen($stream, '8bit'),
' /Lenght1 ' . $sizeBefore,
$filter,
'>>',
'stream',
'<</Length ' . mb_strlen($stream, '8bit') . '/Lenght1 ' . $sizeBefore . $filter . '>>stream',
$stream,
'endstream',
'endobj',
Expand Down
4 changes: 1 addition & 3 deletions lib/Objects/Font.php
Original file line number Diff line number Diff line change
Expand Up @@ -777,9 +777,7 @@ protected function loadFontData()
public function render(): string
{
return implode("\n", [$this->getRawId() . ' obj',
'<<',
' /Type /Font',
' /Subtype /CIDFontType2',
'<</Type /Font/Subtype /CIDFontType2',
' /BaseFont /' . $this->getFullName(),
' /FontDescriptor ' . $this->fontDescriptor->getReference(),
' /DW 500',
Expand Down
3 changes: 1 addition & 2 deletions lib/Objects/FontEncoding.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ public function render(): string
{
return implode("\n", [
$this->getRawId() . ' obj',
'<<',
' /Type Encoding',
'<</Type Encoding',
' /BaseEncoding /' . $this->encoding,
'>>',
'endobj',
Expand Down
5 changes: 1 addition & 4 deletions lib/Objects/TextStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,7 @@ public function render(): string
$stream = $this->getRawStream();
return implode("\n", [
$this->getRawId() . ' obj',
'<<',
' /Length ' . \strlen($stream),
'>>',
'stream',
'<</Length ' . \strlen($stream) . '>>stream',
$stream,
'endstream',
'endobj',
Expand Down
2 changes: 1 addition & 1 deletion lib/Objects/Trailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ public function render(): string
'trailer',
'<<',
' /Root ' . $this->root->getReference(),
' /Size ' . $this->size,
' /Info ' . $this->document->getMeta()->getReference(),
' /Size ' . $this->size,
'>>',
]);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ class Page extends \YetiForcePDF\Objects\Basic\DictionaryObject
/**
* Initialisation.
*
* @return $this|\YetiForcePDF\Objects\PdfObject
* @return $this
*/
public function init()
{
Expand Down
23 changes: 13 additions & 10 deletions lib/Style/Color.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,7 @@
*/
class Color
{
/**
* Color names hash values.
*
* @var array
*/
/** @var string[] Color names hash values. */
protected static $colorNames = [
'aliceblue' => '#f0f8ff',
'antiquewhite' => '#faebd7',
Expand Down Expand Up @@ -176,14 +172,19 @@ class Color
'yellowgreen' => '#9acd32',
];

/** @var array Color names rgba values. */
protected static $colorCustomNames = [
'transparent' => [0, 0, 0, 0.1]
];

/**
* Get rgba array from color name.
*
* @param string $colorName
*
* @return string[]
*/
public static function fromName(string $colorName)
public static function fromName(string $colorName): array
{
$colorName = strtolower($colorName);
if (isset(static::$colorNames[$colorName])) {
Expand All @@ -199,7 +200,7 @@ public static function fromName(string $colorName)
*
* @return string[]
*/
public static function fromHash(string $hashColor)
public static function fromHash(string $hashColor): array
{
$color = substr($hashColor, 1);
if (3 === \strlen($color)) {
Expand Down Expand Up @@ -233,7 +234,7 @@ public static function fromHash(string $hashColor)
*
* @return string[] rgb/a
*/
public static function fromRGBA(string $rgbColor)
public static function fromRGBA(string $rgbColor): array
{
$matches = [];
preg_match_all('/rgb\(([0-9]+)\s?\,\s?([0-9]+)\s?\,\s?([0-9]+)\s?([0-9]+)?\s?\)/ui', str_replace("\n\t\r ", '', $rgbColor), $matches);
Expand All @@ -253,7 +254,7 @@ public static function fromRGBA(string $rgbColor)
*
* @return int[]
*/
public static function toRGBA($colorInput, bool $inPDFColorSpace = false)
public static function toRGBA($colorInput, bool $inPDFColorSpace = false): array
{
$colorInput = trim(strtolower($colorInput));
if ($colorInput) {
Expand All @@ -263,6 +264,8 @@ public static function toRGBA($colorInput, bool $inPDFColorSpace = false)
$color = static::fromRGBA($colorInput);
} elseif (\array_key_exists($colorInput, static::$colorNames)) {
$color = static::fromName($colorInput);
} elseif (isset(static::$colorCustomNames[$colorInput])) {
return static::$colorCustomNames[$colorInput];
}
$r = $inPDFColorSpace ? Math::div($color[0], '255') : $color[0];
$g = $inPDFColorSpace ? Math::div($color[1], '255') : $color[1];
Expand All @@ -280,7 +283,7 @@ public static function toRGBA($colorInput, bool $inPDFColorSpace = false)
*
* @return string
*/
public static function toPdfString(string $colorInput)
public static function toPdfString(string $colorInput): string
{
$color = static::toRGBA($colorInput);
return "{$color[0]} {$color[1]} {$color[2]} RG";
Expand Down