Skip to content

Commit

Permalink
Merge pull request #15 from semion1956/master
Browse files Browse the repository at this point in the history
Add bidi levels attribute, update decor dependency to 0.6.x
  • Loading branch information
semion1956 committed May 12, 2015
2 parents 4e293b1 + 0de7e4c commit 7ffbaca
Show file tree
Hide file tree
Showing 7 changed files with 235 additions and 64 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ This project provides bidi utilities for JavaScript/AMD/Dojo Applications

## Status

The most recent pre-release is 0.2.0.
The most recent pre-release is 0.3.0.

## Migration

Expand Down
93 changes: 36 additions & 57 deletions TextLayoutEngine.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ define([

// Array, containing positions of each character from the resulting text in the source text.
targetToSource: [],

// Array, containing bidi level of each character from the source text
levels: [],

bidiTransform: function (/*String*/text, /*String*/formatIn, /*String*/formatOut) {
// summary:
Expand Down Expand Up @@ -152,7 +155,10 @@ define([
if (!this.checkParameters(formatIn, formatOut)) {
return text;
}


formatIn = this.inputFormat;
formatOut = this.outputFormat;
var result = text;
var bdx = BDX;
var orientIn = getOrientation(formatIn.charAt(1)),
orientOut = getOrientation(formatOut.charAt(1)),
Expand Down Expand Up @@ -180,14 +186,16 @@ define([
tsMap = this.targetToSource;

if (formatIn.charAt(3) === formatOut.charAt(3)) {
return stage1Text;
result = stage1Text;
} else if (formatOut.charAt(3) === "S") {
return shape(isRtl, stage1Text, true);
result = shape(isRtl, stage1Text, true);
} else { //formatOut.charAt(3) === "N"
return deshape(stage1Text, isRtl, true);
result = deshape(stage1Text, isRtl, true);
}
this.sourceToTarget = stMap;
this.targetToSource = tsMap;
this.levels = lvMap;
return result;
},

_setInputFormatAttr: function (format) {
Expand Down Expand Up @@ -272,30 +280,16 @@ define([
// text:
// The source string.
// description:
// Iterates over the text string, letter by letter starting from its beginning,
// searching for RTL directed character.
// Return true if found else false. Needed for vml transformation.
// Searches for RTL directed character.
// Returns true if found, else returns false.
// returns: /*Boolean*/
// true - if text has a RTL directed character.
// false - otherwise.
// tags:
// public

var type = null, uc = null, hi = null;
for (var i = 0; i < text.length; i++) {
uc = text.charAt(i).charCodeAt(0);
hi = MasterTable[uc >> 8];
type = hi < TBBASE ? hi : UnicodeTable[hi - TBBASE][uc & 0xFF];
if (type === UBAT_R || type === UBAT_AL) {
return true;
}
if (type === UBAT_B) {
break;
}
}
return false;
return bidiChars.test(text);
}

});

function doBidiReorder(/*String*/text, /*String*/inFormat,
Expand Down Expand Up @@ -359,7 +353,8 @@ define([
if ((inOrdering === "V") && (outOrdering === "V")) {
//inOrientation != outOrientation
//cases: VRTL->VLTR, VLTR->VRTL
return invertStr(text);
bdx.dir = inOrientation === "RTL" ? RTL : LTR;
return invertStr(text, bdx);
}
if ((inOrdering === "L") && (outFormat === "VRTL")) {
//cases: LLTR->VRTL, LRTL->VRTL
Expand Down Expand Up @@ -590,8 +585,7 @@ define([
// text:
// The source string.
// description:
// Iterates over the text string, letter by letter starting from its beginning,
// searching for first "strong" character.
// Searches for first "strong" character.
// Returns if strong character was found with the direction defined by this
// character, if no strong character was found returns an empty string.
// returns: String
Expand All @@ -601,22 +595,9 @@ define([
// tags:
// private

var type = null, uc = null, hi = null;
for (var i = 0; i < text.length; i++) {
uc = text.charAt(i).charCodeAt(0);
hi = MasterTable[uc >> 8];
type = hi < TBBASE ? hi : UnicodeTable[hi - TBBASE][uc & 0xFF];
if (type === UBAT_R || type === UBAT_AL) {
return "rtl";
}
if (type === UBAT_L) {
return "ltr";
}
if (type === UBAT_B) {
break;
}
}
return "";
var fdc = /[A-Za-z\u05d0-\u065f\u066a-\u06ef\u06fa-\u07ff\ufb1d-\ufdff\ufe70-\ufefc]/.exec(text);
// if found return the direction that defined by the character
return fdc ? (fdc[0] <= "z" ? "ltr" : "rtl") : "";
}

function lastStrongDir(text) {
Expand All @@ -625,26 +606,14 @@ define([
// text:
// The source string.
// description:
// Iterates over the text string, letter by letter starting from its end,
// searching for first (from the end) "strong" character.
// Searches for first (from the end) "strong" character.
// Returns if strong character was found with the direction defined by this
// character, if no strong character was found returns an empty string.
// tags:
// private
var type = null;
for (var i = text.length - 1; i >= 0; i--) {
type = getCharacterType(text.charAt(i));
if (type === UBAT_R || type === UBAT_AL) {
return "rtl";
}
if (type === UBAT_L) {
return "ltr";
}
if (type === UBAT_B) {
break;
}
}
return "";
var chars = text.split("");
chars.reverse();
return firstStrongDir(chars.join(""));
}

function deshape(/*String*/text, /*boolean*/rtl, /*boolean*/consumeNextSpace) { //jshint maxcomplexity: 15
Expand Down Expand Up @@ -730,6 +699,7 @@ define([
swapChars(chars, levels, bdx);
invertLevel(2, chars, levels, bdx);
invertLevel(1, chars, levels, bdx);
lvMap = levels;
return chars.join("");
}

Expand Down Expand Up @@ -843,7 +813,7 @@ define([
return (hi < TBBASE) ? hi : UnicodeTable[hi - TBBASE][uc & 0xFF];
}

function invertStr(str) {
function invertStr(str, bdx) {
// summary:
// Return the reversed string.
// str:
Expand All @@ -853,6 +823,11 @@ define([
// tags:
// private
var chars = str.split("");
if (bdx) {
var levels = [];
computeLevels(chars, levels, bdx);
lvMap = levels;
}
chars.reverse();
stMap.reverse();
return chars.join("");
Expand Down Expand Up @@ -1117,6 +1092,7 @@ define([

function initMaps(map1, map2, length) {
stMap = [];
lvMap = [];
for (var i = 0; i < length; i++) {
map1[i] = i;
map2[i] = i;
Expand All @@ -1142,6 +1118,7 @@ define([

var stMap = [];
var tsMap = [];
var lvMap = [];

var BDX = {
dir: 0,
Expand All @@ -1168,6 +1145,8 @@ define([

var validFormat = /^[(I|V)][(L|R|C|D)][(Y|N)][(S|N)][N]$/;

var bidiChars = /[\u0591-\u06ff\ufb1d-\ufefc]/;

/****************************************************************************/
/* Array in which directional characters are replaced by their symmetric. */
/****************************************************************************/
Expand Down
4 changes: 2 additions & 2 deletions bower.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"name": "dbidi",
"version": "0.2.0",
"version": "0.3.0",
"dependencies": {
"dcl": "1.1.x",
"decor": "0.3.x"
"decor": "0.6.x"
},
"devDependencies": {
"requirejs": "2.1.x"
Expand Down
6 changes: 4 additions & 2 deletions docs/TextLayoutEngine.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ The `dbidi/TextLayoutEngine` module provides a bidi transformation engine used f
Engine implements Unicode Bidi Algorithm as specified at [Unicode Standard Annex #9](http://www.unicode.org/reports/tr9/).
Type of the transformation is controlled by parameters, which describe actual format of input and required format of output.
Object holds these parameters as a properties, which allow to reuse them in multiply calls of engine.
Additionally object holds info about relative positions of each character in the source and resulting strings.
This info is placed into sourceToTarget and targetToSource attributes.
Additionally object holds
* info about relative positions of each character in the source and resulting strings;
* info about bidi levels of each character in the source string.
This info is placed into `sourceToTarget`, `targetToSource` and `levels` attributes.

##### Table of Contents
[Instantiation](#instantiation)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "dbidi",
"version": "0.2.0",
"version": "0.3.0",
"repository": {
"type": "git",
"url": "http://github.com/ibm-js/dbidi.git"
Expand Down
10 changes: 9 additions & 1 deletion tests/TextLayoutEngineMapsTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ define([
], function (registerSuite, assert, TextLayoutEngine) {
var engine = new TextLayoutEngine();
registerSuite({
name: "Test Text Layout Engine source-to-target and target-to-source maps",
name: "Test Text Layout Engine source-to-target, target-to-source and level maps",
"(1) implicit ltr -> visual ltr" : function () {
var result = engine.bidiTransform(txt1, "ILYNN", "VLNNN");
assert.equal(engine.sourceToTarget.length, result.length, lengthErr);
Expand Down Expand Up @@ -50,6 +50,14 @@ define([
assert.equal(txt1.charAt(ind), result.charAt(val), contErr);
});
},
"(5) Levels: ltr" : function () {
engine.bidiTransform(txt1, "ILYNN", "VRNNN");
assert.equal(engine.levels.join(""), "1111222000000000", contErr);
},
"(6) Levels: rtl" : function () {
engine.bidiTransform(txt1, "IRYNN", "VLNNN");
assert.equal(engine.levels.join(""), "1111222122222221", contErr);
}
});

var txt1 = "\u05d0\u05d1\u05d2 123 ABC 456.";
Expand Down
Loading

0 comments on commit 7ffbaca

Please sign in to comment.