Browse Source

After testing, I have applied ucko's latest patch for providing access

to "core" fonts when XFT is selected. This allows contexts that need
bitmapped fonts (e.g. GL) to access a bitmap core font, even though
XFT2 does not generally provide bitmap font access.

STR #2214 refers.


git-svn-id: file:///fltk/svn/fltk/branches/branch-1.3@7474 ea41ed52-d2ee-0310-a9c1-e6b18d33e121
pull/49/head
Ian MacArthur 15 years ago
parent
commit
52470488ce
  1. 28
      FL/x.H
  2. 34
      src/fl_font_x.cxx
  3. 28
      src/fl_font_xft.cxx

28
FL/x.H

@ -65,11 +65,35 @@ extern FL_EXPORT int fl_screen; @@ -65,11 +65,35 @@ extern FL_EXPORT int fl_screen;
extern FL_EXPORT XVisualInfo *fl_visual;
extern FL_EXPORT Colormap fl_colormap;
// access to core fonts:
// This class provides a "smart pointer" that returns a pointer to an XFontStruct.
// The global variable fl_xfont can be called wherever a bitmap "core" font is
// needed, e.g. when rendering to a GL context under X11.
// With Xlib / X11 fonts, fl_xfont will return the current selected font.
// With XFT / X11 fonts, fl_xfont will attempt to return the bitmap "core" font most
// similar to (usually the same as) the current XFT font.
class Fl_XFont_On_Demand
{
public:
Fl_XFont_On_Demand(XFontStruct* p = NULL) : ptr(p) { }
Fl_XFont_On_Demand& operator=(const Fl_XFont_On_Demand& x)
{ ptr = x.ptr; return *this; }
Fl_XFont_On_Demand& operator=(XFontStruct* p)
{ ptr = p; return *this; }
XFontStruct* value();
operator XFontStruct*() { return value(); }
XFontStruct& operator*() { return *value(); }
XFontStruct* operator->() { return value(); }
bool operator==(const Fl_XFont_On_Demand& x) { return ptr == x.ptr; }
bool operator!=(const Fl_XFont_On_Demand& x) { return ptr != x.ptr; }
private:
XFontStruct *ptr;
};
extern FL_EXPORT Fl_XFont_On_Demand fl_xfont;
// drawing functions:
extern FL_EXPORT GC fl_gc;
extern FL_EXPORT Window fl_window;
//extern FL_EXPORT XFontStruct* fl_xfont;
extern FL_EXPORT XUtf8FontStruct* fl_xfont;
extern FL_EXPORT void *fl_xftfont;
FL_EXPORT ulong fl_xpixel(Fl_Color i);
FL_EXPORT ulong fl_xpixel(uchar r, uchar g, uchar b);

34
src/fl_font_x.cxx

