<- previous    index    next ->

Lecture 13, Text Sizes and Fonts

Text size is measured in "points".
One "point" is 1/72 of an inch.
Thus text drawn at 72pt would be about one inch high.
(On paper!, it may be almost any size on a computer screen.)

Text such as lower case letters 'p' and 'q' extend below
the baseline for placing text. Upper case letters and language
marks may extend above the normal character height.
A letter or symbol in a font is called a glyph.

The bad news about fonts is that they require a lot of work
to create and thus are almost always copyrighted.

The good news is that you computer probably has many fonts available
for your program to use.

Fonts may have casual names such as Times Roman 12pt or
Courier 10pt. The "official" computer name is presented later.

Fonts may have proportional spacing, e.g. Times Roman,
where 'm' takes more space than 'i', and additionally
may be rendered using kerning that may place "AT" closer
together than 'A' 'T'.

Fonts may have fixed spacing, e.g. Courier, where every letter,
glyph, takes the same width.

Most people prefer proportional kerned spacing when reading
a newspaper or book, yet looking at computer source code
most prefer fixed width spacing.

TestFonts.java shows Courier New and Times New Roman



and writes out the available fonts TestFonts.outs

If you application needs the user to select a font, a style and a size,
then a Font Selection Box may be the most user friendly. Below is the
Word Perfect and Microsoft Word font selection windows.





Using X Windows you can experiment with creating or modifying
a font using the X Font Editor, xfed.c
on font timr24.bdf or font courr24.bdf
"bdf" indicates Berkeley Distribution Font IIRC.
Note the line starting "FONT" that has the official name of the font.
Fonts are registered so that every font has a unique official
designation.

To find the fonts available on your system:
In X Windows use font_list.c and
font_show.c
Note the font designation format in these two programs.

font_list.out shows over 500 fonts
on one system where font_list.c was run.

In OpenGL using GLUT the following bitmap fonts are available:
  GLUT_BITMAP_HELVETICA_10
  GLUT_BITMAP_HELVETICA_12
  GLUT_BITMAP_HELVETICA_18
  GLUT_BITMAP_TIMES_ROMAN_10
  GLUT_BITMAP_TIMES_ROMAN_24
  GLUT_BITMAP_9_BY_15
  GLUT_BITMAP_8_BY_13

Bitmap fonts do not move with the scene and do not scale
when the window size changes.

These are rendered using code such as 'show_text' from
text_in.c

void show_text(GLfloat x, GLfloat y, char msg[])
{
  int len, i;

  glPushMatrix();
    glRasterPos2f(x, y);
    len = strlen(msg);
    for (i = 0; i<len; i++)
      glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, msg[i]);
  glPopMatrix();
} /* end show_text */

Then in 'display' set the color or material and render the text:

  glLoadIdentity();
  glColor3f(0.0, 0.0, 0.0); /* black */
  show_text(-0.5, -1.0, "user input, file name");

If you do not see your text:
If using lighting, be sure material is applied to the text.
If using lighting, be sure the 'Z' coordinate is correct to
receive the light on the front of the text.
In various perspective views, it may be hard to figure out
where to place the text. One extreme measure is to use
the second projection as in:

static void drawText(int x, int y, char * msg)
{
  int   i, len;

  glMatrixMode(GL_PROJECTION);
  glPushMatrix();
    glLoadIdentity();
    glOrtho(0, winWidth, 0, winHeight, -1, 1);
    glMatrixMode(GL_MODELVIEW);
    glPushMatrix();
      glLoadIdentity();
      glColor3f(1.0f, 1.0f, 0.0f);
      glRasterPos2i(x, y);
      len = strlen(msg);
      for (i=0; i<len; i++)
        glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, msg[i]);
    glPopMatrix();
  glPopMatrix();
}

In OpenGL there are stroke fonts that move with objects and
scale when the window size changes. These fonts include:

  GLUT_STROKE_ROMAN
  GLUT_STROKE_MONO_ROMAN

static void drawText(GLfloat x, GLfloat y, char text[])
{
  char *p;

  glPushMatrix();
    glLoadIdentity();
    glTranslatef(x, y, 0.0);
    glScalef(0.01, 0.01, 0.0); /* 0.1 to 0.001 as required */
    for(p=text; *p; p++)
      glutStrokeCharacter(GLUT_STROKE_ROMAN, *p);
  glPopMatrix();
}

Then in 'display' call the 'drawText' function using:

  glLoadIdentity ();
  glEnable(GL_LINE_SMOOTH);
  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  glEnable(GL_BLEND);
  glLineWidth(1.0);
  /* glColor3f or material set here */
  drawText(3.0, 3.0, "Roman_stroke");
  glLineWidth(2.0);
  drawText(2.0, 6.0, "width 2 Roman_stroke");


    <- previous    index    next ->

Other links

Go to top