@@ -7,73 +7,74 @@ description: Fonts for ART, implements code from [Cufón](http://cufon.shoqolate
7
7
8
8
authors: [Simo Kinnunen](http://twitter.com/sorccu), ART adaptation by [Valerio Proietti](http://mad4milk.net/)
9
9
10
- provides: [ ART:text, ART .Font]
10
+ provides: ART.Font
11
11
12
- requires: [ ART.Canvas, ART.VML]
12
+ requires: ART.Shape
13
13
14
14
...
15
15
*/
16
16
17
- // stub
18
-
19
- ART . Font = function ( font ) {
20
- return font ;
21
- } ;
22
-
23
17
( function ( ) {
24
18
25
19
var fonts = { } ;
26
20
27
- ART . defineFont = function ( name , font ) {
28
- fonts [ name ] = new ART . Font ( font ) ;
29
- } ;
30
-
31
- ART . lookupFont = function ( name ) {
32
- return fonts [ name ] ;
33
- } ;
34
-
35
21
ART . registerFont = function ( font ) {
36
- var face = font . face , name = face [ 'font-family' ] . toLowerCase ( ) . split ( ' ' ) ;
37
- if ( face [ 'font-weight' ] > 400 ) name . push ( 'bold' ) ;
38
- if ( face [ 'font-stretch' ] == 'oblique' ) name . push ( 'italic' ) ;
39
- ART . defineFont ( name . join ( '-' ) , font ) ;
22
+ var face = font . face , name = face [ 'font-family' ] ;
23
+ if ( ! fonts [ name ] ) fonts [ name ] = { } ;
24
+ var currentFont = fonts [ name ] ;
25
+ var isBold = ( face [ 'font-weight' ] > 400 ) , isItalic = ( face [ 'font-stretch' ] == 'oblique' ) ;
26
+ if ( isBold && isItalic ) currentFont . boldItalic = font ;
27
+ else if ( isBold ) currentFont . bold = font ;
28
+ else if ( isItalic ) currentFont . italic = font ;
29
+ else currentFont . normal = font ;
30
+ return this ;
40
31
} ;
41
32
42
- var path = function ( path , s , self ) {
43
- var X = 0 , Y = 0 ;
33
+ var VMLToSVG = function ( path , s , x , y ) {
34
+ var end = '' ;
44
35
var regexp = / ( [ m r v x e ] ) ( [ ^ a - z ] * ) / g, match ;
45
36
while ( ( match = regexp . exec ( path ) ) ) {
46
37
var c = match [ 2 ] . split ( ',' ) ;
47
38
switch ( match [ 1 ] ) {
48
- case 'v' :
49
- self . bezierTo ( { x : X + s * ~ ~ c [ 0 ] , y : Y + s * ~ ~ c [ 1 ] } , { x : X + s * ~ ~ c [ 2 ] , y : Y + s * ~ ~ c [ 3 ] } , { x : X += s * ~ ~ c [ 4 ] , y : Y += s * ~ ~ c [ 5 ] } ) ;
50
- break ;
51
- case 'r' : self . lineTo ( { x : X += s * ~ ~ c [ 0 ] , y : Y += s * ~ ~ c [ 1 ] } ) ; break ;
52
- case 'm' : self . moveTo ( { x : X = s * ~ ~ c [ 0 ] , y : Y = s * ~ ~ c [ 1 ] } ) ; break ;
53
- case 'x' : self . join ( ) ; break ;
54
- case 'e' : return ;
39
+ case 'v' : end += 'c ' + ( s * c [ 0 ] ) + ',' + ( s * c [ 1 ] ) + ',' + ( s * c [ 2 ] ) + ',' + ( s * c [ 3 ] ) + ',' + ( s * c [ 4 ] ) + ',' + ( s * c [ 5 ] ) ; break ;
40
+ case 'r' : end += 'l ' + ( s * c [ 0 ] ) + ',' + ( s * c [ 1 ] ) ; break ;
41
+ case 'm' : end += 'M ' + ( x + ( s * c [ 0 ] ) ) + ',' + ( y + ( s * c [ 1 ] ) ) ; break ;
42
+ case 'x' : end += 'z' ; break ;
55
43
}
56
44
}
45
+
46
+ return end ;
57
47
} ;
58
48
59
- ART . implement ( { 'text' : function ( font , size , text ) {
60
- if ( typeof font == 'string' ) font = fonts [ font ] ;
61
- if ( ! font ) return new Error ( 'The specified font has not been found.' ) ;
62
-
63
- this . save ( ) ;
64
- size = size / font . face [ 'units-per-em' ] ;
65
- // Temporary "relative" fix shifting the whole layer by the pointer, since the pointer is lost with path. Should not matter since it's later restored.
66
- this . shift ( { x : this . pointer . x , y : this . pointer . y + Math . round ( size * font . face . ascent ) } ) ;
67
- var width = 0 ;
68
- for ( var i = 0 , l = text . length ; i < l ; ++ i ) {
69
- var glyph = font . glyphs [ text . charAt ( i ) ] || font . glyphs [ ' ' ] ;
70
- if ( glyph . d ) path ( 'm' + glyph . d + 'x' , size , this ) ;
71
- var w = size * ( glyph . w || font . w ) ;
72
- this . shift ( { x : w , y : 0 } ) ;
73
- width += w ;
49
+ ART . Font = new Class ( {
50
+
51
+ Extends : ART . Shape ,
52
+
53
+ initialize : function ( font , variant , text , size ) {
54
+ if ( typeof font == 'string' ) font = fonts [ font ] [ ( variant || 'normal' ) . camelCase ( ) ] ;
55
+ if ( ! font ) throw new Error ( 'The specified font has not been found.' ) ;
56
+ this . font = font ;
57
+
58
+ this . parent ( ) ;
59
+ if ( text != null && size != null ) this . draw ( text , size ) ;
60
+ } ,
61
+
62
+ draw : function ( text , size ) {
63
+ var font = this . font ;
64
+ size = size / font . face [ 'units-per-em' ] ;
65
+
66
+ var width = 0 , height = size * font . face . ascent , path = '' ;
67
+
68
+ for ( var i = 0 , l = text . length ; i < l ; ++ i ) {
69
+ var glyph = font . glyphs [ text . charAt ( i ) ] || font . glyphs [ ' ' ] ;
70
+ var w = size * ( glyph . w || font . w ) ;
71
+ if ( glyph . d ) path += VMLToSVG ( 'm' + glyph . d + 'x' , size , width , height ) ;
72
+ width += w ;
73
+ }
74
+
75
+ return this . parent ( path ) ;
74
76
}
75
- this . restore ( ) ;
76
- return { x : width , y : size * ( font . face . ascent - font . face . descent ) } ;
77
- } } ) ;
77
+
78
+ } ) ;
78
79
79
80
} ) ( ) ;
0 commit comments