@ -39,6 +39,7 @@ Fl_Font_Descriptor::Fl_Font_Descriptor(const char* name) { @@ -39,6 +39,7 @@ Fl_Font_Descriptor::Fl_Font_Descriptor(const char* name) {
}
Fl_Font_Descriptor* fl_fontsize;
Fl_XFont_On_Demand fl_xfont;
Fl_Font_Descriptor::~Fl_Font_Descriptor() {
# if HAVE_GL
@ -52,7 +53,10 @@ Fl_Font_Descriptor::~Fl_Font_Descriptor() { @@ -52,7 +53,10 @@ Fl_Font_Descriptor::~Fl_Font_Descriptor() {
// glDeleteLists(listbase+base,size);
// }
# endif
if (this == fl_fontsize) fl_fontsize = 0;
if (this == fl_fontsize) {
fl_fontsize = 0;
fl_xfont = 0;
}
XFreeUtf8FontStruct(fl_display, font);
}
@ -83,6 +87,8 @@ Fl_Fontdesc* fl_fonts = built_in_table; @@ -83,6 +87,8 @@ Fl_Fontdesc* fl_fonts = built_in_table;
#define MAXSIZE 32767
#define current_font (fl_fontsize->font)
// return dash number N, or pointer to ending null if none:
const char* fl_font_word(const char* p, int n) {
while (*p) {if (*p=='-') {if (!--n) break;} p++;}
@ -256,11 +262,13 @@ static Fl_Font_Descriptor* find(int fnum, int size) { @@ -256,11 +262,13 @@ static Fl_Font_Descriptor* find(int fnum, int size) {
Fl_Font fl_font_ = 0;
Fl_Fontsize fl_size_ = 0;
//XFontStruct* fl_xfont = 0;
XUtf8FontStruct* fl_xfont;
void *fl_xftfont = 0;
static GC font_gc;
XFontStruct* Fl_XFont_On_Demand::value() {
return ptr;
}
void Fl_Device::font(Fl_Font fnum, Fl_Fontsize size) {
if (fnum==-1) {
fl_font_ = 0; fl_size_ = 0;
@ -271,28 +279,28 @@ void Fl_Device::font(Fl_Font fnum, Fl_Fontsize size) { @@ -271,28 +279,28 @@ void Fl_Device::font(Fl_Font fnum, Fl_Fontsize size) {
Fl_Font_Descriptor* f = find(fnum, size);
if (f != fl_fontsize) {
fl_fontsize = f;
fl_xfont = f->font;
fl_xfont = current_font->fonts[0];
font_gc = 0;
}
}
int fl_height() {
if (fl_xfont) return (fl_xfont->ascent + fl_xfont->descent);
if (current_font) return (current_font->ascent + current_font->descent);
else return -1;
}
int fl_descent() {
if (fl_xfont) return fl_xfont->descent;
if (current_font) return current_font->descent;
else return -1;
}
double fl_width(const char* c, int n) {
if (fl_xfont) return (double) XUtf8TextWidth(fl_xfont, c, n);
if (current_font) return (double) XUtf8TextWidth(current_font, c, n);
else return -1;
}
double fl_width(unsigned int c) {
if (fl_xfont) return (double) XUtf8UcsWidth(fl_xfont, c);
if (current_font) return (double) XUtf8UcsWidth(current_font, c);
else return -1;
}
@ -312,12 +320,12 @@ void fl_text_extents(const char *c, int n, int &dx, int &dy, int &W, int &H) { @@ -312,12 +320,12 @@ void fl_text_extents(const char *c, int n, int &dx, int &dy, int &W, int &H) {
void Fl_Device::draw(const char* c, int n, int x, int y) {
if (font_gc != fl_gc) {
if (!fl_xfont) fl_font(FL_HELVETICA, 14);
if (!current_font) fl_font(FL_HELVETICA, 14);
font_gc = fl_gc;
XSetFont(fl_display, fl_gc, fl_xfont->fid);
XSetFont(fl_display, fl_gc, current_font->fid);
}
// XDrawString(fl_display, fl_window, fl_gc, x, y, c, n);
XUtf8DrawString(fl_display, fl_window, fl_xfont, fl_gc, x, y, c, n);
XUtf8DrawString(fl_display, fl_window, current_font, fl_gc, x, y, c, n);
}
void Fl_Device::draw(int angle, const char *str, int n, int x, int y) {
fprintf(stderr,"ROTATING TEXT NOT IMPLIMENTED\n");
@ -329,10 +337,10 @@ void Fl_Device::draw(int angle, const char *str, int n, int x, int y) { @@ -329,10 +337,10 @@ void Fl_Device::draw(int angle, const char *str, int n, int x, int y) {
void fl_rtl_draw(const char* c, int n, int x, int y) {
if (font_gc != fl_gc) {
if (!fl_xfont) fl_font(FL_HELVETICA, 12);
if (!current_font) fl_font(FL_HELVETICA, 12);
font_gc = fl_gc;
}
XUtf8DrawRtlString(fl_display, fl_window, fl_xfont, fl_gc, x, y, c, n);
XUtf8DrawRtlString(fl_display, fl_window, current_font, fl_gc, x, y, c, n);
}
#endif // FL_DOXYGEN
//

28
src/fl_font_xft.cxx

@ -94,8 +94,7 @@ Fl_Fontdesc* fl_fonts = built_in_table; @@ -94,8 +94,7 @@ Fl_Fontdesc* fl_fonts = built_in_table;
Fl_Font fl_font_ = 0;
Fl_Fontsize fl_size_ = 0;
int fl_angle_ = 0; // internal for rotating text support
//XFontStruct* fl_xfont = 0;
XUtf8FontStruct* fl_xfont = 0;
Fl_XFont_On_Demand fl_xfont;
void *fl_xftfont = 0;
//const char* fl_encoding_ = "iso8859-1";
const char* fl_encoding_ = "iso10646-1";
@ -128,6 +127,8 @@ void fl_font(Fl_Font fnum, Fl_Fontsize size, int angle) { @@ -128,6 +127,8 @@ void fl_font(Fl_Font fnum, Fl_Fontsize size, int angle) {
fl_fontsize = f;
#if XFT_MAJOR < 2
fl_xfont = f->font->u.core.font;
#else
fl_xfont = NULL; // invalidate
#endif // XFT_MAJOR < 2
fl_xftfont = (void*)f->font;
}
@ -372,17 +373,16 @@ void fl_text_extents(const char *c, int n, int &dx, int &dy, int &w, int &h) { @@ -372,17 +373,16 @@ void fl_text_extents(const char *c, int n, int &dx, int &dy, int &w, int &h) {
} // fl_text_extents
#if HAVE_GL
/* 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...
/* This code is used (mainly 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.
// for GL or other bitmap font needs (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
@ -392,8 +392,8 @@ void fl_text_extents(const char *c, int n, int &dx, int &dy, int &w, int &h) { @@ -392,8 +392,8 @@ void fl_text_extents(const char *c, int n, int &dx, int &dy, int &w, int &h) {
// 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, FC8, ubuntu8.04) this works well for the
// fltk "built-in" font names.
// NOTE: On my test boxes (FC6, FC7, FC8, ubuntu8.04, 9.04, 9.10) 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_;
@ -468,7 +468,11 @@ XFontStruct* fl_xxfont() { @@ -468,7 +468,11 @@ XFontStruct* fl_xxfont() {
return xftfont->u.core.font;
# endif // XFT_MAJOR > 1
}
#endif // HAVE_GL
XFontStruct* Fl_XFont_On_Demand::value() {
if (!ptr) ptr = fl_xxfont();
return ptr;
}
#if USE_OVERLAY
// Currently Xft does not work with colormapped visuals, so this probably

Loading…
Cancel
Save