diff --git a/src/Typography/Line.php b/src/Typography/Line.php index 21493e32..555acd91 100644 --- a/src/Typography/Line.php +++ b/src/Typography/Line.php @@ -86,8 +86,8 @@ public function setPosition(Point $point): self } /** - * Count segments of line - * + * Count segments (individual words including punctuation marks) of line + * * @return int */ public function count(): int @@ -95,6 +95,16 @@ public function count(): int return count($this->segments); } + /** + * Count characters of line + * + * @return int + */ + public function length(): int + { + return mb_strlen((string) $this); + } + /** * Cast line to string * diff --git a/src/Typography/TextBlock.php b/src/Typography/TextBlock.php index d95be4f8..d7f648dd 100644 --- a/src/Typography/TextBlock.php +++ b/src/Typography/TextBlock.php @@ -62,10 +62,10 @@ public function longestLine(): Line { $lines = $this->lines(); usort($lines, function (Line $a, Line $b) { - if (mb_strlen((string) $a) === mb_strlen((string) $b)) { + if ($a->length() === $b->length()) { return 0; } - return mb_strlen((string) $a) > mb_strlen((string) $b) ? -1 : 1; + return $a->length() > $b->length() ? -1 : 1; }); return $lines[0]; diff --git a/tests/Unit/Typography/LineTest.php b/tests/Unit/Typography/LineTest.php index 80a91b4d..4f313441 100644 --- a/tests/Unit/Typography/LineTest.php +++ b/tests/Unit/Typography/LineTest.php @@ -45,6 +45,21 @@ public function testCount(): void $this->assertEquals(2, $line->count()); } + public function testLength(): void + { + $line = new Line(); + $this->assertEquals(0, $line->length()); + + $line = new Line("foo"); + $this->assertEquals(3, $line->length()); + + $line = new Line("foo bar."); + $this->assertEquals(8, $line->length()); + + $line = new Line("🫷🙂🫸"); + $this->assertEquals(3, $line->length()); + } + public function testAdd(): void { $line = new Line();