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