Skip to content

Commit

Permalink
Bump PHP version to 8.1
Browse files Browse the repository at this point in the history
  • Loading branch information
antonkomarev committed Dec 10, 2023
1 parent 7171590 commit acaa2b4
Show file tree
Hide file tree
Showing 12 changed files with 33 additions and 185 deletions.
2 changes: 1 addition & 1 deletion .docker/php/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# ----------------------
# The FPM base container
# ----------------------
FROM php:7.4-fpm-alpine AS dev
FROM php:8.1-cli-alpine AS dev

RUN apk add --no-cache --virtual .build-deps \
$PHPIZE_DEPS \
Expand Down
85 changes: 0 additions & 85 deletions .docker/php/www.conf

This file was deleted.

4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@
}
},
"require": {
"php": "^7.4|^8.0",
"php": "^8.1",
"cybercog/php-unicode": "dev-master"
},
"require-dev": {
"ext-simplexml": "*",
"ext-xmlreader": "*",
"phpunit/phpunit": "^9.0|^10.0"
"phpunit/phpunit": "^10.0"
},
"suggest": {
"ext-simplexml": "*",
Expand Down
3 changes: 1 addition & 2 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ services:
build:
context: ./
dockerfile: ./.docker/php/Dockerfile
restart: unless-stopped
tty: true
working_dir: /app
volumes:
- ./:/app
- ./.docker/php/www.conf:/usr/local/etc/php-fpm.d/www.conf:ro
44 changes: 11 additions & 33 deletions src/Font.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,64 +11,42 @@ final class Font
{
private const UNICODE_CODE_POINT_LINE_FEED = 10;

private string $id;

private int $horizontalAdvance;

private FontFace $fontFace;

private MissingGlyph $missingGlyph;

/** @var array<int, Glyph> $glyphs */
private array $glyphs;

/**
* @param array<int, Glyph> $glyphs
*/
public function __construct(
string $id,
int $horizontalAdvance,
FontFace $fontFace,
MissingGlyph $missingGlyph,
array $glyphs = []
public readonly string $id,
private readonly int $horizontalAdvance,
private readonly FontFace $fontFace,
private readonly MissingGlyph $missingGlyph,
private readonly array $glyphs = [],
) {
$this->id = $id;
$this->horizontalAdvance = $horizontalAdvance;
$this->fontFace = $fontFace;
$this->missingGlyph = $missingGlyph;
$this->glyphs = $glyphs;

if ($horizontalAdvance < 0) {
throw new \InvalidArgumentException(
"Font with id `$id` has negative horizontal advance",
);
}
}

public function id(): string
{
return $this->id;
}

public function computeCharacterWidth(
Character $character,
int $size,
float $letterSpacing = 0.0
): float {
$size = $size / $this->fontFace->unitsPerEm();
$size = $size / $this->fontFace->unitsPerEm;

$glyphHorizontalAdvance = $this->getGlyphHorizontalAdvance($character);

$glyphWidth = $glyphHorizontalAdvance * $size;
$letterSpacingWidth = $this->fontFace->unitsPerEm() * $letterSpacing * $size;
$letterSpacingWidth = $this->fontFace->unitsPerEm * $letterSpacing * $size;

return $glyphWidth + $letterSpacingWidth;
}

public function computeStringWidth(
UnicodeString $string,
int $size,
float $letterSpacing = 0.0
float $letterSpacing = 0.0,
): float {
$maxLineWidth = $lineWidth = 0;

Expand All @@ -88,14 +66,14 @@ public function computeStringWidth(
}

private function getGlyphHorizontalAdvance(
Character $character
Character $character,
): int {
if (!isset($this->glyphs[strval($character)])) {
return $this->missingGlyph->horizontalAdvance()
return $this->missingGlyph->horizontalAdvance
?? $this->horizontalAdvance;
}

return $this->glyphs[strval($character)]->horizontalAdvance()
return $this->glyphs[strval($character)]->horizontalAdvance
?? $this->horizontalAdvance;
}
}
10 changes: 1 addition & 9 deletions src/FontFace.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,8 @@ final class FontFace
{
private const DEFAULT_UNITS_PER_EM = 1000;

private int $unitsPerEm;

public function __construct(
int $unitsPerEm = null
public readonly int | null $unitsPerEm = self::DEFAULT_UNITS_PER_EM,
) {
$this->unitsPerEm = $unitsPerEm ?? self::DEFAULT_UNITS_PER_EM;
}

public function unitsPerEm(): int
{
return $this->unitsPerEm;
}
}
13 changes: 5 additions & 8 deletions src/FontList.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,19 @@

final class FontList
{
private array $fontList;

/**
* @param list<Font> $fontList
*/
private function __construct(
array $fontList
private readonly array $fontList,
) {
$this->fontList = $fontList;
}

/**
* @param list<Font> $fontList
*/
public static function of(
array $fontList
array $fontList,
): self {
foreach ($fontList as $font) {
if (!($font instanceof Font)) {
Expand All @@ -41,7 +38,7 @@ public static function of(

public static function ofFile(
string $filePath,
SvgFontFileParserInterface $fontFileParser = null
SvgFontFileParserInterface $fontFileParser = null,
): self {
if ($fontFileParser === null) {
$fontFileParser = new SimpleXmlSvgFontFileParser();
Expand All @@ -59,10 +56,10 @@ public static function ofFile(
}

public function getById(
string $id
string $id,
): Font {
foreach ($this->fontList as $font) {
if ($font->id() === $id) {
if ($font->id === $id) {
return $font;
}
}
Expand Down
31 changes: 3 additions & 28 deletions src/Glyph.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,40 +8,15 @@

final class Glyph
{
private Character $character;

private ?string $name;

private ?int $horizontalAdvance;

public function __construct(
Character $character,
?string $name = null,
?int $horizontalAdvance = null
public readonly Character $character,
public readonly string | null $name = null,
public readonly int | null $horizontalAdvance = null,
) {
$this->name = $name;
$this->horizontalAdvance = $horizontalAdvance;

if ($horizontalAdvance !== null && $horizontalAdvance < 0) {
throw new \InvalidArgumentException(
"Glyph with unicode `$character` has negative horizontal advance",
);
}
$this->character = $character;
}

public function character(): Character
{
return $this->character;
}

public function name(): ?string
{
return $this->name;
}

public function horizontalAdvance(): ?int
{
return $this->horizontalAdvance;
}
}
10 changes: 1 addition & 9 deletions src/MissingGlyph.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,8 @@

final class MissingGlyph
{
private ?int $horizontalAdvance;

public function __construct(
?int $horizontalAdvance = null
public readonly int | null $horizontalAdvance = null,
) {
$this->horizontalAdvance = $horizontalAdvance;
}

public function horizontalAdvance(): ?int
{
return $this->horizontalAdvance;
}
}
8 changes: 4 additions & 4 deletions src/Parser/SimpleXmlSvgFontFileParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function __construct()
* @inheritDoc
*/
public function parseFile(
string $filePath
string $filePath,
): array {
$xml = simplexml_load_file($filePath);
$xml->registerXPathNamespace('svg', 'http://www.w3.org/2000/svg');
Expand All @@ -51,7 +51,7 @@ public function parseFile(
}

private function initFont(
\SimpleXMLElement $fontElement
\SimpleXMLElement $fontElement,
): Font {
$fontId = strval($fontElement[self::ATTRIBUTE_ID]);
$defaultHorizontalAdvance = intval($fontElement[self::ATTRIBUTE_HORIZ_ADV_X]);
Expand Down Expand Up @@ -102,7 +102,7 @@ private function initFont(
}

private function initFontFace(
\SimpleXMLElement $fontFaceElement
\SimpleXMLElement $fontFaceElement,
): FontFace {
$unitsPerEm = intval($fontFaceElement[self::ATTRIBUTE_UNITS_PER_EM]);

Expand Down Expand Up @@ -139,7 +139,7 @@ private function initGlyph(
}

private function initMissingGlyph(
\SimpleXMLElement $missingGlyphElement
\SimpleXMLElement $missingGlyphElement,
): MissingGlyph {
$horizontalAdvance = intval($missingGlyphElement[self::ATTRIBUTE_HORIZ_ADV_X]);

Expand Down
2 changes: 1 addition & 1 deletion src/Parser/SvgFontFileParserInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ interface SvgFontFileParserInterface
* @return list<Font>
*/
public function parseFile(
string $filePath
string $filePath,
): array;
}
Loading

0 comments on commit acaa2b4

Please sign in to comment.