annotate Gui/skin/font.c @ 4785:fefd14e3072f

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