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_MEASURE_START = 0,
33  // Non-justifiable
34  ALIGNMENT_SCOREDEF_CLEF,
35  ALIGNMENT_SCOREDEF_KEYSIG,
36  ALIGNMENT_SCOREDEF_MENSUR,
37  ALIGNMENT_SCOREDEF_METERSIG,
38  ALIGNMENT_MEASURE_LEFT_BARLINE,
39  // Justifiable
40  ALIGNMENT_FULLMEASURE,
41  ALIGNMENT_FULLMEASURE2,
42  ALIGNMENT_CLEF,
43  ALIGNMENT_KEYSIG,
44  ALIGNMENT_MENSUR,
45  ALIGNMENT_METERSIG,
46  ALIGNMENT_PROPORT,
47  ALIGNMENT_DOT,
48  ALIGNMENT_CUSTOS,
49  ALIGNMENT_ACCID,
50  ALIGNMENT_GRACENOTE,
51  ALIGNMENT_BARLINE,
52  ALIGNMENT_DIVLINE,
53  ALIGNMENT_DEFAULT,
54  // Non-justifiable
55  ALIGNMENT_MEASURE_RIGHT_BARLINE,
56  ALIGNMENT_SCOREDEF_CAUTION_CLEF,
57  ALIGNMENT_SCOREDEF_CAUTION_KEYSIG,
58  ALIGNMENT_SCOREDEF_CAUTION_MENSUR,
59  ALIGNMENT_SCOREDEF_CAUTION_METERSIG,
60  ALIGNMENT_MEASURE_END
61 };
62 
63 #define BARLINE_REFERENCES -1
64 #define TSTAMP_REFERENCES -2
65 
66 //----------------------------------------------------------------------------
67 // Alignment
68 //----------------------------------------------------------------------------
69 
73 class Alignment : public Object {
74 public:
80  Alignment();
81  Alignment(const Fraction &time, AlignmentType type = ALIGNMENT_DEFAULT);
82  virtual ~Alignment();
83  void Reset() override;
85 
90 
94  bool IsSupportedChild(ClassId classId) override;
95 
100  void SetXRel(int xRel);
101  int GetXRel() const { return m_xRel; }
103 
108  void SetTime(const Fraction &time) { m_time = time; }
109  Fraction GetTime() const { return m_time; }
111 
116  bool operator==(const Alignment &other) const;
117  std::weak_ordering operator<=>(const Alignment &other) const;
119 
126 
131  void SetType(AlignmentType type) { m_type = type; }
132  AlignmentType GetType() const { return m_type; }
134 
138  bool IsOfType(const std::vector<AlignmentType> &types) const;
139 
146  const std::vector<int> &staffNs, int &minLeft, int &maxRight, const std::vector<ClassId> &excludes = {}) const;
147  void GetLeftRight(int staffN, int &minLeft, int &maxRight, const std::vector<ClassId> &excludes = {}) const;
148 
152  const MapOfIntGraceAligners &GetGraceAligners() { return m_graceAligners; }
153 
159 
163  bool HasGraceAligner(int id) const;
164 
169 
175  AlignmentReference *GetReferenceWithElement(const LayerElement *element, int staffN = VRV_UNSET);
176  const AlignmentReference *GetReferenceWithElement(const LayerElement *element, int staffN = VRV_UNSET) const;
178 
182  std::pair<int, int> GetAlignmentTopBottom() const;
183 
187  bool HasAccidVerticalOverlap(const Alignment *otherAlignment, int staffN) const;
188 
192  bool HasAlignmentReference(int staffN) const;
193 
197  bool HasTimestampOnly() const;
198 
202  std::string LogDebugTreeMsg() override
203  {
204  return StringFormat("%d %f", this->GetXRel(), this->GetTime().ToDouble());
205  }
206 
207  //----------------//
208  // Static methods //
209  //----------------//
210 
227  const Fraction &intervalTime, data_DURATION maxActualDur, double spacingLinear, double spacingNonLinear);
228 
229  //----------//
230  // Functors //
231  //----------//
232 
237  FunctorCode Accept(Functor &functor) override;
238  FunctorCode Accept(ConstFunctor &functor) const override;
239  FunctorCode AcceptEnd(Functor &functor) override;
240  FunctorCode AcceptEnd(ConstFunctor &functor) const override;
242 
243 private:
248  AlignmentReference *GetAlignmentReference(int staffN);
249 
250 public:
251  //
252 private:
259  int m_xRel;
264  Fraction m_time;
272  AlignmentType m_type;
277  MapOfIntGraceAligners m_graceAligners;
278 };
279 
280 //----------------------------------------------------------------------------
281 // AlignmentReference
282 //----------------------------------------------------------------------------
283 
290 class AlignmentReference : public Object, public AttNInteger {
291 public:
298  AlignmentReference(int staffN);
299  virtual ~AlignmentReference();
300  void Reset() override;
302 
306  bool IsSupportedChild(ClassId classId) override;
307 
311  void AddChild(Object *object) override;
312 
316  bool HasAccidVerticalOverlap(const ArrayOfConstObjects &objects) const;
317 
321  bool HasMultipleLayer() const { return (m_layerCount > 1); }
322 
326  bool HasCrossStaffElements() const;
327 
328  //----------//
329  // Functors //
330  //----------//
331 
336  FunctorCode Accept(Functor &functor) override;
337  FunctorCode Accept(ConstFunctor &functor) const override;
338  FunctorCode AcceptEnd(Functor &functor) override;
339  FunctorCode AcceptEnd(ConstFunctor &functor) const override;
341 
342 private:
343  //
344 public:
345  //
346 private:
350  int m_layerCount;
351 };
352 
353 //----------------------------------------------------------------------------
354 // HorizontalAligner
355 //----------------------------------------------------------------------------
356 
362 class HorizontalAligner : public Object {
363 public:
369  HorizontalAligner(ClassId classId);
370  virtual ~HorizontalAligner();
371  void Reset() override;
373 
377  bool CopyChildren() const override { return false; }
378 
379  int GetAlignmentCount() const { return this->GetChildCount(); }
380 
381  //----------//
382  // Functors //
383  //----------//
384 
389  FunctorCode Accept(Functor &functor) override;
390  FunctorCode Accept(ConstFunctor &functor) const override;
391  FunctorCode AcceptEnd(Functor &functor) override;
392  FunctorCode AcceptEnd(ConstFunctor &functor) const override;
394 
395 protected:
401  Alignment *SearchAlignmentAtTime(const Fraction &time, AlignmentType type, int &idx);
402  const Alignment *SearchAlignmentAtTime(const Fraction &time, AlignmentType type, int &idx) const;
404 
408  void AddAlignment(Alignment *alignment, int idx = -1);
409 
410 private:
411  //
412 public:
413  //
414 private:
415 };
416 
417 //----------------------------------------------------------------------------
418 // MeasureAligner
419 //----------------------------------------------------------------------------
420 
426 public:
432  MeasureAligner();
433  virtual ~MeasureAligner();
434  void Reset() override;
436 
440  bool IsSupportedChild(ClassId classId) override;
441 
447  Alignment *GetAlignmentAtTime(const Fraction &time, AlignmentType type);
448 
454  void SetMaxTime(const Fraction &time);
455 
460 
465  int GetNonJustifiableMargin() const { return m_nonJustifiableLeftMargin; }
467 
473  void SetInitialTstamp(data_DURATION meterUnit);
474  Fraction GetInitialTstampDur() const { return m_initialTstampDur; }
476 
483  Alignment *GetLeftAlignment() { return m_leftAlignment; }
484  const Alignment *GetLeftAlignment() const { return m_leftAlignment; }
485  Alignment *GetLeftBarLineAlignment() { return m_leftBarLineAlignment; }
486  const Alignment *GetLeftBarLineAlignment() const { return m_leftBarLineAlignment; }
488 
495  Alignment *GetRightAlignment() { return m_rightAlignment; }
496  const Alignment *GetRightAlignment() const { return m_rightAlignment; }
497  Alignment *GetRightBarLineAlignment() { return m_rightBarLineAlignment; }
498  const Alignment *GetRightBarLineAlignment() const { return m_rightBarLineAlignment; }
500 
505  void AdjustProportionally(const ArrayOfAdjustmentTuples &adjustments);
506 
512 
517  void AdjustGraceNoteSpacing(const Doc *doc, Alignment *alignment, int staffN);
518 
519  //----------//
520  // Functors //
521  //----------//
522 
527  FunctorCode Accept(Functor &functor) override;
528  FunctorCode Accept(ConstFunctor &functor) const override;
529  FunctorCode AcceptEnd(Functor &functor) override;
530  FunctorCode AcceptEnd(ConstFunctor &functor) const override;
532 
533 private:
534  //
535 public:
536  //
537 private:
541  Alignment *m_leftAlignment;
542  Alignment *m_rightAlignment;
543 
547  Alignment *m_leftBarLineAlignment;
548  Alignment *m_rightBarLineAlignment;
549 
553  int m_nonJustifiableLeftMargin;
554 
559  Fraction m_initialTstampDur;
560 };
561 
562 //----------------------------------------------------------------------------
563 // GraceAligner
564 //----------------------------------------------------------------------------
565 
571 public:
578  virtual ~GraceAligner();
579  void Reset() override;
581 
586  Alignment *GetAlignmentAtTime(const Fraction &time, AlignmentType type);
587 
595 
599  void AlignStack();
600 
605  void SetWidth(int totalWidth) { m_totalWidth = totalWidth; }
606  int GetWidth() const { return m_totalWidth; }
608 
614  int GetGraceGroupLeft(int staffN) const;
615  int GetGraceGroupRight(int staffN) const;
617 
622  void SetGraceAlignmentXPos(const Doc *doc);
623 
624  //----------//
625  // Functors //
626  //----------//
627 
632  FunctorCode Accept(Functor &functor) override;
633  FunctorCode Accept(ConstFunctor &functor) const override;
634  FunctorCode AcceptEnd(Functor &functor) override;
635  FunctorCode AcceptEnd(ConstFunctor &functor) const override;
637 
638 private:
639  //
640 public:
641  //
642 private:
646  ArrayOfObjects m_graceStack;
651  int m_totalWidth;
652 };
653 
654 //----------------------------------------------------------------------------
655 // TimestampAligner
656 //----------------------------------------------------------------------------
657 
664 class TimestampAligner : public Object {
665 public:
666  // constructors and destructors
668  virtual ~TimestampAligner();
669 
673  void Reset() override;
674 
678  bool IsSupportedChild(ClassId classId) override;
679 
685 
686  //----------//
687  // Functors //
688  //----------//
689 
694  FunctorCode Accept(Functor &functor) override;
695  FunctorCode Accept(ConstFunctor &functor) const override;
696  FunctorCode AcceptEnd(Functor &functor) override;
697  FunctorCode AcceptEnd(ConstFunctor &functor) const override;
699 
700 private:
701  //
702 public:
703  //
704 private:
705  //
706 };
707 
708 } // namespace vrv
709 
710 #endif
This class stores an alignment position elements will point to.
Definition: horizontalaligner.h:73
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:202
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:152
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:290
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.
void AddChild(Object *object) override
Overwritten method for AlignmentReference children.
bool HasMultipleLayer() const
Return true if the reference has elements from multiple layers.
Definition: horizontalaligner.h:321
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:570
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:362
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:377
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:425
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:483
Alignment * GetRightAlignment()
Get right Alignment for the measure.
Definition: horizontalaligner.h:495
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:61
This class stores the timestamps (TimestampsAttr) in a measure.
Definition: horizontalaligner.h:664
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