From a06087be8ee29a471b199df31b8aec281d656704 Mon Sep 17 00:00:00 2001
From: Christophe Geuzaine <cgeuzaine@ulg.ac.be>
Date: Mon, 3 Jun 2013 18:42:10 +0000
Subject: [PATCH] make font engine selection available in the GUI

---
 Common/Context.h            |  2 +-
 Common/DefaultOptions.h     |  2 ++
 Common/Options.cpp          | 31 +++++++++++++++++++++++++++++++
 Common/Options.h            |  1 +
 Fltk/FlGui.cpp              | 13 ++++++-------
 Fltk/drawContextFltk.h      |  1 +
 Fltk/drawContextFltkCairo.h |  1 +
 Fltk/optionWindow.cpp       | 23 +++++++++++++++++++----
 Graphics/drawContext.h      |  1 +
 9 files changed, 63 insertions(+), 12 deletions(-)

diff --git a/Common/Context.h b/Common/Context.h
index 2c530bd4b1..565c7b3181 100644
--- a/Common/Context.h
+++ b/Common/Context.h
@@ -180,7 +180,7 @@ class CTX {
   // fltk font size (and delta for palette windows)
   int fontSize, deltaFontSize;
   // font name, FLTK enum and size for opengl graphics
-  std::string glFont, glFontTitle;
+  std::string glFont, glFontTitle, glFontEngine;
   int glFontEnum, glFontEnumTitle, glFontSize, glFontSizeTitle;
   // point/line widths
   double pointSize, lineWidth;
diff --git a/Common/DefaultOptions.h b/Common/DefaultOptions.h
index e1b15db69d..62c6682bb8 100644
--- a/Common/DefaultOptions.h
+++ b/Common/DefaultOptions.h
@@ -61,6 +61,8 @@ StringXString GeneralOptions_String[] = {
 
   { F|O, "GraphicsFont" , opt_general_graphics_font , "Helvetica" ,
     "Font used in the graphic window" },
+  { F|O, "GraphicsFontEngine" , opt_general_graphics_font_engine , "Native" ,
+    "Set graphics font engine (Native, Cairo)" },
   { F|O, "GraphicsFontTitle" , opt_general_graphics_font_title , "Helvetica" ,
     "Font used in the graphic window for titles" },
 
diff --git a/Common/Options.cpp b/Common/Options.cpp
index 93509fa4bd..8a5378e394 100644
--- a/Common/Options.cpp
+++ b/Common/Options.cpp
@@ -51,6 +51,7 @@
 #include "clippingWindow.h"
 #include "onelabGroup.h"
 #include "viewButton.h"
+#include "drawContextFltkCairo.h"
 #endif
 
 // General routines for string options
@@ -1232,6 +1233,36 @@ std::string opt_general_graphics_font_title(OPT_ARGS_STR)
   return CTX::instance()->glFontTitle;
 }
 
+std::string opt_general_graphics_font_engine(OPT_ARGS_STR)
+{
+  if(action & GMSH_SET)
+    CTX::instance()->glFontEngine = val;
+
+#if defined(HAVE_FLTK)
+  if(action & GMSH_SET){
+    drawContextGlobal *old = drawContext::global();
+    if(!old || old->getName() != CTX::instance()->glFontEngine){
+#if defined(HAVE_CAIRO)
+      if(CTX::instance()->glFontEngine == "Cairo")
+        drawContext::setGlobal(new drawContextFltkCairo);
+      else
+#endif
+        drawContext::setGlobal(new drawContextFltk);
+      if(old) delete old;
+    }
+  }
+  if(FlGui::available() && (action & GMSH_GUI)){
+    int index = 0;
+#if defined(HAVE_CAIRO)
+    if(CTX::instance()->glFontEngine == "Cairo") index = 1;
+#endif
+    FlGui::instance()->options->general.choice[7]->value(index);
+  }
+#endif
+
+  return CTX::instance()->glFontEngine;
+}
+
 std::string opt_solver_socket_name(OPT_ARGS_STR)
 {
   if(action & GMSH_SET)
diff --git a/Common/Options.h b/Common/Options.h
index f4fccb8721..82d81c9100 100644
--- a/Common/Options.h
+++ b/Common/Options.h
@@ -51,6 +51,7 @@ std::string opt_general_watch_file_pattern(OPT_ARGS_STR);
 std::string opt_general_gui_theme(OPT_ARGS_STR);
 std::string opt_general_graphics_font(OPT_ARGS_STR);
 std::string opt_general_graphics_font_title(OPT_ARGS_STR);
+std::string opt_general_graphics_font_engine(OPT_ARGS_STR);
 std::string opt_solver_socket_name(OPT_ARGS_STR);
 std::string opt_solver_name(OPT_ARGS_STR);
 std::string opt_solver_name0(OPT_ARGS_STR);
diff --git a/Fltk/FlGui.cpp b/Fltk/FlGui.cpp
index 23866fcf65..fee6ad9a2b 100644
--- a/Fltk/FlGui.cpp
+++ b/Fltk/FlGui.cpp
@@ -234,17 +234,16 @@ FlGui::FlGui(int argc, char **argv)
   Fl::set_boxtype(GMSH_SIMPLE_RIGHT_BOX, simple_right_box_draw, 0, 0, 1, 0);
   Fl::set_boxtype(GMSH_SIMPLE_TOP_BOX, simple_top_box_draw, 0, 1, 0, 1);
 
-  if(CTX::instance()->gamepad)   Fl::add_timeout(5.,gamepad_handler , (void*)0);
+  // add gamepad handler
+  if(CTX::instance()->gamepad)
+    Fl::add_timeout(5.,gamepad_handler, (void*)0);
 
   // add global shortcuts
   Fl::add_handler(globalShortcut);
 
-  // set global fltk-dependent drawing functions
-#if defined(HAVE_CAIRO)
-  drawContext::setGlobal(new drawContextFltkCairo);
-#else
-  drawContext::setGlobal(new drawContextFltk);
-#endif
+  // make sure a global drawing context is setup
+  if(!drawContext::global())
+    drawContext::setGlobal(new drawContextFltk);
 
   // set default font size
   FL_NORMAL_SIZE = drawContext::global()->getFontSize();
diff --git a/Fltk/drawContextFltk.h b/Fltk/drawContextFltk.h
index 5dcacbfaa1..9715e867d0 100644
--- a/Fltk/drawContextFltk.h
+++ b/Fltk/drawContextFltk.h
@@ -149,6 +149,7 @@ class drawContextFltk : public drawContextGlobal{
     gl_texture_pile_height(1); // force font texture recomputation
 #endif
   }
+  std::string getName(){ return "Fltk"; }
 };
 
 #endif
diff --git a/Fltk/drawContextFltkCairo.h b/Fltk/drawContextFltkCairo.h
index 28f043cb19..ec257f705b 100644
--- a/Fltk/drawContextFltkCairo.h
+++ b/Fltk/drawContextFltkCairo.h
@@ -32,6 +32,7 @@ class drawContextFltkCairo : public drawContextFltk {
   //int getStringDescent();
   void drawString(const char *str);
   void setFont(int fontid, int fontsize);
+  std::string getName(){ return "Cairo"; }
 };
 
 #endif
diff --git a/Fltk/optionWindow.cpp b/Fltk/optionWindow.cpp
index ea3700af06..dbd261eb33 100644
--- a/Fltk/optionWindow.cpp
+++ b/Fltk/optionWindow.cpp
@@ -352,6 +352,7 @@ static void general_options_ok_cb(Fl_Widget *w, void *data)
   opt_general_vector_type(0, GMSH_SET, o->general.choice[0]->value() + 1);
   opt_general_graphics_font(0, GMSH_SET, o->general.choice[1]->text());
   opt_general_graphics_font_title(0, GMSH_SET, o->general.choice[6]->text());
+  opt_general_graphics_font_engine(0, GMSH_SET, o->general.choice[7]->text());
   opt_general_orthographic(0, GMSH_SET, !o->general.choice[2]->value());
   opt_general_axes(0, GMSH_SET, o->general.choice[4]->value());
   opt_general_background_gradient(0, GMSH_SET, o->general.choice[5]->value());
@@ -1674,25 +1675,39 @@ optionWindow::optionWindow(int deltaFontSize)
         (L + 2 * IW - 2 * WB, 2 * WB + 8 * BH, BB, BH, "Edit arrow");
       b->callback(general_arrow_param_cb);
 
+      static Fl_Menu_Item menu_font_engine[] = {
+        {"Native",   0, 0, 0},
+        {"Cairo",    0, 0, 0},
+        {0}
+      };
+      general.choice[7] = new Fl_Choice
+        (L + 2 * WB, 2 * WB + 9 * BH, IW, BH, "Font rendering engine");
+      general.choice[7]->menu(menu_font_engine);
+      general.choice[7]->align(FL_ALIGN_RIGHT);
+      general.choice[7]->callback(general_options_ok_cb);
+#if !defined(HAVE_CAIRO)
+      general.choice[7]->deactivate();
+#endif
+
       int w1 = (int)(4. * IW / 5.), w2 = IW - w1;
-      general.choice[1] = new Fl_Choice(L + 2 * WB, 2 * WB + 9 * BH, w1, BH);
+      general.choice[1] = new Fl_Choice(L + 2 * WB, 2 * WB + 10 * BH, w1, BH);
       general.choice[1]->menu(menu_font_names);
       general.choice[1]->align(FL_ALIGN_RIGHT);
       general.choice[1]->callback(general_options_ok_cb);
       general.value[12] = new Fl_Value_Input
-        (L + 2 * WB + w1, 2 * WB + 9 * BH, w2, BH, "Default font");
+        (L + 2 * WB + w1, 2 * WB + 10 * BH, w2, BH, "Default font");
       general.value[12]->minimum(5);
       general.value[12]->maximum(40);
       general.value[12]->step(1);
       general.value[12]->align(FL_ALIGN_RIGHT);
       general.value[12]->callback(general_options_ok_cb);
 
-      general.choice[6] = new Fl_Choice(L + 2 * WB, 2 * WB + 10 * BH, w1, BH);
+      general.choice[6] = new Fl_Choice(L + 2 * WB, 2 * WB + 11 * BH, w1, BH);
       general.choice[6]->menu(menu_font_names);
       general.choice[6]->align(FL_ALIGN_RIGHT);
       general.choice[6]->callback(general_options_ok_cb);
       general.value[28] = new Fl_Value_Input
-        (L + 2 * WB + w1, 2 * WB + 10 * BH, w2, BH, "Title font");
+        (L + 2 * WB + w1, 2 * WB + 11 * BH, w2, BH, "Title font");
       general.value[28]->minimum(5);
       general.value[28]->maximum(40);
       general.value[28]->step(1);
diff --git a/Graphics/drawContext.h b/Graphics/drawContext.h
index ff1c1fd4d5..8bbaef2f2d 100644
--- a/Graphics/drawContext.h
+++ b/Graphics/drawContext.h
@@ -98,6 +98,7 @@ class drawContextGlobal {
   virtual int getStringDescent(){ return 3; }
   virtual void drawString(const char *str){}
   virtual void resetFontTextures(){}
+  virtual std::string getName(){ return "None"; }
 };
 
 class drawContext {
-- 
GitLab