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_CLEF,
45  ALIGNMENT_KEYSIG,
46  ALIGNMENT_MENSUR,
47  ALIGNMENT_METERSIG,
48  ALIGNMENT_PROPORT,
49  ALIGNMENT_DOT,
50  ALIGNMENT_CUSTOS,
51  ALIGNMENT_ACCID,
52  ALIGNMENT_GRACENOTE,
53  ALIGNMENT_CURSOR,
54  ALIGNMENT_CURSOR_CHORD,
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; }
137  template <typename Range> bool IsAnyOfType(const Range &alignmentType) const
138  {
139  return std::find(std::begin(alignmentType), std::end(alignmentType), GetType()) != std::end(alignmentType);
140  }
142 
146  bool IsOfType(const std::vector<AlignmentType> &types) const;
147 
154  const std::vector<int> &staffNs, int &minLeft, int &maxRight, const std::vector<ClassId> &excludes = {}) const;
155  void GetLeftRight(int staffN, int &minLeft, int &maxRight, const std::vector<ClassId> &excludes = {}) const;
156 
160  const MapOfIntGraceAligners &GetGraceAligners() { return m_graceAligners; }
161 
167 
171  bool HasGraceAligner(int id) const;
172 
177 
183  AlignmentReference *GetReferenceWithElement(const LayerElement *element, int staffN = VRV_UNSET);
184  const AlignmentReference *GetReferenceWithElement(const LayerElement *element, int staffN = VRV_UNSET) const;
186 
190  std::pair<int, int> GetAlignmentTopBottom() const;
191 
195  bool HasAccidVerticalOverlap(const Alignment *otherAlignment, int staffN) const;
196 
200  bool HasAlignmentReference(int staffN) const;
201 
205  bool HasTimestampOnly() const;
206 
210  std::string LogDebugTreeMsg() override
211  {
212  return StringFormat("%d %f", this->GetXRel(), this->GetTime().ToDouble());
213  }
214 
215  //----------------//
216  // Static methods //
217  //----------------//
218 
235  const Fraction &intervalTime, data_DURATION maxActualDur, double spacingLinear, double spacingNonLinear);
236 
237  //----------//
238  // Functors //
239  //----------//
240 
245  FunctorCode Accept(Functor &functor) override;
246  FunctorCode Accept(ConstFunctor &functor) const override;
247  FunctorCode AcceptEnd(Functor &functor) override;
248  FunctorCode AcceptEnd(ConstFunctor &functor) const override;
250 
251 private:
256  AlignmentReference *GetAlignmentReference(int staffN);
257 
258 public:
259  //
260 private:
267  int m_xRel;
272  Fraction m_time;
280  AlignmentType m_type;
285  MapOfIntGraceAligners m_graceAligners;
286 };
287 
288 //----------------------------------------------------------------------------
289 // AlignmentReference
290 //----------------------------------------------------------------------------
291 
298 class AlignmentReference : public Object, public AttNInteger {
299 public:
306  AlignmentReference(int staffN);
307  virtual ~AlignmentReference();
308  void Reset() override;
310 
314  bool IsSupportedChild(ClassId classId) override;
315 
319  bool AddChild(Object *object) override;
320 
324  bool HasAccidVerticalOverlap(const ArrayOfConstObjects &objects) const;
325 
329  bool HasMultipleLayer() const { return (m_layerCount > 1); }
330 
334  bool HasCrossStaffElements() const;
335 
336  //----------//
337  // Functors //
338  //----------//
339 
344  FunctorCode Accept(Functor &functor) override;
345  FunctorCode Accept(ConstFunctor &functor) const override;
346  FunctorCode AcceptEnd(Functor &functor) override;
347  FunctorCode AcceptEnd(ConstFunctor &functor) const override;
349 
350 private:
351  //
352 public:
353  //
354 private:
358  int m_layerCount;
359 };
360 
361 //----------------------------------------------------------------------------
362 // HorizontalAligner
363 //----------------------------------------------------------------------------
364 
370 class HorizontalAligner : public Object {
371 public:
377  HorizontalAligner(ClassId classId);
378  virtual ~HorizontalAligner();
379  void Reset() override;
381 
385  bool CopyChildren() const override { return false; }
386 
387  int GetAlignmentCount() const { return this->GetChildCount(); }
388 
389  //----------//
390  // Functors //
391  //----------//
392 
397  FunctorCode Accept(Functor &functor) override;
398  FunctorCode Accept(ConstFunctor &functor) const override;
399  FunctorCode AcceptEnd(Functor &functor) override;
400  FunctorCode AcceptEnd(ConstFunctor &functor) const override;
402 
403 protected:
409  Alignment *SearchAlignmentAtTime(const Fraction &time, AlignmentType type, int &idx);
410  const Alignment *SearchAlignmentAtTime(const Fraction &time, AlignmentType type, int &idx) const;
412 
416  void AddAlignment(Alignment *alignment, int idx = -1);
417 
418 private:
419  //
420 public:
421  //
422 private:
423 };
424 
425 //----------------------------------------------------------------------------
426 // MeasureAligner
427 //----------------------------------------------------------------------------
428 
434 public:
440  MeasureAligner();
441  virtual ~MeasureAligner();
442  void Reset() override;
444 
448  bool IsSupportedChild(ClassId classId) override;
449 
455  Alignment *GetAlignmentAtTime(const Fraction &time, AlignmentType type);
456 
462  void SetMaxTime(const Fraction &time);
463 
468 
473  int GetNonJustifiableMargin() const { return m_nonJustifiableLeftMargin; }
475 
481  void SetInitialTstamp(data_DURATION meterUnit);
482  Fraction GetInitialTstampDur() const { return m_initialTstampDur; }
484 
491  Alignment *GetLeftAlignment() { return m_leftAlignment; }
492  const Alignment *GetLeftAlignment() const { return m_leftAlignment; }
493  Alignment *GetLeftBarLineAlignment() { return m_leftBarLineAlignment; }
494  const Alignment *GetLeftBarLineAlignment() const { return m_leftBarLineAlignment; }
496 
503  Alignment *GetRightAlignment() { return m_rightAlignment; }
504  const Alignment *GetRightAlignment() const { return m_rightAlignment; }
505  Alignment *GetRightBarLineAlignment() { return m_rightBarLineAlignment; }
506  const Alignment *GetRightBarLineAlignment() const { return m_rightBarLineAlignment; }
508 
513  void AdjustProportionally(const ArrayOfAdjustmentTuples &adjustments);
514 
520 
525  void AdjustGraceNoteSpacing(const Doc *doc, Alignment *alignment, int staffN);
526 
527  //----------//
528  // Functors //
529  //----------//
530 
535  FunctorCode Accept(Functor &functor) override;
536  FunctorCode Accept(ConstFunctor &functor) const override;
537  FunctorCode AcceptEnd(Functor &functor) override;
538  FunctorCode AcceptEnd(ConstFunctor &functor) const override;
540 
541 private:
542  //
543 public:
544  //
545 private:
549  Alignment *m_leftAlignment;
550  Alignment *m_rightAlignment;
551 
555  Alignment *m_leftBarLineAlignment;
556  Alignment *m_rightBarLineAlignment;
557 
561  int m_nonJustifiableLeftMargin;
562 
567  Fraction m_initialTstampDur;
568 };
569 
570 //----------------------------------------------------------------------------
571 // GraceAligner
572 //----------------------------------------------------------------------------
573 
579 public:
586  virtual ~GraceAligner();
587  void Reset() override;
589 
594  Alignment *GetAlignmentAtTime(const Fraction &time, AlignmentType type);
595 
603 
607  void AlignStack();
608 
613  void SetWidth(int totalWidth) { m_totalWidth = totalWidth; }
614  int GetWidth() const { return m_totalWidth; }
616 
622  int GetGraceGroupLeft(int staffN) const;
623  int GetGraceGroupRight(int staffN) const;
625 
630  void SetGraceAlignmentXPos(const Doc *doc);
631 
632  //----------//
633  // Functors //
634  //----------//
635 
640  FunctorCode Accept(Functor &functor) override;
641  FunctorCode Accept(ConstFunctor &functor) const override;
642  FunctorCode AcceptEnd(Functor &functor) override;
643  FunctorCode AcceptEnd(ConstFunctor &functor) const override;
645 
646 private:
647  //
648 public:
649  //
650 private:
654  ArrayOfObjects m_graceStack;
659  int m_totalWidth;
660 };
661 
662 //----------------------------------------------------------------------------
663 // TimestampAligner
664 //----------------------------------------------------------------------------
665 
672 class TimestampAligner : public Object {
673 public:
674  // constructors and destructors
676  virtual ~TimestampAligner();
677 
681  void Reset() override;
682 
686  bool IsSupportedChild(ClassId classId) override;
687 
693 
694  //----------//
695  // Functors //
696  //----------//
697 
702  FunctorCode Accept(Functor &functor) override;
703  FunctorCode Accept(ConstFunctor &functor) const override;
704  FunctorCode AcceptEnd(Functor &functor) override;
705  FunctorCode AcceptEnd(ConstFunctor &functor) const override;
707 
708 private:
709  //
710 public:
711  //
712 private:
713  //
714 };
715 
716 } // namespace vrv
717 
718 #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:210
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:160
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:298
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:329
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:578
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:370
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:385
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:433
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:491
Alignment * GetRightAlignment()
Get right Alignment for the measure.
Definition: horizontalaligner.h:503
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:672
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