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

图片显示

开发平台:

C/C++

  1. /*
  2.  * ITextDim.c
  3.  *
  4.  * Image library
  5.  *
  6.  * Description:
  7.  * Portable routines to manipulate raster images.
  8.  *
  9.  * History:
  10.  * 19-May-98 Craig Knudsen cknudsen@radix.net
  11.  * Added support for anti-aliasing.
  12.  * Added IGC arg to ITextDimensions, ITextWidth and
  13.  * ITextHeight.
  14.  * 17-May-98 Craig Knudsen cknudsen@radix.net
  15.  * Created
  16.  *
  17.  ****************************************************************************/
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <ctype.h>
  21. #include <memory.h>
  22. #include "Ilib.h"
  23. #include "IlibP.h"
  24. #include "IFontBDF.h"
  25. #define SPACES_PER_TAB 8
  26. IError ITextWidth ( gc, font, text, len, width_return )
  27. IGC gc;
  28. IFont font;
  29. char *text;
  30. unsigned int len;
  31. unsigned int *width_return;
  32. {
  33.   unsigned int height_return;
  34.   return ( ITextDimensions ( gc, font, text, len, width_return,
  35.     &height_return ) );
  36. }
  37. IError ITextHeight ( gc, font, text, len, height_return )
  38. IGC gc;
  39. IFont font;
  40. char *text;
  41. unsigned int len;
  42. unsigned int *height_return;
  43. {
  44.   unsigned int width_return;
  45.   return ( ITextDimensions ( gc, font, text, len, &width_return,
  46.     height_return ) );
  47. }
  48. IError ITextDimensions ( gc, font, text, len, width_return, height_return )
  49. IGC gc;
  50. IFont font;
  51. char *text;
  52. unsigned int len;
  53. unsigned int *width_return;
  54. unsigned int *height_return;
  55. {
  56.   IGCP *gcp = (IGCP *)gc;
  57.   IFontP *fontp = (IFontP *)font;
  58.   unsigned int *bitdata;
  59.   unsigned int height, width, actual_width, size, font_height;
  60.   int xoffset, yoffset, charx, chary;
  61.   char ch[256], *ptr;
  62.   int loop, loop2;
  63.   IError ret;
  64.   int char_num = 0;
  65.   int ret_width = 0, ret_height = 0;
  66.   if ( ! font )
  67.     return ( INoFontSet );
  68.   if ( fontp->magic != IMAGIC_FONT )
  69.     return ( IInvalidFont );
  70.   if ( ! gcp )
  71.     return ( IInvalidGC );
  72.   if ( gcp->magic != IMAGIC_GC )
  73.     return ( IInvalidGC );
  74.   charx = 0;
  75.   chary = 0;
  76.   IFontSize ( font, &font_height );
  77.   ret_height = font_height;
  78.   for ( ptr = text, loop = 0; loop < len; loop++, ptr++ ) {
  79.     if ( *ptr == '12' ) {
  80.       charx = 0;
  81.       chary += font_height;
  82.       ret_height += font_height;
  83.       char_num = 0;
  84.       continue;
  85.     }
  86.     else if ( *ptr == 't' ) {
  87.       ret = IFontBDFGetChar ( fontp->name, ch, &bitdata, &width, &height,
  88.         &actual_width, &size, &xoffset, &yoffset );
  89.       charx += ( 8 - ( char_num % 8 ) ) * actual_width;
  90.       if ( charx > ret_width )
  91.         ret_width = charx;
  92.       continue;
  93.     }
  94.     else if ( *ptr != '33' ) {
  95.       ch[0] = *ptr;
  96.       ch[1] = '';
  97.     }
  98.     else {
  99.       loop2 = 0;
  100.       ptr++;
  101.       while ( *ptr != ';' && loop < len && loop2 < 256 ) {
  102.         ch[loop2] = *ptr;
  103.         ptr++;
  104.         loop++;
  105.         loop2++;
  106.       }
  107.       ch[loop2] = '';
  108.       if ( *ptr != ';' ) {
  109.         return ( IInvalidEscapeSequence );
  110.       }
  111.     }
  112.     ret = IFontBDFGetChar ( fontp->name, ch, &bitdata, &width, &height,
  113.       &actual_width, &size, &xoffset, &yoffset );
  114.     if ( ! ret ) {
  115.       charx += actual_width;
  116.       char_num++;
  117.       if ( charx > ret_width )
  118.         ret_width = charx;
  119.     }
  120.   }
  121.   if ( gcp->antialiased ) {
  122.     ret_width /= 2;
  123.     ret_height /= 2;
  124.   }
  125.   *width_return = (unsigned int)ret_width;
  126.   *height_return = (unsigned int)ret_height;
  127.   return ( INoError );
  128. }