// // Bidirectional Textfield Prototype v1.0 // // Tries to render a Hebrew or mixed text into a textfield // Tries to handle Hebrew input into textfields // // Author: Mario Klingemann (mario@coma2.com) // // Usage: // anyTextfield.setBidiText("any testtext") // This is the standard method which is used for multine texts // // anyTextfield.setBidiText("any testtext",true) // This forces the text into one line. This is useful for headlines. // Due to the low precision of getTextExtent it can happen that Flash // will not fill a line even if there is enough space for a word left // // anyTextfield.initBidiInput() // adds Bidirectionial Text handling for inputfields // // anyTextfield.removeBidiInput() // removes Bidirectionial Text handling from inputfields // // // Shortcomings: // - does not render HTML text // - precision of .getTextExtend is limited, this can // lead to cases where the layout is not satisfying // - does not handle cases of deep nested mixed text // - works only withj embedded fonts, otherwise measuring // of text width is not possible and interference with // system routines might occur // // DO NOT USE AUTOFORMAT ON THIS CODE! Due to a bug in Flash this would // destroy the Unicode char string in line 140 // TextField.prototype.setBidiText = function(text,forceOneLine) { var w, s, Hebrew, maxWidth, format, words, currentLine, spac, currentHebrew, currentWord, testLine, lineLength, space, testTempLine, tempLine; maxWidth = this._width; format = this.getTextFormat(); paragraphs = text.bidi_cleanWhitespace().split("\n"); this.text="" for (s=0; smaxWidth) { this.replaceSel(currentLine+"\n"); testLine = ""; currentLine = currentWord; } else { currentLine = testLine; } } else { if (lineLength>maxWidth) { this.replaceSel(tempLine+space+currentLine+"\n"); testLine = ""; if (currentWord.bidi_isEnd() && (!words[w-1].bidi_isHebrewWord() && (words[w+1].bidi_isHebrewWord() || ((words[w+1] == null) && words[w+2].isHebrew())))) { testTempLine = currentWord.bidi_flipLastChar(); } else { testTempLine = currentWord; } tempLine = ""; currentLine = ""; testLine = ""; } else { tempLine = testTempLine; } } } else { if (currentHebrew) { currentLine = testLine; } else { tempLine = testTempLine; } } } if (currentHebrew) { if (currentLine != "") { this.replaceSel(currentLine+"\n"); } } else { if (testTempLine != "") { this.replaceSel(testTempLine+space+currentLine+"\n"); testTempLine = ""; } } tempLine = ""; testLine = ""; currentLine = ""; } }; String.prototype.bidi_revert = function() { return (this.charAt(0).bidi_bracketRevert()+this.substr(1, this.length-2)+this.charAt(this.length-1).bidi_bracketRevert()).split("").reverse().join(""); }; String.prototype.bidi_isHebrewWord = function() { for (var i = 0; i-1); } return ('\u05d0\u05d1\u05d2\u05d3\u05d4\u05d5\u05d6\u05d7\u05d8\u05d9\u05da\u05db\u05dc\u05dd\u05de\u05df\u05e0\u05e1\u05e2\u05e3\u05e4\u05e5\u05e6\u05e7\u05e8\u05e9\u05ea'.indexOf(this.charAt(n))>-1); } String.prototype.bidi_isNeutral = function(n) { var chars = "?!{}[]()-=,.*\"' :;"; if (chars.indexOf(this.charAt(n)) == -1) { return 0; } else { return 1; } }; String.prototype.bidi_isEnd = function() { var chars = "?!.:,;"; if (chars.indexOf(this.charAt(this.length-1)) == -1) { return 0; } else { return 1; } }; String.prototype.bidi_flipLastChar = function() { if (this.charAt(0) == " ") { return this.charAt(this.length-1)+this.substr(1, this.length-2); } else { return this.charAt(this.length-1)+this.substr(0, this.length-1); } }; String.prototype.bidi_bracketRevert = function() { var s = this.toString(); switch (s) { case "(" : return ")"; break; case ")" : return "("; break; case "[" : return "]"; break; case "]" : return "["; break; case "}" : return "{"; break; case "{" : return "}"; break; default : return this; break; } }; String.prototype.bidi_CleanWhitespace = function() { var s = this.split("\r").join("\n"); while (s.indexOf(" ")>-1) { s = s.split(" ").join(" "); } return s; }; Textfield.prototype.initBidiInput = function() { this.addListener(this); this.onChanged = this.bidiProcess; this.lastText = this.text; this.lastHebrew = false; }; Textfield.prototype.removeBidiInput = function() { this.removeListener(this); this.onChanged = null; }; Textfield.prototype.bidiProcess = function() { if (this.text == this.lastText) { return; } var insertEnd = Selection.getCaretIndex(); var newCheck = this.text.substr(0, insertEnd); var oldCheck = this.lastText.substr(0, this.lastText.length-(this.text.length-insertEnd)); var insertStart = 0; for (var i = 0; i1) { var words = dif.explode(" "); var currentLine, currentWord, currentHebrew, tempLine, testLine; var Hebrew = this.lastHebrew; var space = ""; if (words == null) { words = [dif]; } for (var w=0; w