1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3 
4   Copyright (C) 2005--2021 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 "score.hh"
21 
22 #include "music.hh"
23 #include "output-def.hh"
24 #include "global-context.hh"
25 #include "music-output.hh"
26 
27 LY_DEFINE (ly_make_score, "ly:make-score",
28            1, 0, 0,
29            (SCM music),
30            "Return score with @var{music} encapsulated in it.")
31 {
32   LY_ASSERT_SMOB (Music, music, 1);
33 
34   Score *score = new Score;
35   score->set_music (music);
36 
37   return score->unprotect ();
38 }
39 
40 LY_DEFINE (ly_score_output_defs, "ly:score-output-defs",
41            1, 0, 0, (SCM score),
42            "All output definitions in a score.")
43 {
44   auto *const sc = LY_ASSERT_SMOB (Score, score, 1);
45 
46   SCM l = SCM_EOL;
47   for (vsize i = 0; i < sc->defs_.size (); i++)
48     l = scm_cons (sc->defs_[i]->self_scm (), l);
49   return scm_reverse_x (l, SCM_EOL);
50 }
51 
52 LY_DEFINE (ly_score_add_output_def_x, "ly:score-add-output-def!",
53            2, 0, 0, (SCM score, SCM def),
54            "Add an output definition @var{def} to @var{score}.")
55 {
56   auto *const sc = LY_ASSERT_SMOB (Score, score, 1);
57   auto *const output_def = LY_ASSERT_SMOB (Output_def, def, 2);
58   sc->add_output_def (output_def);
59   return SCM_UNSPECIFIED;
60 }
61 
62 LY_DEFINE (ly_score_header, "ly:score-header",
63            1, 0, 0, (SCM score),
64            "Return score header.")
65 {
66   auto *const sc = LY_ASSERT_SMOB (Score, score, 1);
67   return sc->get_header ();
68 }
69 
70 LY_DEFINE (ly_score_set_header_x, "ly:score-set-header!",
71            2, 0, 0, (SCM score, SCM module),
72            "Set the score header.")
73 {
74   auto *const sc = LY_ASSERT_SMOB (Score, score, 1);
75   SCM_ASSERT_TYPE (ly_is_module (module), module, SCM_ARG2, __FUNCTION__,
76                    "module");
77 
78   sc->set_header (module);
79   return SCM_UNSPECIFIED;
80 }
81 
82 LY_DEFINE (ly_score_music, "ly:score-music",
83            1, 0, 0, (SCM score),
84            "Return score music.")
85 {
86   auto *const sc = LY_ASSERT_SMOB (Score, score, 1);
87   return sc->get_music ();
88 }
89 
90 LY_DEFINE (ly_score_error_p, "ly:score-error?",
91            1, 0, 0, (SCM score),
92            "Was there an error in the score?")
93 {
94   auto *const sc = LY_ASSERT_SMOB (Score, score, 1);
95   return scm_from_bool (sc->error_found_);
96 }
97 
98 LY_DEFINE (ly_score_embedded_format, "ly:score-embedded-format",
99            2, 0, 0, (SCM score, SCM layout),
100            "Run @var{score} through @var{layout} (an output definition)"
101            " scaled to correct @code{output-scale} already, returning a list"
102            " of layout lines.")
103 {
104   auto *const sc = LY_ASSERT_SMOB (Score, score, 1);
105   auto *const od = LY_ASSERT_SMOB (Output_def, layout, 2);
106 
107   if (sc->error_found_)
108     return SCM_EOL;
109 
110   Output_def *score_def = 0;
111 
112   /* UGR, FIXME, these are default \layout blocks once again.  They
113      suck. */
114   for (vsize i = 0; !score_def && i < sc->defs_.size (); i++)
115     if (from_scm<bool> (sc->defs_[i]->c_variable ("is-layout")))
116       score_def = sc->defs_[i];
117 
118   if (!score_def)
119     return SCM_BOOL_F;
120 
121   /* Don't rescale if the layout has already been scaled */
122   if (from_scm<bool> (score_def->c_variable ("cloned")))
123     score_def = score_def->clone ();
124   else
125     score_def = scale_output_def (score_def, output_scale (od));
126 
127   score_def->parent_ = od;
128 
129   SCM context = ly_run_translator (sc->get_music (), score_def->unprotect ());
130   SCM output = ly_format_output (context);
131 
132   return output;
133 }
134