+++ /dev/null
-/*
- * main.c
- *
- * A simple utility to generate the bitmap fonts to be used in freeglut.
- *
- * Copyright (c) 1999-2000 by Pawel W. Olszta
- * Written by Pawel W. Olszta, <olszta@sourceforge.net>
- * Creation date: nie gru 26 21:52:36 CET 1999
- *
- * Permission is hereby granted, free of charge, to any person obtaining a
- * copy of this software and associated documentation files (the "Software")
- * to deal in the Software without restriction, including without limitation
- * the rights to use, copy, modify, merge, publish, distribute, sublicense,
- * and/or sell copies of the Software, and to permit persons to whom the
- * Software is furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included
- * in all copies or substantial portions of the Sotware.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
- * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
- * PAWEL W. OLSZTA BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
- * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
- * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- */
-
-#ifdef HAVE_CONFIG_H
- #include <config.h>
-#endif
-
-#include <X11/Xlib.h>
-#include <X11/Xutil.h>
-
-#include <string.h>
-#include <stdlib.h>
-#include <stdio.h>
-#include <assert.h>
-
-/*
- * Define the log domain
- */
-#undef G_LOG_DOMAIN
-#define G_LOG_DOMAIN "genfonts"
-
-/*
- * The alphabet we want to export.
- */
-char* g_Alphabet = " abcdefghijklmnopqrstuwvxyzABCDEFGHIJKLMNOPQRSTUWVXYZ0123456789`~!@#$%^&*()-_=+[{}];:,.<>/?\\\"";
-int g_AlphabetLength = 0;
-
-/*
- * All undefined characters will get replaced by this one:
- */
-char g_NoChar = '*';
-
-/*
- * The stream we want to redirect our output to
- */
-FILE* g_Output = NULL;
-
-/*
- * The display we're getting the fonts from
- */
-Display* g_Display;
-
-/*
- * Our argv[0]
- */
-char *g_ProgName = "";
-
-/*
- * This function outputs the font file prologue
- */
-void OutputPrologue( char* fileName )
-{
- /*
- * Output the copyright and permission notices:
- */
- fprintf( g_Output, "/*\n * %s\n *\n * This file has been automatically generated by the genfonts utility.\n *\n", fileName );
- fprintf( g_Output, " * Copyright (c) 1999-2000 by Pawel W. Olszta\n * Written by Pawel W. Olszta, <olszta@sourceforge.net>\n * \n" );
- fprintf( g_Output, " * Permission is hereby granted, free of charge, to any person obtaining a\n" );
- fprintf( g_Output, " * copy of this software and associated documentation files (the \"Software\"),\n" );
- fprintf( g_Output, " * to deal in the Software without restriction, including without limitation\n" );
- fprintf( g_Output, " * the rights to use, copy, modify, merge, publish, distribute, sublicense,\n" );
- fprintf( g_Output, " * and/or sell copies of the Software, and to permit persons to whom the\n" );
- fprintf( g_Output, " * Software is furnished to do so, subject to the following conditions:\n *\n" );
- fprintf( g_Output, " * The above copyright notice and this permission notice shall be included\n" );
- fprintf( g_Output, " * in all copies or substantial portions of the Sotware.\n *\n" );
- fprintf( g_Output, " * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS\n" );
- fprintf( g_Output, " * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n" );
- fprintf( g_Output, " * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL\n" );
- fprintf( g_Output, " * PAWEL W. OLSZTA BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\n" );
- fprintf( g_Output, " * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\n" );
- fprintf( g_Output, " * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n */\n" );
-
- /*
- * The obvious include headers
- */
- fprintf( g_Output, "\n#ifdef HAVE_CONFIG_H\n#include \"config.h\"\n#endif\n\n#include \"../include/GL/freeglut.h\"\n#include \"../include/GL/freeglut_internal.h\"\n" );
-}
-
-/*
- * This function outputs a font set
- */
-void OutputFont( char* freeglutFontName, char* fontName )
-{
- int character, lineWidth, maxWidth = 0, maxHeight = 0;
- XFontStruct* fontStruct = NULL;
- XGCValues contextValues;
- XImage* image = NULL;
- unsigned char* lineBuffer;
- Pixmap buffer;
- GC context;
-
- /*
- * Check if there is a font that meets our requirements
- */
- fontStruct = XLoadQueryFont( g_Display, fontName );
-
- if( fontStruct == NULL )
- {
- /*
- * Whoops, the font was not found
- */
- fprintf( stderr, "%s: couldn't get font `%s' using local display\n",
- g_ProgName, fontName );
- exit( 1 );
- }
-
- /*
- * Get the maximum size of the font characters
- */
- maxWidth = fontStruct->max_bounds.rbearing - fontStruct->min_bounds.lbearing;
- maxHeight = fontStruct->max_bounds.ascent + fontStruct->max_bounds.descent;
-
- /*
- * Allocate the line buffer for storing the font bitmap lines
- */
- lineBuffer = malloc( maxWidth );
-
- /*
- * Create a pixmap buffer where we'll be rendering our fonts to.
- */
- buffer = XCreatePixmap(
- g_Display,
- RootWindow( g_Display, DefaultScreen( g_Display ) ),
- maxWidth,
- maxHeight,
- 1
- );
-
- /*
- * We'll need a graphics context to handle the font writes and buffer clears
- */
- context = XCreateGC(
- g_Display,
- buffer,
- 0,
- &contextValues
- );
-
- /*
- * Have the font assigned to the graphics context
- */
- XSetFont( g_Display, context, fontStruct->fid );
-
- /*
- * For every character we want to have outputted...
- */
- for( character=0; character<g_AlphabetLength; character++ )
- {
- int x, y, start_x, stop_x;
-
- /*
- * Clear the context black (0 is black in our case)...
- */
- XSetForeground( g_Display, context, 0x00 );
- XFillRectangle( g_Display, buffer, context, 0, 0, maxWidth, maxHeight );
-
- /*
- * Be kind and draw the characters white (which is 1 for us)
- */
- XSetForeground( g_Display, context, 0xff );
-
- /*
- * Draw the n-th character of the alphabet
- */
- XDrawString(
- g_Display,
- buffer,
- context,
- -fontStruct->min_bounds.lbearing,
- fontStruct->max_bounds.ascent,
- (g_Alphabet + character),
- 1
- );
-
- /*
- * We need some a way to access the font we've just drawn:
- */
- image = XGetImage(
- g_Display,
- buffer,
- 0, 0,
- maxWidth, maxHeight,
- 1, XYPixmap
- );
-
- /*
- * Find the first non-empty column:
- */
- start_x = -1; stop_x = -1;
-
- for( x=0; x<maxWidth; x++ )
- for( y=0; y<maxHeight; y++ )
- if( (XGetPixel( image, x, y ) == 1) && (start_x == -1) )
- start_x = x;
-
- /*
- * Find the last empty column
- */
- for( x=maxWidth-1; x>=0; x-- )
- for( y=0; y<maxHeight; y++ )
- if( (XGetPixel( image, x, y) == 1) && (stop_x == -1) )
- stop_x = x + 1;
-
- /*
- * If the size is too little, enhance it a bit
- */
- if( stop_x - start_x < 1 )
- {
- start_x = 0; stop_x = maxWidth - 1;
- }
-
- /*
- * Output the character we have just grabbed
- */
- fprintf( g_Output, "static const GLubyte %s_Character_%03i[] = {%3i",
- freeglutFontName, (int) g_Alphabet[ character ], stop_x-start_x
- );
-
- for( y=maxHeight-1; y>=0; y-- )
- {
- /*
- * Prepare the line buffer for being used again
- */
- memset( lineBuffer, 0, maxWidth );
-
- /*
- * Grab the rasterized character face into the line buffer
- */
- for( x=start_x, lineWidth=0; x<stop_x; x++, lineWidth++ )
- if( XGetPixel( image, x, y ) )
- lineBuffer[ lineWidth / 8 ] |= 1 << (7 - (lineWidth % 8));
-
- /*
- * Feel free to output the final line bitmap now
- */
- for( x=0; x<(stop_x - start_x + 7) / 8; x++ )
- fprintf( g_Output, ",%3i", lineBuffer[ x ] );
- }
-
- fprintf( g_Output, "};\n" );
-
- /*
- * Free the image, and get to the next character...
- */
- XDestroyImage( image );
- }
-
- /*
- * Now we are ready to output the final data concerning the font charset
- */
- fprintf( g_Output, "\n/* The font characters mapping: */\n" );
- fprintf( g_Output, "static const GLubyte* %s_Character_Map[] = {", freeglutFontName );
-
- /*
- * I have decided to change the characters mapping a bit...
- */
- for( character=1; character<256; character++ )
- {
- char ourCharacter[ 2 ] = { 0, 0 };
-
- /*
- * Do we have the character defined or not?
- */
- ourCharacter[ 0 ] = (char) character;
-
- if( strstr( g_Alphabet, ourCharacter ) == NULL )
- {
- /*
- * Nope, output the g_NoChar character instead:
- */
- fprintf( g_Output, "%s_Character_%03i,", freeglutFontName, (int) g_NoChar );
- }
- else
- {
- /*
- * Otherwise we're welcome to output the character:
- */
- fprintf( g_Output, "%s_Character_%03i,", freeglutFontName, (int) ourCharacter[ 0 ] );
- }
- }
-
- fprintf( g_Output, "NULL};\n\n" );
-
- /*
- * And finally have the font structure written to the output stream
- */
- fprintf( g_Output, "/* The font structure: */\n" );
- fprintf( g_Output, "const SFG_Font fgFont%s = { \"%s\", %i, %i, %s_Character_Map };\n\n",
- freeglutFontName, fontName, g_AlphabetLength, maxHeight, freeglutFontName
- );
-
- /*
- * Done, clean up behind...
- */
- XFreeGC( g_Display, context );
- XFreePixmap( g_Display, buffer );
- free( lineBuffer );
-}
-
-/*
- * This function outputs the font file epilogue
- */
-void OutputEpilogue( void )
-{
- fprintf( g_Output, "/*** END OF FILE ***/\n" );
-}
-
-/*
- * The main function processes the command line arguments
- * and outputs all the fonts we need to have rasterized.
- */
-int main( int argc, char** argv )
-{
- char ourCharacter[ 2 ] = { 0, 0 };
- char* outputFileName = NULL;
- char* displayName = NULL;
- int i = 1;
-
- /*
- * The fonts that are going to be rasterized and added to the output file:
- */
- int fontsQuantity = 7;
- char* fontsList[] = {
- "Fixed8x13", "-misc-fixed-medium-r-normal--13-120-75-75-C-80-iso8859-1",
- "Fixed9x15", "-misc-fixed-medium-r-normal--15-140-75-75-C-90-iso8859-1",
- "Helvetica10", "-adobe-helvetica-medium-r-normal--10-100-75-75-p-56-iso8859-1",
- "Helvetica12", "-adobe-helvetica-medium-r-normal--12-120-75-75-p-67-iso8859-1",
- "Helvetica18", "-adobe-helvetica-medium-r-normal--18-180-75-75-p-98-iso8859-1",
- "TimesRoman10", "-adobe-times-medium-r-normal--10-100-75-75-p-54-iso8859-1",
- "TimesRoman24", "-adobe-times-medium-r-normal--24-240-75-75-p-124-iso8859-1"
- };
-
- g_ProgName = argv[0];
-
- /*
- * Initialize the alphabet's length
- */
- g_AlphabetLength = strlen( g_Alphabet );
-
- /*
- * Make sure that the no-character character is in the alphabet
- */
- ourCharacter[ 0 ] = g_NoChar;
-
- if( strstr( g_Alphabet, ourCharacter ) == NULL )
- {
- fprintf( stderr, "%s the g_NoChar `%c' character not found in the alphabet `%s'\n",
- g_ProgName, g_NoChar, g_Alphabet );
- exit( 1 );
- }
-
- /*
- * Grab the display name to be used
- */
- displayName = strdup( getenv( "DISPLAY" ) );
-
- /*
- * Define the default output file name
- */
- outputFileName = strdup( "freeglut_font_data.c" );
-
- /*
- * Process the command line arguments now. Command line arguments expected:
- *
- * -display <DISPLAYNAME> -- the display to connect to
- * -file <FILENAME> -- the destination file name
- */
- while( i < argc )
- {
- /*
- * See what the current token is
- */
- if( strcasecmp( argv[ i ], "-display" ) == 0 )
- {
- assert( (i + 1) < argc );
- free( displayName );
-
- /*
- * The next token is expected to contain the X display name to use
- */
- displayName = strdup( argv[ ++i ] );
- }
- else if( strcasecmp( argv[ i ], "-file" ) == 0 )
- {
- assert( (i + 1) < argc );
- free( outputFileName );
-
- /*
- * The next token is expected to contain the destination file name
- */
- outputFileName = strdup( argv[ ++i ] );
- }
-
- /*
- * Get to the next argument
- */
- i++;
- }
-
- /*
- * Connect to the X display
- */
- g_Display = XOpenDisplay( displayName );
- assert( g_Display != NULL );
-
- /*
- * Have the destination file opened
- */
- g_Output = fopen( outputFileName, "wt" );
- assert( g_Output != NULL );
-
- /*
- * Output the file header first
- */
- OutputPrologue( outputFileName );
-
- /*
- * In the file header, have the list of the fonts written:
- */
- fprintf( g_Output, "\n/*\n * Following fonts are defined in this file:\n * \n" );
-
- for( i=0; i<fontsQuantity; i++ )
- fprintf( g_Output, " * %i. fgFont%s <%s>\n",
- i + 1, fontsList[ i*2 + 0 ], fontsList[ i*2 + 1 ]
- );
-
- fprintf( g_Output, " */\n\n" );
-
- /*
- * Output all of the fonts we want to output
- */
- for( i=0; i<fontsQuantity; i++ )
- OutputFont( fontsList[ i*2 + 0 ], fontsList[ i*2 + 1 ] );
-
- /*
- * Finally, have the file epilogue outputted
- */
- OutputEpilogue();
-
- /*
- * Close the output stream
- */
- fclose( g_Output );
-
- /*
- * Close the X display
- */
- XCloseDisplay( g_Display );
-
- /*
- * Clean up all the rest of the mess
- */
- free( outputFileName );
- free( displayName );
-
- /*
- * Return successful!
- */
- return( EXIT_SUCCESS );
-}
-
-/*** END OF FILE ***/