summaryrefslogtreecommitdiff
path: root/lily/score.cc
blob: d10023dd8506b5103bed7a09ef673a074cff8574 (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*
  This file is part of LilyPond, the GNU music typesetter.

  Copyright (C) 1997--2015 Han-Wen Nienhuys <hanwen@xs4all.nl>

  LilyPond is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  LilyPond is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "score.hh"

#include <cstdio>

using namespace std;

#include "book.hh"
#include "cpu-timer.hh"
#include "global-context.hh"
#include "international.hh"
#include "lily-parser.hh"
#include "ly-module.hh"
#include "main.hh"
#include "music.hh"
#include "output-def.hh"
#include "paper-book.hh"
#include "paper-score.hh"
#include "warn.hh"


Input *
Score::origin () const
{
  return unsmob<Input> (input_location_);
}

Score::Score ()
{
  header_ = SCM_EOL;
  music_ = SCM_EOL;
  input_location_ = SCM_EOL;

  error_found_ = false;

  smobify_self ();
  input_location_ = Input ().smobbed_copy ();
}

Score::~Score ()
{
}

const char * const Score::type_p_name_ = "ly:score?";

SCM
Score::mark_smob () const
{
  scm_gc_mark (header_);
  for (vsize i = defs_.size (); i--;)
    scm_gc_mark (defs_[i]->self_scm ());

  scm_gc_mark (input_location_);
  return music_;
}

Score::Score (Score const &s)
  : Smob<Score> ()
{
  header_ = SCM_EOL;
  music_ = SCM_EOL;
  input_location_ = SCM_EOL;
  error_found_ = s.error_found_;

  smobify_self ();
  input_location_ = s.origin ()->smobbed_copy ();

  Music *m = unsmob<Music> (s.music_);
  if (m)
    {
      Music *mclone = m->clone ();
      music_ = mclone->unprotect ();
    }
  else
    music_ = SCM_EOL;

  for (vsize i = 0, n = s.defs_.size (); i < n; i++)
    {
      Output_def *copy = s.defs_[i]->clone ();
      defs_.push_back (copy);
      copy->unprotect ();
    }
  header_ = ly_make_module (false);
  if (ly_is_module (s.header_))
    ly_module_copy (header_, s.header_);
}

/*
  Format score, return list of Music_output objects.

  LAYOUTBOOK should be scaled already.
*/
SCM
Score::book_rendering (Output_def *layoutbook,
                       Output_def *default_def)
{
  if (error_found_)
    return SCM_EOL;

  Real scale = 1.0;

  if (layoutbook && to_boolean (layoutbook->c_variable ("is-paper")))
    scale = scm_to_double (layoutbook->c_variable ("output-scale"));

  SCM outputs = SCM_EOL;

  int outdef_count = defs_.size ();

  for (int i = 0; !i || i < outdef_count; i++)
    {
      Output_def *def = outdef_count ? defs_[i] : default_def;
      SCM scaled = def->self_scm ();

      if (to_boolean (def->c_variable ("is-layout")))
        {
          def = scale_output_def (def, scale);
          def->parent_ = layoutbook;

          scaled = def->unprotect ();
        }

      /* TODO: fix or junk --no-layout.  */
      SCM context = ly_run_translator (music_, scaled);
      if (unsmob<Global_context> (context))
        {
          SCM s = ly_format_output (context);

          outputs = scm_cons (s, outputs);
        }

      scm_remember_upto_here_1 (scaled);
    }

  return scm_reverse_x (outputs, SCM_EOL);
}

void
Score::set_music (SCM music)
{
  if (unsmob<Music> (music_))
    {
      unsmob<Music> (music)->origin ()->non_fatal_error
        (_ ("already have music in score"));
      unsmob<Music> (music_)->origin ()->non_fatal_error
        (_ ("this is the previous music"));
    }
  Music *m = unsmob<Music> (music);
  if (m && to_boolean (m->get_property ("error-found")))
    {
      m->origin ()->non_fatal_error
        (_ ("errors found, ignoring music expression"));

      error_found_ = true;
    }

  if (error_found_)
    music_ = SCM_EOL;
  else
    music_ = music;
}

SCM
Score::get_music () const
{
  return music_;
}

void
Score::add_output_def (Output_def *def)
{
  defs_.push_back (def);
}

SCM
Score::get_header () const
{
  return header_;
}

void
Score::set_header (SCM module)
{
  assert (ly_is_module (module));
  header_ = module;
}