Skip to content

Commit d0f097b

Browse files
authored
Enable wrapping for non western text (#1444)
1 parent 9659e76 commit d0f097b

File tree

2 files changed

+43
-6
lines changed

2 files changed

+43
-6
lines changed

src/Typography/Line.php

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function __construct(
3636
protected PointInterface $position = new Point()
3737
) {
3838
if (is_string($text)) {
39-
$this->segments = explode(" ", $text);
39+
$this->segments = $this->wordsSeperatedBySpaces($text) ? explode(" ", $text) : mb_str_split($text);
4040
}
4141
}
4242

@@ -88,7 +88,7 @@ public function setPosition(PointInterface $point): self
8888

8989
/**
9090
* Count segments (individual words including punctuation marks) of line
91-
*
91+
*
9292
* @return int
9393
*/
9494
public function count(): int
@@ -106,13 +106,36 @@ public function length(): int
106106
return mb_strlen((string) $this);
107107
}
108108

109+
/**
110+
* Dermine if words are sperarated by spaces in the written language of the given text
111+
*/
112+
private function wordsSeperatedBySpaces(string $text): bool
113+
{
114+
return 1 !== preg_match(
115+
'/[' .
116+
'\x{4E00}-\x{9FFF}' . // CJK Unified Ideographs (chinese)
117+
'\x{3400}-\x{4DBF}' . // CJK Unified Ideographs Extension A (chinese)
118+
'\x{3040}-\x{309F}' . // hiragana (japanese)
119+
'\x{30A0}-\x{30FF}' . // katakana (japanese)
120+
'\x{0E00}-\x{0E7F}' . // thai
121+
']/u',
122+
$text
123+
);
124+
}
125+
109126
/**
110127
* Cast line to string
111128
*
112129
* @return string
113130
*/
114131
public function __toString(): string
115132
{
116-
return implode(" ", $this->segments);
133+
$string = implode("", $this->segments);
134+
135+
if ($this->wordsSeperatedBySpaces($string)) {
136+
return implode(" ", $this->segments);
137+
}
138+
139+
return $string;
117140
}
118141
}

tests/Unit/Typography/LineTest.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44

55
namespace Intervention\Image\Tests\Unit\Typography;
66

7+
use Generator;
78
use Intervention\Image\Geometry\Point;
89
use Intervention\Image\Tests\BaseTestCase;
910
use Intervention\Image\Typography\Line;
1011
use PHPUnit\Framework\Attributes\CoversClass;
12+
use PHPUnit\Framework\Attributes\DataProvider;
1113

1214
#[CoversClass(Line::class)]
1315
final class LineTest extends BaseTestCase
@@ -18,10 +20,12 @@ public function testConstructor(): void
1820
$this->assertInstanceOf(Line::class, $line);
1921
}
2022

21-
public function testToString(): void
23+
#[DataProvider('toStringDataProvider')]
24+
public function testToString(string $text, int $words): void
2225
{
23-
$line = new Line('foo bar');
24-
$this->assertEquals('foo bar', (string) $line);
26+
$line = new Line($text);
27+
$this->assertEquals($words, $line->count());
28+
$this->assertEquals($text, (string) $line);
2529
}
2630

2731
public function testSetGetPosition(): void
@@ -75,4 +79,14 @@ public function testAdd(): void
7579
$this->assertEquals(2, $line->count());
7680
$this->assertEquals(2, $result->count());
7781
}
82+
83+
public static function toStringDataProvider(): Generator
84+
{
85+
yield ['foo', 1];
86+
yield ['foo bar', 2];
87+
yield ['测试', 2]; // CJK Unified Ideographs
88+
yield ['テスト', 3]; // japanese
89+
yield ['ทดสอบ', 5]; // thai
90+
yield ['这只是我写的一个测试。', 11]; // CJK Unified Ideographs
91+
}
7892
}

0 commit comments

Comments
 (0)