Initial commit

This commit is contained in:
2026-08-30 22:02:02 +00:00
commit b6bd2277f5
2334 changed files with 646393 additions and 0 deletions
+20
View File
@@ -0,0 +1,20 @@
# Ref: https://EditorConfig.org
# top-most EditorConfig file
root = true
# Unix-style end of lines and a blank line at the end of the file
[*]
indent_style = tab
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
[*.php]
indent_style = space
indent_size = 4
[*.{js,json,scss,css,yml,vue}]
indent_style = space
indent_size = 2
@@ -0,0 +1,22 @@
# Normalize text sources to LF in the repository and on checkout everywhere.
# Critical for a byte-level library: keeps source and fixtures byte-identical
# across Windows/macOS/Linux checkouts regardless of core.autocrlf.
*.php text eol=lf
*.md text eol=lf
*.xml text eol=lf
*.xml.dist text eol=lf
*.json text eol=lf
*.toml text eol=lf
*.yml text eol=lf
*.yaml text eol=lf
*.txt text eol=lf
Makefile text eol=lf
# Byte-exact fixtures must never be transformed (CRLF/auto-detection off).
test/**/*.bin -text
*.png binary
*.jpg binary
*.gif binary
*.pdf binary
*.ttf binary
*.otf binary
@@ -0,0 +1,29 @@
---
name: Bug report
about: Create a report to help us improve
title: ''
labels: ''
assignees: ''
---
**Describe the bug**
A clear and concise description of what the bug is.
**To Reproduce**
Steps to reproduce the behavior:
1. ...
**Expected behavior**
A clear and concise description of what you expected to happen.
**Logs**
If applicable, copy the relevant logs to help explain your problem.
**Environment:**
- OS:
- PHP version:
- Version:
**Additional context**
Add any other context about the problem here.
@@ -0,0 +1,20 @@
---
name: Feature request
about: Suggest an idea for this project
title: ''
labels: ''
assignees: ''
---
**Is your feature request related to a problem? Please describe.**
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
**Describe the solution you'd like**
A clear and concise description of what you want to happen.
**Describe alternatives you've considered**
A clear and concise description of any alternative solutions or features you've considered.
**Additional context**
Add any other context or screenshots about the feature request here.
@@ -0,0 +1,25 @@
# Description
Please include a summary of the change and include relevant motivation and context.
...
## Checklist:
- [ ] The `make buildall` command has been run successfully without any error or warning.
- [ ] Any new code line is covered by unit tests and the coverage has not dropped.
- [ ] Any new code follows the style guidelines of this project.
- [ ] The code changes have been self-reviewed.
- [ ] Corresponding changes to the documentation have been made.
- [ ] The version has been updated in the VERSION file.
## Type of change:
- [ ] Bug fix (non-breaking change which fixes an issue) → The patch number in the VERSION file has been increased.
- [ ] New feature (non-breaking change which adds functionality) → The minor number in the VERSION file has been increased.
- [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected) → The major number in the VERSION file has been increased.
- [ ] Automation.
- [ ] Documentation.
- [ ] Example.
- [ ] Testing.
@@ -0,0 +1,59 @@
name: check
env:
XDEBUG_MODE: coverage
permissions:
contents: read
on:
push:
branches:
- 'main'
pull_request:
types: [opened, synchronize, reopened]
branches:
- main
workflow_dispatch:
jobs:
test-php:
name: Test on php ${{ matrix.php-version }} and ${{ matrix.os }}
runs-on: ${{ matrix.os }}
continue-on-error: ${{ matrix.experimental }}
strategy:
fail-fast: false
matrix:
php-version: ["8.2", "8.3", "8.4", "8.5"]
experimental: [false]
os: [ubuntu-latest]
coverage-extension: [pcov]
steps:
- uses: actions/checkout@v7
- name: Use php ${{ matrix.php-version }}
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-version }}
coverage: ${{ matrix.coverage-extension }}
extensions: bcmath, curl, date, gd, hash, imagick, json, mbstring, openssl, pcre, zlib
ini-values: display_errors=on, error_reporting=-1, zend.assertions=1
- name: List php modules
run: php -m
- name: List php modules using "no php ini" mode
run: php -m -n
- name: Cache module
uses: actions/cache@v6
with:
path: ~/.composer/cache/
key: composer-cache
- name: Install dependencies
env:
GH_TOKEN: ${{ github.token }}
run: make deps
- name: Run all tests
run: make qa
- name: Send coverage
uses: codecov/codecov-action@v7
with:
flags: php-${{ matrix.php-version }}-${{ matrix.os }}
name: php-${{ matrix.php-version }}-${{ matrix.os }}
@@ -0,0 +1,21 @@
.phpdoc
.phpunit.cache
.phpunit.result.cache
**/._*
**/.#*
**/.DS_Store
**/.idea
**/.vagrant
**/*.bak
**/*.tmp
**/auth.json
**/nbproject
**/temp.php
**/test.php
composer.lock
ecs.php
phpunit.xml
PLAN_*
rector.php
target
vendor
+1
View File
@@ -0,0 +1 @@
* @nicolaasuni
+128
View File
@@ -0,0 +1,128 @@
# Contributor Covenant Code of Conduct
## Our Pledge
We as members, contributors, and leaders pledge to make participation in our
community a harassment-free experience for everyone, regardless of age, body
size, visible or invisible disability, ethnicity, sex characteristics, gender
identity and expression, level of experience, education, socio-economic status,
nationality, personal appearance, race, religion, or sexual identity
and orientation.
We pledge to act and interact in ways that contribute to an open, welcoming,
diverse, inclusive, and healthy community.
## Our Standards
Examples of behavior that contributes to a positive environment for our
community include:
* Demonstrating empathy and kindness toward other people
* Being respectful of differing opinions, viewpoints, and experiences
* Giving and gracefully accepting constructive feedback
* Accepting responsibility and apologizing to those affected by our mistakes,
and learning from the experience
* Focusing on what is best not just for us as individuals, but for the
overall community
Examples of unacceptable behavior include:
* The use of sexualized language or imagery, and sexual attention or
advances of any kind
* Trolling, insulting or derogatory comments, and personal or political attacks
* Public or private harassment
* Publishing others' private information, such as a physical or email
address, without their explicit permission
* Other conduct which could reasonably be considered inappropriate in a
professional setting
## Enforcement Responsibilities
Community leaders are responsible for clarifying and enforcing our standards of
acceptable behavior and will take appropriate and fair corrective action in
response to any behavior that they deem inappropriate, threatening, offensive,
or harmful.
Community leaders have the right and responsibility to remove, edit, or reject
comments, commits, code, wiki edits, issues, and other contributions that are
not aligned to this Code of Conduct, and will communicate reasons for moderation
decisions when appropriate.
## Scope
This Code of Conduct applies within all community spaces, and also applies when
an individual is officially representing the community in public spaces.
Examples of representing our community include using an official e-mail address,
posting via an official social media account, or acting as an appointed
representative at an online or offline event.
## Enforcement
Instances of abusive, harassing, or otherwise unacceptable behavior may be
reported to the community leaders responsible for enforcement at
.
All complaints will be reviewed and investigated promptly and fairly.
All community leaders are obligated to respect the privacy and security of the
reporter of any incident.
## Enforcement Guidelines
Community leaders will follow these Community Impact Guidelines in determining
the consequences for any action they deem in violation of this Code of Conduct:
### 1. Correction
**Community Impact**: Use of inappropriate language or other behavior deemed
unprofessional or unwelcome in the community.
**Consequence**: A private, written warning from community leaders, providing
clarity around the nature of the violation and an explanation of why the
behavior was inappropriate. A public apology may be requested.
### 2. Warning
**Community Impact**: A violation through a single incident or series
of actions.
**Consequence**: A warning with consequences for continued behavior. No
interaction with the people involved, including unsolicited interaction with
those enforcing the Code of Conduct, for a specified period of time. This
includes avoiding interactions in community spaces as well as external channels
like social media. Violating these terms may lead to a temporary or
permanent ban.
### 3. Temporary Ban
**Community Impact**: A serious violation of community standards, including
sustained inappropriate behavior.
**Consequence**: A temporary ban from any sort of interaction or public
communication with the community for a specified period of time. No public or
private interaction with the people involved, including unsolicited interaction
with those enforcing the Code of Conduct, is allowed during this period.
Violating these terms may lead to a permanent ban.
### 4. Permanent Ban
**Community Impact**: Demonstrating a pattern of violation of community
standards, including sustained inappropriate behavior, harassment of an
individual, or aggression toward or disparagement of classes of individuals.
**Consequence**: A permanent ban from any sort of public interaction within
the community.
## Attribution
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
version 2.0, available at
https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
Community Impact Guidelines were inspired by [Mozilla's code of conduct
enforcement ladder](https://github.com/mozilla/diversity).
[homepage]: https://www.contributor-covenant.org
For answers to common questions about this code of conduct, see the FAQ at
https://www.contributor-covenant.org/faq. Translations are available at
https://www.contributor-covenant.org/translations.
+194
View File
@@ -0,0 +1,194 @@
# Contributing to tc-lib-unicode
Thank you for your interest in contributing to **tc-lib-unicode**.
Contributions of all kinds are welcome: bug reports, bug fixes, documentation improvements, new features, and refactors.
Please take a moment to read this guide before opening an issue or pull request.
---
## Table of Contents
- [Code of Conduct](#code-of-conduct)
- [Security Vulnerabilities](#security-vulnerabilities)
- [Getting Started](#getting-started)
- [Reporting a Bug](#reporting-a-bug)
- [Submitting a Bug Fix](#submitting-a-bug-fix)
- [Proposing a New Feature](#proposing-a-new-feature)
- [Development Workflow](#development-workflow)
- [Coding Standards](#coding-standards)
- [Testing](#testing)
- [Pull Request Guidelines](#pull-request-guidelines)
- [Commit Message Guidelines](#commit-message-guidelines)
---
## Code of Conduct
This project follows the [Contributor Covenant Code of Conduct](CODE_OF_CONDUCT.md). By participating you agree to abide by its terms. Please report unacceptable behaviour to [info@tecnick.com](mailto:info@tecnick.com).
---
## Security Vulnerabilities
**Do not open a public GitHub issue for security vulnerabilities.**
Please follow the [Security Policy](SECURITY.md) and report them privately.
---
## Getting Started
### Requirements
- PHP **≥ 8.2**
- [Composer](https://getcomposer.org/) v2
- `make`, `git`
- Optional: `rpmbuild` (RPM packaging), `dpkg-buildpackage` (DEB packaging)
### Local setup
```bash
git clone https://github.com/tecnickcom/tc-lib-unicode.git
cd tc-lib-unicode
make buildall
```
To verify everything is working after a change:
```bash
make qa
```
This runs linting, static analysis, and the full unit-test suite with coverage.
---
## Reporting a Bug
Before opening an issue:
1. **Check the [Security Policy](SECURITY.md)** — if the bug is a security vulnerability, do not file a public issue.
2. **Search [existing issues](https://github.com/tecnickcom/tc-lib-unicode/issues)** to avoid duplicates.
If no existing issue matches, [open a new one](https://github.com/tecnickcom/tc-lib-unicode/issues/new) and include:
- A **clear title and description** of the problem.
- The **library version** (`composer show tecnickcom/tc-lib-unicode`) and PHP version.
- A **minimal, self-contained reproduction** — a short PHP script or a failing PHPUnit test case is ideal.
- **Expected vs. actual behaviour** — what you expected to happen and what actually happened.
- Any relevant **stack trace or error output**.
The more precise and reproducible the report, the faster it can be triaged and fixed.
---
## Submitting a Bug Fix
1. [Fork the repository](https://github.com/tecnickcom/tc-lib-unicode/fork) and create a branch from `main`:
```bash
git checkout -b fix/short-description-of-bug
```
2. Make your changes, following the [Coding Standards](#coding-standards) below.
3. Add or update unit tests to cover the changes.
4. Run the full quality-assurance suite locally and ensure it passes:
```bash
make qa
```
5. Commit your changes (see [Commit Message Guidelines](#commit-message-guidelines)).
6. Open a pull request against `main` and fill in the PR template:
- Describe the problem and your solution.
- Reference the related issue number (e.g. `Fixes #123`).
---
## Proposing a New Feature
Before writing any code:
1. **Open a Feature Request** on [GitHub Issues](https://github.com/tecnickcom/tc-lib-unicode/issues/new) describing the use case and proposed API.
2. Wait for feedback from the maintainer. This avoids investing time in a direction that may not be accepted.
Once the feature is agreed upon, follow the same branch → code → test → PR workflow as for bug fixes, using a branch named `feature/short-description`.
---
## Development Workflow
The `Makefile` exposes all common development tasks:
| Command | Description |
|---------|-------------|
| `make qa` | Run linting, static analysis, tests, and reports |
| `make test` | Run PHPUnit with code coverage |
| `make lint` | Check coding standards |
| `make format` | Auto-format the code |
| `make buildall` | Install dependencies, fix style, run QA, and build packages |
| `make clean` | Remove `vendor/` and `target/` directories |
| `make server` | Start the built-in PHP development server for the examples |
Run `make help` to see the full list of available targets.
---
## Coding Standards
- The codebase follows **PSR-12** for formatting.
- Run `make format` to auto-format the code.
- Run `make lint` to catch remaining issues.
- All source files live under `src/`, all tests under `test/`.
- Use strict types and explicit visibility on all class members.
- Avoid introducing new external dependencies without prior discussion.
---
## Testing
Tests are written with [PHPUnit](https://phpunit.de/) and live in `test/`.
```bash
# Run the full test suite with coverage
make test
# Run a specific test file
XDEBUG_MODE=coverage ./vendor/bin/phpunit test/HTMLTest.php
```
Requirements for contributions:
- Every bug fix must be accompanied by a regression test that fails before the fix and passes after.
- Every new feature must be accompanied by tests that cover both the happy path and edge cases.
Coverage reports are generated in `target/coverage/`.
---
## Pull Request Guidelines
- Target the `main` branch.
- Keep PRs focused — one fix or feature per PR.
- Ensure `make qa` passes locally before opening the PR.
- Do not bump the version number in your PR; that is handled by the maintainer at release time.
- Be responsive to review feedback; stale PRs may be closed after an extended period of inactivity.
---
## Commit Message Guidelines
Use concise, imperative-mood commit messages:
```
fix: correct path traversal in font loader
feat: add support for XYZ
test: add regression test for #123
docs: update CONTRIBUTING workflow
refactor: extract text measurement into helper
```
Prefix tags: `fix`, `feat`, `test`, `docs`, `refactor`, `chore`, `ci`.
Reference issues where relevant: `fix: correct X (closes #42)`.
---
## Questions?
If you have a question that is not covered here, feel free to open a [GitHub Discussion](https://github.com/tecnickcom/tc-lib-unicode/discussions) or contact the maintainer at [info@tecnick.com](mailto:info@tecnick.com).
+862
View File
@@ -0,0 +1,862 @@
**********************************************************************
* LICENSE
*
* SOFTWARE : tc-lib-unicode
* AUTHOR : Nicola Asuni <info@tecnick.com>
* COPYRIGHT : 2011-2026 Nicola Asuni - Tecnick.com LTD
**********************************************************************
This is free software: you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
**********************************************************************
**********************************************************************
GNU LESSER GENERAL PUBLIC LICENSE
Version 3, 29 June 2007
Copyright (C) 2007 Free Software Foundation, Inc. <https://www.fsf.org/>
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
This version of the GNU Lesser General Public License incorporates
the terms and conditions of version 3 of the GNU General Public
License, supplemented by the additional permissions listed below.
0. Additional Definitions.
As used herein, "this License" refers to version 3 of the GNU Lesser
General Public License, and the "GNU GPL" refers to version 3 of the GNU
General Public License.
"The Library" refers to a covered work governed by this License,
other than an Application or a Combined Work as defined below.
An "Application" is any work that makes use of an interface provided
by the Library, but which is not otherwise based on the Library.
Defining a subclass of a class defined by the Library is deemed a mode
of using an interface provided by the Library.
A "Combined Work" is a work produced by combining or linking an
Application with the Library. The particular version of the Library
with which the Combined Work was made is also called the "Linked
Version".
The "Minimal Corresponding Source" for a Combined Work means the
Corresponding Source for the Combined Work, excluding any source code
for portions of the Combined Work that, considered in isolation, are
based on the Application, and not on the Linked Version.
The "Corresponding Application Code" for a Combined Work means the
object code and/or source code for the Application, including any data
and utility programs needed for reproducing the Combined Work from the
Application, but excluding the System Libraries of the Combined Work.
1. Exception to Section 3 of the GNU GPL.
You may convey a covered work under sections 3 and 4 of this License
without being bound by section 3 of the GNU GPL.
2. Conveying Modified Versions.
If you modify a copy of the Library, and, in your modifications, a
facility refers to a function or data to be supplied by an Application
that uses the facility (other than as an argument passed when the
facility is invoked), then you may convey a copy of the modified
version:
a) under this License, provided that you make a good faith effort to
ensure that, in the event an Application does not supply the
function or data, the facility still operates, and performs
whatever part of its purpose remains meaningful, or
b) under the GNU GPL, with none of the additional permissions of
this License applicable to that copy.
3. Object Code Incorporating Material from Library Header Files.
The object code form of an Application may incorporate material from
a header file that is part of the Library. You may convey such object
code under terms of your choice, provided that, if the incorporated
material is not limited to numerical parameters, data structure
layouts and accessors, or small macros, inline functions and templates
(ten or fewer lines in length), you do both of the following:
a) Give prominent notice with each copy of the object code that the
Library is used in it and that the Library and its use are
covered by this License.
b) Accompany the object code with a copy of the GNU GPL and this license
document.
4. Combined Works.
You may convey a Combined Work under terms of your choice that,
taken together, effectively do not restrict modification of the
portions of the Library contained in the Combined Work and reverse
engineering for debugging such modifications, if you also do each of
the following:
a) Give prominent notice with each copy of the Combined Work that
the Library is used in it and that the Library and its use are
covered by this License.
b) Accompany the Combined Work with a copy of the GNU GPL and this license
document.
c) For a Combined Work that displays copyright notices during
execution, include the copyright notice for the Library among
these notices, as well as a reference directing the user to the
copies of the GNU GPL and this license document.
d) Do one of the following:
0) Convey the Minimal Corresponding Source under the terms of this
License, and the Corresponding Application Code in a form
suitable for, and under terms that permit, the user to
recombine or relink the Application with a modified version of
the Linked Version to produce a modified Combined Work, in the
manner specified by section 6 of the GNU GPL for conveying
Corresponding Source.
1) Use a suitable shared library mechanism for linking with the
Library. A suitable mechanism is one that (a) uses at run time
a copy of the Library already present on the user's computer
system, and (b) will operate properly with a modified version
of the Library that is interface-compatible with the Linked
Version.
e) Provide Installation Information, but only if you would otherwise
be required to provide such information under section 6 of the
GNU GPL, and only to the extent that such information is
necessary to install and execute a modified version of the
Combined Work produced by recombining or relinking the
Application with a modified version of the Linked Version. (If
you use option 4d0, the Installation Information must accompany
the Minimal Corresponding Source and Corresponding Application
Code. If you use option 4d1, you must provide the Installation
Information in the manner specified by section 6 of the GNU GPL
for conveying Corresponding Source.)
5. Combined Libraries.
You may place library facilities that are a work based on the
Library side by side in a single library together with other library
facilities that are not Applications and are not covered by this
License, and convey such a combined library under terms of your
choice, if you do both of the following:
a) Accompany the combined library with a copy of the same work based
on the Library, uncombined with any other library facilities,
conveyed under the terms of this License.
b) Give prominent notice with the combined library that part of it
is a work based on the Library, and explaining where to find the
accompanying uncombined form of the same work.
6. Revised Versions of the GNU Lesser General Public License.
The Free Software Foundation may publish revised and/or new versions
of the GNU Lesser General Public License from time to time. Such new
versions will be similar in spirit to the present version, but may
differ in detail to address new problems or concerns.
Each version is given a distinguishing version number. If the
Library as you received it specifies that a certain numbered version
of the GNU Lesser General Public License "or any later version"
applies to it, you have the option of following the terms and
conditions either of that published version or of any later version
published by the Free Software Foundation. If the Library as you
received it does not specify a version number of the GNU Lesser
General Public License, you may choose any version of the GNU Lesser
General Public License ever published by the Free Software Foundation.
If the Library as you received it specifies that a proxy can decide
whether future versions of the GNU Lesser General Public License shall
apply, that proxy's public statement of acceptance of any version is
permanent authorization for you to choose that version for the
Library.
**********************************************************************
**********************************************************************
GNU GENERAL PUBLIC LICENSE
Version 3, 29 June 2007
Copyright (C) 2007 Free Software Foundation, Inc. <https://www.fsf.org/>
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
Preamble
The GNU General Public License is a free, copyleft license for
software and other kinds of works.
The licenses for most software and other practical works are designed
to take away your freedom to share and change the works. By contrast,
the GNU General Public License is intended to guarantee your freedom to
share and change all versions of a program--to make sure it remains free
software for all its users. We, the Free Software Foundation, use the
GNU General Public License for most of our software; it applies also to
any other work released this way by its authors. You can apply it to
your programs, too.
When we speak of free software, we are referring to freedom, not
price. Our General Public Licenses are designed to make sure that you
have the freedom to distribute copies of free software (and charge for
them if you wish), that you receive source code or can get it if you
want it, that you can change the software or use pieces of it in new
free programs, and that you know you can do these things.
To protect your rights, we need to prevent others from denying you
these rights or asking you to surrender the rights. Therefore, you have
certain responsibilities if you distribute copies of the software, or if
you modify it: responsibilities to respect the freedom of others.
For example, if you distribute copies of such a program, whether
gratis or for a fee, you must pass on to the recipients the same
freedoms that you received. You must make sure that they, too, receive
or can get the source code. And you must show them these terms so they
know their rights.
Developers that use the GNU GPL protect your rights with two steps:
(1) assert copyright on the software, and (2) offer you this License
giving you legal permission to copy, distribute and/or modify it.
For the developers' and authors' protection, the GPL clearly explains
that there is no warranty for this free software. For both users' and
authors' sake, the GPL requires that modified versions be marked as
changed, so that their problems will not be attributed erroneously to
authors of previous versions.
Some devices are designed to deny users access to install or run
modified versions of the software inside them, although the manufacturer
can do so. This is fundamentally incompatible with the aim of
protecting users' freedom to change the software. The systematic
pattern of such abuse occurs in the area of products for individuals to
use, which is precisely where it is most unacceptable. Therefore, we
have designed this version of the GPL to prohibit the practice for those
products. If such problems arise substantially in other domains, we
stand ready to extend this provision to those domains in future versions
of the GPL, as needed to protect the freedom of users.
Finally, every program is threatened constantly by software patents.
States should not allow patents to restrict development and use of
software on general-purpose computers, but in those that do, we wish to
avoid the special danger that patents applied to a free program could
make it effectively proprietary. To prevent this, the GPL assures that
patents cannot be used to render the program non-free.
The precise terms and conditions for copying, distribution and
modification follow.
TERMS AND CONDITIONS
0. Definitions.
"This License" refers to version 3 of the GNU General Public License.
"Copyright" also means copyright-like laws that apply to other kinds of
works, such as semiconductor masks.
"The Program" refers to any copyrightable work licensed under this
License. Each licensee is addressed as "you". "Licensees" and
"recipients" may be individuals or organizations.
To "modify" a work means to copy from or adapt all or part of the work
in a fashion requiring copyright permission, other than the making of an
exact copy. The resulting work is called a "modified version" of the
earlier work or a work "based on" the earlier work.
A "covered work" means either the unmodified Program or a work based
on the Program.
To "propagate" a work means to do anything with it that, without
permission, would make you directly or secondarily liable for
infringement under applicable copyright law, except executing it on a
computer or modifying a private copy. Propagation includes copying,
distribution (with or without modification), making available to the
public, and in some countries other activities as well.
To "convey" a work means any kind of propagation that enables other
parties to make or receive copies. Mere interaction with a user through
a computer network, with no transfer of a copy, is not conveying.
An interactive user interface displays "Appropriate Legal Notices"
to the extent that it includes a convenient and prominently visible
feature that (1) displays an appropriate copyright notice, and (2)
tells the user that there is no warranty for the work (except to the
extent that warranties are provided), that licensees may convey the
work under this License, and how to view a copy of this License. If
the interface presents a list of user commands or options, such as a
menu, a prominent item in the list meets this criterion.
1. Source Code.
The "source code" for a work means the preferred form of the work
for making modifications to it. "Object code" means any non-source
form of a work.
A "Standard Interface" means an interface that either is an official
standard defined by a recognized standards body, or, in the case of
interfaces specified for a particular programming language, one that
is widely used among developers working in that language.
The "System Libraries" of an executable work include anything, other
than the work as a whole, that (a) is included in the normal form of
packaging a Major Component, but which is not part of that Major
Component, and (b) serves only to enable use of the work with that
Major Component, or to implement a Standard Interface for which an
implementation is available to the public in source code form. A
"Major Component", in this context, means a major essential component
(kernel, window system, and so on) of the specific operating system
(if any) on which the executable work runs, or a compiler used to
produce the work, or an object code interpreter used to run it.
The "Corresponding Source" for a work in object code form means all
the source code needed to generate, install, and (for an executable
work) run the object code and to modify the work, including scripts to
control those activities. However, it does not include the work's
System Libraries, or general-purpose tools or generally available free
programs which are used unmodified in performing those activities but
which are not part of the work. For example, Corresponding Source
includes interface definition files associated with source files for
the work, and the source code for shared libraries and dynamically
linked subprograms that the work is specifically designed to require,
such as by intimate data communication or control flow between those
subprograms and other parts of the work.
The Corresponding Source need not include anything that users
can regenerate automatically from other parts of the Corresponding
Source.
The Corresponding Source for a work in source code form is that
same work.
2. Basic Permissions.
All rights granted under this License are granted for the term of
copyright on the Program, and are irrevocable provided the stated
conditions are met. This License explicitly affirms your unlimited
permission to run the unmodified Program. The output from running a
covered work is covered by this License only if the output, given its
content, constitutes a covered work. This License acknowledges your
rights of fair use or other equivalent, as provided by copyright law.
You may make, run and propagate covered works that you do not
convey, without conditions so long as your license otherwise remains
in force. You may convey covered works to others for the sole purpose
of having them make modifications exclusively for you, or provide you
with facilities for running those works, provided that you comply with
the terms of this License in conveying all material for which you do
not control copyright. Those thus making or running the covered works
for you must do so exclusively on your behalf, under your direction
and control, on terms that prohibit them from making any copies of
your copyrighted material outside their relationship with you.
Conveying under any other circumstances is permitted solely under
the conditions stated below. Sublicensing is not allowed; section 10
makes it unnecessary.
3. Protecting Users' Legal Rights From Anti-Circumvention Law.
No covered work shall be deemed part of an effective technological
measure under any applicable law fulfilling obligations under article
11 of the WIPO copyright treaty adopted on 20 December 1996, or
similar laws prohibiting or restricting circumvention of such
measures.
When you convey a covered work, you waive any legal power to forbid
circumvention of technological measures to the extent such circumvention
is effected by exercising rights under this License with respect to
the covered work, and you disclaim any intention to limit operation or
modification of the work as a means of enforcing, against the work's
users, your or third parties' legal rights to forbid circumvention of
technological measures.
4. Conveying Verbatim Copies.
You may convey verbatim copies of the Program's source code as you
receive it, in any medium, provided that you conspicuously and
appropriately publish on each copy an appropriate copyright notice;
keep intact all notices stating that this License and any
non-permissive terms added in accord with section 7 apply to the code;
keep intact all notices of the absence of any warranty; and give all
recipients a copy of this License along with the Program.
You may charge any price or no price for each copy that you convey,
and you may offer support or warranty protection for a fee.
5. Conveying Modified Source Versions.
You may convey a work based on the Program, or the modifications to
produce it from the Program, in the form of source code under the
terms of section 4, provided that you also meet all of these conditions:
a) The work must carry prominent notices stating that you modified
it, and giving a relevant date.
b) The work must carry prominent notices stating that it is
released under this License and any conditions added under section
7. This requirement modifies the requirement in section 4 to
"keep intact all notices".
c) You must license the entire work, as a whole, under this
License to anyone who comes into possession of a copy. This
License will therefore apply, along with any applicable section 7
additional terms, to the whole of the work, and all its parts,
regardless of how they are packaged. This License gives no
permission to license the work in any other way, but it does not
invalidate such permission if you have separately received it.
d) If the work has interactive user interfaces, each must display
Appropriate Legal Notices; however, if the Program has interactive
interfaces that do not display Appropriate Legal Notices, your
work need not make them do so.
A compilation of a covered work with other separate and independent
works, which are not by their nature extensions of the covered work,
and which are not combined with it such as to form a larger program,
in or on a volume of a storage or distribution medium, is called an
"aggregate" if the compilation and its resulting copyright are not
used to limit the access or legal rights of the compilation's users
beyond what the individual works permit. Inclusion of a covered work
in an aggregate does not cause this License to apply to the other
parts of the aggregate.
6. Conveying Non-Source Forms.
You may convey a covered work in object code form under the terms
of sections 4 and 5, provided that you also convey the
machine-readable Corresponding Source under the terms of this License,
in one of these ways:
a) Convey the object code in, or embodied in, a physical product
(including a physical distribution medium), accompanied by the
Corresponding Source fixed on a durable physical medium
customarily used for software interchange.
b) Convey the object code in, or embodied in, a physical product
(including a physical distribution medium), accompanied by a
written offer, valid for at least three years and valid for as
long as you offer spare parts or customer support for that product
model, to give anyone who possesses the object code either (1) a
copy of the Corresponding Source for all the software in the
product that is covered by this License, on a durable physical
medium customarily used for software interchange, for a price no
more than your reasonable cost of physically performing this
conveying of source, or (2) access to copy the
Corresponding Source from a network server at no charge.
c) Convey individual copies of the object code with a copy of the
written offer to provide the Corresponding Source. This
alternative is allowed only occasionally and noncommercially, and
only if you received the object code with such an offer, in accord
with subsection 6b.
d) Convey the object code by offering access from a designated
place (gratis or for a charge), and offer equivalent access to the
Corresponding Source in the same way through the same place at no
further charge. You need not require recipients to copy the
Corresponding Source along with the object code. If the place to
copy the object code is a network server, the Corresponding Source
may be on a different server (operated by you or a third party)
that supports equivalent copying facilities, provided you maintain
clear directions next to the object code saying where to find the
Corresponding Source. Regardless of what server hosts the
Corresponding Source, you remain obligated to ensure that it is
available for as long as needed to satisfy these requirements.
e) Convey the object code using peer-to-peer transmission, provided
you inform other peers where the object code and Corresponding
Source of the work are being offered to the general public at no
charge under subsection 6d.
A separable portion of the object code, whose source code is excluded
from the Corresponding Source as a System Library, need not be
included in conveying the object code work.
A "User Product" is either (1) a "consumer product", which means any
tangible personal property which is normally used for personal, family,
or household purposes, or (2) anything designed or sold for incorporation
into a dwelling. In determining whether a product is a consumer product,
doubtful cases shall be resolved in favor of coverage. For a particular
product received by a particular user, "normally used" refers to a
typical or common use of that class of product, regardless of the status
of the particular user or of the way in which the particular user
actually uses, or expects or is expected to use, the product. A product
is a consumer product regardless of whether the product has substantial
commercial, industrial or non-consumer uses, unless such uses represent
the only significant mode of use of the product.
"Installation Information" for a User Product means any methods,
procedures, authorization keys, or other information required to install
and execute modified versions of a covered work in that User Product from
a modified version of its Corresponding Source. The information must
suffice to ensure that the continued functioning of the modified object
code is in no case prevented or interfered with solely because
modification has been made.
If you convey an object code work under this section in, or with, or
specifically for use in, a User Product, and the conveying occurs as
part of a transaction in which the right of possession and use of the
User Product is transferred to the recipient in perpetuity or for a
fixed term (regardless of how the transaction is characterized), the
Corresponding Source conveyed under this section must be accompanied
by the Installation Information. But this requirement does not apply
if neither you nor any third party retains the ability to install
modified object code on the User Product (for example, the work has
been installed in ROM).
The requirement to provide Installation Information does not include a
requirement to continue to provide support service, warranty, or updates
for a work that has been modified or installed by the recipient, or for
the User Product in which it has been modified or installed. Access to a
network may be denied when the modification itself materially and
adversely affects the operation of the network or violates the rules and
protocols for communication across the network.
Corresponding Source conveyed, and Installation Information provided,
in accord with this section must be in a format that is publicly
documented (and with an implementation available to the public in
source code form), and must require no special password or key for
unpacking, reading or copying.
7. Additional Terms.
"Additional permissions" are terms that supplement the terms of this
License by making exceptions from one or more of its conditions.
Additional permissions that are applicable to the entire Program shall
be treated as though they were included in this License, to the extent
that they are valid under applicable law. If additional permissions
apply only to part of the Program, that part may be used separately
under those permissions, but the entire Program remains governed by
this License without regard to the additional permissions.
When you convey a copy of a covered work, you may at your option
remove any additional permissions from that copy, or from any part of
it. (Additional permissions may be written to require their own
removal in certain cases when you modify the work.) You may place
additional permissions on material, added by you to a covered work,
for which you have or can give appropriate copyright permission.
Notwithstanding any other provision of this License, for material you
add to a covered work, you may (if authorized by the copyright holders of
that material) supplement the terms of this License with terms:
a) Disclaiming warranty or limiting liability differently from the
terms of sections 15 and 16 of this License; or
b) Requiring preservation of specified reasonable legal notices or
author attributions in that material or in the Appropriate Legal
Notices displayed by works containing it; or
c) Prohibiting misrepresentation of the origin of that material, or
requiring that modified versions of such material be marked in
reasonable ways as different from the original version; or
d) Limiting the use for publicity purposes of names of licensors or
authors of the material; or
e) Declining to grant rights under trademark law for use of some
trade names, trademarks, or service marks; or
f) Requiring indemnification of licensors and authors of that
material by anyone who conveys the material (or modified versions of
it) with contractual assumptions of liability to the recipient, for
any liability that these contractual assumptions directly impose on
those licensors and authors.
All other non-permissive additional terms are considered "further
restrictions" within the meaning of section 10. If the Program as you
received it, or any part of it, contains a notice stating that it is
governed by this License along with a term that is a further
restriction, you may remove that term. If a license document contains
a further restriction but permits relicensing or conveying under this
License, you may add to a covered work material governed by the terms
of that license document, provided that the further restriction does
not survive such relicensing or conveying.
If you add terms to a covered work in accord with this section, you
must place, in the relevant source files, a statement of the
additional terms that apply to those files, or a notice indicating
where to find the applicable terms.
Additional terms, permissive or non-permissive, may be stated in the
form of a separately written license, or stated as exceptions;
the above requirements apply either way.
8. Termination.
You may not propagate or modify a covered work except as expressly
provided under this License. Any attempt otherwise to propagate or
modify it is void, and will automatically terminate your rights under
this License (including any patent licenses granted under the third
paragraph of section 11).
However, if you cease all violation of this License, then your
license from a particular copyright holder is reinstated (a)
provisionally, unless and until the copyright holder explicitly and
finally terminates your license, and (b) permanently, if the copyright
holder fails to notify you of the violation by some reasonable means
prior to 60 days after the cessation.
Moreover, your license from a particular copyright holder is
reinstated permanently if the copyright holder notifies you of the
violation by some reasonable means, this is the first time you have
received notice of violation of this License (for any work) from that
copyright holder, and you cure the violation prior to 30 days after
your receipt of the notice.
Termination of your rights under this section does not terminate the
licenses of parties who have received copies or rights from you under
this License. If your rights have been terminated and not permanently
reinstated, you do not qualify to receive new licenses for the same
material under section 10.
9. Acceptance Not Required for Having Copies.
You are not required to accept this License in order to receive or
run a copy of the Program. Ancillary propagation of a covered work
occurring solely as a consequence of using peer-to-peer transmission
to receive a copy likewise does not require acceptance. However,
nothing other than this License grants you permission to propagate or
modify any covered work. These actions infringe copyright if you do
not accept this License. Therefore, by modifying or propagating a
covered work, you indicate your acceptance of this License to do so.
10. Automatic Licensing of Downstream Recipients.
Each time you convey a covered work, the recipient automatically
receives a license from the original licensors, to run, modify and
propagate that work, subject to this License. You are not responsible
for enforcing compliance by third parties with this License.
An "entity transaction" is a transaction transferring control of an
organization, or substantially all assets of one, or subdividing an
organization, or merging organizations. If propagation of a covered
work results from an entity transaction, each party to that
transaction who receives a copy of the work also receives whatever
licenses to the work the party's predecessor in interest had or could
give under the previous paragraph, plus a right to possession of the
Corresponding Source of the work from the predecessor in interest, if
the predecessor has it or can get it with reasonable efforts.
You may not impose any further restrictions on the exercise of the
rights granted or affirmed under this License. For example, you may
not impose a license fee, royalty, or other charge for exercise of
rights granted under this License, and you may not initiate litigation
(including a cross-claim or counterclaim in a lawsuit) alleging that
any patent claim is infringed by making, using, selling, offering for
sale, or importing the Program or any portion of it.
11. Patents.
A "contributor" is a copyright holder who authorizes use under this
License of the Program or a work on which the Program is based. The
work thus licensed is called the contributor's "contributor version".
A contributor's "essential patent claims" are all patent claims
owned or controlled by the contributor, whether already acquired or
hereafter acquired, that would be infringed by some manner, permitted
by this License, of making, using, or selling its contributor version,
but do not include claims that would be infringed only as a
consequence of further modification of the contributor version. For
purposes of this definition, "control" includes the right to grant
patent sublicenses in a manner consistent with the requirements of
this License.
Each contributor grants you a non-exclusive, worldwide, royalty-free
patent license under the contributor's essential patent claims, to
make, use, sell, offer for sale, import and otherwise run, modify and
propagate the contents of its contributor version.
In the following three paragraphs, a "patent license" is any express
agreement or commitment, however denominated, not to enforce a patent
(such as an express permission to practice a patent or covenant not to
sue for patent infringement). To "grant" such a patent license to a
party means to make such an agreement or commitment not to enforce a
patent against the party.
If you convey a covered work, knowingly relying on a patent license,
and the Corresponding Source of the work is not available for anyone
to copy, free of charge and under the terms of this License, through a
publicly available network server or other readily accessible means,
then you must either (1) cause the Corresponding Source to be so
available, or (2) arrange to deprive yourself of the benefit of the
patent license for this particular work, or (3) arrange, in a manner
consistent with the requirements of this License, to extend the patent
license to downstream recipients. "Knowingly relying" means you have
actual knowledge that, but for the patent license, your conveying the
covered work in a country, or your recipient's use of the covered work
in a country, would infringe one or more identifiable patents in that
country that you have reason to believe are valid.
If, pursuant to or in connection with a single transaction or
arrangement, you convey, or propagate by procuring conveyance of, a
covered work, and grant a patent license to some of the parties
receiving the covered work authorizing them to use, propagate, modify
or convey a specific copy of the covered work, then the patent license
you grant is automatically extended to all recipients of the covered
work and works based on it.
A patent license is "discriminatory" if it does not include within
the scope of its coverage, prohibits the exercise of, or is
conditioned on the non-exercise of one or more of the rights that are
specifically granted under this License. You may not convey a covered
work if you are a party to an arrangement with a third party that is
in the business of distributing software, under which you make payment
to the third party based on the extent of your activity of conveying
the work, and under which the third party grants, to any of the
parties who would receive the covered work from you, a discriminatory
patent license (a) in connection with copies of the covered work
conveyed by you (or copies made from those copies), or (b) primarily
for and in connection with specific products or compilations that
contain the covered work, unless you entered into that arrangement,
or that patent license was granted, prior to 28 March 2007.
Nothing in this License shall be construed as excluding or limiting
any implied license or other defenses to infringement that may
otherwise be available to you under applicable patent law.
12. No Surrender of Others' Freedom.
If conditions are imposed on you (whether by court order, agreement or
otherwise) that contradict the conditions of this License, they do not
excuse you from the conditions of this License. If you cannot convey a
covered work so as to satisfy simultaneously your obligations under this
License and any other pertinent obligations, then as a consequence you may
not convey it at all. For example, if you agree to terms that obligate you
to collect a royalty for further conveying from those to whom you convey
the Program, the only way you could satisfy both those terms and this
License would be to refrain entirely from conveying the Program.
13. Use with the GNU Affero General Public License.
Notwithstanding any other provision of this License, you have
permission to link or combine any covered work with a work licensed
under version 3 of the GNU Affero General Public License into a single
combined work, and to convey the resulting work. The terms of this
License will continue to apply to the part which is the covered work,
but the special requirements of the GNU Affero General Public License,
section 13, concerning interaction through a network will apply to the
combination as such.
14. Revised Versions of this License.
The Free Software Foundation may publish revised and/or new versions of
the GNU General Public License from time to time. Such new versions will
be similar in spirit to the present version, but may differ in detail to
address new problems or concerns.
Each version is given a distinguishing version number. If the
Program specifies that a certain numbered version of the GNU General
Public License "or any later version" applies to it, you have the
option of following the terms and conditions either of that numbered
version or of any later version published by the Free Software
Foundation. If the Program does not specify a version number of the
GNU General Public License, you may choose any version ever published
by the Free Software Foundation.
If the Program specifies that a proxy can decide which future
versions of the GNU General Public License can be used, that proxy's
public statement of acceptance of a version permanently authorizes you
to choose that version for the Program.
Later license versions may give you additional or different
permissions. However, no additional obligations are imposed on any
author or copyright holder as a result of your choosing to follow a
later version.
15. Disclaimer of Warranty.
THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
16. Limitation of Liability.
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
SUCH DAMAGES.
17. Interpretation of Sections 15 and 16.
If the disclaimer of warranty and limitation of liability provided
above cannot be given local legal effect according to their terms,
reviewing courts shall apply local law that most closely approximates
an absolute waiver of all civil liability in connection with the
Program, unless a warranty or assumption of liability accompanies a
copy of the Program in return for a fee.
END OF TERMS AND CONDITIONS
How to Apply These Terms to Your New Programs
If you develop a new program, and you want it to be of the greatest
possible use to the public, the best way to achieve this is to make it
free software which everyone can redistribute and change under these terms.
To do so, attach the following notices to the program. It is safest
to attach them to the start of each source file to most effectively
state the exclusion of warranty; and each file should have at least
the "copyright" line and a pointer to where the full notice is found.
<one line to give the program's name and a brief idea of what it does.>
Copyright (C) <year> <name of author>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
Also add information on how to contact you by electronic and paper mail.
If the program does terminal interaction, make it output a short
notice like this when it starts in an interactive mode:
<program> Copyright (C) <year> <name of author>
This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
This is free software, and you are welcome to redistribute it
under certain conditions; type `show c' for details.
The hypothetical commands `show w' and `show c' should show the appropriate
parts of the General Public License. Of course, your program's commands
might be different; for a GUI interface, you would use an "about box".
You should also get your employer (if you work as a programmer) or school,
if any, to sign a "copyright disclaimer" for the program, if necessary.
For more information on this, and how to apply and follow the GNU GPL, see
<https://www.gnu.org/licenses/>.
The GNU General Public License does not permit incorporating your program
into proprietary programs. If your program is a subroutine library, you
may consider it more useful to permit linking proprietary applications with
the library. If this is what you want to do, use the GNU Lesser General
Public License instead of this License. But first, please read
<https://www.gnu.org/philosophy/why-not-lgpl.html>.
**********************************************************************
**********************************************************************
+294
View File
@@ -0,0 +1,294 @@
# makefile
#
# @since 2015-07-13
# @category Library
# @package Unicode
# @author Nicola Asuni <info@tecnick.com>
# @copyright 2011-2026 Nicola Asuni - Tecnick.com LTD
# @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
# @link https://github.com/tecnickcom/tc-lib-unicode
#
# This file is part of tc-lib-unicode software library.
# ----------------------------------------------------------------------------------------------------------------------
SHELL=/bin/bash
.SHELLFLAGS=-o pipefail -c
# Project owner
OWNER=tecnickcom
# Project vendor
VENDOR=${OWNER}
# Project name
PROJECT=tc-lib-unicode
# Project version
VERSION=$(shell cat VERSION)
# Project release number (packaging build number)
RELEASE=$(shell cat RELEASE)
# Name of RPM or DEB package
PKGNAME=php-${OWNER}-${PROJECT}
# Data dir
DATADIR=usr/share
# PHP home folder
PHPHOME=${DATADIR}/php/Com/Tecnick
# Default installation path for code
LIBPATH=${PHPHOME}/Unicode/
# Path for configuration files (etc/$(PKGNAME)/)
CONFIGPATH=
# Default installation path for documentation
DOCPATH=${DATADIR}/doc/$(PKGNAME)/
# Installation path for the code
PATHINSTBIN=$(DESTDIR)/$(LIBPATH)
# Installation path for the configuration files
PATHINSTCFG=$(DESTDIR)/$(CONFIGPATH)
# Installation path for documentation
PATHINSTDOC=$(DESTDIR)/$(DOCPATH)
# Current directory
CURRENTDIR=$(CURDIR)/
# Target directory
TARGETDIR=target
# RPM Packaging path (where RPMs will be stored)
PATHRPMPKG=$(TARGETDIR)/RPM
# RPM local database path (avoid host rpmdb permission issues)
RPMDBPATH=$(PATHRPMPKG)/.rpmdb
# DEB Packaging path (where DEBs will be stored)
PATHDEBPKG=$(TARGETDIR)/DEB
# BZ2 Packaging path (where BZ2s will be stored)
PATHBZ2PKG=$(TARGETDIR)/BZ2
# sed argument for in-place substitutions
SEDINPLACE=-i
ifeq ($(shell uname -s),Darwin)
SEDINPLACE=-i ''
endif
# Default port number for the example server
PORT?=8000
# PHP binary
PHP=$(shell which php)
# Composer executable (disable APC to as a work-around of a bug)
COMPOSER=$(PHP) -d "apc.enable_cli=0" $(shell which composer)
# phpDocumentor executable file
PHPDOC=$(shell which phpDocumentor)
# Mago version
MAGOVERSION=1.43.0
# Version of the Unicode Character Database used by the conformance test
UCDVERSION=17.0.0
# Directory of the Unicode Character Database conformance data
UCDDIR=$(TARGETDIR)/ucd/$(UCDVERSION)
# --- MAKE TARGETS ---
# Display general help about this command
.PHONY: help
help:
@echo ""
@echo "$(PROJECT) Makefile."
@echo "The following commands are available:"
@echo ""
@awk '/^## /{desc=substr($$0,4)} /^\.PHONY:/{if(NF>1) {target=$$2; if(desc) printf " make %-15s: %s\n",target,desc; desc=""}}' Makefile
@echo ""
@echo "To test and build everything from scratch, use the shortcut:"
@echo " make x"
@echo ""
# alias for help target
.PHONY: all
all: help
# Full build and test sequence
.PHONY: x
x: buildall
## Full build and test sequence
.PHONY: buildall
buildall: deps format qa bz2 rpm deb
## Package the library in a compressed bz2 archive
.PHONY: bz2
bz2:
rm -rf "$(PATHBZ2PKG)"
make install DESTDIR="$(PATHBZ2PKG)"
tar -jcvf "$(PATHBZ2PKG)/$(PKGNAME)-$(VERSION)-$(RELEASE).tbz2" -C "$(PATHBZ2PKG)" "$(DATADIR)"
## Delete the vendor and target directories
.PHONY: clean
clean:
rm -rf ./vendor "$(TARGETDIR)"
## Build a DEB package for Debian-like Linux distributions
.PHONY: deb
deb:
rm -rf "$(PATHDEBPKG)"
$(MAKE) install DESTDIR="$(PATHDEBPKG)/$(PKGNAME)-$(VERSION)"
rm -f "$(PATHDEBPKG)/$(PKGNAME)-$(VERSION)/$(DOCPATH)LICENSE"
tar -zcvf "$(PATHDEBPKG)/$(PKGNAME)_$(VERSION).orig.tar.gz" -C "$(PATHDEBPKG)/" "$(PKGNAME)-$(VERSION)"
cp -rf ./resources/debian "$(PATHDEBPKG)/$(PKGNAME)-$(VERSION)/debian"
find "$(PATHDEBPKG)/$(PKGNAME)-$(VERSION)/debian/" -type f -name '*.bak' -delete
chmod 755 "$(PATHDEBPKG)/$(PKGNAME)-$(VERSION)/debian/rules"
find "$(PATHDEBPKG)/$(PKGNAME)-$(VERSION)/debian/" -type f -exec sed $(SEDINPLACE) "s/~#DATE#~/`date -R`/" {} \;
find "$(PATHDEBPKG)/$(PKGNAME)-$(VERSION)/debian/" -type f -exec sed $(SEDINPLACE) "s/~#VENDOR#~/$(VENDOR)/" {} \;
find "$(PATHDEBPKG)/$(PKGNAME)-$(VERSION)/debian/" -type f -exec sed $(SEDINPLACE) "s/~#PROJECT#~/$(PROJECT)/" {} \;
find "$(PATHDEBPKG)/$(PKGNAME)-$(VERSION)/debian/" -type f -exec sed $(SEDINPLACE) "s/~#PKGNAME#~/$(PKGNAME)/" {} \;
find "$(PATHDEBPKG)/$(PKGNAME)-$(VERSION)/debian/" -type f -exec sed $(SEDINPLACE) "s/~#VERSION#~/$(VERSION)/" {} \;
find "$(PATHDEBPKG)/$(PKGNAME)-$(VERSION)/debian/" -type f -exec sed $(SEDINPLACE) "s/~#RELEASE#~/$(RELEASE)/" {} \;
echo "$(LIBPATH)" > "$(PATHDEBPKG)/$(PKGNAME)-$(VERSION)/debian/$(PKGNAME).dirs"
echo "$(LIBPATH)* $(LIBPATH)" > "$(PATHDEBPKG)/$(PKGNAME)-$(VERSION)/debian/install"
echo "$(DOCPATH)" >> "$(PATHDEBPKG)/$(PKGNAME)-$(VERSION)/debian/$(PKGNAME).dirs"
echo "$(DOCPATH)* $(DOCPATH)" >> "$(PATHDEBPKG)/$(PKGNAME)-$(VERSION)/debian/install"
ifneq ($(strip $(CONFIGPATH)),)
echo "$(CONFIGPATH)" >> "$(PATHDEBPKG)/$(PKGNAME)-$(VERSION)/debian/$(PKGNAME).dirs"
echo "$(CONFIGPATH)* $(CONFIGPATH)" >> "$(PATHDEBPKG)/$(PKGNAME)-$(VERSION)/debian/install"
endif
echo "new-package-should-close-itp-bug" > "$(PATHDEBPKG)/$(PKGNAME)-$(VERSION)/debian/$(PKGNAME).lintian-overrides"
cd "$(PATHDEBPKG)/$(PKGNAME)-$(VERSION)" && debuild -us -uc
## Clean all artifacts and download all dependencies
.PHONY: deps
deps: ensuretarget
rm -rf ./vendor/*
($(COMPOSER) install -vvv --no-interaction)
curl --proto '=https' --tlsv1.2 --silent --show-error --fail --location https://carthage.software/mago.sh | bash -s -- --install-dir=./vendor/bin --version=$(MAGOVERSION)
## Generate source code documentation
.PHONY: doc
doc: ensuretarget
rm -rf "$(TARGETDIR)/doc"
$(PHPDOC) -d ./src -t "$(TARGETDIR)/doc/"
## Create missing target directories for test and build artifacts
.PHONY: ensuretarget
ensuretarget:
mkdir -p "$(TARGETDIR)/test"
mkdir -p "$(TARGETDIR)/report"
mkdir -p "$(TARGETDIR)/doc"
## Install this application
.PHONY: install
install: uninstall
mkdir -p "$(PATHINSTBIN)"
cp -rf ./src/* "$(PATHINSTBIN)"
cp -f ./resources/autoload.php "$(PATHINSTBIN)"
find "$(PATHINSTBIN)" -type d -exec chmod 755 {} \;
find "$(PATHINSTBIN)" -type f -exec chmod 644 {} \;
mkdir -p "$(PATHINSTDOC)"
cp -f ./LICENSE "$(PATHINSTDOC)"
cp -f ./README.md "$(PATHINSTDOC)"
cp -f ./VERSION "$(PATHINSTDOC)"
cp -f ./RELEASE "$(PATHINSTDOC)"
chmod -R 644 "$(PATHINSTDOC)"*
ifneq ($(strip $(CONFIGPATH)),)
mkdir -p "$(PATHINSTCFG)"
touch -c "$(PATHINSTCFG)"*
cp -ru "./resources/${CONFIGPATH}"* "$(PATHINSTCFG)"
find "$(PATHINSTCFG)" -type d -exec chmod 755 {} \;
find "$(PATHINSTCFG)" -type f -exec chmod 644 {} \;
endif
## Format the source code
.PHONY: format
format:
./vendor/bin/mago fmt src test example
## Analyze and Lint the source code
.PHONY: lint
lint:
./vendor/bin/mago --config ./mago.src.toml analyze src
./vendor/bin/mago --config ./mago.test.toml analyze test
./vendor/bin/mago --config ./mago.src.toml lint src
./vendor/bin/mago --config ./mago.test.toml lint test
## Run all tests and reports
.PHONY: qa
qa: ensuretarget lint test report
## Generate various reports
.PHONY: report
report: ensuretarget
./vendor/bin/pdepend --jdepend-xml="$(TARGETDIR)/report/dependencies.xml" --summary-xml="$(TARGETDIR)/report/metrics.xml" --jdepend-chart="$(TARGETDIR)/report/dependecies.svg" --overview-pyramid="$(TARGETDIR)/report/overview-pyramid.svg" --ignore=vendor ./src
#./vendor/bartlett/php-compatinfo/bin/phpcompatinfo --no-ansi analyser:run src/ > $(TARGETDIR)/report/phpcompatinfo.txt
## Build the RPM package for RedHat-like Linux distributions
.PHONY: rpm
rpm:
@test $(words $(CURDIR)) -eq 1 || { echo "ERROR: rpmbuild does not support spaces in the project path: $(CURDIR)"; exit 1; }
rm -rf "$(PATHRPMPKG)"
mkdir -p "$(RPMDBPATH)" "$(PATHRPMPKG)/tmp"
rpmbuild \
--define "_topdir $(CURRENTDIR)$(PATHRPMPKG)" \
--define "_dbpath $(CURRENTDIR)$(RPMDBPATH)" \
--define "_tmppath $(CURRENTDIR)$(PATHRPMPKG)/tmp" \
--define "_vendor $(VENDOR)" \
--define "_owner $(OWNER)" \
--define "_project $(PROJECT)" \
--define "_package $(PKGNAME)" \
--define "_version $(VERSION)" \
--define "_release $(RELEASE)" \
--define "_current_directory $(CURRENTDIR)" \
--define "_libpath /$(LIBPATH)" \
--define "_docpath /$(DOCPATH)" \
--define "_configpath /$(CONFIGPATH)" \
-bb resources/rpm/rpm.spec
## Start the development server
.PHONY: server
server:
$(PHP) -t example -S localhost:$(PORT)
## Tag this GIT version
.PHONY: tag
tag:
git checkout main && \
git tag -a ${VERSION} -m "Release ${VERSION}" && \
git push origin --tags && \
git pull
## Download the Unicode Character Database conformance data
.PHONY: ucd
ucd: $(UCDDIR)/BidiCharacterTest.txt
$(UCDDIR)/BidiCharacterTest.txt:
mkdir -p "$(UCDDIR)"
curl --proto '=https' --tlsv1.2 --silent --show-error --fail --location --output "$@" "https://www.unicode.org/Public/$(UCDVERSION)/ucd/BidiCharacterTest.txt"
## Run unit tests
.PHONY: test
test: ucd
cp phpunit.xml.dist phpunit.xml
#./vendor/bin/phpunit --migrate-configuration || true
XDEBUG_MODE=coverage $(PHP) -d zend.assertions=1 -d assert.exception=1 ./vendor/bin/phpunit --stderr test
## Remove all installed files
.PHONY: uninstall
uninstall:
rm -rf "$(PATHINSTBIN)"
rm -rf "$(PATHINSTDOC)"
## Increase the version patch number
.PHONY: versionup
versionup:
echo ${VERSION} | gawk -F. '{printf("%d.%d.%d\n",$$1,$$2,(($$3+1)));}' > VERSION
+163
View File
@@ -0,0 +1,163 @@
# tc-lib-unicode
> UTF-8 and Unicode processing utilities, including bidirectional text handling.
[![Latest Stable Version](https://poser.pugx.org/tecnickcom/tc-lib-unicode/version)](https://packagist.org/packages/tecnickcom/tc-lib-unicode)
[![Build](https://github.com/tecnickcom/tc-lib-unicode/actions/workflows/check.yml/badge.svg)](https://github.com/tecnickcom/tc-lib-unicode/actions/workflows/check.yml)
[![Coverage](https://codecov.io/gh/tecnickcom/tc-lib-unicode/graph/badge.svg?token=XLM0QWY9BE)](https://codecov.io/gh/tecnickcom/tc-lib-unicode)
[![License](https://poser.pugx.org/tecnickcom/tc-lib-unicode/license)](https://packagist.org/packages/tecnickcom/tc-lib-unicode)
[![Downloads](https://poser.pugx.org/tecnickcom/tc-lib-unicode/downloads)](https://packagist.org/packages/tecnickcom/tc-lib-unicode)
[![Sponsor on GitHub](https://img.shields.io/badge/sponsor-github-EA4AAA.svg?logo=githubsponsors&logoColor=white)](https://github.com/sponsors/tecnickcom)
> 💖 Part of the [tc-lib-pdf / TCPDF](https://github.com/tecnickcom/tc-lib-pdf) ecosystem (100M+ installs). [Sponsor its maintenance →](https://github.com/sponsors/tecnickcom)
---
## Overview
`tc-lib-unicode` provides Unicode conversion helpers and bidirectional algorithm support for robust multilingual text processing.
It is built to handle multilingual text paths where normalization, code-point handling, and bidirectional ordering directly affect rendering quality. By isolating Unicode-heavy operations, dependent libraries can keep text processing accurate and easier to audit.
| | |
|---|---|
| **Namespace** | `\Com\Tecnick\Unicode` |
| **Author** | Nicola Asuni <info@tecnick.com> |
| **License** | [GNU LGPL v3](https://www.gnu.org/copyleft/lesser.html) - see [LICENSE](LICENSE) |
| **API docs** | <https://tcpdf.org/docs/srcdoc/tc-lib-unicode> |
| **Packagist** | <https://packagist.org/packages/tecnickcom/tc-lib-unicode> |
---
## Features
### Unicode Utilities
- UTF-8 character and ordinal conversion helpers
- String/character array transformations
- Integration-ready conversion methods for document engines
### Bidirectional Support
- Unicode Bidirectional Algorithm (UAX #9) implementation, passing the full official `BidiCharacterTest.txt` conformance suite
- Right-to-left and mixed-direction text processing
- Arabic shaping driven by the Joining_Type property
### Character Substitution
- Context-sensitive codepoint-level substitution via `Substitution::replaceChars()`
- **Devanagari**: moves left-positional matras (U+093F, U+094E) to precede their base consonant cluster, including conjuncts joined by Virama (U+094D)
- **Hangul**: composes Hangul Jamo sequences (U+1100-U+11FF, U+A960-U+A97F, U+D7B0-U+D7FF) into precomposed syllables (U+AC00-U+D7A3) per section 3.12 of the Unicode standard
- **Thai**: returned unchanged, because Thai preposed vowels are already stored in visual order
---
## Requirements
- PHP 8.2 or later
- Extension: `mbstring`
- Composer
---
## Installation
```bash
composer require tecnickcom/tc-lib-unicode
```
---
## Quick Start
```php
<?php
require_once __DIR__ . '/vendor/autoload.php';
$bidi = new \Com\Tecnick\Unicode\Bidi('hello ', null, null, 'R', false);
echo $bidi->getString();
```
---
## Character substitution
`Substitution::replaceChars()` takes an array of Unicode codepoints and returns a transformed array with script-specific substitutions applied. It is a pure codepoint-level transform with no font or PDF dependency.
```php
<?php
require_once __DIR__ . '/vendor/autoload.php';
$sub = new \Com\Tecnick\Unicode\Substitution();
// Devanagari: left matra repositioned before its base consonant cluster
// Logical order: [U+0915 KA, U+093F VOWEL SIGN I]
// Visual order: [U+093F VOWEL SIGN I, U+0915 KA]
$result = $sub->replaceChars([0x0915, 0x093F]);
// $result === [0x093F, 0x0915]
// Hangul: Jamo composed into a precomposed syllable
// [U+1100 KIYEOK, U+1161 JUNGSEONG A, U+11A8 JONGSEONG KIYEOK] → [U+AC01 각]
$result = $sub->replaceChars([0x1100, 0x1161, 0x11A8]);
// $result === [0xAC01]
```
### Supported scripts and Unicode ranges
| Script | Unicode range(s) | Transformation |
|---|---|---|
| Devanagari | U+0900-U+097F | Left matras repositioned before consonant cluster |
| Hangul Jamo | U+1100-U+11FF, U+A960-U+A97F, U+D7B0-U+D7FF | Jamo composed to precomposed syllables (U+AC00-U+D7A3) |
| Thai | U+0E00-U+0E7F | None: the stored order is the display order |
Codepoints belonging to unsupported scripts are passed through unchanged.
---
## Limitations
- The paragraph separator is dropped during processing and appended again at the end of
the paragraph output instead of being reset by L1 and reversed by L2. A strict UAX #9
implementation would place it at the visual left edge of a right-to-left paragraph;
keeping it at the end of the string preserves line splitting for the consumers.
- Rule L3 (combining marks applied to characters shown in a different order) is not
implemented.
- Shaping is Arabic only. The other cursive scripts (Syriac, N'Ko, Mandaic, Adlam) are
returned unshaped.
- `Bidi` and `Convert` require valid UTF-8: malformed byte sequences raise an exception,
while code points that cannot be encoded are replaced with '?'.
---
## Development
```bash
make deps
make help
make qa
make server
```
`make server` starts the local PHP development server for the `example/` directory on `http://localhost:8000`.
Use a custom port with `make server PORT=8080`.
---
## Packaging
```bash
make rpm
make deb
```
For system packages, bootstrap with:
```php
require_once '/usr/share/php/Com/Tecnick/Unicode/autoload.php';
```
---
## Contributing
Contributions are welcome. Please review [CONTRIBUTING.md](CONTRIBUTING.md), [CODE_OF_CONDUCT.md](CODE_OF_CONDUCT.md), and [SECURITY.md](SECURITY.md).
+1
View File
@@ -0,0 +1 @@
1
+62
View File
@@ -0,0 +1,62 @@
# Security Policy
This document describes the security policy for **tc-lib-unicode**.
---
## Supported Versions
Security fixes are applied only to the **latest stable release** on the `main` branch.
We strongly recommend always running the latest release.
---
## Reporting a Vulnerability
**Please do not open a public GitHub issue for security vulnerabilities.**
If you discover a security vulnerability — or suspect one — follow responsible disclosure:
1. **Email** the maintainer directly at **[info@tecnick.com](mailto:info@tecnick.com)** with the subject line:
`[SECURITY] tc-lib-unicode <brief description>`
2. Include as much detail as possible (see [What to include](#what-to-include) below).
3. You will receive an acknowledgement as soon as possible.
4. We will work on a fix or mitigation as promptly as the complexity of the issue allows.
If you do not receive a timely response, please follow up by replying to the same email thread.
---
## What to Include
A high-quality report helps us triage and fix issues faster. Please provide:
- **Description** — a clear summary of the vulnerability and its potential impact.
- **Affected component** — which class, method, or feature is involved (e.g., `HTML::render()`, font loading, image processing).
- **Steps to reproduce** — a minimal, self-contained PHP script or unit test that demonstrates the issue.
- **Expected vs. actual behaviour** — what you expected to happen and what actually happened.
- **Environment** — PHP version, OS, library version (output of `composer show tecnickcom/tc-lib-unicode`).
- **CVE / CWE reference** (optional) — if you have already identified a relevant classification.
- **Suggested fix** (optional) — a patch or proposed mitigation if you have one.
---
## Security Best Practices for Integrators
Integrators are responsible for sanitising input **before** passing it to the library. We recommend:
- **Validate and sanitise all user-supplied data**. Use a dedicated sanitiser when accepting content from end users.
- **Keep dependencies up to date.** Run `composer update` regularly and monitor advisories via [Packagist Security Advisories](https://packagist.org/packages/tecnickcom/tc-lib-unicode) or tools such as `composer audit`.
- **Pin versions in production.** Use `composer.lock` and review changes on every update.
---
## Contact
| Channel | Details |
|---------|---------|
| Security email | [info@tecnick.com](mailto:info@tecnick.com) |
| Project website | <https://tcpdf.org> |
| GitHub repository | <https://github.com/tecnickcom/tc-lib-unicode> |
| Packagist | <https://packagist.org/packages/tecnickcom/tc-lib-unicode> |
+1
View File
@@ -0,0 +1 @@
3.0.2
+65
View File
@@ -0,0 +1,65 @@
{
"name": "tecnickcom/tc-lib-unicode",
"description": "PHP library containing Unicode methods",
"type": "library",
"homepage": "https://tcpdf.org",
"license": "LGPL-3.0-or-later",
"keywords": [
"tc-lib-unicode",
"PDF",
"font",
"UTF-8",
"Unicode"
],
"authors": [
{
"name": "Nicola Asuni",
"email": "info@tecnick.com",
"role": "lead"
}
],
"funding": [
{
"type": "github",
"url": "https://github.com/sponsors/tecnickcom"
}
],
"require": {
"php": ">=8.2",
"ext-ctype": "*",
"ext-mbstring": "*",
"ext-pcre": "*",
"tecnickcom/tc-lib-unicode-data": "^3.0"
},
"minimum-stability": "stable",
"prefer-stable": true,
"require-dev": {
"pdepend/pdepend": "^2.16",
"phpunit/phpunit": "^11.5 || ^12.5 || ^13.2"
},
"autoload": {
"psr-4": {
"Com\\Tecnick\\Unicode\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"Test\\": "test"
}
},
"support": {
"issues": "https://github.com/tecnickcom/tc-lib-unicode/issues",
"source": "https://github.com/tecnickcom/tc-lib-unicode"
},
"scripts": {
"test": "@php -d xdebug.mode=coverage -d zend.assertions=1 -d assert.exception=1 vendor/bin/phpunit --stderr test",
"analyse": ["@analyse:src", "@analyse:test"],
"analyse:src": "mago --config mago.src.toml analyze src",
"analyse:test": "mago --config mago.test.toml analyze test",
"cs-check": ["@cs-check:src", "@cs-check:test"],
"cs-check:src": "mago --config mago.src.toml lint src",
"cs-check:test": "mago --config mago.test.toml lint test",
"cs-fix": "mago fmt src test",
"qa": ["@cs-check", "@analyse", "@test"]
}
}
+26
View File
@@ -0,0 +1,26 @@
<?php
/**
* index.php
*
* @since 2011-05-23
* @category Library
* @package Unicode
* @author Nicola Asuni <info@tecnick.com>
* @copyright 2011-2026 Nicola Asuni - Tecnick.com LTD
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
* @link https://github.com/tecnickcom/tc-lib-unicode
*
* This file is part of tc-lib-unicode software library.
*/
// NOTE: run "make deps" in the project root to install the dependencies before running this example.
// autoloader when using Composer
require '../vendor/autoload.php';
use \Com\Tecnick\Unicode\Bidi as Bidi;
$bidi = new Bidi(str: 'hello ', chrarr: null, ordarr: null, forcedir: 'R', shaping: false);
echo $bidi->getString() . "\n";
+56
View File
@@ -0,0 +1,56 @@
#:schema https://mago.carthage.software/1.43.0/schema.json
version = "1"
php-version = "8.2.0"
[source]
workspace = "."
paths = ["src"]
includes = ["vendor"]
excludes = []
[source.glob]
literal-separator = true
[formatter]
print-width = 120
tab-width = 4
use-tabs = false
[linter]
integrations = []
[linter.rules]
ambiguous-function-call = { enabled = false }
literal-named-argument = { enabled = false }
cyclomatic-complexity = { enabled = false }
excessive-parameter-list = { enabled = true }
halstead = { enabled = true, effort-threshold = 7000 }
identity-comparison = { enabled = true }
kan-defect = { enabled = false }
no-boolean-flag-parameter = { enabled = false }
no-else-clause = { enabled = false }
no-empty = { enabled = true }
too-many-methods = { enabled = false }
no-isset = { enabled = true, allow-array-checks = true }
[analyzer]
plugins = []
find-unused-definitions = true
find-unused-expressions = true
analyze-dead-code = true
memoize-properties = true
check-throws = true
unchecked-exceptions = [
"Error",
"LogicException",
"ReflectionException",
]
unchecked-exception-classes = []
check-missing-override = true
find-unused-parameters = true
strict-list-index-checks = true
strict-array-index-existence = true
allow-array-truthy-operand = false
no-boolean-literal-comparison = true
check-missing-type-hints = true
register-super-globals = true
+66
View File
@@ -0,0 +1,66 @@
#:schema https://mago.carthage.software/1.43.0/schema.json
version = "1"
php-version = "8.2.0"
[source]
workspace = "."
paths = ["src", "test"]
includes = ["vendor"]
excludes = []
[source.glob]
literal-separator = true
[formatter]
print-width = 120
tab-width = 4
use-tabs = false
[linter]
integrations = ["phpunit"]
[linter.rules]
ambiguous-function-call = { enabled = false }
literal-named-argument = { enabled = false }
assertion-style = { enabled = false }
cyclomatic-complexity = { enabled = false }
excessive-parameter-list = { enabled = false }
halstead = { enabled = false, effort-threshold = 7000 }
identity-comparison = { enabled = false }
kan-defect = { enabled = false }
no-boolean-flag-parameter = { enabled = false }
no-else-clause = { enabled = false }
no-empty = { enabled = false }
no-empty-catch-clause = { enabled = false }
no-isset = { enabled = false }
readable-literal = { enabled = false }
str-contains = { enabled = false }
strict-assertions = { enabled = false }
strict-behavior = { enabled = false }
strict-types = { enabled = false }
too-many-methods = { enabled = false }
[analyzer]
plugins = []
find-unused-definitions = true
find-unused-expressions = true
analyze-dead-code = true
memoize-properties = true
check-throws = true
unchecked-exceptions = [
"Error",
"LogicException",
"ReflectionException",
"PHPUnit\\Framework\\Exception",
"PHPUnit\\Framework\\ExpectationFailedException",
"PHPUnit\\Framework\\UnknownClassOrInterfaceException",
]
unchecked-exception-classes = []
check-missing-override = true
find-unused-parameters = true
strict-list-index-checks = true
strict-array-index-existence = true
allow-array-truthy-operand = false
no-boolean-literal-comparison = true
check-missing-type-hints = true
register-super-globals = true
+15
View File
@@ -0,0 +1,15 @@
{
"source-providers": [
{
"in": "src as source",
"exclude": "vendor",
"name": "/\\.(php)$/"
}
],
"plugins": [
],
"analysers": [
],
"services": [
]
}
+31
View File
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
bootstrap="vendor/autoload.php"
colors="true"
displayDetailsOnTestsThatTriggerDeprecations="true"
displayDetailsOnTestsThatTriggerErrors="true"
displayDetailsOnTestsThatTriggerNotices="true"
displayDetailsOnTestsThatTriggerWarnings="true"
displayDetailsOnPhpunitDeprecations="true"
processIsolation="false"
stopOnFailure="false">
<testsuites>
<testsuite name="tc-lib-unicode Test Suite">
<directory>./test</directory>
</testsuite>
</testsuites>
<source>
<include>
<directory suffix=".php">src</directory>
</include>
</source>
<coverage>
<report>
<clover outputFile="target/coverage/coverage.xml"/>
<html outputDirectory="target/coverage" lowUpperBound="50" highLowerBound="90"/>
</report>
</coverage>
<logging>
<junit outputFile="target/logs/junit.xml"/>
</logging>
</phpunit>
+30
View File
@@ -0,0 +1,30 @@
<?php
/**
* autoload.php
*
* Autoloader for Tecnick.com libraries
*
* @since 2015-03-04
* @category Library
* @package Unicode
* @author Nicola Asuni <info@tecnick.com>
* @copyright 2015-2026 Nicola Asuni - Tecnick.com LTD
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
* @link https://github.com/tecnickcom/tc-lib-unicode
*
* This file is part of tc-lib-unicode software library.
*/
\spl_autoload_register(
function ($class) {
$prefix = 'Com\\Tecnick\\';
$len = \strlen($prefix);
if (\strncmp($prefix, $class, $len) !== 0) {
return;
}
$relative_class = \substr($class, $len);
$file = \dirname(__DIR__).'/'.\str_replace('\\', '/', $relative_class).'.php';
if (\file_exists($file)) {
require $file;
}
}
);
@@ -0,0 +1,7 @@
~#PKGNAME#~ (~#VERSION#~-~#RELEASE#~) UNRELEASED; urgency=low
* Please check the
https://github.com/~#VENDOR#~/~#PROJECT#~
commit history
-- Nicola Asuni <info@tecnick.com> ~#DATE#~
@@ -0,0 +1,17 @@
Source: ~#PKGNAME#~
Maintainer: Nicola Asuni <info@tecnick.com>
Section: php
Priority: optional
Build-Depends: debhelper-compat (= 13)
Standards-Version: 4.7.2
Rules-Requires-Root: no
Homepage: https://github.com/~#VENDOR#~/~#PROJECT#~
Vcs-Git: https://github.com/~#VENDOR#~/~#PROJECT#~.git
Vcs-Browser: https://github.com/~#VENDOR#~/~#PROJECT#~
Package: ~#PKGNAME#~
Provides: php-~#PROJECT#~
Architecture: all
Depends: php (>= 8.2.0), php-ctype, php-mbstring, php-tecnickcom-tc-lib-unicode-data (<< 3.0.0), php-tecnickcom-tc-lib-unicode-data (>= 3.0.2), ${misc:Depends}
Description: PHP Unicode methods.
PHP library containing Unicode methods.
@@ -0,0 +1,20 @@
Format: https://www.debian.org/doc/packaging-manuals/copyright-format/1.0/
Upstream-Name: ~#PROJECT#~
Source: https://github.com/~#VENDOR#~/~#PROJECT#~
Files: *
Copyright: Copyright 2001-2026 Nicola Asuni <info@tecnick.com>
License: LGPL-3
License: LGPL-3
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/> or
/usr/share/common-licenses/LGPL-3
@@ -0,0 +1,3 @@
#!/usr/bin/make -f
%:
dh $@
@@ -0,0 +1 @@
3.0 (quilt)
@@ -0,0 +1,4 @@
version=4
opts=filenamemangle=s/.+\/v?(\d[\d\.]+)\.tar\.gz/~#PKGNAME#~_$1.orig.tar.gz/ \
https://github.com/~#VENDOR#~/~#PROJECT#~/tags \
.*/archive/refs/tags/v?(\d[\d\.]*)\.tar\.gz
+45
View File
@@ -0,0 +1,45 @@
# SPEC file
%global c_vendor %{_vendor}
%global gh_owner %{_owner}
%global gh_project %{_project}
Name: %{_package}
Version: %{_version}
Release: %{_release}%{?dist}
Summary: PHP library containing Unicode methods
License: LGPLv3+
URL: https://github.com/%{gh_owner}/%{gh_project}
BuildArch: noarch
Requires: php(language) >= 8.2.0
Requires: php-composer(%{c_vendor}/tc-lib-unicode-data) < 3.0.0
Requires: php-composer(%{c_vendor}/tc-lib-unicode-data) >= 3.0.2
Requires: php-ctype
Requires: php-mbstring
Requires: php-pcre
Provides: php-composer(%{c_vendor}/%{gh_project}) = %{version}
Provides: php-%{gh_project} = %{version}
%description
PHP library containing Unicode methods
%build
#(cd %{_current_directory} && make build)
%install
rm -rf "%{buildroot}"
(cd "%{_current_directory}" && make install DESTDIR="%{buildroot}")
%files
%attr(-,root,root) %{_libpath}
%attr(-,root,root) %{_docpath}
%docdir %{_docpath}
# Optional config files can be listed here when used by a project.
%changelog
* Tue Jul 21 2026 Nicola Asuni <info@tecnick.com> 1.0.0-1
- Initial Commit
+419
View File
@@ -0,0 +1,419 @@
<?php
declare(strict_types=1);
/**
* Bidi.php
*
* @since 2011-05-23
* @category Library
* @package Unicode
* @author Nicola Asuni <info@tecnick.com>
* @copyright 2011-2026 Nicola Asuni - Tecnick.com LTD
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
* @link https://github.com/tecnickcom/tc-lib-unicode
*
* This file is part of tc-lib-unicode software library.
*/
namespace Com\Tecnick\Unicode;
use Com\Tecnick\Unicode\Bidi\Shaping;
use Com\Tecnick\Unicode\Bidi\StepI;
use Com\Tecnick\Unicode\Bidi\StepL;
use Com\Tecnick\Unicode\Bidi\StepN;
use Com\Tecnick\Unicode\Bidi\StepP;
use Com\Tecnick\Unicode\Bidi\StepW;
use Com\Tecnick\Unicode\Bidi\StepX;
use Com\Tecnick\Unicode\Bidi\StepXten;
use Com\Tecnick\Unicode\Data\Constant as UniConstant;
use Com\Tecnick\Unicode\Data\Type as UniType;
use Com\Tecnick\Unicode\Exception as UnicodeException;
/**
* Com\Tecnick\Unicode\Bidi
*
* @since 2015-07-13
* @category Library
* @package Unicode
* @author Nicola Asuni <info@tecnick.com>
* @copyright 2011-2026 Nicola Asuni - Tecnick.com LTD
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
* @link https://github.com/tecnickcom/tc-lib-unicode
*/
class Bidi
{
/**
* The input contains strong right-to-left characters (bidi class R, AL or AN)
*/
public const CONTAINS_RTL = 1;
/**
* The input contains Arabic characters (bidi class AL or AN)
*/
public const CONTAINS_ARABIC = 2;
/**
* The input contains explicit formatting characters
* (LRE, RLE, PDF, LRO, RLO, LRI, RLI, FSI, PDI)
*/
public const CONTAINS_FORMATTING = 4;
/**
* String to process
*/
protected string $str = '';
/**
* Array of UTF-8 chars
*
* @var array<string>
*/
protected array $chrarr = [];
/**
* Array of UTF-8 codepoints
*
* @var array<int>
*/
protected array $ordarr = [];
/**
* Processed string (null until it is built)
*/
protected ?string $bidistr = null;
/**
* Array of processed UTF-8 chars (null until it is built)
*
* @var ?array<string>
*/
protected ?array $bidichrarr = null;
/**
* Array of processed UTF-8 codepoints
*
* @var array<int>
*/
protected array $bidiordarr = [];
/**
* If 'R' forces RTL, if 'L' forces LTR
*/
protected string $forcedir = '';
/**
* If true enable shaping
*/
protected bool $shaping = true;
/**
* Content flags of the input, as a combination of the CONTAINS_* constants
*/
protected int $content = 0;
/**
* Convert object
*/
protected Convert $conv;
/**
* Reverse the RTL substrings using the Bidirectional Algorithm
* http://unicode.org/reports/tr9/
*
* @param ?string $str String to convert (if null it will be generated from $chrarr or $ordarr)
* @param ?array<string> $chrarr Array of UTF-8 chars (if empty it will be generated from $str or $ordarr)
* @param ?array<int> $ordarr Array of UTF-8 codepoints (if empty it will be generated from $str or $chrarr)
* @param string|TextDirection $forcedir If 'R' forces RTL, if 'L' forces LTR ('' auto), or a TextDirection case
* @param bool $shaping If true enable the shaping algorithm
*
* @throws UnicodeException
*/
public function __construct(
?string $str = null,
?array $chrarr = null,
?array $ordarr = null,
string|TextDirection $forcedir = '',
bool $shaping = true,
) {
if ($str === null && ($chrarr === null || $chrarr === []) && ($ordarr === null || $ordarr === [])) {
throw new UnicodeException('empty input');
}
$this->conv = new Convert();
$this->setInput($str, $chrarr, $ordarr, $forcedir);
$this->scanInput();
// The explicit formatting characters are removed by X9, so an input that contains
// them is processed even when it holds no right-to-left character.
if (!$this->isRtlMode() && !$this->hasFormatting()) {
$this->bidistr = $this->str;
$this->bidichrarr = $this->chrarr;
$this->bidiordarr = $this->ordarr;
return;
}
$this->shaping = $shaping && $this->isArabic();
$this->process();
}
/**
* Set Input data
*
* @param ?string $str String to convert (if null it will be generated from $chrarr or $ordarr)
* @param ?array<string> $chrarr Array of UTF-8 chars (if empty it will be generated from $str or $ordarr)
* @param ?array<int> $ordarr Array of UTF-8 codepoints (if empty it will be generated from $str or $chrarr)
* @param string|TextDirection $forcedir If 'R' forces RTL, if 'L' forces LTR ('' auto), or a TextDirection case
*
* @throws UnicodeException
* @SuppressWarnings("PHPMD.CyclomaticComplexity")
*/
protected function setInput(
?string $str = null,
?array $chrarr = null,
?array $ordarr = null,
string|TextDirection $forcedir = '',
): void {
if ($str === null) {
$str = '';
if (($chrarr === null || $chrarr === []) && ($ordarr !== null && $ordarr !== [])) {
$chrarr = $this->conv->ordArrToChrArr($ordarr);
}
if ($chrarr !== null && $chrarr !== []) {
$str = \implode('', $chrarr);
}
}
if ($chrarr === null || $chrarr === []) {
$chrarr = $this->conv->strToChrArr($str);
}
if ($ordarr === null || $ordarr === []) {
$ordarr = $this->conv->chrArrToOrdArr($chrarr);
}
if (\count($chrarr) !== \count($ordarr)) {
throw new UnicodeException('the input forms contain a different number of characters');
}
$this->str = $str;
$this->chrarr = $chrarr;
$this->ordarr = $ordarr;
$this->forcedir = TextDirection::fromLoose($forcedir)->value;
}
/**
* Classify the input once into the content flags used to decide whether the
* bidirectional algorithm and the shaping have to run.
*/
protected function scanInput(): void
{
foreach ($this->ordarr as $ord) {
if (
$ord >= UniConstant::LRE && $ord <= UniConstant::RLO
|| $ord >= UniConstant::LRI && $ord <= UniConstant::PDI
) {
$this->content |= self::CONTAINS_FORMATTING;
continue;
}
$type = UniType::getType($ord);
if ($type === 'AL' || $type === 'AN') {
$this->content |= self::CONTAINS_ARABIC | self::CONTAINS_RTL;
continue;
}
if ($type === 'R') {
$this->content |= self::CONTAINS_RTL;
}
}
}
/**
* Returns the processed array of UTF-8 codepoints
*
* @return array<int>
*/
public function getOrdArray(): array
{
return $this->bidiordarr;
}
/**
* Returns the processed array of UTF-8 chars
*
* @return array<string>
*
* @throws UnicodeException
*/
public function getChrArray(): array
{
if ($this->bidichrarr === null) {
$this->bidichrarr = $this->conv->ordArrToChrArr($this->bidiordarr);
}
return $this->bidichrarr;
}
/**
* Returns the number of characters in the processed string
*
* @throws UnicodeException
*/
public function getNumChars(): int
{
return \count($this->getChrArray());
}
/**
* Returns the processed string
*
* @throws UnicodeException
*/
public function getString(): string
{
if ($this->bidistr === null) {
$this->bidistr = \implode('', $this->getChrArray());
}
return $this->bidistr;
}
/**
* Returns an array with processed chars as keys
*
* @return array<int, true>
*/
public function getCharKeys(): array
{
return \array_fill_keys($this->bidiordarr, true);
}
/**
* P1. Split the text into separate paragraphs.
* A paragraph separator is kept with the previous paragraph.
*
* @return array<int, array<int, int>>
*/
protected function getParagraphs(): array
{
$paragraph = [
0 => [],
];
$pdx = 0; // paragraphs index
foreach ($this->ordarr as $ord) {
$paragraph[$pdx][] = $ord;
if (UniType::getType($ord) === 'B') {
++$pdx;
$paragraph[$pdx] = [];
}
}
return $paragraph;
}
/**
* Process the string
*
* @SuppressWarnings("PHPMD.CyclomaticComplexity")
*/
protected function process(): void
{
// split the text into separate paragraphs.
$paragraph = $this->getParagraphs();
// Within each paragraph, apply all the other rules of this algorithm.
foreach ($paragraph as $par) {
// A trailing paragraph separator produces an empty final paragraph; it contributes
// nothing and would otherwise run through the full Step* pipeline for no result.
if ($par === []) {
continue;
}
$pel = $this->getPel($par);
$stepx = new StepX($par, $pel);
$stepx10 = new StepXten($stepx->getChrData(), $pel);
$ilrs = $stepx10->getIsolatedLevelRunSequences();
$chardata = [];
$maxlevel = 0;
foreach ($ilrs as $ilr) {
$stepw = new StepW($ilr);
$stepn = new StepN($stepw->getSequence());
$stepi = new StepI($stepn->getSequence());
$ilr = $stepi->getSequence();
if ($this->shaping) {
$shaping = new Shaping($ilr, $par);
$ilr = $shaping->getSequence();
}
\array_push($chardata, ...$ilr['item']);
if ($ilr['maxlevel'] > $maxlevel) {
$maxlevel = $ilr['maxlevel'];
}
}
$stepl = new StepL($chardata, $pel, $maxlevel);
$chardata = $stepl->getChrData();
foreach ($chardata as $chardatum) {
$this->bidiordarr[] = $chardatum['char'];
}
// add back the paragraph separators ($par is non-empty, so end() returns a codepoint)
$lastchar = \end($par);
if ($lastchar < 0) {
continue;
}
if (UniType::getType($lastchar) !== 'B') {
continue;
}
$this->bidiordarr[] = $lastchar;
}
}
/**
* Get the paragraph embedding level
*
* @param array<int> $par Paragraph
*/
protected function getPel(array $par): int
{
if ($this->forcedir === 'R') {
return 1;
}
if ($this->forcedir === 'L') {
return 0;
}
$stepp = new StepP($par);
return $stepp->getPel();
}
/**
* Check if the input contains Arabic characters
*/
protected function isArabic(): bool
{
return ($this->content & self::CONTAINS_ARABIC) !== 0;
}
/**
* Check if the input contains explicit formatting characters
*/
protected function hasFormatting(): bool
{
return ($this->content & self::CONTAINS_FORMATTING) !== 0;
}
/**
* Check if the input contains right-to-left characters to process
*/
protected function isRtlMode(): bool
{
return $this->forcedir === 'R' || ($this->content & self::CONTAINS_RTL) !== 0;
}
}
+264
View File
@@ -0,0 +1,264 @@
<?php
declare(strict_types=1);
/**
* Shaping.php
*
* @since 2011-05-23
* @category Library
* @package Unicode
* @author Nicola Asuni <info@tecnick.com>
* @copyright 2011-2026 Nicola Asuni - Tecnick.com LTD
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
* @link https://github.com/tecnickcom/tc-lib-unicode
*
* This file is part of tc-lib-unicode software library.
*/
namespace Com\Tecnick\Unicode\Bidi;
use Com\Tecnick\Unicode\Data\Arabic as UniArabic;
/**
* Com\Tecnick\Unicode\Bidi\Shaping
*
* @since 2015-07-13
* @category Library
* @package Unicode
* @author Nicola Asuni <info@tecnick.com>
* @copyright 2011-2026 Nicola Asuni - Tecnick.com LTD
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
* @link https://github.com/tecnickcom/tc-lib-unicode
*
* @phpstan-import-type SeqData from \Com\Tecnick\Unicode\Bidi\Shaping\Arabic
* @phpstan-import-type CharData from \Com\Tecnick\Unicode\Bidi\Shaping\Arabic
*/
class Shaping extends \Com\Tecnick\Unicode\Bidi\Shaping\Arabic
{
/**
* Unicode code for ARABIC LETTER ALEF (U+0627)
*/
protected const ALEF = 0x0627;
/**
* Shaping
* Cursively connected scripts, such as Arabic or Syriac,
* require the selection of positional character shapes that depend on adjacent characters.
* Shaping is logically applied after the Bidirectional Algorithm is used and is limited to
* characters within the same directional run.
*
* @param SeqData $seq Isolated run sequence array
* @param array<int, int> $paragraph Codepoints of the paragraph, in logical order
*/
public function __construct(array $seq, array $paragraph = [])
{
$this->seq = $seq;
$this->newchardata = $seq['item'];
$this->paragraph = $paragraph;
$this->process();
}
/**
* Returns the processed sequence
*
* @return SeqData
*/
public function getSequence(): array
{
return $this->seq;
}
/**
* Process
*/
protected function process(): void
{
$this->setJoiningContext();
foreach ($this->seq['item'] as $idx => $item) {
$this->shapeChar($idx, $item);
}
$this->combineShadda();
$this->removeDeletedChars();
$this->seq['item'] = \array_values($this->newchardata);
// Keep 'length' consistent with 'item': removeDeletedChars() may have dropped entries.
$this->seq['length'] = \count($this->seq['item']);
$this->newchardata = []; // reset
}
/**
* Replace a character with its presentation form or with the ligature it belongs to.
*
* @param int $idx Index of the sequence item
* @param CharData $item Sequence item
*/
protected function shapeChar(int $idx, array $item): void
{
$char = $item['char'];
$pos = $item['pos'];
if ($this->setAllahLigature($idx, $char, $pos)) {
return;
}
if ($this->setLamAlefLigature($idx, $char, $pos)) {
return;
}
$forms = UniArabic::SUBSTITUTE[$char] ?? null;
if ($forms === null) {
return;
}
$shaped = $forms[$this->getForm($pos)] ?? null;
if ($shaped !== null) {
$this->setNewChar($idx, $shaped);
}
}
/**
* Replace an alef preceded by a lam with the corresponding lam-alef ligature and
* delete the lam. The ligature takes the final form when the lam connects to the
* character before it.
*
* @param int $idx Index of the sequence item of the alef
* @param int $char Codepoint of the alef
* @param int $pos Paragraph position of the alef
*/
protected function setLamAlefLigature(int $idx, int $char, int $pos): bool
{
if (!\array_key_exists($char, UniArabic::LAA)) {
return false;
}
$lam = $this->getPrevPosition($pos);
if ($lam === null || $this->getParagraphChar($lam) !== UniArabic::LAM) {
return false;
}
$lamIdx = $this->seqindex[$lam] ?? null;
if ($lamIdx === null) {
return false;
}
$form = $this->joinsPrev($lam) ? self::FORM_FINAL : self::FORM_ISOLATED;
$ligature = UniArabic::LAA[$char][$form] ?? null;
if ($ligature === null) {
return false;
}
$this->setNewChar($lamIdx, -1);
$this->setNewChar($idx, $ligature);
return true;
}
/**
* Replace the word alef + lam + lam + heh with the ligature U+FDF2, whose
* compatibility decomposition covers the four characters; combining marks between
* them are kept. The ligature has an isolated form only, so it is used just when the
* alef does not connect to the character before it and the heh does not connect to
* the one after it.
*
* @param int $idx Index of the sequence item of the heh
* @param int $char Codepoint of the heh
* @param int $pos Paragraph position of the heh
*/
protected function setAllahLigature(int $idx, int $char, int $pos): bool
{
if ($char !== UniArabic::HEH || $this->joinsNext($pos)) {
return false;
}
$second = $this->getPrevPosition($pos);
if ($second === null || $this->getParagraphChar($second) !== UniArabic::LAM) {
return false;
}
$first = $this->getPrevPosition($second);
if ($first === null || $this->getParagraphChar($first) !== UniArabic::LAM) {
return false;
}
$alef = $this->getPrevPosition($first);
if ($alef === null || $this->getParagraphChar($alef) !== self::ALEF || $this->joinsPrev($alef)) {
return false;
}
$deleted = [];
foreach ([$alef, $first, $second] as $position) {
$deletedIdx = $this->seqindex[$position] ?? null;
if ($deletedIdx === null) {
// The word is split between two runs: shape the characters individually.
return false;
}
$deleted[] = $deletedIdx;
}
foreach ($deleted as $deletedIdx) {
$this->setNewChar($deletedIdx, -1);
}
$this->setNewChar($idx, UniArabic::LIGATURE_ALLAH_ISOLATED_FORM);
return true;
}
/**
* Combine characters that can occur with Arabic Shadda (U+0651).
* Putting the combining mark and shadda in the same glyph allows
* to avoid the two marks overlapping each other in an illegible manner.
* Both orders are combined: the canonical combining class of shadda (33) is higher
* than the one of the vowels it merges with, so normalized text has the vowel first.
*/
protected function combineShadda(): void
{
$last = $this->seq['length'] - 1;
for ($idx = 0; $idx < $last; ++$idx) {
$currentItem = $this->newchardata[$idx] ?? null;
$nextItem = $this->newchardata[$idx + 1] ?? null;
assert(
$currentItem !== null && $nextItem !== null,
'Expected adjacent chars while combining Arabic shadda',
);
$cur = $currentItem['char'];
$nxt = $nextItem['char'];
if ($cur === UniArabic::SHADDA && $nxt >= 0) {
$diacritic = UniArabic::DIACRITIC[$nxt] ?? null;
if ($diacritic !== null) {
$this->setNewChar($idx, -1);
$this->setNewChar($idx + 1, $diacritic);
}
continue;
}
if ($nxt === UniArabic::SHADDA && $cur >= 0) {
$diacritic = UniArabic::DIACRITIC[$cur] ?? null;
if ($diacritic !== null) {
$this->setNewChar($idx, $diacritic);
$this->setNewChar($idx + 1, -1);
}
}
}
}
/**
* Remove marked characters
*/
protected function removeDeletedChars(): void
{
foreach ($this->newchardata as $key => $value) {
if ($value['char'] >= 0) {
continue;
}
unset($this->newchardata[$key]);
}
}
}
@@ -0,0 +1,268 @@
<?php
declare(strict_types=1);
/**
* Arabic.php
*
* @since 2011-05-23
* @category Library
* @package Unicode
* @author Nicola Asuni <info@tecnick.com>
* @copyright 2011-2026 Nicola Asuni - Tecnick.com LTD
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
* @link https://github.com/tecnickcom/tc-lib-unicode
*
* This file is part of tc-lib-unicode software library.
*/
namespace Com\Tecnick\Unicode\Bidi\Shaping;
use Com\Tecnick\Unicode\Data\Arabic as UniArabic;
/**
* Com\Tecnick\Unicode\Bidi\Shaping\Arabic
*
* Joining context of the Arabic shaping: the form of a character is decided by the
* Joining_Type of the nearest non-transparent characters around it, as defined by
* ArabicShaping.txt and section 9.2 of the Unicode standard.
*
* @since 2015-07-13
* @category Library
* @package Unicode
* @author Nicola Asuni <info@tecnick.com>
* @copyright 2011-2026 Nicola Asuni - Tecnick.com LTD
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
* @link https://github.com/tecnickcom/tc-lib-unicode
*
* @phpstan-type CharData array{
* 'char': int,
* 'i': int,
* 'level': int,
* 'otype': string,
* 'pdimatch': int,
* 'pos': int,
* 'type': string,
* 'x': int,
* }
*
* @phpstan-type SeqData array{
* 'e': int,
* 'edir': string,
* 'end': int,
* 'eos': string,
* 'length': int,
* 'maxlevel': int,
* 'sos': string,
* 'start': int,
* 'item': array<int, CharData>,
* }
*/
abstract class Arabic
{
/**
* Index of the isolated form in the substitution rows.
*/
protected const FORM_ISOLATED = 0;
/**
* Index of the final form in the substitution rows.
*/
protected const FORM_FINAL = 1;
/**
* Index of the initial form in the substitution rows.
*/
protected const FORM_INITIAL = 2;
/**
* Index of the medial form in the substitution rows.
*/
protected const FORM_MEDIAL = 3;
/**
* Sequence to process and return
*
* @var SeqData
*/
protected array $seq = [
'e' => 0,
'edir' => '',
'end' => 0,
'eos' => '',
'length' => 0,
'maxlevel' => 0,
'sos' => '',
'start' => 0,
'item' => [],
];
/**
* Array of processed chars
*
* @var array<int, CharData>
*/
protected array $newchardata = [];
/**
* Codepoints of the paragraph the sequence belongs to, in logical order.
* The joining context is read from the paragraph and not from the sequence because
* X9 removes the joining-relevant format characters (ZWJ and ZWNJ) from the latter.
*
* @var array<int, int>
*/
protected array $paragraph = [];
/**
* Joining_Type of each paragraph position
*
* @var array<int, string>
*/
protected array $joining = [];
/**
* Index of the sequence item of each paragraph position
*
* @var array<int, int>
*/
protected array $seqindex = [];
/**
* Build the joining type of every paragraph position and the map from a paragraph
* position to the sequence item that holds it.
*/
protected function setJoiningContext(): void
{
$this->joining = [];
foreach ($this->paragraph as $pos => $ord) {
$this->joining[$pos] = UniArabic::getJoiningType($ord);
}
$this->seqindex = [];
foreach ($this->seq['item'] as $idx => $item) {
$this->seqindex[$item['pos']] = $idx;
}
}
/**
* Position of the nearest non-transparent character before the given one,
* or null at the beginning of the paragraph.
*/
protected function getPrevPosition(int $pos): ?int
{
for ($idx = $pos - 1; $idx >= 0; --$idx) {
if (($this->joining[$idx] ?? 'U') !== 'T') {
return $idx;
}
}
return null;
}
/**
* Position of the nearest non-transparent character after the given one,
* or null at the end of the paragraph.
*/
protected function getNextPosition(int $pos): ?int
{
$length = \count($this->paragraph);
for ($idx = $pos + 1; $idx < $length; ++$idx) {
if (($this->joining[$idx] ?? 'U') !== 'T') {
return $idx;
}
}
return null;
}
/**
* Returns the Joining_Type of a paragraph position (Non_Joining outside the paragraph).
*/
protected function getJoiningType(?int $pos): string
{
if ($pos === null) {
return 'U';
}
return $this->joining[$pos] ?? 'U';
}
/**
* True when a character of the given joining type connects to the character that
* follows it: Dual_Joining, Join_Causing and Left_Joining do.
*/
protected function linksForward(string $type): bool
{
return $type === 'D' || $type === 'C' || $type === 'L';
}
/**
* True when a character of the given joining type connects to the character that
* precedes it: Dual_Joining, Join_Causing and Right_Joining do.
*/
protected function linksBackward(string $type): bool
{
return $type === 'D' || $type === 'C' || $type === 'R';
}
/**
* True when the character at the given paragraph position connects to the preceding one.
*/
protected function joinsPrev(int $pos): bool
{
return (
$this->linksBackward($this->getJoiningType($pos))
&& $this->linksForward($this->getJoiningType($this->getPrevPosition($pos)))
);
}
/**
* True when the character at the given paragraph position connects to the following one.
*/
protected function joinsNext(int $pos): bool
{
return (
$this->linksForward($this->getJoiningType($pos))
&& $this->linksBackward($this->getJoiningType($this->getNextPosition($pos)))
);
}
/**
* Index of the presentation form of the character at the given paragraph position.
*/
protected function getForm(int $pos): int
{
$prev = $this->joinsPrev($pos);
$next = $this->joinsNext($pos);
return match (true) {
$prev && $next => self::FORM_MEDIAL,
$prev => self::FORM_FINAL,
$next => self::FORM_INITIAL,
default => self::FORM_ISOLATED,
};
}
/**
* Replace the character of a sequence item, or mark it for deletion with -1.
*/
protected function setNewChar(int $idx, int $char): void
{
$item = $this->newchardata[$idx] ?? null;
assert($item !== null, 'Expected shaped character at the requested index');
$item['char'] = $char;
$this->newchardata[$idx] = $item;
}
/**
* Returns the codepoint at the given paragraph position, or null outside of it.
*/
protected function getParagraphChar(?int $pos): ?int
{
if ($pos === null) {
return null;
}
return $this->paragraph[$pos] ?? null;
}
}
+84
View File
@@ -0,0 +1,84 @@
<?php
declare(strict_types=1);
/**
* StepBase.php
*
* @since 2011-05-23
* @category Library
* @package Unicode
* @author Nicola Asuni <info@tecnick.com>
* @copyright 2011-2026 Nicola Asuni - Tecnick.com LTD
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
* @link https://github.com/tecnickcom/tc-lib-unicode
*
* This file is part of tc-lib-unicode software library.
*/
namespace Com\Tecnick\Unicode\Bidi;
/**
* Com\Tecnick\Unicode\Bidi\StepBase
*
* @since 2015-07-13
* @category Library
* @package Unicode
* @author Nicola Asuni <info@tecnick.com>
* @copyright 2011-2026 Nicola Asuni - Tecnick.com LTD
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
* @link https://github.com/tecnickcom/tc-lib-unicode
*
* @phpstan-import-type SeqData from \Com\Tecnick\Unicode\Bidi\Shaping\Arabic
*/
abstract class StepBase
{
/**
* Initialize Sequence to process
*
* @param SeqData $seq Isolated Sequence array
* @param bool $process If false disable automatic processing (this is a testing flag)
*/
public function __construct(
/**
* Sequence to process and return
*/
protected array $seq,
bool $process = true,
) {
if ($process) {
$this->process();
}
}
/**
* Returns the processed array
*
* @return SeqData
*/
public function getSequence(): array
{
return $this->seq;
}
/**
* Process the current step
*/
abstract protected function process(): void;
/**
* Generic step
*
* @param callable(int): void|string $processor Processing callback or method name
*/
public function processStep(callable|string $processor): void
{
if (\is_string($processor)) {
$processor = [$this, $processor];
}
for ($idx = 0; $idx < $this->seq['length']; ++$idx) {
$processor($idx);
}
}
}
+80
View File
@@ -0,0 +1,80 @@
<?php
declare(strict_types=1);
/**
* StepI.php
*
* @since 2011-05-23
* @category Library
* @package Unicode
* @author Nicola Asuni <info@tecnick.com>
* @copyright 2011-2026 Nicola Asuni - Tecnick.com LTD
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
* @link https://github.com/tecnickcom/tc-lib-unicode
*
* This file is part of tc-lib-unicode software library.
*/
namespace Com\Tecnick\Unicode\Bidi;
/**
* Com\Tecnick\Unicode\Bidi\StepI
*
* @since 2015-07-13
* @category Library
* @package Unicode
* @author Nicola Asuni <info@tecnick.com>
* @copyright 2011-2026 Nicola Asuni - Tecnick.com LTD
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
* @link https://github.com/tecnickcom/tc-lib-unicode
*/
class StepI extends \Com\Tecnick\Unicode\Bidi\StepBase
{
/**
* Process I steps
*/
protected function process(): void
{
$this->seq['maxlevel'] = 0;
$this->processStep($this->processI(...));
}
/**
* I1. For all characters with an even (left-to-right) embedding level, those of type R go up one level and those
* of type AN or EN go up two levels.
* I2. For all characters with an odd (right-to-left) embedding level, those of type L, EN or AN go up one level.
*
* @param int $idx Current character position
*/
protected function processI(int $idx): void
{
$item = $this->seq['item'][$idx] ?? null;
assert($item !== null, 'Expected StepI sequence item at current index');
$odd = $item['level'] % 2;
if ($odd !== 0) {
if ($item['type'] === 'L' || $item['type'] === 'EN' || $item['type'] === 'AN') {
++$item['level'];
}
$this->seq['item'][$idx] = $item;
$this->seq['maxlevel'] = (int) \max($this->seq['maxlevel'], $item['level']);
return;
}
if ($item['type'] === 'R') {
++$item['level'];
}
if ($item['type'] === 'AN' || $item['type'] === 'EN') {
$item['level'] += 2;
}
$this->seq['item'][$idx] = $item;
// update the maximum level
$this->seq['maxlevel'] = (int) \max($this->seq['maxlevel'], $item['level']);
}
}
+217
View File
@@ -0,0 +1,217 @@
<?php
declare(strict_types=1);
/**
* StepL.php
*
* @since 2011-05-23
* @category Library
* @package Unicode
* @author Nicola Asuni <info@tecnick.com>
* @copyright 2011-2026 Nicola Asuni - Tecnick.com LTD
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
* @link https://github.com/tecnickcom/tc-lib-unicode
*
* This file is part of tc-lib-unicode software library.
*/
namespace Com\Tecnick\Unicode\Bidi;
use Com\Tecnick\Unicode\Data\Constant as UniConstant;
use Com\Tecnick\Unicode\Data\Mirror as UniMirror;
/**
* Com\Tecnick\Unicode\Bidi\StepL
*
* @since 2015-07-13
* @category Library
* @package Unicode
* @author Nicola Asuni <info@tecnick.com>
* @copyright 2011-2026 Nicola Asuni - Tecnick.com LTD
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
* @link https://github.com/tecnickcom/tc-lib-unicode
*
* @phpstan-import-type CharData from \Com\Tecnick\Unicode\Bidi\Shaping\Arabic
*/
class StepL
{
/**
* Array of characters data to return
*
* @var array<int, CharData>
*/
protected array $chardata = [];
/**
* Number of characters in $this->chardata
*/
protected int $numchars;
/**
* L steps
*
* @param array<int, CharData> $chardata Array of characters data
* @param int $pel Paragraph embedding level
* @param int $maxlevel Maximum level
*/
public function __construct(
array $chardata,
/**
* Paragraph embedding level
*/
protected int $pel,
/**
* Maximum level
*/
protected int $maxlevel,
) {
// reorder chars by their original position
\usort($chardata, static fn($apos, $bpos): int => $apos['pos'] - $bpos['pos']);
$this->chardata = $chardata;
$this->numchars = \count($this->chardata);
$this->processL1();
$this->processL2();
$this->processL4();
}
/**
* Returns the processed array
*
* @return array<int, CharData>
*/
public function getChrData(): array
{
return $this->chardata;
}
/**
* Reset the embedding level of the character at the given index to the paragraph embedding level.
*
* @param int $idx Character index
*/
private function resetLevel(int $idx): void
{
$item = $this->chardata[$idx] ?? null;
assert($item !== null, 'Expected StepL character data at the index to reset');
$item['level'] = $this->pel;
$this->chardata[$idx] = $item;
}
/**
* Returns true when the codepoint is an isolate formatting character (FSI, LRI, RLI, or PDI).
*
* @param int $char Codepoint
*/
private function isIsolateFormat(int $char): bool
{
return $char >= UniConstant::LRI && $char <= UniConstant::PDI;
}
/**
* L1. On each line, reset the embedding level of the following characters to the paragraph embedding level:
* 1. Segment separators,
* 2. Paragraph separators,
* 3. Any sequence of whitespace characters and/or isolate formatting characters (FSI, LRI, RLI, and PDI)
* preceding a segment separator or paragraph separator, and
* 4. Any sequence of whitespace characters and/or isolate formatting characters (FSI, LRI, RLI, and PDI)
* at the end of the line.
*
* This rule is applied using the original character types, not the resolved ones.
*/
protected function processL1(): void
{
// Indexes of the current pending run of whitespace and/or isolate formatting characters.
$pending = [];
for ($idx = 0; $idx < $this->numchars; ++$idx) {
$item = $this->chardata[$idx] ?? null;
assert($item !== null, 'Expected StepL character data at the current index');
// L1.1 / L1.2: reset segment and paragraph separators and, with them (L1.3),
// any immediately preceding run of whitespace / isolate formatting characters.
if ($item['otype'] === 'S' || $item['otype'] === 'B') {
foreach ($pending as $pidx) {
$this->resetLevel($pidx);
}
$this->resetLevel($idx);
$pending = [];
continue;
}
// Accumulate whitespace and isolate formatting characters as a candidate run.
if ($item['otype'] === 'WS' || $this->isIsolateFormat($item['char'])) {
$pending[] = $idx;
continue;
}
// Any other character breaks (and discards) the pending run.
$pending = [];
}
// L1.4: reset a run of whitespace / isolate formatting characters at the end of the line.
foreach ($pending as $pidx) {
$this->resetLevel($pidx);
}
}
/**
* L2. From the highest level found in the text to the lowest odd level on each line,
* including intermediate levels not actually present in the text,
* reverse any contiguous sequence of characters that are at that level or higher.
* This rule reverses a progressively larger series of substrings.
*/
protected function processL2(): void
{
for ($level = $this->maxlevel; $level > 0; --$level) {
$ordered = [];
$reversed = [];
foreach ($this->chardata as $chardatum) {
if ($chardatum['level'] >= $level) {
$reversed[] = $chardatum;
continue;
}
if ($reversed !== []) {
$ordered = \array_merge($ordered, \array_reverse($reversed));
$reversed = [];
}
$ordered[] = $chardatum;
}
if ($reversed !== []) {
$ordered = \array_merge($ordered, \array_reverse($reversed));
}
$this->chardata = $ordered;
}
}
/**
* L4. A character is depicted by a mirrored glyph if and only if
* (a) the resolved directionality of that character is R, and
* (b) the Bidi_Mirrored property value of that character is true.
*
* The resolved directionality is R exactly when the embedding level is odd, which also
* covers the neutral mirrored characters (brackets, guillemets) resolved into a
* right-to-left run. Each character is mirrored once, after the reordering of L2.
*/
protected function processL4(): void
{
foreach ($this->chardata as $idx => $chardatum) {
if (($chardatum['level'] % 2) !== 1) {
continue;
}
$mirror = UniMirror::UNI[$chardatum['char']] ?? null;
if ($mirror === null) {
continue;
}
$chardatum['char'] = $mirror;
$this->chardata[$idx] = $chardatum;
}
}
}
+409
View File
@@ -0,0 +1,409 @@
<?php
declare(strict_types=1);
/**
* StepN.php
*
* @since 2011-05-23
* @category Library
* @package Unicode
* @author Nicola Asuni <info@tecnick.com>
* @copyright 2011-2026 Nicola Asuni - Tecnick.com LTD
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
* @link https://github.com/tecnickcom/tc-lib-unicode
*
* This file is part of tc-lib-unicode software library.
*/
namespace Com\Tecnick\Unicode\Bidi;
use Com\Tecnick\Unicode\Data\Bracket as UniBracket;
use Com\Tecnick\Unicode\Data\Type as UniType;
/**
* Com\Tecnick\Unicode\Bidi\StepN
*
* @since 2015-07-13
* @category Library
* @package Unicode
* @author Nicola Asuni <info@tecnick.com>
* @copyright 2011-2026 Nicola Asuni - Tecnick.com LTD
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
* @link https://github.com/tecnickcom/tc-lib-unicode
*/
class StepN extends \Com\Tecnick\Unicode\Bidi\StepBase
{
/**
* List or bracket pairs positions
*
* @var array<int, int>
*/
protected array $brackets = [];
/**
* Maximum number of entries of the BD16 bracket stack
*/
public const MAX_PAIRING_DEPTH = 63;
/**
* Stack used to store bracket positions
*
* @var array<int, array{int, int}>
*/
protected array $bstack = [];
/**
* True when BD16 has been stopped for the remainder of the isolating run sequence
* because the bracket stack overflowed.
*/
protected bool $bstackFull = false;
/**
* Neutral or Isolate formatting types per UAX #9: B, S, WS, ON, FSI, LRI, RLI and PDI.
* 'NI' is the type StepX gives to a character inside a neutral directional override.
*
* @var array<string, string>
*/
protected const ISOLATE_FORMATTING = [
'FSI' => 'FSI',
'LRI' => 'LRI',
'RLI' => 'RLI',
'PDI' => 'PDI',
'NI' => 'NI',
];
/**
* Returns true when the character at the given index has a Neutral or Isolate formatting
* (NI) type per UAX #9, i.e. it is subject to the N rules.
*/
protected function isNI(int $idx): bool
{
$type = $this->getItem($idx)['type'];
return isset(self::ISOLATE_FORMATTING[$type]) || isset(UniType::NEUTRAL[$type]);
}
/**
* @return array{char: int, i: int, level: int, otype: string, pdimatch: int, pos: int, type: string, x: int}
*/
private function getItem(int $idx): array
{
$item = $this->seq['item'][$idx] ?? null;
assert($item !== null, 'Expected StepN sequence item at the requested index');
return $item;
}
private function setItemType(int $idx, string $type): void
{
$item = $this->getItem($idx);
$item['type'] = $type;
$this->seq['item'][$idx] = $item;
}
/**
* Process N steps
* Resolving Neutral and Isolate Formatting Types
*
* Neutral and isolate formatting (i.e. NI) characters are resolved one isolating run sequence at a time.
* Its results are that all NIs become either R or L. Generally, NIs take on the direction of the surrounding text.
* In case of a conflict, they take on the embedding direction.
* At isolating run sequence boundaries where the type of the character on the other side of the boundary
* is required, the type assigned to sos or eos is used.
*
* Bracket pairs within an isolating run sequence are processed as units so that both the opening and the closing
* paired bracket in a pair resolve to the same direction. Note that this rule is applied based on the current
* bidirectional character type of each paired bracket and not the original type, as this could have changed under
* X6. The current bidirectional character type may also have changed under a previous iteration of the for loop in
* N0 in the case of nested bracket pairs.
*/
protected function process(): void
{
$this->processStep($this->getBracketPairs(...));
$this->processN0();
$this->processStep($this->processN1(...));
$this->processStep($this->processN2(...));
}
/**
* BD16. Find all bracket pairs
*/
protected function getBracketPairs(int $idx): void
{
if ($this->bstackFull) {
return;
}
$item = $this->getItem($idx);
// BD14 and BD15: only a bracket whose current bidirectional type is ON can be part
// of a pair, so a bracket retyped by an override (X6) does not pair.
if ($item['type'] !== 'ON') {
return;
}
$char = $item['char'];
if (array_key_exists($char, UniBracket::OPEN)) {
// process open bracket
if ($char === 0x3008) {
$char = 0x2329;
}
// BD16 uses a fixed 63 element stack: when it overflows, stop processing BD16 for
// the remainder of the isolating run sequence and keep the pairs found so far.
if (\count($this->bstack) >= self::MAX_PAIRING_DEPTH) {
$this->bstackFull = true;
return;
}
$this->bstack[] = [$idx, (int) $char];
return;
}
if (array_key_exists($char, UniBracket::CLOSE)) {
// process closing bracket
if ($char === 0x3009) {
$char = 0x232A;
}
// Find the matching opening bracket: scan the stack from the top and stop at the
// first (nearest) match, popping through and including the matched entry. Per BD16
// a closing bracket pairs with the nearest opener; without stopping here, multiple
// openers of the same type would be paired to the same closer and the stack emptied.
$tmpstack = $this->bstack;
while ($tmpstack !== []) {
$item = \array_pop($tmpstack);
$openBracket = UniBracket::OPEN[$item[1]] ?? null;
if ($openBracket !== null && $char === $openBracket) {
$this->brackets[$item[0]] = $idx;
$this->bstack = $tmpstack;
break;
}
}
}
}
/**
* Return the normalized char type for the N0 step
* Within this scope, bidirectional types EN and AN are treated as R.
*
* @param string $type Char type
*/
protected function getN0Type(string $type): string
{
return $type === 'AN' || $type === 'EN' ? 'R' : $type;
}
/**
* N0. Process bracket pairs in an isolating run sequence sequentially in the logical order of the text positions
* of the opening paired brackets.
*/
protected function processN0(): void
{
// Sort the list of bracket pairs in ascending order based on the text position
// of the opening paired bracket (done once, after all pairs have been collected).
\ksort($this->brackets);
$odir = $this->seq['edir'] === 'L' ? 'R' : 'L';
// For each bracket-pair element in the list of pairs of text positions
foreach ($this->brackets as $open => $close) {
if (!$this->processInsideBrackets($open, $close, $odir)) {
continue;
}
for ($jdx = $open - 1; $jdx >= 0; --$jdx) {
$btype = $this->getN0Type($this->getItem($jdx)['type']);
if ($btype === $odir) {
// 1. If the preceding strong type is also opposite the embedding direction,
// context is established, so set the type for both brackets in the pair to that direction.
$this->setBracketsType($open, $close, $odir);
break;
}
if ($btype === $this->seq['edir']) {
// 2. Otherwise set the type for both brackets in the pair to the embedding direction.
$this->setBracketsType($open, $close, $this->seq['edir']);
break;
}
}
if ($jdx < 0) {
$this->setBracketsType($open, $close, $this->seq['sos']);
}
// d. Otherwise, there are no strong types within the bracket pair. Therefore, do not set the type for that
// bracket pair. Note that if the enclosed text contains no strong types the bracket pairs will both
// resolve to the same level when resolved individually using rules N1 and N2.
}
}
/**
* Inspect the bidirectional types of the characters enclosed within the bracket pair.
*
* @param int $open Open bracket entry
* @param int $close Close bracket entry
* @param string $odir Opposite direction (L or R)
*
* @return bool True if type has not been found
*/
protected function processInsideBrackets(int $open, int $close, string $odir): bool
{
$opposite = false;
// a. Inspect the bidirectional types of the characters enclosed within the bracket pair.
for ($jdx = $open + 1; $jdx < $close; ++$jdx) {
$btype = $this->getN0Type($this->getItem($jdx)['type']);
// b. If any strong type (either L or R) matching the embedding direction is found,
// set the type for both brackets in the pair to match the embedding direction.
if ($btype === $this->seq['edir']) {
$this->setBracketsType($open, $close, $this->seq['edir']);
break;
}
if ($btype === $odir) {
// c. Otherwise, if there is a strong type it must be opposite the embedding direction.
$opposite = true;
}
}
// Therefore, test for an established context with a preceding strong type by checking backwards before
// the opening paired bracket until the first strong type (L, R, or sos) is found.
return $jdx === $close && $opposite;
}
/**
* Set the brackets type
*
* @param int $open Open bracket entry
* @param int $close Close bracket entry
* @param string $type Type
*/
protected function setBracketsType(int $open, int $close, string $type): void
{
$this->setItemType($open, $type);
$this->setItemType($close, $type);
// Any number of characters that had original bidirectional character type NSM
// prior to the application of W1 that immediately follow a paired bracket which
// changed to L or R under N0 should change to match the type of their preceding bracket.
$next = $close + 1;
while ($next < $this->seq['length']) {
$item = $this->getItem($next);
if ($item['otype'] !== 'NSM') {
break;
}
$this->setItemType($next, $type);
++$next;
}
}
/**
* N1. A sequence of NIs takes the direction of the surrounding strong text if the text on both sides has the same
* direction. European and Arabic numbers act as if they were R in terms of their influence on NIs.
* The start-of-sequence (sos) and end-of-sequence (eos) types are used at isolating run sequence boundaries.
*
* @param int $idx Current character position
*/
protected function processN1(int $idx): void
{
if (!$this->isNI($idx)) {
return;
}
$bdx = $idx - 1;
$prev = $this->processN1prev($bdx);
if ($prev === '') {
return;
}
$jdx = $this->getNextN1Char($idx);
$next = $this->processN1next($jdx);
if ($next === '') {
return;
}
if ($next === $prev) {
for ($bdx = $idx; $bdx < $jdx && $bdx < $this->seq['length']; ++$bdx) {
$this->setItemType($bdx, $next);
}
}
}
/**
* Get the previous direction
*
* @param int $bdx Position of the preceding character
*
* @return string Previous direction
*/
protected function processN1prev(int &$bdx): string
{
if ($bdx < 0) {
$bdx = 0;
return $this->seq['sos'];
}
$item = $this->getItem($bdx);
if (\in_array($item['type'], ['R', 'AN', 'EN'], true)) {
return 'R';
}
if ($item['type'] === 'L') {
return 'L';
}
return '';
}
/**
* Get the next direction
*
* @param int $jdx Position of the next character
*
* @return string Next direction
*/
protected function processN1next(int &$jdx): string
{
if ($jdx >= $this->seq['length']) {
$jdx = $this->seq['length'];
return $this->seq['eos'];
}
$item = $this->getItem($jdx);
if (\in_array($item['type'], ['R', 'AN', 'EN'], true)) {
return 'R';
}
if ($item['type'] === 'L') {
return 'L';
}
return '';
}
/**
* Return the index of the next valid char for N1
*
* @param int $idx Start index
*/
protected function getNextN1Char(int $idx): int
{
$jdx = $idx + 1;
while ($jdx < $this->seq['length'] && $this->isNI($jdx)) {
++$jdx;
}
return $jdx;
}
/**
* N2. Any remaining NIs take the embedding direction.
*
* @param int $idx Current character position
*/
protected function processN2(int $idx): void
{
if ($this->isNI($idx)) {
$this->setItemType($idx, $this->seq['edir']);
}
}
}
+94
View File
@@ -0,0 +1,94 @@
<?php
declare(strict_types=1);
/**
* StepP.php
*
* @since 2011-05-23
* @category Library
* @package Unicode
* @author Nicola Asuni <info@tecnick.com>
* @copyright 2011-2026 Nicola Asuni - Tecnick.com LTD
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
* @link https://github.com/tecnickcom/tc-lib-unicode
*
* This file is part of tc-lib-unicode software library.
*/
namespace Com\Tecnick\Unicode\Bidi;
use Com\Tecnick\Unicode\Data\Constant as UniConstant;
use Com\Tecnick\Unicode\Data\Type as UniType;
/**
* Com\Tecnick\Unicode\Bidi\StepP
*
* @since 2015-07-13
* @category Library
* @package Unicode
* @author Nicola Asuni <info@tecnick.com>
* @copyright 2011-2026 Nicola Asuni - Tecnick.com LTD
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
* @link https://github.com/tecnickcom/tc-lib-unicode
*/
class StepP
{
/**
* P Steps for Bidirectional algorithm
*
* @param array<int> $ordarr Array of UTF-8 codepoints
*/
public function __construct(
/**
* Array of UTF-8 codepoints
*/
protected array $ordarr,
) {}
/**
* Get the Paragraph Embedding Level
*/
public function getPel(): int
{
// P2. In each paragraph, find the first character of type L, AL, or R
// while skipping over any characters between an isolate initiator and its matching PDI or,
// if it has no matching PDI, the end of the paragraph.
// P3. If a character is found in P2 and it is of type AL or R,
// then set the paragraph embedding level to one; otherwise, set it to zero.
$isolate = 0;
foreach ($this->ordarr as $ord) {
$isolate = $this->getIsolateLevel($ord, $isolate);
if ($isolate !== 0) {
continue;
}
$type = UniType::getType($ord);
if ($type === 'L') {
return 0;
}
if ($type === 'R' || $type === 'AL') {
return 1;
}
}
return 0;
}
/**
* Update the level of explicit directional isolates
*/
protected function getIsolateLevel(int $ord, int $isolate): int
{
if ($ord === UniConstant::LRI || $ord === UniConstant::RLI || $ord === UniConstant::FSI) {
++$isolate;
}
if ($ord === UniConstant::PDI) {
--$isolate;
}
return \max(0, $isolate);
}
}
+270
View File
@@ -0,0 +1,270 @@
<?php
declare(strict_types=1);
/**
* StepW.php
*
* @since 2011-05-23
* @category Library
* @package Unicode
* @author Nicola Asuni <info@tecnick.com>
* @copyright 2011-2026 Nicola Asuni - Tecnick.com LTD
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
* @link https://github.com/tecnickcom/tc-lib-unicode
*
* This file is part of tc-lib-unicode software library.
*/
namespace Com\Tecnick\Unicode\Bidi;
use Com\Tecnick\Unicode\Data\Constant as UniConstant;
/**
* Com\Tecnick\Unicode\Bidi\StepW
*
* @since 2015-07-13
* @category Library
* @package Unicode
* @author Nicola Asuni <info@tecnick.com>
* @copyright 2011-2026 Nicola Asuni - Tecnick.com LTD
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
* @link https://github.com/tecnickcom/tc-lib-unicode
*/
class StepW extends \Com\Tecnick\Unicode\Bidi\StepBase
{
/**
* Returns the sequence item at the given index.
*
* @return array{char: int, i: int, level: int, otype: string, pdimatch: int, pos: int, type: string, x: int}
*/
private function getItem(int $idx): array
{
$item = $this->seq['item'][$idx] ?? null;
assert($item !== null, 'Expected StepW sequence item at the requested index');
return $item;
}
private function setItemType(int $idx, string $type): void
{
$item = $this->getItem($idx);
$item['type'] = $type;
$this->seq['item'][$idx] = $item;
}
/**
* Process W steps
* Resolving Weak Types
*/
protected function process(): void
{
$this->processStep($this->processW1(...));
$this->processStep($this->processW2(...));
$this->processStep($this->processW3(...));
$this->processStep($this->processW4(...));
$this->processStep($this->processW5(...));
$this->processStep($this->processW6(...));
$this->processStep($this->processW7(...));
}
/**
* W1. Examine each nonspacing mark (NSM) in the isolating run sequence, and
* change the type of the NSM to Other Neutral if the previous character is an isolate initiator or PDI, and
* to the type of the previous character otherwise.
* If the NSM is at the start of the isolating run sequence, it will get the type of sos.
* (Note that in an isolating run sequence, an isolate initiator followed by an NSM or any type
* other than PDI must be an overflow isolate initiator.)
*
* @param int $idx Current character position
*/
protected function processW1(int $idx): void
{
$item = $this->getItem($idx);
if ($item['type'] !== 'NSM') {
return;
}
$jdx = $idx - 1;
if ($jdx < 0) {
$this->setItemType($idx, $this->seq['sos']);
return;
}
$prevItem = $this->getItem($jdx);
if ($prevItem['char'] >= UniConstant::LRI && $prevItem['char'] <= UniConstant::PDI) {
$this->setItemType($idx, 'ON');
return;
}
$this->setItemType($idx, $prevItem['type']);
}
/**
* W2. Search backward from each instance of a European number until the first strong type (R, L, AL, or sos)
* is found. If an AL is found, change the type of the European number to Arabic number.
*
* @param int $idx Current character position
*/
protected function processW2(int $idx): void
{
$item = $this->getItem($idx);
if ($item['type'] !== 'EN') {
return;
}
$jdx = $idx - 1;
while ($jdx >= 0) {
$prevItem = $this->getItem($jdx);
if ($prevItem['type'] === 'AL') {
$this->setItemType($idx, 'AN');
break;
}
if (\in_array($prevItem['type'], ['R', 'L'], true)) {
break;
}
--$jdx;
}
}
/**
* W3. Change all ALs to R.
*
* @param int $idx Current character position
*/
protected function processW3(int $idx): void
{
if ($this->getItem($idx)['type'] === 'AL') {
$this->setItemType($idx, 'R');
}
}
/**
* W4. A single European separator between two European numbers changes to a European number.
* A single common separator between two numbers of the same type changes to that type.
*
* @param int $idx Current character position
*/
protected function processW4(int $idx): void
{
$item = $this->getItem($idx);
if ($item['type'] !== 'ES' && $item['type'] !== 'CS') {
return;
}
$bdx = $idx - 1;
$fdx = $idx + 1;
if ($bdx < 0 || $fdx >= $this->seq['length']) {
return;
}
$prev = $this->getItem($bdx)['type'];
if ($prev !== $this->getItem($fdx)['type']) {
return;
}
// A European separator only joins two European numbers, while a common separator
// joins two numbers of the same type (EN,EN or AN,AN).
if ($prev === 'EN' || $prev === 'AN' && $item['type'] === 'CS') {
$this->setItemType($idx, $prev);
}
}
/**
* W5. A sequence of European terminators adjacent to European numbers changes to all European numbers.
*
* @param int $idx Current character position
*/
protected function processW5(int $idx): void
{
if ($this->getItem($idx)['type'] !== 'ET') {
return;
}
$this->processW5a($idx);
$this->processW5b($idx);
}
/**
* W5a
*
* @param int $idx Current character position
*/
protected function processW5a(int $idx): void
{
for ($jdx = $idx - 1; $jdx >= 0; --$jdx) {
if ($this->getItem($jdx)['type'] !== 'EN') {
break;
}
$this->setItemType($idx, 'EN');
}
}
/**
* W5b
*
* @param int $idx Current character position
*/
protected function processW5b(int $idx): void
{
if ($this->getItem($idx)['type'] !== 'ET') {
return;
}
for ($jdx = $idx + 1; $jdx < $this->seq['length']; ++$jdx) {
$nextItem = $this->getItem($jdx);
if ($nextItem['type'] === 'EN') {
$this->setItemType($idx, 'EN');
continue;
}
if ($nextItem['type'] !== 'ET') {
break;
}
}
}
/**
* W6. Otherwise, separators and terminators change to Other Neutral.
*
* @param int $idx Current character position
*/
protected function processW6(int $idx): void
{
if (\in_array($this->getItem($idx)['type'], ['ET', 'ES', 'CS', 'ON'], true)) {
$this->setItemType($idx, 'ON');
}
}
/**
* W7. Search backward from each instance of a European number until the first strong type (R, L, or sos) is found.
* If an L is found, then change the type of the European number to L.
*
* @param int $idx Current character position
*/
protected function processW7(int $idx): void
{
if ($this->getItem($idx)['type'] !== 'EN') {
return;
}
for ($jdx = $idx - 1; $jdx >= 0; --$jdx) {
$prevItem = $this->getItem($jdx);
if ($prevItem['type'] === 'L') {
$this->setItemType($idx, 'L');
break;
}
if ($prevItem['type'] === 'R') {
break;
}
}
if ($this->seq['sos'] === 'L' && $jdx < 0) {
$this->setItemType($idx, 'L');
}
}
}
+476
View File
@@ -0,0 +1,476 @@
<?php
declare(strict_types=1);
/**
* StepX.php
*
* @since 2011-05-23
* @category Library
* @package Unicode
* @author Nicola Asuni <info@tecnick.com>
* @copyright 2011-2026 Nicola Asuni - Tecnick.com LTD
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
* @link https://github.com/tecnickcom/tc-lib-unicode
*
* This file is part of tc-lib-unicode software library.
*/
namespace Com\Tecnick\Unicode\Bidi;
use Com\Tecnick\Unicode\Data\Constant as UniConstant;
use Com\Tecnick\Unicode\Data\Type as UniType;
/**
* Com\Tecnick\Unicode\Bidi\StepX
*
* @since 2015-07-13
* @category Library
* @package Unicode
* @author Nicola Asuni <info@tecnick.com>
* @copyright 2011-2026 Nicola Asuni - Tecnick.com LTD
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
* @link https://github.com/tecnickcom/tc-lib-unicode
*
* @phpstan-import-type SeqData from \Com\Tecnick\Unicode\Bidi\Shaping\Arabic
* @phpstan-import-type CharData from \Com\Tecnick\Unicode\Bidi\Shaping\Arabic
*
* @phpstan-type DssData array{
* 'ord': int,
* 'cel': int,
* 'dos': string,
* 'dis': bool,
* }
*/
class StepX
{
/**
* Maximum embedding level
*/
public const MAX_DEPTH = 125;
/**
* Directional Status Stack
*
* @var array<int, DssData>
*/
protected array $dss = [];
/**
* Overflow Isolate Count
*/
protected int $oic = 0;
/**
* Overflow Embedding Count
*/
protected int $oec = 0;
/**
* Valid Isolate Count
*/
protected int $vic = 0;
/**
* Array of characters data to return
*
* @var array<int, CharData>
*/
protected array $chardata = [];
/**
* X Steps for Bidirectional algorithm
* Explicit Levels and Directions
*
* @param array<int> $ordarr Array of UTF-8 codepoints
* @param int $pel Paragraph embedding level
*/
public function __construct(
/**
* Array of UTF-8 codepoints
*/
protected array $ordarr,
int $pel,
) {
// - Push onto the stack an entry consisting of the paragraph embedding level,
// a neutral directional override status, and a false directional isolate status.
$this->dss[] = [
'ord' => -1, // dummy value, not used
'cel' => $pel,
'dos' => 'NI',
'dis' => false,
];
// - Process each character iteratively, applying rules X2 through X8.
// Only embedding levels from 0 through max_depth are valid in this phase.
// (Note that in the resolution of levels in rules I1 and I2,
// the maximum embedding level of max_depth+1 can be reached.)
$this->processX();
}
/**
* Returns the processed array
*
* @return array<int, CharData>
*/
public function getChrData(): array
{
return $this->chardata;
}
/**
* Returns the last directional status stack entry.
*
* @return DssData
*/
protected function getLastDss(): array
{
$lastKey = \array_key_last($this->dss);
assert($lastKey !== null, 'Expected at least one directional stack entry');
$item = $this->dss[$lastKey] ?? null;
assert($item !== null, 'Expected directional stack entry at the last key');
return $item;
}
protected function popDss(): void
{
$lastKey = \array_key_last($this->dss);
assert($lastKey !== null, 'Expected at least one directional stack entry before pop');
unset($this->dss[$lastKey]);
}
/**
* Calculate the Least Even
*
* @param int $num Number to process
*/
protected function getLEven(int $num): int
{
return 2 + $num - ($num % 2);
}
/**
* Calculate the Least Odd
*
* @param int $num Number to process
*/
protected function getLOdd(int $num): int
{
return 1 + $num + ($num % 2);
}
/**
* Process X1
*/
protected function processX(): void
{
foreach ($this->ordarr as $key => $ord) {
$this->processXcase((int) $key, $ord);
}
}
/**
* Process X1 case
*
* @param int $pos Original character position in the input string
* @param int $ord Char code
*
* @SuppressWarnings("PHPMD.CyclomaticComplexity")
*/
protected function processXcase(int $pos, int $ord): void
{
$edss = $this->getLastDss();
switch ($ord) {
case UniConstant::RLE:
// X2
$this->setDss($this->getLOdd($edss['cel']), UniConstant::RLE, 'NI');
break;
case UniConstant::LRE:
// X3
$this->setDss($this->getLEven($edss['cel']), UniConstant::LRE, 'NI');
break;
case UniConstant::RLO:
// X4
$this->setDss($this->getLOdd($edss['cel']), UniConstant::RLO, 'R');
break;
case UniConstant::LRO:
// X5
$this->setDss($this->getLEven($edss['cel']), UniConstant::LRO, 'L');
break;
case UniConstant::RLI:
// X5a
$this->processChar($pos, $ord, $edss);
$this->setDss($this->getLOdd($edss['cel']), UniConstant::RLI, 'NI', true);
break;
case UniConstant::LRI:
// X5b
$this->processChar($pos, $ord, $edss);
$this->setDss($this->getLEven($edss['cel']), UniConstant::LRI, 'NI', true);
break;
case UniConstant::FSI:
// X5c
$this->processChar($pos, $ord, $edss);
$this->processFsiCase($pos, $edss);
break;
case UniConstant::PDI:
// X6a
$this->processPdiCase($pos, $ord, $edss);
break;
case UniConstant::PDF:
// X7
$this->processPdfCase($edss);
break;
default:
// X6
$this->processChar($pos, $ord, $edss);
break;
}
}
/**
* Set temporary data (X2 to X5)
*
* @param int $cel Embedding Level
* @param int $ord Char code
* @param string $dos Directional override status
* @param bool $isolate True if Isolate initiator
*/
protected function setDss(int $cel, int $ord, string $dos, bool $isolate = false): void
{
$dis = $isolate;
$ivic = $isolate ? 1 : 0;
// X2 to X5
// - Compute the least odd|even embedding level greater than the embedding level of the last entry
// on the directional status stack.
// - If this new level would be valid, and the overflow isolate count and overflow embedding
// count are both zero, then this RLE is valid. Push an entry consisting of the new embedding
// level, neutral|left|right directional override status, and false directional isolate status onto the
// directional status stack.
// - Otherwise, this is an overflow RLE. If the overflow isolate count is zero, increment the
// overflow embedding|isolate count by one. Leave all other variables unchanged.
// X1 allows the embedding levels 0 through max_depth, so a new level equal to
// MAX_DEPTH is still valid.
if ($cel > self::MAX_DEPTH || $this->oic !== 0 || $this->oec !== 0) {
if ($isolate) {
++$this->oic;
return;
}
if ($this->oic === 0) {
++$this->oec;
}
return;
}
$this->vic += $ivic;
$this->dss[] = [
'ord' => $ord,
'cel' => $cel,
'dos' => $dos,
'dis' => $dis,
];
}
/**
* Push a char on the stack
*
* @param int $pos Original character position in the input string
* @param int $ord Char code
* @param DssData $edss Last entry in the Directional Status Stack
*/
protected function pushChar(int $pos, int $ord, array $edss): void
{
// The original type is the bidirectional type of the codepoint; the resolved type is
// the directional override status when the last stack entry is not neutral (X6).
$unitype = UniType::getType($ord);
$this->chardata[] = [
'char' => $ord,
'i' => -1,
'level' => $edss['cel'],
'otype' => $unitype,
'pdimatch' => -1,
'pos' => $pos,
'type' => $edss['dos'] !== 'NI' ? $edss['dos'] : $unitype,
'x' => -1,
];
}
/**
* Process normal char (X6)
*
* @param int $pos Original character position in the input string
* @param int $ord Char code
* @param DssData $edss Last entry in the Directional Status Stack
*/
protected function processChar(int $pos, int $ord, array $edss): void
{
// X6. For all types besides B, BN, RLE, LRE, RLO, LRO, PDF, RLI, LRI, FSI, and PDI:
// - Set the current characters embedding level to the embedding level
// of the last entry on the directional status stack.
// - Whenever the directional override status of the last entry on the directional status stack
// is not neutral, reset the current character type according to the directional override
// status of the last entry on the directional status stack.
$charType = UniType::getType($ord);
if ($charType === 'B' || $charType === 'BN') {
return;
}
$this->pushChar($pos, $ord, $edss);
}
/**
* Process the PDF type character
*
* @param DssData $edss Last entry in the Directional Status Stack
*/
protected function processPdfCase(array $edss): void
{
// X7. With each PDF, perform the following steps:
// - If the overflow isolate count is greater than zero, do nothing. (This PDF is within the
// scope of an overflow isolate initiator. It either matches and terminates the scope of an
// overflow embedding initiator within that overflow isolate, or does not match any
// embedding initiator.)
if ($this->oic > 0) {
return;
}
// - Otherwise, if the overflow embedding count is greater than zero, decrement it by one.
// (This PDF matches and terminates the scope of an overflow embedding initiator that is not
// within the scope of an overflow isolate initiator.)
if ($this->oec > 0) {
--$this->oec;
return;
}
// - Otherwise, if the directional isolate status of the last entry on the directional status
// stack is false, and the directional status stack contains at least two entries, pop the
// last entry from the directional status stack. (This PDF matches and terminates the scope
// of a valid embedding initiator. Since the stack has at least two entries, this pop does
// not leave the stack empty.)
if (!$edss['dis'] && \count($this->dss) > 1) {
$this->popDss();
}
// - Otherwise, do nothing. (This PDF does not match any embedding initiator.)
}
/**
* Process the PDI type character
*
* @param int $pos Original character position in the input string
* @param int $ord Char code
* @param DssData $edss Last entry in the Directional Status Stack
*/
protected function processPdiCase(int $pos, int $ord, array $edss): void
{
// X6a. With each PDI, perform the following steps:
// - If the overflow isolate count is greater than zero, this PDI matches an overflow isolate
// initiator. Decrement the overflow isolate count by one.
// X9 retains the PDI, so it is pushed with the level of the current stack entry in
// every case, including the two cases below where it matches no valid initiator.
if ($this->oic > 0) {
--$this->oic;
$this->pushChar($pos, $ord, $edss);
return;
}
// - Otherwise, if the valid isolate count is zero, this PDI does not match any isolate
// initiator, valid or overflow. Do nothing.
if ($this->vic === 0) {
$this->pushChar($pos, $ord, $edss);
return;
}
// - Otherwise, this PDI matches a valid isolate initiator. Perform the following steps:
// - Reset the overflow embedding count to zero. (This terminates the scope of those overflow
// embedding initiators within the scope of the matched isolate initiator whose scopes have
// not been terminated by a matching PDF, and which thus lack a matching PDF.)
$this->oec = 0;
// - While the directional isolate status of the last entry on the stack is false, pop the
// last entry from the directional status stack. (This terminates the scope of those valid
// embedding initiators within the scope of the matched isolate initiator whose scopes have
// not been terminated by a matching PDF, and which thus lack a matching PDF. Given that the
// valid isolate count is non-zero, the directional status stack before this step is
// executed must contain an entry with directional isolate status true, and thus after this
// step is executed the last entry on the stack will indeed have a true directional isolate
// status, i.e. represent the scope of the matched isolate initiator. This cannot be the
// stack's first entry, which always belongs to the paragraph level and has a false
// directional status, so there is at least one more entry below it on the stack.)
$count_dss = \count($this->dss);
while (!$edss['dis'] && $count_dss > 1) {
$this->popDss();
--$count_dss;
$edss = $this->getLastDss();
}
// - Pop the last entry from the directional status stack and decrement the valid isolate
// count by one. (This terminates the scope of the matched isolate initiator. Since the
// preceding step left the stack with at least two entries, this pop does not leave the
// stack empty.)
$this->popDss();
--$this->vic;
$edss = $this->getLastDss();
// - In all cases, look up the last entry on the directional status stack left after the
// steps above and:
// - Set the PDIs level to the entry's embedding level.
// - If the entry's directional override status is not neutral, reset the current character type
// from PDI to L if the override status is left-to-right, and to R if the override status is
// right-to-left.
$this->pushChar($pos, $ord, $edss);
}
/**
* Process the FSI type character
*
* @param int $pos Original character position in the input string
* @param DssData $edss Last entry in the Directional Status Stack
*/
protected function processFsiCase(int $pos, array $edss): void
{
// X5c. With each FSI, apply rules P2 and P3 to the sequence of characters between the FSI and its
// matching PDI, or if there is no matching PDI, the end of the paragraph, as if this sequence
// of characters were a paragraph. If these rules decide on paragraph embedding level 1, treat
// the FSI as an RLI in rule X5a. Otherwise, treat it as an LRI in rule X5b.
$stepp = new StepP($this->getIsolateContent($pos));
if ($stepp->getPel() === 0) {
$this->setDss($this->getLEven($edss['cel']), UniConstant::LRI, 'NI', true);
return;
}
$this->setDss($this->getLOdd($edss['cel']), UniConstant::RLI, 'NI', true);
}
/**
* Return the codepoints between the isolate initiator at the given position and its
* matching PDI (BD9), or the end of the paragraph when there is no matching PDI.
*
* @param int $pos Position of the isolate initiator
*
* @return array<int>
*/
protected function getIsolateContent(int $pos): array
{
$content = [];
$depth = 0;
foreach (\array_slice($this->ordarr, $pos + 1) as $ord) {
if ($ord === UniConstant::PDI) {
if ($depth === 0) {
break;
}
--$depth;
} elseif ($ord >= UniConstant::LRI && $ord <= UniConstant::FSI) {
++$depth;
}
$content[] = $ord;
}
return $content;
}
}
+295
View File
@@ -0,0 +1,295 @@
<?php
declare(strict_types=1);
/**
* StepXten.php
*
* @since 2011-05-23
* @category Library
* @package Unicode
* @author Nicola Asuni <info@tecnick.com>
* @copyright 2011-2026 Nicola Asuni - Tecnick.com LTD
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
* @link https://github.com/tecnickcom/tc-lib-unicode
*
* This file is part of tc-lib-unicode software library.
*/
namespace Com\Tecnick\Unicode\Bidi;
use Com\Tecnick\Unicode\Data\Constant as UniConstant;
/**
* Com\Tecnick\Unicode\Bidi\StepXten
*
* @since 2015-07-13
* @category Library
* @package Unicode
* @author Nicola Asuni <info@tecnick.com>
* @copyright 2011-2026 Nicola Asuni - Tecnick.com LTD
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
* @link https://github.com/tecnickcom/tc-lib-unicode
*
* @phpstan-import-type SeqData from \Com\Tecnick\Unicode\Bidi\Shaping\Arabic
* @phpstan-import-type CharData from \Com\Tecnick\Unicode\Bidi\Shaping\Arabic
*/
class StepXten
{
/**
* Number of characters
*/
protected int $numchars;
/**
* Array of Level Run sequences
*
* @var array<int, array{'start': int, 'end': int, 'e': int}>
*/
protected array $runseq = [];
/**
* Number of Level Run sequences
*/
protected int $numrunseq = 0;
/**
* Array of Isolated Level Run sequences
*
* @var array<int, SeqData>
*/
protected array $ilrs = [];
/**
* @return CharData
*/
private function getCharData(int $idx): array
{
$charData = $this->chardata[$idx] ?? null;
assert($charData !== null, 'Expected StepXten character data at the requested index');
return $charData;
}
/**
* @return array{start: int, end: int, e: int}
*/
private function getRunSequence(int $idx): array
{
$runSequence = $this->runseq[$idx] ?? null;
assert($runSequence !== null, 'Expected StepXten level run sequence at the requested index');
return $runSequence;
}
/**
* @param SeqData $isorun
*/
private function findMatchingPdiStart(int $idx, array $isorun, int $numiso): int
{
$endItem = $isorun['item'][$isorun['length'] - 1] ?? null;
assert($endItem !== null, 'Expected final StepXten isolate-run item');
if (!$this->isIsolateInitiator($endItem['char'])) {
return -1;
}
for ($kdx = $idx + 1; $kdx < $this->numrunseq; ++$kdx) {
$runSequence = $this->getRunSequence($kdx);
if ($runSequence['e'] !== $isorun['e']) {
continue;
}
$startChar = $this->getCharData($runSequence['start']);
if ($startChar['char'] !== UniConstant::PDI) {
continue;
}
$pdimatch = $runSequence['start'];
$this->chardata[$pdimatch]['pdimatch'] = $numiso;
return $pdimatch;
}
return -1;
}
/**
* @param SeqData $isorun
*/
private function appendToParentRun(int $parent, array $isorun, int $pdimatch): void
{
$parentRun = $this->ilrs[$parent] ?? null;
assert($parentRun !== null, 'Expected parent isolate-run sequence before appending');
$parentRun['item'] = \array_merge($parentRun['item'], $isorun['item']);
$parentRun['length'] += $isorun['length'];
// The merged sequence now ends where the appended (child) run ends; 'end' is an
// absolute position in the paragraph, so it is replaced, not accumulated.
$parentRun['end'] = $isorun['end'];
$this->ilrs[$parent] = $parentRun;
if ($pdimatch >= 0) {
$this->chardata[$pdimatch]['pdimatch'] = $parent;
}
}
/**
* X Steps for Bidirectional algorithm
*
* @param array<int, CharData> $chardata Array of UTF-8 codepoints
* @param int $pel Paragraph Embedding Level
*/
public function __construct(
/**
* Array of characters data to return
*/
protected array $chardata,
/**
* Paragraph Embedding Level
*/
protected int $pel,
) {
$this->numchars = \count($chardata);
$this->setIsolatedLevelRunSequences();
}
/**
* Get the Isolated Run Sequences
*
* @return array<int, SeqData>
*/
public function getIsolatedLevelRunSequences(): array
{
return $this->ilrs;
}
/**
* Get the embedded direction (L or R)
*/
protected function getEmbeddedDirection(int $level): string
{
return ($level % 2) === 0 ? 'L' : 'R';
}
protected function setLevelRunSequences(): void
{
$start = 0;
while ($start < $this->numchars) {
$level = $this->getCharData($start)['level'];
$end = $start + 1;
while ($end < $this->numchars) {
$charData = $this->chardata[$end] ?? null;
if ($charData === null || $charData['level'] !== $level) {
break;
}
++$end;
}
--$end;
$this->runseq[] = [
'start' => $start,
'end' => $end,
'e' => $level,
];
++$this->numrunseq;
$start = $end + 1;
}
}
/**
* returns true if the input char is an Isolate Initiator
*/
protected function isIsolateInitiator(int $ord): bool
{
return $ord === UniConstant::RLI || $ord === UniConstant::LRI || $ord === UniConstant::FSI;
}
/**
* Set level Isolated Level Run Sequences
*
* @SuppressWarnings("PHPMD.CyclomaticComplexity")
*/
protected function setIsolatedLevelRunSequences(): void
{
$this->setLevelRunSequences();
$numiso = 0;
foreach ($this->runseq as $idx => $seq) {
// Create a new level run sequence, and initialize it to contain just that level run
$isorun = [
'e' => $seq['e'],
'edir' => $this->getEmbeddedDirection($seq['e']), // embedded direction
'start' => $seq['start'], // position of the first char
'end' => $seq['end'], // position of the last char
'length' => $seq['end'] - $seq['start'] + 1,
'sos' => '', // start-of-sequence
'eos' => '', // end-of-sequence
'maxlevel' => 0,
'item' => [],
];
for ($jdx = 0; $jdx < $isorun['length']; ++$jdx) {
$isorun['item'][$jdx] = $this->getCharData($seq['start'] + $jdx);
}
// While the level run currently last in the sequence ends with an isolate initiator that has a
// matching PDI, append the level run containing the matching PDI to the sequence.
// (Note that this matching PDI must be the first character of its level run.)
$pdimatch = $this->findMatchingPdiStart($idx, $isorun, $numiso);
// For each level run in the paragraph whose first character is not a PDI,
// or is a PDI that does not match any isolate initiator
$parent = $this->getCharData($seq['start'])['pdimatch'];
if ($parent >= 0 && array_key_exists($parent, $this->ilrs)) {
$this->appendToParentRun($parent, $isorun, $pdimatch);
continue;
}
$this->ilrs[$numiso] = $isorun;
++$numiso;
}
$this->setStartEndOfSequence();
}
/**
* Determine the start-of-sequence (sos) and end-of-sequence (eos) types, either L or R,
* for each isolating run sequence.
*/
protected function setStartEndOfSequence(): void
{
foreach ($this->ilrs as $key => $seq) {
// For sos, compare the level of the first character in the sequence with the level of the character
// preceding it in the paragraph (not counting characters removed by X9), and if there is none,
// with the paragraph embedding level.
$firstChar = $seq['item'][0] ?? null;
assert($firstChar !== null, 'Expected first character for StepXten isolate-run sequence');
$lev = $firstChar['level'];
$prev = $this->pel;
if ($seq['start'] !== 0) {
$prev = $this->getCharData($seq['start'] - 1)['level'];
}
$this->ilrs[$key]['sos'] = $this->getEmbeddedDirection((int) \max($prev, $lev));
// For eos, compare the level of the last character in the sequence with the level of the character
// following it in the paragraph (not counting characters removed by X9), and if there is none or the
// last character of the sequence is an isolate initiator (lacking a matching PDI), with the paragraph
// embedding level.
$lastchr = \end($seq['item']);
// A level run always contains at least one character, so end() is not false.
assert($lastchr !== false, 'Expected final character for StepXten isolate-run sequence');
$lev = $lastchr['level'];
$next = $this->pel;
$nextChar = $this->chardata[$seq['end'] + 1] ?? null;
if ($nextChar !== null && !$this->isIsolateInitiator($lastchr['char'])) {
$next = $nextChar['level'];
}
$this->ilrs[$key]['eos'] = $this->getEmbeddedDirection((int) \max($next, $lev));
// If the higher level is odd, the sos or eos is R; otherwise, it is L.
}
}
}
+197
View File
@@ -0,0 +1,197 @@
<?php
declare(strict_types=1);
/**
* Convert.php
*
* @since 2011-05-23
* @category Library
* @package Unicode
* @author Nicola Asuni <info@tecnick.com>
* @copyright 2011-2026 Nicola Asuni - Tecnick.com LTD
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
* @link https://github.com/tecnickcom/tc-lib-unicode
*
* This file is part of tc-lib-unicode software library.
*/
namespace Com\Tecnick\Unicode;
use Com\Tecnick\Unicode\Exception as UniException;
/**
* Com\Tecnick\Unicode\Convert
*
* Malformed input policy: a string or char array that is not valid UTF-8 raises an
* exception, while a code point that cannot be encoded (negative, surrogate or greater
* than U+10FFFF) is replaced with '?'.
*
* @since 2015-07-13
* @category Library
* @package Unicode
* @author Nicola Asuni <info@tecnick.com>
* @copyright 2011-2026 Nicola Asuni - Tecnick.com LTD
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
* @link https://github.com/tecnickcom/tc-lib-unicode
*/
class Convert extends \Com\Tecnick\Unicode\Convert\Encoding
{
/**
* Returns the unicode string containing the character specified by value
*
* @param int $ord Unicode character value to convert
*
* @return string Returns the unicode string
*
* @throws UniException
*/
public function chr(int $ord): string
{
$result = \mb_convert_encoding(\pack('N', $ord), 'UTF-8', 'UCS-4BE');
if ($result === false) {
throw new UniException('Error converting character');
}
return $result;
}
/**
* Returns the unicode value of the specified character.
* If more than one character is given, only the first codepoint is returned.
*
* @param string $chr Unicode character
*
* @return int Returns the unicode value
*
* @throws UniException
*/
public function ord(string $chr): int
{
$ucs = \mb_convert_encoding($chr, 'UCS-4BE', 'UTF-8');
if ($ucs === false || \strlen($ucs) < 4) {
throw new UniException('Error converting string');
}
$uni = \unpack('N', $ucs);
if ($uni === false) {
throw new UniException('Error converting string');
}
return $uni[1];
}
/**
* Converts an UTF-8 string to an array of UTF-8 characters
*
* @param string $str String to convert
*
* @return array<int, string>
*
* @throws UniException
*/
public function strToChrArr(string $str): array
{
$ret = \preg_split('//u', $str, -1, PREG_SPLIT_NO_EMPTY);
if ($ret === false) {
throw new UniException('Error splitting string');
}
return $ret;
}
/**
* Converts an array of UTF-8 chars to an array of codepoints (integer values)
*
* @param array<string> $chars Array of UTF-8 chars
*
* @return array<int>
*
* @throws UniException
*/
public function chrArrToOrdArr(array $chars): array
{
if ($chars === []) {
return [];
}
return $this->strToOrdArr(\implode('', $chars));
}
/**
* Converts an array of UTF-8 code points to an array of chars
*
* @param array<int> $ords Array of UTF-8 code points
*
* @return array<string>
*
* @throws UniException
*/
public function ordArrToChrArr(array $ords): array
{
if ($ords === []) {
return [];
}
// Surrogate code points are not valid UTF-8 but are passed through by
// mb_convert_encoding(), so they are substituted like the other invalid values.
$valid = \array_map(static fn(int $ord): int => $ord >= 0xD800 && $ord <= 0xDFFF ? 0x3F : $ord, $ords);
$str = \mb_convert_encoding(\pack('N*', ...$valid), 'UTF-8', 'UCS-4BE');
if ($str === false) {
throw new UniException('Error converting code points');
}
return \mb_str_split($str, 1, 'UTF-8');
}
/**
* Converts an UTF-8 string to an array of UTF-8 codepoints (integer values)
*
* @param string $str String to convert
*
* @return array<int>
*
* @throws UniException
*/
public function strToOrdArr(string $str): array
{
if ($str === '') {
return [];
}
if (!\mb_check_encoding($str, 'UTF-8')) {
throw new UniException('Invalid UTF-8 string');
}
$ucs = \mb_convert_encoding($str, 'UCS-4BE', 'UTF-8');
if ($ucs === false) {
throw new UniException('Error converting string');
}
$ords = \unpack('N*', $ucs);
if ($ords === false) {
throw new UniException('Error unpacking string');
}
return \array_values($ords);
}
/**
* Extract a slice of the $uniarr array and return it as string
*
* @param array<string> $uniarr The input array of characters
* @param int $start The position of the starting element
* @param int|null $end The position of the first element that will not be returned.
*
* @return string
*/
public function getSubUniArrStr(array $uniarr, int $start = 0, ?int $end = null): string
{
if ($end === null) {
$end = \count($uniarr);
}
return \implode('', \array_slice($uniarr, $start, $end - $start));
}
}
@@ -0,0 +1,148 @@
<?php
declare(strict_types=1);
/**
* Encoding.php
*
* @since 2011-05-23
* @category Library
* @package Unicode
* @author Nicola Asuni <info@tecnick.com>
* @copyright 2011-2026 Nicola Asuni - Tecnick.com LTD
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
* @link https://github.com/tecnickcom/tc-lib-unicode
*
* This file is part of tc-lib-unicode software library.
*/
namespace Com\Tecnick\Unicode\Convert;
use Com\Tecnick\Unicode\Data\Latin as Latin;
/**
* Com\Tecnick\Unicode\Convert\Encoding
*
* @since 2015-07-13
* @category Library
* @package Unicode
* @author Nicola Asuni <info@tecnick.com>
* @copyright 2011-2026 Nicola Asuni - Tecnick.com LTD
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
* @link https://github.com/tecnickcom/tc-lib-unicode
*/
class Encoding
{
/**
* Converts UTF-8 code array to Latin1 codes
*
* @param array<int> $ordarr Array containing UTF-8 code points
*
* @return array<int> Array containing Latin1 code points
*/
public function uniArrToLatinArr(array $ordarr): array
{
$latarr = [];
foreach ($ordarr as $chr) {
if ($chr < 0) {
$latarr[] = 63; // '?' character
continue;
}
if ($chr < 256) {
$latarr[] = $chr;
continue;
}
$substitute = Latin::SUBSTITUTE[$chr] ?? null;
if (\is_int($substitute)) {
$latarr[] = $substitute & 0xFF;
continue;
}
if ($chr !== 0xFFFD) {
$latarr[] = 63; // '?' character
}
}
return $latarr;
}
/**
* Converts an array of Latin1 code points to a string
*
* @param array<int<0, 255>> $latarr Array of Latin1 code points
*/
public function latinArrToStr(array $latarr): string
{
return \implode('', \array_map('chr', $latarr));
}
/**
* Convert a string to an hexadecimal string (byte string) representation (as in the PDF standard)
*
* @param string $str String to convert
*/
public function strToHex(string $str): string
{
return \bin2hex($str);
}
/**
* Convert an hexadecimal string (byte string - as in the PDF standard) to string.
* Pairs of characters that are not hexadecimal digits are converted to a NUL byte.
*
* @param string $hex Hex code to convert
*/
public function hexToStr(string $hex): string
{
if (\strlen($hex) === 0) {
return '';
}
$str = '';
$bytes = \str_split($hex, 2);
foreach ($bytes as $byte) {
// hexdec() raises a deprecation notice on non-hexadecimal characters.
$str .= \ctype_xdigit($byte) ? \chr((int) \hexdec($byte) & 0xFF) : "\x00";
}
return $str;
}
/**
* Converts a string with an unknown encoding to UTF-8
*
* @param string $str String to convert
* @param null|string|array<string> $enc Array or comma separated list string of encodings
*
* @return string UTF-8 encoded string
*/
public function toUTF8(string $str, string|array|null $enc = null): string
{
if ($enc === null) {
$enc = \mb_detect_order();
}
$chrenc = \mb_detect_encoding($str, $enc);
if ($chrenc === false) {
$chrenc = null;
}
$result = \mb_convert_encoding($str, 'UTF-8', $chrenc);
return $result === false ? '' : $result;
}
/**
* Converts an UTF-8 string to UTF-16BE
*
* @param string $str UTF-8 String to convert
*
* @return string UTF-16BE encoded string
*/
public function toUTF16BE(string $str): string
{
$result = \mb_convert_encoding($str, 'UTF-16BE', 'UTF-8');
return $result === false ? '' : $result;
}
}
@@ -0,0 +1,92 @@
<?php
declare(strict_types=1);
/**
* Devanagari.php
*
* @since 2026-04-30
* @category Library
* @package Unicode
* @author Nicola Asuni <info@tecnick.com>
* @copyright 2011-2026 Nicola Asuni - Tecnick.com LTD
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
* @link https://github.com/tecnickcom/tc-lib-unicode
*
* This file is part of tc-lib-unicode software library.
*
* Source: https://www.unicode.org/Public/17.0.0/ucd/UnicodeData.txt
* https://www.unicode.org/Public/17.0.0/ucd/IndicPositionalCategory.txt
*/
namespace Com\Tecnick\Unicode\Data;
/**
* Com\Tecnick\Unicode\Data\Devanagari
*
* Devanagari codepoint tables for character substitution and cluster
* reordering.
*
* @since 2026-04-30
* @category Library
* @package Unicode
* @author Nicola Asuni <info@tecnick.com>
* @copyright 2011-2026 Nicola Asuni - Tecnick.com LTD
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
* @link https://github.com/tecnickcom/tc-lib-unicode
*/
final class Devanagari
{
/**
* Devanagari vowel signs with Indic Positional Category "Left".
*
* These matras are stored after their base consonant (or consonant
* cluster) in Unicode logical order but must be rendered to the LEFT of
* the base in a PDF glyph stream. They are therefore repositioned to
* precede the consonant cluster during substitution.
*
* @var array<int, true>
*/
public const LEFT_MATRAS = [
0x093F => true, // DEVANAGARI VOWEL SIGN I
0x094E => true, // DEVANAGARI VOWEL SIGN PRISHTHAMATRA E
];
/**
* Devanagari Virama (U+094D).
*
* Joins two consonants into a conjunct cluster. When scanning a consonant
* cluster for pre-base matra reordering, consecutive (consonant + VIRAMA)
* pairs extend the cluster.
*/
public const VIRAMA = 0x094D;
/**
* First codepoint of the standard Devanagari consonant range.
*
* U+0915 DEVANAGARI LETTER KA
*/
public const BASE_CONSONANT_FIRST = 0x0915;
/**
* Last codepoint of the standard Devanagari consonant range.
*
* U+0939 DEVANAGARI LETTER HA
*/
public const BASE_CONSONANT_LAST = 0x0939;
/**
* First codepoint of the extended Devanagari consonant range
* (consonants with nukta, deprecated precomposed forms).
*
* U+0958 DEVANAGARI LETTER QA
*/
public const BASE_CONSONANT_EXT_FIRST = 0x0958;
/**
* Last codepoint of the extended Devanagari consonant range.
*
* U+095F DEVANAGARI LETTER YYA
*/
public const BASE_CONSONANT_EXT_LAST = 0x095F;
}
+105
View File
@@ -0,0 +1,105 @@
<?php
declare(strict_types=1);
/**
* Hangul.php
*
* @since 2026-04-30
* @category Library
* @package Unicode
* @author Nicola Asuni <info@tecnick.com>
* @copyright 2011-2026 Nicola Asuni - Tecnick.com LTD
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
* @link https://github.com/tecnickcom/tc-lib-unicode
*
* This file is part of tc-lib-unicode software library.
*
* Source: section 3.12 "Conjoining Jamo Behavior" of the Unicode standard
*/
namespace Com\Tecnick\Unicode\Data;
/**
* Com\Tecnick\Unicode\Data\Hangul
*
* Algorithmic constants for Hangul Jamo → precomposed Hangul syllable
* composition, as defined in section 3.12 of the Unicode Standard.
*
* Precomposed syllables occupy the range U+AC00U+D7A3 and are derived by:
*
* S = SBase + (L LBase) × NCount + (V VBase) × TCount + (T TBase)
*
* where T = TBase means "no trailing consonant" (TBase itself is not a
* trailing consonant; the effective trailing index is 0 in that case).
*
* @since 2026-04-30
* @category Library
* @package Unicode
* @author Nicola Asuni <info@tecnick.com>
* @copyright 2011-2026 Nicola Asuni - Tecnick.com LTD
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
* @link https://github.com/tecnickcom/tc-lib-unicode
*/
final class Hangul
{
/**
* First precomposed Hangul syllable: U+AC00 HANGUL SYLLABLE GA.
*/
public const SBASE = 0xAC00;
/**
* First Hangul leading consonant (choseong): U+1100 HANGUL CHOSEONG KIYEOK.
*/
public const LBASE = 0x1100;
/**
* First Hangul vowel (jungseong): U+1161 HANGUL JUNGSEONG A.
*/
public const VBASE = 0x1161;
/**
* Trailing consonant base value: U+11A7.
*
* The first actual trailing consonant (jongseong) is U+11A8; TBase is
* one below that, so that (T TBase) gives a 1-based index and a T of
* TBase itself encodes "no trailing consonant" (index 0).
*/
public const TBASE = 0x11A7;
/**
* Number of leading consonants (19).
*
* Covers U+1100U+1112.
*/
public const LCOUNT = 19;
/**
* Number of vowels (21).
*
* Covers U+1161U+1175.
*/
public const VCOUNT = 21;
/**
* Number of trailing consonant slots (28), including the "none" slot.
*
* Effective trailing consonants: U+11A8U+11C2 (27 codepoints).
* The 28th slot represents absence of a trailing consonant.
*/
public const TCOUNT = 28;
/**
* Number of precomposed syllables per leading consonant.
*
* NCount = VCount × TCount = 21 × 28 = 588.
*/
public const NCOUNT = self::VCOUNT * self::TCOUNT;
/**
* Total number of precomposed Hangul syllables.
*
* SCount = LCount × NCount = 19 × 588 = 11172.
*/
public const SCOUNT = self::LCOUNT * self::NCOUNT;
}
@@ -0,0 +1,80 @@
<?php
declare(strict_types=1);
/**
* ScriptRanges.php
*
* @since 2026-04-30
* @category Library
* @package Unicode
* @author Nicola Asuni <info@tecnick.com>
* @copyright 2011-2026 Nicola Asuni - Tecnick.com LTD
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
* @link https://github.com/tecnickcom/tc-lib-unicode
*
* This file is part of tc-lib-unicode software library.
*
* Source: https://www.unicode.org/Public/17.0.0/ucd/Blocks.txt
*/
namespace Com\Tecnick\Unicode\Data;
/**
* Com\Tecnick\Unicode\Data\ScriptRanges
*
* Unicode codepoint ranges used to detect which scripts are present in a
* codepoint array. Each entry is [firstCodepoint, lastCodepoint].
*
* @since 2026-04-30
* @category Library
* @package Unicode
* @author Nicola Asuni <info@tecnick.com>
* @copyright 2011-2026 Nicola Asuni - Tecnick.com LTD
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
* @link https://github.com/tecnickcom/tc-lib-unicode
*/
final class ScriptRanges
{
/**
* Thai block: U+0E00U+0E7F
*
* @var array{int, int}
*/
public const THAI = [0x0E00, 0x0E7F];
/**
* Devanagari block: U+0900U+097F
*
* @var array{int, int}
*/
public const DEVANAGARI = [0x0900, 0x097F];
/**
* Bengali block: U+0980U+09FF
*
* @var array{int, int}
*/
public const BENGALI = [0x0980, 0x09FF];
/**
* Hangul Jamo block: U+1100U+11FF
*
* @var array{int, int}
*/
public const HANGUL_JAMO = [0x1100, 0x11FF];
/**
* Hangul Jamo Extended-A block: U+A960U+A97F
*
* @var array{int, int}
*/
public const HANGUL_JAMO_EXT_A = [0xA960, 0xA97F];
/**
* Hangul Jamo Extended-B block: U+D7B0U+D7FF
*
* @var array{int, int}
*/
public const HANGUL_JAMO_EXT_B = [0xD7B0, 0xD7FF];
}
+94
View File
@@ -0,0 +1,94 @@
<?php
declare(strict_types=1);
/**
* Thai.php
*
* @since 2026-04-30
* @category Library
* @package Unicode
* @author Nicola Asuni <info@tecnick.com>
* @copyright 2011-2026 Nicola Asuni - Tecnick.com LTD
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
* @link https://github.com/tecnickcom/tc-lib-unicode
*
* This file is part of tc-lib-unicode software library.
*
* Source: https://www.unicode.org/Public/17.0.0/ucd/UnicodeData.txt
*/
namespace Com\Tecnick\Unicode\Data;
/**
* Com\Tecnick\Unicode\Data\Thai
*
* Thai codepoint tables for character substitution and reordering.
*
* @since 2026-04-30
* @category Library
* @package Unicode
* @author Nicola Asuni <info@tecnick.com>
* @copyright 2011-2026 Nicola Asuni - Tecnick.com LTD
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
* @link https://github.com/tecnickcom/tc-lib-unicode
*/
final class Thai
{
/**
* Thai leading vowels that must be repositioned after their base consonant
* in a PDF glyph stream (visual order).
*
* These vowels visually precede the base consonant in text but are stored
* before the consonant in Unicode logical order. For PDF rendering they
* must appear after the consonant in the glyph array.
*
* Codepoints:
* U+0E40 THAI CHARACTER SARA E
* U+0E41 THAI CHARACTER SARA AE
* U+0E42 THAI CHARACTER SARA O
* U+0E43 THAI CHARACTER SARA AI MAIMUAN
* U+0E44 THAI CHARACTER SARA AI MAIMALAI
*
*
* @var array<int, true>
*/
public const LEADING_VOWELS = [
0x0E40 => true,
0x0E41 => true,
0x0E42 => true,
0x0E43 => true,
0x0E44 => true,
];
/**
* Thai tone marks (must not be treated as base consonants during cluster
* scanning).
*
* Codepoints:
* U+0E48 THAI CHARACTER MAI EK
* U+0E49 THAI CHARACTER MAI THO
* U+0E4A THAI CHARACTER MAI TRI
* U+0E4B THAI CHARACTER MAI JATTAWA
*
*
* @var array<int, true>
*/
public const TONE_MARKS = [
0x0E48 => true,
0x0E49 => true,
0x0E4A => true,
0x0E4B => true,
];
/**
* Thai base consonant range: U+0E01U+0E2E
* (THAI CHARACTER KO KAI through THAI CHARACTER HO NOKHUK)
*/
public const BASE_CONSONANT_FIRST = 0x0E01;
/**
* Thai base consonant range upper bound: U+0E2E
*/
public const BASE_CONSONANT_LAST = 0x0E2E;
}
+34
View File
@@ -0,0 +1,34 @@
<?php
declare(strict_types=1);
/**
* Exception.php
*
* @since 2015-07-28
* @category Library
* @package Unicode
* @author Nicola Asuni <info@tecnick.com>
* @copyright 2015-2026 Nicola Asuni - Tecnick.com LTD
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
* @link https://github.com/tecnickcom/tc-lib-unicode
*
* This file is part of tc-lib-unicode software library.
*/
namespace Com\Tecnick\Unicode;
/**
* Com\Tecnick\Unicode\Exception
*
* Custom Exception class
*
* @since 2015-07-28
* @category Library
* @package Unicode
* @author Nicola Asuni <info@tecnick.com>
* @copyright 2015-2026 Nicola Asuni - Tecnick.com LTD
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
* @link https://github.com/tecnickcom/tc-lib-unicode
*/
class Exception extends \Exception {}
+133
View File
@@ -0,0 +1,133 @@
<?php
declare(strict_types=1);
/**
* Substitution.php
*
* @since 2026-04-30
* @category Library
* @package Unicode
* @author Nicola Asuni <info@tecnick.com>
* @copyright 2011-2026 Nicola Asuni - Tecnick.com LTD
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
* @link https://github.com/tecnickcom/tc-lib-unicode
*
* This file is part of tc-lib-unicode software library.
*/
namespace Com\Tecnick\Unicode;
use Com\Tecnick\Unicode\Data\ScriptRanges;
use Com\Tecnick\Unicode\Substitution\Devanagari as DevanagariHandler;
use Com\Tecnick\Unicode\Substitution\Hangul as HangulHandler;
use Com\Tecnick\Unicode\Substitution\Thai as ThaiHandler;
/**
* Com\Tecnick\Unicode\Substitution
*
* Top-level entry point for context-sensitive Unicode character substitution.
*
* Detects which scripts are present in the codepoint array in a single pass,
* then applies the matching per-script handler(s) in sequence:
* Thai → Devanagari → Hangul.
*
* Codepoints belonging to unsupported or unrecognised scripts are passed
* through unmodified. The method never discards input it cannot classify.
*
* @since 2026-04-30
* @category Library
* @package Unicode
* @author Nicola Asuni <info@tecnick.com>
* @copyright 2011-2026 Nicola Asuni - Tecnick.com LTD
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
* @link https://github.com/tecnickcom/tc-lib-unicode
*/
final class Substitution
{
/**
* Applies script-specific character substitutions to the codepoint array.
*
* Performs one pass to detect active scripts, then one pass per active
* handler. Empty input is returned as-is.
*
* @param array<int, int> $ordarr Array of Unicode codepoints.
*
* @return array<int, int> Transformed codepoint array.
*/
public function replaceChars(array $ordarr): array
{
if ($ordarr === []) {
return [];
}
$scripts = $this->detectScripts($ordarr);
if ($scripts['thai']) {
$subobj = new ThaiHandler($ordarr);
$ordarr = $subobj->getOrdarr();
}
if ($scripts['devanagari']) {
$subobj = new DevanagariHandler($ordarr);
$ordarr = $subobj->getOrdarr();
}
if ($scripts['hangul']) {
$subobj = new HangulHandler($ordarr);
$ordarr = $subobj->getOrdarr();
}
return $ordarr;
}
/**
* Scans $ordarr once and returns a map of which scripts are present.
*
* @param array<int, int> $ordarr
*
* @return array{thai: bool, devanagari: bool, hangul: bool}
*/
private function detectScripts(array $ordarr): array
{
$scripts = ['thai' => false, 'devanagari' => false, 'hangul' => false];
foreach ($ordarr as $codepoint) {
if (!$scripts['thai'] && $this->isInRange($codepoint, ScriptRanges::THAI)) {
$scripts['thai'] = true;
}
if (!$scripts['devanagari'] && $this->isInRange($codepoint, ScriptRanges::DEVANAGARI)) {
$scripts['devanagari'] = true;
}
if (!$scripts['hangul'] && $this->isHangulJamo($codepoint)) {
$scripts['hangul'] = true;
}
}
return $scripts;
}
/**
* Returns true when $codepoint is in any of the three Hangul Jamo ranges.
*/
private function isHangulJamo(int $codepoint): bool
{
return (
$this->isInRange($codepoint, ScriptRanges::HANGUL_JAMO)
|| $this->isInRange($codepoint, ScriptRanges::HANGUL_JAMO_EXT_A)
|| $this->isInRange($codepoint, ScriptRanges::HANGUL_JAMO_EXT_B)
);
}
/**
* Returns true when $codepoint falls within $range[0]..$range[1] inclusive.
*
* @param int $codepoint
* @param array{int, int} $range
*/
private function isInRange(int $codepoint, array $range): bool
{
return $codepoint >= $range[0] && $codepoint <= $range[1];
}
}
@@ -0,0 +1,208 @@
<?php
declare(strict_types=1);
/**
* Devanagari.php
*
* @since 2026-04-30
* @category Library
* @package Unicode
* @author Nicola Asuni <info@tecnick.com>
* @copyright 2011-2026 Nicola Asuni - Tecnick.com LTD
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
* @link https://github.com/tecnickcom/tc-lib-unicode
*
* This file is part of tc-lib-unicode software library.
*/
namespace Com\Tecnick\Unicode\Substitution;
use Com\Tecnick\Unicode\Data\Devanagari as DevanagariData;
/**
* Com\Tecnick\Unicode\Substitution\Devanagari
*
* Repositions Devanagari left-positional matras (vowel signs with Indic
* Positional Category "Left", such as U+093F VOWEL SIGN I) to precede their
* base consonant cluster in the codepoint array.
*
* In Unicode logical order a left matra is stored after the consonant (or
* conjunct cluster) it modifies. For PDF glyph streams the matra glyph must
* appear before the consonant, so each left matra is moved to immediately
* before the cluster that precedes it.
*
* A consonant cluster is: base_consonant (VIRAMA base_consonant)*
* Only base consonants in the range U+0915U+0939 and U+0958U+095F are
* recognised as cluster heads. Orphaned matras (no preceding consonant) and
* unknown codepoints are left unchanged.
*
* @since 2026-04-30
* @category Library
* @package Unicode
* @author Nicola Asuni <info@tecnick.com>
* @copyright 2011-2026 Nicola Asuni - Tecnick.com LTD
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
* @link https://github.com/tecnickcom/tc-lib-unicode
*/
final class Devanagari
{
/**
* Transformed codepoint array.
*
* @var list<int>
*/
private array $ordarr;
/**
* @param array<int, int> $ordarr Array of Unicode codepoints.
*/
public function __construct(array $ordarr)
{
$this->ordarr = array_values($ordarr);
$this->process();
}
/**
* Returns the transformed codepoint array.
*
* @return list<int>
*/
public function getOrdarr(): array
{
return $this->ordarr;
}
/**
* Iterates over the codepoint array and repositions left matras that
* follow a consonant cluster.
*/
private function process(): void
{
$len = count($this->ordarr);
$result = [];
$idx = 0;
while ($idx < $len) {
$cp = $this->ordarr[$idx] ?? null;
if ($cp === null) {
++$idx;
continue;
}
if ($this->isBaseConsonant($cp)) {
$idx = $this->processConsonantCluster($idx, $len, $result);
continue;
}
$result[] = $cp;
++$idx;
}
$this->ordarr = array_values($result);
}
/**
* Collects the full conjunct cluster starting at $idx, then checks
* whether the next codepoint is a left matra. If so, emits the matra
* first and the cluster second; otherwise emits the cluster as-is.
*
* @param int $idx Current index in $this->ordarr.
* @param int $len Length of $this->ordarr.
* @param list<int> $result Result accumulator (passed by reference).
*
* @return int Updated index after all consumed codepoints.
*/
private function processConsonantCluster(int $idx, int $len, array &$result): int
{
$cluster = $this->collectCluster($idx, $len);
$endIdx = $idx + count($cluster);
if ($endIdx < $len) {
$matra = $this->ordarr[$endIdx] ?? null;
if ($matra !== null && $this->isLeftMatra($matra)) {
$result[] = $matra;
foreach ($cluster as $codepoint) {
$result[] = $codepoint;
}
return $endIdx + 1;
}
}
foreach ($cluster as $codepoint) {
$result[] = $codepoint;
}
return $endIdx;
}
/**
* Collects the full conjunct cluster: base_consonant (VIRAMA base_consonant)*
*
* @param int $idx Starting index of the cluster head.
* @param int $len Length of $this->ordarr.
*
* @return list<int> Collected cluster codepoints.
*/
private function collectCluster(int $idx, int $len): array
{
$first = $this->ordarr[$idx] ?? null;
if ($first === null) {
return [];
}
$cluster = [$first];
$pos = $idx + 1;
while (($pos + 1) < $len) {
$virama = $this->ordarr[$pos] ?? null;
$baseConsonant = $this->ordarr[$pos + 1] ?? null;
assert($baseConsonant !== null, 'Expected Devanagari base consonant after virama candidate');
if ($virama !== DevanagariData::VIRAMA || !$this->isBaseConsonant($baseConsonant)) {
break;
}
$cluster[] = $virama;
$cluster[] = $baseConsonant;
$pos += 2;
}
return $cluster;
}
/**
* Returns true when $codepoint is a Devanagari base consonant.
*/
private function isBaseConsonant(int $codepoint): bool
{
return $this->isInStandardRange($codepoint) || $this->isInExtendedRange($codepoint);
}
/**
* Returns true when $codepoint is in the standard consonant range
* U+0915U+0939.
*/
private function isInStandardRange(int $codepoint): bool
{
return $codepoint >= DevanagariData::BASE_CONSONANT_FIRST && $codepoint <= DevanagariData::BASE_CONSONANT_LAST;
}
/**
* Returns true when $codepoint is in the extended consonant range
* U+0958U+095F.
*/
private function isInExtendedRange(int $codepoint): bool
{
return (
$codepoint >= DevanagariData::BASE_CONSONANT_EXT_FIRST
&& $codepoint <= DevanagariData::BASE_CONSONANT_EXT_LAST
);
}
/**
* Returns true when $codepoint is a Devanagari left matra.
*/
private function isLeftMatra(int $codepoint): bool
{
return array_key_exists($codepoint, DevanagariData::LEFT_MATRAS);
}
}
@@ -0,0 +1,208 @@
<?php
declare(strict_types=1);
/**
* Hangul.php
*
* @since 2026-04-30
* @category Library
* @package Unicode
* @author Nicola Asuni <info@tecnick.com>
* @copyright 2011-2026 Nicola Asuni - Tecnick.com LTD
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
* @link https://github.com/tecnickcom/tc-lib-unicode
*
* This file is part of tc-lib-unicode software library.
*/
namespace Com\Tecnick\Unicode\Substitution;
use Com\Tecnick\Unicode\Data\Hangul as HangulData;
/**
* Com\Tecnick\Unicode\Substitution\Hangul
*
* Composes Hangul Jamo sequences into precomposed syllables, as defined by section
* 3.12 "Conjoining Jamo Behavior" of the Unicode standard. Both rules are applied
* left to right in a single pass:
*
* Rule 1, L + V to an LV syllable: a leading consonant (choseong, U+1100 to U+1112)
* followed by a vowel (jungseong, U+1161 to U+1175) becomes
* S = SBase + (L - LBase) * NCount + (V - VBase) * TCount.
*
* Rule 2, LV + T to an LVT syllable: an LV syllable, produced by rule 1 or already
* present in the input, followed by a trailing consonant (jongseong, U+11A8 to
* U+11C2) becomes S = LV + (T - TBase).
*
* The other codepoints are returned unchanged.
*
* @since 2026-04-30
* @category Library
* @package Unicode
* @author Nicola Asuni <info@tecnick.com>
* @copyright 2011-2026 Nicola Asuni - Tecnick.com LTD
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
* @link https://github.com/tecnickcom/tc-lib-unicode
*/
final class Hangul
{
/**
* Transformed codepoint array.
*
* @var list<int>
*/
private array $ordarr;
/**
* @param array<int, int> $ordarr Array of Unicode codepoints.
*/
public function __construct(array $ordarr)
{
$this->ordarr = array_values($ordarr);
$this->process();
}
/**
* Returns the transformed codepoint array.
*
* @return list<int>
*/
public function getOrdarr(): array
{
return $this->ordarr;
}
/**
* Iterates over the codepoint array applying L+V and LV+T composition.
*/
private function process(): void
{
$len = count($this->ordarr);
$result = [];
$idx = 0;
while ($idx < $len) {
$codepoint = $this->ordarr[$idx] ?? null;
if ($codepoint === null) {
++$idx;
continue;
}
if ($this->isLeadingConsonant($codepoint) && ($idx + 1) < $len) {
$idx = $this->composeLV($idx, $len, $result);
continue;
}
if ($this->isLvSyllable($codepoint) && ($idx + 1) < $len) {
$tChar = $this->ordarr[$idx + 1] ?? null;
if ($tChar !== null && $this->isTrailingConsonant($tChar)) {
$result[] = $codepoint + ($tChar - HangulData::TBASE);
$idx += 2;
continue;
}
}
$result[] = $codepoint;
++$idx;
}
$this->ordarr = array_values($result);
}
/**
* Attempts L+V composition at $idx. On success also attempts LV+T.
* Falls back to emitting the leading consonant unchanged if no vowel
* follows.
*
* @param int $idx Current index (L position).
* @param int $len Length of $this->ordarr.
* @param list<int> $result Result accumulator (passed by reference).
*
* @return int Updated index after all consumed codepoints.
*/
private function composeLV(int $idx, int $len, array &$result): int
{
$lChar = $this->ordarr[$idx] ?? null;
$vChar = $this->ordarr[$idx + 1] ?? null;
if ($lChar === null) {
return $idx + 1;
}
if ($vChar === null || !$this->isVowel($vChar)) {
$result[] = $lChar;
return $idx + 1;
}
$lvSyllable = $this->buildLVSyllable($lChar, $vChar);
$nextIdx = $idx + 2;
if ($nextIdx < $len) {
$tChar = $this->ordarr[$nextIdx] ?? null;
if ($tChar !== null && $this->isTrailingConsonant($tChar)) {
$result[] = $lvSyllable + ($tChar - HangulData::TBASE);
return $nextIdx + 1;
}
}
$result[] = $lvSyllable;
return $nextIdx;
}
/**
* Computes the LV precomposed syllable from a leading consonant and vowel.
*
* @param int $lChar Leading consonant codepoint.
* @param int $vChar Vowel codepoint.
*/
private function buildLVSyllable(int $lChar, int $vChar): int
{
$lIndex = $lChar - HangulData::LBASE;
$vIndex = $vChar - HangulData::VBASE;
return HangulData::SBASE + ($lIndex * HangulData::NCOUNT) + ($vIndex * HangulData::TCOUNT);
}
/**
* Returns true when $codepoint is a precomposed LV syllable: a syllable of the
* U+AC00-U+D7A3 range that has no trailing consonant yet.
*/
private function isLvSyllable(int $codepoint): bool
{
if ($codepoint < HangulData::SBASE || $codepoint >= (HangulData::SBASE + HangulData::SCOUNT)) {
return false;
}
return (($codepoint - HangulData::SBASE) % HangulData::TCOUNT) === 0;
}
/**
* Returns true when $codepoint is a Hangul leading consonant (choseong).
*
* Range: U+1100U+1112 (LCount = 19 entries).
*/
private function isLeadingConsonant(int $codepoint): bool
{
return $codepoint >= HangulData::LBASE && $codepoint < (HangulData::LBASE + HangulData::LCOUNT);
}
/**
* Returns true when $codepoint is a Hangul vowel (jungseong).
*
* Range: U+1161U+1175 (VCount = 21 entries).
*/
private function isVowel(int $codepoint): bool
{
return $codepoint >= HangulData::VBASE && $codepoint < (HangulData::VBASE + HangulData::VCOUNT);
}
/**
* Returns true when $codepoint is a Hangul trailing consonant (jongseong).
*
* Range: U+11A8U+11C2 (TCount 1 = 27 entries; TBase = U+11A7 is
* not itself a valid trailing consonant).
*/
private function isTrailingConsonant(int $codepoint): bool
{
return $codepoint > HangulData::TBASE && $codepoint < (HangulData::TBASE + HangulData::TCOUNT);
}
}
@@ -0,0 +1,65 @@
<?php
declare(strict_types=1);
/**
* Thai.php
*
* @since 2026-04-30
* @category Library
* @package Unicode
* @author Nicola Asuni <info@tecnick.com>
* @copyright 2011-2026 Nicola Asuni - Tecnick.com LTD
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
* @link https://github.com/tecnickcom/tc-lib-unicode
*
* This file is part of tc-lib-unicode software library.
*/
namespace Com\Tecnick\Unicode\Substitution;
/**
* Com\Tecnick\Unicode\Substitution\Thai
*
* Thai codepoints are returned unchanged.
*
* Thai preposed vowels (sara E, sara AE, sara O, sara AI) are the documented
* exception to the logical order of Unicode: they are stored before the consonant
* they follow in pronunciation and are displayed in that same position, so a
* left-to-right glyph stream needs no reordering.
*
* @since 2026-04-30
* @category Library
* @package Unicode
* @author Nicola Asuni <info@tecnick.com>
* @copyright 2011-2026 Nicola Asuni - Tecnick.com LTD
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
* @link https://github.com/tecnickcom/tc-lib-unicode
*/
final class Thai
{
/**
* Transformed codepoint array.
*
* @var list<int>
*/
private array $ordarr;
/**
* @param array<int, int> $ordarr Array of Unicode codepoints.
*/
public function __construct(array $ordarr)
{
$this->ordarr = array_values($ordarr);
}
/**
* Returns the codepoint array.
*
* @return list<int>
*/
public function getOrdarr(): array
{
return $this->ordarr;
}
}
+70
View File
@@ -0,0 +1,70 @@
<?php
declare(strict_types=1);
/**
* TextDirection.php
*
* @since 2026-07-17
* @category Library
* @package Unicode
* @author Nicola Asuni <info@tecnick.com>
* @copyright 2011-2026 Nicola Asuni - Tecnick.com LTD
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
* @link https://github.com/tecnickcom/tc-lib-unicode
*
* This file is part of tc-lib-unicode software library.
*/
namespace Com\Tecnick\Unicode;
/**
* Com\Tecnick\Unicode\TextDirection
*
* Backed enum for the forced paragraph direction accepted by Bidi. The backing
* value matches the normalized value stored internally: '' (auto detection),
* 'R' (force right-to-left) or 'L' (force left-to-right).
*
* @since 2026-07-17
* @category Library
* @package Unicode
* @author Nicola Asuni <info@tecnick.com>
* @copyright 2011-2026 Nicola Asuni - Tecnick.com LTD
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
* @link https://github.com/tecnickcom/tc-lib-unicode
*/
enum TextDirection: string
{
case Auto = '';
case Rtl = 'R';
case Ltr = 'L';
/**
* Resolve a loose forced-direction value to the matching enum case.
*
* Accepts an enum instance (returned unchanged) or a string. The string is
* interpreted exactly as Bidi always has: the empty string means auto, and
* otherwise the first character (case-insensitive) selects the direction;
* anything that is not R or L falls back to auto (never throws).
*
* @param string|self $value Forced direction identifier or enum case.
*/
public static function fromLoose(string|self $value): self
{
if ($value instanceof self) {
return $value;
}
if ($value === '') {
return self::Auto;
}
return match (\strtoupper($value[0])) {
'R' => self::Rtl,
'L' => self::Ltr,
default => self::Auto,
};
}
}
+306
View File
@@ -0,0 +1,306 @@
<?php
/**
* StepITest.php
*
* @since 2011-05-23
* @category Library
* @package Unicode
* @author Nicola Asuni <info@tecnick.com>
* @copyright 2011-2026 Nicola Asuni - Tecnick.com LTD
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
* @link https://github.com/tecnickcom/tc-lib-unicode
*
* This file is part of tc-lib-unicode software library.
*/
namespace Test\Bidi;
use Com\Tecnick\Unicode\Bidi\StepI;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
/**
* Bidi Test
*
* @since 2011-05-23
* @category Library
* @package Unicode
* @author Nicola Asuni <info@tecnick.com>
* @copyright 2011-2026 Nicola Asuni - Tecnick.com LTD
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
* @link https://github.com/tecnickcom/tc-lib-unicode
*/
class StepITest extends TestCase
{
/**
* @param array{
* 'e': int,
* 'edir': string,
* 'end': int,
* 'eos': string,
* 'length': int,
* 'maxlevel': int,
* 'sos': string,
* 'start': int,
* 'item': array<int, array{
* 'char': int,
* 'i': int,
* 'level': int,
* 'otype': string,
* 'pdimatch': int,
* 'pos': int,
* 'type': string,
* 'x': int,
* }>,
* } $seq Isolated Sequence array
*
* @param mixed $expected Expected result
*/
#[DataProvider('stepIDataProvider')]
public function testStepI(array $seq, mixed $expected): void
{
$stepi = new StepI($seq);
$this->assertEquals($expected, $stepi->getSequence());
}
/**
* @return array<int, array<int, array{
* 'e': int,
* 'edir': string,
* 'end': int,
* 'eos': string,
* 'length': int,
* 'maxlevel': int,
* 'sos': string,
* 'start': int,
* 'item': array<int, array{
* 'char': int,
* 'i': int,
* 'level': int,
* 'otype': string,
* 'pdimatch': int,
* 'pos': int,
* 'type': string,
* 'x': int,
* }>,
* }>>
*/
public static function stepIDataProvider(): array
{
return [
[
[
'e' => 0,
'edir' => 'L',
'end' => 3,
'eos' => 'L',
'length' => 4,
'maxlevel' => 0,
'sos' => 'L',
'start' => 0,
'item' => [
[
'char' => 65,
'i' => -1,
'level' => 0,
'otype' => 'L',
'pos' => 0,
'pdimatch' => -1,
'type' => 'L',
'x' => 0,
],
[
'char' => 8207,
'i' => -1,
'level' => 0,
'otype' => 'R',
'pos' => 1,
'pdimatch' => -1,
'type' => 'R',
'x' => 0,
],
[
'char' => 1632,
'i' => -1,
'level' => 0,
'otype' => 'AN',
'pos' => 2,
'pdimatch' => -1,
'type' => 'AN',
'x' => 0,
],
[
'char' => 1776,
'i' => -1,
'level' => 0,
'otype' => 'EN',
'pos' => 3,
'pdimatch' => -1,
'type' => 'EN',
'x' => 0,
],
],
],
[
'e' => 0,
'edir' => 'L',
'end' => 3,
'eos' => 'L',
'length' => 4,
'maxlevel' => 2,
'sos' => 'L',
'start' => 0,
'item' => [
[
'char' => 65,
'i' => -1,
'level' => 0,
'otype' => 'L',
'pos' => 0,
'pdimatch' => -1,
'type' => 'L',
'x' => 0,
],
[
'char' => 8207,
'i' => -1,
'level' => 1,
'otype' => 'R',
'pos' => 1,
'pdimatch' => -1,
'type' => 'R',
'x' => 0,
],
[
'char' => 1632,
'i' => -1,
'level' => 2,
'otype' => 'AN',
'pos' => 2,
'pdimatch' => -1,
'type' => 'AN',
'x' => 0,
],
[
'char' => 1776,
'i' => -1,
'level' => 2,
'otype' => 'EN',
'pos' => 3,
'pdimatch' => -1,
'type' => 'EN',
'x' => 0,
],
],
],
],
[
[
'e' => 1,
'edir' => 'R',
'end' => 3,
'eos' => 'R',
'length' => 4,
'maxlevel' => 0,
'sos' => 'R',
'start' => 0,
'item' => [
[
'char' => 65,
'i' => -1,
'level' => 1,
'otype' => 'L',
'pos' => 0,
'pdimatch' => -1,
'type' => 'L',
'x' => 0,
],
[
'char' => 8207,
'i' => -1,
'level' => 1,
'otype' => 'R',
'pos' => 1,
'pdimatch' => -1,
'type' => 'R',
'x' => 0,
],
[
'char' => 1632,
'i' => -1,
'level' => 1,
'otype' => 'AN',
'pos' => 2,
'pdimatch' => -1,
'type' => 'AN',
'x' => 0,
],
[
'char' => 1776,
'i' => -1,
'level' => 1,
'otype' => 'EN',
'pos' => 3,
'pdimatch' => -1,
'type' => 'EN',
'x' => 0,
],
],
],
[
'e' => 1,
'edir' => 'R',
'end' => 3,
'eos' => 'R',
'length' => 4,
'maxlevel' => 2,
'sos' => 'R',
'start' => 0,
'item' => [
[
'char' => 65,
'i' => -1,
'level' => 2,
'otype' => 'L',
'pos' => 0,
'pdimatch' => -1,
'type' => 'L',
'x' => 0,
],
[
'char' => 8207,
'i' => -1,
'level' => 1,
'otype' => 'R',
'pos' => 1,
'pdimatch' => -1,
'type' => 'R',
'x' => 0,
],
[
'char' => 1632,
'i' => -1,
'level' => 2,
'otype' => 'AN',
'pos' => 2,
'pdimatch' => -1,
'type' => 'AN',
'x' => 0,
],
[
'char' => 1776,
'i' => -1,
'level' => 2,
'otype' => 'EN',
'pos' => 3,
'pdimatch' => -1,
'type' => 'EN',
'x' => 0,
],
],
],
],
];
}
}
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,164 @@
<?php
/**
* BidiConformanceTest.php
*
* @since 2026-08-06
* @category Library
* @package Unicode
* @author Nicola Asuni <info@tecnick.com>
* @copyright 2011-2026 Nicola Asuni - Tecnick.com LTD
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
* @link https://github.com/tecnickcom/tc-lib-unicode
*
* This file is part of tc-lib-unicode software library.
*/
namespace Test;
use Com\Tecnick\Unicode\Bidi;
use Com\Tecnick\Unicode\Data\Mirror as UniMirror;
use PHPUnit\Framework\TestCase;
/**
* Runs the official BidiCharacterTest.txt conformance suite of the Unicode Character
* Database against the bidirectional algorithm.
*
* The data file is downloaded by "make ucd" into target/ucd/<version>/; the test is
* skipped when it is not available.
*
* @since 2026-08-06
* @category Library
* @package Unicode
* @author Nicola Asuni <info@tecnick.com>
* @copyright 2011-2026 Nicola Asuni - Tecnick.com LTD
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
* @link https://github.com/tecnickcom/tc-lib-unicode
*/
class BidiConformanceTest extends TestCase
{
/**
* Paragraph direction of field 1 of the data file.
*/
private const DIRECTION = [
'0' => 'L',
'1' => 'R',
'2' => '',
];
/**
* Known failures, as "<field 0>;<field 1>" of the data file, with the reason.
* A case listed here must fail: the test reports the entries that can be removed.
*
* @var array<string, string>
*/
private const KNOWN_FAILURES = [];
/**
* Maximum number of unexpected failures reported by the assertion message.
*/
private const MAX_REPORTED = 10;
public function testBidiCharacterTest(): void
{
$path = self::findDataFile();
if ($path === null) {
$this->markTestSkipped('BidiCharacterTest.txt not found: run "make ucd" to download it');
}
$handle = \fopen($path, 'r');
$this->assertIsResource($handle);
$total = 0;
$failures = [];
$fixed = [];
while (($line = \fgets($handle)) !== false) {
$line = \trim($line);
if ($line === '' || $line[0] === '#') {
continue;
}
$field = \explode(';', $line);
if (\count($field) < 5) {
continue;
}
++$total;
$key = $field[0] . ';' . ($field[1] ?? '');
$passed = self::runCase($field);
if (isset(self::KNOWN_FAILURES[$key])) {
if ($passed) {
$fixed[] = $key;
}
continue;
}
if (!$passed) {
$failures[] = $line;
}
}
\fclose($handle);
$this->assertGreaterThan(90_000, $total, 'unexpected number of conformance cases');
$this->assertSame([], $fixed, 'these cases now pass and must be removed from KNOWN_FAILURES');
$this->assertSame(
[],
\array_slice($failures, 0, self::MAX_REPORTED),
\sprintf('%d of %d conformance cases failed', \count($failures), $total),
);
}
/**
* Runs a single data file line: codepoints, paragraph direction, paragraph level,
* resolved levels and visual order.
*
* @param array<int, string> $field Fields of the data file line
*/
private static function runCase(array $field): bool
{
$codes = \trim($field[0] ?? '');
$direction = \trim($field[1] ?? '');
$levelStr = \trim($field[3] ?? '');
$orderStr = \trim($field[4] ?? '');
$ordarr = \array_map(static fn(string $hex): int => (int) \hexdec($hex), \explode(' ', $codes));
$levels = \explode(' ', $levelStr);
$order = $orderStr === '' ? [] : \array_map('intval', \explode(' ', $orderStr));
// The data file lists the reordered indexes without applying rule L4, which the
// library does apply, so the mirrored form is used for the odd levels.
$expected = [];
foreach ($order as $idx) {
$ord = $ordarr[$idx] ?? 0;
$expected[] = ((int) ($levels[$idx] ?? 0) % 2) === 1 ? UniMirror::UNI[$ord] ?? $ord : $ord;
}
try {
$bidi = new Bidi(null, null, $ordarr, self::DIRECTION[$direction] ?? '', false);
$result = \array_values($bidi->getOrdArray());
} catch (\Throwable) {
return false;
}
return $result === $expected;
}
/**
* Returns the path of the most recent BidiCharacterTest.txt available, or null.
*/
private static function findDataFile(): ?string
{
$found = \glob(__DIR__ . '/../target/ucd/*/BidiCharacterTest.txt');
if ($found === false || $found === []) {
return null;
}
\sort($found);
return \end($found);
}
}
+549
View File
@@ -0,0 +1,549 @@
<?php
/**
* BidiTest.php
*
* @since 2011-05-23
* @category Library
* @package Unicode
* @author Nicola Asuni <info@tecnick.com>
* @copyright 2011-2026 Nicola Asuni - Tecnick.com LTD
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
* @link https://github.com/tecnickcom/tc-lib-unicode
*
* This file is part of tc-lib-unicode software library.
*/
namespace Test;
use Com\Tecnick\Unicode\Bidi;
use PHPUnit\Framework\Attributes\DataProvider;
/**
* Bidi Test
*
* @since 2011-05-23
* @category Library
* @package Unicode
* @author Nicola Asuni <info@tecnick.com>
* @copyright 2011-2026 Nicola Asuni - Tecnick.com LTD
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
* @link https://github.com/tecnickcom/tc-lib-unicode
*/
class BidiTest extends TestUtil
{
private static function decodeJsonString(string $json): string
{
/** @var string */
return \json_decode($json);
}
/**
* @throws \Com\Tecnick\Unicode\Exception
*/
public function testException(): void
{
$this->bcExpectException(\Com\Tecnick\Unicode\Exception::class);
new \Com\Tecnick\Unicode\Bidi();
}
/**
* @param ?string $str String to convert (if null it will be generated from $chrarr or $ordarr)
* @param ?array<string> $chrarr Array of UTF-8 chars (if empty it will be generated from $str or $ordarr)
* @param ?array<int> $ordarr Array of UTF-8 codepoints (if empty it will be generated from $str or $chrarr)
* @param string $forcedir If 'R' forces RTL, if 'L' forces LTR
* @param bool $shaping If true enable the shaping algorithm
*
* @throws \Com\Tecnick\Unicode\Exception
*/
#[DataProvider('inputDataProvider')]
public function testStr(
?string $str = null,
?array $chrarr = null,
?array $ordarr = null,
string $forcedir = '',
bool $shaping = true,
): void {
$bidi = new Bidi($str, $chrarr, $ordarr, $forcedir, $shaping);
$this->assertEquals('test', $bidi->getString());
$this->assertEquals(['t', 'e', 's', 't'], $bidi->getChrArray());
$this->assertEquals([116, 101, 115, 116], $bidi->getOrdArray());
$this->assertEquals(
[
116 => true,
101 => true,
115 => true,
],
$bidi->getCharKeys(),
);
$this->assertEquals(4, $bidi->getNumChars());
}
/**
* @return array<int, array{?string, ?array<string>, ?array<int>, string, bool}>
*/
public static function inputDataProvider(): array
{
return [
['test', null, null, '', true],
[null, ['t', 'e', 's', 't'], null, '', true],
[null, null, [116, 101, 115, 116], '', true],
['test', ['t', 'e', 's', 't'], null, '', true],
['test', null, [116, 101, 115, 116], '', true],
[null, ['t', 'e', 's', 't'], [116, 101, 115, 116], '', true],
['test', ['t', 'e', 's', 't'], [116, 101, 115, 116], '', true],
['test', null, null, 'L', true],
['test', null, null, 'R', true],
];
}
/**
* @throws \Com\Tecnick\Unicode\Exception
*/
#[DataProvider('bidiStrDataProvider')]
public function testBidiStr(string $str, mixed $expected, string $forcedir = ''): void
{
$bidi = new Bidi($str, null, null, $forcedir, true);
$this->assertEquals($expected, $bidi->getString());
}
/**
* @return array<int, array{string, string, string}>
*/
public static function bidiStrDataProvider(): array
{
return [
[
"\n\nABC\nEFG\n\nHIJ\n\n",
"\n\nABC\nEFG\n\nHIJ\n\n",
'L',
],
[
self::decodeJsonString('"\u202EABC\u202C"'),
'CBA',
'',
],
[
// All-L text in a forced-RTL paragraph: N1 resolves the spaces to L,
// so the whole phrase is a single LTR run and keeps its word order.
'left to right',
'left to right',
'R',
],
[
// Same, but L1.4 resets the trailing space to the paragraph level,
// moving it to the visual left.
'left to right ',
' left to right',
'R',
],
[
// U+0600..U+0602 are Arabic numbers (AN): I1 puts them on an even level,
// so they keep their logical order inside the right-to-left paragraph.
self::decodeJsonString('"smith (fabrikam \u0600\u0601\u0602) \u05de\u05d6\u05dc"'),
self::decodeJsonString('"\u05dc\u05d6\u05de (\u0600\u0601\u0602 fabrikam) smith"'),
'R',
],
[
self::decodeJsonString('"\u0600\u0601\u0602 book(s)"'),
self::decodeJsonString('"book(s) \u0600\u0601\u0602"'),
'R',
],
[
self::decodeJsonString('"\u0600\u0601(\u0602\u0603[&ef]!)gh"'),
self::decodeJsonString('"gh(![ef&]\u0602\u0603)\u0600\u0601"'),
'R',
],
[
'تشكيل اختبار',
'ﺭﺎﺒﺘﺧﺍ ﻞﻴﻜﺸﺗ',
'',
],
[
self::decodeJsonString('"\u05de\u05d6\u05dc \u05d8\u05d5\u05d1"'),
self::decodeJsonString('"\u05d1\u05d5\u05d8 \u05dc\u05d6\u05de"'),
'',
],
[
self::decodeJsonString(
'"\u0644\u0644\u0647 \u0600\u0601\u0602 \uFB50'
. ' \u0651\u064c\u0651\u064d\u0651\u064e\u0651\u064f\u0651\u0650'
. ' \u0644\u0622"',
),
// "\u0644\u0644\u0647" is not the word Allah: without the alef it shapes as
// lam initial, lam medial and heh final instead of the U+FDF2 ligature.
self::decodeJsonString(
'"\ufef5 \ufc62\ufc61\ufc60\ufc5f\ufc5e \ufb50 \u0600\u0601\u0602 \ufeea\ufee0\ufedf"',
),
'',
],
[
self::decodeJsonString('"A\u2067\u05d8\u2069B"'),
self::decodeJsonString('"A\u2067\u05d8\u2069B"'),
'',
],
[
// Unterminated isolate: RLI with no matching PDI. Exercises StepXten's
// findMatchingPdiStart() returning -1 and the eos-from-paragraph-level fallback.
self::decodeJsonString('"\u05d0\u2067\u05d1"'),
self::decodeJsonString('"\u05d1\u2067\u05d0"'),
'',
],
[
// Unterminated FSI wrapping LTR text: covers FSI auto-direction with no matching PDI.
self::decodeJsonString('"\u05d0\u2068ab\u05d1"'),
self::decodeJsonString('"ab\u05d1\u2068\u05d0"'),
'',
],
[
// RLI + PDI
self::decodeJsonString('"The words \"\u2067\u05de\u05d6\u05dc [mazel] \u05d8\u05d5\u05d1 [tov]\u2069\"'
. ' mean \"Congratulations!\""'),
'The words "[tov] בוט [mazel] לזמ⁩" mean "Congratulations!"',
'',
],
[
// RLE + PDF. The all-L phrase inside the embedding stays one LTR run,
// and the legacy embedding spills over: sos of the run after PDF is R, so N1
// resolves '" - ' between it and the number to R, pulling '" - $19.95' into
// the RTL context (the spillover problem that isolates were made to solve).
self::decodeJsonString('"it is called \"\u202bAN INTRODUCTION TO java\u202c\" - $19.95 in hardcover."'),
'it is called "$19.95 - "AN INTRODUCTION TO java in hardcover.',
'',
],
[
// RLI + PDI: the isolate shields the surrounding text, so unlike the RLE
// case above the quote and price stay in place and the phrase keeps its
// word order.
self::decodeJsonString('"it is called \"\u2067AN INTRODUCTION TO java\u2069\" - $19.95 in hardcover."'),
'it is called "AN INTRODUCTION TO java" - $19.95 in hardcover.',
'',
],
[
// Hebrew with embedded paragraph separator (covers getParagraphs() splitting and re-insertion)
self::decodeJsonString('"\u05de\u05d6\u05dc \u05d8\u05d5\u05d1"')
. "\n"
. self::decodeJsonString('"\u05de\u05d6\u05dc \u05d8\u05d5\u05d1"'),
self::decodeJsonString('"\u05d1\u05d5\u05d8 \u05dc\u05d6\u05de"')
. "\n"
. self::decodeJsonString('"\u05d1\u05d5\u05d8 \u05dc\u05d6\u05de"'),
'',
],
[
// Hebrew ending with paragraph separator (covers empty last paragraph handling)
self::decodeJsonString('"\u05de\u05d6\u05dc \u05d8\u05d5\u05d1"') . "\n",
self::decodeJsonString('"\u05d1\u05d5\u05d8 \u05dc\u05d6\u05de"') . "\n",
'',
],
[
// Arabic with forced LTR direction (covers getPel() returning 0 for forcedir='L').
// N1 resolves the space between the two R words, so the phrase is a single RTL
// run inside the LTR paragraph and is reversed as a unit (words swap places).
'تشكيل اختبار',
self::decodeJsonString('"\ufead\ufe8e\ufe92\ufe98\ufea7\ufe8d\u0020\ufede\ufef4\ufedc\ufeb8\ufe97"'),
'L',
],
];
}
/**
* Regression test for https://github.com/tecnickcom/tc-lib-unicode/issues/12
* Arabic shaping must replace a lam-alef pair with a single ligature glyph
* without deleting any other character of the run.
*
* @param array<int> $expected Shaped codepoints in visual order
*
* @throws \Com\Tecnick\Unicode\Exception
*/
#[DataProvider('lamAlefShapingDataProvider')]
public function testLamAlefShaping(string $str, array $expected): void
{
$bidi = new Bidi($str);
$this->assertSame($expected, $bidi->getOrdArray());
}
/**
* @return array<int, array{string, array<int>}>
*/
public static function lamAlefShapingDataProvider(): array
{
return [
// khah + lam + alef + lam: the pair merges and the khah keeps its initial form
['خلال', [0xFEDD, 0xFEFC, 0xFEA7]],
// nine letters with one lam-alef pair: one glyph less, leading alef preserved
['الاستخدام', [0xFEE1, 0xFE8D, 0xFEAA, 0xFEA8, 0xFE98, 0xFEB3, 0xFEFB, 0xFE8D]],
// two adjacent lam-alef pairs merge independently
['لالا', [0xFEFB, 0xFEFB]],
// both words shape identically (the second merge must not delete the first char)
['خلال خلال', [0xFEDD, 0xFEFC, 0xFEA7, 0x0020, 0xFEDD, 0xFEFC, 0xFEA7]],
// NSM between the pair: the ligature still forms around the shadda
['لّا', [0xFEFB, 0x0651]],
];
}
/**
* The Arabic shaper picks the presentation form from the Joining_Type of the
* surrounding characters: transparent marks are skipped, ZWJ joins, ZWNJ and every
* non-joining character break the connection.
*
* @param array<int> $expected Shaped codepoints in visual order
*
* @throws \Com\Tecnick\Unicode\Exception
*/
#[DataProvider('joiningShapingDataProvider')]
public function testJoiningShaping(string $str, array $expected): void
{
$bidi = new Bidi($str);
$this->assertSame($expected, $bidi->getOrdArray());
}
/**
* @return array<string, array{string, array<int>}>
*/
public static function joiningShapingDataProvider(): array
{
return [
// ZWNJ (U+200C) breaks the connection between two dual-joining letters
'beh ZWNJ teh' => ["\u{0628}\u{200C}\u{062A}", [0xFE95, 0xFE8F]],
// and it also blocks the lam-alef ligature
'lam ZWNJ alef' => ["\u{0644}\u{200C}\u{0627}", [0xFE8D, 0xFEDD]],
// ZWJ (U+200D) is join causing: the beh takes its initial form
'beh ZWJ' => ["\u{0628}\u{200D}", [0xFE91]],
// Persian needs ZWNJ inside a word
'persian mikhaham' => [
'می‌خواهم',
[0xFEE2, 0xFEEB, 0xFE8D, 0xFEEE, 0xFEA7, 0xFBFD, 0xFEE3],
],
// a non-joining character between two letters isolates both
'beh comma teh' => ['ب،ت', [0xFE95, 0x060C, 0xFE8F]],
'beh digit teh' => ['ب١ت', [0xFE95, 0x0661, 0xFE8F]],
// letters with an isolated and a final form only
'waw with hamza' => ['سؤال', [0xFEDD, 0xFE8D, 0xFE86, 0xFEB3]],
'teh marbuta' => ['بةب', [0xFE8F, 0xFE94, 0xFE91]],
// right joining letters do not connect to the following letter
'urdu rreh' => ['بڑا', [0xFE8D, 0xFB8D, 0xFE91]],
'alef wasla' => ['ٱلحمد', [0xFEAA, 0xFEE4, 0xFEA4, 0xFEDF, 0xFB50]],
'uyghur' => ['ئۇيغۇر', [0xFEAD, 0xFBD8, 0xFED0, 0xFEF3, 0xFBD8, 0xFE8B]],
// non-joining letters and punctuation are not a joining context
'hamza' => ['شيء', [0xFE80, 0xFEF2, 0xFEB7]],
'arabic semicolon' => ['ب؛', [0x061B, 0xFE8F]],
'urdu full stop' => ['اب۔', [0x06D4, 0xFE8F, 0xFE8D]],
'arabic question mark' => ['ب؟', [0x061F, 0xFE8F]],
'two beh and question mark' => ['بب؟', [0x061F, 0xFE90, 0xFE91]],
];
}
/**
* Shadda (U+0651) and a second mark are merged into a single glyph in both orders:
* canonical ordering puts the vowel first, as its combining class is lower.
*
* @throws \Com\Tecnick\Unicode\Exception
*/
public function testCombineShadda(): void
{
// BEH + SHADDA + FATHA and BEH + FATHA + SHADDA
$bidi = new Bidi(null, null, [0x0628, 0x0651, 0x064E]);
$this->assertSame([0xFC60, 0xFE8F], $bidi->getOrdArray());
$bidi = new Bidi(null, null, [0x0628, 0x064E, 0x0651]);
$this->assertSame([0xFC60, 0xFE8F], $bidi->getOrdArray());
// BEH + SHADDA + SUPERSCRIPT ALEF
$bidi = new Bidi(null, null, [0x0628, 0x0651, 0x0670]);
$this->assertSame([0xFC63, 0xFE8F], $bidi->getOrdArray());
}
/**
* The U+FDF2 ligature covers alef + lam + lam + heh and has an isolated form only.
*
* @param array<int> $expected Shaped codepoints in visual order
*
* @throws \Com\Tecnick\Unicode\Exception
*/
#[DataProvider('allahShapingDataProvider')]
public function testAllahShaping(string $str, array $expected): void
{
$bidi = new Bidi($str);
$this->assertSame($expected, $bidi->getOrdArray());
}
/**
* @return array<string, array{string, array<int>}>
*/
public static function allahShapingDataProvider(): array
{
return [
// the four characters become one glyph
'allah' => ['الله', [0xFDF2]],
// the combining marks are transparent and are kept after the ligature
'vocalized allah' => ['اللّٰه', [0xFDF2, 0xFC63]],
// without the alef the word is shaped letter by letter
'lillah' => ['لله', [0xFEEA, 0xFEE0, 0xFEDF]],
'three lam and heh' => ['للله', [0xFEEA, 0xFEE0, 0xFEE0, 0xFEDF]],
// the alef joins the beh, so the isolated ligature cannot be used
'billah' => ['بالله', [0xFEEA, 0xFEE0, 0xFEDF, 0xFE8E, 0xFE91]],
];
}
/**
* Regression test for https://github.com/tecnickcom/tc-lib-unicode/issues/13
* Rules N1/N2 must resolve ordinary neutrals (WS, ON, S), not only the literal
* 'NI'-typed isolate formatting characters: a neutral between two strong characters
* of the same direction takes that direction, keeping multi-word runs together.
*
* @throws \Com\Tecnick\Unicode\Exception
*/
#[DataProvider('neutralResolutionDataProvider')]
public function testNeutralResolution(string $str, string $expected, string $forcedir): void
{
$bidi = new Bidi($str, null, null, $forcedir, false);
$this->assertSame($expected, $bidi->getString());
}
/**
* @return array<int, array{string, string, string}>
*/
public static function neutralResolutionDataProvider(): array
{
// The issue's third case (all-Latin text forced RTL) is covered by
// bidiStrDataProvider: shaping is a no-op for ASCII input.
return [
// RTL paragraph with a two-word Latin phrase: the phrase keeps its word order
['تجربة - John Doe', 'John Doe - ةبرجت', ''],
// LTR paragraph with a two-word Arabic phrase: reversed as a single run
['John Doe - تجربة خاصة', 'John Doe - ةصاخ ةبرجت', ''],
];
}
/**
* Test Bidi with edge-case ordarr inputs: a negative codepoint and a Private Use Area
* codepoint as the last character of the paragraph.
*
* @throws \Com\Tecnick\Unicode\Exception
*/
public function testBidiWithSpecialOrdarr(): void
{
// Negative codepoint as last char: covers the $lastchar < 0 branch
$bidi1 = new \Com\Tecnick\Unicode\Bidi(null, null, [0x05D0, -1], 'R', false);
$this->assertEquals([-1, 1488], $bidi1->getOrdArray());
// Codepoint 0xE001 (Private Use Area): type L, so it forms its own left-to-right run
$bidi2 = new \Com\Tecnick\Unicode\Bidi(null, null, [0x05D0, 0xE001], 'R', false);
$this->assertEquals([57345, 1488], $bidi2->getOrdArray());
}
/**
* X9 removes the explicit formatting characters also when the text holds no
* right-to-left character, so the left-to-right fast path cannot return them.
*
* @throws \Com\Tecnick\Unicode\Exception
*/
public function testFormattingCharactersAreRemoved(): void
{
$bidi = new Bidi(self::decodeJsonString('"a\u202Ab\u202Cc"'));
$this->assertSame('abc', $bidi->getString());
$bidi = new Bidi(self::decodeJsonString('"a\u2066b\u2069c"'));
$this->assertSame(
self::decodeJsonString('"a\u2066b\u2069c"'),
$bidi->getString(),
'the isolate formatting characters are retained',
);
}
/**
* The string, char array and codepoint array forms of the input must describe the
* same text: a different number of characters is an input error.
*
* @throws \Com\Tecnick\Unicode\Exception
*/
public function testMismatchedInputForms(): void
{
$this->bcExpectException(\Com\Tecnick\Unicode\Exception::class);
new Bidi('test', null, [0x05D0]);
}
/**
* Conformance repros taken from the official BidiCharacterTest.txt suite, checked
* against the codepoint sequence the UBA produces (shaping disabled).
*
* @param array<int> $ordarr Input codepoints
* @param array<int> $expected Expected codepoints in visual order
*
* @throws \Com\Tecnick\Unicode\Exception
*/
#[DataProvider('ubaOrdArrDataProvider')]
public function testUbaOrdArr(array $ordarr, string $forcedir, array $expected): void
{
$bidi = new Bidi(null, null, $ordarr, $forcedir, false);
$this->assertSame($expected, \array_values($bidi->getOrdArray()));
}
/**
* @return array<string, array{array<int>, string, array<int>}>
*/
public static function ubaOrdArrDataProvider(): array
{
return [
// X5c: the direction of an FSI comes from the content between it and its
// matching PDI, so an FSI wrapping Hebrew text acts as an RLI.
'FSI with RTL content' => [
[0x2068, 0x05D0, 0x0021, 0x2069, 0x0061, 0x0062],
'',
[0x2068, 0x0021, 0x05D0, 0x2069, 0x0061, 0x0062],
],
// X5c with no matching PDI: the scan runs to the end of the paragraph.
'unterminated FSI with RTL content' => [
[0x2068, 0x05D0, 0x0061],
'',
[0x2068, 0x0061, 0x05D0],
],
// X6a: a PDI matching no isolate initiator keeps the level of the current stack
// entry and is retained by X9 instead of being deleted.
'unmatched PDI is retained' => [
[0x05D0, 0x2069, 0x05D1],
'',
[0x05D1, 0x2069, 0x05D0],
],
// W4: a European separator only joins two European numbers, so the plus sign
// between two Arabic-Indic digits stays a separator and N1 resolves it as R.
'ES does not join two Arabic numbers' => [
[0x0660, 0x002B, 0x0661],
'R',
[0x0661, 0x002B, 0x0660],
],
// Same rule with a hyphen between two European numbers resolved to AN by W2.
'ES after an Arabic letter' => [
[0x0028, 0x0627, 0x0029, 0x0020, 0x0031, 0x002D, 0x0032],
'L',
[0x0028, 0x0627, 0x0029, 0x0020, 0x0032, 0x002D, 0x0031],
],
// BD14/BD15: a bracket whose current type is not ON (here retyped by an
// override) is not part of a bracket pair.
'brackets retyped by an override do not pair' => [
[0x202E, 0x0028, 0x202C, 0x202B, 0x05D0, 0x0062, 0x0029, 0x0063, 0x202C],
'R',
[0x0062, 0x0029, 0x0063, 0x05D0, 0x0029],
],
];
}
/**
* BD16 uses a fixed 63 element stack: when a 64th opening bracket is found, bracket
* pairing stops for the remainder of the isolating run sequence and the pairs found
* so far are kept.
*
* @throws \Com\Tecnick\Unicode\Exception
*/
public function testBracketStackLimit(): void
{
$ordarr = \array_merge([0x0061], \array_fill(0, 64, 0x0028), [0x0062], \array_fill(0, 64, 0x0029));
// None of the 64 pairs is resolved to L by N0, so N1 leaves the brackets at the
// paragraph level: the whole line is reversed and every bracket is mirrored.
$expected = \array_merge(\array_fill(0, 64, 0x0028), [0x0061], \array_fill(0, 64, 0x0028), [0x0062]);
$bidi = new Bidi(null, null, $ordarr, 'R', false);
$this->assertSame($expected, \array_values($bidi->getOrdArray()));
}
}
+322
View File
@@ -0,0 +1,322 @@
<?php
/**
* ConvertTest.php
*
* @since 2011-05-23
* @category Library
* @package Unicode
* @author Nicola Asuni <info@tecnick.com>
* @copyright 2011-2026 Nicola Asuni - Tecnick.com LTD
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
* @link https://github.com/tecnickcom/tc-lib-unicode
*
* This file is part of tc-lib-unicode software library.
*/
namespace Test;
use Com\Tecnick\Unicode\Data\Latin;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
/**
* Convert Test
*
* @since 2011-05-23
* @category Library
* @package Unicode
* @author Nicola Asuni <info@tecnick.com>
* @copyright 2011-2026 Nicola Asuni - Tecnick.com LTD
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
* @link https://github.com/tecnickcom/tc-lib-unicode
*/
class ConvertTest extends TestCase
{
protected function getTestObject(): \Com\Tecnick\Unicode\Convert
{
return new \Com\Tecnick\Unicode\Convert();
}
private static function decodeJsonString(string $json): string
{
/** @var string */
return \json_decode($json);
}
/**
* @throws \Com\Tecnick\Unicode\Exception
*/
#[DataProvider('chrDataProvider')]
public function testChr(int $ord, mixed $expected): void
{
$convert = $this->getTestObject();
$chr = $convert->chr($ord);
$this->assertEquals($expected, $chr);
}
/**
* @throws \Com\Tecnick\Unicode\Exception
*/
#[DataProvider('chrDataProvider')]
public function testOrd(mixed $expected, string $chr): void
{
$convert = $this->getTestObject();
$ord = $convert->ord($chr);
$this->assertEquals($expected, $ord);
}
/**
* @return array<int, array{0:int,1:string}>
*/
public static function chrDataProvider(): array
{
return [
[32, ' '],
[48, '0'],
[65, 'A'],
[182, '¶'],
[255, 'ÿ'],
[256, 'Ā'],
[544, 'Ƞ'],
[916, 'Δ'],
[1488, 'א'],
[21488, '台'],
[49436, '서'],
[70039, '𑆗'],
[195101, '𪘀'],
];
}
/**
* @throws \Com\Tecnick\Unicode\Exception
*/
public function testStrToChrArr(): void
{
$convert = $this->getTestObject();
$res = $convert->strToChrArr('0A¶ÿĀȠΔא台서');
$this->assertEquals(['0', 'A', '¶', 'ÿ', 'Ā', 'Ƞ', 'Δ', 'א', '台', '서'], $res);
}
/**
* @throws \Com\Tecnick\Unicode\Exception
*/
public function testChrArrToOrdArr(): void
{
$convert = $this->getTestObject();
$res = $convert->chrArrToOrdArr(['0', 'A', '¶', 'ÿ', 'Ā', 'Ƞ', 'Δ', 'א', '台', '서']);
$this->assertEquals([48, 65, 182, 255, 256, 544, 916, 1488, 21488, 49436], $res);
}
/**
* @throws \Com\Tecnick\Unicode\Exception
*/
public function testOrdArrToChrArr(): void
{
$convert = $this->getTestObject();
$res = $convert->ordArrToChrArr([48, 65, 182, 255, 256, 544, 916, 1488, 21488, 49436]);
$this->assertEquals(['0', 'A', '¶', 'ÿ', 'Ā', 'Ƞ', 'Δ', 'א', '台', '서'], $res);
}
/**
* @throws \Com\Tecnick\Unicode\Exception
*/
public function testStrToOrdArr(): void
{
$convert = $this->getTestObject();
$res = $convert->strToOrdArr('0A¶ÿĀȠΔא台서');
$this->assertEquals([48, 65, 182, 255, 256, 544, 916, 1488, 21488, 49436], $res);
}
/**
* Empty inputs must short-circuit to an empty array in every direction.
*
* @throws \Com\Tecnick\Unicode\Exception
*/
public function testConvertArrEmpty(): void
{
$convert = $this->getTestObject();
$this->assertSame([], $convert->strToOrdArr(''));
$this->assertSame([], $convert->ordArrToChrArr([]));
$this->assertSame([], $convert->chrArrToOrdArr([]));
}
/**
* Supplementary-plane (4-byte) code points must round-trip through every conversion.
*
* @throws \Com\Tecnick\Unicode\Exception
*/
public function testConvertArrSupplementaryPlane(): void
{
$convert = $this->getTestObject();
// Includes two supplementary-plane (4-byte UTF-8) code points: U+11197 and U+2A600.
$str = "A\u{11197}\u{00B6}\u{2A600}\u{53F0}";
$ords = [65, 70039, 182, 173568, 21488];
$chrs = ['A', "\u{11197}", "\u{00B6}", "\u{2A600}", "\u{53F0}"];
$this->assertSame($ords, $convert->strToOrdArr($str));
$this->assertSame($chrs, $convert->ordArrToChrArr($ords));
$this->assertSame($ords, $convert->chrArrToOrdArr($chrs));
$this->assertSame($str, \implode('', $convert->ordArrToChrArr($convert->strToOrdArr($str))));
}
/**
* Malformed UTF-8 raises an exception, like strToChrArr(): a byte sequence that is not
* valid UTF-8 is an input error, while an invalid code point is substituted with '?'.
*
* @throws \Com\Tecnick\Unicode\Exception
*/
public function testStrToOrdArrRejectsMalformed(): void
{
$this->expectException(\Com\Tecnick\Unicode\Exception::class);
$convert = $this->getTestObject();
$convert->strToOrdArr("\xff\xfe");
}
/**
* Code points that cannot be encoded (negative, surrogate or above U+10FFFF) are
* substituted with '?'.
*
* @throws \Com\Tecnick\Unicode\Exception
*/
public function testOrdArrToChrArrSubstitutesInvalid(): void
{
$convert = $this->getTestObject();
$this->assertSame(['?', '?', '?', 'A'], $convert->ordArrToChrArr([-1, 0xD800, 0x110000, 0x41]));
}
public function testGetSubUniArrStr(): void
{
$convert = $this->getTestObject();
$res = $convert->getSubUniArrStr(['0', 'A', '¶', 'ÿ', 'Ā', 'Ƞ', 'Δ', 'א', '台', '서']);
$this->assertEquals('0A¶ÿĀȠΔא台서', $res);
$res = $convert->getSubUniArrStr(['0', 'A', '¶', 'ÿ', 'Ā', 'Ƞ', 'Δ', 'א', '台', '서'], 2, 8);
$this->assertEquals('¶ÿĀȠΔא', $res);
}
public function testUniArrToLatinArr(): void
{
$convert = $this->getTestObject();
$uniarr = \array_keys(Latin::SUBSTITUTE);
$uniarr[] = 65533; // 0xFFFD - character to ignore
$uniarr[] = 123456; // undefined char
$uniarr[] = 65; // ASCII char
$latarr = \array_values(Latin::SUBSTITUTE);
$latarr[] = 63;
$latarr[] = 65;
$res = $convert->uniArrToLatinArr($uniarr);
$this->assertEquals($latarr, $res);
}
public function testLatinArrToStr(): void
{
$convert = $this->getTestObject();
$res = $convert->latinArrToStr([48, 57, 65, 90, 97, 122]);
$this->assertEquals('09AZaz', $res);
}
#[DataProvider('strToHexDataProvider')]
public function testStrToHex(string $str, mixed $hex): void
{
$convert = $this->getTestObject();
$res = $convert->strToHex($str);
$this->assertEquals($hex, $res);
}
#[DataProvider('strToHexDataProvider')]
public function testHexToStr(mixed $str, string $hex): void
{
$convert = $this->getTestObject();
$res = $convert->hexToStr($hex);
$this->assertEquals($str, $res);
}
/**
* @return array<int, array<string>>
*/
public static function strToHexDataProvider(): array
{
return [
['', ''],
['A', '41'],
['AB', '4142'],
['ABC', '414243'],
["\n", '0a'],
];
}
#[DataProvider('toUTF16BEDataProvider')]
public function testToUTF16BE(string $str, mixed $exp): void
{
$convert = $this->getTestObject();
$res = $convert->toUTF16BE($str);
$this->assertEquals($exp, $convert->strToHex($res));
}
/**
* @return array<int, array<string>>
*/
public static function toUTF16BEDataProvider(): array
{
return [
['', ''],
['ABC', '004100420043'],
[self::decodeJsonString('"\u0010\uffff\u00ff\uff00"'), '0010ffff00ffff00'],
];
}
#[DataProvider('toUTF8DataProvider')]
public function testToUTF8(string $str, mixed $exp, ?string $enc = null): void
{
$convert = $this->getTestObject();
$res = $convert->toUTF8($str, $enc);
$this->assertEquals($exp, $res);
}
/**
* @return array<int, array<string>>
*/
public static function toUTF8DataProvider(): array
{
return [
['', ''],
['òèìòù', 'òèìòù'],
['òèìòù', 'òèìòù', 'ISO-8859-1'],
];
}
public function testToUTF8WithUndetectableEncoding(): void
{
// BASE64 and HTML-ENTITIES cannot detect a plain ASCII string,
// causing mb_detect_encoding() to return false; the method must
// handle this gracefully by falling back to a null source encoding.
$convert = $this->getTestObject();
$res = $convert->toUTF8('abc', ['BASE64', 'HTML-ENTITIES']);
$this->assertEquals('abc', $res);
}
/**
* @throws \Com\Tecnick\Unicode\Exception
*/
public function testOrdException(): void
{
$this->expectException(\Com\Tecnick\Unicode\Exception::class);
$convert = $this->getTestObject();
// An empty string produces a zero-length UCS-4BE buffer,
// making unpack() fail and triggering the exception.
$convert->ord('');
}
/**
* @throws \Com\Tecnick\Unicode\Exception
*/
public function testStrToChrArrException(): void
{
$this->expectException(\Com\Tecnick\Unicode\Exception::class);
$convert = $this->getTestObject();
// Invalid UTF-8 bytes cause preg_split() with the /u flag to return
// false, triggering the exception.
$convert->strToChrArr("\xff\xfe");
}
}
@@ -0,0 +1,187 @@
<?php
/**
* DevanagariTest.php
*
* @since 2026-04-30
* @category Library
* @package Unicode
* @author Nicola Asuni <info@tecnick.com>
* @copyright 2011-2026 Nicola Asuni - Tecnick.com LTD
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
* @link https://github.com/tecnickcom/tc-lib-unicode
*
* This file is part of tc-lib-unicode software library.
*/
namespace Test\Substitution;
use Com\Tecnick\Unicode\Substitution\Devanagari;
use PHPUnit\Framework\Attributes\DataProvider;
use Test\TestUtil;
/**
* Devanagari substitution test
*
* @since 2026-04-30
* @category Library
* @package Unicode
* @author Nicola Asuni <info@tecnick.com>
* @copyright 2011-2026 Nicola Asuni - Tecnick.com LTD
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
* @link https://github.com/tecnickcom/tc-lib-unicode
*/
class DevanagariTest extends TestUtil
{
/**
* @param array<int, int> $input
* @param array<int, int> $expected
*/
#[DataProvider('devanagariDataProvider')]
public function testGetOrdarr(array $input, array $expected): void
{
$obj = new Devanagari($input);
$this->assertSame($expected, $obj->getOrdarr());
}
/**
* @return array<string, array{0: array<int, int>, 1: array<int, int>}>
*/
public static function devanagariDataProvider(): array
{
// Codepoint reference:
// U+0915 KA U+0916 KHA U+0937 SHA U+0939 HA
// U+094D VIRAMA U+093F VOWEL SIGN I (left matra)
// U+0958 QA (first extended) U+095F YYA (last extended)
return [
// Empty input returns empty output
'empty' => [
[],
[],
],
// Pure ASCII: no Devanagari codepoints, pass through unchanged
'ascii_only' => [
[0x41, 0x42, 0x43],
[0x41, 0x42, 0x43],
],
// Single consonant: no matra follows, pass through unchanged
// U+0915 KA
'consonant_only' => [
[0x0915],
[0x0915],
],
// Last standard consonant: U+0939 HA
'last_standard_consonant' => [
[0x0939],
[0x0939],
],
// First extended consonant: U+0958 QA, unchanged (no matra)
'first_extended_consonant' => [
[0x0958],
[0x0958],
],
// Last extended consonant: U+095F YYA, unchanged (no matra)
'last_extended_consonant' => [
[0x095F],
[0x095F],
],
// Orphaned left matra at start (no preceding consonant): unchanged
// U+093F alone
'orphaned_left_matra' => [
[0x093F],
[0x093F],
],
// Left matra followed by consonant (orphaned leading matra):
// the matra is not recognised as following a cluster, unchanged
// U+093F, U+0915
'matra_then_consonant_no_reorder' => [
[0x093F, 0x0915],
[0x093F, 0x0915],
],
// Simple reposition: U+0915 KA + U+093F → U+093F, U+0915
'ka_with_i_vowel' => [
[0x0915, 0x093F],
[0x093F, 0x0915],
],
// U+094E PRISHTHAMATRA E is the second vowel sign of Indic
// Positional Category Left: U+0915 KA + U+094E
'ka_with_prishthamatra_e' => [
[0x0915, 0x094E],
[0x094E, 0x0915],
],
// Last standard consonant + left matra: U+0939 HA + U+093F
'ha_with_i_vowel' => [
[0x0939, 0x093F],
[0x093F, 0x0939],
],
// Extended consonant + left matra: U+0958 QA + U+093F
'extended_consonant_with_matra' => [
[0x0958, 0x093F],
[0x093F, 0x0958],
],
// Conjunct cluster: U+0915 KA + U+094D VIRAMA + U+0916 KHA + U+093F
// → U+093F, U+0915, U+094D, U+0916
'conjunct_with_matra' => [
[0x0915, 0x094D, 0x0916, 0x093F],
[0x093F, 0x0915, 0x094D, 0x0916],
],
// Longer conjunct: KA + VIRAMA + SHA + VIRAMA + HA + VOWEL SIGN I
// → U+093F, KA, VIRAMA, SHA, VIRAMA, HA
'three_consonant_conjunct_with_matra' => [
[0x0915, 0x094D, 0x0937, 0x094D, 0x0939, 0x093F],
[0x093F, 0x0915, 0x094D, 0x0937, 0x094D, 0x0939],
],
// Conjunct where virama is not followed by a consonant: cluster
// ends at the virama; the matra after virama is NOT moved
// U+0915, U+094D, U+093F → U+0915, U+094D, U+093F
// (U+094D followed by non-consonant ends the cluster at KA only,
// but then U+094D is the next codepoint: not a left matra)
'virama_then_matra_no_reorder' => [
[0x0915, 0x094D, 0x093F],
[0x0915, 0x094D, 0x093F],
],
// Consonant followed by non-matra: pass through unchanged
// U+0915, 0x41 (ASCII A)
'consonant_then_ascii' => [
[0x0915, 0x41],
[0x0915, 0x41],
],
// Two separate simple clusters
// KA+I, KHA+I → I+KA, I+KHA
'two_simple_clusters' => [
[0x0915, 0x093F, 0x0916, 0x093F],
[0x093F, 0x0915, 0x093F, 0x0916],
],
// Mixed: ASCII + Devanagari cluster + ASCII
// 0x41, U+0915, U+093F, 0x42 → 0x41, U+093F, U+0915, 0x42
'mixed_ascii_devanagari' => [
[0x41, 0x0915, 0x093F, 0x42],
[0x41, 0x093F, 0x0915, 0x42],
],
];
}
public function testNormalizesSparseIndexes(): void
{
$obj = new Devanagari([5 => 0x0915, 9 => 0x093F]);
$this->assertSame([0x093F, 0x0915], $obj->getOrdarr());
}
}
@@ -0,0 +1,230 @@
<?php
/**
* HangulTest.php
*
* @since 2026-04-30
* @category Library
* @package Unicode
* @author Nicola Asuni <info@tecnick.com>
* @copyright 2011-2026 Nicola Asuni - Tecnick.com LTD
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
* @link https://github.com/tecnickcom/tc-lib-unicode
*
* This file is part of tc-lib-unicode software library.
*/
namespace Test\Substitution;
use Com\Tecnick\Unicode\Substitution\Hangul;
use PHPUnit\Framework\Attributes\DataProvider;
use Test\TestUtil;
/**
* Hangul Jamo composition test
*
* @since 2026-04-30
* @category Library
* @package Unicode
* @author Nicola Asuni <info@tecnick.com>
* @copyright 2011-2026 Nicola Asuni - Tecnick.com LTD
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
* @link https://github.com/tecnickcom/tc-lib-unicode
*/
class HangulTest extends TestUtil
{
/**
* @param array<int, int> $input
* @param array<int, int> $expected
*/
#[DataProvider('hangulDataProvider')]
public function testGetOrdarr(array $input, array $expected): void
{
$obj = new Hangul($input);
$this->assertSame($expected, $obj->getOrdarr());
}
/**
* @return array<string, array{0: array<int, int>, 1: array<int, int>}>
*
* Expected values verified against Unicode conformance data (NormalizationTest.txt,
* Hangul section) and the algorithmic formula in Unicode Standard 15.1 §3.12:
* S = SBase(AC00) + (L1100)×NCount(588) + (V1161)×TCount(28) + (T11A7)
*
* Spot-checks:
* 가 (U+AC00) = AC00 + (11001100)×588 + (11611161)×28 + 0 = AC00
* 나 (U+B098) = AC00 + (11021100)×588 + (11611161)×28 = AC00 + 2×588 = AC00+1176 = B1D8? No wait:
* L=U+1102 (NIEUN), V=U+1161 (A)
* S = AC00 + (1102-1100)*588 + (1161-1161)*28 = AC00 + 2*588 = AC00 + 1176 = 0xB1D8 -- that's 나 but wait
* Actually 나 = U+B098: B098 - AC00 = 1176 - hmm, 0xB098 - 0xAC00 = 0x498 = 1176. Yes.
* (1102-1100)*588 = 2*588 = 1176. Correct.
* 닭 (U+B2ED) = AC00 + (1103-1100)*588 + (1161-1161)*28 + (11AF-11A7) -- wait닭 has T=U+11BC?
* 닭: L=U+1103 (TIKEUT), V=U+1161 (A), T=U+11BC (IEUNG)? No.
* Let's just use simple known values:
* 가 U+AC00: L=U+1100, V=U+1161 → AC00 + 0 + 0 = AC00 ✓
* 각 U+AC01: L=U+1100, V=U+1161, T=U+11A8 → AC00 + 0 + (11A8-11A7)=1 = AC01 ✓
* 갈 U+AC08: L=U+1100, V=U+1161, T=U+11AF → AC00 + 7 = AC07? 11AF-11A7=8, so AC00+8=AC08 ✓
* 나 U+B098: L=U+1102, V=U+1161 → AC00+2*588=AC00+0x498=B098 -- 0xAC00+0x498=0xB098?
* 0xAC00=44032, 2*588=1176, 44032+1176=45208=0xB098. ✓
*/
public static function hangulDataProvider(): array
{
return [
// Empty input returns empty output
'empty' => [
[],
[],
],
// Rule 2 with a precomposed LV syllable already in the input:
// U+AC00 (GA) + U+11A8 (KIYEOK) -> U+AC01 (GAG)
'precomposed_lv_plus_t' => [
[0xAC00, 0x11A8],
[0xAC01],
],
// A syllable that already has a trailing consonant is not composed again
'lvt_plus_t' => [
[0xAC01, 0x11A8],
[0xAC01, 0x11A8],
],
// A precomposed LV syllable followed by anything else is left alone
'precomposed_lv_plus_ascii' => [
[0xAC00, 0x41],
[0xAC00, 0x41],
],
// Pure ASCII: no Hangul, pass through unchanged
'ascii_only' => [
[0x41, 0x42, 0x43],
[0x41, 0x42, 0x43],
],
// Leading consonant at end of array (no following vowel): unchanged
// U+1100 KIYEOK alone
'lone_leading_consonant' => [
[0x1100],
[0x1100],
],
// First leading consonant, last leading consonant: boundary check
// U+1100, U+1112: no vowels follow; both pass through
'leading_consonant_boundaries' => [
[0x1100, 0x1112],
[0x1100, 0x1112],
],
// Codepoint just above leading consonant range (U+1113): not L, pass through
'above_leading_consonant_range' => [
[0x1113],
[0x1113],
],
// Vowel alone: not a leading consonant, pass through
// U+1161 JUNGSEONG A
'lone_vowel' => [
[0x1161],
[0x1161],
],
// Trailing consonant alone: not a leading consonant, pass through
// U+11A8 JONGSEONG KIYEOK
'lone_trailing_consonant' => [
[0x11A8],
[0x11A8],
],
// L + V → LV syllable (no trailing consonant)
// U+1100 + U+1161 → U+AC00 가 (GA)
'l_plus_v_ga' => [
[0x1100, 0x1161],
[0xAC00],
],
// L + V boundary: last L (U+1112) + last V (U+1175) → syllable
// S = AC00 + 18*588 + 20*28 = AC00 + 10584 + 560 = AC00 + 11144 = D7A4 - 28 = D784?
// 0xAC00 + 18*588 + 20*28 = 44032 + 10584 + 560 = 55176 = 0xD788
'l_plus_v_boundary' => [
[0x1112, 0x1175],
[0xD788],
],
// L + V + T → LVT syllable
// U+1100 + U+1161 + U+11A8 → U+AC01 각 (GAK)
// LV = AC00, T = 11A8 11A7 = 1 → AC00 + 1 = AC01
'l_plus_v_plus_t_gak' => [
[0x1100, 0x1161, 0x11A8],
[0xAC01],
],
// L + V + T with T = last valid trailing consonant (U+11C2)
// U+1100 + U+1161 + U+11C2 → AC00 + (11C2 11A7) = AC00 + 27 = AC1B
'l_plus_v_plus_t_last_trailing' => [
[0x1100, 0x1161, 0x11C2],
[0xAC1B],
],
// L + V + TBase (U+11A7): TBase itself is NOT a valid trailing
// consonant; treated as next non-T codepoint. LV emitted, then
// U+11A7 passed through unchanged.
'l_plus_v_plus_tbase_not_trailing' => [
[0x1100, 0x1161, 0x11A7],
[0xAC00, 0x11A7],
],
// L + V + codepoint above T range (U+11C3): not a trailing consonant,
// LV emitted then U+11C3 passed through
'l_plus_v_then_above_t_range' => [
[0x1100, 0x1161, 0x11C3],
[0xAC00, 0x11C3],
],
// L followed by non-vowel (ASCII): L emitted unchanged, then ASCII
'leading_consonant_then_ascii' => [
[0x1100, 0x41],
[0x1100, 0x41],
],
// Two separate LV syllables in sequence
// U+1100+U+1161, U+1102+U+1161 → U+AC00, U+B098
// B098: AC00 + 2*588 = AC00 + 1176 = 0xB098
'two_lv_syllables' => [
[0x1100, 0x1161, 0x1102, 0x1161],
[0xAC00, 0xB098],
],
// Mixed: ASCII + Jamo cluster + ASCII
// 0x41, U+1100, U+1161, 0x42 → 0x41, U+AC00, 0x42
'mixed_ascii_hangul' => [
[0x41, 0x1100, 0x1161, 0x42],
[0x41, 0xAC00, 0x42],
],
// L + V + T + next L + V: two clusters in series
// U+1100, U+1161, U+11A8, U+1102, U+1161 → U+AC01, U+B098
'two_clusters_with_trailing' => [
[0x1100, 0x1161, 0x11A8, 0x1102, 0x1161],
[0xAC01, 0xB098],
],
// L + first vowel out-of-range: U+1160 is just below VBASE: not a vowel
'leading_consonant_then_below_vbase' => [
[0x1100, 0x1160],
[0x1100, 0x1160],
],
// L + first codepoint above vowel range: U+1176: not a vowel
'leading_consonant_then_above_vrange' => [
[0x1100, 0x1176],
[0x1100, 0x1176],
],
];
}
public function testNormalizesSparseIndexes(): void
{
$obj = new Hangul([10 => 0x1100, 20 => 0x1161]);
$this->assertSame([0xAC00], $obj->getOrdarr());
}
}
@@ -0,0 +1,157 @@
<?php
/**
* ThaiTest.php
*
* @since 2026-04-30
* @category Library
* @package Unicode
* @author Nicola Asuni <info@tecnick.com>
* @copyright 2011-2026 Nicola Asuni - Tecnick.com LTD
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
* @link https://github.com/tecnickcom/tc-lib-unicode
*
* This file is part of tc-lib-unicode software library.
*/
namespace Test\Substitution;
use Com\Tecnick\Unicode\Substitution\Thai;
use PHPUnit\Framework\Attributes\DataProvider;
use Test\TestUtil;
/**
* Thai substitution test
*
* @since 2026-04-30
* @category Library
* @package Unicode
* @author Nicola Asuni <info@tecnick.com>
* @copyright 2011-2026 Nicola Asuni - Tecnick.com LTD
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
* @link https://github.com/tecnickcom/tc-lib-unicode
*/
class ThaiTest extends TestUtil
{
/**
* @param array<int, int> $input
* @param array<int, int> $expected
*/
#[DataProvider('thaiDataProvider')]
public function testGetOrdarr(array $input, array $expected): void
{
$obj = new Thai($input);
$this->assertSame($expected, $obj->getOrdarr());
}
/**
* @return array<string, array{0: array<int, int>, 1: array<int, int>}>
*/
public static function thaiDataProvider(): array
{
return [
// Empty input returns empty output
'empty' => [
[],
[],
],
// Pure ASCII: no Thai codepoints, pass through unchanged
'ascii_only' => [
[0x41, 0x42, 0x43],
[0x41, 0x42, 0x43],
],
// Thai consonant only (no leading vowel): unchanged
// U+0E01 THAI CHARACTER KO KAI
'consonant_only' => [
[0x0E01],
[0x0E01],
],
// Tone mark only: not a leading vowel, unchanged
// U+0E48 THAI CHARACTER MAI EK
'tone_mark_only' => [
[0x0E48],
[0x0E48],
],
// Single leading vowel at end of array (orphaned): leave unchanged
// U+0E40 THAI CHARACTER SARA E
'orphaned_leading_vowel_end' => [
[0x0E40],
[0x0E40],
],
// Leading vowel followed by a tone mark (not a base consonant):
// leave unchanged: U+0E40, U+0E48
'leading_vowel_then_tone_mark' => [
[0x0E40, 0x0E48],
[0x0E40, 0x0E48],
],
// Leading vowel followed by an ASCII character (not a base
// consonant): leave unchanged: U+0E40, 0x41
'leading_vowel_then_ascii' => [
[0x0E40, 0x41],
[0x0E40, 0x41],
],
// Preposed vowel: stored and displayed before the consonant
// U+0E40 (SARA E) + U+0E01 (KO KAI)
'sara_e_before_ko_kai' => [
[0x0E40, 0x0E01],
[0x0E40, 0x0E01],
],
// U+0E41 (SARA AE) + U+0E02 (KHO KHAI)
'sara_ae_before_kho_khai' => [
[0x0E41, 0x0E02],
[0x0E41, 0x0E02],
],
// U+0E44 (SARA AI MAIMALAI) + last base consonant U+0E2E
'sara_ai_before_ho_nokhuk' => [
[0x0E44, 0x0E2E],
[0x0E44, 0x0E2E],
],
// Multiple consecutive preposed vowels before one consonant
'two_leading_vowels_then_consonant' => [
[0x0E40, 0x0E41, 0x0E01],
[0x0E40, 0x0E41, 0x0E01],
],
// Preposed vowel + consonant + tone mark
'vowel_consonant_tone' => [
[0x0E40, 0x0E01, 0x0E48],
[0x0E40, 0x0E01, 0x0E48],
],
// Mixed: ASCII + Thai cluster + ASCII
'mixed_ascii_thai' => [
[0x41, 0x0E40, 0x0E01, 0x0E48, 0x42],
[0x41, 0x0E40, 0x0E01, 0x0E48, 0x42],
],
// Two separate Thai clusters in one array
'two_clusters' => [
[0x0E40, 0x0E01, 0x0E44, 0x0E2E],
[0x0E40, 0x0E01, 0x0E44, 0x0E2E],
],
// Multiple consecutive leading vowels followed by non-consonant
// (both left unchanged)
'two_leading_vowels_no_consonant' => [
[0x0E40, 0x0E41],
[0x0E40, 0x0E41],
],
];
}
public function testNormalizesSparseIndexes(): void
{
$obj = new Thai([3 => 0x0E40, 7 => 0x0E01]);
$this->assertSame([0x0E40, 0x0E01], $obj->getOrdarr());
}
}
@@ -0,0 +1,177 @@
<?php
/**
* SubstitutionTest.php
*
* @since 2026-04-30
* @category Library
* @package Unicode
* @author Nicola Asuni <info@tecnick.com>
* @copyright 2011-2026 Nicola Asuni - Tecnick.com LTD
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
* @link https://github.com/tecnickcom/tc-lib-unicode
*
* This file is part of tc-lib-unicode software library.
*/
namespace Test;
use Com\Tecnick\Unicode\Substitution;
use PHPUnit\Framework\Attributes\DataProvider;
/**
* Substitution dispatcher test
*
* @since 2026-04-30
* @category Library
* @package Unicode
* @author Nicola Asuni <info@tecnick.com>
* @copyright 2011-2026 Nicola Asuni - Tecnick.com LTD
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
* @link https://github.com/tecnickcom/tc-lib-unicode
*/
class SubstitutionTest extends TestUtil
{
protected function getTestObject(): Substitution
{
return new Substitution();
}
/**
* @param array<int, int> $input
* @param array<int, int> $expected
*/
#[DataProvider('replaceCharsDataProvider')]
public function testReplaceChars(array $input, array $expected): void
{
$sub = $this->getTestObject();
$this->assertSame($expected, $sub->replaceChars($input));
}
/**
* @return array<string, array{0: array<int, int>, 1: array<int, int>}>
*/
public static function replaceCharsDataProvider(): array
{
return [
// Empty input
'empty' => [
[],
[],
],
// Pure ASCII: Thai script not detected, returned unchanged
'ascii_only' => [
[0x41, 0x42, 0x43],
[0x41, 0x42, 0x43],
],
// Non-Thai non-Devanagari Unicode (Bengali): not yet handled, pass through
// U+0985 BENGALI LETTER A
'bengali_passthrough' => [
[0x0985, 0x0986],
[0x0985, 0x0986],
],
// Thai only consonants (Thai detected but no leading vowels):
// handler runs but nothing is repositioned
// U+0E01 KO KAI, U+0E02 KHO KHAI
'thai_consonants_only' => [
[0x0E01, 0x0E02],
[0x0E01, 0x0E02],
],
// Thai with multiple codepoints: exercises the detectScripts
// short-circuit where the second Thai codepoint skips the check
// U+0E01, U+0E02, U+0E03
'thai_multiple_consonants' => [
[0x0E01, 0x0E02, 0x0E03],
[0x0E01, 0x0E02, 0x0E03],
],
// Thai with a preposed vowel: the dispatcher delegates to ThaiHandler,
// which keeps the stored order
'thai_preposed_vowel' => [
[0x0E40, 0x0E01],
[0x0E40, 0x0E01],
],
// Mixed script: Thai cluster plus ASCII
'mixed_ascii_thai' => [
[0x41, 0x0E40, 0x0E01, 0x42],
[0x41, 0x0E40, 0x0E01, 0x42],
],
// Devanagari consonant without matra: handler runs, nothing moved
// U+0915 KA, U+0916 KHA
'devanagari_consonants_only' => [
[0x0915, 0x0916],
[0x0915, 0x0916],
],
// Devanagari: detectScripts short-circuit: second Devanagari
// codepoint skips the range check once already detected
// U+0915, U+0916, U+0917
'devanagari_multiple_consonants' => [
[0x0915, 0x0916, 0x0917],
[0x0915, 0x0916, 0x0917],
],
// Devanagari left matra reposition via dispatcher
// U+0915 KA + U+093F → U+093F, U+0915
'devanagari_matra_reposition' => [
[0x0915, 0x093F],
[0x093F, 0x0915],
],
// Mixed ASCII + Devanagari cluster
// 0x41, U+0915, U+093F, 0x42 → 0x41, U+093F, U+0915, 0x42
'mixed_ascii_devanagari' => [
[0x41, 0x0915, 0x093F, 0x42],
[0x41, 0x093F, 0x0915, 0x42],
],
// Devanagari codepoint in block but outside consonant range:
// U+0900 INVERTED CANDRABINDU (combining mark): no matra reorder
'devanagari_non_consonant' => [
[0x0900, 0x093F],
[0x0900, 0x093F],
],
// Hangul Jamo leading consonant + vowel: dispatcher delegates
// U+1100 + U+1161 → U+AC00 가 (GA)
'hangul_lv_composition' => [
[0x1100, 0x1161],
[0xAC00],
],
// Hangul Jamo detected via extended-A range (U+A960): triggers hangul handler;
// U+A960 is not in the standard L range so it passes through unchanged but
// ensures isHangulJamo covers HANGUL_JAMO_EXT_A
'hangul_ext_a_passthrough' => [
[0xA960],
[0xA960],
],
// Hangul Jamo detected via extended-B range (U+D7B0): same as above for EXT_B
'hangul_ext_b_passthrough' => [
[0xD7B0],
[0xD7B0],
],
// Hangul: detectScripts short-circuit: second Hangul Jamo codepoint skips check
// U+1100, U+1102: both L, no V, so no composition
'hangul_multiple_leading_consonants' => [
[0x1100, 0x1102],
[0x1100, 0x1102],
],
// Hangul L + V + T full composition via dispatcher
// U+1100, U+1161, U+11A8 → U+AC01
'hangul_lvt_composition' => [
[0x1100, 0x1161, 0x11A8],
[0xAC01],
],
];
}
}
+41
View File
@@ -0,0 +1,41 @@
<?php
/**
* TestUtil.php
*
* @since 2020-12-19
* @category Library
* @package Unicode
* @author Nicola Asuni <info@tecnick.com>
* @copyright 2015-2026 Nicola Asuni - Tecnick.com LTD
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
* @link https://github.com/tecnickcom/tc-lib-unicode
*
* This file is part of tc-lib-color software library.
*/
namespace Test;
use PHPUnit\Framework\TestCase;
/**
* Web Color class test
*
* @since 2020-12-19
* @category Library
* @package Unicode
* @author Nicola Asuni <info@tecnick.com>
* @copyright 2015-2026 Nicola Asuni - Tecnick.com LTD
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
* @link https://github.com/tecnickcom/tc-lib-unicode
*/
class TestUtil extends TestCase
{
/**
* @param class-string<\Throwable> $exception
*/
public function bcExpectException(string $exception): void
{
parent::expectException($exception);
}
}
@@ -0,0 +1,93 @@
<?php
/**
* TextDirectionTest.php
*
* @since 2026-07-17
* @category Library
* @package Unicode
* @author Nicola Asuni <info@tecnick.com>
* @copyright 2011-2026 Nicola Asuni - Tecnick.com LTD
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
* @link https://github.com/tecnickcom/tc-lib-unicode
*
* This file is part of tc-lib-unicode software library.
*/
namespace Test;
use Com\Tecnick\Unicode\Bidi;
use Com\Tecnick\Unicode\TextDirection;
/**
* TextDirection enum test
*
* @since 2026-07-17
* @category Library
* @package Unicode
* @author Nicola Asuni <info@tecnick.com>
* @copyright 2011-2026 Nicola Asuni - Tecnick.com LTD
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
* @link https://github.com/tecnickcom/tc-lib-unicode
*/
class TextDirectionTest extends TestUtil
{
public function testCaseBackingValues(): void
{
$this->assertSame('', TextDirection::Auto->value);
$this->assertSame('R', TextDirection::Rtl->value);
$this->assertSame('L', TextDirection::Ltr->value);
}
public function testFromLooseCanonical(): void
{
$this->assertSame(TextDirection::Auto, TextDirection::fromLoose(''));
$this->assertSame(TextDirection::Rtl, TextDirection::fromLoose('R'));
$this->assertSame(TextDirection::Ltr, TextDirection::fromLoose('L'));
}
public function testFromLooseIsLenientOnFirstCharacter(): void
{
$this->assertSame(TextDirection::Rtl, TextDirection::fromLoose('r'));
$this->assertSame(TextDirection::Ltr, TextDirection::fromLoose('l'));
$this->assertSame(TextDirection::Rtl, TextDirection::fromLoose('RTL'));
$this->assertSame(TextDirection::Ltr, TextDirection::fromLoose('ltr'));
$this->assertSame(TextDirection::Rtl, TextDirection::fromLoose('right'));
$this->assertSame(TextDirection::Ltr, TextDirection::fromLoose('left'));
}
public function testFromLooseFallsBackToAuto(): void
{
$this->assertSame(TextDirection::Auto, TextDirection::fromLoose('X'));
$this->assertSame(TextDirection::Auto, TextDirection::fromLoose('auto'));
$this->assertSame(TextDirection::Auto, TextDirection::fromLoose('1'));
}
public function testFromLoosePassesThroughEnumInstance(): void
{
$this->assertSame(TextDirection::Rtl, TextDirection::fromLoose(TextDirection::Rtl));
}
public function testFromLooseRoundTrip(): void
{
foreach (TextDirection::cases() as $case) {
$this->assertSame($case, TextDirection::fromLoose($case->value));
}
}
/**
* The widened Bidi constructor accepts a TextDirection and behaves exactly
* like the equivalent legacy string.
*
* @throws \Com\Tecnick\Unicode\Exception
*/
public function testBidiAcceptsEnum(): void
{
// The trailing space makes the forced RTL direction visible in the output:
// L1.4 resets it to the paragraph level, moving it to the visual left.
$fromEnum = new Bidi('left to right ', null, null, TextDirection::Rtl, true);
$fromString = new Bidi('left to right ', null, null, 'R', true);
$this->assertSame(' left to right', $fromEnum->getString());
$this->assertSame($fromString->getString(), $fromEnum->getString());
}
}