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

Maint/static analysis #31

Merged
merged 16 commits into from
Jan 1, 2024
Merged
5 changes: 5 additions & 0 deletions .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ jobs:
run: |
export PHP_CS_FIXER_IGNORE_ENV=1 && ./vendor/bin/php-cs-fixer fix --diff --dry-run -v

- name: Static analysis
run: |
./vendor/bin/phpstan analyse src --level 3
./vendor/bin/phpstan analyse tests --level 3

- name: Test
run: |
export XDEBUG_MODE=coverage && php vendor/bin/phpunit
Expand Down
2 changes: 1 addition & 1 deletion src/Backend/File/TokenVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function visitArrayToken(ArrayToken $token): string

public function visitNumberToken(NumberToken $token): string
{
return NumberToken::format($token->getNumber());
return strval(NumberToken::format($token->getNumber()));
}

public function visitDictionaryToken(DictionaryToken $token): string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@

abstract readonly class BaseDocumentStructure
{
abstract public function accept(DocumentVisitor $documentVisitor);
abstract public function accept(DocumentVisitor $documentVisitor): mixed;
}
9 changes: 6 additions & 3 deletions src/Backend/Structure/Document/DocumentResources.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

namespace PdfGenerator\Backend\Structure\Document;

use PdfGenerator\Backend\Catalog\Base\BaseStructure as CatalogBaseStructure;
use PdfGenerator\Backend\Catalog\Font as CatalogFont;
use PdfGenerator\Backend\Catalog\Image as CatalogImage;
use PdfGenerator\Backend\Structure\Document\Base\BaseDocumentStructure;
Expand Down Expand Up @@ -44,9 +43,13 @@ public function getImage(Image $structure): CatalogImage
}

