00001
00002
00003
00004
00005
00006
00007
00008
00009 #include "mit-copyright.h"
00010
00011
00012
00013
00014
00015
00016
00017 #ifndef X_DISPLAY_MISSING
00018
00019 #include "X_fonts.h"
00020 #include "new_string.h"
00021 #include "error.h"
00022 #include "pointer_dictionary.h"
00023 #include "main.h"
00024
00025
00026
00027
00028
00029 static pointer_dictionary family_dict = NULL;
00030 static pointer_dictionary fontname_dict = NULL;
00031 static pointer_dictionary fontst_dict = NULL;
00032 static pointer_dictionary fidst_dict = NULL;
00033
00034
00035
00036
00037
00038
00039 static string face_to_string[] = {"roman", "bold", "italic", "bolditalic"};
00040 static string size_to_string[] = {"small", "medium", "large"};
00041
00042 extern char *get_string_resources();
00043
00044 static char *
00045 get_family(style, substyle)
00046 char *style;
00047 char *substyle;
00048 {
00049 char *desc;
00050 pointer_dictionary_binding *binding;
00051 int exists;
00052 char *family;
00053
00054 desc = string_Concat("style.", style);
00055 desc = string_Concat2(desc, ".substyle.");
00056 desc = string_Concat2(desc, substyle);
00057 desc = string_Concat2(desc, ".fontfamily");
00058
00059 if (!family_dict)
00060 family_dict = pointer_dictionary_Create(37);
00061 binding = pointer_dictionary_Define(family_dict, desc, &exists);
00062
00063 if (exists) {
00064 free(desc);
00065 return ((string) binding->value);
00066 }
00067 else {
00068 #define STYLE_CLASS "StyleKey.Style1.Style2.Style3.SubstyleKey.Substyle.FontfamilyKey"
00069 family = get_string_resource(desc, STYLE_CLASS);
00070 #undef STYLE_CLASS
00071 free(desc);
00072 if (family == NULL)
00073 pointer_dictionary_Delete(family_dict, binding);
00074 else
00075 binding->value = (pointer) family;
00076 return (family);
00077 }
00078 }
00079
00080 static char *
00081 get_specific_fontname(family, size, face)
00082 char *family;
00083 int size;
00084 int face;
00085 {
00086 char *desc;
00087 pointer_dictionary_binding *binding;
00088 int exists;
00089 char *fontname;
00090
00091 desc = string_Concat("fontfamily.", family);
00092 desc = string_Concat2(desc, ".");
00093 desc = string_Concat2(desc, size_to_string[size]);
00094 desc = string_Concat2(desc, ".");
00095 desc = string_Concat2(desc, face_to_string[face]);
00096
00097 if (!fontname_dict)
00098 fontname_dict = pointer_dictionary_Create(37);
00099 binding = pointer_dictionary_Define(fontname_dict, desc, &exists);
00100
00101 if (exists) {
00102 free(desc);
00103 return ((string) binding->value);
00104 }
00105 else {
00106 #define FAMILY_CLASS "FontfamilyKey.Fontfamily.Size.Face"
00107 fontname = get_string_resource(desc, FAMILY_CLASS);
00108 free(desc);
00109 if (fontname == NULL)
00110 pointer_dictionary_Delete(fontname_dict, binding);
00111 else
00112 binding->value = (pointer) fontname;
00113 return (fontname);
00114
00115 }
00116 }
00117
00118
00119
00120
00121
00122
00123
00124 static char hexdigits[] = {"0123456789ABCDEF"};
00125 static char *
00126 Font_to_hex(num)
00127 Font num;
00128 {
00129 char *temp;
00130 int i;
00131
00132 temp = (char *) malloc((sizeof(Font) << 1) + 2);
00133
00134 for (i = 0; i < ((sizeof(Font) << 1) + 1); i++)
00135 temp[i] = hexdigits[(num >> (i * 4)) & 0x0f];
00136 temp[i] = '\0';
00137
00138 return (temp);
00139 }
00140
00141 void
00142 add_fid(font)
00143 XFontStruct *font;
00144 {
00145
00146 char *fidstr;
00147 pointer_dictionary_binding *binding;
00148 int exists;
00149
00150 if (!fidst_dict)
00151 fidst_dict = pointer_dictionary_Create(37);
00152 fidstr = Font_to_hex(font->fid);
00153 binding = pointer_dictionary_Define(fidst_dict, fidstr, &exists);
00154 free(fidstr);
00155
00156 if (!exists)
00157 binding->value = (pointer) font;
00158 }
00159
00160
00161 XFontStruct *
00162 get_fontst_from_fid(fid)
00163 Font fid;
00164 {
00165 char *fidstr;
00166 pointer_dictionary_binding *binding;
00167 int exists;
00168
00169 fidstr = Font_to_hex(fid);
00170
00171 binding = pointer_dictionary_Define(fidst_dict, fidstr, &exists);
00172 free(fidstr);
00173 return ((XFontStruct *) binding->value);
00174 }
00175
00176 static XFontStruct *
00177 get_fontst(dpy, fontname)
00178 Display *dpy;
00179 char *fontname;
00180 {
00181 pointer_dictionary_binding *binding;
00182 int exists;
00183 XFontStruct *fontst;
00184
00185 if (!fontst_dict)
00186 fontst_dict = pointer_dictionary_Create(37);
00187 binding = pointer_dictionary_Define(fontst_dict, fontname, &exists);
00188
00189 if (exists) {
00190 return ((XFontStruct *) binding->value);
00191 }
00192 else {
00193 fontst = XLoadQueryFont(dpy, fontname);
00194 if (fontst == NULL) {
00195 pointer_dictionary_Delete(fontst_dict, binding);
00196 }
00197 else {
00198 binding->value = (pointer) fontst;
00199 add_fid(fontst);
00200 } return (fontst);
00201
00202 }
00203 }
00204
00205 static char *
00206 get_fontname(family, size, face)
00207 char *family;
00208 int size;
00209 int face;
00210 {
00211 char *fontname;
00212
00213 if (!(fontname = get_specific_fontname(family, size, face)))
00214 if (!(fontname = get_specific_fontname(family, size, ROMAN_FACE)))
00215 if (!(fontname = get_specific_fontname(family, MEDIUM_SIZE, face)))
00216 fontname = get_specific_fontname(family, MEDIUM_SIZE, ROMAN_FACE);
00217 return (fontname);
00218 }
00219
00220 static XFontStruct *
00221 complete_get_fontst(dpy, style, substyle, size, face)
00222 Display *dpy;
00223 string style;
00224 string substyle;
00225 int size;
00226 int face;
00227 {
00228 char *family, *fontname;
00229 XFontStruct *fontst;
00230
00231 if ((family = get_family(style, substyle)))
00232 if ((fontname = get_fontname(family, size, face)))
00233 if ((fontst = get_fontst(dpy, fontname)))
00234 return (fontst);
00235
00236 return (NULL);
00237 }
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247 XFontStruct *
00248 get_font(dpy, style, substyle, size, face)
00249 Display *dpy;
00250 string style;
00251 string substyle;
00252 int size;
00253 int face;
00254 {
00255 char *family, *fontname;
00256 XFontStruct *fontst;
00257
00258 if (size == SPECIAL_SIZE) {
00259
00260 if ((fontst = get_fontst(dpy, substyle)))
00261 return (fontst);
00262 }
00263 else {
00264 if ((family = get_family(style, substyle))) {
00265 if ((fontname = get_fontname(family, size, face)))
00266 if ((fontst = get_fontst(dpy, fontname)))
00267 return (fontst);
00268 }
00269 else {
00270 if ((fontname = get_fontname(substyle, size, face)))
00271 if ((fontst = get_fontst(dpy, fontname)))
00272 return (fontst);
00273 }
00274
00275
00276
00277
00278
00279
00280
00281 fontst = NULL;
00282 if (!(fontst = complete_get_fontst(dpy, style, "text", size, face)))
00283 if (!(fontst = complete_get_fontst(dpy, "default", substyle, size, face)))
00284 if (!(fontst = complete_get_fontst(dpy, "default", "text", size, face)))
00285 if ((fontname = get_fontname("default", size, face)))
00286 fontst = get_fontst(dpy, fontname);
00287 if (fontst)
00288 return (fontst);
00289 }
00290
00291
00292
00293 if ((fontst = get_fontst(dpy, "fixed")))
00294 return (fontst);
00295
00296
00297
00298 ERROR("Unable to open font \"fixed\". Aborting...");
00299 exit(1);
00300 }
00301
00302 #endif