]> git.donarmstrong.com Git - lilypond.git/blobdiff - lily/font-config-scheme.cc
Run grand-replace for 2009.
[lilypond.git] / lily / font-config-scheme.cc
index 157d1bb9110ca8dc2ab316efc78e020a12d5913a..65ad68fe43adeee11d88405a6420d59c3f682dd5 100644 (file)
@@ -3,17 +3,23 @@
 
   source file of the GNU LilyPond music typesetter
 
-  (c) 2005--2006 Han-Wen Nienhuys <hanwen@xs4all.nl>
+  (c) 2005--2009 Han-Wen Nienhuys <hanwen@xs4all.nl>
 
 */
 
 #include "lily-guile.hh"
+#include "international.hh"
+#include "main.hh"
+#include "string-convert.hh"
+#include "warn.hh"
 
 #include <fontconfig/fontconfig.h>
 
-void
+string
 display_fontset (FcFontSet *fs)
 {
+  string retval;
+
   int j;
   for (j = 0; j < fs->nfont; j++)
     {
@@ -22,62 +28,136 @@ display_fontset (FcFontSet *fs)
 
       font = FcNameUnparse (fs->fonts[j]);
       if (FcPatternGetString (fs->fonts[j], FC_FILE, 0, &str) == FcResultMatch)
-       printf ("FILE %s\n", str);
+       retval += String_convert::form_string ("FILE %s\n", str);
+      if (FcPatternGetString (fs->fonts[j], FC_INDEX, 0, &str) == FcResultMatch)
+       retval += String_convert::form_string ("INDEX %s\n", str);
       if (FcPatternGetString (fs->fonts[j], FC_FAMILY, 0, &str) == FcResultMatch)
-       printf ("family %s\n ", str);
+       retval += String_convert::form_string ("family %s\n ", str);
       if (FcPatternGetString (fs->fonts[j],
                              "designsize", 0, &str) == FcResultMatch)
-       printf ("designsize %s\n ", str);
-      
-      printf ("%s\n", (const char*) font);
+       retval += String_convert::form_string ("designsize %s\n ", str);
+
+      retval += String_convert::form_string ("%s\n", (const char *)font);
       free (font);
     }
+
+  return retval;
 }
 
-void
-display_strlist (char const*what, FcStrList *slist)
+string
+display_strlist (char const *what, FcStrList *slist)
 {
+  string retval;
   while (FcChar8 *dir = FcStrListNext (slist))
     {
-      printf("%s: %s\n", what, dir);
+      retval += String_convert::form_string ("%s: %s\n", what, dir);
     }
+  return retval;
 }
 
-void
-display_dirs (FcConfig *fcc)
+string
+display_config (FcConfig *fcc)
 {
-  display_strlist ("config dir", FcConfigGetConfigDirs(fcc));
-  display_strlist ("font dir", FcConfigGetFontDirs(fcc));
+  string retval;
+  retval += display_strlist ("Config files", FcConfigGetConfigFiles (fcc));
+  retval += display_strlist ("Config dir", FcConfigGetConfigDirs (fcc));
+  retval += display_strlist ("Font dir", FcConfigGetFontDirs (fcc));
+  return retval;
 }
 
-void
+string
 display_list (FcConfig *fcc)
 {
-  FcPattern*pat = FcPatternCreate ();
+  FcPattern *pat = FcPatternCreate ();
 
   FcObjectSet *os = 0;
   if (!os)
-    os = FcObjectSetBuild (FC_FAMILY, FC_STYLE, (char *) 0);
+    os = FcObjectSetBuild (FC_FAMILY, FC_STYLE, (char *)0);
 
-  FcFontSet   * fs = FcFontList (fcc, pat, os);
+  FcFontSet *fs = FcFontList (fcc, pat, os);
   FcObjectSetDestroy (os);
   if (pat)
     FcPatternDestroy (pat);
 
+  string retval;
   if (fs)
     {
-      display_fontset (fs);
+      retval = display_fontset (fs);
       FcFontSetDestroy (fs);
     }
+  return retval;
 }
 
 
+LY_DEFINE (ly_font_config_get_font_file, "ly:font-config-get-font-file", 1, 0, 0,
+          (SCM name),
+          "Get the file for font @var{name}.")
+{
+  LY_ASSERT_TYPE (scm_is_string, name, 1);
+
+  FcPattern *pat = FcPatternCreate ();
+  FcValue val;
+
+  val.type = FcTypeString;
+  val.u.s = (const FcChar8 *)ly_scm2string (name).c_str (); // FC_SLANT_ITALIC;
+  FcPatternAdd (pat, FC_FAMILY, val, FcFalse);
+
+  FcResult result;
+  SCM scm_result = SCM_BOOL_F;
+
+  FcConfigSubstitute (NULL, pat, FcMatchFont);
+  FcDefaultSubstitute (pat);
+
+  pat = FcFontMatch (NULL, pat, &result);
+  FcChar8 *str = 0;
+  if (FcPatternGetString (pat, FC_FILE, 0, &str) == FcResultMatch)
+    scm_result = scm_from_locale_string ((char const *)str);
+
+  FcPatternDestroy (pat);
+
+  return scm_result;
+}
+       
 LY_DEFINE (ly_font_config_display_fonts, "ly:font-config-display-fonts", 0, 0, 0,
           (),
           "Dump a list of all fonts visible to FontConfig.")
 {
-  display_list (NULL);
-  display_dirs (NULL);
-  
+  string str = display_list (NULL);
+  str += display_config (NULL);
+
+  progress_indication (str);
+
+  return SCM_UNSPECIFIED;
+}
+
+LY_DEFINE (ly_font_config_add_directory, "ly:font-config-add-directory", 1, 0, 0,
+          (SCM dir),
+          "Add directory @var{dir} to FontConfig.")
+{
+  LY_ASSERT_TYPE (scm_is_string, dir, 1);
+
+  string d = ly_scm2string (dir);
+
+  if (!FcConfigAppFontAddDir (0, (const FcChar8 *)d.c_str ()))
+    error (_f ("failed adding font directory: %s", d.c_str ()));
+  else if (be_verbose_global)
+    message (_f ("adding font directory: %s", d.c_str ()));
+
+  return SCM_UNSPECIFIED;
+}
+
+LY_DEFINE (ly_font_config_add_font, "ly:font-config-add-font", 1, 0, 0,
+          (SCM font),
+          "Add font @var{font} to FontConfig.")
+{
+  LY_ASSERT_TYPE (scm_is_string, font, 1);
+
+  string f = ly_scm2string (font);
+
+  if (!FcConfigAppFontAddFile (0, (const FcChar8 *)f.c_str ()))
+    error (_f ("failed adding font file: %s", f.c_str ()));
+  else if (be_verbose_global)
+    message (_f ("adding font file: %s", f.c_str ()));
+
   return SCM_UNSPECIFIED;
 }