/* GD font center Copyright (C) 2002 VVK (valera@sbnet.ru), CNII Center, Moscow This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ #include #include #if defined( __MSDOS__ ) || defined( _WIN32 ) || defined( __OS2__ ) #include #include #endif #include "gd.h" #include "gdfontmb.h" #include "gdmfont.h" /***************************************************************************/ /* */ /* Font image */ /* */ /***************************************************************************/ #define NUM_FONT gdFontMediumBold #define SPACE_SIZE 5 #define BACKGROUND_COLOR 0x888888l #define BASE_COLOR 0xeeeeeel #define CHAR_COLOR 0x305080l #define NUMB_COLOR 0xB0B0B0l #define MAX(a,b) (((a) > (b)) ? (a) : (b)) #define COLOR_RED(c) ((int) (((c) >> 16) & 0xff)) #define COLOR_GREEN(c) ((int) (((c) >> 8) & 0xff)) #define COLOR_BLUE(c) ((int) ((c) & 0xff)) #if defined( gdAlphaMax ) #define COLOR(c) \ gdTrueColor( COLOR_RED(c), COLOR_GREEN(c), COLOR_BLUE(c)) #define BGCOLOR(c) \ gdImageFilledRectangle( im, 0, 0, im->sx-1, im->sy-1, COLOR(c)) #else #define COLOR(c) \ gdImageColorResolve( im, COLOR_RED(c), COLOR_GREEN(c), COLOR_BLUE(c)) #define BGCOLOR(c) \ COLOR(c) #endif int gdMFontPrintImage( const char *fileName, const struct gd_mfont *mf, unsigned int flags) { FILE *stream; int i, j, cellWidth, cellHeight; gdImagePtr im; int bsColor, chColor, nmColor; /* Образуем изображение */ switch( flags & (mffRotateLeft | mffRotateRight) ) { case 0: default: cellWidth = MAX( NUM_FONT->w, mf->width); cellHeight = MAX( NUM_FONT->h, mf->height); break; case mffRotateLeft: case mffRotateRight: cellHeight = MAX( NUM_FONT->w, mf->width); cellWidth = MAX( NUM_FONT->h, mf->height); break; } #if defined( gdAlphaMax ) if( (im = gdImageCreateTrueColor( #else if( (im = gdImageCreate( #endif (cellWidth + SPACE_SIZE) * 16 + NUM_FONT->w + 2 * SPACE_SIZE, (cellHeight + SPACE_SIZE) * 16 + NUM_FONT->h + 2 * SPACE_SIZE)) == NULL ) return -1; BGCOLOR( BACKGROUND_COLOR ); bsColor = COLOR( BASE_COLOR ); chColor = COLOR( CHAR_COLOR ); nmColor = COLOR( NUMB_COLOR ); /* Заполним цифрами */ for( i = 0; i < 16; i++) { int n = (i < 10) ? (i + '0') : ('A' + i - 10); gdImageChar( im, NUM_FONT, 2 * SPACE_SIZE + NUM_FONT->w + (cellWidth + SPACE_SIZE) * i + (cellWidth - NUM_FONT->w) / 2, SPACE_SIZE, n, nmColor); gdImageChar( im, NUM_FONT, SPACE_SIZE, 2 * SPACE_SIZE + NUM_FONT->h + (cellHeight + SPACE_SIZE) * i + (cellHeight - NUM_FONT->h) / 2, n, nmColor); } /* Заполним символами */ for( j = 0; j < 16; j++) for( i = 0; i < 16; i++) { int c = j * 16 + i; int cWidth = gdMFontCharSize( mf, c); int x, y, xe, ye; if( cWidth <= 0 ) continue; switch( flags & (mffRotateLeft | mffRotateRight) ) { case 0: default: x = 2 * SPACE_SIZE + NUM_FONT->w + (SPACE_SIZE + cellWidth) * i + (cellWidth - cWidth) / 2; y = 2 * SPACE_SIZE + NUM_FONT->h + (SPACE_SIZE + cellHeight) * j + (cellHeight - mf->height) / 2; xe = x + cWidth - 1; ye = y + mf->height - 1; break; case mffRotateLeft: case mffRotateRight: x = 2 * SPACE_SIZE + NUM_FONT->w + (SPACE_SIZE + cellWidth) * i + (cellWidth - mf->height) / 2; y = 2 * SPACE_SIZE + NUM_FONT->h + (SPACE_SIZE + cellHeight) * j + (cellHeight - cWidth) / 2; xe = x + mf->height - 1; ye = y + cWidth - 1; break; } gdImageFilledRectangle( im, x, y, xe, ye, bsColor); switch( flags & (mffRotateLeft | mffRotateRight) ) { case 0: break; case mffRotateLeft: y = ye; break; case mffRotateRight: x = xe; break; default: y = ye; x = xe; break; } gdMFontChar( im, mf, x, y, c, chColor, flags); } /* Сохраним изображение */ if( fileName == NULL ) { #if defined( __MSDOS__ ) || defined( _WIN32 ) || defined( __OS2__ ) setmode( fileno( stdout ), O_BINARY); #endif stream = stdout; } else if( (stream = fopen( fileName, "wb")) == NULL ) { gdImageDestroy( im ); return -2; } gdImagePng( im, stream); gdImageDestroy( im ); if( stream != stdout ) fclose( stream ); return 0; } int gdMFontTextImage( const char *fileName, const struct gd_mfont *mf, const char *text, const unsigned char *table, unsigned int flags) { FILE *stream; int w, h, width, height; gdImagePtr im; int color; flags |= mffNewLine; /* Создадим изображение */ w = gdMFontStringSize( mf, text, &h, flags, table); switch( flags & (mffRotateLeft | mffRotateRight) ) { case 0: default: width = w; height = h; break; case mffRotateLeft: case mffRotateRight: width = h; height = w; break; } #if defined( gdAlphaMax ) if( (im = gdImageCreateTrueColor( #else if( (im = gdImageCreate( #endif width + 2 * SPACE_SIZE, height + 2 * SPACE_SIZE)) == NULL ) return -1; BGCOLOR( BASE_COLOR ); color = COLOR( CHAR_COLOR ); /* Выведем текст */ gdMFontString( im, mf, SPACE_SIZE, SPACE_SIZE, text, color, flags, table); /* Сохраним изображение */ if( fileName == NULL ) { #if defined( __MSDOS__ ) || defined( _WIN32 ) || defined( __OS2__ ) setmode( fileno( stdout ), O_BINARY); #endif stream = stdout; } else if( (stream = fopen( fileName, "wb")) == NULL ) { gdImageDestroy( im ); return -2; } gdImagePng( im, stream); gdImageDestroy( im ); if( stream != stdout ) fclose( stream ); return 0; } /***************************************************************************/ /* */ /* Font list */ /* */ /***************************************************************************/ int gdMFontPrintList( const struct gd_mfont_item *list, unsigned int flags) { const struct gd_mfont_item *cur; for( cur = list; cur->mf != NULL; cur++) gdMFontPrintImage( cur->fileName, cur->mf, flags); return 0; } int gdMFontTextList( const struct gd_mfont_item *list, const char *text, const unsigned char *table, unsigned int flags) { const struct gd_mfont_item *cur; for( cur = list; cur->mf != NULL; cur++) gdMFontTextImage( cur->fileName, cur->mf, text, table, flags); return 0; }