1 /* See license.txt for terms of usage */ 2 3 define([ 4 "firebug/lib/trace", 5 "firebug/lib/dom", 6 "firebug/lib/url" 7 ], 8 function(FBTrace, Dom, Url) { 9 10 // ********************************************************************************************* // 11 // Constants 12 13 var Ci = Components.interfaces; 14 var Cc = Components.classes; 15 var Cu = Components.utils; 16 17 var Fonts = {}; 18 19 // ********************************************************************************************* // 20 // Fonts 21 22 /** 23 * Retrieves all fonts used inside a node 24 * @node: Node to return the fonts for 25 * @return Array of fonts 26 */ 27 Fonts.getFonts = function(node) 28 { 29 if (!Dom.domUtils) 30 return []; 31 32 var range = node.ownerDocument.createRange(); 33 try 34 { 35 range.selectNode(node); 36 } 37 catch(err) 38 { 39 if (FBTrace.DBG_FONTS || FBTrace.DBG_ERRORS) 40 FBTrace.sysout("Fonts.getFonts; node couldn't be selected", err); 41 } 42 43 var fontFaces = Dom.domUtils.getUsedFontFaces(range); 44 // converts fontFaces to an array: 45 var fonts = Array.slice(fontFaces); 46 47 if (FBTrace.DBG_FONTS) 48 FBTrace.sysout("Fonts.getFonts; used fonts", fonts); 49 50 return fonts; 51 } 52 53 /** 54 * Retrieves all fonts used in a context, cached so that the first use is 55 * potentially slow (several seconds on the HTML5 spec), and later ones are 56 * instant but not up-to-date. 57 * @context: Context to return the fonts for 58 * @return Array of fonts 59 */ 60 Fonts.getFontsUsedInContext = function(context) 61 { 62 if (context.fontCache) 63 return context.fontCache; 64 65 var fonts = []; 66 if (context.window) 67 { 68 var doc = context.window.document; 69 if (doc) 70 fonts = Fonts.getFonts(doc.documentElement); 71 } 72 context.fontCache = fonts; 73 return fonts; 74 } 75 76 /** 77 * Retrieves the information about a font 78 * @context: Context of the font 79 * @win: Window the font is used in 80 * @identifier: Either a URL in case of a web font or the font name 81 * @return Object with information about the font 82 */ 83 Fonts.getFontInfo = function(context, win, identifier) 84 { 85 if (!context) 86 context = Firebug.currentContext; 87 var doc = win ? win.document : context.window.document; 88 if (!doc) 89 { 90 if (FBTrace.DBG_ERRORS) 91 FBTrace.sysout("lib.getFontInfo; NO DOCUMENT", {win:win, context:context}); 92 return false; 93 } 94 95 var fonts = Fonts.getFonts(doc.documentElement); 96 97 if (FBTrace.DBG_FONTS) 98 FBTrace.sysout("Fonts.getFontInfo;", {fonts:fonts, identifier: identifier}); 99 100 for (var i=0; i<fonts.length; i++) 101 { 102 if (identifier == fonts[i].URI || 103 identifier.toLowerCase() == fonts[i].CSSFamilyName.toLowerCase() || 104 identifier.toLowerCase() == fonts[i].name.toLowerCase()) 105 { 106 return fonts[i]; 107 } 108 } 109 110 return false; 111 } 112 113 // ********************************************************************************************* // 114 115 return Fonts; 116 117 // ********************************************************************************************* // 118 }); 119