How to get a Font from the GC - Xwindows

This is a discussion on How to get a Font from the GC - Xwindows ; How is someone suppose to get information about the default font used by a GC? One can set the font by using the call XSetFont. It would seem logical that someone could call XGetFont to retreive the font. However, no ...

+ Reply to Thread
Results 1 to 4 of 4

Thread: How to get a Font from the GC

  1. How to get a Font from the GC

    How is someone suppose to get information about the default font used
    by a GC?

    One can set the font by using the call XSetFont. It would seem
    logical that someone could call XGetFont to retreive the font.
    However, no such API call exists.

    The standard way to get a font seems to be to call XGetGCValues
    specifying the GCFont for the valuemask parameter. Then, use the font
    member of the XGCValues structure in a call to XQueryFont to get an
    XFontStruct pointer that defines the properties for the font.

    This has a problem, however, because the documentation says
    XGetGCValues will return an invalid font value if the user hasn't
    explicitly set a font for the GC via XSetFont. Thus, the call the
    XQueryFont with the invalid font will return a NULL pointer.

    Why does XGetGCValues behave this way? Someone can use XCreateGC to
    create the GC and not explicitly set any font for it. That doesn't
    prevent the GC from successfully drawing text with whatever font it
    uses. Why can't someone get access to that particular font? Or, is
    there some other method for getting the default font?

  2. Re: How to get a Font from the GC

    Tron Thomas wrote:
    >How is someone suppose to get information about the default font used
    >by a GC?
    >
    >One can set the font by using the call XSetFont. It would seem
    >logical that someone could call XGetFont to retreive the font.
    >However, no such API call exists.
    >
    >The standard way to get a font seems to be to call XGetGCValues
    >specifying the GCFont for the valuemask parameter. Then, use the font
    >member of the XGCValues structure in a call to XQueryFont to get an
    >XFontStruct pointer that defines the properties for the font.
    >
    >This has a problem, however, because the documentation says
    >XGetGCValues will return an invalid font value if the user hasn't
    >explicitly set a font for the GC via XSetFont. Thus, the call the
    >XQueryFont with the invalid font will return a NULL pointer.
    >
    >Why does XGetGCValues behave this way? Someone can use XCreateGC to
    >create the GC and not explicitly set any font for it. That doesn't
    >prevent the GC from successfully drawing text with whatever font it
    >uses. Why can't someone get access to that particular font? Or, is
    >there some other method for getting the default font?



    XGetGCValues is not a real call to the server -- there is no such
    protocol item. Instead, Xlib maintains a client-side cache that
    remembers values that have been set in the app's GC's; XGetGCValues
    can only return values from this cache, and never the default values
    found in a fresh GC.

    XQueryFont is allowed to take a GContext ID as well as a Font ID;
    the GContextID is returned by calling XGContextFromGC(gc). This allows
    you to get you XFontStruct and proceed as normal.

    The default font is compiled into the server -- usually "fixed" --
    but this can be overridden on the command line when starting the server.


  3. Re: How to get a Font from the GC

    In comp.windows.x, Tron Thomas

    wrote
    on 4 Feb 2004 11:28:06 -0800
    :
    > How is someone suppose to get information about the default font used
    > by a GC?
    >
    > One can set the font by using the call XSetFont. It would seem
    > logical that someone could call XGetFont to retreive the font.
    > However, no such API call exists.
    >
    > The standard way to get a font seems to be to call XGetGCValues
    > specifying the GCFont for the valuemask parameter. Then, use the font
    > member of the XGCValues structure in a call to XQueryFont to get an
    > XFontStruct pointer that defines the properties for the font.
    >
    > This has a problem, however, because the documentation says
    > XGetGCValues will return an invalid font value if the user hasn't
    > explicitly set a font for the GC via XSetFont. Thus, the call the
    > XQueryFont with the invalid font will return a NULL pointer.
    >
    > Why does XGetGCValues behave this way? Someone can use XCreateGC to
    > create the GC and not explicitly set any font for it. That doesn't
    > prevent the GC from successfully drawing text with whatever font it
    > uses. Why can't someone get access to that particular font? Or, is
    > there some other method for getting the default font?


    Good question, although I do have an answer; the
    traditional "first font" has always been "fixed".
    This font is monospaced and rather tiny. Another font
    that should be always available is 'variable', which is
    variable-width and also rather tiny.

    Admittedly, this might have worked better in 1986... :-)

    --
    #191, ewill3@earthlink.net
    It's still legal to go .sigless.

  4. Re: How to get a Font from the GC

    kbr@pangea.ca (Kip Rugger) wrote in message news:...
    > XGetGCValues is not a real call to the server -- there is no such
    > protocol item. Instead, Xlib maintains a client-side cache that
    > remembers values that have been set in the app's GC's; XGetGCValues
    > can only return values from this cache, and never the default values
    > found in a fresh GC.
    >
    > XQueryFont is allowed to take a GContext ID as well as a Font ID;
    > the GContextID is returned by calling XGContextFromGC(gc). This allows
    > you to get you XFontStruct and proceed as normal.
    >
    > The default font is compiled into the server -- usually "fixed" --
    > but this can be overridden on the command line when starting the server.


    I tried using the XGContextFromGC as the font id for XQueryFont, and I
    initially had some trouble with it. It appears that if someone uses
    XGContextFromGC they should not call XFreeFont on the structure
    returned from XQueryFont.

+ Reply to Thread