Debian Codegraph @

dojox/drawing/stencil/Text.js

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