The family FONT_FAMILY_DEFAULT is the default font for XView. The FONT_FAM-
ILY_DEFAULT_ FIXEDWIDTH
font is the default fixed-width font.
All the characters in fixed-width fonts occupy the same amount of space. Other fonts are
proportionally spaced; that is, each character may occupy a different amount of space.
The available styles are:
• FONT_STYLE_DEFAULT
• FONT_STYLE_NORMAL
• FONT_STYLE_BOLD
• FONT_STYLE_ITALIC
• FONT_STYLE_OBLIQUE
• FONT_STYLE_BOLD_ITALIC
• FONT_STYLE_BOLD_OBLIQUE
The default style indicates the default font’s type for XView.
The call to xv_find() in simple_font.c could have been written to specify the family and
style of the font rather than the name of the font:
font = (Xv_Font)xv_find(frame, FONT,
FONT_FAMILY, FONT_FAMILY_LUCIDA,
FONT_STYLE, FONT_STYLE_NORMAL,
NULL);
Since normal is the default style of the font, this example renders the same font as in the ear-
lier example. However, we could specify a different style:
font = (Xv_Font)xv_find(frame, FONT,
FONT_FAMILY, FONT_FAMILY_LUCIDA,
FONT_STYLE, FONT_STYLE_BOLD,
NULL);
This call returns a bold style of the lucidasans font. For most font families, you can specify a
font family with any style, although some families may not support an italic or bold style of
the font. Therefore, you should be prepared to handle a
NULL return from the call to
xv_create() or xv_find():
if (!(font = (Xv_Font)xv_find(canvas, FONT,
FONT_FAMILY, FONT_FAMILY_COUR,
FONT_STYLE, FONT_STYLE_ITALIC,
NULL))) {
/* Handle the case where the font fails. */
font = (Xv_Font)xv_get(canvas, XV_FONT);
}
372 XView Programming Manual
16.1.2 Font Sizes
Fonts can be specified in any size from one point on up, as long as the bitmapped font of that
size exists on the server. If the server supports scalable fonts, it can scale the font to the
specified size, depending on the amount of available memory on the server. When a size is
specified, provided the font is not already loaded, a new font is created at run time according
to the family and style in the size specified, as below:
Xv_Font font;
font = (Xv_Font)xv_find(canvas, FONT,
FONT_FAMILY, FONT_FAMILY_ROMAN,
FONT_STYLE, FONT_STYLE_BOLD,
FONT_SIZE, 36,
NULL);
This code fragment attempts to find or create a font from the Times-Roman font family in
bold and in 36-point. The font may already exist in the server, or the font’s family may exist
but not the size. In the latter case, the server must attempt to scale the font to the specified
size. If the font cannot be created because of memory limitations, the call to xv_find()
will return NULL. Because a 36-point font takes a lot of memory, it might take a while to load
this font.
16.1.3 Scaling Fonts
Bitmapped fonts are not scalable, but they are provided in so many sizes that they appear to
be scalable to any size. On the other hand, some fonts are scalable because they are not
stored as static bitmaps. However, in order to scale even these fonts, the server must support
font scaling. Sun’s xnews server is an example of such a server.
You can request a relative scale of a font with respect to other fonts within the same family.
The relative scales are small, medium, large and extra large. These scales are represented by
the attributes
WIN_SCALE_SMALL, WIN_SCALE_MEDIUM, WIN_SCALE_LARGE, and
WIN_SCALE_EXTRALARGE.
These attributes are members of the enumerated type Window_rescale_state. They
are
WINDOW attributes because fonts can be scaled in proportion to their windows. For
example, if your application is resized to a larger or smaller size, you may wish to reset the
fonts for some windows according to different scaling sizes. By default, the sizes of the fonts
corresponding to the scale as shown in Table 16-1.
Fonts
Fonts 373
Table 16-1. Default Font Sizes
Attribute Font Size
WIN_SCALE_SMALL 10
WIN_SCALE_MEDIUM 12
WIN_SCALE_LARGE 14
WIN_SCALE_EXTRALARGE 19
If FONT_SIZE is not specified when a font is requested, the size will correspond to the
medium scale size. Specifying the font size overrides the request for a scaling factor. The
code fragment below requests a font from the Lucida family with an italic style in large
scale:
Xv_Font font;
font = (Xv_Font)xv_find(canvas, FONT,
FONT_FAMILY, FONT_FAMILY_LUCIDA,
FONT_STYLE, FONT_STYLE_ITALIC,
FONT_SCALE, WIN_SCALE_LARGE,
NULL);
If you have already created a font and wish to get it in a different scale, the attribute
FONT_RESCALE_OF is specified with with two arguments: a font and a Win-
dow_rescale_state.
Xv_Font font, small_font;
font = (Xv_Font)xv_find(canvas, FONT,
FONT_FAMILY, FONT_FAMILY_LUCIDA,
FONT_STYLE, FONT_STYLE_BOLD,
NULL);
...
small_font = (Xv_Font)xv_find(canvas, FONT,
FONT_RESCALE_OF, font, WIN_SCALE_SMALL,
NULL);
You can reset the sizes of a fonts’ scale factors by using the attribute FONT_SIZES_
FOR_SCALE
. This attribute takes four values that correspond to each scaling factor:
font = (Xv_Font)xv_find(canvas, FONT,
FONT_FAMILY, FONT_LUCIDA,
FONT_STYLE, FONT_STYLE_NORMAL,
FONT_SIZES_FOR_SCALE, 12, 14, 16, 22,
NULL);
In this example, the Lucida font is created so that the small, medium, large, and extra-large
scaling sizes are 12, 14, 16, and 22, respectively. The font size returned is 14-point.
374 XView Programming Manual
16.1.4 Fonts by Name
When specifying a font by name, the entire name of the font is given as it appears in the out-
put of a program such as xlsfonts or by the Xlib call XListFonts().* These programs
provide a complete list of all the fonts available on the server.
In Example 16-1 above, the program simple_font.c showed how to load a font using a given
name:
font = (Xv_Font) xv_find(canvas,
FONT, FONT_NAME, "lucidasans-12", NULL);
When specifying fonts by name, other attributes normally used to specify family, style, and
size or scale are ignored in favor of the font name. However, it is possible to determine the
family, style, and scale of a font by looking at its name. For example, with names such as
those shown below, this is easy.
lucidasans-12
lucidasans-bold-12
The X Logical Font Description (
XLFD) Conventions, which defines a standard way of nam-
ing a font, is a X Consortium standard. The font names are constructed using this convention
are unique, and descriptive (the name contains most of the information about the font.) For
details on these conventions, refer to Volume Zero, X Protocol Reference Manual. Below are
some sample XLFD font names:
-adobe-courier-bold-o-normal--10-100-75-75-m-60-iso8859-1
-adobe-courier-medium-r-normal--12-120-75-75-m-70-iso8859-1
When attributes other than FONT_NAME are used, XView will try to construct a font name
using the XLFD Conventions based on the information passed (family, style, size) and depen-
dent on the fonts available on the server. The constructed name will be returned when
xv_get() is used with FONT_NAME.
Likewise, if FONT_NAME is used, XView will load that font, and will try to decrypt the infor-
mation as stored in the name to fill in information for FONT_FAMILY, FONT_STYE, and so on.
16.2 Font Dimensions
Once a font is created, it can be used in applications using Xlib routines, as demonstrated in
simple_font.c earlier in this chapter. In the program, text is rendered using the Xlib routine
XDrawString(). This routine uses the font ID from the GC parameter. This ID is
extracted from the font using xv_get() and XV_XID. You can also get a pointer to the
font’s XFontStruct structure by specifying the attribute FONT_INFO to xv_get(). This
data structure is what describes the characteristics of the font such as width and height for
each character. See Volume One, Xlib Programming Manual, for more information.
*See Volume Two, Xlib Reference Manual, for a description of XListFonts().
Fonts
Fonts 375
You can get information about the sizes of individual characters or the dimensions of entire
strings of characters in a particular font using several methods. You can use the information
in the XFontStruct data structure obtained from the font, or you can use Xlib routines
such as XTextWidth() or XTextExtents(), or you can use xv_get() along with
XView attributes such as FONT_CHAR_WIDTH, FONT_CHAR_HEIGHT, FONT_DEFAULT_
CHAR_HEIGHT
, FONT_DEFAULT_CHAR_WIDTH, and FONT_STRING_DIMS.
The usage is as follows:
Xv_Font font;
int width, height;
The following code shows how to get the dimensions of a particular character in a font:
width = (int)xv_get(font, FONT_CHAR_WIDTH, ’m’);
height = (int)xv_get(font, FONT_CHAR_HEIGHT, ’m’);
The calls to xv_get() return the width and height of the characters in pixels for that partic-
ular font. If you are using a fixed-width font, then each character will be the same width, so
you can specify the default character width and height of the font. The following code shows
how to get the dimensions of characters for a fixed-width font:
Xv_Font font;
int width, height;
width = (int)xv_get(font, FONT_DEFAULT_CHAR_WIDTH);
height = (int)xv_get(font, FONT_DEFAULT_CHAR_HEIGHT);
If you use FONT_DEFAULT_CHAR_WIDTH (or height) on a variable-width font, you will get the
average width of a character in that font.
To get the width and height dimensions of a complete string of text in a given font, use the
FONT_STRING_DIMS attribute:
extern Xv_Font font;
Font_string_dims dims;
(void) xv_get(font, FONT_STRING_DIMS, "Hello World", &dims);
In this case, the call to xv_get() returns a pointer to the dims structure passed as the last
argument. The return value may be ignored since the dims parameter will have the value
filled in upon return of xv_get(). The Font_string_dims data structure is as follows:
typedef struct {
int width;
int height;
} Font_string_dims;
Thus, xv_get() returns the dimensions of the string for the font specified. This would be
equivalent to calling XTextExtents() in the manner below:
Font_string_dims dims;
extern char *str;
int len = strlen(str);
extern Xv_Font font;
XFontStruct *font_info = (XFontStruct *)xv_get(font, FONT_INFO);
int direction, ascent, descent;
XCharStruct overall_return;
(void) XTextExtents(font_info, str, len,
376 XView Programming Manual
Get Volume 7A: XView Programming Manual now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.