| 1 |
define(["./_base", "dojo/_base/lang","dojo/_base/sniff", "dojo/dom", "dojo/_base/html", "dojo/_base/array", |
| 2 |
"./utils", "./shape", "./path", "dojox/string/BidiEngine"], |
| 3 |
function(g, lang, has, dom, html, arr, utils, shapeLib, pathLib, BidiEngine){ |
| 4 |
lang.getObject("dojox.gfx._gfxBidiSupport", true); |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
switch (g.renderer){ |
| 13 |
case 'vml': |
| 14 |
g.isVml = true; |
| 15 |
break; |
| 16 |
case 'svg': |
| 17 |
g.isSvg = true; |
| 18 |
if(g.svg.useSvgWeb){ |
| 19 |
g.isSvgWeb = true; |
| 20 |
} |
| 21 |
break; |
| 22 |
case 'silverlight': |
| 23 |
g.isSilverlight = true; |
| 24 |
break; |
| 25 |
case 'canvas': |
| 26 |
case 'canvasWithEvents': |
| 27 |
g.isCanvas = true; |
| 28 |
break; |
| 29 |
} |
| 30 |
|
| 31 |
var bidi_const = { |
| 32 |
LRM : '\u200E', |
| 33 |
LRE : '\u202A', |
| 34 |
PDF : '\u202C', |
| 35 |
RLM : '\u200f', |
| 36 |
RLE : '\u202B' |
| 37 |
}; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
var bidiEngine = new BidiEngine(); |
| 43 |
|
| 44 |
lang.extend(g.shape.Surface, { |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
textDir: "", |
| 55 |
|
| 56 |
setTextDir: function(newTextDir){ |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
setTextDir(this, newTextDir); |
| 61 |
}, |
| 62 |
|
| 63 |
getTextDir: function(){ |
| 64 |
return this.textDir; |
| 65 |
} |
| 66 |
}); |
| 67 |
|
| 68 |
lang.extend(g.Group, { |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
textDir: "", |
| 73 |
|
| 74 |
setTextDir: function(newTextDir){ |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
setTextDir(this, newTextDir); |
| 79 |
}, |
| 80 |
|
| 81 |
getTextDir: function(){ |
| 82 |
return this.textDir; |
| 83 |
} |
| 84 |
}); |
| 85 |
|
| 86 |
lang.extend(g.Text, { |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
textDir: "", |
| 99 |
|
| 100 |
formatText: function ( text, textDir){ |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
if(textDir && text && text.length > 1){ |
| 132 |
var sourceDir = "ltr", targetDir = textDir; |
| 133 |
|
| 134 |
if(targetDir == "auto"){ |
| 135 |
|
| 136 |
if(g.isVml){ |
| 137 |
return text; |
| 138 |
} |
| 139 |
targetDir = bidiEngine.checkContextual(text); |
| 140 |
} |
| 141 |
|
| 142 |
if(g.isVml){ |
| 143 |
sourceDir = bidiEngine.checkContextual(text); |
| 144 |
if(targetDir != sourceDir){ |
| 145 |
if(targetDir == "rtl"){ |
| 146 |
return !bidiEngine.hasBidiChar(text) ? bidiEngine.bidiTransform(text,"IRNNN","ILNNN") : bidi_const.RLM + bidi_const.RLM + text; |
| 147 |
}else{ |
| 148 |
return bidi_const.LRM + text; |
| 149 |
} |
| 150 |
} |
| 151 |
return text; |
| 152 |
} |
| 153 |
|
| 154 |
if(g.isSvgWeb){ |
| 155 |
if(targetDir == "rtl"){ |
| 156 |
return bidiEngine.bidiTransform(text,"IRNNN","ILNNN"); |
| 157 |
} |
| 158 |
return text; |
| 159 |
} |
| 160 |
|
| 161 |
if(g.isSilverlight){ |
| 162 |
return (targetDir == "rtl") ? bidiEngine.bidiTransform(text,"IRNNN","VLYNN") : bidiEngine.bidiTransform(text,"ILNNN","VLYNN"); |
| 163 |
} |
| 164 |
|
| 165 |
if(g.isCanvas){ |
| 166 |
return (targetDir == "rtl") ? bidi_const.RLE + text + bidi_const.PDF : bidi_const.LRE + text + bidi_const.PDF; |
| 167 |
} |
| 168 |
|
| 169 |
if(g.isSvg){ |
| 170 |
if(has("ff") < 4){ |
| 171 |
return (targetDir == "rtl") ? bidiEngine.bidiTransform(text,"IRYNN","VLNNN") : bidiEngine.bidiTransform(text,"ILYNN","VLNNN"); |
| 172 |
}else{ |
| 173 |
return bidi_const.LRM + (targetDir == "rtl" ? bidi_const.RLE : bidi_const.LRE) + text + bidi_const.PDF; |
| 174 |
} |
| 175 |
} |
| 176 |
} |
| 177 |
return text; |
| 178 |
}, |
| 179 |
|
| 180 |
bidiPreprocess: function(newShape){ |
| 181 |
return newShape; |
| 182 |
} |
| 183 |
}); |
| 184 |
|
| 185 |
lang.extend(g.TextPath, { |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
textDir: "", |
| 194 |
|
| 195 |
formatText: function (text, textDir){ |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
if(textDir && text && text.length > 1){ |
| 218 |
var sourceDir = "ltr", targetDir = textDir; |
| 219 |
|
| 220 |
if(targetDir == "auto"){ |
| 221 |
|
| 222 |
if(g.isVml){ |
| 223 |
return text; |
| 224 |
} |
| 225 |
targetDir = bidiEngine.checkContextual(text); |
| 226 |
} |
| 227 |
|
| 228 |
if(g.isVml){ |
| 229 |
sourceDir = bidiEngine.checkContextual(text); |
| 230 |
if(targetDir != sourceDir){ |
| 231 |
if(targetDir == "rtl"){ |
| 232 |
return !bidiEngine.hasBidiChar(text) ? bidiEngine.bidiTransform(text,"IRNNN","ILNNN") : bidi_const.RLM + bidi_const.RLM + text; |
| 233 |
}else{ |
| 234 |
return bidi_const.LRM + text; |
| 235 |
} |
| 236 |
} |
| 237 |
return text; |
| 238 |
} |
| 239 |
if(g.isSvgWeb){ |
| 240 |
if(targetDir == "rtl"){ |
| 241 |
return bidiEngine.bidiTransform(text,"IRNNN","ILNNN"); |
| 242 |
} |
| 243 |
return text; |
| 244 |
} |
| 245 |
|
| 246 |
|
| 247 |
if(g.isSvg){ |
| 248 |
if(has("opera") || has("ff") >= 4){ |
| 249 |
text = bidi_const.LRM + (targetDir == "rtl"? bidi_const.RLE : bidi_const.LRE) + text + bidi_const.PDF; |
| 250 |
}else{ |
| 251 |
text = (targetDir == "rtl") ? bidiEngine.bidiTransform(text,"IRYNN","VLNNN") : bidiEngine.bidiTransform(text,"ILYNN","VLNNN"); |
| 252 |
} |
| 253 |
} |
| 254 |
} |
| 255 |
return text; |
| 256 |
}, |
| 257 |
bidiPreprocess: function(newText){ |
| 258 |
if(newText && (typeof newText == "string")){ |
| 259 |
this.origText = newText; |
| 260 |
newText = this.formatText(newText,this.textDir); |
| 261 |
} |
| 262 |
return newText; |
| 263 |
} |
| 264 |
}); |
| 265 |
|
| 266 |
var extendMethod = function(shape, method, before, after){ |
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
var old = shape.prototype[method]; |
| 279 |
shape.prototype[method] = |
| 280 |
function(){ |
| 281 |
var rBefore; |
| 282 |
if (before){ |
| 283 |
rBefore = before.apply(this, arguments); |
| 284 |
} |
| 285 |
var r = old.call(this, rBefore); |
| 286 |
if (after){ |
| 287 |
r = after.call(this, r, arguments); |
| 288 |
} |
| 289 |
return r; |
| 290 |
}; |
| 291 |
}; |
| 292 |
|
| 293 |
var bidiPreprocess = function(newText){ |
| 294 |
if (newText){ |
| 295 |
if (newText.textDir){ |
| 296 |
newText.textDir = validateTextDir(newText.textDir); |
| 297 |
} |
| 298 |
if (newText.text && (newText.text instanceof Array)){ |
| 299 |
newText.text = newText.text.join(","); |
| 300 |
} |
| 301 |
} |
| 302 |
if(newText && (newText.text != undefined || newText.textDir) && (this.textDir != newText.textDir || newText.text != this.origText)){ |
| 303 |
|
| 304 |
this.origText = (newText.text != undefined) ? newText.text : this.origText; |
| 305 |
if(newText.textDir){ |
| 306 |
this.textDir = newText.textDir; |
| 307 |
} |
| 308 |
newText.text = this.formatText(this.origText,this.textDir); |
| 309 |
} |
| 310 |
return this.bidiPreprocess(newText); |
| 311 |
|
| 312 |
}; |
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
extendMethod(g.Text,"setShape", bidiPreprocess, null); |
| 318 |
extendMethod(g.TextPath,"setText", bidiPreprocess, null); |
| 319 |
|
| 320 |
var restoreText = function(origObj){ |
| 321 |
var obj = lang.clone(origObj); |
| 322 |
if (obj && this.origText){ |
| 323 |
obj.text = this.origText; |
| 324 |
} |
| 325 |
return obj; |
| 326 |
}; |
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
extendMethod(g.Text, "getShape", null, restoreText); |
| 332 |
extendMethod(g.TextPath, "getText", null, restoreText); |
| 333 |
|
| 334 |
var groupTextDir = function(group, args){ |
| 335 |
var textDir; |
| 336 |
if (args && args[0]){ |
| 337 |
textDir = validateTextDir(args[0]); |
| 338 |
} |
| 339 |
group.setTextDir(textDir ? textDir : this.textDir); |
| 340 |
return group; |
| 341 |
}; |
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
extendMethod(g.Surface, "createGroup", null, groupTextDir); |
| 349 |
extendMethod(g.Group, "createGroup", null, groupTextDir); |
| 350 |
|
| 351 |
var textDirPreprocess = function(text){ |
| 352 |
|
| 353 |
if(text){ |
| 354 |
var textDir = text.textDir ? validateTextDir(text.textDir) : this.textDir; |
| 355 |
if(textDir){ |
| 356 |
text.textDir = textDir; |
| 357 |
} |
| 358 |
} |
| 359 |
return text; |
| 360 |
}; |
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
extendMethod(g.Surface,"createText", textDirPreprocess, null); |
| 367 |
extendMethod(g.Surface,"createTextPath", textDirPreprocess, null); |
| 368 |
extendMethod(g.Group,"createText", textDirPreprocess, null); |
| 369 |
extendMethod(g.Group,"createTextPath", textDirPreprocess, null); |
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
g.createSurface = function(parentNode, width, height, textDir) { |
| 377 |
var s = g[g.renderer].createSurface(parentNode, width, height); |
| 378 |
var tDir = validateTextDir(textDir); |
| 379 |
|
| 380 |
if(g.isSvgWeb){ |
| 381 |
s.textDir = tDir ? tDir : html.style(dom.byId(parentNode),"direction"); |
| 382 |
return s; |
| 383 |
} |
| 384 |
|
| 385 |
|
| 386 |
if(g.isVml || g.isSvg || g.isCanvas){ |
| 387 |
s.textDir = tDir ? tDir : html.style(s.rawNode,"direction"); |
| 388 |
} |
| 389 |
if(g.isSilverlight){ |
| 390 |
|
| 391 |
|
| 392 |
s.textDir = tDir ? tDir : html.style(s._nodes[1],"direction"); |
| 393 |
} |
| 394 |
|
| 395 |
return s; |
| 396 |
}; |
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
function setTextDir( obj, newTextDir){ |
| 402 |
var tDir = validateTextDir(newTextDir); |
| 403 |
if (tDir){ |
| 404 |
g.utils.forEach(obj,function(e){ |
| 405 |
if(e instanceof g.Surface || e instanceof g.Group){ |
| 406 |
e.textDir = tDir; |
| 407 |
} |
| 408 |
if(e instanceof g.Text){ |
| 409 |
e.setShape({textDir: tDir}); |
| 410 |
} |
| 411 |
if(e instanceof g.TextPath){ |
| 412 |
e.setText({textDir: tDir}) |
| 413 |
} |
| 414 |
}, obj); |
| 415 |
} |
| 416 |
return obj; |
| 417 |
} |
| 418 |
|
| 419 |
function validateTextDir(textDir){ |
| 420 |
var validValues = ["ltr","rtl","auto"]; |
| 421 |
if (textDir){ |
| 422 |
textDir = textDir.toLowerCase(); |
| 423 |
if (arr.indexOf(validValues, textDir) < 0){ |
| 424 |
return null; |
| 425 |
} |
| 426 |
} |
| 427 |
return textDir; |
| 428 |
} |
| 429 |
|
| 430 |
return g; |
| 431 |
}); |
| 432 |
|