Search code examples
javascriptfontsfirefox-addonxpcom

Get a list of fonts in a Firefox extension


In a Firefox extension of mine, the preferences window apparently broke somewhere after Firefox 2.0, and (a part of) the problem lies in the following line:

var fontList  = Components.classes["@mozilla.org/gfx/fontlist;1"].
    createInstance(Components.interfaces.nsIFontList);

which errors with a

Error: Components.classes['@mozilla.org/gfx/fontlist;1'] is undefined

Some digging suggests that fontlist is in fact a wrapper around nsIFontEnumerator (https://bugzilla.mozilla.org/show_bug.cgi?id=397813) but I can't find anything on how to use nsIFontEnumerator.

Stackoverflow itself only has one question which sort of touches on the topic, which is unanswered: How to discover Font Type?


Solution

  • The interface you refer to no longer exists in current Firefox versions. What exists however is nsIFontEnumerator:

    var enumerator = Components.classes["@mozilla.org/gfx/fontenumerator;1"]
                               .getService(Components.interfaces.nsIFontEnumerator);
    var fonts = enumerator.EnumerateAllFonts({});
    

    It doesn't seem to be documented on MDC so http://www.oxymoronical.com/experiments/apidocs/interface/nsIFontEnumerator is the best reference you can get.