summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMichael R Sweet <michael.r.sweet@gmail.com>2007-12-15 17:35:57 +0000
committerMichael R Sweet <michael.r.sweet@gmail.com>2007-12-15 17:35:57 +0000
commit83a8cba9a758dabcca786df8835bb8567304cb11 (patch)
tree5906c6ca4013e49a75e674049441fe2fe9f40124 /src
parent56cafe56c361014797cd6b6788a6fd2a05274768 (diff)
Merge gl_font() changes for Xft+X11 (STR #1809)
git-svn-id: file:///fltk/svn/fltk/branches/branch-1.1@5999 ea41ed52-d2ee-0310-a9c1-e6b18d33e121
Diffstat (limited to 'src')
-rw-r--r--src/fl_font_xft.cxx217
1 files changed, 187 insertions, 30 deletions
diff --git a/src/fl_font_xft.cxx b/src/fl_font_xft.cxx
index d84af32d6..cf1116843 100644
--- a/src/fl_font_xft.cxx
+++ b/src/fl_font_xft.cxx
@@ -3,7 +3,7 @@
//
// Xft font code for the Fast Light Tool Kit (FLTK).
//
-// Copyright 2001-2005 Bill Spitzak and others.
+// Copyright 2001-2007 Bill Spitzak and others.
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Library General Public
@@ -125,28 +125,111 @@ void fl_font(int fnum, int size) {
}
static XftFont* fontopen(const char* name, bool core) {
+ // Check: does it look like we have been passed an old-school XLFD fontname?
+ bool is_xlfd = false;
+ int hyphen_count = 0;
+ int comma_count = 0;
+ unsigned len = strlen(name);
+ if (len > 512) len = 512; // ensure we are not passed an unbounded font name
+ for(unsigned idx = 0; idx < len; idx++) {
+ if(name[idx] == '-') hyphen_count++; // check for XLFD hyphens
+ if(name[idx] == ',') comma_count++; // are there multiple names?
+ }
+ if(hyphen_count >= 14) is_xlfd = true; // Not a robust check, but good enough?
+
fl_open_display();
- int slant = XFT_SLANT_ROMAN;
- int weight = XFT_WEIGHT_MEDIUM;
- // may be efficient, but this is non-obvious
- switch (*name++) {
- case 'I': slant = XFT_SLANT_ITALIC; break;
- case 'P': slant = XFT_SLANT_ITALIC;
- case 'B': weight = XFT_WEIGHT_BOLD; break;
- case ' ': break;
- default: name--;
+
+ if(!is_xlfd) { // Not an XLFD - open as a XFT style name
+ XftFont *the_font; // the font we will return;
+ XftPattern *fnt_pat = XftPatternCreate(); // the pattern we will use for matching
+ int slant = XFT_SLANT_ROMAN;
+ int weight = XFT_WEIGHT_MEDIUM;
+
+ /* This "converts" FLTK-style font names back into "regular" names, extracting
+ * the BOLD and ITALIC codes as it does so - all FLTK font names are prefixed
+ * by 'I' (italic) 'B' (bold) 'P' (bold italic) or ' ' (regular) modifiers.
+ * This gives a fairly limited font selection ability, but is retained for
+ * compatability reasons. If you really need a more complex choice, you are best
+ * calling Fl::set_fonts(*) then selecting the font by font-index rather than by
+ * name anyway. Probably.
+ * If you want to load a font who's name does actually begin with I, B or P, you
+ * MUST use a leading space OR simply use lowercase for the name...
+ */
+ /* This may be efficient, but it is non-obvious. */
+ switch (*name++) {
+ case 'I': slant = XFT_SLANT_ITALIC; break; // italic
+ case 'P': slant = XFT_SLANT_ITALIC; // bold-italic (falls-through)
+ case 'B': weight = XFT_WEIGHT_BOLD; break; // bold
+ case ' ': break; // regular
+ default: name--; // no prefix, restore name
+ }
+
+ if(comma_count) { // multiple comma-spearated names were passed
+ char *local_name = strdup(name); // duplicate the full name so we can edit the copy
+ char *curr = local_name; // points to first name in string
+ char *nxt; // next name in string
+ do {
+ nxt = strchr(curr, ','); // find comma seperator
+ if (nxt) {
+ *nxt = 0; // terminate first name
+ nxt++; // first char of next name
+ }
+
+ // Add the current name to the match pattern
+ XftPatternAddString(fnt_pat, XFT_FAMILY, curr);
+
+ if(nxt) curr = nxt; // move onto next name (if it exists)
+ // Now do a cut-down version of the FLTK name conversion.
+ // NOTE: we only use the slant and weight of the first name,
+ // subsequent names we ignore this for... But we still need to do the check.
+ switch (*curr++) {
+ case 'I': break; // italic
+ case 'P': // bold-italic (falls-through)
+ case 'B': break; // bold
+ case ' ': break; // regular
+ default: curr--; // no prefix, restore name
+ }
+
+ comma_count--; // decrement name sections count
+ } while (comma_count >= 0);
+ free(local_name); // release our local copy of font names
+ }
+ else { // single name was passed - add it directly
+ XftPatternAddString(fnt_pat, XFT_FAMILY, name);
+ }
+
+ // Construct a match pattern for the font we want...
+ XftPatternAddInteger(fnt_pat, XFT_WEIGHT, weight);
+ XftPatternAddInteger(fnt_pat, XFT_SLANT, slant);
+ XftPatternAddDouble (fnt_pat, XFT_PIXEL_SIZE, (double)fl_size_);
+ XftPatternAddString (fnt_pat, XFT_ENCODING, fl_encoding_);
+ if (core) {
+ XftPatternAddBool(fnt_pat, XFT_CORE, FcTrue);
+ XftPatternAddBool(fnt_pat, XFT_RENDER, FcFalse);
+ }
+
+ XftPattern *match_pat; // the best available match on the system
+ XftResult match_result; // the result of our matching attempt
+ // query the system to find a match for this font
+ match_pat = XftFontMatch(fl_display, fl_screen, fnt_pat, &match_result);
+ // open the matched font
+ the_font = XftFontOpenPattern(fl_display, match_pat);
+ // Tidy up the resources we allocated
+ XftPatternDestroy(fnt_pat);
+// XftPatternDestroy(match_pat); // FontConfig will destroy this resource for us. We must not!
+ return the_font;
}
- // this call is extremely slow...
- return XftFontOpen(fl_display, fl_screen,
- XFT_FAMILY, XftTypeString, name,
- XFT_WEIGHT, XftTypeInteger, weight,
- XFT_SLANT, XftTypeInteger, slant,
- XFT_ENCODING, XftTypeString, fl_encoding_,
- XFT_PIXEL_SIZE, XftTypeDouble, (double)fl_size_,
- core ? XFT_CORE : 0, XftTypeBool, true,
- XFT_RENDER, XftTypeBool, false,
- (void *)0);
-}
+ else { // We were passed a font name in XLFD format
+ char *local_name = strdup(name);
+ if(comma_count) { // This means we were passed multiple XLFD's
+ char *pc = strchr(local_name, ',');
+ *pc = 0; // terminate the XLFD at the first comma
+ }
+ XftFont *the_font = XftFontOpenXlfd(fl_display, fl_screen, local_name);
+ free(local_name);
+ return the_font;
+ }
+} // end of fontopen
Fl_FontSize::Fl_FontSize(const char* name) {
encoding = fl_encoding_;
@@ -184,18 +267,92 @@ double fl_width(uchar c) {
}
#if HAVE_GL
-// This call is used by opengl to get a bitmapped font.
+/* This code is used by opengl to get a bitmapped font. The original XFT-1 code
+ * used XFT's "core" fonts methods to load an XFT font that was actually a
+ * X-bitmap font, that could then be readily used with GL.
+ * But XFT-2 does not provide that ability, and there is no easy method to use
+ * an XFT font directly with GL. So...
+*/
+
+# if XFT_MAJOR > 1
+// This function attempts, on XFT2 systems, to find a suitable "core" Xfont
+// for GL to use, since we dont have an XglUseXftFont(...) function.
+// There's probably a better way to do this. I can't believe it is this hard...
+// Anyway... This code attempts to make an XLFD out of the fltk-style font
+// name it is passed, then tries to load that font. Surprisingly, this quite
+// often works - boxes that have XFT generally also have a fontserver that
+// can serve TTF and other fonts to X, and so the font name that fltk makes
+// from the XFT name often also "exists" as an "core" X font...
+// If this code fails to load the requested font, it falls back through a
+// series of tried 'n tested alternatives, ultimately resorting to what the
+// original fltk code did.
+// NOTE:
+// On my test boxes (FC6, FC7) this works well for the fltk "built-in" font names.
+static XFontStruct* load_xfont_for_xft2(void) {
+ XFontStruct* xgl_font = 0;
+ int size = fl_size_;
+ char *weight = "medium"; // no specifc weight requested - accept any
+ char slant = 'r'; // regular non-italic by default
+ char xlfd[128]; // we will put our synthetic XLFD in here
+ char *pc = strdup(fl_fonts[fl_font_].name); // what font were we asked for?
+ char *name = pc; // keep a handle to the original name for freeing later
+ // Parse the "fltk-name" of the font
+ switch (*name++) {
+ case 'I': slant = 'i'; break; // italic
+ case 'P': slant = 'i'; // bold-italic (falls-through)
+ case 'B': weight = "bold"; break; // bold
+ case ' ': break; // regular
+ default: name--; // no prefix, restore name
+ }
+
+ // first, we do a query with no prefered size, to see if the font exists at all
+ snprintf(xlfd, 128, "-*-*%s*-%s-%c-*--*-*-*-*-*-*-*-*", name, weight, slant); // make up xlfd style name
+ xgl_font = XLoadQueryFont(fl_display, xlfd);
+ if(xgl_font) { // the face exists, but can we get it in a suitable size?
+ XFreeFont(fl_display, xgl_font); // release the non-sized version
+ snprintf(xlfd, 128, "-*-*%s*-%s-%c-*--*-%d-*-*-*-*-*-*", name, weight, slant, (size*10));
+ xgl_font = XLoadQueryFont(fl_display, xlfd); // attempt to load the font at the right size
+ }
+ free(pc); // release our copy of the font name
+
+ // if we have nothing loaded, try a generic proportional font
+ if(!xgl_font) {
+ snprintf(xlfd, 128, "-*-helvetica-*-%c-*--*-%d-*-*-*-*-*-*", slant, (size*10));
+ xgl_font = XLoadQueryFont(fl_display, xlfd);
+ }
+ // If that still didn't work, try this instead
+ if(!xgl_font) {
+ snprintf(xlfd, 128, "-*-courier-medium-%c-*--*-%d-*-*-*-*-*-*", slant, (size*10));
+ xgl_font = XLoadQueryFont(fl_display, xlfd);
+ }
+ // Last chance fallback - this usually loads something...
+ if (!xgl_font) xgl_font = XLoadQueryFont(fl_display, "fixed");
+
+ return xgl_font;
+} // end of load_xfont_for_xft2
+# endif
+
XFontStruct* fl_xxfont() {
# if XFT_MAJOR > 1
- // kludge!
- static XFontStruct* fixed = 0;
- if (!fixed) fixed = XLoadQueryFont(fl_display, "fixed");
- return fixed;
-# else
- if (current_font->core) return current_font->u.core.font;
+ // kludge! XFT 2 and later does not provide core fonts for us to use with GL
+ // try to load a bitmap X font instead
+ static XFontStruct* xgl_font = 0;
+ static int glsize = 0;
+ static int glfont = -1;
+ // Do we need to load a new font?
+ if ((!xgl_font) || (glsize != fl_size_) || (glfont != fl_font_)) {
+ if (xgl_font) XFreeFont(fl_display, xgl_font); // font already loaded, free it
+ glsize = fl_size_; // record current font size
+ glfont = fl_font_; // and face
+ // create a dummy XLFD for some font of the appropriate size...
+ xgl_font = load_xfont_for_xft2();
+ }
+ return xgl_font;
+# else // XFT-1 provides a means to load a "core" font directly
+ if (current_font->core) return current_font->u.core.font; // is the current font a "core" font? If so, use it.
static XftFont* xftfont;
if (xftfont) XftFontClose (fl_display, xftfont);
- xftfont = fontopen(fl_fonts[fl_font_].name, true);
+ xftfont = fontopen(fl_fonts[fl_font_].name, true); // else request XFT to load a suitable "core" font instead.
return xftfont->u.core.font;
# endif // XFT_MAJOR > 1
}
@@ -233,7 +390,7 @@ void fl_draw(const char *str, int n, int x, int y) {
#if USE_OVERLAY
XftDraw*& draw = fl_overlay ? draw_overlay : ::draw;
if (fl_overlay) {
- if (!draw)
+ if (!draw)
draw = XftDrawCreate(fl_display, draw_overlay_window = fl_window,
fl_overlay_visual->visual, fl_overlay_colormap);
else //if (draw_overlay_window != fl_window)