# HG changeset patch
# User Pantxo Diribarne <pantxo.diribarne@gmail.com>
# Date 1507021881 -7200
#      Tue Oct 03 11:11:21 2017 +0200
# Node ID 45c209dd4935f9f5159c528667aa9f178cb4f217
# Parent  e250aeab3c85da520e14670f9559186a0d1c2a10
Provide default font files and use them (bug #52150)

* ft-text-renderer.cc (ft_manager::do_get_font): Look for FreeSans font files in
  oct_etc_dir by default when OCTAVE_FONT_DIR is not set. Use this font as 
  default ("*") or fallback when fontconfig is unable to find a suitable font
  by name. 

* run-octave.in: export OCTAVE_FONT_DIR

* etc/fonts: new directory containing default font files FreeSans.otf, 
  FreeSansBold.otf, FreeSansBoldOblique.otf and FreeSansOblique.otf. 

* etc/module.mk (etc_fontsdir, etc_fonts_DATA): New variables.  Include
$(etc_fonts_DATA) in the source distribution.

diff --git a/etc/fonts/FreeSans.otf b/etc/fonts/FreeSans.otf
new file mode 100644
index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..0b04a5d5bffce0728474cbedf51c4de19908e5e2
GIT binary patch
diff --git a/etc/fonts/FreeSansBold.otf b/etc/fonts/FreeSansBold.otf
new file mode 100644
index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..17865b108adb34c683318f73a419320827ca3489
GIT binary patch
diff --git a/etc/fonts/FreeSansBoldOblique.otf b/etc/fonts/FreeSansBoldOblique.otf
new file mode 100644
index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..3675cfc4e2a02e0cd81fdcf11b7f819ba80a8aa1
GIT binary patch
diff --git a/etc/fonts/FreeSansOblique.otf b/etc/fonts/FreeSansOblique.otf
new file mode 100644
index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..ec1f8f7308c34f76a052738f14622b983ac44501
GIT binary patch
diff --git a/etc/module.mk b/etc/module.mk
--- a/etc/module.mk
+++ b/etc/module.mk
@@ -21,6 +21,17 @@
   %reldir%/OLD-ChangeLogs/src-ChangeLog \
   %reldir%/OLD-ChangeLogs/test-ChangeLog
 
+etc_fontsdir = $(octetcdir)/fonts
+
+etc_fonts_DATA = \
+  %reldir%/fonts/FreeSans.otf \
+  %reldir%/fonts/FreeSansBold.otf \
+  %reldir%/fonts/FreeSansBoldOblique.otf \
+  %reldir%/fonts/FreeSansOblique.otf
+
+%canon_reldir%_EXTRA_DIST += \
+  $(etc_fonts_DATA)
+
 icon_IMAGE_FILES = \
   %reldir%/icons/octave-logo.svg \
   %reldir%/icons/octave-sombrero.png
diff --git a/libinterp/corefcn/ft-text-renderer.cc b/libinterp/corefcn/ft-text-renderer.cc
--- a/libinterp/corefcn/ft-text-renderer.cc
+++ b/libinterp/corefcn/ft-text-renderer.cc
@@ -54,7 +54,10 @@
 
 #include "singleton-cleanup.h"
 
+#include "defaults.h"
 #include "error.h"
+#include "file-ops.h"
+#include "oct-env.h"
 #include "pr-output.h"
 #include "text-renderer.h"
 
@@ -202,11 +205,37 @@
           return it->second;
         }
 #endif
+ 
+      static std::string font_dir;
 
+      if (font_dir.empty ())
+        {
+          font_dir = sys::env::getenv ("OCTAVE_FONT_DIR");
+
+          if (font_dir.empty ())
+            font_dir = config::oct_etc_dir () + sys::file_ops::dir_sep_str ()
+              + "fonts";
+        }
+
+
+      // Default font file
       std::string file;
 
+      if (! font_dir.empty ())
+        {
+          file = font_dir + octave::sys::file_ops::dir_sep_str () + "FreeSans";
+
+          if (weight == "bold")
+            file += "Bold";
+
+          if (angle == "italic" || angle == "oblique")
+            file += "Oblique";
+
+          file += ".otf";
+        }
+
 #if defined (HAVE_FONTCONFIG)
-      if (fontconfig_initialized)
+      if (name != "*" && fontconfig_initialized)
         {
           int fc_weight, fc_angle;
 
@@ -230,7 +259,7 @@
 
           FcPatternAddString (pat, FC_FAMILY,
                               (reinterpret_cast<const FcChar8 *>
-                               (name == "*" ? "sans" : name.c_str ())));
+                               (name.c_str ())));
 
           FcPatternAddInteger (pat, FC_WEIGHT, fc_weight);
           FcPatternAddInteger (pat, FC_SLANT, fc_angle);
@@ -254,7 +283,7 @@
                   file = reinterpret_cast<char *> (tmp);
                 }
               else
-                ::warning ("could not match any font: %s-%s-%s-%g",
+                ::warning ("could not match any font: %s-%s-%s-%g, using default font",
                            name.c_str (), weight.c_str (), angle.c_str (),
                            size);
 
@@ -267,15 +296,8 @@
 #endif
 
       if (file.empty ())
-        {
-#if defined (OCTAVE_USE_WINDOWS_API)
-          file = "C:/WINDOWS/Fonts/verdana.ttf";
-#else
-          // FIXME: find a "standard" font for UNIX platforms
-#endif
-        }
-
-      if (! file.empty ())
+        ::warning ("unable to find default font files");
+      else
         {
           if (FT_New_Face (library, file.c_str (), 0, &retval))
             ::warning ("ft_manager: unable to load font: %s", file.c_str ());
diff --git a/run-octave.in b/run-octave.in
--- a/run-octave.in
+++ b/run-octave.in
@@ -104,6 +104,7 @@
 OCTAVE_ARCHLIBDIR="$builddir/src"; export OCTAVE_ARCHLIBDIR
 OCTAVE_BINDIR="$builddir/src"; export OCTAVE_BINDIR
 OCTAVE_DEFAULT_QT_SETTINGS="$builddir/libgui/default-qt-settings"; export OCTAVE_DEFAULT_QT_SETTINGS
+OCTAVE_FONT_DIR="$top_srcdir/etc/fonts"; export OCTAVE_FONT_DIR
 OCTAVE_JAVA_DIR="$builddir/scripts/java"; export OCTAVE_JAVA_DIR
 OCTAVE_LOCALE_DIR="$builddir/libgui/languages"; export OCTAVE_LOCALE_DIR
 OCTAVE_SITE_INITFILE="$top_srcdir/scripts/startup/site-rcfile"; export OCTAVE_SITE_INITFILE
