| 1 |
define(["dojo/_base/lang","dojo/_base/declare","dojo/_base/array", "dojo/_base/loader" , |
| 2 |
"dojo/_base/xhr","./_base", "dojox/xml/DomParser", "dojox/html/metrics","./matrix"], |
| 3 |
function (lang,declare,arr,loader,xhr,gfx,xmlDomParser,HtmlMetrics,Matrix){ |
| 4 |
var _getText = function(url){ |
| 5 |
var result; |
| 6 |
xhr.get({url:url, sync:true, load:function(text){ |
| 7 |
result = text; |
| 8 |
}}); |
| 9 |
return result; |
| 10 |
}; |
| 11 |
|
| 12 |
lang.getObject("dojox.gfx.VectorText", true); |
| 13 |
|
| 14 |
lang.mixin(gfx, { |
| 15 |
vectorFontFitting: { |
| 16 |
NONE: 0, |
| 17 |
FLOW: 1, |
| 18 |
FIT: 2 |
| 19 |
}, |
| 20 |
defaultVectorText: { |
| 21 |
type:"vectortext", x:0, y:0, width: null, height: null, |
| 22 |
text: "", align: "start", decoration: "none", fitting: 0, |
| 23 |
leading: 1.5 |
| 24 |
}, |
| 25 |
defaultVectorFont: { |
| 26 |
type:"vectorfont", size: "10pt", family: null |
| 27 |
}, |
| 28 |
_vectorFontCache: {}, |
| 29 |
_svgFontCache: {}, |
| 30 |
getVectorFont: function(url){ |
| 31 |
if(gfx._vectorFontCache[url]){ |
| 32 |
return gfx._vectorFontCache[url]; |
| 33 |
} |
| 34 |
return new gfx.VectorFont(url); |
| 35 |
} |
| 36 |
}); |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
return declare("dojox.gfx.VectorFont", null, { |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
_entityRe: /&(quot|apos|lt|gt|amp|#x[^;]+|#\d+);/g, |
| 69 |
_decodeEntitySequence: function(str){ |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
if(!str.match(this._entityRe)){ return; } |
| 74 |
var xmlEntityMap = { |
| 75 |
amp:"&", apos:"'", quot:'"', lt:"<", gt:">" |
| 76 |
}; |
| 77 |
|
| 78 |
|
| 79 |
var r, tmp=""; |
| 80 |
while((r=this._entityRe.exec(str))!==null){ |
| 81 |
if(r[1].charAt(1)=="x"){ |
| 82 |
tmp += String.fromCharCode(parseInt(r[1].slice(2), 16)); |
| 83 |
} |
| 84 |
else if(!isNaN(parseInt(r[1].slice(1),10))){ |
| 85 |
tmp += String.fromCharCode(parseInt(r[1].slice(1), 10)); |
| 86 |
} |
| 87 |
else { |
| 88 |
tmp += xmlEntityMap[r[1]] || ""; |
| 89 |
} |
| 90 |
} |
| 91 |
return tmp; |
| 92 |
}, |
| 93 |
_parse: function(svg, url){ |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
var doc = gfx._svgFontCache[url]||xmlDomParser.parse(svg); |
| 99 |
|
| 100 |
|
| 101 |
var f = doc.documentElement.byName("font")[0], face = doc.documentElement.byName("font-face")[0]; |
| 102 |
var unitsPerEm = parseFloat(face.getAttribute("units-per-em")||1000, 10); |
| 103 |
var advance = { |
| 104 |
x: parseFloat(f.getAttribute("horiz-adv-x"), 10), |
| 105 |
y: parseFloat(f.getAttribute("vert-adv-y")||0, 10) |
| 106 |
}; |
| 107 |
if(!advance.y){ |
| 108 |
advance.y = unitsPerEm; |
| 109 |
} |
| 110 |
|
| 111 |
var origin = { |
| 112 |
horiz: { |
| 113 |
x: parseFloat(f.getAttribute("horiz-origin-x")||0, 10), |
| 114 |
y: parseFloat(f.getAttribute("horiz-origin-y")||0, 10) |
| 115 |
}, |
| 116 |
vert: { |
| 117 |
x: parseFloat(f.getAttribute("vert-origin-x")||0, 10), |
| 118 |
y: parseFloat(f.getAttribute("vert-origin-y")||0, 10) |
| 119 |
} |
| 120 |
}; |
| 121 |
|
| 122 |
|
| 123 |
var family = face.getAttribute("font-family"), |
| 124 |
style = face.getAttribute("font-style")||"all", |
| 125 |
variant = face.getAttribute("font-variant")||"normal", |
| 126 |
weight = face.getAttribute("font-weight")||"all", |
| 127 |
stretch = face.getAttribute("font-stretch")||"normal", |
| 128 |
|
| 129 |
|
| 130 |
range = face.getAttribute("unicode-range")||"U+0-10FFFF", |
| 131 |
panose = face.getAttribute("panose-1") || "0 0 0 0 0 0 0 0 0 0", |
| 132 |
capHeight = face.getAttribute("cap-height"), |
| 133 |
ascent = parseFloat(face.getAttribute("ascent")||(unitsPerEm-origin.vert.y), 10), |
| 134 |
descent = parseFloat(face.getAttribute("descent")||origin.vert.y, 10), |
| 135 |
baseline = {}; |
| 136 |
|
| 137 |
|
| 138 |
var name = family; |
| 139 |
if(face.byName("font-face-name")[0]){ |
| 140 |
name = face.byName("font-face-name")[0].getAttribute("name"); |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
if(gfx._vectorFontCache[name]){ return; } |
| 145 |
|
| 146 |
|
| 147 |
arr.forEach(["alphabetic", "ideographic", "mathematical", "hanging" ], function(attr){ |
| 148 |
var a = face.getAttribute(attr); |
| 149 |
if(a !== null ){ |
| 150 |
baseline[attr] = parseFloat(a, 10); |
| 151 |
} |
| 152 |
}); |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
var missing = parseFloat(doc.documentElement.byName("missing-glyph")[0].getAttribute("horiz-adv-x")||advance.x, 10); |
| 166 |
|
| 167 |
|
| 168 |
var glyphs = {}, glyphsByName={}, g=doc.documentElement.byName("glyph"); |
| 169 |
arr.forEach(g, function(node){ |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
var code = node.getAttribute("unicode"), |
| 175 |
name = node.getAttribute("glyph-name"), |
| 176 |
xAdv = parseFloat(node.getAttribute("horiz-adv-x")||advance.x, 10), |
| 177 |
path = node.getAttribute("d"); |
| 178 |
|
| 179 |
|
| 180 |
if(code.match(this._entityRe)){ |
| 181 |
code = this._decodeEntitySequence(code); |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
var o = { code: code, name: name, xAdvance: xAdv, path: path }; |
| 186 |
glyphs[code]=o; |
| 187 |
glyphsByName[name]=o; |
| 188 |
}, this); |
| 189 |
|
| 190 |
|
| 191 |
var hkern=doc.documentElement.byName("hkern"); |
| 192 |
arr.forEach(hkern, function(node, i){ |
| 193 |
var k = -parseInt(node.getAttribute("k"),10); |
| 194 |
|
| 195 |
var u1=node.getAttribute("u1"), |
| 196 |
g1=node.getAttribute("g1"), |
| 197 |
u2=node.getAttribute("u2"), |
| 198 |
g2=node.getAttribute("g2"), |
| 199 |
gl; |
| 200 |
|
| 201 |
if(u1){ |
| 202 |
|
| 203 |
|
| 204 |
u1 = this._decodeEntitySequence(u1); |
| 205 |
if(glyphs[u1]){ |
| 206 |
gl = glyphs[u1]; |
| 207 |
} |
| 208 |
} else { |
| 209 |
|
| 210 |
|
| 211 |
if(glyphsByName[g1]){ |
| 212 |
gl = glyphsByName[g1]; |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
if(gl){ |
| 217 |
if(!gl.kern){ gl.kern = {}; } |
| 218 |
if(u2){ |
| 219 |
|
| 220 |
u2 = this._decodeEntitySequence(u2); |
| 221 |
gl.kern[u2] = { x: k }; |
| 222 |
} else { |
| 223 |
if(glyphsByName[g2]){ |
| 224 |
gl.kern[glyphsByName[g2].code] = { x: k }; |
| 225 |
} |
| 226 |
} |
| 227 |
} |
| 228 |
}, this); |
| 229 |
|
| 230 |
|
| 231 |
lang.mixin(this, { |
| 232 |
family: family, |
| 233 |
name: name, |
| 234 |
style: style, |
| 235 |
variant: variant, |
| 236 |
weight: weight, |
| 237 |
stretch: stretch, |
| 238 |
range: range, |
| 239 |
viewbox: { width: unitsPerEm, height: unitsPerEm }, |
| 240 |
origin: origin, |
| 241 |
advance: lang.mixin(advance, { |
| 242 |
missing:{ x: missing, y: missing } |
| 243 |
}), |
| 244 |
ascent: ascent, |
| 245 |
descent: descent, |
| 246 |
baseline: baseline, |
| 247 |
glyphs: glyphs |
| 248 |
}); |
| 249 |
|
| 250 |
|
| 251 |
gfx._vectorFontCache[name] = this; |
| 252 |
gfx._vectorFontCache[url] = this; |
| 253 |
if(name!=family && !gfx._vectorFontCache[family]){ |
| 254 |
gfx._vectorFontCache[family] = this; |
| 255 |
} |
| 256 |
|
| 257 |
|
| 258 |
if(!gfx._svgFontCache[url]){ |
| 259 |
gfx._svgFontCache[url]=doc; |
| 260 |
} |
| 261 |
}, |
| 262 |
_clean: function(){ |
| 263 |
|
| 264 |
|
| 265 |
var name = this.name, family = this.family; |
| 266 |
arr.forEach(["family","name","style","variant", |
| 267 |
"weight","stretch","range","viewbox", |
| 268 |
"origin","advance","ascent","descent", |
| 269 |
"baseline","glyphs"], function(prop){ |
| 270 |
try{ delete this[prop]; } catch(e) { } |
| 271 |
}, this); |
| 272 |
|
| 273 |
|
| 274 |
if(gfx._vectorFontCache[name]){ |
| 275 |
delete gfx._vectorFontCache[name]; |
| 276 |
} |
| 277 |
if(gfx._vectorFontCache[family]){ |
| 278 |
delete gfx._vectorFontCache[family]; |
| 279 |
} |
| 280 |
return this; |
| 281 |
}, |
| 282 |
|
| 283 |
constructor: function(url){ |
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
this._defaultLeading = 1.5; |
| 289 |
if(url!==undefined){ |
| 290 |
this.load(url); |
| 291 |
} |
| 292 |
}, |
| 293 |
load: function(url){ |
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
this.onLoadBegin(url.toString()); |
| 299 |
this._parse( |
| 300 |
gfx._svgFontCache[url.toString()]||_getText(url.toString()), |
| 301 |
url.toString() |
| 302 |
); |
| 303 |
this.onLoad(this); |
| 304 |
return this; |
| 305 |
}, |
| 306 |
initialized: function(){ |
| 307 |
|
| 308 |
|
| 309 |
return (this.glyphs!==null); |
| 310 |
}, |
| 311 |
|
| 312 |
|
| 313 |
_round: function(n){ return Math.round(1000*n)/1000; }, |
| 314 |
_leading: function(unit){ return this.viewbox.height * (unit||this._defaultLeading); }, |
| 315 |
_normalize: function(str){ |
| 316 |
return str.replace(/\s+/g, String.fromCharCode(0x20)); |
| 317 |
}, |
| 318 |
|
| 319 |
_getWidth: function(glyphs){ |
| 320 |
var w=0, last=0, lastGlyph=null; |
| 321 |
arr.forEach(glyphs, function(glyph, i){ |
| 322 |
last=glyph.xAdvance; |
| 323 |
if(glyphs[i] && glyph.kern && glyph.kern[glyphs[i].code]){ |
| 324 |
last += glyph.kern[glyphs[i].code].x; |
| 325 |
} |
| 326 |
w += last; |
| 327 |
lastGlyph = glyph; |
| 328 |
}); |
| 329 |
|
| 330 |
|
| 331 |
if(lastGlyph && lastGlyph.code == " "){ |
| 332 |
w -= lastGlyph.xAdvance; |
| 333 |
} |
| 334 |
|
| 335 |
return this._round(w); |
| 336 |
}, |
| 337 |
|
| 338 |
_getLongestLine: function(lines){ |
| 339 |
var maxw=0, idx=0; |
| 340 |
arr.forEach(lines, function(line, i){ |
| 341 |
var max = Math.max(maxw, this._getWidth(line)); |
| 342 |
if(max > maxw){ |
| 343 |
maxw = max; |
| 344 |
idx=i; |
| 345 |
} |
| 346 |
}, this); |
| 347 |
return { width: maxw, index: idx, line: lines[idx] }; |
| 348 |
}, |
| 349 |
|
| 350 |
_trim: function(lines){ |
| 351 |
var fn = function(arr){ |
| 352 |
|
| 353 |
if(!arr.length){ return; } |
| 354 |
if(arr[arr.length-1].code == " "){ arr.splice(arr.length-1, 1); } |
| 355 |
if(!arr.length){ return; } |
| 356 |
if(arr[0].code == " "){ arr.splice(0, 1); } |
| 357 |
}; |
| 358 |
|
| 359 |
if(lang.isArray(lines[0])){ |
| 360 |
|
| 361 |
arr.forEach(lines, fn); |
| 362 |
} else { |
| 363 |
fn(lines); |
| 364 |
} |
| 365 |
return lines; |
| 366 |
}, |
| 367 |
|
| 368 |
_split: function(chars, nLines){ |
| 369 |
|
| 370 |
|
| 371 |
var w = this._getWidth(chars), |
| 372 |
limit = Math.floor(w/nLines), |
| 373 |
lines = [], |
| 374 |
cw = 0, |
| 375 |
c = [], |
| 376 |
found = false; |
| 377 |
|
| 378 |
for(var i=0, l=chars.length; i<l; i++){ |
| 379 |
if(chars[i].code == " "){ found = true; } |
| 380 |
cw += chars[i].xAdvance; |
| 381 |
if(i+1<l && chars[i].kern && chars[i].kern[chars[i+1].code]){ |
| 382 |
cw += chars[i].kern[chars[i+1].code].x; |
| 383 |
} |
| 384 |
|
| 385 |
if(cw>=limit){ |
| 386 |
var chr=chars[i]; |
| 387 |
while(found && chr.code != " " && i>=0){ |
| 388 |
chr = c.pop(); i--; |
| 389 |
} |
| 390 |
lines.push(c); |
| 391 |
c=[]; |
| 392 |
cw=0; |
| 393 |
found=false; |
| 394 |
} |
| 395 |
c.push(chars[i]); |
| 396 |
} |
| 397 |
if(c.length){ lines.push(c); } |
| 398 |
|
| 399 |
return this._trim(lines); |
| 400 |
}, |
| 401 |
|
| 402 |
_getSizeFactor: function(size){ |
| 403 |
|
| 404 |
|
| 405 |
size += ""; |
| 406 |
var metrics = HtmlMetrics.getCachedFontMeasurements(), |
| 407 |
height=this.viewbox.height, |
| 408 |
f=metrics["1em"], |
| 409 |
unit=parseFloat(size, 10); |
| 410 |
if(size.indexOf("em")>-1){ |
| 411 |
return this._round((metrics["1em"]*unit)/height); |
| 412 |
} |
| 413 |
else if(size.indexOf("ex")>-1){ |
| 414 |
return this._round((metrics["1ex"]*unit)/height); |
| 415 |
} |
| 416 |
else if(size.indexOf("pt")>-1){ |
| 417 |
return this._round(((metrics["12pt"] / 12)*unit) / height); |
| 418 |
} |
| 419 |
else if(size.indexOf("px")>-1){ |
| 420 |
return this._round(((metrics["16px"] / 16)*unit) / height); |
| 421 |
} |
| 422 |
else if(size.indexOf("%")>-1){ |
| 423 |
return this._round((metrics["1em"]*(unit / 100)) / height); |
| 424 |
} |
| 425 |
else { |
| 426 |
f=metrics[size]||metrics.medium; |
| 427 |
return this._round(f/height); |
| 428 |
} |
| 429 |
}, |
| 430 |
|
| 431 |
_getFitFactor: function(lines, w, h, l){ |
| 432 |
|
| 433 |
|
| 434 |
if(!h){ |
| 435 |
|
| 436 |
return this._round(w/this._getWidth(lines)); |
| 437 |
} else { |
| 438 |
var maxw = this._getLongestLine(lines).width, |
| 439 |
maxh = (lines.length*(this.viewbox.height*l))-((this.viewbox.height*l)-this.viewbox.height); |
| 440 |
return this._round(Math.min(w/maxw, h/maxh)); |
| 441 |
} |
| 442 |
}, |
| 443 |
_getBestFit: function(chars, w, h, ldng){ |
| 444 |
|
| 445 |
|
| 446 |
var limit=32, |
| 447 |
factor=0, |
| 448 |
lines=limit; |
| 449 |
while(limit>0){ |
| 450 |
var f=this._getFitFactor(this._split(chars, limit), w, h, ldng); |
| 451 |
if(f>factor){ |
| 452 |
factor = f; |
| 453 |
lines=limit; |
| 454 |
} |
| 455 |
limit--; |
| 456 |
} |
| 457 |
return { scale: factor, lines: this._split(chars, lines) }; |
| 458 |
}, |
| 459 |
|
| 460 |
_getBestFlow: function(chars, w, scale){ |
| 461 |
|
| 462 |
|
| 463 |
var lines = [], |
| 464 |
cw = 0, |
| 465 |
c = [], |
| 466 |
found = false; |
| 467 |
for(var i=0, l=chars.length; i<l; i++){ |
| 468 |
if(chars[i].code == " "){ found = true; } |
| 469 |
var tw = chars[i].xAdvance; |
| 470 |
if(i+1<l && chars[i].kern && chars[i].kern[chars[i+1].code]){ |
| 471 |
tw += chars[i].kern[chars[i+1].code].x; |
| 472 |
} |
| 473 |
cw += scale*tw; |
| 474 |
|
| 475 |
if(cw>=w){ |
| 476 |
var chr=chars[i]; |
| 477 |
while(found && chr.code != " " && i>=0){ |
| 478 |
chr = c.pop(); i--; |
| 479 |
} |
| 480 |
lines.push(c); |
| 481 |
c=[]; |
| 482 |
cw=0; |
| 483 |
found=false; |
| 484 |
} |
| 485 |
c.push(chars[i]); |
| 486 |
} |
| 487 |
if(c.length){ lines.push(c); } |
| 488 |
return this._trim(lines); |
| 489 |
}, |
| 490 |
|
| 491 |
|
| 492 |
getWidth: function(text, scale){ |
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
return this._getWidth(arr.map(this._normalize(text).split(""), function(chr){ |
| 500 |
return this.glyphs[chr] || { xAdvance: this.advance.missing.x }; |
| 501 |
}, this)) * (scale || 1); |
| 502 |
}, |
| 503 |
getLineHeight: function(scale){ |
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
return this.viewbox.height * (scale || 1); |
| 509 |
}, |
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
getCenterline: function(scale){ |
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
return (scale||1) * (this.viewbox.height/2); |
| 521 |
}, |
| 522 |
getBaseline: function(scale){ |
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
return (scale||1) * (this.viewbox.height+this.descent); |
| 528 |
}, |
| 529 |
|
| 530 |
draw: function( |
| 531 |
group, |
| 532 |
textArgs, |
| 533 |
fontArgs, |
| 534 |
fillArgs, |
| 535 |
strokeArgs){ |
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
if(!this.initialized()){ |
| 618 |
throw new Error("dojox.gfx.VectorFont.draw(): we have not been initialized yet."); |
| 619 |
} |
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
var g = group.createGroup(); |
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
if(textArgs.x || textArgs.y){ |
| 629 |
group.applyTransform({ dx: textArgs.x||0, dy: textArgs.y||0 }); |
| 630 |
} |
| 631 |
|
| 632 |
|
| 633 |
var text = arr.map(this._normalize(textArgs.text).split(""), function(chr){ |
| 634 |
return this.glyphs[chr] || { path:null, xAdvance: this.advance.missing.x }; |
| 635 |
}, this); |
| 636 |
|
| 637 |
|
| 638 |
var size = fontArgs.size, |
| 639 |
fitting = textArgs.fitting, |
| 640 |
width = textArgs.width, |
| 641 |
height = textArgs.height, |
| 642 |
align = textArgs.align, |
| 643 |
leading = textArgs.leading||this._defaultLeading; |
| 644 |
|
| 645 |
|
| 646 |
if(fitting){ |
| 647 |
|
| 648 |
if((fitting==gfx.vectorFontFitting.FLOW && !width) || (fitting==gfx.vectorFontFitting.FIT && (!width || !height))){ |
| 649 |
|
| 650 |
fitting = gfx.vectorFontFitting.NONE; |
| 651 |
} |
| 652 |
} |
| 653 |
|
| 654 |
|
| 655 |
var lines, scale; |
| 656 |
switch(fitting){ |
| 657 |
case gfx.vectorFontFitting.FIT: |
| 658 |
var o=this._getBestFit(text, width, height, leading); |
| 659 |
scale = o.scale; |
| 660 |
lines = o.lines; |
| 661 |
break; |
| 662 |
|
| 663 |
case gfx.vectorFontFitting.FLOW: |
| 664 |
scale = this._getSizeFactor(size); |
| 665 |
lines = this._getBestFlow(text, width, scale); |
| 666 |
break; |
| 667 |
|
| 668 |
default: |
| 669 |
scale = this._getSizeFactor(size); |
| 670 |
lines = [ text ]; |
| 671 |
|
| 672 |
} |
| 673 |
|
| 674 |
|
| 675 |
lines = arr.filter(lines, function(item){ |
| 676 |
return item.length>0; |
| 677 |
}); |
| 678 |
|
| 679 |
|
| 680 |
var cy = 0, |
| 681 |
maxw = this._getLongestLine(lines).width; |
| 682 |
|
| 683 |
for(var i=0, l=lines.length; i<l; i++){ |
| 684 |
var cx = 0, |
| 685 |
line=lines[i], |
| 686 |
linew = this._getWidth(line), |
| 687 |
lg=g.createGroup(); |
| 688 |
|
| 689 |
|
| 690 |
for (var j=0; j<line.length; j++){ |
| 691 |
var glyph=line[j]; |
| 692 |
if(glyph.path!==null){ |
| 693 |
var p = lg.createPath(glyph.path).setFill(fillArgs); |
| 694 |
if(strokeArgs){ p.setStroke(strokeArgs); } |
| 695 |
p.setTransform([ |
| 696 |
Matrix.flipY, |
| 697 |
Matrix.translate(cx, -this.viewbox.height-this.descent) |
| 698 |
]); |
| 699 |
} |
| 700 |
cx += glyph.xAdvance; |
| 701 |
if(j+1<line.length && glyph.kern && glyph.kern[line[j+1].code]){ |
| 702 |
cx += glyph.kern[line[j+1].code].x; |
| 703 |
} |
| 704 |
} |
| 705 |
|
| 706 |
|
| 707 |
var dx = 0; |
| 708 |
if(align=="middle"){ dx = maxw/2 - linew/2; } |
| 709 |
else if(align=="end"){ dx = maxw - linew; } |
| 710 |
lg.setTransform({ dx: dx, dy: cy }); |
| 711 |
cy += this.viewbox.height * leading; |
| 712 |
} |
| 713 |
|
| 714 |
|
| 715 |
g.setTransform(Matrix.scale(scale)); |
| 716 |
|
| 717 |
|
| 718 |
return g; |
| 719 |
}, |
| 720 |
|
| 721 |
|
| 722 |
onLoadBegin: function(url){ }, |
| 723 |
onLoad: function(font){ } |
| 724 |
}); |
| 725 |
|
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
|
| 733 |
|
| 734 |
|
| 735 |
|
| 736 |
|
| 737 |
|
| 738 |
|
| 739 |
|
| 740 |
|
| 741 |
|
| 742 |
|
| 743 |
|
| 744 |
|
| 745 |
|
| 746 |
|
| 747 |
|
| 748 |
|
| 749 |
|
| 750 |
|
| 751 |
|
| 752 |
|
| 753 |
|
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
|
| 758 |
|
| 759 |
|
| 760 |
|
| 761 |
|
| 762 |
|
| 763 |
|
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
|
| 768 |
|
| 769 |
|
| 770 |
|
| 771 |
|
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
|
| 776 |
|
| 777 |
|
| 778 |
|
| 779 |
|
| 780 |
|
| 781 |
|
| 782 |
|
| 783 |
|
| 784 |
|
| 785 |
|
| 786 |
|
| 787 |
|
| 788 |
|
| 789 |
|
| 790 |
|
| 791 |
|
| 792 |
|
| 793 |
|
| 794 |
|
| 795 |
|
| 796 |
|
| 797 |
|
| 798 |
|
| 799 |
|
| 800 |
|
| 801 |
|
| 802 |
|
| 803 |
}); |