File: fontedit.C

package info (click to toggle)
sabre 0.2.4b-26
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 6,108 kB
  • ctags: 7,208
  • sloc: cpp: 36,842; ansic: 8,272; sh: 2,619; makefile: 213
file content (363 lines) | stat: -rw-r--r-- 8,495 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
// -*- mode: C; -*-
// jed does not understand .C

//
// Minefields:
// 
// This code is mine. You may use, modify or redistribute this code 
// in any APPROPRIATE manner you may choose. 
//  
// If you use, modify or redistribute this code, I am NOT responsible
// for ANY issue raised. 
// 
// ( If you modify or remove this note, this code is no longer mine. ) 
// 
// proff@iki.fi 
// 
//

// Mon Dec 22 20:43:18 EET 1997 (proff@alf.melmac)
// created file fontedit.C

// Current version:
// Last modified:

#include "fontedit.h"

// --
// -- main( options ) 
// --
// --

// standard vga mode
int screen_dimx=320, screen_dimy=200;
char *fontfn;
char *editfontpaths[]={
   "/usr/lib/kbd/consolefonts",
   "lib/fonts",
   "../lib/fonts",
   "fonts",
   "../fonts",
   "gdev/fonts",
   "../gdev/fonts",
   NULL
};

int main( int argc, char ** argv )
{
   fontpaths=editfontpaths;
   parseargs( argc, argv );
   edit( fontfn ); 
   G->close();
   fprintf( stderr, "\n" );
   return 0;
}

// --
// -- usage( ) 
// --
// --

void usage( void )
{
   fprintf( stderr, "usage: fontedit [options] <file>\n" 
	              );
   exit(1);
}

// --
// -- parseargs( args) 
// --

void parseargs( int argc, char **argv )
{
   int c;

   while( 1 ) {
      int option_index = 0;
      static struct option long_options[] =
      {
        {"help", 0, 0, '?'},
        {0, 0, 0, 0}
      };

      c = getopt_long( argc, argv, "?", long_options, &option_index );
      if( c == -1 )
	break;

      switch( c ) {
	 case '?':
	   usage();
          break;  
      } 
   }
   
   if( optind < argc ) {
      fontfn=argv[optind];
   } else
     usage();
}

fontdev *FONT=new fontdev;
fontdev *SYSFONT=new fontdev;

#define colormouse         23
#define colorborderdark    25
#define colorpixel         21 //33
#define colorbackground    27
#define colorborderbright  29

static int currentcell='A', previouscell='A';

static void drawborder( int x0, int y0, int x1, int y1, int invert=0 )
{
   int c1=invert?colorborderdark:colorborderbright,
       c2=invert?colorborderbright:colorborderdark;
   G->line(x0,y0,x1,y0,c1);
   G->line(x0,y0,x0,y1,c1);
   G->line(x0,y1,x1,y1,c2);
   G->line(x1,y0,x1,y1,c2);
}

static unsigned char buttlbl[]=
{24,27,26,25,'G',' ',' ','C','<','S','X','>'};

static int mx=0, my=0;
static int gridx,gridy;
static unsigned char mouseback[5*6];

static void hidemouse( void )
{
   unsigned char *dest=(unsigned char *)G->getvbuf( );
   unsigned char  *src=(unsigned char *)mouseback;
   dest+=(mx+my*G->getdimx());
   int i, j, dimx=SYSFONT->getdimx(), dimy=SYSFONT->getdimy();
   int dstep=(G->getdimx()-dimx);
   for( j=0; j<dimy; j++, dest+=dstep )
     for( i=0; i<dimx; i++ )
       *dest++=*src++;
   G->update(1,mx,my,mx+dimx,my+dimy);
}

static void savemouse( void )
{
   unsigned char  *src=(unsigned char *)G->getvbuf( );
   unsigned char *dest=(unsigned char *)mouseback;
   src+=(mx+my*G->getdimx());
   int i, j, dimx=SYSFONT->getdimx(), dimy=SYSFONT->getdimy();
   int dstep=(G->getdimx()-dimx);
   for( j=0; j<dimy; j++, src+=dstep )
     for( i=0; i<dimx; i++ )
       *dest++=*src++;
}

static void showmouse( void )
{
   gputc( SYSFONT, mx, my, colormouse, 128 );
   G->update(1,mx,my,mx+SYSFONT->getdimx(),my+SYSFONT->getdimy());
}

static void showcell( int ascii, int update=1 )
{
   int dx=10,dy=18, i=ascii%24, j=ascii/24;
   if( ascii == currentcell )	   
     G->rect( 1+i*dx, 1+j*dy, 8, 16, colorborderbright );
   else
     G->rect( 1+i*dx, 1+j*dy, 8, 16, colorborderdark );	   
   gputc( FONT, 1+i*dx, 1+j*dy, colorpixel, ascii );
   if( update )
     G->update(1,1+i*dx, 1+j*dy, 1+i*dx+8, 1+i*dy+16 );
}

static void showpixel( int x, int y, int update=1 )
{
   int dx=9,dy=9, x0=242, y0=0;
   unsigned char *src=(unsigned char *)FONT->getfbp(currentcell);
   src+=(y*8+x);
   G->rect( x0+x*dx+1, y0+y*dy+1, dx-1, dy-1, *src?colorpixel:colorborderdark );
   if( update )
   G->update(1,x0+x*dx+1, y0+y*dy+1, x0+(x+1)*dx, y0+(y+1)*dy );
}

static void showallpixels( int update=1 )
{
   int i, j;
   for( j=0; j<16; j++ ) {
      for( i=0; i<8; i++ ) {
	 showpixel( i, j, 0 );
      }
   }
   if( update )
     G->update();
}

