denshoch / harusame
Add special HTML classes for Tate-Chu-Yoko and text-orientaion.
Requires
- php: >=7.4 || ^8.0
Requires (Dev)
- phpstan/phpstan: ^2.0
- phpunit/phpunit: ^11.4
- squizlabs/php_codesniffer: ^3.5
README
Harusame is a library designed to control the orientation of characters in vertical writing. It allows for the proper display of text in Japanese vertical format, ensuring that characters are displayed upright or sideways as needed. This is particularly useful for applications that require text to be formatted according to traditional Japanese typesetting standards.
Wrap sequence of numbers (default is two-digit) and sequence of EXCLAMATION (QUESTION) MARKs with span.tcy
in (see Tate-Chu-Yoko).
Wrap each character of upright glyph orientation with span.upright
and sideways glyph orientation with span.sideways
(see UTR50).
Expected CSS;
.tcy { text-combine-upright: all; } .upright { text-orientation: upright; } .sideways { text-orientation: sideways; }
Install
composer install
Usage
New static method transformText
$result = Denshoch\Harusame::transformText('平成20年!?'); // => 平成<span class="tcy">20</span>年<span class="tcy">!?</span> // With options $options = ['tcyDigit' => 3]; $result = Denshoch\Harusame::transformText('10円玉と100円玉がある。', $options); // => <span class="tcy">10</span>円玉と<span class="tcy">100</span>円玉がある。
Previous usage
$harusame = new Denshoch\Harusame(); $harusame->transform('平成20年!?'); // => 平成<span class="tcy">20</span>年<span class="tcy">!?</span> $harusame->transform('<html><head><title>平成20年!?</title></head><body>平成20年!?</body></html>'); // You can pass HTML string. Only text nodes within the body tag are transformed. // => <html><head><title>平成20年!?</title></head><body>平成<span class="tcy">20</span>年<span class="tcy">!?</span></body></html> $harusame->transform('⓵☂÷∴'); // => <span class="upright">⓵</span><span class="upright">☂</span><span class="sideways">÷</span><span class="sideways">∴</span>
Options
$options = array("tcyDigit" => 3); $harusame = new Denshoch\Harusame($options); $harusame->transform('10円玉と100円玉がある。'); // => <span class="tcy">10</span>円玉と<span class="tcy">100</span>円玉がある。 // or $harusame = new Denshoch\Harusame(); $harusame->tcyDigit = 3; $harusame->transform('10円玉と100円玉がある。'); // => <span class="tcy">10</span>円玉と<span class="tcy">100</span>円玉がある。
Error Handling
When invalid XML is provided as input, the transform
method will return the original text instead of throwing an exception. Additionally, an error message will be logged to indicate that invalid XML was encountered. This allows for graceful handling of errors while still providing feedback about the issue.
Test
$ vendor/bin/phpunit