]> git.donarmstrong.com Git - lilypond.git/blob - lily/paper-def.cc
* lily/scaled-font-metric.cc (text_dimension): move function from
[lilypond.git] / lily / paper-def.cc
1 /*
2   paper-def.cc -- implement Paper_def
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1997--2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9
10 #include <math.h>
11
12 #include "virtual-font-metric.hh"
13 #include "all-font-metrics.hh"
14 #include "string.hh"
15 #include "misc.hh"
16 #include "paper-def.hh"
17 #include "warn.hh"
18 #include "scaled-font-metric.hh"
19 #include "main.hh"
20 #include "scm-hash.hh"
21 #include "input-file-results.hh" // urg? header_global
22 #include "paper-outputter.hh"
23 #include "ly-module.hh"
24
25 /*
26   This is an almost empty thing. The only substantial thing this class
27   handles is scaling up and down to real-world dimensions (internally
28   dimensions are against global staff-space.)
29  */
30
31 Paper_def::Paper_def ()
32 {
33   scaled_fonts_ = SCM_EOL;
34   /*
35     don't remove above statement,  scm_make_hash_table may trigger GC.
36    */
37   scaled_fonts_ = scm_c_make_hash_table (11);
38 }
39
40 Paper_def::~Paper_def ()
41 {
42 }
43
44 Paper_def::Paper_def (Paper_def const&src)
45   : Music_output_def (src)
46 {
47   scaled_fonts_ = SCM_EOL;
48   /*
49     don't remove above statement,  scm_make_hash_table may trigger GC.
50    */
51   scaled_fonts_ = scm_c_make_hash_table (11);
52 }
53
54 void
55 Paper_def::derived_mark ()
56 {
57   scm_gc_mark (scaled_fonts_);
58 }
59
60 Real
61 Paper_def::get_dimension (SCM s) const
62 {
63   SCM val = lookup_variable (s);
64   SCM scale = lookup_variable (ly_symbol2scm ("outputscale"));
65   
66   Real sc = gh_scm2double (scale);
67   return gh_scm2double (val) / sc;
68 }
69
70 /*
71   FIXME. This is broken until we have a generic way of
72   putting lists inside the \paper block.
73  */
74 Interval
75 Paper_def::line_dimensions_int (int n) const
76 {
77   Real lw =  get_dimension (ly_symbol2scm ("linewidth"));
78   Real ind = n? 0.0:get_dimension (ly_symbol2scm ("indent"));
79
80   return Interval (ind, lw);
81 }
82
83
84
85
86 Paper_outputter*
87 Paper_def::get_paper_outputter (String outname)  const
88 {
89   progress_indication (_f ("paper output to `%s'...",
90                            outname == "-" ? String ("<stdout>") : outname));
91   progress_indication("\n");
92
93   global_input_file->target_strings_.push (outname);
94   Paper_outputter * po = new Paper_outputter (outname);
95   Path p = split_path (outname);
96   p.ext = "";
97   po->basename_ = p.to_string ();
98   return po;
99 }
100
101
102
103 Font_metric *
104 Paper_def::find_scaled_font (Font_metric *f, Real m)
105 {
106   SCM sizes = scm_hashq_ref (scaled_fonts_, f->self_scm (), SCM_BOOL_F);
107   if (sizes != SCM_BOOL_F)
108     {
109       SCM met = scm_assoc (gh_double2scm (m), sizes);
110       if (gh_pair_p (met))
111         return unsmob_metrics (ly_cdr (met));
112     }
113   else
114     {
115       sizes = SCM_EOL;
116     }
117   
118
119   /*
120     Hmm. We're chaining font - metrics. Should consider wether to merge
121     virtual-font and scaled_font.
122   */
123   SCM val = SCM_EOL;
124   if (Virtual_font_metric * vf = dynamic_cast<Virtual_font_metric*> (f))
125     {
126       /*
127         For fontify_atom (), the magnification and name must be known
128         at the same time. That's impossible for
129
130           Scaled (Virtual_font (Font1,Font2))
131
132         so we replace by
133
134           Virtual_font (Scaled (Font1), Scaled (Font2))
135         
136        */
137       SCM l = SCM_EOL;
138       SCM *t =  &l;
139       for (SCM s = vf->get_font_list (); gh_pair_p (s); s = gh_cdr (s))
140         {
141           Font_metric*scaled
142             = find_scaled_font (unsmob_metrics (gh_car (s)), m);
143           *t = scm_cons (scaled->self_scm (), SCM_EOL);
144           t = SCM_CDRLOC(*t);
145         }
146
147       vf = new Virtual_font_metric (l);
148       val = vf->self_scm ();
149     }
150   else
151     {
152       SCM scale_var = ly_module_lookup (scope_, ly_symbol2scm ("outputscale"));
153
154       m /= gh_scm2double (scm_variable_ref (scale_var));
155
156       val = Modified_font_metric::make_scaled_font_metric (f, m);
157     }
158
159   sizes = scm_acons (gh_double2scm (m), val, sizes);
160   scm_gc_unprotect_object (val);
161
162   scm_hashq_set_x (scaled_fonts_, f->self_scm (), sizes);
163   
164   return unsmob_metrics (val);
165 }
166
167
168
169 /*
170   Return alist to translate internally used fonts back to real-world
171   coordinates.  */
172 SCM
173 Paper_def::font_descriptions () const
174 {
175   SCM func = ly_scheme_function ("hash-table->alist");
176
177   SCM l = SCM_EOL;
178   for (SCM s = scm_call_1 (func, scaled_fonts_); gh_pair_p (s); s = ly_cdr (s))
179     {
180       SCM entry = gh_car (s);
181       for (SCM t = gh_cdr (entry); gh_pair_p (t); t  = gh_cdr (t))
182         {
183           Font_metric *fm= unsmob_metrics (gh_cdar (t));
184
185           if (dynamic_cast<Modified_font_metric*> (fm))
186             l = gh_cons (fm->self_scm (), l);
187         }
188     }
189   return l;
190 }
191
192 Paper_def* 
193 unsmob_paper (SCM x)
194 {
195   return dynamic_cast<Paper_def*> (unsmob_music_output_def (x));
196 }
197   
198
199 LY_DEFINE (ly_paper_def_p, "ly:paper-def?",
200            1, 0,0, (SCM def),
201            "Is @var{def} a paper definition?")
202 {
203   Paper_def *op = dynamic_cast<Paper_def*> (unsmob_music_output_def (def));
204
205   bool pap = op;
206   return gh_bool2scm (pap);
207 }