2

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?

Community
  • 1
  • 1
Kalle
  • 13,186
  • 7
  • 61
  • 76

1 Answers1

4

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.

Wladimir Palant
  • 56,865
  • 12
  • 98
  • 126
  • Fantastic, thanks this still works! Is this still the recommended way to do it? I'll eventually ctype this ( [windows](http://stackoverflow.com/questions/11253827/too-many-fonts-when-enumerating-with-enumfontfamiliesex-function) / [mac](http://stackoverflow.com/a/30189936/1828637) / [linux](http://askubuntu.com/a/758184/321266) ) but i needed a quick solution for right now. – Noitidart Apr 17 '16 at 09:16
  • As this is undocumented functionality, it cannot really be recommended - subject to change any time. However, this particular interface has been stable for the past five years apparently. – Wladimir Palant Apr 17 '16 at 13:04