Index: trunk/extensions/WebFonts/js/webfonts.js |
— | — | @@ -2,8 +2,10 @@ |
3 | 3 | |
4 | 4 | $.webfonts = { |
5 | 5 | |
| 6 | + |
| 7 | + oldconfig: false, |
| 8 | + config : mw.config.get( "wgWebFonts" ), |
6 | 9 | /* Version number */ |
7 | | - oldconfig: false, |
8 | 10 | version: "0.1.2", |
9 | 11 | set: function( font ) { |
10 | 12 | if ( font === "none" ) { |
— | — | @@ -11,36 +13,16 @@ |
12 | 14 | return; |
13 | 15 | } |
14 | 16 | |
15 | | - var config = mw.config.get( "wgWebFonts" ); |
16 | | - if ( !font in config.fonts ) { |
| 17 | + if ( !font in $.webfonts.config.fonts ) { |
17 | 18 | console.log( "Requested unknown font", font ); |
18 | 19 | return; |
19 | 20 | } else { |
20 | | - config = config.fonts[font]; |
| 21 | + config = $.webfonts.config.fonts[font]; |
21 | 22 | } |
22 | 23 | |
23 | | - var styleString = |
24 | | - "<style type='text/css'>\n@font-face {\n" |
25 | | - + "\tfont-family: '"+font+"';\n"; |
26 | | - if ( 'eot' in config ) { |
27 | | - styleString += "\tsrc: url('"+config.eot+"');\n"; |
28 | | - } |
29 | | - //If the font is present locally, use it. |
30 | | - styleString += "\tsrc: local('"+ font +"'),"; |
| 24 | + //load the style sheet for the font |
| 25 | + $.webfonts.loadcss(font); |
31 | 26 | |
32 | | - if ( 'woff' in config ) { |
33 | | - styleString += "\t\turl('"+config.woff+"') format('woff'),"; |
34 | | - } |
35 | | - if ( 'svg' in config ) { |
36 | | - styleString += "\t\turl('"+config.svg+"#"+font+"') format('svg'),"; |
37 | | - } |
38 | | - if ( 'ttf' in config ) { |
39 | | - styleString += "\t\turl('"+config.ttf+"') format('truetype');\n"; |
40 | | - } |
41 | | - |
42 | | - styleString += "\tfont-weight: normal;\n}\n</style>\n"; |
43 | | - $(styleString).appendTo("head" ); |
44 | | - |
45 | 27 | //save the current font and its size. Used for reset. |
46 | 28 | if ( !$.webfonts.oldconfig ) { |
47 | 29 | $.webfonts.oldconfig = { |
— | — | @@ -120,7 +102,39 @@ |
121 | 103 | }); |
122 | 104 | }); |
123 | 105 | }, |
124 | | - |
| 106 | + |
| 107 | + /* |
| 108 | + * Construct the css required for the fontfamily, inject it to the head of the body |
| 109 | + * so that it gets loaded. |
| 110 | + * @param fontfamily The fontfamily name |
| 111 | + */ |
| 112 | + loadcss: function(fontfamily){ |
| 113 | + var fontconfig = $.webfonts.config.fonts[fontfamily]; |
| 114 | + var styleString = |
| 115 | + "<style type='text/css'>\n@font-face {\n" |
| 116 | + + "\tfont-family: '"+fontfamily+"';\n"; |
| 117 | + if ( 'eot' in fontconfig ) { |
| 118 | + styleString += "\tsrc: url('"+fontconfig.eot+"');\n"; |
| 119 | + } |
| 120 | + //If the font is present locally, use it. |
| 121 | + styleString += "\tsrc: local('"+ fontfamily +"'),"; |
| 122 | + |
| 123 | + if ( 'woff' in fontconfig ) { |
| 124 | + styleString += "\t\turl('"+fontconfig.woff+"') format('woff'),"; |
| 125 | + } |
| 126 | + if ( 'svg' in fontconfig ) { |
| 127 | + styleString += "\t\turl('"+fontconfig.svg+"#"+fontfamily+"') format('svg'),"; |
| 128 | + } |
| 129 | + if ( 'ttf' in fontconfig ) { |
| 130 | + styleString += "\t\turl('"+fontconfig.ttf+"') format('truetype');\n"; |
| 131 | + } |
| 132 | + |
| 133 | + styleString += "\tfont-weight: normal;\n}\n</style>\n"; |
| 134 | + |
| 135 | + //inject the css to the head of the page. |
| 136 | + $(styleString).appendTo("head" ); |
| 137 | + |
| 138 | + }, |
125 | 139 | /** |
126 | 140 | * Setup the font selection menu. |
127 | 141 | * It also apply the font from cookie, if any. |