mirror of
https://github.com/fltk/fltk.git
synced 2026-06-05 08:06:35 +08:00
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
This commit is contained in:
@@ -4,6 +4,7 @@ CHANGES IN FLTK 1.1.8
|
|||||||
STR #1457, STR #1458, STR #1460, STR #1481, STR #1578,
|
STR #1457, STR #1458, STR #1460, STR #1481, STR #1578,
|
||||||
STR #1639, STR #1645, STR #1644, STR #1792, STR #1793,
|
STR #1639, STR #1645, STR #1644, STR #1792, STR #1793,
|
||||||
STR #1742, STR #1777, STR #1794, STR #1827, STR #1843)
|
STR #1742, STR #1777, STR #1794, STR #1827, STR #1843)
|
||||||
|
- gl_font() support for Xft+X11 (STR #1809)
|
||||||
- Fl_Gl_Window::mode() needed to hide and show the window
|
- Fl_Gl_Window::mode() needed to hide and show the window
|
||||||
when toggling stereo mode (STR #1846)
|
when toggling stereo mode (STR #1846)
|
||||||
- Fl_Gl_Window::show() did not work reliably on Mac OS X
|
- Fl_Gl_Window::show() did not work reliably on Mac OS X
|
||||||
|
|||||||
+183
-26
@@ -3,7 +3,7 @@
|
|||||||
//
|
//
|
||||||
// Xft font code for the Fast Light Tool Kit (FLTK).
|
// 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
|
// This library is free software; you can redistribute it and/or
|
||||||
// modify it under the terms of the GNU Library General Public
|
// 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) {
|
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();
|
fl_open_display();
|
||||||
|
|
||||||
|
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 slant = XFT_SLANT_ROMAN;
|
||||||
int weight = XFT_WEIGHT_MEDIUM;
|
int weight = XFT_WEIGHT_MEDIUM;
|
||||||
// may be efficient, but this is non-obvious
|
|
||||||
|
/* 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++) {
|
switch (*name++) {
|
||||||
case 'I': slant = XFT_SLANT_ITALIC; break;
|
case 'I': slant = XFT_SLANT_ITALIC; break; // italic
|
||||||
case 'P': slant = XFT_SLANT_ITALIC;
|
case 'P': slant = XFT_SLANT_ITALIC; // bold-italic (falls-through)
|
||||||
case 'B': weight = XFT_WEIGHT_BOLD; break;
|
case 'B': weight = XFT_WEIGHT_BOLD; break; // bold
|
||||||
case ' ': break;
|
case ' ': break; // regular
|
||||||
default: name--;
|
default: name--; // no prefix, restore name
|
||||||
}
|
}
|
||||||
// this call is extremely slow...
|
|
||||||
return XftFontOpen(fl_display, fl_screen,
|
if(comma_count) { // multiple comma-spearated names were passed
|
||||||
XFT_FAMILY, XftTypeString, name,
|
char *local_name = strdup(name); // duplicate the full name so we can edit the copy
|
||||||
XFT_WEIGHT, XftTypeInteger, weight,
|
char *curr = local_name; // points to first name in string
|
||||||
XFT_SLANT, XftTypeInteger, slant,
|
char *nxt; // next name in string
|
||||||
XFT_ENCODING, XftTypeString, fl_encoding_,
|
do {
|
||||||
XFT_PIXEL_SIZE, XftTypeDouble, (double)fl_size_,
|
nxt = strchr(curr, ','); // find comma seperator
|
||||||
core ? XFT_CORE : 0, XftTypeBool, true,
|
if (nxt) {
|
||||||
XFT_RENDER, XftTypeBool, false,
|
*nxt = 0; // terminate first name
|
||||||
(void *)0);
|
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;
|
||||||
|
}
|
||||||
|
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) {
|
Fl_FontSize::Fl_FontSize(const char* name) {
|
||||||
encoding = fl_encoding_;
|
encoding = fl_encoding_;
|
||||||
@@ -184,18 +267,92 @@ double fl_width(uchar c) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#if HAVE_GL
|
#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() {
|
XFontStruct* fl_xxfont() {
|
||||||
# if XFT_MAJOR > 1
|
# if XFT_MAJOR > 1
|
||||||
// kludge!
|
// kludge! XFT 2 and later does not provide core fonts for us to use with GL
|
||||||
static XFontStruct* fixed = 0;
|
// try to load a bitmap X font instead
|
||||||
if (!fixed) fixed = XLoadQueryFont(fl_display, "fixed");
|
static XFontStruct* xgl_font = 0;
|
||||||
return fixed;
|
static int glsize = 0;
|
||||||
# else
|
static int glfont = -1;
|
||||||
if (current_font->core) return current_font->u.core.font;
|
// 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;
|
static XftFont* xftfont;
|
||||||
if (xftfont) XftFontClose (fl_display, 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;
|
return xftfont->u.core.font;
|
||||||
# endif // XFT_MAJOR > 1
|
# endif // XFT_MAJOR > 1
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user