Setting the font size

This is a discussion on Setting the font size within the Linux Programming forums, part of the Platform Specific Boards category; In XWIndows how do I set the font size to draw with? There's probably something simple I'm missing on this. ...

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    77

    Setting the font size

    In XWIndows how do I set the font size to draw with? There's probably something simple I'm missing on this.

    I know you can get a font's size with something like this:
    Code:
    int FontSize(Display Disp, char *FontName)
    {
        XFontStruct *FontInfo;
        int Height;
        FontInfo = XLoadQueryFont(Disp, "fixed");
        Height =  FontInfo.ascent + FontInfo.descent
        XFreeFont(Disp, FontInfo);
        return Height;
    }
    But how can I tell it I want to use font size 18 when I output text?
    So, do you understand everything you know about this yet?

    "Begin at the beginning," the King said, very gravely, "and go on till you come to the end; then stop."

  2. #2
    Registered User
    Join Date
    Aug 2004
    Posts
    77
    Maybe an answer?

    This is a crude way of setting the font size, its by no means very thorough though. If you read the font.alias file (not sure what its default location is, I found mine at /usr/X11R6/lib/X11/fonts/misc/fonts.alias) it has a list of alias names for fonts. Among the aliases you see things like 5x7, 5x8, 6x9 and so on. Those appear to be average character sizes, width x height.

    You can load fonts based on these aliases. Like this:

    Code:
    Font font;
    font = XLoadFont(cdisDisplay, "6x9");
    gc = XCreateGC(/*Code Omitted*/);
    XSetFont(cdisDisplay, Context, font);
    Any text drawing done with that GC (XDrawText() function calls) will write using the font aliased by 6x9. This also permits wildcards, so you can do stuff like this:
    Code:
    font = XLoadFont(cdisDisplay, "*x9");
    And it will load any font with x9 in the name. By using that it seems you can choose a font height (which is pretty close to selecting a font size) and it will look for a matching name or alias.

    This is obviously not the best way of doing this, since not all sizes will have a convenient alias and I don't know how well you can predict what aliases will be available from one system to the next. The other thing it doesn't permit is selecting a font and size, since you are tied to the fonts that are aliases (which appear to be all fixed width fonts).

    As a side note, you can take a look at fonts.dir files (do a search for then, their appears to be multiple) to get a full listing of the available fonts. No aliases, but you can load them by name, and wildcards are still permitted in loading them. So you can load "*helvetica*" to get the helvetica font, but no size. Not sure how to get a size and font together though.

    Theres a number of functions that allow you to enumerate the fonts set in those files. But it appears that loading fonts is tied to what is set in these files.

    Its not perfect, but its a pretty decent method of setting the size of a font.
    Last edited by Exile; 02-16-2005 at 09:15 AM.
    So, do you understand everything you know about this yet?

    "Begin at the beginning," the King said, very gravely, "and go on till you come to the end; then stop."

  3. #3
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Although you seem to have worked out a solution for yourself , I thought you might still be interested in this page (ie. Appendix A of Xlib Programming Manual ) which seems to provide a more formal approach to enumerating and using fonts with xlib.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  4. #4
    Registered User
    Join Date
    Aug 2004
    Posts
    77
    Thanks, that clears up a lot of the questions I had left.
    So, do you understand everything you know about this yet?

    "Begin at the beginning," the King said, very gravely, "and go on till you come to the end; then stop."

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Change Font Size
    By strokebow in forum Windows Programming
    Replies: 1
    Last Post: 03-30-2009, 12:01 PM
  2. Adventures in labyrinth generation.
    By guesst in forum Game Programming
    Replies: 8
    Last Post: 10-12-2008, 01:30 PM
  3. Changing font size of CStatic
    By earth_angel in forum Windows Programming
    Replies: 2
    Last Post: 08-12-2005, 10:51 AM
  4. setting font in buttons etc.
    By lobo in forum Windows Programming
    Replies: 4
    Last Post: 01-19-2005, 02:48 PM
  5. Replies: 11
    Last Post: 03-25-2003, 04:13 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21