2 * $Logfile: /Freespace2/code/Fonttool/FontKern.cpp $
7 * Tool for interactively kerning fonts
10 * Revision 1.1 2002/05/03 03:28:08 root
14 * 6 5/19/99 4:07p Dave
15 * Moved versioning code into a nice isolated common place. Fixed up
16 * updating code on the pxo screen. Fixed several stub problems.
18 * 5 12/18/98 1:14a Dave
19 * Rough 1024x768 support for Direct3D. Proper detection and usage through
22 * 4 12/02/98 9:58a Dave
23 * Got fonttool working under glide/direct3d.
25 * 3 11/30/98 1:09p Dave
27 * 2 10/24/98 5:15p Dave
29 * 1 10/24/98 4:58p Dave
31 * 14 5/06/98 5:30p John
32 * Removed unused cfilearchiver. Removed/replaced some unused/little used
33 * graphics functions, namely gradient_h and _v and pixel_sp. Put in new
34 * DirectX header files and libs that fixed the Direct3D alpha blending
37 * 13 4/13/98 10:11a John
38 * Made timer functions thread safe. Made timer_init be called in all
41 * 12 3/10/98 4:18p John
42 * Cleaned up graphics lib. Took out most unused gr functions. Made D3D
43 * & Glide have popups and print screen. Took out all >8bpp software
44 * support. Made Fred zbuffer. Made zbuffer allocate dynamically to
45 * support Fred. Made zbuffering key off of functions rather than one
48 * 11 3/05/98 11:15p Hoffoss
49 * Changed non-game key checking to use game_check_key() instead of
52 * 10 10/30/97 4:56p John
53 * Fixed up font stuff to build. Fixed bug where it didn't show the last
54 * 3 characters in kerning table.
56 * 9 9/03/97 4:32p John
57 * changed bmpman to only accept ani and pcx's. made passing .pcx or .ani
58 * to bm_load functions not needed. Made bmpman keep track of palettes
59 * for bitmaps not mapped into game palettes.
61 * 8 6/06/97 6:47p John
64 * 7 6/06/97 4:41p John
65 * Fixed alpha colors to be smoothly integrated into gr_set_color_fast
68 * 6 6/06/97 11:10a John
69 * made scrolling kern pair box.
71 * 5 6/06/97 9:21a John
72 * added some kerning pairs
74 * 4 6/06/97 9:18a John
75 * Added capital hamburger.
77 * 3 6/05/97 5:00p John
80 * 2 6/05/97 4:53p John
81 * First rev of new antialiased font stuff.
83 * 1 6/02/97 4:04p John
104 #include "osregistry.h"
106 #include "fonttool.h"
108 char *SampleText = "This is some sample text that is here to\n" \
109 "Show you how the antialiasing will\n" \
110 "look over different color backgrounds\n" \
111 "KERN PAIRS: VaWaVeWeVAV-LyT.T,TyTvTcYe\n";
113 static void myexit(int value)
136 int fonttool_get_kerning( font *fnt, int c1, int c2, int *pairnum )
140 int l1 = c1 - fnt->first_ascii;
141 int l2 = c2 - fnt->first_ascii;
143 for (i=0; i<fnt->num_kern_pairs; i++ ) {
144 if ( (fnt->kern_data[i].c1 == l1) && (fnt->kern_data[i].c2 == l2) ) {
145 if (pairnum) *pairnum = i;
146 return fnt->kern_data[i].offset;
152 void fonttool_resync_kerning( font *fnt )
156 // update all the font into
157 for (i=0; i<fnt->num_chars; i++ ) {
158 fnt->char_data[i].kerning_entry = -1;
161 for (i=0; i<fnt->num_kern_pairs; i++ ) {
162 int c = fnt->kern_data[i].c1;
163 if ( fnt->char_data[c].kerning_entry == -1 )
164 fnt->char_data[c].kerning_entry = (short)i;
168 void fonttool_remove_kern_pair( font *fnt, int index )
171 int i, n, new_num_pairs;
173 new_num_pairs = fnt->num_kern_pairs - 1;
175 if ( new_num_pairs < 1 ) {
176 fonttool_remove_kerning(fnt);
180 font_kernpair *new_kern_data = (font_kernpair *)malloc( new_num_pairs*sizeof(font_kernpair) );
181 if (!new_kern_data) {
182 printf( "Out of memory!\n" );
188 for (i=0; i<fnt->num_kern_pairs; i++ ) {
190 new_kern_data[n] = fnt->kern_data[i];
195 if ( fnt->kern_data ) free( fnt->kern_data );
196 fnt->kern_data = new_kern_data;
197 fnt->kern_data_size = sizeof(font_kernpair)*new_num_pairs;
198 fnt->num_kern_pairs = new_num_pairs;
200 fonttool_resync_kerning(fnt);
202 mprintf(( "Font has %d kern pairs\n", fnt->num_kern_pairs ));
205 void fonttool_set_kerning( font *fnt, int c1, int c2, int dist )
209 int l1 = c1 - fnt->first_ascii;
210 int l2 = c2 - fnt->first_ascii;
212 for (i=0; i<fnt->num_kern_pairs; i++ ) {
213 if ( (fnt->kern_data[i].c1 == l1) && (fnt->kern_data[i].c2 == l2) ) {
214 fnt->kern_data[i].offset = (signed char)dist;
216 fonttool_remove_kern_pair( fnt, i );
221 if ( dist == 0 ) return;
226 new_num_pairs = fnt->num_kern_pairs+1;
228 font_kernpair *new_kern_data = (font_kernpair *)malloc( new_num_pairs*sizeof(font_kernpair) );
229 if (!new_kern_data) {
230 printf( "Out of memory!\n" );
236 uint newcode = l1*256+l2;
238 for (i=0; i<fnt->num_kern_pairs; i++ ) {
239 uint code = fnt->kern_data[i].c1*256 + fnt->kern_data[i].c2;
240 if ( code < newcode ) {
241 new_kern_data[n] = fnt->kern_data[i];
249 new_kern_data[n].c1 = (char)l1;
250 new_kern_data[n].c2 = (char)l2;
251 new_kern_data[n].offset = (signed char)dist;
255 for (; i<fnt->num_kern_pairs; i++ ) {
256 new_kern_data[n] = fnt->kern_data[i];
261 if ( fnt->kern_data ) free( fnt->kern_data );
262 fnt->kern_data = new_kern_data;
263 fnt->kern_data_size += sizeof(font_kernpair);
264 fnt->num_kern_pairs++;
266 fonttool_resync_kerning(fnt);
268 mprintf(( "Font has %d kern pairs\n", fnt->num_kern_pairs ));
273 void fonttool_remove_kerning( font *fnt )
277 for (i=0; i<fnt->num_chars; i++ ) {
278 fnt->char_data[i].kerning_entry = -1;
281 fnt->kern_data_size = 0;
283 free( fnt->kern_data );
284 fnt->kern_data = NULL;
285 fnt->num_kern_pairs = 0;
289 void fonttool_edit_kerning(char *fname1)
294 font KernFont, tmpfont;
296 int cr=16, cb=16, cg=16;
297 int c1 = 'b', c2 = 'u';
299 int current_pair = -1;
301 int current_item = 0;
302 int num_items_displayed = 1;
303 int last_good_pair = -1;
307 printf( "Editing kerning data for %s\n", fname1 );
308 fonttool_read( fname1, &KernFont );
312 // setup the fred exe directory so CFILE can init properly
313 //char *c = GetCommandLine();
315 //char *tok = strtok(c, " ");
316 //Assert(tok != NULL);
317 cfile_init(__argv[0]);
319 os_init( "FontTool", "FontTool - Kerning Table Editor" );
321 os_init_registry_stuff(Osreg_company_name, Osreg_app_name,NULL);
322 ptr = os_config_read_string(NULL, NOX("Videocard"), NULL);
323 if((ptr == NULL) || !stricmp(ptr, "Aucune accélération 3D") || !stricmp(ptr, "Keine 3D-Beschleunigerkarte") || !stricmp(ptr, "No 3D acceleration")){
324 MessageBox((HWND)os_get_window(), "Warning, Freespace 2 requires Glide or Direct3D hardware accleration. You will not be able to run Freespace 2 without it", "Warning", MB_OK);
328 if (!stricmp(ptr, NOX("3DFX Glide"))) {
330 gr_init(GR_640, GR_GLIDE);
331 } else if (strstr(ptr, NOX("Direct 3D -"))){
333 gr_init(GR_640, GR_DIRECT3D);
338 gr_set_palette("none",NULL);
339 bkg = bm_load( "code\\fonttool\\FontTool" );
341 printf("Error loading FontTool\n" );
344 palette_use_bm_palette(bkg);
349 gr_init_alphacolor( &ac, cr*16,cg*16,cb*16,alpha*16 );
353 extern font *Current_font;
354 Current_font = &KernFont;
362 fonttool_read( fname1, &tmpfont );
363 fonttool_copy_kern( &tmpfont, &KernFont );
367 fonttool_remove_kerning( &KernFont );
371 fonttool_dump( fname1, &KernFont );
378 gr_init_alphacolor(&ac,cr*16,cg*16,cb*16,alpha*16);
385 gr_init_alphacolor(&ac,cr*16,cg*16,cb*16,alpha*16);
390 if ( cr == 16 ) cr = 1; else cr = 16;
391 gr_init_alphacolor(&ac,cr*16,cg*16,cb*16,alpha*16);
395 if ( cg == 16 ) cg = 1; else cg = 16;
396 gr_init_alphacolor(&ac,cr*16,cg*16,cb*16,alpha*16);
400 if ( cb == 16 ) cb = 1; else cb = 16;
401 gr_init_alphacolor(&ac,cr*16,cg*16,cb*16,alpha*16);
405 x = fonttool_get_kerning( &KernFont, c1, c2, NULL );
406 fonttool_set_kerning( &KernFont, c1, c2, x+1 );
410 x = fonttool_get_kerning( &KernFont, c1, c2, NULL );
411 fonttool_set_kerning( &KernFont, c1, c2, x-1 );
415 fonttool_set_kerning( &KernFont, c1, c2, 0 );
419 if ( c1 < KernFont.first_ascii + KernFont.num_chars-1 ) c1++;
423 if ( c1 > KernFont.first_ascii ) c1--;
427 if ( c2 < KernFont.first_ascii + KernFont.num_chars-1 ) c2++;
428 mprintf(( "C2 = %d\n", c2 ));
432 if ( c2 > KernFont.first_ascii ) c2--;
433 mprintf(( "C2 = %d\n", c2 ));
437 if ( current_pair < 0 )
438 current_pair = last_good_pair;
441 if ( current_pair >= KernFont.num_kern_pairs ) {
442 current_pair = KernFont.num_kern_pairs-1;
444 if ( (current_pair < KernFont.num_kern_pairs) && (current_pair > -1) ) {
445 c1 = KernFont.kern_data[current_pair].c1 + KernFont.first_ascii;
446 c2 = KernFont.kern_data[current_pair].c2 + KernFont.first_ascii;
451 if ( current_pair < 0 )
452 current_pair = last_good_pair;
455 if ( current_pair < 0 ) {
458 if ( (current_pair < KernFont.num_kern_pairs) && (current_pair > -1) ) {
459 c1 = KernFont.kern_data[current_pair].c1 + KernFont.first_ascii;
460 c2 = KernFont.kern_data[current_pair].c2 + KernFont.first_ascii;
469 if ( current_pair >= KernFont.num_kern_pairs ) {
474 fonttool_get_kerning( &KernFont, c1, c2, &tmpp );
479 if ( current_pair > -1 )
480 last_good_pair = current_pair;
487 gr_set_color_fast(&ac);
489 sprintf( kerntext, "%c (%d)", c1, c1 );
490 gr_string( 240, 210, kerntext );
491 sprintf( kerntext, "%c (%d)", c2, c2 );
492 gr_string( 340, 210, kerntext );
494 sprintf( kerntext, "Ham%c%crger", c1, c2 );
495 gr_string( 0x8000, 240, kerntext );
497 sprintf( kerntext, "HAM%c%cRGER", c1, c2 );
498 gr_string( 0x8000, 270, kerntext );
500 sprintf( kerntext, "Offset: %d pixels", fonttool_get_kerning( &KernFont, c1, c2, NULL ) );
501 gr_string( 0x8000, 300, kerntext );
505 gr_get_string_size( &tw, &th, kerntext );
507 gr_string( 0x8000, 360, SampleText );
508 gr_string( 20, 360+th+20, SampleText );
514 //= ( 330 - 200 ) / KernFont.h;
515 int num_items = KernFont.num_kern_pairs;
517 if ( current_pair > -1 )
518 current_item = current_pair;
520 if (current_item <0 )
523 if (current_item >= num_items )
524 current_item = num_items-1;
526 if (current_item<first_item)
527 first_item = current_item;
529 if (current_item>=(first_item+num_items_displayed))
530 first_item = current_item-num_items_displayed+1;
532 if (num_items <= num_items_displayed )
535 int stop = first_item+num_items_displayed;
536 if (stop>num_items) stop = num_items;
539 for (i=first_item; i<stop; i++ ) {
542 sprintf( kerntext, "%c%c", KernFont.kern_data[i].c1 + KernFont.first_ascii, KernFont.kern_data[i].c2 + KernFont.first_ascii );
543 if ( i==current_pair ) {
544 gr_set_color( 255, 0, 0 );
545 //hud_tri( i2fl(x),i2fl(y), i2fl(x+6), i2fl(y+5), i2fl(x), i2fl(y+8) );
554 gr_line(x1,y1,x2,y2);
555 gr_line(x2,y2,x3,y3);
556 gr_line(x3,y3,x1,y1);
558 gr_set_color_fast(&ac);
560 gr_string( x+8, y, kerntext );
562 gr_get_string_size( &tw, &th, kerntext );
571 num_items_displayed=n;
578 num_items_displayed++;
580 if (num_items_displayed < 1 )
581 num_items_displayed = 1;
583 if (num_items_displayed > num_items )
584 num_items_displayed = num_items;
586 //mprintf(( "Num items = %d\n", num_items_displayed ));