Project

General

Profile

Statistics
| Revision:

root / trunk / web / dojo / dojox / gfx / VectorText.js @ 11

History | View | Annotate | Download (9.95 KB)

1
/*
2
        Copyright (c) 2004-2010, 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

    
7

    
8
if(!dojo._hasResource["dojox.gfx.VectorText"]){
9
dojo._hasResource["dojox.gfx.VectorText"]=true;
10
dojo.provide("dojox.gfx.VectorText");
11
dojo.require("dojox.gfx");
12
dojo.require("dojox.xml.DomParser");
13
dojo.require("dojox.html.metrics");
14
(function(){
15
dojo.mixin(dojox.gfx,{vectorFontFitting:{NONE:0,FLOW:1,FIT:2},defaultVectorText:{type:"vectortext",x:0,y:0,width:null,height:null,text:"",align:"start",decoration:"none",fitting:0,leading:1.5},defaultVectorFont:{type:"vectorfont",size:"10pt",family:null},_vectorFontCache:{},_svgFontCache:{},getVectorFont:function(_1){
16
if(dojox.gfx._vectorFontCache[_1]){
17
return dojox.gfx._vectorFontCache[_1];
18
}
19
return new dojox.gfx.VectorFont(_1);
20
}});
21
dojo.declare("dojox.gfx.VectorFont",null,{_entityRe:/&(quot|apos|lt|gt|amp|#x[^;]+|#\d+);/g,_decodeEntitySequence:function(_2){
22
if(!_2.match(this._entityRe)){
23
return;
24
}
25
var _3={amp:"&",apos:"'",quot:"\"",lt:"<",gt:">"};
26
var r,_4="";
27
while((r=this._entityRe.exec(_2))!==null){
28
if(r[1].charAt(1)=="x"){
29
_4+=String.fromCharCode(parseInt(r[1].slice(2),16));
30
}else{
31
if(!isNaN(parseInt(r[1].slice(1),10))){
32
_4+=String.fromCharCode(parseInt(r[1].slice(1),10));
33
}else{
34
_4+=_3[r[1]]||"";
35
}
36
}
37
}
38
return _4;
39
},_parse:function(_5,_6){
40
var _7=dojox.gfx._svgFontCache[_6]||dojox.xml.DomParser.parse(_5);
41
var f=_7.documentElement.byName("font")[0],_8=_7.documentElement.byName("font-face")[0];
42
var _9=parseFloat(_8.getAttribute("units-per-em")||1000,10);
43
var _a={x:parseFloat(f.getAttribute("horiz-adv-x"),10),y:parseFloat(f.getAttribute("vert-adv-y")||0,10)};
44
if(!_a.y){
45
_a.y=_9;
46
}
47
var _b={horiz:{x:parseFloat(f.getAttribute("horiz-origin-x")||0,10),y:parseFloat(f.getAttribute("horiz-origin-y")||0,10)},vert:{x:parseFloat(f.getAttribute("vert-origin-x")||0,10),y:parseFloat(f.getAttribute("vert-origin-y")||0,10)}};
48
var _c=_8.getAttribute("font-family"),_d=_8.getAttribute("font-style")||"all",_e=_8.getAttribute("font-variant")||"normal",_f=_8.getAttribute("font-weight")||"all",_10=_8.getAttribute("font-stretch")||"normal",_11=_8.getAttribute("unicode-range")||"U+0-10FFFF",_12=_8.getAttribute("panose-1")||"0 0 0 0 0 0 0 0 0 0",_13=_8.getAttribute("cap-height"),_14=parseFloat(_8.getAttribute("ascent")||(_9-_b.vert.y),10),_15=parseFloat(_8.getAttribute("descent")||_b.vert.y,10),_16={};
49
var _17=_c;
50
if(_8.byName("font-face-name")[0]){
51
_17=_8.byName("font-face-name")[0].getAttribute("name");
52
}
53
if(dojox.gfx._vectorFontCache[_17]){
54
return;
55
}
56
dojo.forEach(["alphabetic","ideographic","mathematical","hanging"],function(_18){
57
var a=_8.getAttribute(_18);
58
if(a!==null){
59
_16[_18]=parseFloat(a,10);
60
}
61
});
62
var _19=parseFloat(_7.documentElement.byName("missing-glyph")[0].getAttribute("horiz-adv-x")||_a.x,10);
63
var _1a={},_1b={},g=_7.documentElement.byName("glyph");
64
dojo.forEach(g,function(_1c){
65
var _1d=_1c.getAttribute("unicode"),_17=_1c.getAttribute("glyph-name"),_1e=parseFloat(_1c.getAttribute("horiz-adv-x")||_a.x,10),_1f=_1c.getAttribute("d");
66
if(_1d.match(this._entityRe)){
67
_1d=this._decodeEntitySequence(_1d);
68
}
69
var o={code:_1d,name:_17,xAdvance:_1e,path:_1f};
70
_1a[_1d]=o;
71
_1b[_17]=o;
72
},this);
73
var _20=_7.documentElement.byName("hkern");
74
dojo.forEach(_20,function(_21,i){
75
var k=-parseInt(_21.getAttribute("k"),10);
76
var u1=_21.getAttribute("u1"),g1=_21.getAttribute("g1"),u2=_21.getAttribute("u2"),g2=_21.getAttribute("g2"),gl;
77
if(u1){
78
u1=this._decodeEntitySequence(u1);
79
if(_1a[u1]){
80
gl=_1a[u1];
81
}
82
}else{
83
if(_1b[g1]){
84
gl=_1b[g1];
85
}
86
}
87
if(gl){
88
if(!gl.kern){
89
gl.kern={};
90
}
91
if(u2){
92
u2=this._decodeEntitySequence(u2);
93
gl.kern[u2]={x:k};
94
}else{
95
if(_1b[g2]){
96
gl.kern[_1b[g2].code]={x:k};
97
}
98
}
99
}
100
},this);
101
dojo.mixin(this,{family:_c,name:_17,style:_d,variant:_e,weight:_f,stretch:_10,range:_11,viewbox:{width:_9,height:_9},origin:_b,advance:dojo.mixin(_a,{missing:{x:_19,y:_19}}),ascent:_14,descent:_15,baseline:_16,glyphs:_1a});
102
dojox.gfx._vectorFontCache[_17]=this;
103
dojox.gfx._vectorFontCache[_6]=this;
104
if(_17!=_c&&!dojox.gfx._vectorFontCache[_c]){
105
dojox.gfx._vectorFontCache[_c]=this;
106
}
107
if(!dojox.gfx._svgFontCache[_6]){
108
dojox.gfx._svgFontCache[_6]=_7;
109
}
110
},_clean:function(){
111
var _22=this.name,_23=this.family;
112
dojo.forEach(["family","name","style","variant","weight","stretch","range","viewbox","origin","advance","ascent","descent","baseline","glyphs"],function(_24){
113
try{
114
delete this[_24];
115
}
116
catch(e){
117
}
118
},this);
119
if(dojox.gfx._vectorFontCache[_22]){
120
delete dojox.gfx._vectorFontCache[_22];
121
}
122
if(dojox.gfx._vectorFontCache[_23]){
123
delete dojox.gfx._vectorFontCache[_23];
124
}
125
return this;
126
},constructor:function(url){
127
this._defaultLeading=1.5;
128
if(url!==undefined){
129
this.load(url);
130
}
131
},load:function(url){
132
this.onLoadBegin(url.toString());
133
this._parse(dojox.gfx._svgFontCache[url.toString()]||dojo._getText(url.toString()),url.toString());
134
this.onLoad(this);
135
return this;
136
},initialized:function(){
137
return (this.glyphs!==null);
138
},_round:function(n){
139
return Math.round(1000*n)/1000;
140
},_leading:function(_25){
141
return this.viewbox.height*(_25||this._defaultLeading);
142
},_normalize:function(str){
143
return str.replace(/\s+/g,String.fromCharCode(32));
144
},_getWidth:function(_26){
145
var w=0,_27=0,_28=null;
146
dojo.forEach(_26,function(_29,i){
147
_27=_29.xAdvance;
148
if(_26[i]&&_29.kern&&_29.kern[_26[i].code]){
149
_27+=_29.kern[_26[i].code].x;
150
}
151
w+=_27;
152
_28=_29;
153
});
154
if(_28&&_28.code==" "){
155
w-=_28.xAdvance;
156
}
157
return this._round(w);
158
},_getLongestLine:function(_2a){
159
var _2b=0,idx=0;
160
dojo.forEach(_2a,function(_2c,i){
161
var max=Math.max(_2b,this._getWidth(_2c));
162
if(max>_2b){
163
_2b=max;
164
idx=i;
165
}
166
},this);
167
return {width:_2b,index:idx,line:_2a[idx]};
168
},_trim:function(_2d){
169
var fn=function(arr){
170
if(!arr.length){
171
return;
172
}
173
if(arr[arr.length-1].code==" "){
174
arr.splice(arr.length-1,1);
175
}
176
if(!arr.length){
177
return;
178
}
179
if(arr[0].code==" "){
180
arr.splice(0,1);
181
}
182
};
183
if(dojo.isArray(_2d[0])){
184
dojo.forEach(_2d,fn);
185
}else{
186
fn(_2d);
187
}
188
return _2d;
189
},_split:function(_2e,_2f){
190
var w=this._getWidth(_2e),_30=Math.floor(w/_2f),_31=[],cw=0,c=[],_32=false;
191
for(var i=0,l=_2e.length;i<l;i++){
192
if(_2e[i].code==" "){
193
_32=true;
194
}
195
cw+=_2e[i].xAdvance;
196
if(i+1<l&&_2e[i].kern&&_2e[i].kern[_2e[i+1].code]){
197
cw+=_2e[i].kern[_2e[i+1].code].x;
198
}
199
if(cw>=_30){
200
var chr=_2e[i];
201
while(_32&&chr.code!=" "&&i>=0){
202
chr=c.pop();
203
i--;
204
}
205
_31.push(c);
206
c=[];
207
cw=0;
208
_32=false;
209
}
210
c.push(_2e[i]);
211
}
212
if(c.length){
213
_31.push(c);
214
}
215
return this._trim(_31);
216
},_getSizeFactor:function(_33){
217
_33+="";
218
var _34=dojox.html.metrics.getCachedFontMeasurements(),_35=this.viewbox.height,f=_34["1em"],_36=parseFloat(_33,10);
219
if(_33.indexOf("em")>-1){
220
return this._round((_34["1em"]*_36)/_35);
221
}else{
222
if(_33.indexOf("ex")>-1){
223
return this._round((_34["1ex"]*_36)/_35);
224
}else{
225
if(_33.indexOf("pt")>-1){
226
return this._round(((_34["12pt"]/12)*_36)/_35);
227
}else{
228
if(_33.indexOf("px")>-1){
229
return this._round(((_34["16px"]/16)*_36)/_35);
230
}else{
231
if(_33.indexOf("%")>-1){
232
return this._round((_34["1em"]*(_36/100))/_35);
233
}else{
234
f=_34[_33]||_34.medium;
235
return this._round(f/_35);
236
}
237
}
238
}
239
}
240
}
241
},_getFitFactor:function(_37,w,h,l){
242
if(!h){
243
return this._round(w/this._getWidth(_37));
244
}else{
245
var _38=this._getLongestLine(_37).width,_39=(_37.length*(this.viewbox.height*l))-((this.viewbox.height*l)-this.viewbox.height);
246
return this._round(Math.min(w/_38,h/_39));
247
}
248
},_getBestFit:function(_3a,w,h,_3b){
249
var _3c=32,_3d=0,_3e=_3c;
250
while(_3c>0){
251
var f=this._getFitFactor(this._split(_3a,_3c),w,h,_3b);
252
if(f>_3d){
253
_3d=f;
254
_3e=_3c;
255
}
256
_3c--;
257
}
258
return {scale:_3d,lines:this._split(_3a,_3e)};
259
},_getBestFlow:function(_3f,w,_40){
260
var _41=[],cw=0,c=[],_42=false;
261
for(var i=0,l=_3f.length;i<l;i++){
262
if(_3f[i].code==" "){
263
_42=true;
264
}
265
var tw=_3f[i].xAdvance;
266
if(i+1<l&&_3f[i].kern&&_3f[i].kern[_3f[i+1].code]){
267
tw+=_3f[i].kern[_3f[i+1].code].x;
268
}
269
cw+=_40*tw;
270
if(cw>=w){
271
var chr=_3f[i];
272
while(_42&&chr.code!=" "&&i>=0){
273
chr=c.pop();
274
i--;
275
}
276
_41.push(c);
277
c=[];
278
cw=0;
279
_42=false;
280
}
281
c.push(_3f[i]);
282
}
283
if(c.length){
284
_41.push(c);
285
}
286
return this._trim(_41);
287
},getWidth:function(_43,_44){
288
return this._getWidth(dojo.map(this._normalize(_43).split(""),function(chr){
289
return this.glyphs[chr]||{xAdvance:this.advance.missing.x};
290
},this))*(_44||1);
291
},getLineHeight:function(_45){
292
return this.viewbox.height*(_45||1);
293
},getCenterline:function(_46){
294
return (_46||1)*(this.viewbox.height/2);
295
},getBaseline:function(_47){
296
return (_47||1)*(this.viewbox.height+this.descent);
297
},draw:function(_48,_49,_4a,_4b,_4c){
298
if(!this.initialized()){
299
throw new Error("dojox.gfx.VectorFont.draw(): we have not been initialized yet.");
300
}
301
var g=_48.createGroup();
302
if(_49.x||_49.y){
303
_48.applyTransform({dx:_49.x||0,dy:_49.y||0});
304
}
305
var _4d=dojo.map(this._normalize(_49.text).split(""),function(chr){
306
return this.glyphs[chr]||{path:null,xAdvance:this.advance.missing.x};
307
},this);
308
var _4e=_4a.size,_4f=_49.fitting,_50=_49.width,_51=_49.height,_52=_49.align,_53=_49.leading||this._defaultLeading;
309
if(_4f){
310
if((_4f==dojox.gfx.vectorFontFitting.FLOW&&!_50)||(_4f==dojox.gfx.vectorFontFitting.FIT&&(!_50||!_51))){
311
_4f=dojox.gfx.vectorFontFitting.NONE;
312
}
313
}
314
var _54,_55;
315
switch(_4f){
316
case dojox.gfx.vectorFontFitting.FIT:
317
var o=this._getBestFit(_4d,_50,_51,_53);
318
_55=o.scale;
319
_54=o.lines;
320
break;
321
case dojox.gfx.vectorFontFitting.FLOW:
322
_55=this._getSizeFactor(_4e);
323
_54=this._getBestFlow(_4d,_50,_55);
324
break;
325
default:
326
_55=this._getSizeFactor(_4e);
327
_54=[_4d];
328
}
329
_54=dojo.filter(_54,function(_56){
330
return _56.length>0;
331
});
332
var cy=0,_57=this._getLongestLine(_54).width;
333
for(var i=0,l=_54.length;i<l;i++){
334
var cx=0,_58=_54[i],_59=this._getWidth(_58),lg=g.createGroup();
335
for(var j=0;j<_58.length;j++){
336
var _5a=_58[j];
337
if(_5a.path!==null){
338
var p=lg.createPath(_5a.path).setFill(_4b);
339
if(_4c){
340
p.setStroke(_4c);
341
}
342
p.setTransform([dojox.gfx.matrix.flipY,dojox.gfx.matrix.translate(cx,-this.viewbox.height-this.descent)]);
343
}
344
cx+=_5a.xAdvance;
345
if(j+1<_58.length&&_5a.kern&&_5a.kern[_58[j+1].code]){
346
cx+=_5a.kern[_58[j+1].code].x;
347
}
348
}
349
var dx=0;
350
if(_52=="middle"){
351
dx=_57/2-_59/2;
352
}else{
353
if(_52=="end"){
354
dx=_57-_59;
355
}
356
}
357
lg.setTransform({dx:dx,dy:cy});
358
cy+=this.viewbox.height*_53;
359
}
360
g.setTransform(dojox.gfx.matrix.scale(_55));
361
return g;
362
},onLoadBegin:function(url){
363
},onLoad:function(_5b){
364
}});
365
})();
366
}