/**
* @param CatalogBaseStructure[] $cache
* @template T
*
* @param array<string, T> $cache
*
* @return T
*/
private function getOrCreate(BaseDocumentStructure $structure, array &$cache): CatalogFont|CatalogImage|CatalogBaseStructure
private function getOrCreate(BaseDocumentStructure $structure, array &$cache): mixed
{
$identifier = spl_object_id($structure);

Expand Down
45 changes: 36 additions & 9 deletions src/Backend/Structure/Document/Font/CMapCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ private function getTextToCharacterIndexMappings(array $characters, array $usedC
$codeSpaceDictionaries = $this->getCodeSpaceRange($textInHexToCharacterIndexMappingByLength);
$codeMappingDictionaries = $this->getCidRange($textInHexToCharacterIndexMappingByLength);

/** @var int[] $existingCodepoint */
$existingCodepoint = [];
foreach ($characters as $character) {
if (null !== $character->getUnicodePoint()) {
Expand All @@ -134,6 +135,11 @@ private function getTextToCharacterIndexMappings(array $characters, array $usedC
implode("\n\n", $notDefRangeDictionaries);
}

/**
* @param int[] $codePointsWithoutCharacterIndex
*
* @return string[]
*/
private function getNotDefRange(array $codePointsWithoutCharacterIndex): array
{
// must always map 0 character
Expand All @@ -146,33 +152,51 @@ private function getNotDefRange(array $codePointsWithoutCharacterIndex): array
return $this->toDictionary($notDefRanges, 'notdefrange');
}

private function getCidRange($textInHexToCharacterIndexMappingByLength): array
/**
* @param array<int, array<string, int>> $textInHexToCharacterIndexMappingByLength
*
* @return string[]
*/
private function getCidRange(array $textInHexToCharacterIndexMappingByLength): array
{
$codeMappings = [];
foreach ($textInHexToCharacterIndexMappingByLength as $length => $textInHexToCharacterIndexMapping) {
$codeMappings = array_merge($codeMappings, $this->getSameLengthCidRanges($textInHexToCharacterIndexMapping));
foreach ($textInHexToCharacterIndexMappingByLength as $textInHexToCharacterIndexMapping) {
$sameLengthCidRanges = $this->getSameLengthCidRanges($textInHexToCharacterIndexMapping);
$codeMappings = array_merge($codeMappings, $sameLengthCidRanges);
}

return $this->toDictionary($codeMappings, 'cidrange');
}

private function getBfRange($characterIndexToUnicodeMappingInHexByLength): array
/**
* @param array<int, array<string, int>> $characterIndexToUnicodeMappingInHexByLength
*
* @return string[]
*/
private function getBfRange(array $characterIndexToUnicodeMappingInHexByLength): array
{
$bfRanges = [];
foreach ($characterIndexToUnicodeMappingInHexByLength as $length => $characterIndexToUnicodeMappingInHex) {
$bfRanges = array_merge($bfRanges, $this->getSameLengthBfRanges($characterIndexToUnicodeMappingInHex));
foreach ($characterIndexToUnicodeMappingInHexByLength as $characterIndexToUnicodeMappingInHex) {
$sameLengthBfRanges = $this->getSameLengthBfRanges($characterIndexToUnicodeMappingInHex);
$bfRanges = array_merge($bfRanges, $sameLengthBfRanges);
}

return $this->toDictionary($bfRanges, 'bfrange');
}

/**
* @param array<int, array<string, int>> $hexKeysByLength
*
* @return string[]
*/
private function getCodeSpaceRange(array $hexKeysByLength): array
{
$codeSpaces = [];
foreach ($hexKeysByLength as $length => $textInHexToCharacterIndexMapping) {
foreach ($hexKeysByLength as $textInHexToCharacterIndexMapping) {
ksort($textInHexToCharacterIndexMapping);

$codeSpaces = array_merge($codeSpaces, $this->getSameLengthCodeSpaceRanges(array_keys($textInHexToCharacterIndexMapping)));
$sameLengthCodeSpaceRanges = $this->getSameLengthCodeSpaceRanges(array_keys($textInHexToCharacterIndexMapping));
$codeSpaces = array_merge($codeSpaces, $sameLengthCodeSpaceRanges);
}

return $this->toDictionary($codeSpaces, 'codespacerange');
Expand Down Expand Up @@ -231,9 +255,12 @@ private function getCharacterIndexToUnicodeMappingInHexByLength(array $character

/**
* @param Character[] $characters
*
* @return array<int, array<string, int>>
*/
private function getTextInHexToCharacterIndexMappingByLength(array $characters): array
{
/** @var array<int, array<string, int>> $hexPointsByLength */
$hexPointsByLength = [];
$characterCount = \count($characters);
for ($i = 0; $i < $characterCount; ++$i) {
Expand Down Expand Up @@ -301,7 +328,7 @@ private function getSameLengthCodeSpaceRanges(array $sameLengthHexPoints): array
}

/**
* @param string[] $hexValueToCharacterIndexMapping
* @param array<string, int> $hexValueToCharacterIndexMapping
*
* @return string[]
*/
Expand Down
3 changes: 2 additions & 1 deletion src/Backend/Structure/Document/Font/DefaultFont.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace PdfGenerator\Backend\Structure\Document\Font;

use PdfGenerator\Backend\Catalog\Font\Type1;
use PdfGenerator\Backend\Structure\Document\Font;
use PdfGenerator\Backend\Structure\DocumentVisitor;

Expand All @@ -25,7 +26,7 @@ public function getBaseFont(): string
return $this->baseFont;
}

public function accept(DocumentVisitor $documentVisitor): \PdfGenerator\Backend\Catalog\Font\Type1
public function accept(DocumentVisitor $documentVisitor): Type1
{
return $documentVisitor->visitDefaultFont($this);
}
Expand Down
6 changes: 3 additions & 3 deletions src/Backend/Structure/Document/Font/EmbeddedFont.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace PdfGenerator\Backend\Structure\Document\Font;

use PdfGenerator\Backend\Catalog\Font\TrueType;
use PdfGenerator\Backend\Catalog\Font\Type0;
use PdfGenerator\Backend\Structure\Document\Font;
use PdfGenerator\Backend\Structure\DocumentVisitor;

Expand All @@ -36,11 +38,9 @@ public function getCharactersUsedInText(): string
}

/**
* @return mixed
*
* @throws \Exception
*/
public function accept(DocumentVisitor $documentVisitor): \PdfGenerator\Backend\Catalog\Font\TrueType|\PdfGenerator\Backend\Catalog\Font\Type0
public function accept(DocumentVisitor $documentVisitor): TrueType|Type0
{
return $documentVisitor->visitEmbeddedFont($this);
}
Expand Down
7 changes: 5 additions & 2 deletions src/Font/Backend/File/Table/CMap/FormatVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

namespace PdfGenerator\Font\Backend\File\Table\CMap;

use PdfGenerator\Font\Backend\File\Traits\Writer;
use PdfGenerator\Font\Backend\StreamWriter;

class FormatVisitor
Expand All @@ -23,7 +22,11 @@ public function visitFormat4(Format\Format4 $format, StreamWriter $writer): void
$writer->writeUInt16($format->getLanguage());

$writer->writeUInt16($format->getSegCountX2());
Writer::writeBinaryTreeSearchableUInt16($format, $writer);

// write searchable binary tree uint16
$writer->writeUInt16($format->getSearchRange());
$writer->writeUInt16($format->getEntrySelector());
$writer->writeUInt16($format->getRangeShift());

$writer->writeUInt16Array($format->getEndCodes());
$writer->writeUInt16($format->getReservedPad());
Expand Down
20 changes: 16 additions & 4 deletions src/Font/Backend/File/TableVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
namespace PdfGenerator\Font\Backend\File;

use PdfGenerator\Font\Backend\File\Table\CMap\FormatVisitor;
use PdfGenerator\Font\Backend\File\Traits\Writer;
use PdfGenerator\Font\Backend\StreamWriter;

class TableVisitor
Expand Down Expand Up @@ -69,7 +68,12 @@ public function visitGlyfTable(Table\GlyfTable $glyfTable): string
$writer = new StreamWriter();

$writer->writeInt16($glyfTable->getNumberOfContours());
Writer::writeBoundingBoxFWORD($glyfTable, $writer);

// write bounding box FWORD
$writer->writeFWORD($glyfTable->getXMin());
$writer->writeFWORD($glyfTable->getYMin());
$writer->writeFWORD($glyfTable->getXMax());
$writer->writeFWORD($glyfTable->getYMax());

foreach ($glyfTable->getComponentGlyphs() as $componentGlyph) {
$writer->writeUInt16($componentGlyph->getFlags());
Expand All @@ -96,7 +100,11 @@ public function visitHeadTable(Table\HeadTable $headTable): string
$writer->writeLONGDATETIME($headTable->getCreated());
$writer->writeLONGDATETIME($headTable->getModified());

Writer::writeBoundingBoxInt16($headTable, $writer);
// write bounding box int16
$writer->writeInt16($headTable->getXMin());
$writer->writeInt16($headTable->getYMin());
$writer->writeInt16($headTable->getXMax());
$writer->writeInt16($headTable->getYMax());

$writer->writeUInt16($headTable->getMacStyle());
$writer->writeUInt16($headTable->getLowestRecPPEM());
Expand Down Expand Up @@ -191,7 +199,11 @@ public function visitOffsetTable(Table\OffsetTable $offsetTable): string

$writer->writeUInt32($offsetTable->getScalerType());
$writer->writeUInt16($offsetTable->getNumTables());
Writer::writeBinaryTreeSearchableUInt16($offsetTable, $writer);

// write searchable binary tree uint16
$writer->writeUInt16($offsetTable->getSearchRange());
$writer->writeUInt16($offsetTable->getEntrySelector());
$writer->writeUInt16($offsetTable->getRangeShift());

return $writer->getStream();
}
Expand Down
49 changes: 0 additions & 49 deletions src/Font/Backend/File/Traits/Writer.php

This file was deleted.

24 changes: 8 additions & 16 deletions src/Font/Backend/FileWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
use PdfGenerator\Font\Backend\File\Table\TableDirectoryEntry;
use PdfGenerator\Font\Backend\File\TableDirectory;
use PdfGenerator\Font\Backend\File\TableVisitor;
use PdfGenerator\Font\Backend\File\Traits\BinaryTreeSearchableTrait;
use PdfGenerator\Font\Frontend\StreamReader;
use PdfGenerator\Font\IR\Structure\Character;
use PdfGenerator\Font\IR\Structure\Font;
Expand Down Expand Up @@ -251,8 +250,8 @@ private function generateCMapFormat4(array $characters, int $reservedCharactersO
$format->setLength(8 * 2 + 4 * 2 * $segmentsCount); // 8 fields; 4 arrays of size 2 per entry
$format->setLanguage(0);
$format->setSegCountX2($segmentsCount * 2);
$format->setSearchRange(2 * (2 ** ((int) log($segmentsCount, 2))));
$format->setEntrySelector((int) log($format->getSearchRange() / 2, 2));
$format->setSearchRange(2 * (2 ** ((int) \log($segmentsCount, 2))));
$format->setEntrySelector((int) \log($format->getSearchRange() / 2, 2));
$format->setRangeShift(2 * $segmentsCount - $format->getSearchRange());
$format->setReservedPad(0);

Expand Down Expand Up @@ -497,7 +496,12 @@ private function generateOffsetTable(int $numTables): OffsetTable

$offsetTable->setScalerType(0x00010000);
$offsetTable->setNumTables($numTables);
self::setBinaryTreeSearchableProperties($offsetTable, $numTables);

// binary search properties
$powerOfTwo = (int) \log($numTables, 2);
$offsetTable->setSearchRange(2 ** $powerOfTwo * 16);
$offsetTable->setEntrySelector($powerOfTwo);
$offsetTable->setRangeShift($numTables * 16 - $offsetTable->getSearchRange());

return $offsetTable;
}
Expand Down Expand Up @@ -627,18 +631,6 @@ private static function generatePascalString(array $names): string
return $writer->getStream();
}

/**
* @param BinaryTreeSearchableTrait $binaryTreeSearchable
*/
private static function setBinaryTreeSearchableProperties(mixed $binaryTreeSearchable, int $numberOfEntries): void
{
$powerOfTwo = (int) log($numberOfEntries, 2);

$binaryTreeSearchable->setSearchRange(2 ** $powerOfTwo * 16);
$binaryTreeSearchable->setEntrySelector($powerOfTwo);
$binaryTreeSearchable->setRangeShift($numberOfEntries * 16 - $binaryTreeSearchable->getSearchRange());
}

/**
* @return RawTable[]
*/
Expand Down
9 changes: 6 additions & 3 deletions src/Font/Frontend/File/Table/CMap/FormatReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
use PdfGenerator\Font\Frontend\File\Table\CMap\Format\Format12Group;
use PdfGenerator\Font\Frontend\File\Table\CMap\Format\Format4;
use PdfGenerator\Font\Frontend\File\Table\CMap\Format\Format6;
use PdfGenerator\Font\Frontend\File\Traits\Reader;
use PdfGenerator\Font\Frontend\StreamReader;

class FormatReader
Expand Down Expand Up @@ -63,7 +62,11 @@ private function readFormat4(StreamReader $fileReader, int $startOffset): Format
$this->readUInt16SharedFormat($fileReader, $format);

$format->setSegCountX2($fileReader->readUInt16());
Reader::readBinaryTreeSearchableUInt16($fileReader, $format);

// read binary tree uint16
$format->setSearchRange($fileReader->readUInt16());
$format->setEntrySelector($fileReader->readUInt16());
$format->setRangeShift($fileReader->readUInt16());

$segCount = $format->getSegCountX2() / 2;
$format->setEndCodes($fileReader->readUInt16Array($segCount));
Expand Down Expand Up @@ -100,7 +103,7 @@ private function readFormat12(StreamReader $fileReader): Format12
$format->setLanguage($fileReader->readUInt32());
$format->setNGroups($fileReader->readUInt32());

for ($i = 0; $i < $format->getGroups(); ++$i) {
for ($i = 0; $i < count($format->getGroups()); ++$i) {
$group = new Format12Group();

$group->setStartCharCode($fileReader->readUInt32());
Expand Down
Loading