Peiman3D
Peiman3D

Reputation: 15

How load Persian Text File in Tlf In AS3?

I have Text File : file.txt:

1301, hi My Flash.

3001, سلام بر تو باد

and in my code for load text file is:

var url:URLRequest = new URLRequest("file.txt");
var n=Number;
var loader:URLLoader = new URLLoader();
loader.load(url);
loader.addEventListener(Event.COMPLETE, loaderComplete);
function loaderComplete(e:Event):void
{
	tt.text=loader.data;
	
}

But My Result is :

1301, hi My Flash.

3001, ÓáÇã ÈÑ Êæ ÈÇÏ

Upvotes: 1

Views: 299

Answers (1)

Kiafar
Kiafar

Reputation: 174

The thing is that data is loaded correctly, (assuming your .txt file is properly encoded in unicode format. use notepad++ to do so) but your TextField is incapable of rendering special unicode text, farsi, arabic or hebrew. unfortunately the only available solution is to utilize "TLFTextField" component which was present in flash cs5 and removed in the later versions due to clunky implementation. anyway though it's a bit late, I post the solution with tlf here cuz working with tlf to render text properly could be real painful, here you go:

  • download "textLayout.swc" and "tlfruntime.swc"
  • add those SWCs to project library.
  • embed your font which contains symbols for your unicode language (your preferred Farsi font in this case) this is very important cuz not every target machine has this font, and this is enough not to get an anti-aliased text, even if the text is shown.
  • make sure you chose FTE(DF4) from ActionScript tab of your font propertise
  • you must instantiate your embedded font. in the following code we have done this. assume embeddedFont is the class name of your embedded font.
  • use the following function to render text into tlf (note that this is not a code written in flash timeline, its written in separate editor, compiled by flex sdk) :

import fl.text.TLFTextField;
import flash.text.AntiAliasType;
import flash.text.engine.FontLookup;
import flash.text.Font;
import flash.text.TextFieldAutoSize;
import flash.utils.getDefinitionByName;
import flashx.textLayout.elements.TextFlow;
import flashx.textLayout.formats.TextAlign;
import flashx.textLayout.formats.TextLayoutFormat;
import flashx.textLayout.formats.VerticalAlign;
private var _font:Font= new embeddedFont() as Font;
/**
 * Creates a center registered TLFTextField with the given text
 * @param   text targer text
 * @param   layoutFormat an object containing layoutFormat data. something like { textIndent: 8, color: 0xAAAAAA, fontFamily: "tahoma", fontSize: 28, autoSize: "center", antiAliasType: "advanced" }
 * @param   width width of the TLFTextField, auto set if 0 
 * @param   height height of the TLFTextField, auto set if 0 
 * @return  a center registered TLFTextField with the given text
 */
public function text(text:String,  layoutFormat:Object = null, width:int = 0, height:int = 0):TLFTextField
{
    var _txt:TLFTextField = new TLFTextField();
    var _layoutFormat:TextLayoutFormat = new TextLayoutFormat();
    if (layoutFormat == null) layoutFormat = new Object();
    // creating the textfield
    if (width != 0) _txt.width = width;
    if (height != 0) _txt.height = height;
    _txt.selectable = false;
    _txt.embedFonts = true;
    _txt.multiline = true;
    //if either width or height are 0(not passed to function) and the wordWrap is true, autoSize wont work and width or height will be 0
    if (width != 0 && height != 0) _txt.wordWrap = true;
    if (layoutFormat.backgroud != undefined) _txt.background = layoutFormat.backgroud;
    if (layoutFormat.backgroundColor != undefined) _txt.backgroundColor = layoutFormat.backgroundColor;
    _txt.autoSize = (layoutFormat.autoSize != undefined)?(layoutFormat.autoSize) : (TextFieldAutoSize.CENTER);
    _txt.verticalAlign=(layoutFormat.verticalAlign != undefined)?(layoutFormat.verticalAlign) : (VerticalAlign.MIDDLE)
    _txt.antiAliasType = (layoutFormat.antiAliasType != undefined)?(layoutFormat.antiAliasType) : (AntiAliasType.ADVANCED);
    // creating layout format
    _layoutFormat.textAlign = (layoutFormat.textAlign != undefined)?(layoutFormat.textAlign):(TextAlign.CENTER);
    _layoutFormat.textIndent = (layoutFormat.textIndent != undefined)?(layoutFormat.textIndent) : (8);
    _layoutFormat.fontLookup = FontLookup.EMBEDDED_CFF;
    _layoutFormat.color = (layoutFormat.color != undefined)?(layoutFormat.color) : (0xFFFFFF);
    _layoutFormat.fontFamily = (layoutFormat.fontFamily != undefined)?(layoutFormat.fontFamily) : (_font.fontName);
    _layoutFormat.fontSize = (layoutFormat.fontSize != undefined)?(layoutFormat.fontSize) : (42);
    //setting text flow, text and attaching layout format
    var _textFlow:TextFlow = _txt.textFlow;
    _textFlow.hostFormat = _layoutFormat;
    _textFlow.flowComposer.updateAllControllers();
    _txt.text = text;
    return _txt;
}

  • use this code to get a TLFTextField and add it to display:

private var _tlf:TLFTextField = text("سلام", { textIndent: 8, color: 0xAAAAAA, fontSize: 28, autoSize: "center", antiAliasType: "advanced" } );
stage.addChild(_tlf);

just note that in my experience there is a 100 milisec lag upon creating tlf text in RTL languages. make sure your create all texts in the right time (when no animation, audio or video is on) or you get a tiny halt enough to ruin your user experience.

Upvotes: 1

Related Questions