Game Development Community

EnumeratePlatformFonts for X11

by Gary "ChunkyKs" Briggs · in Torque Game Builder · 12/13/2006 (6:04 pm) · 1 replies

LevelBuilderTextObject needs an enumeratePlatformFonts function. Avert your eyes, those with sensitive dispositions, X11 API code can be found herein!

[slap this on the end of x86UNIXFont.cc, it's prototyped already in platform/ somewhere]

void enumeratePlatformFonts( Vector<StringTableEntry>& fonts, char* fontFamily ) {
	char **xfonts;
	int maxfonts = 10000; // This is a lot of fonts.
				// My Mac's X Server has 5834
	char pattern[32] = "*";
	int count;
	Display *display = XOpenDisplay(getenv("DISPLAY"));
	if(NULL == display) {
		Con::errorf("Couldn't open the display %s", getenv("DISPLAY"));
	}

	if(fontFamily) {
		dSprintf(pattern, 32, "-%s-*", fontFamily);
		// There is a space reserved for X fonts IN HELL
	}
	xfonts = XListFonts(display, pattern, maxfonts, &count);

	char **f = xfonts;

	while(*f) {
		if(NULL != fontFamily || '-' != **f ) {
			// Only show aliases, unless
			// 	they've asked for a specific famliy
			fonts.push_back( StringTable->insert(*f));
		}
		f++;
	}
	XFreeFontNames(xfonts);
	XCloseDisplay(display);
}

Gary (-;

#1
02/04/2007 (3:59 pm)
Just to keep everything together, Andrew Pfiffer offered this version of the function (on this thread):
void enumeratePlatformFonts( Vector<StringTableEntry> &fonts, char* fontFamily)
{
  Display *display = XOpenDisplay(getenv("DISPLAY"));
  int screen;
  XftFontSet *fs;
  char *name;
  int i;

  Con::printf("enumeratePlatformFonts: howdy!");
  if (!display)
    AssertFatal(false, "enumeratePlatformFonts: cannot connect to X server");

  screen = DefaultScreen(display);
  fs = XftListFonts(display, screen, 0, XFT_FAMILY, NULL);
  for (i = 0; i < fs->nfont; i++) {
      if (XftPatternGetString(fs->fonts[i], XFT_FAMILY, 0, &name) == XftResultMatch) {
        Con::printf("enumeratePlatformFonts: name=%s", name);
        StringTableEntry steBuffer = StringTable->insert( name );
        fonts.push_back( steBuffer );
      }
  }
  XftFontSetDestroy(fs);
  XCloseDisplay(display);
  Con::printf("enumeratePlatformFonts: bye bye");
}
Here's his post:
Quote:I've hacked up a version of enumeratePlatformFonts() for Linux that seems to kinda work for me. By "kinda", I mean that the rendered text doesn't look too great, and it did mysteriously crash once when selecting a font in the level editor.

This code goes inside platformX86UNIX/x86UNIXFont.cc, and you'll need to have t2dTextObject.cc, levelBuilderTextObjectTool.cc, and levelBuilderTextEditTool.cc added to targets.torque.mk for it to stand any chance of working.

It doesn't pay any attention to the fontFamily parameter...