idisplayfont.c
资源名称:ilib [点击查看]
上传用户:changbiao
上传日期:2007-01-13
资源大小:141k
文件大小:4k
源码类别:

图片显示

开发平台:

C/C++

  1. /*
  2.   Dump a font to stdout as an image
  3.   Usage:
  4.     displayfont [options] fontfile > output.pnm
  5.     where options are:
  6.     -gif output format is GIF
  7.     -png output format is PNG
  8.     -ppm output format is PPM/PNM (default)
  9.     -hex display ascii char numbers in hex
  10.     -dec display ascii char numbers in decimal (default)
  11.   19-Jul-1999 Added -png option
  12. Craig Knudsen cknudsen@radix.net
  13.   12-Apr-1999 Created
  14. Craig Knudsen cknudsen@radix.net
  15. ***************************************************************************/
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <time.h>
  20. #include <ctype.h>
  21. #include <math.h>
  22. #include <values.h>
  23. #include <sys/types.h>
  24. #include <sys/time.h>
  25. #include <Ilib.h>
  26. #include "courR10.h" /* font for labeling table */
  27. /*
  28. ** Main
  29. */
  30. int main ( argc, argv )
  31. int argc;
  32. char *argv[];
  33. {
  34.   IImage image = NULL;
  35.   IFont font = NULL, smallfont = NULL;
  36.   char *fontname = NULL;
  37.   int loop;
  38.   int format = IFORMAT_PPM;
  39.   IError ret;
  40.   int width, height;
  41.   int cell_width = 40;
  42.   int cell_height = 40;
  43.   IColor black, white, grey, navy;
  44.   IGC gc;
  45.   int x = 5, y = 5, subx, suby;
  46.   char temp[20];
  47.   unsigned int w, h;
  48.   int usehex = 0;
  49.   /* process command line arguments */
  50.   for ( loop = 1; loop < argc; loop++ ) {
  51.     if ( strcmp ( argv[loop], "-ppm" ) == 0 )
  52.       format = IFORMAT_PPM;
  53.     else if ( strcmp ( argv[loop], "-pnm" ) == 0 )
  54.       format = IFORMAT_PPM;
  55.     else if ( strcmp ( argv[loop], "-gif" ) == 0 ) {
  56. #ifdef HAVE_GIFLIB
  57.       format = IFORMAT_GIF;
  58. #else
  59.       fprintf ( stderr, "GIF not supported (missing giflib).n" );
  60.       exit ( 1 );
  61. #endif
  62.     }
  63.     else if ( strcmp ( argv[loop], "-png" ) == 0 ) {
  64. #ifdef HAVE_PNGLIB
  65.       format = IFORMAT_PNG;
  66. #else
  67.       fprintf ( stderr, "PNG not supported (missing libpng).n" );
  68.       exit ( 1 );
  69. #endif
  70.     }
  71.     else if ( strcmp ( argv[loop], "-hex" ) == 0 )
  72.       usehex = 1;
  73.     else if ( strcmp ( argv[loop], "-dec" ) == 0 )
  74.       usehex = 0;
  75.     else if ( *argv[loop] == '-' ) {
  76.       fprintf ( stderr, "%s: unrecognized argument "%s"n",
  77.         argv[0], argv[loop] );
  78.       exit ( 1 );
  79.     }
  80.     else {
  81.       fontname = "myfont";
  82.       ret = ILoadFontFromFile ( fontname, argv[loop], &font );
  83.       if ( ret ) {
  84.         fprintf ( stderr, "Error loading font: %sn",
  85.           IErrorString ( ret ) );
  86.         exit ( 1 );
  87.       }
  88.     }
  89.   }
  90.   if ( fontname == NULL ) {
  91.     fprintf ( stderr, "Error: no font file specified.n" );
  92.     fprintf ( stderr, "Usage:tdisplayfont [-gif|-png|-ppm] [-hex|-dec] bdffile > outputfilen" );
  93.     exit ( 1 );
  94.   }
  95.   /* load the small font used for labeling */
  96.   ret = ILoadFontFromData ( "smallfont", courR10_font, &smallfont );
  97.   /* create output */
  98.   width = 16 * cell_width + 10;
  99.   height = 16 * cell_height + 10;
  100.   image = ICreateImage ( width, height, IOPTION_NONE );
  101.   black = IAllocColor ( 0, 0, 0 );
  102.   white = IAllocColor ( 255, 255, 255 );
  103.   grey = IAllocColor ( 192, 192, 192 );
  104.   navy = IAllocColor ( 0, 0, 128 );
  105.   gc = ICreateGC ();
  106.   ISetForeground ( gc, white );
  107.   IFillRectangle ( image, gc, 0, 0, width, height );
  108.   ISetForeground ( gc, black );
  109.   for ( loop = 0; loop < 256; loop++ ) {
  110.     if ( loop % 16 == 0 && loop ) {
  111.       y += cell_height;
  112.       x = 5;
  113.     } else if ( loop ) {
  114.       x += cell_width;
  115.     }
  116.     IDrawRectangle ( image, gc, x, y, (unsigned int)cell_width,
  117.       (unsigned int)cell_height );
  118.     if ( usehex )
  119.       sprintf ( temp, "%02X", loop );
  120.     else
  121.       sprintf ( temp, "%d", loop );
  122.     ISetFont ( gc, smallfont );
  123.     ret = ITextDimensions ( gc, smallfont, temp, strlen ( temp ), &w, &h );
  124.     subx = x + ( cell_width - w ) / 2;
  125.     suby = y + h + 2;
  126.     ISetForeground ( gc, navy );
  127.     IFillRectangle ( image, gc, x + 2, y + 2, cell_width - 3,
  128.       h );
  129.     ISetForeground ( gc, white );
  130.     IDrawString ( image, gc, subx, suby - 1, temp, strlen ( temp ) );
  131.     ISetFont ( gc, font );
  132.     ISetForeground ( gc, black );
  133.     sprintf ( temp, "%c", loop );
  134.     ret = ITextDimensions ( gc, font, temp, strlen ( temp ), &w, &h );
  135.     subx = x + ( cell_width - w ) / 2;
  136.     suby = y + cell_height - 6;
  137.     /* draw a baseline */
  138.     ISetForeground ( gc, grey );
  139.     IDrawLine ( image, gc, x + 1, suby, x + cell_width - 1, suby );
  140.     /* draw the letter */
  141.     ISetForeground ( gc, black );
  142.     IDrawString ( image, gc, subx, suby, temp, 1 );
  143.   }
  144.   /*
  145.   ** Write GIF output file.
  146.   */
  147.   IWriteImageFile ( stdout, image, format, IOPTION_INTERLACED );
  148.   IFreeImage ( image );
  149.   /* exit */
  150.   return ( 0 );
  151. }