]> git.donarmstrong.com Git - lilypond.git/blobdiff - lily/font-select.cc
Run grand-replace (issue 3765)
[lilypond.git] / lily / font-select.cc
index 2890b629dbe6de7a98011e6261df8b742b5cfdd5..6532a7c2869f6b259dbb0e52535a0e6c3e896042 100644 (file)
-#include "paper-def.hh"
-#include "font-interface.hh"
-#include "warn.hh"
-
-
 /*
-  TODO revise font handling.
-
+  This file is part of LilyPond, the GNU music typesetter.
 
-* relative sizes should relate to staff-space, eg.  font-staff-space
-= 1.2 ^ relative-size
+  Copyright (C) 2003--2014 Han-Wen Nienhuys <hanwen@xs4all.nl>
 
-* If a relative size is given, lily should magnify the closest
-design size font to match that. (ie. fonts should have variable
-scaling)
+  LilyPond is free software: you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation, either version 3 of the License, or
+  (at your option) any later version.
 
-(this requires that fonts are stored as (filename , designsize))
+  LilyPond is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+  GNU General Public License for more details.
 
+  You should have received a copy of the GNU General Public License
+  along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
+*/
 
-  
- */
+#include "dimensions.hh"
+#include "all-font-metrics.hh"
+#include "output-def.hh"
+#include "font-interface.hh"
+#include "warn.hh"
+#include "pango-font.hh"
+#include "main.hh"
 
