Text.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*
  2. Copyright (c) 2004-2012, The Dojo Foundation All Rights Reserved.
  3. Available via Academic Free License >= 2.1 OR the modified BSD license.
  4. see: http://dojotoolkit.org/license for details
  5. */
  6. if(!dojo._hasResource["dojox.drawing.stencil.Text"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.drawing.stencil.Text"] = true;
  8. dojo.provide("dojox.drawing.stencil.Text");
  9. dojox.drawing.stencil.Text = dojox.drawing.util.oo.declare(
  10. // summary:
  11. // Creates a dojox.gfx Text (SVG or VML) based on data provided.
  12. // description:
  13. // There are two text classes. TextBlock extends this one and
  14. // adds editable functionality, discovers text width etc.
  15. // This class displays text only. There is no line wrapping.
  16. // Multiple lines can be acheived by inserting \n linebreaks
  17. // in the text.
  18. //
  19. dojox.drawing.stencil._Base,
  20. function(options){
  21. // summary:
  22. // constructor.
  23. },
  24. {
  25. type:"dojox.drawing.stencil.Text",
  26. anchorType:"none",
  27. baseRender:true,
  28. // align: String
  29. // Text horizontal alignment.
  30. // Options: start, middle, end
  31. align:"start",
  32. //
  33. // valign:String
  34. // Text vertical alignment
  35. // Options: top, middle, bottom (FIXME: bottom not supported)
  36. valign:"top",
  37. //
  38. // _lineHeight: [readonly] Number
  39. // The height of each line of text. Based on style information
  40. // and font size.
  41. _lineHeight:1,
  42. /*=====
  43. StencilData: {
  44. // summary:
  45. // The data used to create the dojox.gfx Text
  46. // x: Number
  47. // Left point x
  48. // y: Number
  49. // Top point y
  50. // width: ? Number
  51. // Optional width of Text. Not required but reccommended.
  52. // for auto-sizing, use TextBlock
  53. // height: ? Number
  54. // Optional height of Text. If not provided, _lineHeight is used.
  55. // text: String
  56. // The string content. If not provided, may auto-delete depending on defaults.
  57. },
  58. StencilPoints: [
  59. // summary:
  60. // An Array of dojox.__StencilPoint objects that describe the Stencil
  61. // 0: Object
  62. // Top left point
  63. // 1: Object
  64. // Top right point
  65. // 2: Object
  66. // Bottom right point
  67. // 3: Object
  68. // Bottom left point
  69. ],
  70. =====*/
  71. typesetter: function(text){
  72. // summary:
  73. // Register raw text, returning typeset form.
  74. // Uses function dojox.drawing.stencil.Text.typeset
  75. // for typesetting, if it exists.
  76. //
  77. if(dojox.drawing.util.typeset){
  78. this._rawText = text;
  79. return dojox.drawing.util.typeset.convertLaTeX(text);
  80. }
  81. return text;
  82. },
  83. setText: function(text){
  84. // summary:
  85. // Setter for text.
  86. //
  87. // Only apply typesetting to objects that the user can modify.
  88. // Else, it is assumed that typesetting is done elsewhere.
  89. if(this.enabled){
  90. text = this.typesetter(text);
  91. }
  92. // This only has an effect if text is null or this.created is false.
  93. this._text = text;
  94. this._textArray = [];
  95. this.created && this.render(text);
  96. },
  97. getText: function(){
  98. // summary:
  99. // Getter for text.
  100. //
  101. return this._rawText || this._text;
  102. },
  103. dataToPoints: function(/*Object*/o){
  104. //summary:
  105. // Converts data to points.
  106. o = o || this.data;
  107. var w = o.width =="auto" ? 1 : o.width;
  108. var h = o.height || this._lineHeight;
  109. this.points = [
  110. {x:o.x, y:o.y}, // TL
  111. {x:o.x + w, y:o.y}, // TR
  112. {x:o.x + w, y:o.y + h}, // BR
  113. {x:o.x, y:o.y + h} // BL
  114. ];
  115. return this.points;
  116. },
  117. pointsToData: function(/*Array*/p){
  118. // summary:
  119. // Converts points to data
  120. p = p || this.points;
  121. var s = p[0];
  122. var e = p[2];
  123. this.data = {
  124. x: s.x,
  125. y: s.y,
  126. width: e.x-s.x,
  127. height: e.y-s.y
  128. };
  129. return this.data;
  130. },
  131. render: function(/* String*/text){
  132. // summary:
  133. // Renders the 'hit' object (the shape used for an expanded
  134. // hit area and for highlighting) and the'shape' (the actual
  135. // display object). Text is slightly different than other
  136. // implementations. Instead of calling render twice, it calls
  137. // _createHilite for the 'hit'
  138. // arguments:
  139. // text String
  140. // Changes text if sent. Be sure to use the setText and
  141. // not to call this directly.
  142. //
  143. this.remove(this.shape, this.hit);
  144. //console.log("text render, outline:", !this.annotation, this.renderHit, (!this.annotation && this.renderHit))
  145. !this.annotation && this.renderHit && this._renderOutline();
  146. if(text!=undefined){
  147. this._text = text;
  148. this._textArray = this._text.split("\n");
  149. }
  150. var d = this.pointsToData();
  151. var h = this._lineHeight;
  152. var x = d.x + this.style.text.pad*2;
  153. var y = d.y + this._lineHeight - (this.textSize*.4);
  154. if(this.valign=="middle"){
  155. y -= h/2;
  156. }
  157. this.shape = this.container.createGroup();
  158. /*console.log(" render ", this.type, this.id)
  159. console.log(" render Y:", d.y, "textSize:", this.textSize, "LH:", this._lineHeight)
  160. console.log(" render text:", y, " ... ", this._text, "enabled:", this.enabled);
  161. console.log(" render text:", this.style.currentText);
  162. */
  163. dojo.forEach(this._textArray, function(txt, i){
  164. var tb = this.shape.createText({x: x, y: y+(h*i), text: unescape(txt), align: this.align})
  165. .setFont(this.style.currentText)
  166. .setFill(this.style.currentText.color);
  167. this._setNodeAtts(tb);
  168. }, this);
  169. this._setNodeAtts(this.shape);
  170. },
  171. _renderOutline: function(){
  172. // summary:
  173. // Create the hit and highlight area
  174. // for the Text.
  175. //
  176. if(this.annotation){ return; }
  177. var d = this.pointsToData();
  178. if(this.align=="middle"){
  179. d.x -= d.width/2 - this.style.text.pad * 2;
  180. }else if(this.align=="start"){
  181. d.x += this.style.text.pad;
  182. }else if(this.align=="end"){
  183. d.x -= d.width - this.style.text.pad * 3;
  184. }
  185. if(this.valign=="middle"){
  186. d.y -= (this._lineHeight )/2 - this.style.text.pad;
  187. }
  188. this.hit = this.container.createRect(d)
  189. .setStroke(this.style.currentHit)
  190. .setFill(this.style.currentHit.fill);
  191. //.setFill("#ffff00");
  192. this._setNodeAtts(this.hit);
  193. this.hit.moveToBack();
  194. },
  195. makeFit: function(text, w){
  196. var span = dojo.create('span', {innerHTML:text, id:"foo"}, document.body);
  197. var sz = 1;
  198. dojo.style(span, "fontSize", sz+"px");
  199. var cnt = 30;
  200. while(dojo.marginBox(span).w<w){
  201. sz++;
  202. dojo.style(span, "fontSize", sz+"px");
  203. if(cnt--<=0) break;
  204. }
  205. sz--;
  206. var box = dojo.marginBox(span);
  207. dojo.destroy(span);
  208. return {size:sz, box:box};
  209. }
  210. }
  211. );
  212. dojox.drawing.register({
  213. name:"dojox.drawing.stencil.Text"
  214. }, "stencil");
  215. }