DRC | 2ff39b8 | 2011-07-28 08:38:59 +0000 | [diff] [blame] | 1 | // |
| 2 | // "$Id: fl_font_xft.cxx 8442 2011-02-18 13:39:48Z manolo $" |
| 3 | // |
| 4 | // Xft font code for the Fast Light Tool Kit (FLTK). |
| 5 | // |
| 6 | // Copyright 2001-2011 Bill Spitzak and others. |
| 7 | // |
| 8 | // This library is free software; you can redistribute it and/or |
| 9 | // modify it under the terms of the GNU Library General Public |
| 10 | // License as published by the Free Software Foundation; either |
| 11 | // version 2 of the License, or (at your option) any later version. |
| 12 | // |
| 13 | // This library is distributed in the hope that it will be useful, |
| 14 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 | // Library General Public License for more details. |
| 17 | // |
| 18 | // You should have received a copy of the GNU Library General Public |
| 19 | // License along with this library; if not, write to the Free Software |
| 20 | // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
| 21 | // USA. |
| 22 | // |
| 23 | // Please report all bugs and problems on the following page: |
| 24 | // |
| 25 | // http://www.fltk.org/str.php |
| 26 | // |
| 27 | |
| 28 | // |
| 29 | // Draw fonts using Keith Packard's Xft library to provide anti- |
| 30 | // aliased text. Yow! |
| 31 | // |
| 32 | // Many thanks to Carl for making the original version of this. |
| 33 | // |
| 34 | // This font code only requires libXft to work. Contrary to popular |
| 35 | // belief there is no need to have FreeType, or the Xrender extension |
| 36 | // available to use this code. You will just get normal Xlib fonts |
| 37 | // (Xft calls them "core" fonts) The Xft algorithms for choosing |
| 38 | // these is about as good as the FLTK ones (I hope to fix it so it is |
| 39 | // exactly as good...), plus it can cache its results and share them |
| 40 | // between programs, so using this should be a win in all cases. Also |
| 41 | // it should be obvious by comparing this file and fl_font_x.cxx that |
| 42 | // it is a lot easier to program with Xft than with Xlib. |
| 43 | // |
| 44 | // Also, Xft supports UTF-8 text rendering directly, which will allow |
| 45 | // us to support UTF-8 on all platforms more easily. |
| 46 | // |
| 47 | // To actually get antialiasing you need the following: |
| 48 | // |
| 49 | // 1. You have XFree86 4 |
| 50 | // 2. You have the XRender extension |
| 51 | // 3. Your X device driver supports the render extension |
| 52 | // 4. You have libXft |
| 53 | // 5. Your libXft has FreeType2 support compiled in |
| 54 | // 6. You have the FreeType2 library |
| 55 | // |
| 56 | // Distributions that have XFree86 4.0.3 or later should have all of this... |
| 57 | // |
| 58 | // Unlike some other Xft packages, I tried to keep this simple and not |
| 59 | // to work around the current problems in Xft by making the "patterns" |
| 60 | // complicated. I believe doing this defeats our ability to improve Xft |
| 61 | // itself. You should edit the ~/.xftconfig file to "fix" things, there |
| 62 | // are several web pages of information on how to do this. |
| 63 | // |
| 64 | #ifndef FL_DOXYGEN |
| 65 | |
| 66 | #include <X11/Xft/Xft.h> |
| 67 | |
| 68 | #include <math.h> |
| 69 | |
| 70 | // The predefined fonts that FLTK has: |
| 71 | static Fl_Fontdesc built_in_table[] = { |
| 72 | #if 1 |
| 73 | {" sans"}, |
| 74 | {"Bsans"}, |
| 75 | {"Isans"}, |
| 76 | {"Psans"}, |
| 77 | {" mono"}, |
| 78 | {"Bmono"}, |
| 79 | {"Imono"}, |
| 80 | {"Pmono"}, |
| 81 | {" serif"}, |
| 82 | {"Bserif"}, |
| 83 | {"Iserif"}, |
| 84 | {"Pserif"}, |
| 85 | {" symbol"}, |
| 86 | {" screen"}, |
| 87 | {"Bscreen"}, |
| 88 | {" zapf dingbats"}, |
| 89 | #else |
| 90 | {" helvetica"}, |
| 91 | {"Bhelvetica"}, |
| 92 | {"Ihelvetica"}, |
| 93 | {"Phelvetica"}, |
| 94 | {" courier"}, |
| 95 | {"Bcourier"}, |
| 96 | {"Icourier"}, |
| 97 | {"Pcourier"}, |
| 98 | {" times"}, |
| 99 | {"Btimes"}, |
| 100 | {"Itimes"}, |
| 101 | {"Ptimes"}, |
| 102 | {" symbol"}, |
| 103 | {" lucidatypewriter"}, |
| 104 | {"Blucidatypewriter"}, |
| 105 | {" zapf dingbats"}, |
| 106 | #endif |
| 107 | }; |
| 108 | |
| 109 | Fl_Fontdesc* fl_fonts = built_in_table; |
| 110 | |
| 111 | Fl_XFont_On_Demand fl_xfont; |
| 112 | void *fl_xftfont = 0; |
| 113 | //const char* fl_encoding_ = "iso8859-1"; |
| 114 | const char* fl_encoding_ = "iso10646-1"; |
| 115 | |
| 116 | static void fl_xft_font(Fl_Xlib_Graphics_Driver *driver, Fl_Font fnum, Fl_Fontsize size, int angle) { |
| 117 | if (fnum==-1) { // special case to stop font caching |
| 118 | driver->Fl_Graphics_Driver::font(0, 0); |
| 119 | return; |
| 120 | } |
| 121 | Fl_Font_Descriptor* f = driver->font_descriptor(); |
| 122 | if (fnum == driver->Fl_Graphics_Driver::font() && size == driver->size() && f && f->angle == angle) |
| 123 | return; |
| 124 | driver->Fl_Graphics_Driver::font(fnum, size); |
| 125 | Fl_Fontdesc *font = fl_fonts + fnum; |
| 126 | // search the fontsizes we have generated already |
| 127 | for (f = font->first; f; f = f->next) { |
| 128 | if (f->size == size && f->angle == angle)// && !strcasecmp(f->encoding, fl_encoding_)) |
| 129 | break; |
| 130 | } |
| 131 | if (!f) { |
| 132 | f = new Fl_Font_Descriptor(font->name, size, angle); |
| 133 | f->next = font->first; |
| 134 | font->first = f; |
| 135 | } |
| 136 | driver->font_descriptor(f); |
| 137 | #if XFT_MAJOR < 2 |
| 138 | fl_xfont = f->font->u.core.font; |
| 139 | #else |
| 140 | fl_xfont = NULL; // invalidate |
| 141 | #endif // XFT_MAJOR < 2 |
| 142 | fl_xftfont = (void*)f->font; |
| 143 | } |
| 144 | |
| 145 | void Fl_Xlib_Graphics_Driver::font(Fl_Font fnum, Fl_Fontsize size) { |
| 146 | fl_xft_font(this,fnum,size,0); |
| 147 | } |
| 148 | |
| 149 | static XftFont* fontopen(const char* name, Fl_Fontsize size, bool core, int angle) { |
| 150 | // Check: does it look like we have been passed an old-school XLFD fontname? |
| 151 | bool is_xlfd = false; |
| 152 | int hyphen_count = 0; |
| 153 | int comma_count = 0; |
| 154 | unsigned len = strlen(name); |
| 155 | if (len > 512) len = 512; // ensure we are not passed an unbounded font name |
| 156 | for(unsigned idx = 0; idx < len; idx++) { |
| 157 | if(name[idx] == '-') hyphen_count++; // check for XLFD hyphens |
| 158 | if(name[idx] == ',') comma_count++; // are there multiple names? |
| 159 | } |
| 160 | if(hyphen_count >= 14) is_xlfd = true; // Not a robust check, but good enough? |
| 161 | |
| 162 | fl_open_display(); |
| 163 | |
| 164 | if(!is_xlfd) { // Not an XLFD - open as a XFT style name |
| 165 | XftFont *the_font; // the font we will return; |
| 166 | XftPattern *fnt_pat = XftPatternCreate(); // the pattern we will use for matching |
| 167 | int slant = XFT_SLANT_ROMAN; |
| 168 | int weight = XFT_WEIGHT_MEDIUM; |
| 169 | |
| 170 | /* This "converts" FLTK-style font names back into "regular" names, extracting |
| 171 | * the BOLD and ITALIC codes as it does so - all FLTK font names are prefixed |
| 172 | * by 'I' (italic) 'B' (bold) 'P' (bold italic) or ' ' (regular) modifiers. |
| 173 | * This gives a fairly limited font selection ability, but is retained for |
| 174 | * compatibility reasons. If you really need a more complex choice, you are best |
| 175 | * calling Fl::set_fonts(*) then selecting the font by font-index rather than by |
| 176 | * name anyway. Probably. |
| 177 | * If you want to load a font who's name does actually begin with I, B or P, you |
| 178 | * MUST use a leading space OR simply use lowercase for the name... |
| 179 | */ |
| 180 | /* This may be efficient, but it is non-obvious. */ |
| 181 | switch (*name++) { |
| 182 | case 'I': slant = XFT_SLANT_ITALIC; break; // italic |
| 183 | case 'P': slant = XFT_SLANT_ITALIC; // bold-italic (falls-through) |
| 184 | case 'B': weight = XFT_WEIGHT_BOLD; break; // bold |
| 185 | case ' ': break; // regular |
| 186 | default: name--; // no prefix, restore name |
| 187 | } |
| 188 | |
| 189 | if(comma_count) { // multiple comma-separated names were passed |
| 190 | char *local_name = strdup(name); // duplicate the full name so we can edit the copy |
| 191 | char *curr = local_name; // points to first name in string |
| 192 | char *nxt; // next name in string |
| 193 | do { |
| 194 | nxt = strchr(curr, ','); // find comma separator |
| 195 | if (nxt) { |
| 196 | *nxt = 0; // terminate first name |
| 197 | nxt++; // first char of next name |
| 198 | } |
| 199 | |
| 200 | // Add the current name to the match pattern |
| 201 | XftPatternAddString(fnt_pat, XFT_FAMILY, curr); |
| 202 | |
| 203 | if(nxt) curr = nxt; // move onto next name (if it exists) |
| 204 | // Now do a cut-down version of the FLTK name conversion. |
| 205 | // NOTE: we only use the slant and weight of the first name, |
| 206 | // subsequent names we ignore this for... But we still need to do the check. |
| 207 | switch (*curr++) { |
| 208 | case 'I': break; // italic |
| 209 | case 'P': // bold-italic (falls-through) |
| 210 | case 'B': break; // bold |
| 211 | case ' ': break; // regular |
| 212 | default: curr--; // no prefix, restore name |
| 213 | } |
| 214 | |
| 215 | comma_count--; // decrement name sections count |
| 216 | } while (comma_count >= 0); |
| 217 | free(local_name); // release our local copy of font names |
| 218 | } |
| 219 | else { // single name was passed - add it directly |
| 220 | XftPatternAddString(fnt_pat, XFT_FAMILY, name); |
| 221 | } |
| 222 | |
| 223 | // Construct a match pattern for the font we want... |
| 224 | XftPatternAddInteger(fnt_pat, XFT_WEIGHT, weight); |
| 225 | XftPatternAddInteger(fnt_pat, XFT_SLANT, slant); |
| 226 | XftPatternAddDouble (fnt_pat, XFT_PIXEL_SIZE, (double)size); |
| 227 | XftPatternAddString (fnt_pat, XFT_ENCODING, fl_encoding_); |
| 228 | |
| 229 | // rotate font if angle!=0 |
| 230 | if (angle !=0) { |
| 231 | XftMatrix m; |
| 232 | XftMatrixInit(&m); |
| 233 | XftMatrixRotate(&m,cos(M_PI*angle/180.),sin(M_PI*angle/180.)); |
| 234 | XftPatternAddMatrix (fnt_pat, XFT_MATRIX,&m); |
| 235 | } |
| 236 | |
| 237 | if (core) { |
| 238 | XftPatternAddBool(fnt_pat, XFT_CORE, FcTrue); |
| 239 | XftPatternAddBool(fnt_pat, XFT_RENDER, FcFalse); |
| 240 | } |
| 241 | |
| 242 | XftPattern *match_pat; // the best available match on the system |
| 243 | XftResult match_result; // the result of our matching attempt |
| 244 | |
| 245 | // query the system to find a match for this font |
| 246 | match_pat = XftFontMatch(fl_display, fl_screen, fnt_pat, &match_result); |
| 247 | |
| 248 | #if 0 // the XftResult never seems to get set to anything... abandon this code? |
| 249 | switch(match_result) { // how good a match is this font for our request? |
| 250 | case XftResultMatch: |
| 251 | puts("Object exists with the specified ID"); |
| 252 | break; |
| 253 | |
| 254 | case XftResultTypeMismatch: |
| 255 | puts("Object exists, but the type does not match"); |
| 256 | break; |
| 257 | |
| 258 | case XftResultNoId: |
| 259 | puts("Object exists, but has fewer values than specified"); |
| 260 | break; |
| 261 | |
| 262 | case FcResultOutOfMemory: |
| 263 | puts("FcResult: Malloc failed"); |
| 264 | break; |
| 265 | |
| 266 | case XftResultNoMatch: |
| 267 | puts("Object does not exist at all"); |
| 268 | break; |
| 269 | |
| 270 | default: |
| 271 | printf("Invalid XftResult status %d \n", match_result); |
| 272 | break; |
| 273 | } |
| 274 | #endif |
| 275 | |
| 276 | #if 0 // diagnostic to print the "full name" of the font we matched. This works. |
| 277 | FcChar8 *picked_name = FcNameUnparse(match_pat); |
| 278 | printf("Match: %s\n", picked_name); |
| 279 | free(picked_name); |
| 280 | #endif |
| 281 | |
| 282 | if (!match_pat) { |
| 283 | // last chance, just open any font in the right size |
| 284 | the_font = XftFontOpen (fl_display, fl_screen, |
| 285 | XFT_FAMILY, XftTypeString, "sans", |
| 286 | XFT_SIZE, XftTypeDouble, (double)size, |
| 287 | NULL); |
| 288 | XftPatternDestroy(fnt_pat); |
| 289 | if (!the_font) { |
| 290 | Fl::error("Unable to find fonts. Check your FontConfig configuration.\n"); |
| 291 | exit(1); |
| 292 | } |
| 293 | return the_font; |
| 294 | } |
| 295 | |
| 296 | // open the matched font |
| 297 | the_font = XftFontOpenPattern(fl_display, match_pat); |
| 298 | |
| 299 | #if 0 // diagnostic to print the "full name" of the font we actually opened. This works. |
| 300 | FcChar8 *picked_name2 = FcNameUnparse(the_font->pattern); |
| 301 | printf("Open : %s\n", picked_name2); |
| 302 | free(picked_name2); |
| 303 | #endif |
| 304 | |
| 305 | XftPatternDestroy(fnt_pat); |
| 306 | // XftPatternDestroy(match_pat); // FontConfig will destroy this resource for us. We must not! |
| 307 | |
| 308 | return the_font; |
| 309 | } |
| 310 | else { // We were passed a font name in XLFD format |
| 311 | /* OksiD's X font code could handle being passed a comma separated list |
| 312 | * of XLFD's. It then attempted to find which font was "best" from this list. |
| 313 | * But XftFontOpenXlfd can not do this, so if a list is passed, we just |
| 314 | * terminate it at the first comma. |
| 315 | * A "better" solution might be to use XftXlfdParse() on each of the passed |
| 316 | * XLFD's to construct a "super-pattern" that incorporates attributes from all |
| 317 | * XLFD's and use that to perform a XftFontMatch(). Maybe... |
| 318 | */ |
| 319 | char *local_name = strdup(name); |
| 320 | if(comma_count) { // This means we were passed multiple XLFD's |
| 321 | char *pc = strchr(local_name, ','); |
| 322 | *pc = 0; // terminate the XLFD at the first comma |
| 323 | } |
| 324 | XftFont *the_font = XftFontOpenXlfd(fl_display, fl_screen, local_name); |
| 325 | free(local_name); |
| 326 | #if 0 // diagnostic to print the "full name" of the font we actually opened. This works. |
| 327 | puts("Font Opened"); fflush(stdout); |
| 328 | FcChar8 *picked_name2 = FcNameUnparse(the_font->pattern); |
| 329 | printf("Open : %s\n", picked_name2); fflush(stdout); |
| 330 | free(picked_name2); |
| 331 | #endif |
| 332 | return the_font; |
| 333 | } |
| 334 | } // end of fontopen |
| 335 | |
| 336 | Fl_Font_Descriptor::Fl_Font_Descriptor(const char* name, Fl_Fontsize fsize, int fangle) { |
| 337 | // encoding = fl_encoding_; |
| 338 | size = fsize; |
| 339 | angle = fangle; |
| 340 | #if HAVE_GL |
| 341 | listbase = 0; |
| 342 | #endif // HAVE_GL |
| 343 | font = fontopen(name, fsize, false, angle); |
| 344 | } |
| 345 | |
| 346 | Fl_Font_Descriptor::~Fl_Font_Descriptor() { |
| 347 | if (this == fl_graphics_driver->font_descriptor()) fl_graphics_driver->font_descriptor(NULL); |
| 348 | // XftFontClose(fl_display, font); |
| 349 | } |
| 350 | |
| 351 | /* decodes the input UTF-8 string into a series of wchar_t characters. |
| 352 | n is set upon return to the number of characters. |
| 353 | Don't deallocate the returned memory. |
| 354 | */ |
| 355 | static const wchar_t *utf8reformat(const char *str, int& n) |
| 356 | { |
| 357 | static const wchar_t empty[] = {0}; |
| 358 | static wchar_t *buffer; |
| 359 | static int lbuf = 0; |
| 360 | int newn; |
| 361 | if (n == 0) return empty; |
| 362 | newn = fl_utf8towc(str, n, (wchar_t*)buffer, lbuf); |
| 363 | if (newn >= lbuf) { |
| 364 | lbuf = newn + 100; |
| 365 | if (buffer) free(buffer); |
| 366 | buffer = (wchar_t*)malloc(lbuf * sizeof(wchar_t)); |
| 367 | n = fl_utf8towc(str, n, (wchar_t*)buffer, lbuf); |
| 368 | } else { |
| 369 | n = newn; |
| 370 | } |
| 371 | return buffer; |
| 372 | } |
| 373 | |
| 374 | static void utf8extents(Fl_Font_Descriptor *desc, const char *str, int n, XGlyphInfo *extents) |
| 375 | { |
| 376 | memset(extents, 0, sizeof(XGlyphInfo)); |
| 377 | const wchar_t *buffer = utf8reformat(str, n); |
| 378 | #ifdef __CYGWIN__ |
| 379 | XftTextExtents16(fl_display, desc->font, (XftChar16 *)buffer, n, extents); |
| 380 | #else |
| 381 | XftTextExtents32(fl_display, desc->font, (XftChar32 *)buffer, n, extents); |
| 382 | #endif |
| 383 | } |
| 384 | |
| 385 | int Fl_Xlib_Graphics_Driver::height() { |
| 386 | if (font_descriptor()) return font_descriptor()->font->ascent + font_descriptor()->font->descent; |
| 387 | else return -1; |
| 388 | } |
| 389 | |
| 390 | int Fl_Xlib_Graphics_Driver::descent() { |
| 391 | if (font_descriptor()) return font_descriptor()->font->descent; |
| 392 | else return -1; |
| 393 | } |
| 394 | |
| 395 | double Fl_Xlib_Graphics_Driver::width(const char* str, int n) { |
| 396 | if (!font_descriptor()) return -1.0; |
| 397 | XGlyphInfo i; |
| 398 | utf8extents(font_descriptor(), str, n, &i); |
| 399 | return i.xOff; |
| 400 | } |
| 401 | |
| 402 | /*double fl_width(uchar c) { |
| 403 | return fl_graphics_driver->width((const char *)(&c), 1); |
| 404 | }*/ |
| 405 | |
| 406 | static double fl_xft_width(Fl_Font_Descriptor *desc, FcChar32 *str, int n) { |
| 407 | if (!desc) return -1.0; |
| 408 | XGlyphInfo i; |
| 409 | XftTextExtents32(fl_display, desc->font, str, n, &i); |
| 410 | return i.xOff; |
| 411 | } |
| 412 | |
| 413 | double Fl_Xlib_Graphics_Driver::width(unsigned int c) { |
| 414 | return fl_xft_width(font_descriptor(), (FcChar32 *)(&c), 1); |
| 415 | } |
| 416 | |
| 417 | void Fl_Xlib_Graphics_Driver::text_extents(const char *c, int n, int &dx, int &dy, int &w, int &h) { |
| 418 | if (!font_descriptor()) { |
| 419 | w = h = 0; |
| 420 | dx = dy = 0; |
| 421 | return; |
| 422 | } |
| 423 | XGlyphInfo gi; |
| 424 | utf8extents(font_descriptor(), c, n, &gi); |
| 425 | |
| 426 | w = gi.width; |
| 427 | h = gi.height; |
| 428 | dx = -gi.x; |
| 429 | dy = -gi.y; |
| 430 | } // fl_text_extents |
| 431 | |
| 432 | |
| 433 | /* This code is used (mainly by opengl) to get a bitmapped font. The |
| 434 | * original XFT-1 code used XFT's "core" fonts methods to load an XFT |
| 435 | * font that was actually a X-bitmap font, that could then be readily |
| 436 | * used with GL. But XFT-2 does not provide that ability, and there |
| 437 | * is no easy method to use an XFT font directly with GL. So... |
| 438 | */ |
| 439 | |
| 440 | # if XFT_MAJOR > 1 |
| 441 | // This function attempts, on XFT2 systems, to find a suitable "core" Xfont |
| 442 | // for GL or other bitmap font needs (we dont have an XglUseXftFont(...) function.) |
| 443 | // There's probably a better way to do this. I can't believe it is this hard... |
| 444 | // Anyway... This code attempts to make an XLFD out of the fltk-style font |
| 445 | // name it is passed, then tries to load that font. Surprisingly, this quite |
| 446 | // often works - boxes that have XFT generally also have a fontserver that |
| 447 | // can serve TTF and other fonts to X, and so the font name that fltk makes |
| 448 | // from the XFT name often also "exists" as an "core" X font... |
| 449 | // If this code fails to load the requested font, it falls back through a |
| 450 | // series of tried 'n tested alternatives, ultimately resorting to what the |
| 451 | // original fltk code did. |
| 452 | // NOTE: On my test boxes (FC6, FC7, FC8, ubuntu8.04, 9.04, 9.10) this works |
| 453 | // well for the fltk "built-in" font names. |
| 454 | static XFontStruct* load_xfont_for_xft2(Fl_Graphics_Driver *driver) { |
| 455 | XFontStruct* xgl_font = 0; |
| 456 | int size = driver->size(); |
| 457 | int fnum = driver->font(); |
| 458 | const char *wt_med = "medium"; |
| 459 | const char *wt_bold = "bold"; |
| 460 | const char *weight = wt_med; // no specifc weight requested - accept any |
| 461 | char slant = 'r'; // regular non-italic by default |
| 462 | char xlfd[128]; // we will put our synthetic XLFD in here |
| 463 | char *pc = strdup(fl_fonts[fnum].name); // what font were we asked for? |
| 464 | const char *name = pc; // keep a handle to the original name for freeing later |
| 465 | // Parse the "fltk-name" of the font |
| 466 | switch (*name++) { |
| 467 | case 'I': slant = 'i'; break; // italic |
| 468 | case 'P': slant = 'i'; // bold-italic (falls-through) |
| 469 | case 'B': weight = wt_bold; break; // bold |
| 470 | case ' ': break; // regular |
| 471 | default: name--; // no prefix, restore name |
| 472 | } |
| 473 | |
| 474 | // first, we do a query with no prefered size, to see if the font exists at all |
| 475 | snprintf(xlfd, 128, "-*-%s-%s-%c-*--*-*-*-*-*-*-*-*", name, weight, slant); // make up xlfd style name |
| 476 | xgl_font = XLoadQueryFont(fl_display, xlfd); |
| 477 | if(xgl_font) { // the face exists, but can we get it in a suitable size? |
| 478 | XFreeFont(fl_display, xgl_font); // release the non-sized version |
| 479 | snprintf(xlfd, 128, "-*-%s-%s-%c-*--*-%d-*-*-*-*-*-*", name, weight, slant, (size*10)); |
| 480 | xgl_font = XLoadQueryFont(fl_display, xlfd); // attempt to load the font at the right size |
| 481 | } |
| 482 | //puts(xlfd); |
| 483 | |
| 484 | // try alternative names |
| 485 | if (!xgl_font) { |
| 486 | if (!strcmp(name, "sans")) { |
| 487 | name = "helvetica"; |
| 488 | } else if (!strcmp(name, "mono")) { |
| 489 | name = "courier"; |
| 490 | } else if (!strcmp(name, "serif")) { |
| 491 | name = "times"; |
| 492 | } else if (!strcmp(name, "screen")) { |
| 493 | name = "lucidatypewriter"; |
| 494 | } else if (!strcmp(name, "dingbats")) { |
| 495 | name = "zapf dingbats"; |
| 496 | } |
| 497 | snprintf(xlfd, 128, "-*-*%s*-%s-%c-*--*-%d-*-*-*-*-*-*", name, weight, slant, (size*10)); |
| 498 | xgl_font = XLoadQueryFont(fl_display, xlfd); |
| 499 | } |
| 500 | free(pc); // release our copy of the font name |
| 501 | |
| 502 | // if we have nothing loaded, try a generic proportional font |
| 503 | if(!xgl_font) { |
| 504 | snprintf(xlfd, 128, "-*-helvetica-*-%c-*--*-%d-*-*-*-*-*-*", slant, (size*10)); |
| 505 | xgl_font = XLoadQueryFont(fl_display, xlfd); |
| 506 | } |
| 507 | // If that still didn't work, try this instead |
| 508 | if(!xgl_font) { |
| 509 | snprintf(xlfd, 128, "-*-courier-medium-%c-*--*-%d-*-*-*-*-*-*", slant, (size*10)); |
| 510 | xgl_font = XLoadQueryFont(fl_display, xlfd); |
| 511 | } |
| 512 | //printf("glf: %d\n%s\n%s\n", size, xlfd, fl_fonts[fl_font_].name); |
| 513 | //if(xgl_font) puts("ok"); |
| 514 | |
| 515 | // Last chance fallback - this usually loads something... |
| 516 | if (!xgl_font) xgl_font = XLoadQueryFont(fl_display, "fixed"); |
| 517 | |
| 518 | return xgl_font; |
| 519 | } // end of load_xfont_for_xft2 |
| 520 | # endif |
| 521 | |
| 522 | static XFontStruct* fl_xxfont(Fl_Graphics_Driver *driver) { |
| 523 | # if XFT_MAJOR > 1 |
| 524 | // kludge! XFT 2 and later does not provide core fonts for us to use with GL |
| 525 | // try to load a bitmap X font instead |
| 526 | static XFontStruct* xgl_font = 0; |
| 527 | static int glsize = 0; |
| 528 | static int glfont = -1; |
| 529 | // Do we need to load a new font? |
| 530 | if ((!xgl_font) || (glsize != driver->size()) || (glfont != driver->font())) { |
| 531 | // create a dummy XLFD for some font of the appropriate size... |
| 532 | if (xgl_font) XFreeFont(fl_display, xgl_font); // font already loaded, free it - this *might* be a Bad Idea |
| 533 | glsize = driver->size(); // record current font size |
| 534 | glfont = driver->font(); // and face |
| 535 | xgl_font = load_xfont_for_xft2(driver); |
| 536 | } |
| 537 | return xgl_font; |
| 538 | # else // XFT-1 provides a means to load a "core" font directly |
| 539 | if (driver->font_descriptor()->font->core) { |
| 540 | return driver->font_descriptor()->font->u.core.font; // is the current font a "core" font? If so, use it. |
| 541 | } |
| 542 | static XftFont* xftfont; |
| 543 | if (xftfont) XftFontClose (fl_display, xftfont); |
| 544 | xftfont = fontopen(fl_fonts[driver->font()].name, driver->size(), true, 0); // else request XFT to load a suitable "core" font instead. |
| 545 | return xftfont->u.core.font; |
| 546 | # endif // XFT_MAJOR > 1 |
| 547 | } |
| 548 | |
| 549 | XFontStruct* Fl_XFont_On_Demand::value() { |
| 550 | if (!ptr) ptr = fl_xxfont(fl_graphics_driver); |
| 551 | return ptr; |
| 552 | } |
| 553 | |
| 554 | #if USE_OVERLAY |
| 555 | // Currently Xft does not work with colormapped visuals, so this probably |
| 556 | // does not work unless you have a true-color overlay. |
| 557 | extern bool fl_overlay; |
| 558 | extern Colormap fl_overlay_colormap; |
| 559 | extern XVisualInfo* fl_overlay_visual; |
| 560 | #endif |
| 561 | |
| 562 | // For some reason Xft produces errors if you destroy a window whose id |
| 563 | // still exists in an XftDraw structure. It would be nice if this is not |
| 564 | // true, a lot of junk is needed to try to stop this: |
| 565 | |
| 566 | static XftDraw* draw_; |
| 567 | static Window draw_window; |
| 568 | #if USE_OVERLAY |
| 569 | static XftDraw* draw_overlay; |
| 570 | static Window draw_overlay_window; |
| 571 | #endif |
| 572 | |
| 573 | void fl_destroy_xft_draw(Window id) { |
| 574 | if (id == draw_window) |
| 575 | XftDrawChange(draw_, draw_window = fl_message_window); |
| 576 | #if USE_OVERLAY |
| 577 | if (id == draw_overlay_window) |
| 578 | XftDrawChange(draw_overlay, draw_overlay_window = fl_message_window); |
| 579 | #endif |
| 580 | } |
| 581 | |
| 582 | void Fl_Xlib_Graphics_Driver::draw(const char *str, int n, int x, int y) { |
| 583 | if ( !this->font_descriptor() ) { |
| 584 | this->font(FL_HELVETICA, FL_NORMAL_SIZE); |
| 585 | } |
| 586 | #if USE_OVERLAY |
| 587 | XftDraw*& draw_ = fl_overlay ? draw_overlay : ::draw_; |
| 588 | if (fl_overlay) { |
| 589 | if (!draw_) |
| 590 | draw_ = XftDrawCreate(fl_display, draw_overlay_window = fl_window, |
| 591 | fl_overlay_visual->visual, fl_overlay_colormap); |
| 592 | else //if (draw_overlay_window != fl_window) |
| 593 | XftDrawChange(draw_, draw_overlay_window = fl_window); |
| 594 | } else |
| 595 | #endif |
| 596 | if (!draw_) |
| 597 | draw_ = XftDrawCreate(fl_display, draw_window = fl_window, |
| 598 | fl_visual->visual, fl_colormap); |
| 599 | else //if (draw_window != fl_window) |
| 600 | XftDrawChange(draw_, draw_window = fl_window); |
| 601 | |
| 602 | Region region = fl_clip_region(); |
| 603 | if (region && XEmptyRegion(region)) return; |
| 604 | XftDrawSetClip(draw_, region); |
| 605 | |
| 606 | // Use fltk's color allocator, copy the results to match what |
| 607 | // XftCollorAllocValue returns: |
| 608 | XftColor color; |
| 609 | color.pixel = fl_xpixel(Fl_Graphics_Driver::color()); |
| 610 | uchar r,g,b; Fl::get_color(Fl_Graphics_Driver::color(), r,g,b); |
| 611 | color.color.red = ((int)r)*0x101; |
| 612 | color.color.green = ((int)g)*0x101; |
| 613 | color.color.blue = ((int)b)*0x101; |
| 614 | color.color.alpha = 0xffff; |
| 615 | |
| 616 | const wchar_t *buffer = utf8reformat(str, n); |
| 617 | #ifdef __CYGWIN__ |
| 618 | XftDrawString16(draw_, &color, font_descriptor()->font, x, y, (XftChar16 *)buffer, n); |
| 619 | #else |
| 620 | XftDrawString32(draw_, &color, font_descriptor()->font, x, y, (XftChar32 *)buffer, n); |
| 621 | #endif |
| 622 | } |
| 623 | |
| 624 | void Fl_Xlib_Graphics_Driver::draw(int angle, const char *str, int n, int x, int y) { |
| 625 | fl_xft_font(this, this->Fl_Graphics_Driver::font(), this->size(), angle); |
| 626 | this->draw(str, n, (int)x, (int)y); |
| 627 | fl_xft_font(this, this->Fl_Graphics_Driver::font(), this->size(), 0); |
| 628 | } |
| 629 | |
| 630 | static void fl_drawUCS4(Fl_Graphics_Driver *driver, const FcChar32 *str, int n, int x, int y) { |
| 631 | #if USE_OVERLAY |
| 632 | XftDraw*& draw_ = fl_overlay ? draw_overlay : ::draw_; |
| 633 | if (fl_overlay) { |
| 634 | if (!draw_) |
| 635 | draw_ = XftDrawCreate(fl_display, draw_overlay_window = fl_window, |
| 636 | fl_overlay_visual->visual, fl_overlay_colormap); |
| 637 | else //if (draw_overlay_window != fl_window) |
| 638 | XftDrawChange(draw_, draw_overlay_window = fl_window); |
| 639 | } else |
| 640 | #endif |
| 641 | if (!draw_) |
| 642 | draw_ = XftDrawCreate(fl_display, draw_window = fl_window, |
| 643 | fl_visual->visual, fl_colormap); |
| 644 | else //if (draw_window != fl_window) |
| 645 | XftDrawChange(draw_, draw_window = fl_window); |
| 646 | |
| 647 | Region region = fl_clip_region(); |
| 648 | if (region && XEmptyRegion(region)) return; |
| 649 | XftDrawSetClip(draw_, region); |
| 650 | |
| 651 | // Use fltk's color allocator, copy the results to match what |
| 652 | // XftCollorAllocValue returns: |
| 653 | XftColor color; |
| 654 | color.pixel = fl_xpixel(driver->color()); |
| 655 | uchar r,g,b; Fl::get_color(driver->color(), r,g,b); |
| 656 | color.color.red = ((int)r)*0x101; |
| 657 | color.color.green = ((int)g)*0x101; |
| 658 | color.color.blue = ((int)b)*0x101; |
| 659 | color.color.alpha = 0xffff; |
| 660 | |
| 661 | XftDrawString32(draw_, &color, driver->font_descriptor()->font, x, y, (FcChar32 *)str, n); |
| 662 | } |
| 663 | |
| 664 | |
| 665 | void Fl_Xlib_Graphics_Driver::rtl_draw(const char* c, int n, int x, int y) { |
| 666 | |
| 667 | #if defined(__GNUC__) |
| 668 | // FIXME: warning Need to improve this XFT right to left draw function |
| 669 | #endif /*__GNUC__*/ |
| 670 | |
| 671 | // This actually draws LtoR, but aligned to R edge with the glyph order reversed... |
| 672 | // but you can't just byte-rev a UTF-8 string, that isn't valid. |
| 673 | // You can reverse a UCS4 string though... |
| 674 | int num_chars, wid, utf_len = strlen(c); |
| 675 | FcChar8 *u8 = (FcChar8 *)c; |
| 676 | FcBool valid = FcUtf8Len(u8, utf_len, &num_chars, &wid); |
| 677 | if (!valid) |
| 678 | { |
| 679 | // badly formed Utf-8 input string |
| 680 | return; |
| 681 | } |
| 682 | if (num_chars < n) n = num_chars; // limit drawing to usable characters in input array |
| 683 | FcChar32 *ucs_txt = new FcChar32[n+1]; |
| 684 | FcChar32* pu; |
| 685 | int in, out, sz; |
| 686 | ucs_txt[n] = 0; |
| 687 | in = 0; out = n-1; |
| 688 | while ((out >= 0) && (utf_len > 0)) |
| 689 | { |
| 690 | pu = &ucs_txt[out]; |
| 691 | sz = FcUtf8ToUcs4(u8, pu, utf_len); |
| 692 | utf_len = utf_len - sz; |
| 693 | u8 = u8 + sz; |
| 694 | out = out - 1; |
| 695 | } |
| 696 | // Now we have a UCS4 version of the input text, reversed, in ucs_txt |
| 697 | int offs = (int)fl_xft_width(font_descriptor(), ucs_txt, n); |
| 698 | fl_drawUCS4(this, ucs_txt, n, (x-offs), y); |
| 699 | |
| 700 | delete[] ucs_txt; |
| 701 | } |
| 702 | #endif |
| 703 | |
| 704 | // |
| 705 | // End of "$Id: fl_font_xft.cxx 8442 2011-02-18 13:39:48Z manolo $" |
| 706 | // |