FLTK logo

[Library] r7474 - in branches/branch-1.3: FL src

FLTK matrix user chat room
(using Element browser app)   FLTK gitter user chat room   GitHub FLTK Project   FLTK News RSS Feed  
  FLTK Apps      FLTK Library      Forums      Links     Login 
 All Forums  |  Back to fltk.commit  ]
 
Previous Message ]Next Message ]

[Library] r7474 - in branches/branch-1.3: FL src fltk-dev Apr 09, 2010  
 
Author: ianmacarthur
Date: 2010-04-09 09:46:45 -0700 (Fri, 09 Apr 2010)
New Revision: 7474
Log:
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.


Modified:
   branches/branch-1.3/FL/x.H
   branches/branch-1.3/src/fl_font_x.cxx
   branches/branch-1.3/src/fl_font_xft.cxx

Modified: branches/branch-1.3/FL/x.H
===================================================================
--- branches/branch-1.3/FL/x.H	2010-04-08 20:25:03 UTC (rev 7473)
+++ branches/branch-1.3/FL/x.H	2010-04-09 16:46:45 UTC (rev 7474)
@@ -65,11 +65,35 @@
 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);

Modified: branches/branch-1.3/src/fl_font_x.cxx
===================================================================
--- branches/branch-1.3/src/fl_font_x.cxx	2010-04-08 20:25:03 UTC (rev 7473)
+++ branches/branch-1.3/src/fl_font_x.cxx	2010-04-09 16:46:45 UTC (rev 7474)
@@ -39,6 +39,7 @@
 }
 
 Fl_Font_Descriptor* fl_fontsize;
+Fl_XFont_On_Demand fl_xfont;
 
 Fl_Font_Descriptor::~Fl_Font_Descriptor() {
 #  if HAVE_GL
@@ -52,7 +53,10 @@
 //  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 @@
 
 #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 @@
 
 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 @@
   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_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_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
 //

Modified: branches/branch-1.3/src/fl_font_xft.cxx
===================================================================
--- branches/branch-1.3/src/fl_font_xft.cxx	2010-04-08 20:25:03 UTC (rev 7473)
+++ branches/branch-1.3/src/fl_font_xft.cxx	2010-04-09 16:46:45 UTC (rev 7474)
@@ -94,8 +94,7 @@
 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 @@
   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 @@
 } // 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 @@
 // 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,8 +468,12 @@
   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
 // does not work unless you have a true-color overlay.

Direct Link to Message ]
 
     
Previous Message ]Next Message ]
 
 

Comments are owned by the poster. All other content is copyright 1998-2024 by Bill Spitzak and others. This project is hosted by The FLTK Team. Please report site problems to 'erco@seriss.com'.