Index: trunk/extensions/WebFonts/js/webfonts.js |
— | — | @@ -13,6 +13,7 @@ |
14 | 14 | config : $.webfonts.config, |
15 | 15 | /* Version number */ |
16 | 16 | version: "0.1.2", |
| 17 | + fonts : [], |
17 | 18 | set: function( font ) { |
18 | 19 | if ( !font || font === "none" ) { |
19 | 20 | $.webfonts.reset(); |
— | — | @@ -28,8 +29,8 @@ |
29 | 30 | var config = $.webfonts.config.fonts[font]; |
30 | 31 | |
31 | 32 | //load the style sheet for the font |
32 | | - $.webfonts.loadcss(font); |
33 | | - |
| 33 | + $.webfonts.addFont(font) |
| 34 | + |
34 | 35 | //save the current font and its size. Used for reset. |
35 | 36 | if ( !$.webfonts.oldconfig ) { |
36 | 37 | var $body = $("body"); |
— | — | @@ -150,6 +151,24 @@ |
151 | 152 | $(styleString).appendTo("head"); |
152 | 153 | |
153 | 154 | }, |
| 155 | + |
| 156 | + /* |
| 157 | + * Add a font to the page. |
| 158 | + * This method ensures that css are not duplicated and |
| 159 | + * keep track of added fonts. |
| 160 | + * @param fontFamilyName The fontfamily name |
| 161 | + */ |
| 162 | + addFont : function( fontFamilyName ) { |
| 163 | + // avoid duplication |
| 164 | + if ( $.inArray(fontFamilyName, $.webfonts.fonts ) < 0 ){ |
| 165 | + // check whether the requested font is available. |
| 166 | + if ( fontFamilyName in $.webfonts.config.fonts ) { |
| 167 | + $.webfonts.loadcss( fontFamilyName ); |
| 168 | + $.webfonts.fonts.push( fontFamilyName ); |
| 169 | + } |
| 170 | + } |
| 171 | + }, |
| 172 | + |
154 | 173 | /** |
155 | 174 | * Setup the font selection menu. |
156 | 175 | * It also apply the font from cookie, if any. |
— | — | @@ -183,43 +202,56 @@ |
184 | 203 | //mark it as checked |
185 | 204 | $('#'+fontID(cookie_font)).attr('checked', 'checked'); |
186 | 205 | } |
| 206 | + |
| 207 | + $.webfonts.loadFontsForFontFamilyStyle(); |
| 208 | + $.webfonts.loadFontsForLangAttr(); |
187 | 209 | |
188 | | - //if there are tags with font-family style definition, get a list of fonts to be loaded |
189 | | - var fontFamilies = []; |
190 | | - $('body').find('*[style]').each(function(index) { |
191 | | - if( this.style.fontFamily) { |
192 | | - var fontFamilyItems = this.style.fontFamily.split(","); |
193 | | - $.each(fontFamilyItems, function(index, value) { |
194 | | - fontFamilies.push(value); |
195 | | - }); |
196 | | - } |
197 | | - }); |
198 | | - |
| 210 | + }, |
| 211 | + |
| 212 | + /** |
| 213 | + * Scan the page for tags with lang attr and load the default font |
| 214 | + * for that language if available. |
| 215 | + */ |
| 216 | + loadFontsForLangAttr: function() { |
| 217 | + var languages = $.webfonts.config.languages; |
199 | 218 | //if there are tags with lang attribute, |
200 | 219 | $('body').find('*[lang]').each(function(index) { |
201 | 220 | //check the availability of font. |
202 | | - if(languages[this.lang]){ |
203 | | - //add a font-family style if it does not have any |
204 | | - if( !this.style.fontFamily) { |
205 | | - //use the default font, ie the first one. |
206 | | - fontFamilies.push(languages[this.lang][0]); |
207 | | - $(this).css('font-family', languages[this.lang][0]); |
208 | | - } |
| 221 | + //add a font-family style if it does not have any |
| 222 | + if( languages[this.lang] && !this.style.fontFamily ) { |
| 223 | + fontFamily = languages[this.lang][0]; |
| 224 | + $.webfonts.addFont( fontFamily ); |
| 225 | + $(this).css('font-family', fontFamily); |
| 226 | + $(this).addClass('webfonts-lang-attr'); |
209 | 227 | } |
210 | 228 | }); |
211 | 229 | |
212 | | - //get unique list |
213 | | - fontFamilies = $.unique(fontFamilies); |
214 | | - //load css for each of the item in fontfamily list |
215 | | - $.each(fontFamilies, function(index, fontFamily) { |
216 | | - //remove the ' characters if any. |
217 | | - fontFamily = fontFamily.replace(/'/g, ''); |
218 | | - if ( fontFamily in $.webfonts.config.fonts ) { |
219 | | - $.webfonts.loadcss(fontFamily); |
| 230 | + }, |
| 231 | + |
| 232 | + /** |
| 233 | + * Scan the page for tags with font-family style declarations |
| 234 | + * If that font is available, embed it. |
| 235 | + */ |
| 236 | + loadFontsForFontFamilyStyle: function() { |
| 237 | + var languages = $.webfonts.config.languages; |
| 238 | + //if there are tags with font-family style definition, get a list of fonts to be loaded |
| 239 | + $('body').find('*[style]').each(function(index) { |
| 240 | + if( this.style.fontFamily ) { |
| 241 | + var fontFamilyItems = this.style.fontFamily.split(","); |
| 242 | + $.each( fontFamilyItems, function(index, fontFamily ) { |
| 243 | + //remove the ' characters if any. |
| 244 | + fontFamily = fontFamily.replace(/'/g, ''); |
| 245 | + $.webfonts.addFont( fontFamily ); |
| 246 | + }); |
220 | 247 | } |
221 | 248 | }); |
222 | 249 | |
223 | 250 | }, |
| 251 | + |
| 252 | + /** |
| 253 | + * Prepare the menu for the webfonts. |
| 254 | + * @param config The webfont configuration. |
| 255 | + */ |
224 | 256 | buildMenu: function(config) { |
225 | 257 | var haveSchemes = false; |
226 | 258 | // Build font dropdown |