static void showstatus( void )
{
   int dx=10,dy=18;
   G->rect( 3+16*dx, 3+10*dy+1, 8*dx-6, dy-6, colorbackground );
   gprintf( SYSFONT, 4+16*dx, 6+10*dy+1, colorpixel, "%3d(%02x) %dx%d", currentcell, currentcell, FONT->getmindimx(), FONT->getmindimy() );
   G->update(1,3+16*dx, 3+10*dy+1, 24*dx-3, 11*dy-3);
}

static void showall( void )
{
   int dx=10,dy=18;
   int x0=0,y0=0,x1=24*dx+1,y1=11*dy+1;
   // browse window
   G->rect(x0+1,y0+1,x1-x0,y1-y0, colorbackground );
   drawborder( x0, y0, x1, y1 );
   // title
   drawborder( 2+16*dx, 2+10*dy+1, 24*dx-2, 11*dy-2, 1 );
   showstatus();
   int i, j, ascii;
   for( ascii=0; ascii<256; ascii++ )
     showcell( ascii, 0 );
   dx=9,dy=9;
   x0=242,y0=0,x1=242+8*dx+1,y1=16*dy+1;
   // edit window
   G->rect(x0+1,y0+1,x1-x0-1,y1-y0-1, colorbackground );
   drawborder( x0, y0, x1, y1 );
   showallpixels(0);
   // buttons
   y0+=16*dy+2;
   for( j=0; j<3; j++ ) {
      for( i=0; i<4; i++ ) {
	 int X0=(int)(x0+i*18.7), Y0=(int)(y0+j*18);
	 int X1=(int)(x0+(i+1)*18.7)-1, Y1=(int)(y0+(j+1)*18)-1;
	 drawborder( X0, Y0, X1, Y1 );
	 G->rect( X0+1, Y0+1, X1-X0-1, Y1-Y0-1, colorbackground );
	 gputc( SYSFONT, X0+7, Y0+7, colorpixel, buttlbl[j*4+i] );
      }
   }
   savemouse();
   showmouse();
   G->update();
}

void edit( char *fn )
{
   if( SYSFONT->load("simple-5x6") )
     return;
   
   if( FONT->load(fn) )
     return;
   FONT->setcell( 8, 16 );

   assign_gdev_svgalib();
   assign_rgbdev8_svgalib();
   start_mouse_gdev_svgalib();
   if( G->open(screen_dimx,screen_dimy) ) {
      fprintf( stderr, "error opening gdev in %dx%dx8 resolution!\n", screen_dimx, screen_dimy );
      return;
   }

   showall();
   
   int looping=1;
   while( looping ) {
      gevent *ge=G->event();
      if( ge ) {
	 unsigned int type=ge->type;
	 if( (type&gevent::device_id) == gevent::device_serial_mouse ) {
	    gevent_data *gd=(gevent_data *)ge->data;
	    // mouse action
	    hidemouse();
	    mx=gd->x; my=gd->y;
	    savemouse();
	    if( (gd->flags&1) && (type&gevent::pressed)!=0) {
	       // button 1 activity
	       if( mx < 24*10 && (gd->flags&1) != 0 ) {
		  int ascii=(mx/10)+(my/18)*24;
		  if( ascii > 255 )
		    ascii =255;  
		  previouscell=currentcell;
		  currentcell=ascii;
		  showcell( previouscell );
		  showcell( currentcell );
		  showallpixels();
		  showstatus();
		  savemouse();
	       } else {
		  gridx=(mx-243)/9,gridy=my/9;
		  if( gridx > -1 && gridy > -1 && gridx < 8 && gridy < 16 ) {
		    // flip pixel
		     unsigned char *p=(unsigned char *)FONT->getfbp(currentcell);
		     p[gridy*8+gridx]^=0xff;
		     showcell(currentcell);
		     showpixel( gridx, gridy );
		     savemouse();
		  } else {
		     gridx=(int)((mx-243)/18.7); gridy=(my-146)/18;
		     switch( buttlbl[gridx+gridy*4] ) {
		      case 24:
			FONT->rotate( currentcell, fontdev::up );
			showallpixels();
			showcell( currentcell );
			break;
		      case 25:
			FONT->rotate( currentcell, fontdev::down );
			showallpixels();
			showcell( currentcell );
			break;
		      case 26:
			FONT->rotate( currentcell, fontdev::right );
			showallpixels();
			showcell( currentcell );
			break;
		      case 27:
			FONT->rotate( currentcell, fontdev::left );
			showallpixels();
			showcell( currentcell );
			break;
		      case 'S':
			FONT->save( "new-font" );
			FONT->setcell( 8, 16 );
			break;
		      case 'X':
			looping=0;
			break;
		      case '>':
			previouscell=currentcell;
			currentcell++;
			if( currentcell > 255 )
			  currentcell=0;
			showcell( previouscell );
			showcell( currentcell );
			showallpixels();
			showstatus();
			savemouse();
			break;
		      case '<':
			previouscell=currentcell;
			currentcell--;
			if( currentcell < 0 )
			  currentcell=255;
			showcell( previouscell );
			showcell( currentcell );
			showallpixels();
			showstatus();
			savemouse();
			break;
		      case 'G':
			FONT->calcmin();
			showstatus();
			break;
		      case 'C':
			memset( FONT->getfbp(currentcell), 0, 16*8 ); 
			showcell( currentcell );
			showallpixels();
			savemouse();
			break;
		     }
		  }
	       }
	    } // button #1 (left)
	    showmouse();
	 } else { // process mouseevents here
	    
	    // process keyevents here!
	 
	 } // process keyevents ends here
      } // got event ends here
   } // main loop ends here
}