1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3 
4   Copyright (C) 1996--2020 Han-Wen Nienhuys
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 #ifndef GROB_HH
21 #define GROB_HH
22 
23 #include "box.hh"
24 #include "virtual-methods.hh"
25 #include "diagnostics.hh"
26 #include "dimension-cache.hh"
27 #include "grob-interface.hh"
28 #include "lily-proto.hh"
29 
30 #include <set>
31 
32 class Grob : public Smob<Grob>, public Diagnostics
33 {
34 public:
35   int print_smob (SCM, scm_print_state *) const;
36   SCM mark_smob () const;
37   static const char *const type_p_name_;
38   virtual ~Grob ();
39 private:
40   VIRTUAL_CLASS_NAME (Grob);
41 
42   void init ();
43 
44 protected:
45   /* data */
46   mutable Dimension_cache dim_cache_[NO_AXES];
47   Output_def *layout_;
48   Grob *original_;
49 
50   /* SCM data */
51   SCM immutable_property_alist_;
52   SCM mutable_property_alist_;
53   SCM object_alist_;
54 
55   /*
56     If this is a property, it accounts for 25% of the property
57     lookups.
58   */
59   SCM interfaces_;
60 
61   void substitute_object_links (SCM, SCM);
62   Real get_offset (Axis a) const;
63   SCM try_callback (SCM, SCM);
64   SCM try_callback_on_alist (SCM *, SCM, SCM);
65   void internal_set_value_on_alist (SCM *alist, SCM sym, SCM val);
66 
67   /* messages */
68   Input *origin () const override;
69 
70 public:
71 
72   /* friends */
73   friend class System;
74   friend SCM ly_grob_properties (SCM);
75   friend SCM ly_grob_basic_properties (SCM);
76 
77   /* standard callbacks */
78   DECLARE_SCHEME_CALLBACK (x_parent_positioning, (SCM));
79   DECLARE_SCHEME_CALLBACK (y_parent_positioning, (SCM));
80   DECLARE_SCHEME_CALLBACK (pure_stencil_height, (SCM smob, SCM, SCM));
81   DECLARE_SCHEME_CALLBACK (stencil_height, (SCM smob));
82   DECLARE_SCHEME_CALLBACK (stencil_width, (SCM smob));
83   DECLARE_SCHEME_CALLBACK (pure_simple_vertical_skylines_from_extents, (SCM smob, SCM, SCM));
84   DECLARE_SCHEME_CALLBACK (simple_vertical_skylines_from_extents, (SCM smob));
85   DECLARE_SCHEME_CALLBACK (vertical_skylines_from_stencil, (SCM smob));
86   DECLARE_SCHEME_CALLBACK (pure_vertical_skylines_from_element_stencils, (SCM smob, SCM, SCM));
87   DECLARE_SCHEME_CALLBACK (vertical_skylines_from_element_stencils, (SCM smob));
88   DECLARE_SCHEME_CALLBACK (pure_simple_horizontal_skylines_from_extents, (SCM smob, SCM, SCM));
89   DECLARE_SCHEME_CALLBACK (simple_horizontal_skylines_from_extents, (SCM smob));
90   DECLARE_SCHEME_CALLBACK (horizontal_skylines_from_stencil, (SCM smob));
91   DECLARE_SCHEME_CALLBACK (pure_horizontal_skylines_from_element_stencils, (SCM smob, SCM, SCM));
92   DECLARE_SCHEME_CALLBACK (horizontal_skylines_from_element_stencils, (SCM smob));
93 
94   /* R/O access */
layout() const95   Output_def *layout () const { return layout_; }
original() const96   Grob *original () const { return original_; }
interfaces() const97   SCM interfaces () const { return interfaces_; }
98 
99   /* life & death */
100   Grob (SCM basic_props);
101   Grob (Grob const &);
102   virtual Grob *clone () const = 0;
103 
104   /* forced death */
105   void suicide ();
106   bool is_live () const;
107 
108   /* naming. */
109   std::string name () const;
110 
111   /* Properties */
112   SCM get_property_alist_chain (SCM) const;
113   SCM internal_get_property (SCM symbol) const;
114   SCM internal_get_property_data (SCM symbol) const;
115   SCM internal_get_pure_property (SCM symbol, vsize start, vsize end) const;
116   SCM internal_get_maybe_pure_property (SCM symbol, bool pure,
117                                         vsize start, vsize end) const;
118   SCM internal_get_non_callback_marker_property_data (SCM symbol) const;
119   SCM internal_get_object (SCM symbol) const;
120   void internal_set_object (SCM sym, SCM val);
121   void internal_del_property (SCM symbol);
122   void instrumented_set_property (SCM, SCM, char const *, int, char const *);
123   void internal_set_property (SCM sym, SCM val);
124 
125   /* causes */
126   Stream_event *event_cause () const;
127   Stream_event *ultimate_event_cause () const;
128 
129   /* class hierarchy */
130   virtual System *get_system () const;
131   static System *get_system (Grob *);
132   virtual void do_break_processing ();
133   virtual Grob *find_broken_piece (System *) const;
134   virtual void break_breakable_item (System *);
135   virtual void derived_mark () const;
136   virtual void handle_broken_dependencies ();
137   virtual void handle_prebroken_dependencies ();
internal_set_as_bound_of_spanner(Spanner *,Direction)138   virtual bool internal_set_as_bound_of_spanner (Spanner *, Direction) { return false; }
139 
140   /* printing */
141   Stencil *get_stencil () const;
142   Stencil get_print_stencil () const;
143 
144   /* interfaces */
145   bool internal_has_interface (SCM intf) const;
146 
147   /* offsets */
148   void translate_axis (Real, Axis);
149   Real relative_coordinate (Grob const *refp, Axis) const;
150   Real parent_relative (Grob const *refp, Axis) const;
151   Real pure_relative_y_coordinate (Grob const *refp, vsize start, vsize end);
152   Real maybe_pure_coordinate (Grob const *refp, Axis a, bool pure, vsize start, vsize end);
153 
154   /* extents */
155   Interval extent (Grob const *refpoint, Axis) const;
156   void flush_extent_cache (Axis);
157   virtual Interval pure_y_extent (Grob *refpoint, vsize start, vsize end);
158   Interval maybe_pure_extent (Grob *refpoint, Axis, bool pure,
159                               vsize start, vsize end);
160 
161   /* refpoints */
162   Grob *common_refpoint (Grob const *s, Axis a) const;
set_x_parent(Grob * e)163   void set_x_parent (Grob *e) { dim_cache_[X_AXIS].parent_ = e; }
set_y_parent(Grob * e)164   void set_y_parent (Grob *e) { dim_cache_[Y_AXIS].parent_ = e; }
set_parent(Grob * e,Axis a)165   void set_parent (Grob *e, Axis a) { dim_cache_[a].parent_ = e; }
get_x_parent() const166   Grob *get_x_parent () const { return dim_cache_[X_AXIS].parent_; }
get_y_parent() const167   Grob *get_y_parent () const { return dim_cache_[Y_AXIS].parent_; }
get_parent(Axis a) const168   Grob *get_parent (Axis a) const { return dim_cache_[a].parent_; }
169   void fixup_refpoint ();
170 
171   /* vertical ordering */
172   static bool internal_vertical_less (Grob *g1, Grob *g2, bool pure);
173   static Grob *get_root_vertical_alignment (Grob *g);
174   static Grob *get_vertical_axis_group (Grob *g);
175   static bool vertical_less (Grob *g1, Grob *g2);
176   static bool pure_vertical_less (Grob *g1, Grob *g2);
177   static int get_vertical_axis_group_index (Grob *g);
178 
179   /* skylines */
180   virtual Interval_t<int> spanned_rank_interval () const = 0;
181   bool check_cross_staff (Grob *common);
182   static bool less (Grob *g1, Grob *g2);
183   static SCM maybe_pure_internal_simple_skylines_from_extents (Grob *, Axis, bool, int, int, bool, bool);
184   static SCM internal_skylines_from_element_stencils (Grob *me, Axis a, bool pure, int beg, int end);
185   static SCM internal_skylines_from_element_stencils (SCM, Axis);
186 };
187 
188 template <class T>
has_interface(Grob const * g)189 inline bool has_interface (Grob const *g)
190 {
191   return g && g->internal_has_interface (Grob_interface<T>::interface_symbol_);
192 }
193 
194 /* unification */
195 void uniquify (std::vector<Grob *> &);
196 
197 /* refpoints */
198 Grob *common_refpoint_of_list (SCM elt_list, Grob *, Axis a);
199 Grob *common_refpoint_of_array (std::vector<Grob *> const &, Grob *, Axis a);
200 Grob *common_refpoint_of_array (std::set<Grob *> const &, Grob *, Axis a);
201 System *get_root_system (Grob *me);
202 
203 /* extents */
204 Interval robust_relative_extent (Grob *, Grob *, Axis);
205 
206 /* offset/extent callbacks. */
207 void add_offset_callback (Grob *g, SCM proc, Axis a);
208 void chain_offset_callback (Grob *g, SCM proc, Axis a);
209 void chain_callback (Grob *g, SCM proc, SCM sym);
210 SCM axis_offset_symbol (Axis a);
211 SCM axis_parent_positioning (Axis a);
212 
213 SCM call_pure_function (SCM unpure, SCM args, vsize start, vsize end);
214 
215 void set_nested_property (Grob *, SCM property_path, SCM value);
216 
217 #endif /* GROB_HH */
218