user10864898
user10864898

Reputation:

Is there a way to get a list of all installed fonts in the system in GTK

Hi I am making a Gtk application and need to run a code to get a list of all the installed fonts in my system to use it to create a list similar to the Gtk.FontChooserWidget, is there any built-in function in Gtk that can do this ?

Upvotes: 5

Views: 3103

Answers (1)

Chiheb Nexus
Chiheb Nexus

Reputation: 9267

Here is an example to get your system fonts names using Pango context from gi.repository.

PS: I'm assuming you're using Gtk3+ binding for Python

import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk


class Example(Gtk.Window):
    """Using Pango to get system fonts names"""

    def list_system_fonts(self):
        """Yield system fonts families names using Pango"""
        context = self.create_pango_context()
        for fam in context.list_families():
            yield fam.get_name()


a = Example()
system_fonts = list(a.list_system_fonts())
print(system_fonts)

Output (depends of the fonts you've in your machine, for example mines are):

['STIXIntegralsUp',
 'Noto Sans Gurmukhi',
 'Noto Serif Bengali',
 'Noto Sans Bengali',
 'Sharjah',
 'Noto Sans Mono CJK TC',
 'Noto Sans CJK TC',
 'Noto Sans Hebrew',
 'Lato',
 'Padauk',
 ...
'Rehan',
 'URW Bookman L',
 'FreeMono',
 'Ubuntu Mono',
 'STIXIntegralsSm',
 'Sans',
 'Serif',
 'Monospace']

Bonus:

More info about Pango and about Pango context.

Upvotes: 3

Related Questions