-#include "paper-def.hh"
-#include "font-interface.hh"
-#include "warn.hh"
-
-
-/*
- TODO revise font handling.
-
+/*
+ font-select.cc -- implement property -> font_metric routines.
-* relative sizes should relate to staff-space, eg. font-staff-space
-= 1.2 ^ relative-size
+ source file of the GNU LilyPond music typesetter
-* If a relative size is given, lily should magnify the closest
-design size font to match that. (ie. fonts should have variable
-scaling)
+ (c) 2003--2004 Han-Wen Nienhuys <hanwen@xs4all.nl>
-(this requires that fonts are stored as (filename , designsize))
-
-
-
*/
+#include <math.h>
+#include "all-font-metrics.hh"
+#include "output-def.hh"
+#include "font-interface.hh"
+#include "warn.hh"
+
+LY_DEFINE (ly_paper_get_font, "ly:paper-get-font", 2, 0, 0,
+ (SCM paper_smob, SCM chain),
-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")
+ "Return a font metric satisfying the font-qualifiers "
+ "in the alist chain @var{chain}.\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");
+ Output_def *paper = unsmob_output_def (paper_smob);
+ SCM_ASSERT_TYPE (paper, paper_smob, SCM_ARG1,
+ __FUNCTION__, "paper definition");
- Font_metric*fm = select_font (pap, chain);
- return fm->self_scm();
+ Font_metric *fm = select_font (paper, chain);
+ return fm->self_scm ();
}
+LY_DEFINE (ly_paper_get_number, "ly:paper-get-number", 2, 0, 0,
+ (SCM paper_smob, SCM name),
+ "Return the paper variable @var{name}.")
+{
+ Output_def *paper = unsmob_output_def (paper_smob);
+ SCM_ASSERT_TYPE (paper, paper_smob, SCM_ARG1,
+ __FUNCTION__, "paper definition");
+ return scm_make_real (paper->get_dimension (name));
+}
bool
wild_compare (SCM field_val, SCM val)
{
- return (val == SCM_BOOL_F || field_val == ly_symbol2scm ("*") || field_val == val);
+ return (val == SCM_BOOL_F
+ || field_val == ly_symbol2scm ("*")
+ || field_val == val);
}
/*
- We can probably get more efficiency points if we preprocess FONTS
- to make lookup easier.
+ TODO: this triggers a great number of font-loads (feta11 upto
+ parmesan23). We could make a Delayed_load_font_metric for which the
+ design size is specced in advance.
*/
-SCM
-properties_to_font_name (SCM fonts, SCM alist_chain)
+Font_metric *
+get_font_by_design_size (Output_def *paper, Real requested,
+ SCM font_vector, SCM input_encoding)
{
- SCM shape = SCM_BOOL_F;
- SCM family = SCM_BOOL_F;
- SCM series = SCM_BOOL_F;
-
+ int n = SCM_VECTOR_LENGTH (font_vector);
+ Real size = 1e6;
+ Real last_size = -1e6;
+ int i = 0;
- 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);
-
-
- if (gh_pair_p (point_sz))
- point_sz = ly_cdr (point_sz);
- else
+ for (; i < n; i++)
{
- 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 entry = SCM_VECTOR_REF (font_vector, i);
+ Font_metric *fm = unsmob_metrics (scm_force (entry));
+ size = fm->design_size ();
+
+ if (size > requested)
+ break;
+ last_size = size;
}
- for (SCM s = fonts ; gh_pair_p (s); s = ly_cdr (s))
+ if (i == n)
+ i = n - 1;
+ else if (i > 0)
{
- 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 ((requested / last_size) < (size / requested))
+ {
+ i--;
+ size = last_size;
+ }
}
- 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");
-
+ Font_metric *fm = unsmob_metrics (scm_force (SCM_VECTOR_REF (font_vector,
+ i)));
+ return find_scaled_font (paper, fm, requested / size, input_encoding);
+
}
+Font_metric*
+get_font_by_mag_step (Output_def *paper, Real requested_step,
+ SCM font_vector, Real default_size,
+ SCM input_encoding)
+{
+ return get_font_by_design_size (paper, default_size
+ * pow (2.0, requested_step / 6.0),
+ font_vector, input_encoding);
+}
+
+SCM
+properties_to_font_size_family (SCM fonts, SCM alist_chain)
+{
+ return scm_call_2 (ly_scheme_function ("lookup-font"), fonts, alist_chain);
+}
Font_metric *
-select_font (Paper_def *paper, SCM chain)
+select_encoded_font (Output_def *paper, SCM chain, SCM input_encoding)
{
SCM name = ly_assoc_chain (ly_symbol2scm ("font-name"), chain);
- if (!gh_pair_p (name) || !gh_string_p (gh_cdr (name)))
+ if (!ly_c_pair_p (name) || !ly_c_string_p (ly_cdr (name)))
{
SCM fonts = paper->lookup_variable (ly_symbol2scm ("fonts"));
- name = properties_to_font_name (fonts, chain);
+ name = properties_to_font_size_family (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;
+ name = ly_cdr (name);
+
+ if (ly_c_string_p (name))
+ {
+ SCM mag = ly_assoc_chain (ly_symbol2scm ("font-magnification"), chain);
+ Real rmag = (ly_c_pair_p (mag)
+ ? robust_scm2double (ly_cdr (mag), 1.0)
+ : 1);
+ Font_metric *fm = all_fonts_global->find_font (ly_scm2string (name));
+
+ return find_scaled_font (paper, fm, rmag, input_encoding);
+ }
+ else if (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"));
+
+ SCM font_size = ly_assoc_chain (ly_symbol2scm ("font-size"), chain);
+ Real req = 0;
+ if (ly_c_pair_p (font_size))
+ req = ly_scm2double (ly_cdr (font_size));
+
+ return get_font_by_mag_step (paper, req, vec, ly_scm2double (base_size),
+ input_encoding);
+ }
+
+ assert (0);
+ return 0;
+}
+
+Font_metric *
+select_font (Output_def *paper, SCM chain)
+{
+ return select_encoded_font (paper, chain, SCM_EOL);
}