Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

newfont.cpp

Go to the documentation of this file.
00001 
00009 /*
00010     This file is part of Advanced Strategic Command; http://www.asc-hq.de
00011     Copyright (C) 1994-2005  Martin Bickel  and  Marc Schellenberger
00012 
00013     This program is free software; you can redistribute it and/or modify
00014     it under the terms of the GNU General Public License as published by
00015     the Free Software Foundation; either version 2 of the License, or
00016     (at your option) any later version.
00017 
00018     This program is distributed in the hope that it will be useful,
00019     but WITHOUT ANY WARRANTY; without even the implied warranty of
00020     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00021     GNU General Public License for more details.
00022 
00023     You should have received a copy of the GNU General Public License
00024     along with this program; see the file COPYING. If not, write to the 
00025     Free Software Foundation, Inc., 59 Temple Place, Suite 330, 
00026     Boston, MA  02111-1307  USA
00027 */
00028 
00029 #include <cstring>
00030 #include <stdio.h>
00031 #include <errno.h>
00032 #include <stdarg.h>
00033 #include <stdlib.h>
00034 #include <math.h>
00035 #include "global.h"
00036 #include "newfont.h"
00037 #include "stack.h"
00038 #include "basestrm.h"
00039 #include "misc.h"
00040 #include "basegfx.h"
00041 
00042 #define blockread(a,b,c) fread(b,1,c,a)
00043 #define seek(a,b) fseek(a,b,SEEK_SET)
00044 
00045 const char* fontid = "I\'m a font file on your Disk. Leave me alone !\x01A\x0A0";
00046 
00047 #define spacewidthkey (int('n'))
00048 
00049 typedef dacpalette256 dacpal;
00050 
00051 tfontsettings activefontsettings;
00052 
00053 void expand(void* p1, void* q1, int size)
00054 {
00055    char* src = (char*) p1;
00056    char* dst = (char*) q1;
00057 
00058    dst[0] = src[0];
00059    dst[1] = src[1];
00060 
00061    src+=2;
00062    dst+=2;
00063 
00064    while ( size ) {
00065       char al = *(src++);
00066       for ( int i = 0; i < 8; i++ )
00067          if ( size ) {
00068             if ( al & (1 << i ))
00069                dst[i] = 1;
00070             else
00071                dst[i] = 0;
00072    
00073             size--;
00074          }
00075       dst += 8;
00076    }
00077 }
00078 
00079 
00080 void toldfont::read ( tnstream& stream )
00081 {
00082    stream.readdata ( &id, sizeof ( id ));
00083    stream.readdata ( &name, sizeof ( name ));
00084    number = stream.readChar();
00085    color = stream.readChar();
00086    for ( int i = 0; i < 256; ++i ) {
00087       character[i].width = stream.readChar();
00088       character[i].size = stream.readWord();
00089       character[i].diskposition = stream.readInt();
00090       character[i].memposition = (char*) stream.readInt();
00091       character[i].dummy = stream.readChar();
00092    }
00093    height = stream.readWord();
00094    for ( int i = 0; i < 256; ++i )
00095       kernchartable[i] = stream.readChar();
00096    for ( int i = 0; i < 101; ++i )
00097       for ( int j = 0; j < 101; ++j )
00098          kerning[i][j] = stream.readChar();
00099 
00100    dummy = stream.readWord();
00101    useems = stream.readChar();
00102    caps = stream.readChar();
00103    palette = (dacpalette256*) stream.readInt();
00104    groundline = stream.readChar();
00105 }
00106 
00107 pfont        loadfont( pnstream stream )
00108 {
00109    toldfont     *font1;
00110    char *p;
00111    void         *q;
00112    int      ll, ll2;
00113    int i;
00114 
00115    font1 = new toldfont;
00116    font1->read ( *stream );
00117 
00118    font1->id[47] = 0;
00119 
00120    if ( strncmp(font1->id,fontid, 46) )
00121       return (NULL);
00122 
00123    font1->useems = false;
00124    font1->palette = NULL;
00125 
00126    for (i = 0; i <= 255; i++) {
00127       if ( font1->character[i].size ) {
00128          ll2 = ( font1->character[i].size / 8 + 1) * 8;
00129          q = new char [ ll2+2 ];
00130          font1->character[i].memposition = (char*) q;
00131 
00132          if (font1->color == false) {
00133             ll = font1->character[i].size / 8 + 1;
00134             p = new char [ ll + 2 ];
00135 
00136             *((Uint16*)p) = stream->readWord();
00137 
00138             stream->readdata ( p +2, ll );
00139             expand( p, q, font1->character[i].size );
00140             delete[] p;
00141          } else {
00142             *((Uint16*)q) = stream->readWord();
00143             stream->readdata ( ((char*)q)+2, font1->character[i].size  );
00144          }
00145 
00146          Uint16* pg = (Uint16*) q;
00147          if (*pg != font1->character[i].size) {
00148             printf("invalid size of character %c\n",i);
00149          } /* endif */
00150          *pg = font1->character[i].width;
00151       } else {
00152         if (i == 32) {
00153            ll = font1->height * ( font1->character[spacewidthkey].width - 2 );
00154            p = new char [ ll+2 ];
00155            Uint16* pg = (Uint16*) p;
00156            memset(p,0,ll+2);
00157            *pg = font1->character[spacewidthkey].width - 2;
00158            font1->character[i].width = font1->character[spacewidthkey].width - 2;
00159            font1->character[i].size = ll;
00160            font1->character[i].memposition = (char*) p;
00161         } /* endif */
00162      }
00163    }
00164 
00165 
00166   if (font1->color) {
00167      if ( font1->palette ) {
00168         font1->palette = (dacpalette256*) new char [ sizeof ( dacpalette256 ) ];
00169         stream->readdata ( font1->palette, sizeof(dacpalette256));
00170      }
00171    } else
00172       font1->palette = NULL;
00173 
00174 
00175 
00176    pfont font2   = new ( tfont );
00177    font2->name   = strdup ( font1->name );
00178    font2->color  = font1->color;
00179    font2->caps   = font1->caps ;
00180    font2->height = font1->height;
00181    font2->groundline = font1->groundline;
00182    font2->palette    = font1->palette;
00183    for (i = 0; i < 256 ;i++ ) {
00184       font2->character[i].width     =  font1->character[i].width;    
00185       font2->character[i].size      =  font1->character[i].size;
00186       font2->character[i].memposition =font1->character[i].memposition;
00187       for (int j = 0; j < 256 ; j++ ) {
00188           font2->kerning[j][i] = font1->kerning[int(font1->kernchartable[i])] [int(font1->kernchartable[j])];
00189           if ( font2->kerning[j][i] > 10  ||  font2->kerning[j][i] < - 10 )
00190              printf("%c%c\n",i,j);
00191       }
00192 
00193 
00194    } /* endfor */
00195 
00196 
00197    delete font1 ;
00198 
00199    return font2;
00200 }
00201 
00202 
00203 
00204 void shrinkfont ( pfont font, int diff )
00205 {
00206    if ( font ) 
00207       for (int i = 0; i < 256 ;i++ ) 
00208          if (font->character[i].size ) 
00209             for (int j = 0; j < 256 ; j++ ) 
00210                if (font->character[j].size ) 
00211                   font->kerning[i][j] += diff;
00212 
00213 }
00214 
00215 int gettextwdth ( const char* txt, pfont font )
00216 {
00217    int i, result;
00218 
00219    result = 0;
00220    if (font == NULL)
00221       font = activefontsettings.font;
00222    if (font == NULL)
00223       return 0;
00224 
00225    i=0;
00226    while (txt[i] != 0) {
00227       if (font->character[int(txt[i])].size) {
00228          result +=2 + font->character[int(txt[i])].width;
00229          if ( txt[i+1] )                          //          if ((txt[i+1] != 0) && (txt[i+1] != ' ')) 
00230             result += font->kerning[int(txt[i+1])][int(txt[i])];
00231       } /* endif */
00232       i++;
00233    } /* endwhile */
00234    if (result > 2) {
00235       result -= 2;
00236    } else {
00237       result = 0;
00238    } /* endif */
00239 
00240    return result;
00241 }
00242 
00243 
00244 void         showtext3( const char *       txt,
00245                        int         xpos,
00246                        int         ypos)
00247 {
00248    // collategraphicoperations cgo ( xpos, ypos, max ( activefontsettings.height, activefontsettings->font.height ),;
00249 
00250     char         *s, *s2, *s3;
00251     int         i, j,k;
00252     int         newx = 0;
00253     signed char  cnt;
00254     int         orgjus; 
00255 
00256 
00257    if (txt[0] == 0)
00258        return;
00259    npush( activefontsettings );
00260    s2 = new char [ 255 ];
00261    s  = new char [ 255 ];
00262    s3 = new char [ 255 ];
00263    s2[0] = 0;
00264 
00265    i = 0;
00266    cnt = 0; 
00267    do { 
00268       if (txt[i] != '~') {
00269          s2[cnt] = txt[i];
00270          cnt++;
00271          s2[cnt] = 0;
00272       } /* endif */
00273       i++;
00274    }  while ( txt[i] != 0 );
00275 
00276    cnt = -2;
00277    showtext2(s2,xpos,ypos); 
00278    i = 0;
00279    orgjus = activefontsettings.justify; 
00280 
00281    while ((txt[i] != '~') && (txt[i] != 0)) {
00282       i++;
00283    } /* endwhile */
00284    
00285    while (txt[i] != 0) {
00286       cnt+=2; 
00287 
00288       j = i + 1; 
00289       s[0] = 0;
00290       s3[0] = 0;
00291       k=0;
00292       activefontsettings.background = 255;
00293 
00294       while ((txt[j] != '~') && ( txt[j] != 0)) {
00295          s[k] = txt[j];
00296          k++;
00297          s[k] = 0;
00298          j++;
00299       } 
00300 
00301       if (s[0] != 0) {
00302          strncpy( s3, s2, k+i-cnt );
00303          s3[k+i-cnt]=0;
00304          if (orgjus == lefttext) { 
00305             newx = xpos + gettextwdth(s3, NULL) - gettextwdth(s,NULL);
00306          } 
00307          else 
00308             if (orgjus == centertext) { 
00309                newx = xpos + (activefontsettings.length - gettextwdth(s2, NULL)) / 2 + gettextwdth(s3,NULL) - gettextwdth(s,NULL) + 1;
00310             } 
00311             else 
00312                if (orgjus == righttext) { 
00313                   newx = xpos + 1 + (activefontsettings.length - gettextwdth(s2, NULL)) + gettextwdth(s3,NULL) - gettextwdth(s,NULL);
00314                } 
00315          activefontsettings.color = activefontsettings.markcolor; 
00316          activefontsettings.justify = lefttext; 
00317          showtext2(s,newx,ypos); 
00318       } 
00319       i = j + 1; 
00320       while ((txt[i] != '~') && (txt[i] != 0))
00321         i++;
00322    } 
00323    delete[] s3;
00324    delete[] s;
00325    delete[] s2;
00326 
00327    npop( activefontsettings );
00328 } 
00329 
00330 
00331 
00332 
00333 
00334 
00335 void         showtext3c( const char *       txt,
00336                        int         xpos,
00337                        int         ypos)
00338 {
00339    // collategraphicoperations cgo;
00340 
00341     int         i, j,k;
00342     int         newx = 0;
00343     int          cnt;
00344     int         orgjus; 
00345 
00346 
00347    npush( activefontsettings );
00348    char s2[1000];
00349    char s[1000];
00350    char s3[1000];
00351    s2[0] = 0;
00352 
00353    i = 0;
00354    cnt = 0; 
00355    while ( txt[i] ) { 
00356       if (txt[i] != '~') {
00357          s2[cnt] = txt[i];
00358          cnt++;
00359          s2[cnt] = 0;
00360       } /* endif */
00361       i++;
00362    } 
00363 
00364    cnt = -2;
00365    showtext2c(s2,xpos,ypos); 
00366    i = 0;
00367    orgjus = activefontsettings.justify; 
00368 
00369    while ((txt[i] != '~') && (txt[i] )) 
00370       i++;
00371    
00372    while ( txt[i] ) {
00373       cnt+=2; 
00374 
00375       j = i + 1; 
00376       s[0] = 0;
00377       s3[0] = 0;
00378       k=0;
00379       activefontsettings.background = 255;
00380 
00381       while ((txt[j] != '~') && ( txt[j] != 0)) {
00382          s[k] = txt[j];
00383          k++;
00384          s[k] = 0;
00385          j++;
00386       } 
00387 
00388       if (s[0] != 0) {
00389          strncpy( s3, s2, k+i-cnt );
00390          s3[k+i-cnt]=0;
00391          if (orgjus == lefttext) { 
00392             newx = xpos + gettextwdth(s3, NULL) - gettextwdth(s,NULL);
00393          } 
00394          else 
00395             if (orgjus == centertext) { 
00396                newx = xpos + (activefontsettings.length - gettextwdth(s2, NULL)) / 2 + gettextwdth(s3,NULL) - gettextwdth(s,NULL) + 1;
00397             } 
00398             else 
00399                if (orgjus == righttext) { 
00400                   newx = xpos + 1 + (activefontsettings.length - gettextwdth(s2, NULL)) + gettextwdth(s3,NULL) - gettextwdth(s,NULL);
00401                } 
00402          activefontsettings.font = activefontsettings.markfont; 
00403          activefontsettings.justify = lefttext; 
00404          showtext2c(s,newx,ypos); 
00405       } 
00406       i = j + 1;
00407       while ((txt[i] != '~') && (txt[i] != 0))
00408         i++;
00409    }
00410 
00411    npop( activefontsettings );
00412 } 
00413 
00414 void showtext4 ( const char* TextToOutput, int x1, int y1, ... )
00415 {
00416    va_list paramlist;
00417    va_start ( paramlist, y1 );
00418 
00419    char tempbuf[1000];
00420 
00421    vsprintf( tempbuf, TextToOutput, paramlist );
00422    va_end ( paramlist );
00423    showtext2 ( tempbuf, x1, y1 );
00424 }
00425 
00426 void showtext4c ( const char* TextToOutput, int x1, int y1, ... )
00427 {
00428    va_list paramlist;
00429    va_start ( paramlist, y1 );
00430 
00431    char tempbuf[1000];
00432 
00433    vsprintf( tempbuf, TextToOutput, paramlist );
00434    va_end ( paramlist );
00435    showtext2c ( tempbuf, x1, y1 );
00436 }
00437 
00438 class InitNewfont {
00439          public:
00440            InitNewfont ( void )
00441            {
00442               memset( &activefontsettings, 0, sizeof ( activefontsettings ));
00443            };
00444       } initnewfont;
00445 
00446 char* int2string ( int i, char* buf )
00447 {
00448    if ( i >= 0 ) {
00449       itoa ( i, buf, 10 );
00450    } else {
00451       buf[0] = '-';
00452       i = -i;
00453       itoa ( i, &buf[1], 10 );
00454    }
00455    if ( gettextwdth ( buf, NULL ) > activefontsettings.length   &&  activefontsettings.length) {
00456       char buf2[50];
00457 
00458       int pot  = (int) log10 ( double(i) );
00459       int base = (int) pow ( float(10), float(pot) );
00460       int first = i / base;
00461       int rest = i - first * base;
00462 
00463       buf2[0] = digit[ first ][0];
00464       int p = 1;
00465       buf2[p] = 'E';
00466       buf2[p+1] = 0;
00467       strcat ( buf2, strrr ( pot ));
00468 
00469       while ( gettextwdth ( buf2, NULL ) < activefontsettings.length) {
00470          strcpy ( buf, buf2 );
00471          base /= 10;
00472          if ( p == 1 )
00473             buf2[p++] = '.';
00474          buf2[p++] = digit[ rest /  base ][0];
00475          rest = rest % base;
00476 
00477          buf2[p] = 'E';
00478          buf2[p+1] = 0;
00479          strcat ( buf2, strrr ( pot ));
00480       } /* endwhile */
00481    }
00482    return buf;
00483 
00484 }

Generated on Tue Jun 24 01:27:46 2008 for Advanced Strategic Command by  doxygen 1.4.2