/com/zehfernando/display/components/text/TextSpriteColumns.as

https://github.com/duponce/as3 · ActionScript · 285 lines · 159 code · 55 blank · 71 comment · 22 complexity · a983363ef646b2ccf79b1088b82796a7 MD5 · raw file

  1. package com.zehfernando.display.components.text {
  2. import flash.display.Sprite;
  3. import flash.text.engine.TextLine;
  4. import flash.text.engine.TextLineCreationResult;
  5. /**
  6. * @author zeh
  7. */
  8. public class TextSpriteColumns extends Sprite {
  9. // Properties
  10. protected var _width:Number;
  11. protected var _height:Number;
  12. protected var _text:String;
  13. protected var _columnWidth:Number;
  14. protected var _margins:Number;
  15. protected var _maxHeight:Number;
  16. protected var _maxColumns:int;
  17. protected var _leading:Number;
  18. protected var _tracking:Number;
  19. protected var _font:String;
  20. protected var _size:Number;
  21. protected var _color:Number;
  22. protected var _columns:int;
  23. protected var textSprites:Vector.<RichTextSprite>;
  24. protected var baseTextSprite:RichTextSprite; // Used for the styles
  25. // Instances
  26. protected var textContainer:Sprite;
  27. /*
  28. Changelog
  29. 2010 02 06 -- Changed to use the new Text Engine, be aligned to the top left
  30. // http://www.insideria.com/2009/03/flash-text-engine.html
  31. */
  32. // ================================================================================================================
  33. // CONSTRUCTOR ----------------------------------------------------------------------------------------------------
  34. public function TextSpriteColumns(__font:String = "_sans", __size:Number = 12, __color:Number = 0x000000, __columnWidth:Number = 100, __margins:Number = 10, __maxHeight:Number = 100, __maxColumns:int = 1000) {
  35. // TODO: add bold, italic (fontPosture)
  36. // Set default values
  37. _height = 0;
  38. _text = "";
  39. _columnWidth = __columnWidth;
  40. _margins = __margins;
  41. _maxColumns = __maxColumns;
  42. _maxHeight = __maxHeight;
  43. _leading = 0;
  44. _tracking = 0;
  45. _columns = 0;
  46. _font = __font;
  47. _size = __size;
  48. _color = __color;
  49. baseTextSprite = new RichTextSprite();
  50. textSprites = new Vector.<RichTextSprite>();
  51. // Create visual assets
  52. textContainer = new Sprite();
  53. addChild(textContainer);
  54. // Finally, redraws everything
  55. redraw();
  56. }
  57. // ================================================================================================================
  58. // INTERNAL functions ---------------------------------------------------------------------------------------------
  59. protected function redraw(): void {
  60. // Redraws everything
  61. removeAllColumns();
  62. // Create all columns
  63. var ts:RichTextSprite;
  64. var lastLine:TextLine = null;
  65. var posX:Number = 0;
  66. _columns = 0;
  67. //trace ("---creating");
  68. while((_columns == 0 || Boolean(lastLine)) && _columns <= _maxColumns && !(Boolean(ts) && ts.textLineCreationResult == TextLineCreationResult.COMPLETE)) {
  69. //trace (" -- column " + _columns);
  70. // Breaks text if it's already past the maximum
  71. // TODO: test this better or find some other way to detect it
  72. //if (Boolean(lastLine) && lastLine.textBlockBeginIndex + lastLine.atomCount >= getTagLessText().length) break; // This sucks -- use a better method to detect whether there's additional lines
  73. //if (Boolean(lastLine) && lastLine.textBlockBeginIndex + lastLine.atomCount >= _text.length) break;
  74. //trace ("col " + _numColumns, lastLine, getTagLessText().length, Boolean(lastLine) ? lastLine.textBlockBeginIndex + lastLine.atomCount : "");
  75. //if (Boolean(lastLine)) trace (" --> ",lastLine, lastLine.textBlockBeginIndex, lastLine.atomCount, getTagLessText().length);
  76. //if (Boolean(ts)) trace(" ---> " + ts.textLineCreationResult);
  77. ts = new RichTextSprite(_font, _size, _color);
  78. ts.trimFirstLineIfBlank = _columns > 0;
  79. ts.setTextStyles(baseTextSprite.getTextStyles());
  80. ts.x = posX;
  81. ts.maxHeight = _maxHeight;
  82. ts.width = _columnWidth;
  83. ts.leading = _leading;
  84. if (_columns == 0) {
  85. ts.tracking = _tracking;
  86. ts.text = _text;
  87. }
  88. ts.previousTextLine = lastLine;
  89. lastLine = ts.lastTextLine;
  90. textContainer.addChild(ts);
  91. textSprites.push(ts);
  92. posX += _columnWidth;
  93. posX += _margins;
  94. _columns++;
  95. }
  96. //trace ("numColumns =================== " + _numColumns);
  97. _width = _columns > 0 ? posX - _margins : 0;
  98. }
  99. // protected function getTagLessText(): String {
  100. // // This sucks
  101. // var txt:String = _text;
  102. // var tag:String;
  103. // for (var i:int = 0; i < baseTextSprite.getTextStyles().length; i++) {
  104. // tag = baseTextSprite.getTextStyles()[i].name;
  105. // txt = txt.split("<"+tag+">").join("");
  106. // txt = txt.split("<"+tag+"/>").join("");
  107. // }
  108. //
  109. // return txt;
  110. // }
  111. protected function removeAllColumns(): void {
  112. while (Boolean(textSprites) && textSprites.length > 0) {
  113. textContainer.removeChild(textSprites[0]);
  114. textSprites.splice(0, 1);
  115. }
  116. }
  117. // ================================================================================================================
  118. // PUBLIC INTERFACE -----------------------------------------------------------------------------------------------
  119. public function setStyle(__name:String, __fontName:String = "", __fontSize:Number = NaN, __color:Number = NaN):void {
  120. baseTextSprite.setStyle(__name, __fontName, __fontSize, __color);
  121. }
  122. // ================================================================================================================
  123. // ACCESSOR functions ---------------------------------------------------------------------------------------------
  124. override public function get width(): Number {
  125. return _width;
  126. }
  127. override public function set width(__value:Number): void {
  128. throw new Error("You cannot set the width of a TextSpriteColumns instance");
  129. }
  130. override public function get height(): Number {
  131. return _height;
  132. }
  133. override public function set height(__value:Number): void {
  134. throw new Error ("Warning: you cannot set the height of a TextSpriteColumns instance.");
  135. }
  136. public function get text(): String {
  137. return _text;
  138. }
  139. public function set text(__value:String): void {
  140. if (_text != __value) {
  141. _text = __value;
  142. redraw();
  143. //if (textSprites.length > 0) textSprites[0].text = _text;
  144. }
  145. }
  146. public function get leading(): Number {
  147. return _leading;
  148. }
  149. public function set leading(__value:Number): void {
  150. if (_leading != __value) {
  151. _leading = __value;
  152. redraw();
  153. }
  154. }
  155. // // FontDescription extensions
  156. //
  157. // public function get renderingMode(): String {
  158. // return fontDescription.renderingMode;
  159. // }
  160. // public function set renderingMode(__value:String): void {
  161. // if (renderingMode != __value) {
  162. // fontDescription.renderingMode = __value;
  163. // redraw();
  164. // }
  165. // }
  166. //
  167. // public function get cffHinting(): String {
  168. // return fontDescription.cffHinting;
  169. // }
  170. // public function set cffHinting(__value:String): void {
  171. // if (cffHinting != __value) {
  172. // fontDescription.cffHinting = __value;
  173. // redraw();
  174. // }
  175. // }
  176. // This doesn't work, or maybe depends on specific font features
  177. public function get tracking(): Number {
  178. return _tracking;
  179. }
  180. public function set tracking(__value:Number): void {
  181. if (_tracking != __value) {
  182. _tracking = __value;
  183. redraw();
  184. //if (textSprites.length > 0) textSprites[0].tracking = __value;
  185. }
  186. }
  187. public function get trackingAsPhotoshop(): Number {
  188. return _tracking / _size * 1000;
  189. }
  190. public function set trackingAsPhotoshop(__value:Number): void {
  191. tracking = __value / 1000 * _size;
  192. }
  193. public function get leadingAsPhotoshop(): Number {
  194. return _size + _leading;
  195. }
  196. public function set leadingAsPhotoshop(__value:Number): void {
  197. leading = __value - _size;
  198. }
  199. public function get columnWidth(): Number {
  200. return _columnWidth;
  201. }
  202. public function set columnWidth(__value:Number): void {
  203. if (_columnWidth != __value) {
  204. _columnWidth = __value;
  205. redraw();
  206. }
  207. }
  208. public function get margins(): Number {
  209. return _margins;
  210. }
  211. public function set margins(__value:Number): void {
  212. if (_margins != __value) {
  213. _margins = __value;
  214. }
  215. }
  216. public function get columns(): Number {
  217. return _columns;
  218. }
  219. public function get maxHeight(): Number {
  220. return _maxHeight;
  221. }
  222. public function set maxHeight(__value:Number): void {
  223. if (_maxHeight != __value) {
  224. _maxHeight = __value;
  225. redraw();
  226. }
  227. }
  228. }
  229. }