+Font_metric *
+get_font_by_design_size (Output_def *layout, Real requested,
+                         SCM font_vector)
+{
+  int n = scm_c_vector_length (font_vector);
+  Real size = 1e6;
+  Real last_size = -1e6;
+  int i = 0;
+
+  SCM pango_description_string = SCM_EOL;
+  SCM last_pango_description_string = SCM_EOL;
+  for (; i < n; i++)
+    {
+      SCM entry = scm_c_vector_ref (font_vector, i);
+
+      if (scm_promise_p (entry) == SCM_BOOL_T)
+        {
+          Font_metric *fm = unsmob_metrics (scm_force (entry));
+          size = fm->design_size ();
+        }
+#if HAVE_PANGO_FT2
+      else if (scm_is_pair (entry)
+               && scm_is_number (scm_car (entry))
+               && scm_is_string (scm_cdr (entry)))
+        {
+          size = scm_to_double (scm_car (entry));
+          pango_description_string
+            = scm_cdr (entry);
+        }
+#endif
+
+      if (size > requested)
+        break;
+      last_size = size;
+      last_pango_description_string = pango_description_string;
+    }
 
+  if (i == n)
+    i = n - 1;
+  else if (i > 0)
+    {
+      if ((requested / last_size) < (size / requested))
+        {
+          i--;
+          size = last_size;
+          pango_description_string = last_pango_description_string;
+        }
+    }
 
+  Font_metric *fm = 0;
+  if (scm_is_string (pango_description_string))
+    {
+#if HAVE_PANGO_FT2
+      return find_pango_font (layout,
+                              pango_description_string,
+                              requested / size);
+#else
+      error ("Trying to retrieve pango font without HAVE_PANGO_FT2.");
+#endif
+    }
+  else
+    fm = unsmob_metrics (scm_force (scm_c_vector_ref (font_vector, i)));
 
-LY_DEFINE(ly_paper_get_font,"ly:paper-get-font", 2, 0, 0,
-         (SCM paper, SCM chain),
-         "Return a font metric satisfying the font-qualifiers in the alist chain @var{chain}.\n"
-"\n"
-"The font object represents the metric information of a font. Every font\n"
-"that is loaded into LilyPond can be accessed via Scheme. \n"
-"\n"
-"LilyPond only needs to know the dimension of glyph to be able to process\n"
-"them. This information is stored in font metric files. LilyPond can read\n"
-"two types of font-metrics: @TeX{} Font Metric files (TFM files) and\n"
-"Adobe Font Metric files (AFM files).  LilyPond will always try to load\n"
-"AFM files first since they are more versatile.\n"
-"\n"
-"An alist chain is a list of alists, containing grob properties.\n")
-{
-  Paper_def *pap = unsmob_paper (paper);
-  SCM_ASSERT_TYPE(pap, paper, SCM_ARG1, __FUNCTION__, "paper definition");
-  
-  Font_metric*fm = select_font (pap, chain);
-  return fm->self_scm();
+  return find_scaled_font (layout, fm, requested / size);
 }
 
-
-bool
-wild_compare (SCM field_val, SCM val)
+Font_metric *
+get_font_by_mag_step (Output_def *layout, Real requested_step,
+                      SCM font_vector, Real default_size)
 {
-  return (val == SCM_BOOL_F || field_val == ly_symbol2scm ("*") || field_val == val);
+  return get_font_by_design_size (layout, default_size
+                                  * pow (2.0, requested_step / 6.0),
+                                  font_vector);
 }
 
-/*
-  We can probably get more efficiency points if we preprocess FONTS
-  to make lookup easier.
- */
 SCM
-properties_to_font_name (SCM fonts, SCM alist_chain)
+properties_to_font_size_family (SCM fonts, SCM alist_chain)
 {
-  SCM shape = SCM_BOOL_F;
-  SCM family = SCM_BOOL_F;
-  SCM series = SCM_BOOL_F;
-
-  
-  SCM point_sz = ly_assoc_chain (ly_symbol2scm ("font-design-size"), alist_chain);
-  SCM rel_sz = SCM_BOOL_F;
-
-  shape = ly_assoc_chain (ly_symbol2scm ("font-shape"), alist_chain);
-  family = ly_assoc_chain (ly_symbol2scm ("font-family"), alist_chain);
-  series = ly_assoc_chain (ly_symbol2scm ("font-series"), alist_chain);
-
-  if (gh_pair_p (shape))
-    shape = ly_cdr (shape);
-  if (gh_pair_p (family))
-    family = ly_cdr (family);
-  if (gh_pair_p (series))
-    series = ly_cdr (series);
+  return scm_call_2 (ly_lily_module_constant ("lookup-font"), fonts,
+                     alist_chain);
+}
 
+Font_metric *
+select_encoded_font (Output_def *layout, SCM chain)
+{
+  SCM name = ly_chain_assoc_get (ly_symbol2scm ("font-name"), chain, SCM_BOOL_F);
 
-  if (gh_pair_p (point_sz))
-    point_sz = ly_cdr (point_sz);
-  else
+  if (!scm_is_string (name))
     {
-      rel_sz = ly_assoc_chain (ly_symbol2scm ("font-relative-size"), alist_chain);
-      if (gh_pair_p (rel_sz))
-       rel_sz = ly_cdr (rel_sz);
+      SCM fonts = layout->lookup_variable (ly_symbol2scm ("fonts"));
+      name = properties_to_font_size_family (fonts, chain);
     }
 
-  for (SCM s = fonts ; gh_pair_p (s); s = ly_cdr (s))
-    {
-      SCM qlist = ly_caar (s);
-
-      if (!wild_compare (scm_list_ref (qlist, gh_int2scm (1)), series))
-       continue;
-      if (!wild_compare (scm_list_ref (qlist, gh_int2scm (2)), shape))
-       continue;
-      if (!wild_compare (scm_list_ref (qlist, gh_int2scm (3)), family))
-       continue;
-  
-      if (point_sz == SCM_BOOL_F && !wild_compare (ly_car (qlist), rel_sz))
-       continue;
-          
-      SCM qname = ly_cdar (s);
-      return qname;
-    }
+#if HAVE_PANGO_FT2
+  if (scm_is_string (name))
+    return select_pango_font (layout, chain);
+  else
+#endif
+    if (scm_is_true (scm_instance_p (name)))
+      {
+        SCM base_size = scm_slot_ref (name, ly_symbol2scm ("default-size"));
+        SCM vec = scm_slot_ref (name, ly_symbol2scm ("size-vector"));
 
-  warning (_ ("couldn't find any font satisfying "));
-  scm_write (scm_list_n (point_sz, shape, series , family, rel_sz,
-                        SCM_UNDEFINED), scm_current_error_port ());
-  scm_flush (scm_current_error_port ());
-  return scm_makfrom0str ("cmr10");
-  
-}
+        Real req = robust_scm2double (ly_chain_assoc_get (ly_symbol2scm ("font-size"), chain, SCM_BOOL_F),
+                                      0.0);
 
+        return get_font_by_mag_step (layout, req, vec,
+                                     scm_to_double (base_size));
+      }
+
+  assert (0);
+  return 0;
+}
 
 Font_metric *
-select_font (Paper_def *paper, SCM chain)
+select_font (Output_def *layout, SCM chain)
 {
-  SCM name = ly_assoc_chain (ly_symbol2scm  ("font-name"), chain);
-  
-  if (!gh_pair_p (name) || !gh_string_p (gh_cdr (name)))
-    {
-      SCM fonts = paper->lookup_variable (ly_symbol2scm ("fonts"));
-      name = properties_to_font_name (fonts, chain);
-    }
-  else
-    name  = gh_cdr (name);
-  
-  SCM mag = ly_assoc_chain (ly_symbol2scm ("font-magnification"), chain);
-  
-  Real rmag = gh_pair_p (mag) && gh_number_p (gh_cdr (mag))
-    ? gh_scm2double (gh_cdr (mag)) : 1.0;
-  
-  Font_metric *fm = paper->find_font (name, rmag);
-  return fm;
+  return select_encoded_font (layout, chain);
 }