| 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 |
|
| 8 |
|
| 9 |
}, |
| 10 |
{ |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
type:"dojox.drawing.stencil.Text", |
| 21 |
anchorType:"none", |
| 22 |
baseRender:true, |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
align:"start", |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
valign:"top", |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
_lineHeight:1, |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
typesetter: function(text){ |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
this._rawText = text; |
| 71 |
return typeset.convertLaTeX(text); |
| 72 |
|
| 73 |
|
| 74 |
}, |
| 75 |
|
| 76 |
setText: function(text){ |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
if(this.enabled){ |
| 83 |
text = this.typesetter(text); |
| 84 |
} |
| 85 |
|
| 86 |
this._text = text; |
| 87 |
|
| 88 |
this._textArray = []; |
| 89 |
this.created && this.render(text); |
| 90 |
}, |
| 91 |
|
| 92 |
getText: function(){ |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
return this._rawText || this._text; |
| 97 |
}, |
| 98 |
|
| 99 |
dataToPoints: function(o){ |
| 100 |
|
| 101 |
|
| 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}, |
| 107 |
{x:o.x + w, y:o.y}, |
| 108 |
{x:o.x + w, y:o.y + h}, |
| 109 |
{x:o.x, y:o.y + h} |
| 110 |
]; |
| 111 |
return this.points; |
| 112 |
}, |
| 113 |
pointsToData: function(p){ |
| 114 |
|
| 115 |
|
| 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(text){ |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
this.remove(this.shape, this.hit); |
| 140 |
|
| 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 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 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 |
|
| 175 |
|
| 176 |
|
| 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 |
|
| 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 |
}); |