]> icculus.org git repositories - taylor/freespace2.git/blob - src/fonttool/fontkern.cpp
update toolset for newer gr_init() calls
[taylor/freespace2.git] / src / fonttool / fontkern.cpp
1 /*
2  * Copyright (C) Volition, Inc. 1999.  All rights reserved.
3  *
4  * All source code herein is the property of Volition, Inc. You may not sell 
5  * or otherwise commercially exploit the source or things you created based on
6  * the source.
7  */
8
9 /*
10  * $Logfile: /Freespace2/code/Fonttool/FontKern.cpp $
11  * $Revision$
12  * $Date$
13  * $Author$
14  *
15  * Tool for interactively kerning fonts
16  *
17  * $Log$
18  * Revision 1.4  2006/04/26 19:38:36  taylor
19  * fix some minor fonttool compile errors
20  *
21  * Revision 1.3  2003/01/30 20:03:48  relnev
22  * various files ported needed for fonttool.  There is a bug where on exit it segfaults in SDL_GL_SwapBuffers, I'm probably missing something (don't know what) but it works fine otherwise (Taylor Richards)
23  *
24  * Revision 1.2  2002/06/09 04:41:16  relnev
25  * added copyright header
26  *
27  * Revision 1.1.1.1  2002/05/03 03:28:08  root
28  * Initial import.
29  *
30  * 
31  * 6     5/19/99 4:07p Dave
32  * Moved versioning code into a nice isolated common place. Fixed up
33  * updating code on the pxo screen. Fixed several stub problems.
34  * 
35  * 5     12/18/98 1:14a Dave
36  * Rough 1024x768 support for Direct3D. Proper detection and usage through
37  * the launcher.
38  * 
39  * 4     12/02/98 9:58a Dave
40  * Got fonttool working under glide/direct3d.
41  * 
42  * 3     11/30/98 1:09p Dave
43  * 
44  * 2     10/24/98 5:15p Dave
45  * 
46  * 1     10/24/98 4:58p Dave
47  * 
48  * 14    5/06/98 5:30p John
49  * Removed unused cfilearchiver.  Removed/replaced some unused/little used
50  * graphics functions, namely gradient_h and _v and pixel_sp.   Put in new
51  * DirectX header files and libs that fixed the Direct3D alpha blending
52  * problems.
53  * 
54  * 13    4/13/98 10:11a John
55  * Made timer functions thread safe.  Made timer_init be called in all
56  * projects.
57  * 
58  * 12    3/10/98 4:18p John
59  * Cleaned up graphics lib.  Took out most unused gr functions.   Made D3D
60  * & Glide have popups and print screen.  Took out all >8bpp software
61  * support.  Made Fred zbuffer.  Made zbuffer allocate dynamically to
62  * support Fred.  Made zbuffering key off of functions rather than one
63  * global variable.
64  * 
65  * 11    3/05/98 11:15p Hoffoss
66  * Changed non-game key checking to use game_check_key() instead of
67  * game_poll().
68  * 
69  * 10    10/30/97 4:56p John
70  * Fixed up font stuff to build.  Fixed bug where it didn't show the last
71  * 3 characters in kerning table.
72  * 
73  * 9     9/03/97 4:32p John
74  * changed bmpman to only accept ani and pcx's.  made passing .pcx or .ani
75  * to bm_load functions not needed.   Made bmpman keep track of palettes
76  * for bitmaps not mapped into game palettes.
77  * 
78  * 8     6/06/97 6:47p John
79  * Fixed bug
80  * 
81  * 7     6/06/97 4:41p John
82  * Fixed alpha colors to be smoothly integrated into gr_set_color_fast
83  * code.
84  * 
85  * 6     6/06/97 11:10a John
86  * made scrolling kern pair box.
87  * 
88  * 5     6/06/97 9:21a John
89  * added some kerning pairs
90  * 
91  * 4     6/06/97 9:18a John
92  * Added capital hamburger.    
93  * 
94  * 3     6/05/97 5:00p John
95  * used fonttool.pcx
96  * 
97  * 2     6/05/97 4:53p John
98  * First rev of new antialiased font stuff.
99  * 
100  * 1     6/02/97 4:04p John
101  *
102  * $NoKeywords: $
103  */
104
105 #ifndef PLAT_UNIX
106 #include <windows.h>
107 #endif
108 #include <stdlib.h>
109 #include <stdio.h>
110 #include <time.h>
111 #ifndef PLAT_UNIX
112 #include <io.h>
113 #include <conio.h>
114 #endif
115
116 #include "pstypes.h"
117 #include "osapi.h"
118 #include "cfile.h"
119 #include "2d.h"
120 #include "key.h"
121 #include "mouse.h"
122 #include "palman.h"
123 #include "timer.h"
124 #include "bmpman.h"
125 #include "osregistry.h"
126 #include "cmdline.h"
127
128 #include "fonttool.h"
129
130 const char *SampleText = "This is some sample text that is here to\n" \
131 "Show you how the antialiasing will\n"  \
132 "look over different color backgrounds\n" \
133 "KERN PAIRS: VaWaVeWeVAV-LyT.T,TyTvTcYe\n";
134
135 static void myexit(int value)
136 {
137 #ifdef WIN32
138         getch();
139 #endif
140         exit(value);
141 }
142
143 int game_check_key()
144 {
145         return key_inkey();
146 }
147
148 int game_poll()
149 {
150         return key_inkey();
151 }
152
153 int os_done = 0;
154
155 void os_close()
156 {
157         os_done = 1;
158 }
159
160 int fonttool_get_kerning( font *fnt, int c1, int c2, int *pairnum )
161 {
162         int i;
163
164         int l1 = c1 - fnt->first_ascii;
165         int l2 = c2 - fnt->first_ascii;
166
167         for (i=0; i<fnt->num_kern_pairs; i++ )  {
168                 if ( (fnt->kern_data[i].c1 == l1) && (fnt->kern_data[i].c2 == l2) )     {
169                         if (pairnum) *pairnum = i;
170                         return fnt->kern_data[i].offset;
171                 }
172         }
173         return 0;
174 }
175
176 void fonttool_resync_kerning( font *fnt )
177 {
178         int i;
179
180         // update all the font into
181         for (i=0; i<fnt->num_chars; i++ )       {
182                 fnt->char_data[i].kerning_entry = -1;
183         }
184
185         for (i=0; i<fnt->num_kern_pairs; i++ )  {
186                 int c = fnt->kern_data[i].c1;
187                 if ( fnt->char_data[c].kerning_entry == -1 )
188                         fnt->char_data[c].kerning_entry = (short)i;
189         }
190 }
191
192 void fonttool_remove_kern_pair( font *fnt, int index )
193 {
194         // not found, add it
195         int i, n, new_num_pairs;
196
197         new_num_pairs = fnt->num_kern_pairs - 1;
198
199         if ( new_num_pairs < 1 )        {
200                 fonttool_remove_kerning(fnt);
201                 return;
202         }
203
204         font_kernpair *new_kern_data = (font_kernpair *)malloc( new_num_pairs*sizeof(font_kernpair) );
205         if (!new_kern_data)     {
206                 printf( "Out of memory!\n" );
207                 myexit(1);
208         }
209
210
211         n=0;
212         for (i=0; i<fnt->num_kern_pairs; i++ )  {
213                 if ( i != index )       {
214                         new_kern_data[n] = fnt->kern_data[i];
215                         n++;
216                 }
217         }
218
219         if ( fnt->kern_data ) free( fnt->kern_data );
220         fnt->kern_data = new_kern_data;
221         fnt->kern_data_size = sizeof(font_kernpair)*new_num_pairs;
222         fnt->num_kern_pairs = new_num_pairs;
223
224         fonttool_resync_kerning(fnt);
225
226         mprintf(( "Font has %d kern pairs\n", fnt->num_kern_pairs ));
227 }
228
229 void fonttool_set_kerning( font *fnt, int c1, int c2, int dist )
230 {
231         int i;
232
233         int l1 = c1 - fnt->first_ascii;
234         int l2 = c2 - fnt->first_ascii;
235
236         for (i=0; i<fnt->num_kern_pairs; i++ )  {
237                 if ( (fnt->kern_data[i].c1 == l1) && (fnt->kern_data[i].c2 == l2) )     {
238                         fnt->kern_data[i].offset = (signed char)dist;
239                         if ( dist == 0 )        {
240                                 fonttool_remove_kern_pair( fnt, i );                            
241                         }
242                         return;
243                 }
244         }
245         if ( dist == 0 )        return;
246
247         // not found, add it
248         int new_num_pairs;
249
250         new_num_pairs = fnt->num_kern_pairs+1;
251
252         font_kernpair *new_kern_data = (font_kernpair *)malloc( new_num_pairs*sizeof(font_kernpair) );
253         if (!new_kern_data)     {
254                 printf( "Out of memory!\n" );
255                 myexit(1);
256         }
257
258
259         int n=0;
260         uint newcode = l1*256+l2;
261
262         for (i=0; i<fnt->num_kern_pairs; i++ )  {
263                 uint code = fnt->kern_data[i].c1*256 + fnt->kern_data[i].c2;
264                 if ( code < newcode )   {
265                         new_kern_data[n] = fnt->kern_data[i];
266                         n++;
267                 } else {
268                         break;
269                 }
270         }
271
272
273         new_kern_data[n].c1 = (char)l1;
274         new_kern_data[n].c2 = (char)l2;
275         new_kern_data[n].offset = (signed char)dist;
276         n++;
277
278         
279         for (; i<fnt->num_kern_pairs; i++ )     {
280                 new_kern_data[n] = fnt->kern_data[i];
281                 n++;
282         }
283
284
285         if ( fnt->kern_data ) free( fnt->kern_data );
286         fnt->kern_data = new_kern_data;
287         fnt->kern_data_size +=  sizeof(font_kernpair);
288         fnt->num_kern_pairs++;
289
290         fonttool_resync_kerning(fnt);
291
292         mprintf(( "Font has %d kern pairs\n", fnt->num_kern_pairs ));
293
294 }
295
296
297 void fonttool_remove_kerning( font *fnt )
298 {
299         int i;
300
301         for (i=0; i<fnt->num_chars; i++ )       {
302                 fnt->char_data[i].kerning_entry = -1;
303         }
304
305         fnt->kern_data_size = 0;
306         if (fnt->kern_data)
307                 free( fnt->kern_data );
308         fnt->kern_data = NULL;
309         fnt->num_kern_pairs = 0;
310 }
311
312
313 void fonttool_edit_kerning(char *fname1)
314 {
315         int i, k,x;
316         int done;
317         int bkg;
318         font KernFont, tmpfont;
319         int alpha = 16;
320         int cr=16, cb=16, cg=16;
321         int c1 = 'b', c2 = 'u';
322         char kerntext[128];
323         int current_pair = -1;
324         int first_item = 0;
325         int current_item = 0;
326         int num_items_displayed = 1;
327         int last_good_pair = -1;
328         color ac;
329         
330         printf( "Editing kerning data for %s\n", fname1 );
331         fonttool_read( fname1, &KernFont );
332
333         timer_init();
334
335         // setup the fred exe directory so CFILE can init properly
336         //char *c = GetCommandLine();
337         //SDL_assert(c != NULL);
338         //char *tok = strtok(c, " ");
339         //SDL_assert(tok != NULL);      
340
341         cfile_init();
342
343         os_init( "FontTool", "FontTool - Kerning Table Editor" );
344
345         // always run this thing in a window
346         Cmdline_fullscreen = 0;
347         Cmdline_window = 1;
348
349         gr_init();
350
351         gr_set_palette("none",NULL);
352
353         char fonttool_pcx[128];
354         sprintf(fonttool_pcx, "src%sfonttool%sfonttool", DIR_SEPARATOR_STR, DIR_SEPARATOR_STR);
355
356         bkg = bm_load( fonttool_pcx );
357
358         if ( bkg < 0 )  {
359                 printf("Error loading FontTool\n" );
360                 myexit(1);
361         }
362         palette_use_bm_palette(bkg);
363
364         key_init();
365         mouse_init();
366
367         gr_init_alphacolor( &ac, cr*16,cg*16,cb*16,alpha*16,AC_TYPE_HUD );
368         
369
370         {
371                 extern font *Current_font;
372                 Current_font = &KernFont;
373         }
374
375         done = 0;
376         while (!done)   {
377                 
378                 os_poll();
379                 k = key_inkey();
380                 switch(k)       {               
381                 case SDLK_F3:
382                         gr_toggle_fullscreen();
383                         break;
384
385                 case SDLK_F5:
386                         fonttool_read( fname1, &tmpfont );
387                         fonttool_copy_kern( &tmpfont, &KernFont );
388                         break;
389
390                 case SDLK_F6:
391                         fonttool_remove_kerning( &KernFont );
392                         break;
393
394                 case SDLK_F10:
395                         fonttool_dump( fname1, &KernFont );
396                         done=1;
397                         break;
398
399                 case SDLK_COMMA:
400                         if ( alpha > 1 )        {
401                                 alpha--;
402                                 gr_init_alphacolor(&ac,cr*16,cg*16,cb*16,alpha*16,AC_TYPE_HUD);
403                         }
404                         break;
405
406                 case SDLK_PERIOD:
407                         if ( alpha < 17 )       {
408                                 alpha++;
409                                 gr_init_alphacolor(&ac,cr*16,cg*16,cb*16,alpha*16,AC_TYPE_HUD);
410                         }
411                         break;
412
413                 case SDLK_r:
414                         if ( cr == 16 ) cr = 1; else cr = 16;
415                         gr_init_alphacolor(&ac,cr*16,cg*16,cb*16,alpha*16,AC_TYPE_HUD);
416                         break;
417
418                 case SDLK_g:
419                         if ( cg == 16 ) cg = 1; else cg = 16;
420                         gr_init_alphacolor(&ac,cr*16,cg*16,cb*16,alpha*16,AC_TYPE_HUD);
421                         break;
422
423                 case SDLK_b:
424                         if ( cb == 16 ) cb = 1; else cb = 16;
425                         gr_init_alphacolor(&ac,cr*16,cg*16,cb*16,alpha*16,AC_TYPE_HUD);
426                         break;
427
428                 case SDLK_KP_6:
429                         x = fonttool_get_kerning( &KernFont, c1, c2, NULL );
430                         fonttool_set_kerning( &KernFont, c1, c2, x+1 );
431                         break;
432
433                 case SDLK_KP_4:
434                         x = fonttool_get_kerning( &KernFont, c1, c2, NULL );
435                         fonttool_set_kerning( &KernFont, c1, c2, x-1 );
436                         break;
437
438                 case SDLK_KP_5:
439                         fonttool_set_kerning( &KernFont, c1, c2, 0 );
440                         break;
441
442                 case SDLK_KP_7:
443                         if ( c1 < KernFont.first_ascii + KernFont.num_chars-1 ) c1++;
444                         break;
445
446                 case SDLK_KP_1:
447                         if ( c1 > KernFont.first_ascii ) c1--;
448                         break;
449
450                 case SDLK_KP_9:
451                         if ( c2 < KernFont.first_ascii + KernFont.num_chars-1 ) c2++;
452                         mprintf(( "C2 = %d\n", c2 ));
453                         break;
454
455                 case SDLK_KP_3:
456                         if ( c2 > KernFont.first_ascii ) c2--;
457                         mprintf(( "C2 = %d\n", c2 ));
458                         break;
459
460                 case SDLK_KP_2:
461                         if ( current_pair < 0 ) 
462                                 current_pair = last_good_pair;
463                         else 
464                                 current_pair++;
465                         if ( current_pair >= KernFont.num_kern_pairs )  {
466                                 current_pair = KernFont.num_kern_pairs-1;
467                         }
468                         if ( (current_pair < KernFont.num_kern_pairs) && (current_pair > -1) )  {
469                                 c1 = KernFont.kern_data[current_pair].c1 + KernFont.first_ascii;
470                                 c2 = KernFont.kern_data[current_pair].c2 + KernFont.first_ascii;
471                         }
472                         break;
473
474                 case SDLK_KP_8:
475                         if ( current_pair < 0 ) 
476                                 current_pair = last_good_pair;
477                         else
478                                 current_pair--;
479                         if ( current_pair < 0 ) {
480                                 current_pair = 0;
481                         }
482                         if ( (current_pair < KernFont.num_kern_pairs) && (current_pair > -1) )  {
483                                 c1 = KernFont.kern_data[current_pair].c1 + KernFont.first_ascii;
484                                 c2 = KernFont.kern_data[current_pair].c2 + KernFont.first_ascii;
485                         }
486                         break;
487
488                 case SDLK_ESCAPE:
489                         done=1;
490                         break;
491                 }
492
493                 if ( current_pair >= KernFont.num_kern_pairs )  {
494                         current_pair = -1;
495                 }
496
497                 int tmpp=-1;
498                 fonttool_get_kerning( &KernFont, c1, c2, &tmpp );
499
500                 if ( tmpp != -1 )       {
501                         current_pair = tmpp;
502                 } else  {
503                         if ( current_pair > -1 )
504                                 last_good_pair = current_pair;
505                         current_pair = -1;
506                 }
507         
508                 gr_reset_clip();
509                 gr_set_bitmap(bkg);
510                 gr_bitmap(0,0);
511                 gr_set_color_fast(&ac);
512
513                 sprintf( kerntext, "%c (%d)", c1, c1 );
514                 gr_string( 240, 210, kerntext );
515                 sprintf( kerntext, "%c (%d)", c2, c2 );
516                 gr_string( 340, 210, kerntext );
517
518                 sprintf( kerntext, "Ham%c%crger", c1, c2 );
519                 gr_string( 0x8000, 240, kerntext );
520
521                 sprintf( kerntext, "HAM%c%cRGER", c1, c2 );
522                 gr_string( 0x8000, 270, kerntext );
523
524                 sprintf( kerntext, "Offset: %d pixels", fonttool_get_kerning( &KernFont, c1, c2, NULL ) );
525                 gr_string( 0x8000, 300, kerntext );
526
527                 {
528                         int tw, th;
529                         gr_get_string_size( &tw, &th, kerntext );
530
531                         gr_string( 0x8000, 360, SampleText );
532                         gr_string( 20, 360+th+20, SampleText );
533                 }
534
535                 int x = 5, y = 200;
536                 int widest = 0;
537
538                  //= ( 330 - 200 ) / KernFont.h;
539                 int num_items = KernFont.num_kern_pairs;
540
541                 if ( current_pair > -1 )
542                         current_item = current_pair;
543
544                 if (current_item <0 )
545                         current_item = 0;
546
547                 if (current_item >= num_items )
548                         current_item = num_items-1;
549
550                 if (current_item<first_item)
551                         first_item = current_item;
552
553                 if (current_item>=(first_item+num_items_displayed))
554                         first_item = current_item-num_items_displayed+1;
555
556                 if (num_items <= num_items_displayed )  
557                         first_item = 0;
558
559                 int stop = first_item+num_items_displayed;
560                 if (stop>num_items) stop = num_items;
561
562                 int n = 0;
563                 for (i=first_item; i<stop; i++ )        {
564                         int tw, th;
565
566                         sprintf( kerntext, "%c%c", KernFont.kern_data[i].c1 + KernFont.first_ascii, KernFont.kern_data[i].c2 + KernFont.first_ascii );
567                         if ( i==current_pair )  {
568                                 gr_set_color( 255, 0, 0 );
569                                 //hud_tri( i2fl(x),i2fl(y), i2fl(x+6), i2fl(y+5), i2fl(x), i2fl(y+8) );
570                                 
571                                 int x1 = x;
572                                 int y1 = y;
573                                 int x2 = x+6;
574                                 int y2 = y+5;
575                                 int x3 = x;
576                                 int y3 = y+8;
577         
578                                 gr_line(x1,y1,x2,y2);
579                                 gr_line(x2,y2,x3,y3);
580                                 gr_line(x3,y3,x1,y1);
581
582                                 gr_set_color_fast(&ac);
583                         }
584                         gr_string( x+8, y, kerntext );
585                         n++;
586                         gr_get_string_size( &tw, &th, kerntext );
587                         tw += 8;
588                         if ( tw > widest )
589                                 widest = tw;
590                         y += KernFont.h;
591                         if ( y > 330 ) {
592                                 y = 200;
593                                 x += widest + 5;
594                                 if ( x > 150 ) {
595                                         num_items_displayed=n;
596                                         break;
597                                 }
598                                 widest = 0;
599                         }
600                 }
601                 if (i==stop)
602                         num_items_displayed++;
603
604                 if (num_items_displayed < 1 )
605                         num_items_displayed = 1;
606
607                 if (num_items_displayed > num_items )
608                         num_items_displayed = num_items;
609
610                 //mprintf(( "Num items = %d\n", num_items_displayed ));
611
612
613                 gr_flip();
614
615                 // sleep a little bit, don't need high framerate here
616                 SDL_Delay(10);
617         }
618
619         // cleanup
620         if (bkg > -1) {
621                 bm_unload(bkg);
622         }
623
624
625         exit(0);
626 }
627