]> git.donarmstrong.com Git - lilypond.git/blob - lily/paper-def.cc
release: 1.3.102
[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--2000 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9 #include <math.h>
10
11 #include "all-font-metrics.hh"
12 #include "string.hh"
13 #include "misc.hh"
14 #include "paper-def.hh"
15 #include "debug.hh"
16 #include "scaled-font-metric.hh"
17 #include "main.hh"
18 #include "scope.hh"
19 #include "file-results.hh" // urg? header_global_p
20 #include "paper-stream.hh"
21
22 Paper_def::Paper_def ()
23 {
24   style_sheet_ = SCM_EOL;
25   scaled_fonts_ = SCM_EOL;
26 }
27
28 Paper_def::~Paper_def ()
29 {
30 }
31
32 Paper_def::Paper_def (Paper_def const&src)
33   : Music_output_def (src)
34 {
35   scaled_fonts_ = SCM_EOL;
36   style_sheet_ = src.style_sheet_;
37 }
38
39
40 Real
41 Paper_def::get_var (String s) const
42 {
43   return get_realvar (ly_symbol2scm (s.ch_C()));
44 }
45
46 SCM
47 Paper_def::get_scmvar (String s) const
48 {
49   return  scope_p_->scm_elem (ly_symbol2scm (s.ch_C()));
50 }
51
52 Real
53 Paper_def::get_realvar (SCM s) const
54 {
55   SCM val ;
56   if (!scope_p_->try_retrieve (s, &val))
57     {
58       programming_error ("unknown paper variable: " +  ly_symbol2string (s));
59       return 0.0;
60     }
61
62   Real sc = 1.0;
63   SCM ssc;
64   if (scope_p_->try_retrieve (ly_symbol2scm ("outputscale"), &ssc))
65     {
66       sc = gh_scm2double (ssc);
67     }
68   if (gh_number_p (val))
69     {
70       return gh_scm2double (val) / sc;
71     }
72   else
73     {
74       programming_error ("not a real variable");
75       return 0.0;
76     }
77 }
78
79 /*
80   FIXME. This is broken until we have a generic way of
81   putting lists inside the \paper block.
82  */
83 Interval
84 Paper_def::line_dimensions_int (int n) const
85 {
86   Real lw =  get_var ("linewidth");
87   Real ind = n? 0.0:get_var ("indent");
88
89   return Interval (ind, lw);
90 }
91
92
93
94 int Paper_def::default_count_i_ = 0;
95
96 int
97 Paper_def::get_next_default_count () const
98 {
99   return default_count_i_ ++;
100 }
101
102 void
103 Paper_def::reset_default_count()
104 {
105   default_count_i_ = 0;
106 }
107
108
109 Paper_stream*
110 Paper_def::paper_stream_p () const
111 {
112   String outname = base_output_str ();
113
114   if (outname != "-")
115     outname += String (".") + output_global_ch;
116   progress_indication (_f ("paper output to %s...",
117                            outname == "-" ? String ("<stdout>") : outname));
118
119   target_str_global_array.push (outname);
120   return new Paper_stream (outname);
121 }
122
123
124 String
125 Paper_def::base_output_str () const
126 {
127   String str = get_default_output ();
128
129   if (str.empty_b ())
130     {
131       str = default_outname_base_global;
132       int def = get_next_default_count ();
133       if (def)
134         str += "-" + to_str (def);
135     }
136   return str;
137 }
138
139 /*
140   todo: use symbols and hashtable idx?
141 */
142 Font_metric *
143 Paper_def::find_font (SCM fn, Real m)
144 {
145   SCM key = gh_cons (fn, gh_double2scm (m));
146   SCM met = scm_assoc (key, scaled_fonts_);
147
148   if (gh_pair_p (met))
149     return unsmob_metrics (gh_cdr (met));
150
151   SCM ssc;
152   if (scope_p_->try_retrieve (ly_symbol2scm ("outputscale"), &ssc))
153     {
154       m /= gh_scm2double (ssc);
155     }
156   
157   Font_metric*  f = all_fonts_global_p->find_font (ly_scm2string (fn));
158   SCM val = Scaled_font_metric::make_scaled_font_metric (f, m);
159   scaled_fonts_ = scm_acons (key, val, scaled_fonts_ );
160
161   scm_unprotect_object (val);
162
163   return dynamic_cast<Scaled_font_metric*> (unsmob_metrics (val));
164 }
165
166
167 /*
168   Return alist to translate internally used fonts back to real-world
169   coordinates.  */
170 SCM
171 Paper_def::font_descriptions ()const
172 {
173
174   
175   SCM l = SCM_EOL;
176   for (SCM s = scaled_fonts_; gh_pair_p (s); s = gh_cdr(s))
177     {
178       SCM desc = gh_caar (s);
179       SCM mdesc = unsmob_metrics (gh_cdar (s))->description_;
180
181       l = gh_cons (gh_cons (mdesc, desc), l);
182     }
183   return l;
184 }