Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/cscott/node-icu-bidi

Node bindings to the ICU (53) Unicode BiDi algorithm
https://github.com/cscott/node-icu-bidi

Last synced: about 2 months ago
JSON representation

Node bindings to the ICU (53) Unicode BiDi algorithm

Awesome Lists containing this project

README

        

# icu-bidi
[![NPM][NPM1]][NPM2]

[![Build Status][1]][2] [![dependency status][3]][4] [![dev dependency status][5]][6]

The node `icu-bidi` package binds to the [ICU][] (55.1) library in order to
provide an implementation of the Unicode [BiDi][] algorithm.

# USAGE

The JavaScript API follows the
[API of `icu4c`](http://icu-project.org/apiref/icu4c/ubidi_8h.html)
fairly closely.

```js
var ubidi = require('icu-bidi');

var e = 'English';
var h = '‭עִבְרִית‬';
var input = e + ' ' + h;

console.log( input );
var p = ubidi.Paragraph(input, {
// this hash is optional; these are the default values:
paraLevel: ubidi.DEFAULT_LTR,
reorderingMode: ubidi.ReorderingMode.DEFAULT,
reorderingOptions: 0, // uses ubidi.ReorderingOptions.*
orderParagraphsLTR: false,
inverse: false,
prologue: '',
epilogue: '',
embeddingLevels: null /* Unimplemented */
});
console.log( 'number of paragraphs', p.countParagraphs() );
console.log( 'paragraph level', p.getParaLevel() );
// direction is 'ltr', 'rtl', or 'mixed'
console.log( 'direction', p.getDirection() );

var i, levels = [];
for (i=0; i < p.getProcessedLength(); i++) {
levels.push( p.getLevelAt(i) );
}
console.log( levels.join(' ') );

for (i=0; i < p.countRuns(); i++) {
var run = p.getVisualRun(i);
console.log( 'run', run.dir, 'from', run.logicalStart, 'len', run.length );
}

console.log( p.writeReordered(ubidi.Reordered.KEEP_BASE_COMBINING) );
```

This example prints the following when run:
```
English ‭עִבְרִית‬
number of paragraphs 1
paragraph level 0
direction mixed
0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1
run ltr from 0 len 8
run rtl from 8 len 8
English ‭תירִבְעִ‬
```

# API

## new ubidi.Paragraph(text, [options])

Returns a new `Paragraph` object with the results of the bidi algorithm.
* `text`: UTF-16 encoded text as a standard JavaScript string; you know
the drill.
* `options` *(optional)*: a hash containing various settings which can
affect the bidi algorithm. All are optional.
- [`paraLevel`][ubidi_setPara]:
Specifies the default [level][UBiDiLevel] for the text; it is
typically 0 (LTR) or 1 (RTL). If the function shall determine
the paragraph level from the text, then paraLevel can be set
to either `ubidi.DEFAULT_LTR` or `ubidi.DEFAULT_RTL`.
- [`reorderingMode`][ubidi_setReorderingMode]:
Modify the operation of the Bidi algorithm such that it implements
some variant to the basic Bidi algorithm or approximates an
"inverse Bidi" algorithm, depending on different values of the
"reordering mode".
- `ubidi.ReorderingMode.DEFAULT`:
The standard Bidi Logical to Visual algorithm is applied.
- `ubidi.ReorderingMode.REORDER_NUMBERS_SPECIAL`:
Approximate the algorithm used in Microsoft Windows XP rather
than strictly conform to the Unicode Bidi algorithm.
- `ubidi.ReorderingMode.GROUP_NUMBERS_WITH_R`:
Numbers located between LTR text and RTL text are
associated with the RTL text. This makes the algorithm
reversible and makes it useful when round trip must be
achieved without adding LRM characters. However, this is a
variation from the standard Unicode Bidi algorithm.
- `ubidi.ReorderingMode.RUNS_ONLY`:
Logical-to-Logical transformation. If the default text
level of the source text (`paraLevel`) is even, the source
text will be handled as LTR logical text and will be
transformed to the RTL logical text which has the same LTR
visual display. If the default level of the source text
is odd, the source text will be handled as RTL logical
text and will be transformed to the LTR logical text which
has the same LTR visual display.
- `ubidi.ReorderingMode.INVERSE_NUMBERS_AS_L`:
An "inverse Bidi" algorithm is applied. This mode is
equivalent to setting the `inverse` option to `true`.
- `ubidi.ReorderingMode.INVERSE_LIKE_DIRECT`:
The "direct" Logical to Visual Bidi algorithm is used as
an approximation of an "inverse Bidi" algorithm.
- `ubidi.ReorderingMode.INVERSE_FOR_NUMBERS_SPECIAL`:
The Logical to Visual Bidi algorithm used in Windows XP is
used as an approximation of an "inverse Bidi" algorithm.
- [`reorderingOptions`][ubidi_setReorderingOptions]:
Specify which of the reordering options should be applied
during Bidi transformations. The value is a combination using
bitwise OR of zero or more of the following:
- [`ubidi.ReorderingOptions.DEFAULT`][UBiDiReorderingOption]
Disable all the options which can be set with this function.
- [`ubidi.ReorderingOptions.INSERT_MARKS`][UBiDiReorderingOption]
Insert Bidi marks (LRM or RLM) when needed to ensure correct
result of a reordering to a Logical order.
- [`ubidi.ReorderingOptions.REMOVE_CONTROLS`][UBiDiReorderingOption]
Remove Bidi control characters.
- [`ubidi.ReorderingOptions.STREAMING`][UBiDiReorderingOption]
Process the output as part of a stream to be continued.
- [`orderParagraphsLTR`][ubidi_orderParagraphsLTR]: *(boolean)*
Specify whether block separators must be allocated level zero, so
that successive paragraphs will progress from left to right.
- [`inverse`][ubidi_setInverse]: *(boolean)*
Modify the operation of the Bidi algorithm such that it approximates
an "inverse Bidi" algorithm.
- [`prologue`][ubidi_setContext]: *(string)*
A preceding context for the given text.
- [`epilogue`][ubidi_setContext]: *(string)*
A trailing context for the given text.
- [`embeddingLevels`][ubidi_setPara]: *(array)*
CURRENTLY UNIMPLEMENTED.

## Paragraph#setLine(start, limit)

Returns a subsidiary `Paragraph` object containing the reordering
information, especially the resolved levels, for all the characters in
a line of text.

This line of text is specified by referring to a `Paragraph` object
representing this information for a piece of text containing one or
more paragraphs, and by specifying a range of indexes in this text.

In the returned line object, the indexes will range from `0` to `limit-start-1`.

This is used after creating a `Paragraph` for a piece of text, and
after line-breaking on that text. It is not necessary if each
paragraph is treated as a single line.

After line-breaking, rules (L1) and (L2) for the treatment of trailing
WS and for reordering are performed on the returned `Paragraph` object
representing a line.

* `start`:
The line's first index into the text
* `limit`:
The index just behind the line's last index into the text (its last
index +1). It must be `0<=start= 52.1

# TESTING

[mocha](https://github.com/visionmedia/mocha) is required to run unit tests.

npm install mocha
npm test

# CONTRIBUTORS

* [C. Scott Ananian](https://github.com/cscott)

# RELATED PROJECTS

* [ICU][]
* [GNU FriBiDi](http://fribidi.org/)

# LICENSE
Copyright (c) 2013-2014 C. Scott Ananian.

`icu-bidi` is licensed using the same [ICU license] as the libicu library
itself. It is an MIT/X-style license.

[ICU]: http://icu-project.org/
[ICU license]: http://source.icu-project.org/repos/icu/icu/trunk/license.html
[BiDi]: http://www.unicode.org/unicode/reports/tr9/

[NPM1]: https://nodei.co/npm/icu-bidi.png
[NPM2]: https://nodei.co/npm/icu-bidi/

[1]: https://travis-ci.org/cscott/node-icu-bidi.png
[2]: https://travis-ci.org/cscott/node-icu-bidi
[3]: https://david-dm.org/cscott/node-icu-bidi.png
[4]: https://david-dm.org/cscott/node-icu-bidi
[5]: https://david-dm.org/cscott/node-icu-bidi/dev-status.png
[6]: https://david-dm.org/cscott/node-icu-bidi#info=devDependencies