1693
|
1
|
|
2 #include <stdlib.h>
|
|
3 #include <stdio.h>
|
|
4 #include <stdarg.h>
|
|
5 #include <string.h>
|
|
6
|
|
7 #include "skin.h"
|
|
8 #include "font.h"
|
|
9 #include "cut.h"
|
|
10 #include "../error.h"
|
|
11
|
|
12 int items;
|
|
13
|
|
14 bmpFont * Fonts[25] = { NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL };
|
|
15
|
|
16 int fntAddNewFont( char * name )
|
|
17 {
|
|
18 int id;
|
|
19 for( id=0;id<25;id++ ) if ( !Fonts[id] ) break;
|
|
20 if ( ( Fonts[id]=malloc( sizeof( bmpFont ) ) ) == NULL ) return -1;
|
|
21 strcpy( Fonts[id]->name,name );
|
|
22 memset( Fonts[id]->Fnt,-1,256 * sizeof( fntChar ) );
|
|
23 return id;
|
|
24 }
|
|
25
|
1829
|
26 void fntFreeFont( void )
|
1693
|
27 {
|
1829
|
28 int i;
|
|
29 for( i=0;i<25;i++ )
|
|
30 {
|
|
31 if ( Fonts[i] )
|
|
32 {
|
|
33 if ( Fonts[i]->Bitmap.Image ) free( Fonts[i]->Bitmap.Image );
|
|
34 free( Fonts[i] );
|
|
35 Fonts[i]=NULL;
|
|
36 }
|
|
37 }
|
1693
|
38 }
|
|
39
|
|
40 int fntRead( char * path,char * fname,int id )
|
|
41 {
|
|
42 FILE * f;
|
|
43 unsigned char tmp[512];
|
|
44 unsigned char * ptmp;
|
|
45 unsigned char command[32];
|
|
46 unsigned char param[256];
|
|
47 int c,i;
|
|
48 int linenumber = 0;
|
|
49
|
|
50 strcpy( tmp,path ); strcat( tmp,fname ); strcat( tmp,".fnt" );
|
|
51 if ( ( f=fopen( tmp,"rt" ) ) == NULL ) return -1;
|
|
52 while ( !feof( f ) )
|
|
53 {
|
|
54 fgets( tmp,255,f ); linenumber++;
|
|
55
|
|
56 c=tmp[ strlen( tmp ) - 1 ]; if ( ( c == '\n' )||( c == '\r' ) ) tmp[ strlen( tmp ) - 1 ]=0;
|
|
57 c=tmp[ strlen( tmp ) - 1 ]; if ( ( c == '\n' )||( c == '\r' ) ) tmp[ strlen( tmp ) - 1 ]=0;
|
|
58 for ( c=0;c < strlen( tmp );c++ )
|
|
59 if ( tmp[c] == ';' )
|
|
60 {
|
|
61 tmp[c]=0;
|
|
62 break;
|
|
63 }
|
|
64 if ( strlen( tmp ) == 0 ) continue;
|
|
65 ptmp=strdelspacesbeforecommand( tmp );
|
|
66 if ( strlen( ptmp ) == 0 ) continue;
|
|
67 ptmp=strswap( ptmp,'\t',' ' );
|
|
68 ptmp=strdelspaces( ptmp );
|
|
69 cutItem( ptmp,command,'=',0 ); cutItem( ptmp,param,'=',1 );
|
|
70 if ( command[0] == '"' )
|
|
71 {
|
|
72 int i;
|
|
73 cutItem( command,command,'"',1 );
|
|
74 i=(int)command[0];
|
|
75 cutItem( param,tmp,',',0 ); Fonts[id]->Fnt[i].x=atoi( tmp );
|
|
76 cutItem( param,tmp,',',1 ); Fonts[id]->Fnt[i].y=atoi( tmp );
|
|
77 cutItem( param,tmp,',',2 ); Fonts[id]->Fnt[i].sx=atoi( tmp );
|
|
78 cutItem( param,tmp,',',3 ); Fonts[id]->Fnt[i].sy=atoi( tmp );
|
|
79 #ifdef DEBUG
|
1907
|
80 dbprintf( 3,"[font] char: '%s' params: %d,%d %dx%d\n",command,Fonts[id]->Fnt[i].x,Fonts[id]->Fnt[i].y,Fonts[id]->Fnt[i].sx,Fonts[id]->Fnt[i].sy );
|
1693
|
81 #endif
|
|
82 }
|
|
83 else
|
|
84 {
|
|
85 if ( !strcmp( command,"image" ) )
|
|
86 {
|
|
87 strcpy( tmp,path ); strcat( tmp,param );
|
|
88 #ifdef DEBUG
|
1907
|
89 dbprintf( 3,"[font] font imagefile: %s\n",tmp );
|
1693
|
90 #endif
|
|
91 if ( skinBPRead( tmp,&Fonts[id]->Bitmap ) ) return -2;
|
|
92 }
|
|
93 }
|
|
94 }
|
|
95 return 0;
|
|
96 }
|
|
97
|
|
98 int fntFindID( char * name )
|
|
99 {
|
|
100 int i;
|
|
101 for ( i=0;i < 25;i++ )
|
|
102 if ( Fonts[i] )
|
|
103 if ( !strcmp( name,Fonts[i]->name ) ) return i;
|
|
104 return -1;
|
|
105 }
|
|
106
|
|
107 int fntTextWidth( int id,char * str )
|
|
108 {
|
|
109 int size = 0;
|
|
110 int i;
|
|
111 if ( !Fonts[id] ) return 0;
|
|
112 for ( i=0;i < strlen( str );i++ )
|
|
113 if ( Fonts[id]->Fnt[ (int)str[i] ].sx != -1 ) size+=Fonts[id]->Fnt[ (int)str[i] ].sx;
|
|
114 return size;
|
|
115 }
|
|
116
|
|
117 int fntTextHeight( int id,char * str )
|
|
118 {
|
|
119 int max = 0,i;
|
|
120 if ( !Fonts[id] ) return 0;
|
|
121 for ( i=0;i < strlen( str );i++ )
|
|
122 if ( Fonts[id]->Fnt[ (int)str[i] ].sy > max ) max=Fonts[id]->Fnt[ (int)str[i] ].sy;
|
|
123 return max;
|
|
124 }
|
|
125
|
|
126 txSample * fntRender( int id,int px,int sx,char * fmt,... )
|
|
127 {
|
|
128 txSample * tmp = NULL;
|
|
129 char p[512];
|
|
130 va_list ap;
|
|
131 unsigned long * ibuf;
|
|
132 unsigned long * obuf;
|
|
133 int i,x,y;
|
|
134 int oy = 0, ox = 0, dx = 0;
|
|
135
|
|
136 va_start( ap,fmt );
|
|
137 vsnprintf( p,512,fmt,ap );
|
|
138 va_end( ap );
|
|
139
|
|
140 if ( ( !Fonts[id] )||
|
|
141 ( !strlen( p ) )||
|
|
142 ( !fntTextWidth( id,p ) )||
|
|
143 ( (tmp=malloc( sizeof( txSample ) )) == NULL ) ) return NULL;
|
|
144
|
|
145 tmp->Width=fntTextWidth( id,p );
|
|
146 tmp->Height=fntTextHeight( id,p );
|
|
147 tmp->BPP=32;
|
|
148 tmp->ImageSize=tmp->Width * tmp->Height * 4;
|
|
149 if ( ( tmp->Image=malloc( tmp->ImageSize ) ) == NULL ) return NULL;
|
|
150
|
|
151 obuf=(unsigned long *)tmp->Image;
|
|
152 ibuf=(unsigned long *)Fonts[id]->Bitmap.Image;
|
|
153 for ( i=0;i < strlen( p );i++ )
|
|
154 {
|
|
155 int c = (int)p[i];
|
|
156 if ( Fonts[id]->Fnt[c].x == -1 ) c=32;
|
|
157 for ( oy=0,y=Fonts[id]->Fnt[c].y;y < Fonts[id]->Fnt[c].y + Fonts[id]->Fnt[c].sy; y++,oy++ )
|
|
158 for ( ox=0,x=Fonts[id]->Fnt[c].x;x < Fonts[id]->Fnt[c].x + Fonts[id]->Fnt[c].sx; x++,ox++ )
|
|
159 {
|
|
160 obuf[ oy * tmp->Width + dx + ox ]=ibuf[ y * Fonts[id]->Bitmap.Width + x ];
|
|
161 }
|
|
162 dx+=Fonts[id]->Fnt[c].sx;
|
|
163 }
|
|
164
|
|
165 if ( ( sx > 0 )&&( sx < tmp->Width ) )
|
|
166 {
|
|
167 txSample tmp2;
|
|
168 tmp2.ImageSize=sx * tmp->Height * 4;
|
|
169 if ( ( tmp2.Image=malloc( tmp2.ImageSize ) ) == NULL ) { free( tmp->Image ); return NULL; }
|
|
170
|
|
171 obuf=(unsigned long *)tmp->Image;
|
|
172 ibuf=(unsigned long *)tmp2.Image;
|
|
173
|
|
174 for ( y=0;y < tmp->Height;y++ )
|
|
175 {
|
|
176 ox=px;
|
|
177 oy=y * sx; dx=y * tmp->Width;
|
|
178 for ( x=0;x < sx;x++ )
|
|
179 {
|
|
180 ibuf[oy++]=obuf[dx + ox++];
|
|
181 if ( ox >= tmp->Width ) ox=0;
|
|
182 }
|
|
183 }
|
|
184
|
|
185 free( tmp->Image ); tmp->Width=sx; tmp->ImageSize=tmp2.ImageSize; tmp->Image=tmp2.Image;
|
|
186 }
|
|
187
|
|
188 return tmp;
|
|
189 }
|