1  /*
     2   Copyright aswing.org, see the LICENCE.txt.
     3  */
     4   
     5  import org.aswing.ASColor;
     6  import org.aswing.ASFont;
     7  import org.aswing.ASTextExtent;
     8  
     9  /**
    10   * ASTextFormat
    11   * @author Tomato
    12   */
    13   
    14   /**
    15    * unHandled TextFormat Properties :
    16    * 	
    17    * 	<li>TextFormat.leading</li>
    18    * 	<li>TextFormat.tabStops</li>
    19    * 	<li>TextFormat.target</li>
    20    * 	<li>TextFormat.url</li>
    21    */
    22   
    23   class org.aswing.ASTextFormat{
    24   	
    25   	/**
    26   	 * align -> left
    27   	 */
    28   	public static var LEFT:Number = -1;
    29   	/**
    30   	 * align -> center
    31   	 */
    32   	public static var CENTER:Number = 0;
    33   	/**
    34   	 * align -> right
    35   	 */
    36   	public static var RIGHT:Number = 1;
    37   	
    38   	/**
    39   	 * Default values
    40   	 * You can customize these values to match your major text format style
    41   	 * And then just use little codes to set some special ones
    42   	 */
    43   	public static var DEFAULT_NAME:String = "Arial";
    44   	public static var DEFAULT_SIZE:Number = 12;
    45   	public static var DEFAULT_BOLD:Boolean = false;
    46   	public static var DEFAULT_ITALIC:Boolean = false;
    47   	public static var DEFAULT_UNDERLINE:Boolean = false;
    48   	public static var DEFAULT_EMBEDFONTS:Boolean = false;
    49   	public static var DEFAULT_ALIGN:Number = LEFT;
    50   	public static var DEFAULT_BLOCKINDENT:Number = 0;
    51   	public static var DEFAULT_INDENT:Number = 0;
    52   	public static var DEFAULT_BULLET:Boolean = false;
    53   	public static function get DEFAULT_COLOR():Number{
    54   		return ASColor.BLACK.getRGB();
    55   	}
    56   	public static var DEFAULT_LEFTMARGIN:Number = 0;
    57   	public static var DEFAULT_RIGHTMARGIN:Number = 0;
    58   	
    59   	/**
    60   	 * 
    61   	 */
    62   	private var _name:String;
    63   	private var _size:Number;
    64   	private var _bold:Boolean;
    65   	private var _italic:Boolean;
    66   	private var _underline:Boolean;
    67   	private var _align:Number;
    68   	private var _blockIndent:Number;
    69   	private var _indent:Number;
    70   	private var _bullet:Boolean;
    71   	private var _color:Number;
    72   	private var _leftMargin:Number;
    73   	private var _rightMargin:Number;
    74   	private var _embedFonts:Boolean;
    75   	
    76   	/**
    77   	 * private constructor
    78   	 */
    79   	private function ASTextFormat(){
    80   		
    81   	}
    82   	
    83   	/**
    84   	 * use these create methods to get an ASTextFormat instance
    85   	 */
    86   	public static function getEmptyASTextFormat():ASTextFormat{
    87   		return new ASTextFormat();
    88   	}
    89   	
    90   	public static function getASTextFormatWithName(name:String):ASTextFormat{
    91   		var asTF:ASTextFormat = new ASTextFormat();
    92   		asTF.setName(name);
    93   		return asTF;
    94   	}
    95   	
    96   	public static function getASTextFormatWithNameSize(name:String , size:Number):ASTextFormat{
    97   		var asTF:ASTextFormat = new ASTextFormat();
    98   		asTF.setName(name);
    99   		asTF.setSize(size);
   100   		return asTF;
   101   	}
   102   	
   103   	public static function getASTextFormatWithNameColor(name:String , color:Number):ASTextFormat{
   104   		var asTF:ASTextFormat = new ASTextFormat();
   105   		asTF.setName(name);
   106   		asTF.setColor(color);
   107   		return asTF;
   108   	}
   109   	
   110   	public static function getASTextFormatWithNameSizeColor(name:String , size:Number , color:Number):ASTextFormat{
   111   		var asTF:ASTextFormat = new ASTextFormat();
   112   		asTF.setName(name);
   113   		asTF.setSize(size);
   114   		asTF.setColor(color);
   115   		return asTF;
   116   	}
   117   	
   118   	public static function getASTextFormatWithASFont(font:ASFont):ASTextFormat{
   119   		var asTF:ASTextFormat = new ASTextFormat();
   120   		asTF.setASFont(font);
   121   		return asTF;
   122   	}
   123   	 	 
   124   	private function getTextFormat():TextFormat{
   125   		var tf:TextFormat = new TextFormat();
   126   		
   127   		tf.font = ASTextFormat.DEFAULT_NAME;
   128   		tf.size = ASTextFormat.DEFAULT_SIZE;
   129  		tf.bold = ASTextFormat.DEFAULT_BOLD;
   130  		tf.italic = ASTextFormat.DEFAULT_ITALIC;
   131  	 	tf.underline = ASTextFormat.DEFAULT_UNDERLINE;
   132  	 	switch(DEFAULT_ALIGN){
   133  	 		case ASTextFormat.LEFT:
   134  	 			tf.align = "left";
   135  	 		case ASTextFormat.CENTER:
   136  	 			tf.align = "center";
   137  	 		case ASTextFormat.RIGHT:
   138  	 			tf.align = "right";
   139  	 		default:
   140  	 			tf.align = "left";
   141  	 	}
   142  	 	tf.blockIndent = ASTextFormat.DEFAULT_BLOCKINDENT;
   143  	 	tf.indent = ASTextFormat.DEFAULT_INDENT;
   144  	 	tf.bullet = ASTextFormat.DEFAULT_BULLET;
   145  	 	tf.color = ASTextFormat.DEFAULT_COLOR;
   146  	 	tf.leftMargin = ASTextFormat.DEFAULT_LEFTMARGIN;
   147  	 	tf.rightMargin = ASTextFormat.DEFAULT_RIGHTMARGIN;
   148  	 	
   149  	 	if(this._name != null){
   150  	 		tf.font = this._name;
   151  	 	}
   152  	 	if(this._size != null){
   153  	 		tf.size = this._size;
   154  	 	}
   155  	 	if(this._bold != null){
   156  	 		tf.bold = this._bold;
   157  	 	}
   158  	 	if(this._italic != null){
   159  	 		tf.italic = this._italic;
   160  	 	}
   161  	 	if(this._underline != null){
   162  	 		tf.underline = this._underline;
   163  	 	}
   164  	 	if(this._align != null){
   165  	 		switch(this._align){
   166  		 		case ASTextFormat.LEFT:
   167  		 			tf.align = "left";
   168  		 		case ASTextFormat.CENTER:
   169  		 			tf.align = "center";
   170  		 		case ASTextFormat.RIGHT:
   171  		 			tf.align = "right";
   172  		 		default:
   173  		 			tf.align = "left";
   174  	 		}
   175  	 	}
   176  	 	if(this._blockIndent != null){
   177  	 		tf.blockIndent = this._blockIndent;
   178  	 	}
   179  	 	if(this._indent != null){
   180  	 		tf.indent = this._indent;
   181  	 	}
   182  	 	if(this._bullet != null){
   183  	 		tf.bullet = this._bullet;
   184  	 	}
   185  	 	if(this._color != null){
   186  	 		tf.color = this._color;
   187  	 	}
   188  	 	if(this._leftMargin != null){
   189  	 		tf.leftMargin = this._leftMargin;
   190  	 	}
   191  	 	if(this._rightMargin != null){
   192  	 		tf.rightMargin = this._rightMargin;
   193  	 	}
   194  	 	return tf;
   195   	} 	
   196   	 
   197   	public function setName(newName:String):Void{
   198   		this._name = newName;
   199   	}
   200   	
   201   	public function getName():String{
   202   		return this._name;
   203   	}
   204   	
   205   	public function setSize(newSize:Number):Void{
   206   		this._size = newSize;
   207   	}
   208   	
   209   	public function getSize():Number{
   210   		return this._size;
   211   	}
   212   	
   213   	public function setBold(newBold:Boolean):Void{
   214   		this._bold = newBold;
   215   	}
   216   	
   217   	public function getBold():Boolean{
   218   		return this._bold;
   219   	}
   220   	
   221   	public function setItalic(newItalic:Boolean):Void{
   222   		this._italic = newItalic;
   223   	}
   224   	
   225   	public function getItalic():Boolean{
   226   		return this._italic;
   227   	}
   228   	
   229   	public function setUnderline(newUnderline:Boolean):Void{
   230   		this._underline = newUnderline;
   231   	}
   232   	
   233   	public function getUnderline():Boolean{
   234   		return this._underline;
   235   	}
   236   	
   237   	public function setAlign(newAlign:Number):Void{
   238   		this._align = newAlign;
   239   	}
   240   	
   241   	public function getAlign():Number{
   242   		return this._align;
   243   	}
   244   	
   245   	public function setBlockIndent(newBlockIndent:Number):Void{
   246   		this._blockIndent = newBlockIndent;
   247   	}
   248   	
   249   	public function getBlockIndent():Number{
   250   		return this._blockIndent;
   251   	}
   252   	
   253   	public function setIndent(newIndent:Number):Void{
   254   		this._indent = newIndent;
   255   	}
   256   	
   257   	public function getIndent():Number{
   258   		return this._indent;
   259   	}
   260   	
   261   	public function setBullet(newBullet:Boolean):Void{
   262   		this._bullet = newBullet;
   263   	}
   264   	
   265   	public function getBullet():Boolean{
   266   		return this._bullet;
   267   	}
   268   	
   269   	public function setColor(newColor:Number):Void{
   270   		this._color = newColor;
   271   	}
   272   	
   273   	public function getColor():Number{
   274   		return this._color;
   275   	}
   276   	
   277   	public function setLeftMargin(newLeftMargin:Number):Void{
   278   		this._leftMargin = newLeftMargin;
   279   	}
   280   	
   281   	public function getLeftMargin():Number{
   282   		return this._leftMargin;
   283   	}
   284   	
   285   	public function setRightMargin(newRightMargin:Number):Void{
   286   		this._rightMargin = newRightMargin;
   287   	}
   288   	
   289   	public function getRightMargin():Number{
   290   		return this._rightMargin;
   291   	}
   292   	
   293   	public function setEmbedFonts(b:Boolean):Void{
   294   		if(b == undefined) b = DEFAULT_EMBEDFONTS;
   295   		_embedFonts = b;
   296   	}
   297   	
   298   	public function getEmbedFonts():Boolean{
   299   		return _embedFonts;
   300   	}
   301   	 	 	
   302   	public function getTextExtent(text:String, wrapWidth:Number):ASTextExtent{
   303   		var tempTextFormat:TextFormat = this.getTextFormat();
   304   		var infos:Object;
   305   		if(wrapWidth != null){
   306   			infos = tempTextFormat.getTextExtent(text , wrapWidth);
   307   		}else{
   308   			infos = tempTextFormat.getTextExtent(text);
   309   		}
   310   		return new ASTextExtent(infos.width, infos.height, infos.ascent, infos.descent, 
   311   								infos.textFieldWidth, infos.textFieldHeight);
   312   	}
   313   	
   314   	public function setASFont(font:ASFont):Void{
   315   		this.setName(font.getName());
   316   		this.setSize(font.getSize());
   317   		this.setBold(font.getBold());
   318   		this.setItalic(font.getItalic());
   319   		this.setUnderline(font.getUnderline());
   320   		this.setEmbedFonts(font.getEmbedFonts());
   321   	}
   322   	
   323   	public function getASFont():ASFont{
   324   		return new ASFont(this.getName() , this.getSize() , this.getBold() , this.getItalic() , this.getUnderline(), this.getEmbedFonts());
   325   	}
   326   	
   327   	
   328   	
   329   	/**
   330   	 * apply to the whole TextField
   331   	 */
   332   	public function applyToText(text:TextField):Void{
   333   		text.setTextFormat(this.getTextFormat());
   334   		text.embedFonts = getEmbedFonts();
   335   	}
   336   	
   337   	/**
   338   	 * apply to part of the TextField
   339   	 */
   340   	public function applyToPartOfText(text:TextField , begin:Number , end:Number):Void{
   341   		text.setTextFormat(begin , end , this.getTextFormat());
   342   		text.embedFonts = getEmbedFonts();
   343   	}
   344   	
   345   	/**
   346   	 * apply one char of the TextField
   347   	 */
   348   	public function applyToOneOfText(text:TextField , index:Number):Void{
   349   		text.setTextFormat(index , this.getTextFormat());
   350   		text.embedFonts = getEmbedFonts();
   351   	}
   352   	
   353   	/**
   354   	 * apply for new Text of the TextField
   355   	 */
   356   	public function applyToTextForNew(text:TextField):Void{
   357   		text.setNewTextFormat(this.getTextFormat());
   358   		text.embedFonts = getEmbedFonts();
   359   	}
   360   	
   361   	public function clone():ASTextFormat{
   362   		var t:ASTextFormat = new ASTextFormat();
   363   		t._align = _align;
   364   		t._blockIndent = _blockIndent;
   365   		t._bold = _bold;
   366   		t._bullet = _bullet;
   367   		t._color = _color;
   368   		t._indent = _indent;
   369   		t._italic = _italic;
   370   		t._leftMargin = _leftMargin;
   371   		t._name = _name;
   372   		t._rightMargin = _rightMargin;
   373   		t._size = _size;
   374   		t._underline = _underline;
   375   		t._embedFonts = _embedFonts;
   376   		return t;
   377   	}
   378   		
   379   	public function toString():String{
   380  		return "ASTextFormat";
   381  	}
   382   	
   383   }
   384