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_BARLINE,
54  ALIGNMENT_DIVLINE,
55  ALIGNMENT_DEFAULT,
56  // Non-justifiable
57  ALIGNMENT_MEASURE_RIGHT_BARLINE,
58  ALIGNMENT_SCOREDEF_CAUTION_CLEF,
59  ALIGNMENT_SCOREDEF_CAUTION_KEYSIG,
60  ALIGNMENT_SCOREDEF_CAUTION_MENSUR,
61  ALIGNMENT_SCOREDEF_CAUTION_METERSIG,
62  ALIGNMENT_MEASURE_END
63 };
64 
65 #define BARLINE_REFERENCES -1
66 #define TSTAMP_REFERENCES -2
67 
68 //----------------------------------------------------------------------------
69 // Alignment
70 //----------------------------------------------------------------------------
71 
75 class Alignment : public Object {
76 public:
82  Alignment();
83  Alignment(const Fraction &time, AlignmentType type = ALIGNMENT_DEFAULT);
84  virtual ~Alignment();
85  void Reset() override;
87 
92 
96  bool IsSupportedChild(ClassId classId) override;
97 
102  void SetXRel(int xRel);
103  int GetXRel() const { return m_xRel; }
105 
110  void SetTime(const Fraction &time) { m_time = time; }
111  Fraction GetTime() const { return m_time; }
113 
118  bool operator==(const Alignment &other) const;
119  std::weak_ordering operator<=>(const Alignment &other) const;
121 
128 
133  void SetType(AlignmentType type) { m_type = type; }
134  AlignmentType GetType() const { return m_type; }
136 
140  bool IsOfType(const std::vector<AlignmentType> &types) const;
141 
148  const std::vector<int> &staffNs, int &minLeft, int &maxRight, const std::vector<ClassId> &excludes = {}) const;
149  void GetLeftRight(int staffN, int &minLeft, int &maxRight, const std::vector<ClassId> &excludes = {}) const;
150 
154  const MapOfIntGraceAligners &GetGraceAligners() { return m_graceAligners; }
155 
161 
165  bool HasGraceAligner(int id) const;
166 
171 
177  AlignmentReference *GetReferenceWithElement(const LayerElement *element, int staffN = VRV_UNSET);
178  const AlignmentReference *GetReferenceWithElement(const LayerElement *element, int staffN = VRV_UNSET) const;
180 
184  std::pair<int, int> GetAlignmentTopBottom() const;
185 
189  bool HasAccidVerticalOverlap(const Alignment *otherAlignment, int staffN) const;
190 
194  bool HasAlignmentReference(int staffN) const;
195 
199  bool HasTimestampOnly() const;
200 
204  std::string LogDebugTreeMsg() override
205  {
206  return StringFormat("%d %f", this->GetXRel(), this->GetTime().ToDouble());
207  }
208 
209  //----------------//
210  // Static methods //
211  //----------------//
212 
229  const Fraction &intervalTime, data_DURATION maxActualDur, double spacingLinear, double spacingNonLinear);
230 
231  //----------//
232  // Functors //
233  //----------//
234 
239  FunctorCode Accept(Functor &functor) override;
240  FunctorCode Accept(ConstFunctor &functor) const override;
241  FunctorCode AcceptEnd(Functor &functor) override;
242  FunctorCode AcceptEnd(ConstFunctor &functor) const override;
244 
245 private:
250  AlignmentReference *GetAlignmentReference(int staffN);
251 
252 public:
253  //
254 private:
261  int m_xRel;
266  Fraction m_time;
274  AlignmentType m_type;
279  MapOfIntGraceAligners m_graceAligners;
280 };
281 
282 //----------------------------------------------------------------------------
283 // AlignmentReference
284 //----------------------------------------------------------------------------
285 
292 class AlignmentReference : public Object, public AttNInteger {
293 public:
300  AlignmentReference(int staffN);
301  virtual ~AlignmentReference();
302  void Reset() override;
304 
308  bool IsSupportedChild(ClassId classId) override;
309 
313  bool AddChild(Object *object) override;
314 
318  bool HasAccidVerticalOverlap(const ArrayOfConstObjects &objects) const;
319 
323  bool HasMultipleLayer() const { return (m_layerCount > 1); }
324 
328  bool HasCrossStaffElements() const;
329 
330  //----------//
331  // Functors //
332  //----------//
333 
338  FunctorCode Accept(Functor &functor) override;
339  FunctorCode Accept(ConstFunctor &functor) const override;
340  FunctorCode AcceptEnd(Functor &functor) override;
341  FunctorCode AcceptEnd(ConstFunctor &functor) const override;
343 
344 private:
345  //
346 public:
347  //
348 private:
352  int m_layerCount;
353 };
354 
355 //----------------------------------------------------------------------------
356 // HorizontalAligner
357 //----------------------------------------------------------------------------
358 
364 class HorizontalAligner : public Object {
365 public:
371  HorizontalAligner(ClassId classId);
372  virtual ~HorizontalAligner();
373  void Reset() override;
375 
379  bool CopyChildren() const override { return false; }
380 
381  int GetAlignmentCount() const { return this->GetChildCount(); }
382 
383  //----------//
384  // Functors //
385  //----------//
386 
391  FunctorCode Accept(Functor &functor) override;
392  FunctorCode Accept(ConstFunctor &functor) const override;
393  FunctorCode AcceptEnd(Functor &functor) override;
394  FunctorCode AcceptEnd(ConstFunctor &functor) const override;
396 
397 protected:
403  Alignment *SearchAlignmentAtTime(const Fraction &time, AlignmentType type, int &idx);
404  const Alignment *SearchAlignmentAtTime(const Fraction &time, AlignmentType type, int &idx) const;
406 
410  void AddAlignment(Alignment *alignment, int idx = -1);
411 
412 private:
413  //
414 public:
415  //
416 private:
417 };
418 
419 //----------------------------------------------------------------------------
420 // MeasureAligner
421 //----------------------------------------------------------------------------
422 
428 public:
434  MeasureAligner();
435  virtual ~MeasureAligner();
436  void Reset() override;
438 
442  bool IsSupportedChild(ClassId classId) override;
443 
449  Alignment *GetAlignmentAtTime(const Fraction &time, AlignmentType type);
450 
456  void SetMaxTime(const Fraction &time);
457 
462 
467  int GetNonJustifiableMargin() const { return m_nonJustifiableLeftMargin; }
469 
475  void SetInitialTstamp(data_DURATION meterUnit);
476  Fraction GetInitialTstampDur() const { return m_initialTstampDur; }
478 
485  Alignment *GetLeftAlignment() { return m_leftAlignment; }
486  const Alignment *GetLeftAlignment() const { return m_leftAlignment; }
487  Alignment *GetLeftBarLineAlignment() { return m_leftBarLineAlignment; }
488  const Alignment *GetLeftBarLineAlignment() const { return m_leftBarLineAlignment; }
490 
497  Alignment *GetRightAlignment() { return m_rightAlignment; }
498  const Alignment *GetRightAlignment() const { return m_rightAlignment; }
499  Alignment *GetRightBarLineAlignment() { return m_rightBarLineAlignment; }
500  const Alignment *GetRightBarLineAlignment() const { return m_rightBarLineAlignment; }
502 
507  void AdjustProportionally(const ArrayOfAdjustmentTuples &adjustments);
508 
514 
519  void AdjustGraceNoteSpacing(const Doc *doc, Alignment *alignment, int staffN);
520 
521  //----------//
522  // Functors //
523  //----------//
524 
529  FunctorCode Accept(Functor &functor) override;
530  FunctorCode Accept(ConstFunctor &functor) const override;
531  FunctorCode AcceptEnd(Functor &functor) override;
532  FunctorCode AcceptEnd(ConstFunctor &functor) const override;
534 
535 private:
536  //
537 public:
538  //
539 private:
543  Alignment *m_leftAlignment;
544  Alignment *m_rightAlignment;
545 
549  Alignment *m_leftBarLineAlignment;
550  Alignment *m_rightBarLineAlignment;
551 
555  int m_nonJustifiableLeftMargin;
556 
561  Fraction m_initialTstampDur;
562 };
563 
564 //----------------------------------------------------------------------------
565 // GraceAligner
566 //----------------------------------------------------------------------------
567 
573 public:
580  virtual ~GraceAligner();
581  void Reset() override;
583 
588  Alignment *GetAlignmentAtTime(const Fraction &time, AlignmentType type);
589 
597 
601  void AlignStack();
602 
607  void SetWidth(int totalWidth) { m_totalWidth = totalWidth; }
608  int GetWidth() const { return m_totalWidth; }
610 
616  int GetGraceGroupLeft(int staffN) const;
617  int GetGraceGroupRight(int staffN) const;
619 
624  void SetGraceAlignmentXPos(const Doc *doc);
625 
626  //----------//
627  // Functors //
628  //----------//
629 
634  FunctorCode Accept(Functor &functor) override;
635  FunctorCode Accept(ConstFunctor &functor) const override;
636  FunctorCode AcceptEnd(Functor &functor) override;
637  FunctorCode AcceptEnd(ConstFunctor &functor) const override;
639 
640 private:
641  //
642 public:
643  //
644 private:
648  ArrayOfObjects m_graceStack;
653  int m_totalWidth;
654 };
655 
656 //----------------------------------------------------------------------------
657 // TimestampAligner
658 //----------------------------------------------------------------------------
659 
666 class TimestampAligner : public Object {
667 public:
668  // constructors and destructors
670  virtual ~TimestampAligner();
671 
675  void Reset() override;
676 
680  bool IsSupportedChild(ClassId classId) override;
681 
687 
688  //----------//
689  // Functors //
690  //----------//
691 
696  FunctorCode Accept(Functor &functor) override;
697  FunctorCode Accept(ConstFunctor &functor) const override;
698  FunctorCode AcceptEnd(Functor &functor) override;
699  FunctorCode AcceptEnd(ConstFunctor &functor) const override;
701 
702 private:
703  //
704 public:
705  //
706 private:
707  //
708 };
709 
710 } // namespace vrv
711 
712 #endif
This class stores an alignment position elements will point to.
Definition: horizontalaligner.h:75
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:204
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:154
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:292
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:323
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:572
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:364
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:379
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:427
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:485
Alignment * GetRightAlignment()
Get right Alignment for the measure.
Definition: horizontalaligner.h:497
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:64
This class stores the timestamps (TimestampsAttr) in a measure.
Definition: horizontalaligner.h:666
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