Hello everyone,

This patch adds a new function (only linked explicitly with fltk_cairo
library):

   cairo_font_face_t* fl_cairo_font_reference(Fl_Font font)

Which makes it possible to access FLTK fonts from cairo code.
It's tested on Linux,Windows and OSX.
Test case cairo_test.cxx is updated with example usage.

The patch is generated against FLTK 1.3 subversion revision:
   Last Changed Rev: 6841
   Last Changed Date: 2009-08-03 08:26:32 +0200 (Mon, 03 Aug 2009

Please consider for inclusion.

Regards,
   Jakob Kemi



diff --git a/FL/Fl_Cairo.H b/FL/Fl_Cairo.H
index aeed1fd..aaabd37 100644
--- a/FL/Fl_Cairo.H
+++ b/FL/Fl_Cairo.H
@@ -37,6 +37,7 @@
 // Win32, Apple Quartz, X11
 
 # include <FL/Fl_Export.H>
+# include <FL/Enumerations.H>
 
 # if defined(USE_X11) // X11
 #  include <cairo-xlib.h>
@@ -87,6 +88,13 @@ private:
     void* window_, *gc_; // for keeping track internally of last win+gc treated
 };
 
+/**
+   Returns the a cairo font face created for the specified FLTK font.
+   Returned font face should be freed by calling cairo_font_face_destroy.
+   \note Only available when configure has the --enable-cairo option
+*/
+FL_EXPORT cairo_font_face_t*	fl_cairo_font_reference(Fl_Font font);
+
 /** @} */
 
 # endif // HAVE_CAIRO
diff --git a/cairo/Fl_Cairo.cxx b/cairo/Fl_Cairo.cxx
index 0272671..5dcbcec 100644
--- a/cairo/Fl_Cairo.cxx
+++ b/cairo/Fl_Cairo.cxx
@@ -26,11 +26,24 @@
 //
 
 #include <config.h>
+#include <FL/Fl_Export.H>
 
 #ifdef HAVE_CAIRO
 #include <FL/Fl.H>
 #include <FL/x.H>
 #include <FL/Fl_Window.H>
+#include <FL/Enumerations.H>
+#include <FL/fl_draw.H>
+#include "src/Fl_Font.H"
+
+#if defined(WIN32)
+#   include <cairo-win32.h>
+#elif (USE_XFT)
+#   include <X11/Xft/Xft.h>
+#   include <cairo-ft.h>
+#elif (__APPLE_QUARTZ__)
+#   include <cairo-quartz.h>
+#endif
 
 // static Fl module initialization :
 Fl_Cairo_State Fl::cairo_state_;	///< contains all necesary info for current cairo context mapping
@@ -155,9 +168,42 @@ cairo_t * Fl::cairo_make_current(void *gc, int W, int H) {
     Fl::cairo_cc(c);
     return c;
 }
+
+FL_EXPORT cairo_font_face_t* fl_cairo_font_reference(Fl_Font font) {
+
+    Fl_Font oldfont;
+    Fl_Fontsize oldsize;
+    cairo_font_face_t* ret;
+
+    oldfont = fl_font();
+    oldsize = fl_size();
+    
+    fl_font(font, FL_NORMAL_SIZE);
+
+    // fetch global font descriptor
+    Fl_Font_Descriptor* d = fl_fontsize;
+    
+#   if defined(WIN32)
+        ret = cairo_win32_font_face_create_for_hfont(d->fid);
+#   elif defined(__APPLE_QUARTZ__)
+		ATSUFontID fontid;
+		ByteCount tmp;
+		ATSUGetAttribute(d->style, kATSUFontTag, sizeof(ATSUFontID), &fontid, &tmp);
+		ret = cairo_quartz_font_face_create_for_atsu_font_id(fontid);
+#   elif (USE_XFT)
+        ret = cairo_ft_font_face_create_for_pattern(d->font->pattern);
+#   else
+#       error Unsupported font configuration!
+#   endif
+    
+    // Restore old font & size
+    fl_font(oldfont, oldsize);
+
+    return ret;
+}
+
 #else
 // just don't leave the libfltk_cairo lib empty to avoid warnings
-#include <FL/Fl_Export.H>
 FL_EXPORT int fltk_cairo_dummy() { return 1;}
 #endif // HAVE_CAIRO
 
diff --git a/test/cairo_test.cxx b/test/cairo_test.cxx
index 0914572..351c19b 100644
--- a/test/cairo_test.cxx
+++ b/test/cairo_test.cxx
@@ -43,7 +43,8 @@ const char* name[7] = {"X", "Y", "W", "H", "start", "end", "rotate"};
 
 
 static void centered_text(cairo_t* cr, double x0,double y0,double w0,double h0, const char * my_text) {
-    cairo_select_font_face (cr, "Sans", CAIRO_FONT_SLANT_OBLIQUE,CAIRO_FONT_WEIGHT_BOLD);
+    cairo_font_face_t* cf = fl_cairo_font_reference(FL_HELVETICA|FL_BOLD|FL_ITALIC);
+    cairo_set_font_face(cr, cf);
     cairo_set_source_rgba (cr, 0.9, 0.9, 0.4, 0.6);
     cairo_text_extents_t extents;
     cairo_text_extents (cr, my_text, &extents);
@@ -57,7 +58,7 @@ static void centered_text(cairo_t* cr, double x0,double y0,double w0,double h0,
     cairo_stroke (cr);
     cairo_set_line_width (cr, DEF_WIDTH);
 
-
+    cairo_font_face_destroy(cf);
 }
 
 static void round_button(cairo_t* cr, double x0, double y0, 

_______________________________________________
fltk-dev mailing list
[email protected]
http://lists.easysw.com/mailman/listinfo/fltk-dev

Reply via email to