Verovio
Source code documentation
horizontalaligner.h
1 // Name: horizontalaligner.h
3 // Author: Laurent Pugin
4 // Created: 2013
5 // Copyright (c) Authors and others. All rights reserved.
7 
8 #ifndef __VRV_HORIZONTAL_ALIGNER_H__
9 #define __VRV_HORIZONTAL_ALIGNER_H__
10 
11 #include "atts_shared.h"
12 #include "fraction.h"
13 #include "object.h"
14 #include "vrv.h"
15 
16 namespace vrv {
17 
18 class Accid;
19 class AlignmentReference;
20 class FloatingObject;
21 class GraceAligner;
22 class LedgerLine;
23 class MeasureAligner;
24 class Note;
25 class TimestampAttr;
26 
31 enum AlignmentType {
32  ALIGNMENT_SCOREDEF_OSSIA_CLEF = -2,
33  ALIGNMENT_SCOREDEF_OSSIA_KEYSIG,
34  ALIGNMENT_MEASURE_START = 0,
35  // Non-justifiable
36  ALIGNMENT_SCOREDEF_CLEF,
37  ALIGNMENT_SCOREDEF_KEYSIG,
38  ALIGNMENT_SCOREDEF_MENSUR,
39  ALIGNMENT_SCOREDEF_METERSIG,
40  ALIGNMENT_MEASURE_LEFT_BARLINE,
41  // Justifiable
42  ALIGNMENT_FULLMEASURE,
43  ALIGNMENT_FULLMEASURE2,
44  ALIGNMENT_CURSOR,
45  ALIGNMENT_CURSOR_CHORD,
46  ALIGNMENT_CLEF,
47  ALIGNMENT_KEYSIG,
48  ALIGNMENT_MENSUR,
49  ALIGNMENT_METERSIG,
50  ALIGNMENT_PROPORT,
51  ALIGNMENT_DOT,
52  ALIGNMENT_CUSTOS,
53  ALIGNMENT_ACCID,
54  ALIGNMENT_GRACENOTE,
55  ALIGNMENT_BARLINE,
56  ALIGNMENT_DIVLINE,
57  ALIGNMENT_DEFAULT,
58  // Non-justifiable
59  ALIGNMENT_MEASURE_RIGHT_BARLINE,
60  ALIGNMENT_SCOREDEF_CAUTION_CLEF,
61  ALIGNMENT_SCOREDEF_CAUTION_KEYSIG,
62  ALIGNMENT_SCOREDEF_CAUTION_MENSUR,
63  ALIGNMENT_SCOREDEF_CAUTION_METERSIG,
64  ALIGNMENT_MEASURE_END
65 };
66 
67 #define BARLINE_REFERENCES -1
68 #define TSTAMP_REFERENCES -2
69 
70 //----------------------------------------------------------------------------
71 // Alignment
72 //----------------------------------------------------------------------------
73 
77 class Alignment : public Object {
78 public:
84  Alignment();
85  Alignment(const Fraction &time, AlignmentType type = ALIGNMENT_DEFAULT);
86  virtual ~Alignment();
87  void Reset() override;
89 
94 
98  bool IsSupportedChild(ClassId classId) override;
99 
104  void SetXRel(int xRel);
105  int GetXRel() const { return m_xRel; }
107 
112  void SetTime(const Fraction &time) { m_time = time; }
113  Fraction GetTime() const { return m_time; }
115 
120  bool operator==(const Alignment &other) const;
121  std::weak_ordering operator<=>(const Alignment &other) const;
123 
130 
135  void SetType(AlignmentType type) { m_type = type; }
136  AlignmentType GetType() const { return m_type; }
138 
142  bool IsOfType(const std::vector<AlignmentType> &types) const;
143 
150  const std::vector<int> &staffNs, int &minLeft, int &maxRight, const std::vector<ClassId> &excludes = {}) const;
151  void GetLeftRight(int staffN, int &minLeft, int &maxRight, const std::vector<ClassId> &excludes = {}) const;
152 
156  const MapOfIntGraceAligners &GetGraceAligners() { return m_graceAligners; }
157 
163 
167  bool HasGraceAligner(int id) const;
168 
173 
179  AlignmentReference *GetReferenceWithElement(const LayerElement *element, int staffN = VRV_UNSET);
180  const AlignmentReference *GetReferenceWithElement(const LayerElement *element, int staffN = VRV_UNSET) const;
182 
186  std::pair<int, int> GetAlignmentTopBottom() const;
187 
191  bool HasAccidVerticalOverlap(const Alignment *otherAlignment, int staffN) const;
192 
196  bool HasAlignmentReference(int staffN) const;
197 
201  bool HasTimestampOnly() const;
202 
206  std::string LogDebugTreeMsg() override
207  {
208  return StringFormat("%d %f", this->GetXRel(), this->GetTime().ToDouble());
209  }
210 
211  //----------------//
212  // Static methods //
213  //----------------//
214 
231  const Fraction &intervalTime, data_DURATION maxActualDur, double spacingLinear, double spacingNonLinear);
232 
233  //----------//
234  // Functors //
235  //----------//
236 
241  FunctorCode Accept(Functor &functor) override;
242  FunctorCode Accept(ConstFunctor &functor) const override;
243  FunctorCode AcceptEnd(Functor &functor) override;
244  FunctorCode AcceptEnd(ConstFunctor &functor) const override;
246 
247 private:
252  AlignmentReference *GetAlignmentReference(int staffN);
253 
254 public:
255  //
256 private:
263  int m_xRel;
268  Fraction m_time;
276  AlignmentType m_type;
281  MapOfIntGraceAligners m_graceAligners;
282 };
283 
284 //----------------------------------------------------------------------------
285 // AlignmentReference
286 //----------------------------------------------------------------------------
287 
294 class AlignmentReference : public Object, public AttNInteger {
295 public:
302  AlignmentReference(int staffN);
303  virtual ~AlignmentReference();
304  void Reset() override;
306 
310  bool IsSupportedChild(ClassId classId) override;
311 
315  bool AddChild(Object *object) override;
316 
320  bool HasAccidVerticalOverlap(const ArrayOfConstObjects &objects) const;
321 
325  bool HasMultipleLayer() const { return (m_layerCount > 1); }
326 
330  bool HasCrossStaffElements() const;
331 
332  //----------//
333  // Functors //
334  //----------//
335 
340  FunctorCode Accept(Functor &functor) override;
341  FunctorCode Accept(ConstFunctor &functor) const override;
342  FunctorCode AcceptEnd(Functor &functor) override;
343  FunctorCode AcceptEnd(ConstFunctor &functor) const override;
345 
346 private:
347  //
348 public:
349  //
350 private:
354  int m_layerCount;
355 };
356 
357 //----------------------------------------------------------------------------
358 // HorizontalAligner
359 //----------------------------------------------------------------------------
360 
366 class HorizontalAligner : public Object {
367 public:
373  HorizontalAligner(ClassId classId);
374  virtual ~HorizontalAligner();
375  void Reset() override;
377 
381  bool CopyChildren() const override { return false; }
382 
383  int GetAlignmentCount() const { return this->GetChildCount(); }
384 
385  //----------//
386  // Functors //
387  //----------//
388 
393  FunctorCode Accept(Functor &functor) override;
394  FunctorCode Accept(ConstFunctor &functor) const override;
395  FunctorCode AcceptEnd(Functor &functor) override;
396  FunctorCode AcceptEnd(ConstFunctor &functor) const override;
398 
399 protected:
405  Alignment *SearchAlignmentAtTime(const Fraction &time, AlignmentType type, int &idx);
406  const Alignment *SearchAlignmentAtTime(const Fraction &time, AlignmentType type, int &idx) const;
408 
412  void AddAlignment(Alignment *alignment, int idx = -1);
413 
414 private:
415  //
416 public:
417  //
418 private:
419 };
420 
421 //----------------------------------------------------------------------------
422 // MeasureAligner
423 //----------------------------------------------------------------------------
424 
430 public:
436  MeasureAligner();
437  virtual ~MeasureAligner();
438  void Reset() override;
440 
444  bool IsSupportedChild(ClassId classId) override;
445 
451  Alignment *GetAlignmentAtTime(const Fraction &time, AlignmentType type);
452 
458  void SetMaxTime(const Fraction &time);
459 
464 
469  int GetNonJustifiableMargin() const { return m_nonJustifiableLeftMargin; }
471 
477  void SetInitialTstamp(data_DURATION meterUnit);
478  Fraction GetInitialTstampDur() const { return m_initialTstampDur; }
480 
487  Alignment *GetLeftAlignment() { return m_leftAlignment; }
488  const Alignment *GetLeftAlignment() const { return m_leftAlignment; }
489  Alignment *GetLeftBarLineAlignment() { return m_leftBarLineAlignment; }
490  const Alignment *GetLeftBarLineAlignment() const { return m_leftBarLineAlignment; }
492 
499  Alignment *GetRightAlignment() { return m_rightAlignment; }
500  const Alignment *GetRightAlignment() const { return m_rightAlignment; }
501  Alignment *GetRightBarLineAlignment() { return m_rightBarLineAlignment; }
502  const Alignment *GetRightBarLineAlignment() const { return m_rightBarLineAlignment; }
504 
509  void AdjustProportionally(const ArrayOfAdjustmentTuples &adjustments);
510 
516 
521  void AdjustGraceNoteSpacing(const Doc *doc, Alignment *alignment, int staffN);
522 
523  //----------//
524  // Functors //
525  //----------//
526 
531  FunctorCode Accept(Functor &functor) override;
532  FunctorCode Accept(ConstFunctor &functor) const override;
533  FunctorCode AcceptEnd(Functor &functor) override;
534  FunctorCode AcceptEnd(ConstFunctor &functor) const override;
536 
537 private:
538  //
539 public:
540  //
541 private:
545  Alignment *m_leftAlignment;
546  Alignment *m_rightAlignment;
547 
551  Alignment *m_leftBarLineAlignment;
552  Alignment *m_rightBarLineAlignment;
553 
557  int m_nonJustifiableLeftMargin;
558 
563  Fraction m_initialTstampDur;
564 };
565 
566 //----------------------------------------------------------------------------
567 // GraceAligner
568 //----------------------------------------------------------------------------
569 
575 public:
582  virtual ~GraceAligner();
583  void Reset() override;
585 
590  Alignment *GetAlignmentAtTime(const Fraction &time, AlignmentType type);
591 
599 
603  void AlignStack();
604 
609  void SetWidth(int totalWidth) { m_totalWidth = totalWidth; }
610  int GetWidth() const { return m_totalWidth; }
612 
618  int GetGraceGroupLeft(int staffN) const;
619  int GetGraceGroupRight(int staffN) const;
621 
626  void SetGraceAlignmentXPos(const Doc *doc);
627 
628  //----------//
629  // Functors //
630  //----------//
631 
636  FunctorCode Accept(Functor &functor) override;
637  FunctorCode Accept(ConstFunctor &functor) const override;
638  FunctorCode AcceptEnd(Functor &functor) override;
639  FunctorCode AcceptEnd(ConstFunctor &functor) const override;
641 
642 private:
643  //
644 public:
645  //
646 private:
650  ArrayOfObjects m_graceStack;
655  int m_totalWidth;
656 };
657 
658 //----------------------------------------------------------------------------
659 // TimestampAligner
660 //----------------------------------------------------------------------------
661 
668 class TimestampAligner : public Object {
669 public:
670  // constructors and destructors
672  virtual ~TimestampAligner();
673 
677  void Reset() override;
678 
682  bool IsSupportedChild(ClassId classId) override;
683 
689 
690  //----------//
691  // Functors //
692  //----------//
693 
698  FunctorCode Accept(Functor &functor) override;
699  FunctorCode Accept(ConstFunctor &functor) const override;
700  FunctorCode AcceptEnd(Functor &functor) override;
701  FunctorCode AcceptEnd(ConstFunctor &functor) const override;
703 
704 private:
705  //
706 public:
707  //
708 private:
709  //
710 };
711 
712 } // namespace vrv
713 
714 #endif
This class stores an alignment position elements will point to.
Definition: horizontalaligner.h:77
std::pair< int, int > GetAlignmentTopBottom() const
Return pair of max and min Y value within alignment.
AlignmentReference * GetReferenceWithElement(const LayerElement *element, int staffN=VRV_UNSET)
Return the AlignmentReference holding the element.
bool HasAccidVerticalOverlap(const Alignment *otherAlignment, int staffN) const
Return true if there is vertical overlap with accidentals from another alignment for specific staffN.
static int HorizontalSpaceForDuration(const Fraction &intervalTime, data_DURATION maxActualDur, double spacingLinear, double spacingNonLinear)
Compute "ideal" horizontal space to allow for a given time interval, ignoring the need to keep consec...
void GetLeftRight(const std::vector< int > &staffNs, int &minLeft, int &maxRight, const std::vector< ClassId > &excludes={}) const
Retrieve the minimum left and maximum right position for the objects in an alignment.
std::string LogDebugTreeMsg() override
Debug message.
Definition: horizontalaligner.h:206
void ClearGraceAligners()
Delete the grace aligners in the map.
bool HasTimestampOnly() const
Return true if the alignment contains only references to timestamp attributes.
bool AddLayerElementRef(LayerElement *element)
Add the LayerElement to the appropriate AlignmentReference child.
bool IsOfType(const std::vector< AlignmentType > &types) const
Check if the element is of on of the types.
bool HasAlignmentReference(int staffN) const
Return true if the alignment contains at least one reference with staffN.
void Reset() override
Reset the object, that is 1) removing all children and 2) resetting all attributes.
bool HasGraceAligner(int id) const
Returns true if the aligner has a GraceAligner.
GraceAligner * GetGraceAligner(int id)
Returns the GraceAligner for the Alignment.
const MapOfIntGraceAligners & GetGraceAligners()
Return all GraceAligners for the Alignment.
Definition: horizontalaligner.h:156
bool IsSupportedChild(ClassId classId) override
Override the method of adding AlignmentReference children.
bool PerformBoundingBoxAlignment() const
Returns true for Alignment for which we want to do bounding box alignment.
FunctorCode Accept(Functor &functor) override
Interface for class functor visitation.
This class stores a references of LayerElements for a staff.
Definition: horizontalaligner.h:294
void Reset() override
Reset the object, that is 1) removing all children and 2) resetting all attributes.
bool HasAccidVerticalOverlap(const ArrayOfConstObjects &objects) const
Return true if one of objects overlaps with accidentals from current reference (i....
bool IsSupportedChild(ClassId classId) override
Override the method of adding Alignment children.
bool HasMultipleLayer() const
Return true if the reference has elements from multiple layers.
Definition: horizontalaligner.h:325
bool AddChild(Object *object) override
Overwritten method for AlignmentReference children.
FunctorCode Accept(Functor &functor) override
Interface for class functor visitation.
bool HasCrossStaffElements() const
Return true if the reference has elements from cross-staff.
This abstract class is the base class for all const functors.
Definition: functor.h:126
This class is a hold the data and corresponds to the model of a MVC design pattern.
Definition: doc.h:41
Definition: fraction.h:19
This abstract class is the base class for all mutable functors.
Definition: functor.h:101
This class aligns the content of a grace note group It contains a vector of Alignment.
Definition: horizontalaligner.h:574
void AlignStack()
Align the notes in the reverse order.
FunctorCode Accept(Functor &functor) override
Interface for class functor visitation.
void Reset() override
Reset the object, that is 1) removing all children and 2) resetting all attributes.
void SetGraceAlignmentXPos(const Doc *doc)
Set an linear defaut position for each grace note This is called from the CalcAlignmentXPosFunctor.
void StackGraceElement(LayerElement *object)
Because the grace notes appear from left to right but need to be aligned from right to left,...
Alignment * GetAlignmentAtTime(const Fraction &time, AlignmentType type)
Retrieve the alignmnet of the type at that time.
This class aligns the content horizontally It contains a vector of Alignment.
Definition: horizontalaligner.h:366
FunctorCode Accept(Functor &functor) override
Interface for class functor visitation.
Alignment * SearchAlignmentAtTime(const Fraction &time, AlignmentType type, int &idx)
Search if an alignment of the type is already there at the time.
HorizontalAligner(ClassId classId)
@(
void Reset() override
Reset the object, that is 1) removing all children and 2) resetting all attributes.
bool CopyChildren() const override
Do not copy children for HorizontalAligner.
Definition: horizontalaligner.h:381
void AddAlignment(Alignment *alignment, int idx=-1)
Add an alignment at the appropriate position (at the end if -1)
This class is a base class for the Layer (<layer>) content.
Definition: layerelement.h:51
This class aligns the content of a measure It contains a vector of Alignment.
Definition: horizontalaligner.h:429
void PushAlignmentsRight()
Push all the ALIGNMENT_GRACENOTE and ALIGNMENT_CONTAINER to the right.
Fraction GetMaxTime() const
Return the max time of the measure (i.e., the right measure alignment time)
FunctorCode Accept(Functor &functor) override
Interface for class functor visitation.
void SetMaxTime(const Fraction &time)
Keep the maximum time of the measure.
Alignment * GetAlignmentAtTime(const Fraction &time, AlignmentType type)
Retrieve the alignmnet of the type at that time.
void AdjustGraceNoteSpacing(const Doc *doc, Alignment *alignment, int staffN)
Adjust the spacing for the grace note group(s) of the alignment on staffN The alignment need to be of...
Alignment * GetLeftAlignment()
Get left Alignment for the measure and for the left BarLine.
Definition: horizontalaligner.h:487
Alignment * GetRightAlignment()
Get right Alignment for the measure.
Definition: horizontalaligner.h:499
void Reset() override
Reset the object, that is 1) removing all children and 2) resetting all attributes.
void AdjustProportionally(const ArrayOfAdjustmentTuples &adjustments)
Adjust the spacing of the measure looking at each tuple of start / end alignment and a distance.
bool IsSupportedChild(ClassId classId) override
Override the method of adding AlignmentReference children.
This class represents a basic object.
Definition: object.h:66
This class stores the timestamps (TimestampsAttr) in a measure.
Definition: horizontalaligner.h:668
bool IsSupportedChild(ClassId classId) override
Override the method of adding TimestampAttr children.
void Reset() override
Reset the aligner (clear the content)
FunctorCode Accept(Functor &functor) override
Interface for class functor visitation.
TimestampAttr * GetTimestampAtTime(double time)
Look for an existing TimestampAttr at a certain time.
Definition: timestamp.h:19