]> git.donarmstrong.com Git - lilypond.git/blob - lily/paper-def.cc
d3526bbfce5e33ef55650f8b58ccacdb7e8e03ff
[lilypond.git] / lily / paper-def.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2004--2014 Han-Wen Nienhuys <hanwen@xs4all.nl>
5
6   LilyPond is free software: you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation, either version 3 of the License, or
9   (at your option) any later version.
10
11   LilyPond is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "dimensions.hh"
21 #include "libc-extension.hh"
22 #include "output-def.hh"
23 #include "modified-font-metric.hh"
24 #include "pango-font.hh"
25 #include "all-font-metrics.hh"
26
27 Real
28 output_scale (Output_def *od)
29 {
30   return scm_to_double (od->lookup_variable (ly_symbol2scm ("output-scale")));
31 }
32
33 SCM
34 get_font_table (Output_def *def)
35 {
36   SCM font_table = def->lookup_variable (ly_symbol2scm ("scaled-fonts"));
37   if (scm_hash_table_p (font_table) != SCM_BOOL_T)
38     {
39       font_table = scm_c_make_hash_table (11);
40       def->set_variable (ly_symbol2scm ("scaled-fonts"), font_table);
41     }
42   return font_table;
43 }
44
45 SCM
46 get_pango_font_table (Output_def *def)
47 {
48   SCM font_table = def->lookup_variable (ly_symbol2scm ("pango-fonts"));
49   if (scm_hash_table_p (font_table) != SCM_BOOL_T)
50     {
51       font_table = scm_c_make_hash_table (11);
52       def->set_variable (ly_symbol2scm ("pango-fonts"), font_table);
53     }
54   return font_table;
55 }
56
57 /* TODO: should add nesting for Output_def here too. */
58 Font_metric *
59 find_scaled_font (Output_def *mod, Font_metric *f, Real m)
60 {
61   if (mod->parent_)
62     return find_scaled_font (mod->parent_, f, m);
63
64   Real lookup_mag = m / output_scale (mod);
65
66   SCM font_table = get_font_table (mod);
67   SCM sizes = scm_hashq_ref (font_table, f->self_scm (), SCM_EOL);
68   SCM handle = scm_assoc (scm_from_double (lookup_mag), sizes);
69   if (scm_is_pair (handle))
70     return unsmob_metrics (scm_cdr (handle));
71
72   SCM val = Modified_font_metric::make_scaled_font_metric (f, lookup_mag);
73
74   sizes = scm_acons (scm_from_double (lookup_mag), val, sizes);
75   unsmob_metrics (val)->unprotect ();
76   scm_hashq_set_x (font_table, f->self_scm (), sizes);
77   return unsmob_metrics (val);
78 }
79
80 Font_metric *
81 find_pango_font (Output_def *layout, SCM descr, Real factor)
82 {
83   if (layout->parent_)
84     return find_pango_font (layout->parent_, descr, factor);
85
86   SCM table = get_pango_font_table (layout);
87   SCM sizes = scm_hash_ref (table, descr, SCM_EOL);
88   SCM size_key = scm_from_double (factor);
89   SCM handle = scm_assoc (size_key, sizes);
90   if (scm_is_pair (handle))
91     return unsmob_metrics (scm_cdr (handle));
92
93   PangoFontDescription *description
94     = pango_font_description_from_string (scm_i_string_chars (descr));
95
96   pango_font_description_set_size
97     (description,
98      gint (my_round (factor * pango_font_description_get_size (description))));
99
100   Font_metric *fm = all_fonts_global->find_pango_font (description,
101                                                        output_scale (layout));
102
103   pango_font_description_free (description);
104
105   sizes = scm_acons (size_key, fm->self_scm (), sizes);
106   scm_hash_set_x (table, descr, sizes);
107
108   return fm;
109 }
110
111 /* TODO: this is a nasty interface. During formatting,
112    the Output_def should be scaled to the output_scale_
113    specified in the toplevel Output_def.  */
114 Output_def *
115 scale_output_def (Output_def *o, Real amount)
116 {
117   SCM proc = ly_lily_module_constant ("scale-layout");
118   SCM new_pap = scm_call_2 (proc, o->self_scm (), scm_from_double (amount));
119
120   o = unsmob_output_def (new_pap);
121   o->protect ();
122   return o;
123 }
124