home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / tutorials / custEducation / opengl1 / lib / text.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  4.0 KB  |  145 lines

  1. /*
  2.  * Copyright 1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17.  
  18.  
  19. #include <aux.h>
  20. #include <auxPrivate.h>
  21.  
  22.  
  23. /*************************************************************************
  24.  *   Text Gloabal variables
  25.  **************************************************************************/
  26.  
  27. GLint                 nextFontId = ZERO;
  28.  
  29. /**************************************************************************
  30.  *   auxNewFont() - Create a usable font from glXUseXfont()
  31.  **************************************************************************/
  32.  
  33. GLint
  34. auxNewFont( char *fontName )
  35. {
  36.     GLint             count;
  37.     FontInfo          *new;
  38.     GLenum          err;
  39.  
  40.     new = (FontInfo *) malloc( sizeof( FontInfo ) );
  41.  
  42.     new->fontId = nextFontId++;
  43.     new->xFontInfo = (XFontStruct *) NULL;
  44.     new->listBase = ZERO;
  45.     new->firstChar = ZERO;
  46.     new->prev = (FontInfo *) NULL;
  47.     new->next = (FontInfo *) NULL;
  48.  
  49.     if ( !fontHead )
  50.         fontHead = new;
  51.     else {
  52.         new->next = fontHead;
  53.         fontHead->prev = new;
  54.         fontHead = new;
  55.     }
  56.  
  57.     new->xFontInfo = XLoadQueryFont( auxState.display, fontName );    
  58.  
  59.     if ( !new->xFontInfo )   /* safety check.. at least you get a font */
  60.         new->xFontInfo = XLoadQueryFont( auxState.display, "fixed" );
  61.  
  62.     if ( !new->xFontInfo->min_byte1 && !new->xFontInfo->max_byte1 ) {
  63.         count = new->xFontInfo->max_char_or_byte2;
  64.     } else {
  65.         printf( "\n\tUnable to figure out font bullshit\n" );
  66.         return MINUS_ONE;
  67.     }
  68.  
  69.     new->listBase = glGenLists(count);
  70.     if ( new->listBase == 0 && ((err = glGetError()) != GL_NO_ERROR) )
  71.         printf( "auxNewFont: %s\n", gluErrorString(err) );
  72.     
  73. #ifdef    DEBUG
  74.     printf( "auxNewFont: listbase = %d, count = %d\n", 
  75.          new->listBase, count );
  76. #endif
  77.     glXUseXFont( new->xFontInfo->fid, 0, count, new->listBase );
  78.     if ((err = glGetError()) != GL_NO_ERROR)
  79.         printf( "auxNewFont: %s\n", gluErrorString(err) );
  80.  
  81.     return new->fontId;
  82. }
  83.  
  84.  
  85. /**************************************************************************
  86.  *   auxRenderFontString() - Print an ASCII text string in given font
  87.  **************************************************************************/
  88.  
  89. GLvoid
  90. auxRenderFontString( GLint fontId, char *str )
  91. {
  92.     FontInfo       *tmpFont = fontHead;
  93.     GLenum        err;
  94.  
  95.     long oldlistbase;
  96.  
  97.     while ( tmpFont )
  98.         if ( tmpFont->fontId == fontId )
  99.             break;
  100.         else
  101.             tmpFont = tmpFont->next;
  102.  
  103.     if ( !tmpFont )
  104.         return;
  105.  
  106.     glPushAttrib( GL_LIST_BIT );
  107.     glListBase( tmpFont->listBase );
  108.     glCallLists( strlen( str ), GL_UNSIGNED_BYTE, (GLubyte *)str );
  109.     glPopAttrib();
  110. }
  111.  
  112.  
  113. /**************************************************************************
  114.  *   auxFontStringLength() - Return the length of an ASCII text string
  115.  *       in pixels for a particular font.  Return MINUS_ONE if passed
  116.  *       an illegal font.
  117.  **************************************************************************/
  118.  
  119. GLint
  120. auxFontStringLength( GLint fontId, char *str )
  121. {
  122.     char           *ch;
  123.     GLint          strLength = ZERO;
  124.     FontInfo       *tmpFont = fontHead;
  125.  
  126.     while ( tmpFont )
  127.         if ( tmpFont->fontId == fontId )
  128.             break;
  129.         else
  130.             tmpFont = tmpFont->next; 
  131.  
  132.     if ( !tmpFont )
  133.         return MINUS_ONE;
  134.  
  135.     for ( ch = str; *ch; ch++ ) {
  136.         int   perCharWidth, perFontWidth, width;
  137.         perCharWidth = tmpFont->xFontInfo->per_char[*ch].width;
  138.         perFontWidth = tmpFont->xFontInfo->max_bounds.width;
  139.         width = ( perCharWidth > perFontWidth ) ? perCharWidth : perFontWidth;
  140.         strLength += width;
  141.     }
  142.  
  143.     return strLength;
  144. }
  145.