diff options
author | Karl Ramm <kcr@1ts.org> | 2009-04-21 04:11:03 +0000 |
---|---|---|
committer | Karl Ramm <kcr@1ts.org> | 2009-04-21 04:11:03 +0000 |
commit | a6fed35df2fddffc5b0397721fbdf46fb1b59399 (patch) | |
tree | 029ad4ed9223526af865364dd711bc523c6b4c8d /zwgc/X_fonts.c | |
parent | 141ce091298a1364e2f6f56f71ad751931120188 (diff) |
Patch from Anders Kaseorg, use Xutf8DrawText et al. where available,
and use XFontSet instead of XFontStruct *
Tweaks to resource file to help the above pick some less wrong fonts.
Diffstat (limited to 'zwgc/X_fonts.c')
-rw-r--r-- | zwgc/X_fonts.c | 165 |
1 files changed, 53 insertions, 112 deletions
diff --git a/zwgc/X_fonts.c b/zwgc/X_fonts.c index 0d8e2b7..67ef488 100644 --- a/zwgc/X_fonts.c +++ b/zwgc/X_fonts.c @@ -35,13 +35,12 @@ static const char rcsid_X_fonts_c[] = "$Id$"; #include "zwgc.h" /* - * font_dict - Lookup cache for fonts (the value pointers are XFontStruct *'s) + * font_dict - Lookup cache for fonts (the value pointers are XFontSet's) */ static pointer_dictionary family_dict = NULL; static pointer_dictionary fontname_dict = NULL; static pointer_dictionary fontst_dict = NULL; -static pointer_dictionary fidst_dict = NULL; /* * {face,size}_to_string - lookup tables for converting {face,size} int @@ -53,7 +52,7 @@ static string size_to_string[] = { "small", "medium", "large" }; static char * get_family(char *style, - char *substyle) + char *substyle) { char *desc; pointer_dictionary_binding *binding; @@ -78,17 +77,17 @@ get_family(char *style, #undef STYLE_CLASS free(desc); if (family==NULL) - pointer_dictionary_Delete(family_dict,binding); + pointer_dictionary_Delete(family_dict,binding); else - binding->value=(pointer) family; + binding->value=(pointer) family; return(family); /* If resource returns NULL, return NULL also */ } } static char * get_specific_fontname(char *family, - int size, - int face) + int size, + int face) { char *desc; pointer_dictionary_binding *binding; @@ -113,104 +112,47 @@ get_specific_fontname(char *family, fontname=get_string_resource(desc,FAMILY_CLASS); free(desc); if (fontname==NULL) - pointer_dictionary_Delete(fontname_dict,binding); + pointer_dictionary_Delete(fontname_dict,binding); else - binding->value=(pointer) fontname; + binding->value=(pointer) fontname; return(fontname); /* If resource returns NULL, return NULL also */ } } -/* fast function to convert Font to hex. Return value - * is on the heap and must be freed. I'm cheating in - * that I know that Font us really an unsigned long. */ - -static char hexdigits[] = {"0123456789ABCDEF"}; -static char * -Font_to_hex(Font num) -{ - char *temp; - int i; - - temp=(char *) malloc((sizeof(Font)<<1)+2); - - for (i=0;i<((sizeof(Font)<<1)+1);i++) - temp[i] = hexdigits[(num>>(i*4))&0x0f]; - temp[i] = '\0'; - - return(temp); -} - -void -add_fid(XFontStruct *font) -{ - - char *fidstr; - pointer_dictionary_binding *binding; - int exists; - - if (!fidst_dict) - fidst_dict = pointer_dictionary_Create(37); - fidstr=Font_to_hex(font->fid); - binding = pointer_dictionary_Define(fidst_dict,fidstr,&exists); - free(fidstr); - - if (!exists) - binding->value=(pointer) font; -} - -/* requires that the font already be cached. */ -XFontStruct * -get_fontst_from_fid(Font fid) -{ - char *fidstr; - pointer_dictionary_binding *binding; - int exists; - - fidstr=Font_to_hex(fid); - - binding = pointer_dictionary_Define(fidst_dict,fidstr,&exists); - free(fidstr); -#ifdef DEBUG - if (exists) { - return((XFontStruct *) binding->value); - } else { - printf("Font fid=0x%s not cached. Oops.\n",fidstr); - abort(); - } -#else - return((XFontStruct *) binding->value); -#endif -} - -static XFontStruct * +static XFontSet get_fontst(Display *dpy, - char *fontname) + char *fontname) { pointer_dictionary_binding *binding; int exists; - XFontStruct *fontst; + XFontSet fontst; + char **missing_list; + int missing_count; + char *def_string; if (!fontst_dict) - fontst_dict = pointer_dictionary_Create(37); - binding = pointer_dictionary_Define(fontst_dict,fontname,&exists); + fontst_dict = pointer_dictionary_Create(37); + binding = pointer_dictionary_Define(fontst_dict, fontname, &exists); if (exists) { - return((XFontStruct *) binding->value); + return((XFontSet)binding->value); } else { - fontst=XLoadQueryFont(dpy,fontname); - if (fontst==NULL) { - pointer_dictionary_Delete(fontst_dict,binding); - } else { - binding->value=(pointer) fontst; - add_fid(fontst); - } return(fontst); /* If resource returns NULL, return NULL also */ + fontst = XCreateFontSet(dpy, fontname, &missing_list, &missing_count, + &def_string); + XFreeStringList(missing_list); + if (fontst == NULL) + pointer_dictionary_Delete(fontst_dict,binding); + else + binding->value = (pointer)fontst; + + return(fontst); /* If resource returns NULL, return NULL also */ } } static char * get_fontname(char *family, - int size, - int face) + int size, + int face) { char *fontname; @@ -221,55 +163,55 @@ get_fontname(char *family, return(fontname); } -static XFontStruct * +static XFontSet complete_get_fontst(Display *dpy, - string style, - string substyle, - int size, - int face) + string style, + string substyle, + int size, + int face) { char *family,*fontname; - XFontStruct *fontst; + XFontSet fontst; if ((family=get_family(style,substyle))) if ((fontname=get_fontname(family,size,face))) if ((fontst=get_fontst(dpy,fontname))) - return(fontst); + return(fontst); /* If any part fails, */ return(NULL); } /* - * XFontStruct *get_font(string style, substyle; int size, face) + * XFontSet get_font(string style, substyle; int size, face) * Requires: size is one of SMALL_SIZE, MEDIUM_SIZE, LARGE_SIZE and * face is one of ROMAN_FACE, BOLD_FACE, ITALIC_FACE, * BOLDITALIC_FACE. * Effects: unknown */ -XFontStruct * +XFontSet get_font(Display *dpy, - string style, - string substyle, - int size, - int face) + string style, + string substyle, + int size, + int face) { char *family,*fontname; - XFontStruct *fontst; + XFontSet fontst; if (size == SPECIAL_SIZE) { /* attempt to process @font explicitly */ if ((fontst = get_fontst(dpy, substyle))) - return(fontst); + return(fontst); } else { if ((family = get_family(style, substyle))) { - if ((fontname = get_fontname(family, size,face))) - if ((fontst = get_fontst(dpy, fontname))) - return(fontst); + if ((fontname = get_fontname(family, size,face))) + if ((fontst = get_fontst(dpy, fontname))) + return(fontst); } else { - if ((fontname = get_fontname(substyle, size, face))) - if ((fontst = get_fontst(dpy, fontname))) - return(fontst); + if ((fontname = get_fontname(substyle, size, face))) + if ((fontst = get_fontst(dpy, fontname))) + return(fontst); } /* At this point, the no-failure case didn't happen, and the case @@ -277,10 +219,10 @@ get_font(Display *dpy, fontst=NULL; if (!(fontst = complete_get_fontst(dpy,style,"text",size,face))) - if (!(fontst = complete_get_fontst(dpy,"default",substyle,size,face))) - if (!(fontst = complete_get_fontst(dpy,"default","text",size,face))) - if ((fontname = get_fontname("default",size,face))) - fontst = get_fontst(dpy,fontname); + if (!(fontst = complete_get_fontst(dpy,"default",substyle,size,face))) + if (!(fontst = complete_get_fontst(dpy,"default","text",size,face))) + if ((fontname = get_fontname("default",size,face))) + fontst = get_fontst(dpy,fontname); if (fontst) return(fontst); } @@ -299,4 +241,3 @@ get_font(Display *dpy, } #endif /* X_DISPLAY_MISSING */ - |