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 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769
|
/*
* This file is part of the XForms library package.
*
* XForms is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1, or
* (at your option) any later version.
*
* XForms is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with XForms. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* \file fonts.c
*
* This file is part of the XForms library package.
* Copyright (c) 1996-2002 T.C. Zhao and Mark Overmars
* All rights reserved.
*
* All font and string size query routines. There are rooms for speed ups.
* For one, font switching can be reduced somewhat.
*
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "include/forms.h"
#include "flinternal.h"
#include "private/flvasprintf.h"
#include <string.h>
#include <ctype.h>
static XFontStruct * defaultfs;
static XFontStruct * try_get_font_struct( int,
int,
int );
static char * get_fname( const char *,
int );
/*
* Question marks indicate the sizes in tenth of a point. It will be
* replaced on the fly by the font requesting routines. Depending on
* the availability of the fonts and capabilities of the server, the
* font may or may not be scalable.
*
* Resolution field is left blank on purpose as the resolution reported
* by the server is not reliable thus the program can't force the
* resolution. This way the admins can set the proper font path.
*
* Order is important as it has to agree with FL_TIMES_STYLE etc
* defined in Basic.h
*
*/
static const char *fnts[ FL_MAXFONTS ] =
{
"-*-helvetica-medium-r-*-*-*-?-*-*-p-*-*-*",
"-*-helvetica-bold-r-*-*-*-?-*-*-p-*-*-*",
"-*-helvetica-medium-o-*-*-*-?-*-*-p-*-*-*",
"-*-helvetica-bold-o-*-*-*-?-*-*-p-*-*-*",
"-*-courier-medium-r-*-*-*-?-*-*-*-*-*-*",
"-*-courier-bold-r-*-*-*-?-*-*-*-*-*-*",
"-*-courier-medium-o-*-*-*-?-*-*-*-*-*-*",
"-*-courier-bold-o-*-*-*-?-*-*-*-*-*-*",
"-*-times-medium-r-*-*-*-?-*-*-p-*-*-*",
"-*-times-bold-r-*-*-*-?-*-*-p-*-*-*",
"-*-times-medium-i-*-*-*-?-*-*-p-*-*-*",
"-*-times-bold-i-*-*-*-?-*-*-p-*-*-*",
"-*-charter-medium-r-*-*-*-?-*-*-*-*-*-*",
"-*-charter-bold-r-*-*-*-?-*-*-*-*-*-*",
"-*-charter-medium-i-*-*-*-?-*-*-*-*-*-*",
"-*-charter-bold-i-*-*-*-?-*-*-*-*-*-*",
};
static FL_FONT fl_fonts[ FL_MAXFONTS ];
static const char *cv_fname( const char * );
#define DEFAULTF1 "fixed"
#define DEFAULTF2 "6x13"
/***************************************
* Global initialization routine. Must be called before any font
* routine can be used. We can place fli_init_font in all font switching
* and string size query routines so a seperate initialization is not
* needed, and it has the added bonus that startup *may* be faster,
* but we pay a function call overhead in every call to any of these
* font related routines.
***************************************/
void
fli_init_font( void )
{
FL_FONT *flf = fl_fonts;
const char *const *f = fnts;
static int initialized;
if ( initialized )
return;
initialized = 1;
/* If fl_set_font_name() is called before fl_initialize(), we need to
keep the change */
for ( ; *f; f++, flf++ )
if ( ! *flf->fname )
strcpy( flf->fname, *f );
/* Load a default font */
if ( ! defaultfs
&& ! ( defaultfs = XLoadQueryFont( flx->display, DEFAULTF1 ) ) )
defaultfs = XLoadQueryFont( flx->display, DEFAULTF2 );
/* Load a couple of fonts at normal size to prevent the caching code from
using bad looking replacement if strange sizes are requested */
fl_get_font_struct( FL_NORMAL_STYLE, FL_DEFAULT_SIZE );
fl_get_font_struct( FL_BOLD_STYLE, FL_DEFAULT_SIZE );
fl_get_font_struct( FL_FIXED_STYLE, FL_DEFAULT_SIZE );
}
/***************************************
* In addition to get the font handle, we also make the font current
* in default GC
***************************************/
void
fl_set_font( int numb,
int size )
{
int dh;
XCharStruct overall;
XFontStruct *fs;
fs = fl_get_font_struct( numb, size );
/* cur_font is always the one in current GC */
if ( fl_state[ fl_vmode ].cur_fnt == fs )
{
#if FL_DEBUG >= ML_DEBUG
M_debug( "fl_set_font", "current", fli_curfnt );
#endif
return;
}
fl_state[ fl_vmode ].cur_fnt = flx->fs = fs;
/* Basic font info (no need to send a string, we just want the maximum
ascent and descent) */
XTextExtents( flx->fs, "", 0, &dh, &flx->fasc, &flx->fdesc, &overall );
flx->fheight = flx->fasc + flx->fdesc;
XSetFont( flx->display, flx->textgc, flx->fs->fid );
if ( fli_cntl.debug > 1 )
{
unsigned long res = 0;
if ( XGetFontProperty( flx->fs, XA_RESOLUTION, &res ) )
M_info2( "fl_set_font", "FontResolution: %lu", res );
}
}
/***************************************
* Add a new font (indexed by n) or change an existing font.
* Preferably the font name constains a '?' in the size
* position so different sizes can be used.
***************************************/
int
fl_set_font_name( int n,
const char * name )
{
FL_FONT *flf;
if ( n < 0 || n >= FL_MAXFONTS )
{
M_warn( "fl_set_font_name", "Bad font number (%d)", n );
return -1;
}
if ( ! name || ! *name )
{
M_warn( "fl_set_font_name", "Bad font name" );
return -1;
}
if ( strlen( name ) > FL_MAX_FONTNAME_LENGTH )
{
M_warn( "fl_set_font_name", "Font name too long" );
return -1;
}
flf = fl_fonts + n;
if ( *flf->fname )
{
int i;
for ( i = 0; i < flf->nsize; i++ )
XFreeFont( flx->display, flf->fs[ i ] );
*flf->fname = '\0';
}
flf->nsize = 0;
strcpy( flf->fname, name );
if ( ! flx || ! flx->display )
return 1;
return try_get_font_struct( n, FL_DEFAULT_SIZE, 1 ) ? 0 : -1;
}
/***************************************
* Add a new font (indexed by n) or change an existing font.
***************************************/
int
fl_set_font_name_f( int n,
const char * fmt,
... )
{
char *buf;
int ret;
EXPAND_FORMAT_STRING( buf, fmt );
ret = fl_set_font_name( n, buf );
fl_free( buf );
return ret;
}
/***************************************
* List built-in fonts
***************************************/
int
fl_enumerate_fonts( void ( * output )( const char *s ),
int shortform )
{
FL_FONT *flf = fl_fonts,
*fe = flf + FL_MAXFONTS;
int n = 0;
for ( ; output && flf < fe; flf++ )
if ( *flf->fname )
{
output( shortform ? cv_fname( flf->fname ) : flf->fname );
n++;
}
return n;
}
/***************************************
* All font changes go through this routine. If with_fail is false,
* this routine will not fail even if requested font can't be loaded.
* A substitution will be made.
***************************************/
static XFontStruct *
try_get_font_struct( int numb,
int size,
int with_fail )
{
FL_FONT *flf = fl_fonts;
XFontStruct *fs = NULL;
int n = 0,
i;
if ( special_style( numb ) )
numb %= FL_SHADOW_STYLE;
flf = fl_fonts + numb;
if ( numb < 0 || numb >= FL_MAXFONTS || ! *flf->fname )
{
if ( ! fli_no_connection ) {
/* This function is typically used to test whether a font is
loadable or not, so it's not a fatal error if it fails. Issue
a message for information therefore. */
M_info( "try_get_font_struct", "Bad FontStyle requested: %d: %s",
numb, flf->fname );
}
if ( ! fl_state[ fl_vmode ].cur_fnt )
M_warn( "try_get_font_struct", "bad font returned" );
return fl_state[ fl_vmode ].cur_fnt;
}
strcpy( fli_curfnt, get_fname( flf->fname, size ) );
/* Search for requested size */
for ( fs = NULL, i = 0; i < flf->nsize; i++ )
if ( size == flf->size[ i ] )
{
fs = flf->fs[ i ];
#if FL_DEBUG >= ML_DEBUG
M_debug( "try_get_font_struct", "Cache hit: %s",
fl_cur_fontname );
#endif
}
/* If requested font is not found or cache is full, get the destination
cache for this size */
if ( ! fs && flf->nsize == FL_MAX_FONTSIZES )
{
XFreeFont( flx->display, flf->fs[ FL_MAX_FONTSIZES - 1 ] );
flf->nsize--;
}
/* Font is not cached, try to load it */
if ( ! fs )
{
n = flf->nsize;
flf->fs[ n ] = XLoadQueryFont( flx->display, fli_curfnt );
if ( ( fs = flf->fs[ n ] ) )
{
flf->size[ n ] = size;
flf->nsize++;
}
}
if ( ! fs && with_fail )
return NULL;
/* Didn't get it. Try to find a substitute */
if ( ! fs )
{
int mdiff = 1000,
k = -1;
M_warn( "try_get_font_struct", "Can't load %s", fli_curfnt );
/* Search for a replacement */
for ( i = 0; i < flf->nsize; i++ )
{
if ( mdiff > FL_abs( size - flf->size[ i ] ) )
{
mdiff = FL_abs( size - flf->size[ i ] );
k = i;
}
}
fs = k == -1 ? ( flx->fs ? flx->fs : defaultfs ) : flf->fs[ k ];
/* If we did not get it this time, we won't get it next time either,
so replace it with whatever we found */
flf->size[ flf->nsize ] = size;
flf->fs[ flf->nsize ] = fs;
flf->nsize++;
}
/* Here we are guranteed a valid font handle although there is no
gurantee the font handle corresponds to the font requested */
return fs;
}
/***************************************
***************************************/
XFontStruct *
fl_get_font_struct( int style,
int size )
{
return try_get_font_struct( style, size, 0 );
}
/***************************************
* Similar to fl_get_string_xxxGC except that there is no side effects.
* Must not free the fontstruct as structure FL_FONT caches the
* structure for possible future use.
***************************************/
int
fl_get_string_width( int style,
int size,
const char * s,
int len )
{
XFontStruct *fs = fl_get_font_struct( style, size );
return fli_no_connection ? ( len * size ) : XTextWidth( fs, s, len );
}
/***************************************
***************************************/
int
fli_get_string_widthTABfs( XFontStruct * fs,
const char * s,
int len )
{
int w,
tab;
const char *p,
*q;
if ( fli_no_connection )
return 12 * len;
tab = fli_get_tabpixels( fs );
for ( w = 0, q = s; *q && ( p = strchr( q, '\t' ) ) && ( p - s ) < len;
q = p + 1 )
{
w += XTextWidth( fs, q, p - q );
w = ( ( w / tab ) + 1 ) * tab;
}
return w += XTextWidth( fs, q, len - ( q - s ) );
}
/***************************************
***************************************/
int
fl_get_string_widthTAB( int style,
int size,
const char * s,
int len )
{
XFontStruct *fs = fl_get_font_struct( style, size );
return fli_get_string_widthTABfs( fs, s, len );
}
/***************************************
* Function returns the height of the string, calculated from adding the
* largest ascent and descent of all its characters in the string, via 'asc'
* and 'desc' (but which both can be NULL pointers), the maximum ascent
* and descent.
***************************************/
int
fl_get_string_height( int style,
int size,
const char * s,
int len,
int * asc,
int * desc )
{
int a, d;
if ( fli_no_connection )
a = d = size / 2;
else
{
XFontStruct *fs = fl_get_font_struct( style, size );
XCharStruct overall;
int dh;
XTextExtents( fs, s, len, &dh, &a, &d, &overall );
}
if ( asc )
*asc = a;
if ( desc )
*desc = d;
return a + d;
}
/***************************************
* Returns font height and, via 'asc' and 'desc' (but which both can be NULL
* pointers), the fonts ascent and descent.
***************************************/
int
fl_get_char_height( int style,
int size,
int * asc,
int * desc )
{
int a, d;
if ( fli_no_connection )
a = d = size / 2;
else
{
XFontStruct *fs = fl_get_font_struct( style, size );
a = fs->ascent;
d = fs->descent;
if ( asc )
*asc = a;
if ( desc )
*desc = d;
}
return a + d;
}
/***************************************
* Function returns the width of the widest character in the requested font
***************************************/
int
fl_get_char_width( int style,
int size )
{
XFontStruct *fs = fl_get_font_struct( style, size );
return fs->max_bounds.width;
}
/***************************************
***************************************/
void
fl_get_string_dimension( int fntstyle,
int fntsize,
const char * s,
int len,
int * width,
int * height )
{
const char *p,
*q;
int h,
maxw = 0,
maxh = 0;
h = fl_get_char_height( fntstyle, fntsize, NULL, NULL );
for ( q = s; *q && ( p = strchr( q, '\n' ) ); q = p + 1 )
{
maxw = FL_max( maxw,
fl_get_string_width( fntstyle, fntsize, q, p - q ) );
maxh += h;
}
maxw = FL_max( maxw, fl_get_string_width( fntstyle, fntsize,
q, len - ( q - s ) ) );
maxh += h;
*width = maxw;
*height = maxh;
}
/*
* Tab handling. Currently only one tab
*/
#define MaxTabs 5
static char *tabstop[ MaxTabs ] = { "aaaaaaaa", 0 };
static int tabstopNchar[ MaxTabs ] = { 7 };
/***************************************
***************************************/
void
fl_set_tabstop( const char *s )
{
static int set;
if ( s )
{
if ( set )
fl_free( *tabstop );
*tabstop = fl_strdup( s );
*tabstopNchar = strlen( *tabstop );
set = 1;
}
}
/***************************************
***************************************/
int
fli_get_tabpixels( XFontStruct * fs )
{
return XTextWidth( fs, *tabstop, *tabstopNchar )
+ XTextWidth( fs, " ", 1 );
}
/***************************************
* Convert X font names to more conventional names by stripping the
* auxiliary info.
***************************************/
static const char *
cv_fname( const char *f )
{
static char fname[ FL_MAX_FONTNAME_LENGTH + 1 ];
char *q,
*p;
/* Remove all the garbages from head */
for ( q = strcpy( fname, f ); *q && ! isalnum( ( unsigned char ) *q ); q++ )
/* empty */ ;
/* Remove all the garbage from the end, starting from '?' */
if ( ( p = strchr( fname, '?' ) ) )
*--p = '\0';
/* Remove all remaining garbages */
for ( p = fname + strlen( fname ) - 1;
p > q && ! isalnum( ( unsigned char ) *p ); p-- )
/* empty */ ;
*++p = '\0';
return q;
}
/***************************************
* Given a font name and a size (in points), assemble the complete name
***************************************/
static char *
get_fname( const char * str,
int size )
{
static char fname[ sizeof fli_curfnt ];
char len_str[ 50 ]; /* should be enough for all ints */
char *p;
/* If necessary truncate font names that are too long, the caller
expects a real string */
strncpy( fname, str, sizeof fname - 1 );
fname[ sizeof fname - 1 ] = '\0';
if ( ( p = strchr( fname, '?' ) ) )
{
int len = sprintf( len_str, "%d0", size );
if ( len + strlen( str ) <= sizeof fname - 1 )
{
memmove( p + len, p + 1, strlen( p ) );
strncpy( p, len_str, len );
}
}
return fname;
}
/***************************************
* Some compatibility stuff, i.e. functions that were never documented
* and were removed from V0.89, but apparently this broke some applications
* that were using them. Put back in 10/22/00.
***************************************/
int
fl_fdesc_( void )
{
return flx->fdesc;
}
/***************************************
***************************************/
int
fl_fheight_( void )
{
return flx->fheight;
}
/***************************************
***************************************/
GC
fl_gc_( void )
{
return flx->gc;
}
/***************************************
***************************************/
GC
fl_textgc_( void )
{
return flx->textgc;
}
/***************************************
***************************************/
Window
fl_cur_win_( void )
{
return flx->win;
}
/***************************************
***************************************/
XFontStruct *
fl_cur_fs_( void )
{
return flx->fs;
}
/***************************************
***************************************/
Display *
fl_display_( void )
{
return flx->display;
}
/*
* Local variables:
* tab-width: 4
* indent-tabs-mode: nil
* End:
*/
|