
    rh@              	      4   S r SSKJr  SSKrSSKrSSKJr  SSKrSSK	J
r
  SSKJr  SSK	Jr  SSK	Jr  SS	K	Jr  SS
K	Jr  SSKJr  \R&                  " S5      r\" S/ SQ5      r\" S/ SQ5      r " S S\
R.                  5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      r " S S\R:                  5      r " S S\R>                  5      r  " S S \RB                  5      r"  S0       S1S" jjr#S# r$S$ r%S2S& jr& " S' S!\RN                  5      r( " S( S%\RN                  5      r) " S) S*\RT                  5      r+ " S+ S,\RX                  5      r-\\\\\\(\)\+\-/	r. " S- S.\R^                  5      r0\1S/:X  a  SSK	r	\	Rd                  " \05        gg)3a  
The layout.py module contains two types of objects that specify the layout on
page (or screen) for Scores and other Stream objects.  There are two main types
of Layout objects: (1) layout describing elements and (2) layout defining Streams.

(1) ScoreLayout, PageLayout, SystemLayout, and StaffLayout objects describe the size of
pages, the geometry of page and system margins, the distance between staves, etc.
The model for these layout objects is taken directly (perhaps too directly?)
from MusicXML.  These objects all inherit from a BaseLayout class, primarily
as an aid to finding all of these objects as a group.  ScoreLayouts give defaults
for each page, system, and staff.  Thus, they contain PageLayout, SystemLayout, and
currently one or more StaffLayout objects (but probably just one. MusicXML allows more than
StaffLayout object because multiple staves can be in a Part.  Music21 uses
the concept of a PartStaff for a Part that is played by the same performer as another.
e.g., the left hand of the Piano is a PartStaff paired with the right hand).

PageLayout and SystemLayout objects also have a property, 'isNew',
which, if set to `True`, signifies that a new page
or system should begin here.  In theory, one could define new dimensions for a page
or system in the middle of the system or page without setting isNew to True, in
which case these measurements would start applying on the next page.  In practice,
there's really one good place to use these Layout objects and that's in the first part
in a score at offset 0 of the first measure on a page or system
(or for ScoreLayout, at the beginning
of a piece outside any parts).  But it's not an
error to put them in other places, such as at offset 0 of the first measure of a page
or system in all the other parts.  In fact, MusicXML tends to do this, and it ends up
not being a waste if a program extracts a single part from the middle of a score.

These objects are standard :class:`~music21.base.Music21Object` types, but many
properties such as .duration, .beat, will probably not apply.

When exporting to MusicXML (which is currently the only format in which music21 can and
does preserve these markings), many MusicXML readers will ignore these tags (or worse,
add a new page or system when PageLayout and SystemLayout objects are found but also
add theme wherever they want).  In Finale, this behavior disappears if the MusicXML
document notes that it <supports> new-page and new-system markings.  Music21 will add
the appropriate <supports> tags if the containing Stream has `.definesExplicitPageBreaks`
and `.definesExplicitSystemBreaks` set to True.  When importing a score that has the
<supports> tag set, music21 will set `.definesExplicitXXXXBreaks` to True for the
outer score and the inner parts.  However, this means that if the score is manipulated
enough that the prior layout information is obsolete, programs will need to set these
properties to False or move the Layout tags accordingly.

(2) The second set of objects are Stream subclasses that can be employed when a program
needs to easily iterate around the systems and pages defined through the layout objects
just described, or to get the exact position on a page (or a graphical representation
of a page) for a particular measure or system.  (Individual notes coming soon).  Normal
Score streams can be changed into LayoutStreams by calling `divideByPages(s)` on them.
A Score that was organized: Score->Part->Measure would then become:
LayoutScore->Page->System->Staff->Measure.

The new LayoutScore has methods that enable querying what page or system a measure is in, and
specifically where on a page a measure is (or the dimensions
of every measure in the piece).  However
do not call .show() on a LayoutScore -- the normal score it's derived from will work just fine.
Nor does calling .show() on a Page or System work yet, but once the LayoutStream has been created,
code like this can be done:

     s = stream.Stream(...)
     ls = layout.divideByPages(s)
     pg2sys3 = ls.pages[1].systems[2]  # n.b.! 1, 2
     measureStart, measureEnd = pg2sys3.measureStart, pg2sys3.measureEnd
     scoreExcerpt = s.measures(measureStart, measureEnd)
     scoreExcerpt.show()  # will show page 2, system 3

Note that while the coordinates given by music21 for a musicxml score (based on margins,
staff size, etc.)
generally reflect what is actually in a musicxml producer, unfortunately, x-positions are
far less accurately
produced by most editors.  For instance, Finale scores with measure sizes that have been
manually adjusted tend to show their
unadjusted measure width and not their actual measure width in the MusicXML.

SmartScore Pro tends to produce very good MusicXML layout data.
    )annotationsN)
namedtuple)base)GatherSpanners)environment)exceptions21)spanner)stream)	StaffTypelayout
SystemSize)topleftrightbottomPageSize)r   r   r   r   widthheightc                  "    \ rS rSrSrSrS rSrg)
LayoutBasep   z
A base class for all Layout objects, defining a classSortOrder
and also an inheritance tree.

>>> scoreLayout = layout.ScoreLayout()
>>> isinstance(scoreLayout, layout.LayoutBase)
True
ic                    g)N  selfs    H/home/james-whalen/.local/lib/python3.13/site-packages/music21/layout.py_reprInternalLayoutBase._reprInternal{   s        r   N)__name__
__module____qualname____firstlineno____doc__classSortOrderr   __static_attributes__r   r    r   r   r   p   s     Nr    r   c                  h   ^  \ rS rSrSrSSSSSSSS.             SU 4S jjjrS	S jrSrU =r$ )
ScoreLayout   a{  
Parameters for configuring a score's layout.

PageLayout objects may be found on Measure or Part Streams.

>>> pl = layout.PageLayout(pageNumber=4, leftMargin=234, rightMargin=124,
...                        pageHeight=4000, pageWidth=3000, isNew=True)
>>> pl.pageNumber
4
>>> pl.rightMargin
124
>>> pl.leftMargin
234
>>> pl.isNew
True

This object represents both <print new-page> and <page-layout>
elements in musicxml.  The appearance tag is handled in the `.style`
for the stream (it was here in v7 and before, but did nothing).

Note that the appearance and style elements are subject to change during
and after the v8 releases.
N)scalingMillimetersscalingTenths	musicFontwordFont
pageLayoutsystemLayoutstaffLayoutListc                  > [         T	U ]  " S0 UD6  Xl        X l        XPl        X`l        / U l        X0l        X@l        Ub  Xpl        g g Nr   )	super__init__r+   r,   r/   r0   r1   r-   r.   )
r   r+   r,   r-   r.   r/   r0   r1   keywords	__class__s
            r   r5   ScoreLayout.__init__   sO     	$8$"4*+5/;24" &#2  'r    c                    U R                   b  U R                  c  gU R                   U R                  -  n[        X!-  S5      $ )a  
given the scalingMillimeters and scalingTenths,
return the value in millimeters of a number of
musicxml "tenths" where a tenth is a tenth of the distance
from one staff line to another.

returns 0.0 if either of scalingMillimeters or scalingTenths
is undefined.

>>> sl = layout.ScoreLayout(scalingMillimeters=2.0, scalingTenths=10)
>>> sl.tenthsToMillimeters(10)
2.0

Numbers are rounded to a maximum of 6 digits (but because they are floats,
there may be inaccuracies.

>>> sl.tenthsToMillimeters(17)
3.4
           )r+   r,   round)r   tenthsmillimetersPerTenths      r   tenthsToMillimetersScoreLayout.tenthsToMillimeters   sF    ( ""*d.@.@.H"558J8JJ(1155r    )r-   r/   r+   r,   r1   r0   r.   )r+   int | float | Noner,   rA   r-   
str | Noner.   rB   r/   zPageLayout | Noner0   zSystemLayout | Noner1   zlist[StaffLayout] | None)r=   int | floatreturnrC   )	r!   r"   r#   r$   r%   r5   r?   r'   __classcell__r7   s   @r   r)   r)      sw    8 7;15'+&*/337;?3%33 !/3 %	3
 $3 -3  13 #93 3.6 6r    r)   c            	      d   ^  \ rS rSrSrSSSSSSSSS.               SU 4S jjjrSrU =r$ )
PageLayout   a  
Parameters for configuring a page's layout.

PageLayout objects may be found on Measure or Part Streams.


>>> pl = layout.PageLayout(pageNumber=4, leftMargin=234, rightMargin=124,
...                        pageHeight=4000, pageWidth=3000, isNew=True)
>>> pl.pageNumber
4
>>> pl.rightMargin
124
>>> pl.leftMargin
234
>>> pl.isNew
True

This object represents both <print new-page> and <page-layout>
elements in musicxml.
N)
pageNumber
leftMarginrightMargin	topMarginbottomMargin
pageHeight	pageWidthisNewc                  > [         T
U ]  " S0 U	D6  Xl        X l        X0l        X@l        XPl        X`l        Xpl        Xl	        g r3   )
r4   r5   rJ   rK   rL   rM   rN   rO   rP   rQ   )r   rJ   rK   rL   rM   rN   rO   rP   rQ   r6   r7   s             r   r5   PageLayout.__init__   sA     	$8$$$&"($" 
r    )rN   rQ   rK   rO   rJ   rP   rL   rM   )rJ   
int | NonerK   rA   rL   rA   rM   rA   rN   rA   rO   rA   rP   rA   rQ   bool | Noner!   r"   r#   r$   r%   r5   r'   rE   rF   s   @r   rH   rH      sz    2 )-.2/3-104.2-1$(% , -	
 +  . , + " r    rH   c                  R   ^  \ rS rSrSrSSSSSS.         SU 4S jjjrSrU =r$ )SystemLayouti  a|  
Object that configures or alters a system's layout.

SystemLayout objects may be found on Measure or
Part Streams.

Importantly, if isNew is True then this object
indicates that a new system should start here.


>>> sl = layout.SystemLayout(leftMargin=234, rightMargin=124, distance=3, isNew=True)
>>> sl.distance
3
>>> sl.rightMargin
124
>>> sl.leftMargin
234
>>> sl.isNew
True
N)rK   rL   distancetopDistancerQ   c               b   > [         TU ]  " S0 UD6  Xl        X l        X0l        X@l        XPl        g r3   )r4   r5   rK   rL   rY   rZ   rQ   )r   rK   rL   rY   rZ   rQ   r6   r7   s          r   r5   SystemLayout.__init__  s4     	$8$$& !& 
r    )rY   rQ   rK   rL   rZ   )
rK   rA   rL   rA   rY   rA   rZ   rA   rQ   rU   rV   rF   s   @r   rX   rX     sS    , /3/3,0/3$(+ - *	
 - " r    rX   c                     ^  \ rS rSr% SrSS0rS\S'   SSSSS\R                  S.           SU 4S	 jjjr	S
 r
SrU =r$ )StaffLayouti-  a>  
Object that configures or alters the distance between
one staff and another in a system.

StaffLayout objects may be found on Measure or
Part Streams.

The musicxml equivalent <staff-layout> lives in
the <defaults> and in <print> attributes.


>>> sl = layout.StaffLayout(distance=3, staffNumber=1, staffSize=113, staffLines=5)
>>> sl.distance
3

The "number" attribute refers to which staff number
in a part group this refers to.  Thus, it's not
necessary in music21, but we store it if it's there.
(defaults to None)

>>> sl.staffNumber
1

staffLines specifies the number of lines for a non 5-line staff.

>>> sl.staffLines
5

staffSize is a percentage of the base staff size, so
this defines a staff 13% larger than normal.  Note that it is always converted to
a floating point number.

>>> sl.staffSize
113.0
>>> sl
<music21.layout.StaffLayout distance 3, staffNumber 1, staffSize 113.0, staffLines 5>


StaffLayout can also specify the staffType:

>>> sl.staffType = stream.enums.StaffType.OSSIA

There is one other attribute, '.hidden' which has three settings:

* None - inherit from previous StaffLayout object, or False if no object exists
* False - not hidden -- show as a default staff
* True - hidden -- for playback only staves, or for a hidden/optimized-out staff

Note: (TODO: .hidden None is not working; always gives False)
	staffTypea>  
            What kind of staff is this as a stream.enums.StaffType.

            >>> sl = layout.StaffLayout()
            >>> sl.staffType
            <StaffType.REGULAR: 'regular'>
            >>> sl.staffType = stream.enums.StaffType.CUE
            >>> sl.staffType
            <StaffType.CUE: 'cue'>
            zdict[str, str]	_DOC_ATTRN)rY   staffNumber	staffSize
staffLineshiddenr_   c                  > [         TU ]  " S0 UD6  Xl        X l        Uc  S O
[	        U5      U l        X@l        XPl        X`l        g r3   )	r4   r5   rY   ra   floatrb   rc   rd   r_   )	r   rY   ra   rb   rc   rd   r_   r6   r7   s	           r   r5   StaffLayout.__init__l  sD     	$8$ !&-6->TE)DT$$-r    c                ~    SU R                   < SU R                  < S3SU R                  < SU R                  < 3-   $ )Nz	distance z, staffNumber , z
staffSize z, staffLines )rY   ra   rb   rc   r   s    r   r   StaffLayout._reprInternal  sF    DMM,N4;K;K:NbQt~~0doo=PQR 	Sr    )rY   rd   rc   ra   rb   r_   )rY   rA   ra   rA   rb   rA   rc   rT   rd   rU   r_   r   )r!   r"   r#   r$   r%   r`   __annotations__r   REGULARr5   r   r'   rE   rF   s   @r   r^   r^   -  s    1f 	 	!I~  -1/3-1(,%)(1(9(9.). -. +	.
 &. #. &. .&S Sr    r^   c                      \ rS rSrSrg)LayoutExceptioni  r   Nr!   r"   r#   r$   r'   r   r    r   rn   rn         r    rn   c                      \ rS rSrSrg)StaffGroupExceptioni  r   Nro   r   r    r   rr   rr     rp   r    rr   c                     ^  \ rS rSrSrSSSSS.       SU 4S jjjrSS jrSS jr\" \\S	S
9r	SS jr
SS jr\" \
\SS
9rSrU =r$ )
StaffGroupi  a  
A StaffGroup defines a collection of one or more
:class:`~music21.stream.Part` objects,
specifying that they should be shown together with a bracket,
brace, or other symbol, and may have a common name.

>>> p1 = stream.Part()
>>> p2 = stream.Part()
>>> p1.append(note.Note('C5', type='whole'))
>>> p1.append(note.Note('D5', type='whole'))
>>> p2.append(note.Note('C3', type='whole'))
>>> p2.append(note.Note('D3', type='whole'))
>>> p3 = stream.Part()
>>> p3.append(note.Note('F#4', type='whole'))
>>> p3.append(note.Note('G#4', type='whole'))
>>> s = stream.Score()
>>> s.insert(0, p1)
>>> s.insert(0, p2)
>>> s.insert(0, p3)
>>> staffGroup1 = layout.StaffGroup([p1, p2],
...      name='Marimba', abbreviation='Mba.', symbol='brace')
>>> staffGroup1.barTogether = 'Mensurstrich'
>>> s.insert(0, staffGroup1)
>>> staffGroup2 = layout.StaffGroup([p3],
...      name='Xylophone', abbreviation='Xyl.', symbol='bracket')
>>> s.insert(0, staffGroup2)
>>> #_DOCS_SHOW s.show()

.. image:: images/layout_StaffGroup_01.*
    :width: 400
NT)namebarTogetherabbreviationsymbolc               x   > [         TU ]  " U0 UD6  U=(       d    UU l        X0l        S U l        X@l        X l        g N)r4   r5   ru   rw   _symbolrx   _barTogether)r   ru   rv   rw   rx   spannedElementsr6   r7   s          r   r5   StaffGroup.__init__  s=     	/6X6(L	(MQ (r    c                    U R                   $ rz   )r|   r   s    r   _getBarTogetherStaffGroup._getBarTogether  s       r    c                    Uc  g US;   a  SU l         g US;   a  SU l         g [        U[        5      (       a  UR                  5       S:X  a  SU l         g [	        SU S35      e)	N)yesTT)noFFmensurstrichMensurstrichzthe bar together value  is not acceptable)r|   
isinstancestrlowerrr   r   values     r   _setBarTogetherStaffGroup._setBarTogether  sb    =m# $Dm# %Ds##(G .D%(?wFX&YZZr    a  
        Get or set the barTogether value, with either Boolean values
        or yes or no strings.  Or the string 'Mensurstrich' which
        indicates barring between staves but not in staves.

        Currently Mensurstrich is not supported by most exporters.

        >>> sg = layout.StaffGroup()
        >>> sg.barTogether = 'yes'
        >>> sg.barTogether
        True
        >>> sg.barTogether = 'Mensurstrich'
        >>> sg.barTogether
        'Mensurstrich'
        )docc                    U R                   $ rz   )r{   r   s    r   
_getSymbolStaffGroup._getSymbol  s    ||r    c                   Ub  [        U5      R                  5       S:X  a  S U l        g UR                  5       S;   a<  [        R                  " [        R
                  S   UR                  5       5      U l        g [        SU S35      e)Nnone)bracelinebracketsquare)r   r   r   r   zthe symbol value r   )r   r   r{   tcastLiteralrr   r   s     r   
_setSymbolStaffGroup._setSymbol  sm    =CJ,,.&8DL[[]DD66!)),P"QSXS^S^S`aDL%(9%@R&STTr    z
        Get or set the symbol value, with either Boolean values or yes or no strings.

        >>> sg = layout.StaffGroup()
        >>> sg.symbol = 'Brace'
        >>> sg.symbol
        'brace'
        )r|   r{   rw   ru   rx   )ru   rB   rv   ,t.Literal[True, False, None, 'Mensurstrich']rw   rB   rx   6t.Literal['bracket', 'line', 'brace', 'square'] | None)rD   r   )r   z9t.Literal[True, False, None, 'Mensurstrich', 'yes', 'no'])rD   r   )r   r   )r!   r"   r#   r$   r%   r5   r   r   propertyrv   r   r   rx   r'   rE   rF   s   @r   rt   rt     s    B #'MQ*.PT(( K(  (	(
 N( (&!
[ ?O B K U j* 3 Fr    rt   LayoutScorec           	        S n[        U 5      n[        U 5      nUS   S   nUS   S   n[        5       nSUl        SUl        Xhl        Xxl        U  H[  n	[        U	[        R                  5      (       a  M$  SU	R                  ;   a  Xl        UR                  U R                  U	5      U	5        M]     Sn
SnSnU GH	  u  pU
S-  n
USL a  [        SU
5        [        5       nXl        Xl        Xl        USL a   U R#                  X/ [$        R&                  S9nOU R#                  X5      nUS-   Ul        U H[  n	[        U	R                  =(       a    S	U	;  [        R                  5      (       a  M:  UR                  UR                  U	5      U	5        M]     UR*                  R-                  5       R/                  [        R0                  5      R-                  5       nU H  n	S
U	R                  ;   d  M  Xl        M     SnU GH  u  nnUU:  d  UU:  a  M  US-  nUS-  nUSL a!  U R#                  UU/ [$        R&                  S9nOU R#                  UU5      n[5        5       nUUl        U
Ul        UUl        UR;                  U5        UUl        UUl        UUl        Sn[?        UR*                  5       H  nUS-  nUS-  n[A        5       nUR;                  U5        UUl!        UUl"        U
Ul        UUl        UUl        URG                  UU5        [?        U[H           5      nU(       d  Mx  US   Ul%        M     U[L           n[O        U5      S:  a  U" [?        U5      5      Ul(        O![O        U5      S:X  a  US   Ul(        OSUl(        URS                  U5        GM     Xl*        URW                  5         URS                  U5        GM     URW                  5         U$ )a,  
Divides a score into a series of smaller scores according to page
breaks.  Only searches for PageLayout.isNew or SystemLayout.isNew
on the first part.  Returns a new `LayoutScore` object.

If fastMeasures is True, then the newly created System objects
do not have Clef signs, Key Signatures, or necessarily all the
applicable spanners in them.  On the other hand, the position
(on the page) information will be just as correct with
fastMeasures = True and it will run much faster on large scores
(because our spanner gathering algorithm is currently O(n^2);
something TODO: to fix.)


>>> lt = corpus.parse('demos/layoutTest.xml')
>>> len(lt.parts)
3
>>> len(lt.parts[0].getElementsByClass(stream.Measure))
80


Divide the score up into layout.Page objects

>>> layoutScore = layout.divideByPages(lt, fastMeasures=True)
>>> len(layoutScore.pages)
4
>>> lastPage = layoutScore.pages[-1]
>>> lastPage.measureStart
64
>>> lastPage.measureEnd
80

the layoutScore is a subclass of stream.Opus:

>>> layoutScore
<music21.layout.LayoutScore ...>
>>> 'Opus' in layoutScore.classes
True

Pages are subclasses of Opus also, since they contain Scores

>>> lastPage
<music21.layout.Page ...>
>>> 'Opus' in lastPage.classes
True


Each page now has Systems not parts.

>>> firstPage = layoutScore.pages[0]
>>> len(firstPage.systems)
4
>>> firstSystem = firstPage.systems[0]
>>> firstSystem.measureStart
1
>>> firstSystem.measureEnd
5

Systems are a subclass of Score:

>>> firstSystem
<music21.layout.System ...>
>>> isinstance(firstSystem, stream.Score)
True

Each System has staves (layout.Staff objects) not parts, though Staff is a subclass of Part

>>> secondStaff = firstSystem.staves[1]
>>> print(len(secondStaff.getElementsByClass(stream.Measure)))
5
>>> secondStaff
<music21.layout.Staff ...>
>>> isinstance(secondStaff, stream.Part)
True
c           
         [         R                  " U S   5      nU SS  H=  nS H4  n[        X5      b  M  [        X#5      c  M  [        X[        X#5      5        M6     M?     U$ )z
If there are multiple systemLayouts in an iterable (list or StreamIterator),
make a copy of the first one and get information from each successive one into
a rich system layout.
r      N)rY   rZ   rK   rL   )copydeepcopygetattrsetattr)inner_allSystemLayoutsrichestSystemLayoutsl	attributes       r   getRichSystemLayout*divideByPages.<locals>.getRichSystemLayoutI  sd     #mm,B1,EF(,BU	/;C#B2>/GB<RS V -
 #"r    r   r   Tr)   zupdating page)collectgatherSpannersrt   rH      N),getPageRegionMeasureNumbersgetSystemRegionMeasureNumbersr   definesExplicitPageBreaksdefinesExplicitSystemBreaksmeasureStart
measureEndr   r
   PartclassesscoreLayoutinsertelementOffsetprintPagerJ   measuresr   NONEsystemStartpartsfirstgetElementsByClassMeasurer/   SystemsystemNumberpageSystemNumbermergeAttributeselementslistStaffscoreStaffNumberra   replacer^   staffLayoutrX   lenr0   
coreAppend	systemEndcoreElementsChanged)scoreInprintUpdatesfastMeasuresr   pageMeasureTuplessystemMeasureTuplesfirstMeasureNumberlastMeasureNumber
scoreListselrJ   r   r   
pageStartMpageEndMthisPagethisPageAllfirstMeasureOfFirstPartr   systemStartM
systemEndMmeasureStacks
thisSystemsystemStaffNumberpstaffObjectallStaffLayoutsallSystemLayoutss                               r   divideByPagesr     s   `# 4G<7@*1-a0)"-a0J+/J(-1J*0-"fkk**

*)+&g33B7<	  JL 1
a
4/:.6 *&(4!**:35:H:M:M + OK "**:@K+a/BbjjC\-CV[[QQ 9 9" =rB  #."3"3"9"9";"N"NNN#!EG 	 )Brzz)&(# * (;$L*j(J,AAL!t# ' 0 0z9;@N@S@S !1 !U !( 0 0z JJ&2J#$.J!*:J'&&}5"/J&2J#$.J! !***+ A% !Q&!#g++A./?,*;')3&/?, ()$""1k259!K.5I&*9!*<'% ,,  *,7#$)*=dCS>T*U
'%&!+*:1*=
'*.
'
+i )<j *$$&h'a !2b ""$r    c                    [        U S5      $ )Nr   getRegionMeasureNumbersr   s    r   r   r     s    "7F33r    c                    [        U S5      $ )Nr   r   r   s    r   r   r     s    "7H55r    r   c                V   US:X  a  S/nOUS:X  a  SS/nO[        S5      eU R                  R                  5       nUR                  [        R
                  5      nUR                  5       R                  nUR                  5       R                  nU/n/ nUR                  5       R                  U5      n	U	 HL  n
U
R                  nU
R                  SL a  M   X;  d  M'  UR                  U5        UR                  US-
  5        MN     UR                  U5        [        [        Xx5      5      nU$ )z
get a list where each entry is a 2-tuplet whose first number
refers to the first measure on a page and whose second number
is the last measure on the page.
r   rH   r   rX   z$region must be one of Page or SystemFr   )
ValueErrorr   r   r   r
   r   numberlastflattenmeasureNumberrQ   appendr   zip)r   regionclassesToReturn	firstPartallMeasuresr   r   measureStartListmeasureEndListallAppropriateLayoutplplMeasureNumbermeasureLists                r   r   r     s    '.	8	'8?@@##%I..v~~>K$**,33#((*11*+N$,,.AA/R"**88u2##O4!!/A"56 # +,s+<=Kr    c                     ^  \ rS rSrSrSU 4S jjr\S 5       rSS jrS r	S r
SS jrS	 rS
 rSS jrSS jr      SS jrSS jrSS jrSS jrSrU =r$ )r   i  z
Designation that this Score is
divided into Pages, Systems, Staves (=Parts),
Measures, etc.

Used for computing location of notes, etc.

If the score does not change between calls to the various getPosition calls,
it is much faster as it uses a cache.
c                R   > [         TU ]  " U40 UD6  S U l        S U l        S U l        g rz   )r4   r5   r   r   r   r   givenElementsr6   r7   s      r   r5   LayoutScore.__init__  s,    3(3 r    c                ,    U R                  [        5      $ rz   )r   r   r   s    r   pagesLayoutScore.pages  s    &&t,,r    c                H    [         R                  R                  " U 4XS.UD6$ )z
Borrows stream.Score.show

>>> lp = layout.Page()
>>> ls = layout.LayoutScore()
>>> ls.append(lp)
>>> ls.show('text')
{0.0} <music21.layout.Page p.1>
<BLANKLINE>
fmtappr
   Scoreshowr   r  r  r6   s       r   r  LayoutScore.show  #     ||  D3D8DDr    c                   SU R                   ;  a  0 U R                   S'   U R                   S   nX;   a  X!   $ SnSn[        U R                  5       H)  u  pVXR                  :  d  XR                  :  a  M%  UnUn  O   Uc  [        S5      eSnSn[        UR                  5       H)  u  pXR                  :  d  XR                  :  a  M%  U
nU	n  O   Uc  [        S5      eXH4X!'   XH4$ )a  
Given a layoutScore from divideByPages and a measureNumber returns a tuple
of (pageId, systemId).  Note that pageId is probably one less than the page number,
assuming that the first page number is 1, the pageId for the first page will be 0.

Similarly, the first systemId on each page will be 0


>>> lt = corpus.parse('demos/layoutTest.xml')
>>> l = layout.divideByPages(lt, fastMeasures=True)
>>> l.getPageAndSystemNumberFromMeasureNumber(80)
(3, 3)
%pageAndSystemNumberFromMeasureNumbersNz%Cannot find this measure on any page!z^that's strange, this measure was supposed to be on this page, but I couldn't find it anywhere!)_cache	enumerater
  r   r   rn   systems)r   r   	dataCache	foundPagefoundPageIdpageIdr   foundSystemfoundSystemIdsystemIdr   s              r   'getPageAndSystemNumberFromMeasureNumber3LayoutScore.getPageAndSystemNumberFromMeasureNumber  s    3$++ECEDKK?@KK GH	%++	 )$** 5F444H[H[8[ I K !6 !"IJJ$-i.?.?$@ H666-J_J_:_$K$M %A ! #G H H$/#?	 ++r    c                   SU R                   ;  a  0 U R                   S'   U R                   S   nX;   a  X!   $ SnSnSnSnSnSnU R                  U   n	U R                  b  U R                  n
U
R                  b  U
R                  nUR                  b  UR                  nUR
                  b  UR
                  nUR                  b  UR                  nUR                  b  UR                  nUR                  b  UR                  nUR                  b  UR                  nU	R                  b  U	R                  nUR                  b  UR                  nUR
                  b  UR
                  nUR                  b  UR                  nUR                  b  UR                  nUR                  b  UR                  nUR                  b  UR                  n[        X4XeXx5      nXU'   U$ )aq  
return a namedtuple of (top, left, bottom, right, width, height)
margins for a given pageId in tenths

Default of (100, 100, 100, 100, 850, 1100) if undefined


>>> #_DOCS_SHOW g = corpus.parse('luca/gloria')
>>> #_DOCS_SHOW m22 = g.parts[0].getElementsByClass(stream.Measure)[22]
>>> #_DOCS_SHOW m22.getElementsByClass(layout.PageLayout).first().leftMargin = 204.0
>>> #_DOCS_SHOW gl = layout.divideByPages(g)
>>> #_DOCS_SHOW gl.getMarginsAndSizeForPageId(1)
>>> layout.PageSize(171.0, 204.0, 171.0, 171.0, 1457.0, 1886.0) #_DOCS_HIDE
PageSize(top=171.0, left=204.0, right=171.0, bottom=171.0, width=1457.0, height=1886.0)
marginsAndSizeForPageIdd   iR  iL  )r  r
  r   r/   rP   rO   rM   rK   rL   rN   r   )r   r  r  pageMarginToppageMarginLeftpageMarginRightpageMarginBottomrP   rO   r   sclr  	dataTuples                r   getMarginsAndSizeForPageId&LayoutScore.getMarginsAndSizeForPageId?  s     %DKK757DKK12KK 9:	$$ 	
::f% '""C~~)^^<<+ "I==,!#J<<+$&LLM==,%']]N>>-&(nnO??.')$ *$$B||'LL	}}(]]
||' "}}(!#~~)"$..*#%?? ]<L&4	%&r    c                P   SU R                   ;  a  0 U R                   S'   U R                   S   nU SU 3nXC;   a  X4   $ US:X  a  US:X  a   SnSnSnU R                  b  U R                  nUR                  bw  UR                  n	U	R                  b  U	R                  nU	R                  b  U	R                  nUS:X  a  U	R
                  b  U	R
                  nOU	R                  b  U	R                  nU R                  U   R                  U   n
U
R                  bw  U
R                  n	U	R                  b  U	R                  nU	R                  b  U	R                  nUS:X  a  U	R
                  b  U	R
                  nOU	R                  b  U	R                  nUS:  a!  U R                  XS-
  5      nUR                  nOSn[        U
R                  5      nUS-
  nU R                  XU5      u  nnX|-   nUU-   n[        [        U5      [        U5      [        U5      [        U5      5      nUX4'   U$ )a  
first systems on a page use a different positioning.

returns a Named tuple of the (top, left, right, and bottom) where each unit is
relative to the page margins

N.B. right is NOT the width -- it is different.  It is the offset to the right margin.
weird, inconsistent, but most useful.  Bottom, however, is the hard part to compute.

>>> lt = corpus.parse('demos/layoutTestMore.xml')
>>> ls = layout.divideByPages(lt, fastMeasures = True)
>>> ls.getPositionForSystem(0, 0)
SystemSize(top=211.0, left=70.0, right=0.0, bottom=696.0)
>>> ls.getPositionForSystem(0, 1)
SystemSize(top=810.0, left=0.0, right=0.0, bottom=1173.0)
>>> ls.getPositionForSystem(0, 2)
SystemSize(top=1340.0, left=67.0, right=92.0, bottom=1610.0)
>>> ls.getPositionForSystem(0, 3)
SystemSize(top=1724.0, left=0.0, right=0.0, bottom=2030.0)
>>> ls.getPositionForSystem(0, 4)
SystemSize(top=2144.0, left=0.0, right=0.0, bottom=2583.0)
positionForSystem-r      r   )r  r   r0   rK   rL   rZ   rY   r
  r  getPositionForSystemr   r   stavesgetPositionForStaffr   rf   )r   r  r!  positionForSystemCachecacheKeyrK   rL   previousDistancer+  r   r   lastSystemDimensionsbottomOfLastSystem	numStaves	lastStaffunused_systemStartsystemHeightr   r   r,  s                       r   r3   LayoutScore.getPositionForSystem  s%   . dkk1/1DKK+,!%-@!AXQxj)-)33Q;8q=
  '""C+%%==,!#J>>-"$..Kq=~~1+->>({{.+-;;( ZZ'//9
"".((B}}(]]
~~) nn1}>>-')~~$;;*'){{$a<#'#<#<VPQ\#R !5!<!<!"
))*	M	+/+C+CFV_+`(L3|#uSz5+<eK>PRWX^R_`	+4(r    c                f   SU R                   ;  a  0 U R                   S'   U R                   S   nU SU SU 3nXT;   a  XE   $ U R                  XU5      nUSLa%  U R                  XU5      nU R                  XU5      nOSnSnUS:  a  U R	                  XUS-
  5      u  pOSn
Xz-   nX-   nX4nXU'   U$ )a  
return a tuple of (top, bottom) for a staff, specified by a given pageId,
systemId, and staffId in tenths of a staff-space.

This distance is specified with respect to the top of the system.

Staff scaling (<staff-details> in musicxml inside an <attributes> object) is
taken into account, but not non-five-line staves.  Thus, a normally sized staff
is always of height 40 (4 spaces of 10-tenths each)

>>> lt = corpus.parse('demos/layoutTest.xml')
>>> ls = layout.divideByPages(lt, fastMeasures=True)

The first staff (staff 0) of each page/system always begins at height 0 and should end at
height 40 if it is a 5-line staff (not taken into account) with no staffSize changes

>>> ls.getPositionForStaff(0, 0, 0)
(0.0, 40.0)
>>> ls.getPositionForStaff(1, 0, 0)
(0.0, 40.0)

The second staff (staff 1) begins at the end of staff 0 (40.0) +
the appropriate staffDistance
and adds the height of the staff.  Staff 1 here has a size of 80 which means
80% of the normal staff size.  40 * 0.8 = 32.0:

>>> ls.getPositionForStaff(0, 0, 1)
(133.0, 165.0)

The third staff (staff 2) begins after the second staff (staff 1) but is a normal
size staff

>>> ls.getPositionForStaff(0, 0, 2)
(266.0, 306.0)

The first staff (staff 0) of the second system (system 1) also begins at 0
and as a normally-sized staff, has height of 40:

>>> ls.getPositionForStaff(0, 1, 0)
(0.0, 40.0)

The spacing between the staves has changed in the second system, but the
staff height has not:

>>> ls.getPositionForStaff(0, 1, 1)
(183.0, 215.0)
>>> ls.getPositionForStaff(0, 1, 2)
(356.0, 396.0)

In the third system (system 2), the staff distance reverts to the distance
of system 0, but the staffSize is now 120 or 48 tenths (40 * 1.2 = 48)

>>> ls.getPositionForStaff(0, 2, 1)
(117.0, 165.0)

Page 1 (0), System 4 (3), Staff 2 (1) is a hidden ("optimized") system.
Thus, its staffLayout notes this:

>>> staffLayout031 = ls.pages[0].systems[3].staves[1].staffLayout
>>> staffLayout031
<music21.layout.StaffLayout distance None, staffNumber None, staffSize 80, staffLines None>
>>> staffLayout031.hidden
True

Thus, the position for this staff will have the same top and bottom, and the
position for the next staff will have the same top as the previous staff:

>>> ls.getPositionForStaff(0, 3, 0)
(0.0, 40.0)
>>> ls.getPositionForStaff(0, 3, 1)
(40.0, 40.0)
>>> ls.getPositionForStaff(0, 3, 2)
(133.0, 173.0)


Tests for a score with PartStaff objects:
>>> lt = corpus.parse('demos/layoutTestMore.xml')
>>> ls = layout.divideByPages(lt, fastMeasures = True)
>>> ls.getPositionForStaff(0, 0, 0)
(0.0, 40.0)
>>> ls.getPositionForStaff(0, 0, 1)
(133.0, 173.0)
>>> ls.getPositionForStaff(0, 0, 2)
(235.0, 275.0)

>>> ls.getPositionForStaff(0, 2, 0)
(0.0, 40.0)
>>> ls.getPositionForStaff(0, 2, 1)
(40.0, 40.0)
>>> ls.getPositionForStaff(0, 2, 2)
(40.0, 40.0)

System 4 has the top staff hidden, which has been causing problems:

>>> ls.getPositionForStaff(0, 4, 0)
(0.0, 0.0)
>>> ls.getPositionForStaff(0, 4, 1)
(0.0, 40.0)
positionForStaffr1  Tr:   r   r   )r  getStaffHiddenAttributegetStaffDistanceFromPreviousgetStaffSizeFromLayoutr5  )r   r  r!  staffIdpositionForStaffCacher7  hiddenStaffstaffDistanceFromPreviousstaffHeightunused_previousStaffToppreviousStaffBottomstaffDistanceFromStartstaffBottomr,  s                 r   r5  LayoutScore.getPositionForStaff  s    L T[[0.0DKK*+ $,> ?XQxj'3,(22226WMd"(,(I(I&\c(d%55fPKK(+%Q;;?;S;S'A+</8#%8 #$!:!P,:+9	*3h'r    c                >   US:X  a  gSU R                   ;  a  0 U R                   S'   U R                   S   nU SU SU 3nXT;   a  XE   $ SnUS-
  nUS:  a'  U R                  XU5      nUSL a  SnOUS-
  nUS:  a  M'  USL a  SXE'   gSn	U R                  bC  U R                  n
U
R                  (       a&  U
R                   H  nUR                  nUc  M  Un	  O   U R
                  U   R                  U   R                  U   nUR                  [        R                  5      R                  5       nUc3  [        R                  " 5       n[        R                  S	U S
U SU 35        UR                  S5      nU(       a  U H  nUR                  nUc  M  Un	  O   XU'   U	$ )z
return the distance of this staff from the previous staff in the same system

for staffId = 0, this is always 0.0

TODO:tests, now that this is out from previous
r   r:   distanceFromPreviousr1  Fr   Tg      N@No measures found in pageId , systemId 
, staffId r^   )r  rB  r   r1   rY   r
  r  r4  r   r
   r   r   StreamenvironLocalwarn)r   r  r!  rE  rF  r7  foundVisibleStaffihiddenStatusrH  r+  slTempdistanceTemp	thisStafffirstMeasureOfStaffr   s                   r   rC  (LayoutScore.getStaffDistanceFromPrevious`  s    a<!424DKK./ $,B CXQxj'3,(22 "aK1f77!LLu$$(!E 1f %.1!+ %)!'""C""!11F#)??L#/4@1	 2 JJv&..x8??H	'::6>>JPPR&"(--/.vhk(:V]U^_ .@@O)%+0<-	 * +Dh'((r    c                   SU R                   ;  a  0 U R                   S'   U R                   S   nU SU SU 3nXT;   a  XE   $ U R                  U   R                  U   R                  U   nUR	                  [
        R                  5      R                  5       nUc3  [
        R                  " 5       n[        R                  SU SU SU 35        SnUS-
  n	U	S-  n
S	nU
n[        UR	                  S
5      5      nU(       a%  US   nUR                  b  XR                  S-  -  nSnUS	L a-  U R                  X5      u  nnUc  U
nOU R                  UUU5      n[        U5      nXU'   U$ )aR  
Get the currently active staff-size for a given pageId, systemId, and staffId.

Note that this does not take into account the hidden state of the staff, which,
if True, makes the effective size 0.0 -- see getStaffHiddenAttribute

>>> lt = corpus.parse('demos/layoutTest.xml')
>>> ls = layout.divideByPages(lt, fastMeasures=True)
>>> ls.getStaffSizeFromLayout(0, 0, 0)
40.0
>>> ls.getStaffSizeFromLayout(0, 0, 1)
32.0
>>> ls.getStaffSizeFromLayout(0, 0, 2)
40.0
>>> ls.getStaffSizeFromLayout(0, 1, 1)
32.0
>>> ls.getStaffSizeFromLayout(0, 2, 1)
48.0
>>> ls.getStaffSizeFromLayout(0, 3, 1)
32.0
rb   r1  rQ  rR  rS     r   g      $@Fr^   r   g      Y@T)r  r
  r  r4  r   r
   r   r   rT  rU  rV  r   rb   getSystemBeforeThisrD  rf   )r   r  r!  rE  staffSizeCacher7  r\  r]  numStaffLines	numSpacesstaffSizeBasestaffSizeDefinedLocallyrb   r   staffLayoutObjpreviousPageIdpreviousSystemIds                    r   rD  "LayoutScore.getStaffSizeFromLayout  s   , dkk)')DKK$[1XQxj'3%!++JJv&..x8??H	'::6>>JPPR&"(--/.vhk(:V]U^_ !A%	!D("'!	2EEmTU,Q/N''3)-E-E-MN	*.'"e+/3/G/G/Y,N,%)	 77HXZab	)$	#,x r    c                   SU R                   ;  a  0 U R                   S'   U R                   S   nU SU SU 3nXT;   a  XE   $ U R                  U   R                  U   R                  U   nSn[	        UR                  5       R                  S5      5      nU(       a  US   nUb  UR                  c,  U R                  X5      u  pU	c  SnOU R                  XU5      nOUR                  nXU'   U$ )a  
returns the staffLayout.hidden attribute for a staffId, or if it is not
defined, recursively search through previous staves until one is found.


>>> lt = corpus.parse('demos/layoutTestMore.xml')
>>> ls = layout.divideByPages(lt, fastMeasures = True)
>>> ls.getStaffHiddenAttribute(0, 0, 0)
False
>>> ls.getStaffHiddenAttribute(0, 0, 1)
False
>>> ls.getStaffHiddenAttribute(0, 1, 1)
True
>>> ls.getStaffHiddenAttribute(0, 2, 1)
True
>>> ls.getStaffHiddenAttribute(0, 3, 1)
False
staffHiddenAttributer1  Nr^   r   F)
r  r
  r  r4  r   r   r   rd   ra  rB  )r   r  r!  rE  staffHiddenCacher7  r\  staffLayoutObjectallStaffLayoutObjectsrh  ri  	hiddenTags               r   rB  #LayoutScore.getStaffHiddenAttribute  s   & "424DKK./;;'=>XQxj'3'#--JJv&..x8??H	  $Y%6%6%8%K%KM%Z [  5a 8$(9(@(@(H/3/G/G/Y,N%!	 88[bc	)00I%."r    c                    US:  a  XS-
  4$ US:X  a  gUS-
  n[        U R                  U   R                  5      nX4S-
  4$ )aZ  
given a pageId and systemId, get the (pageId, systemId) for the previous system.

return (None, None) if it's the first system on the first page

This test score has five systems on the first page,
three on the second, and two on the third

>>> lt = corpus.parse('demos/layoutTestMore.xml')
>>> ls = layout.divideByPages(lt, fastMeasures = True)
>>> systemId = 1
>>> pageId = 2  # last system, last page
>>> while pageId is not None:
...    pageId, systemId = ls.getSystemBeforeThis(pageId, systemId)
...    (pageId, systemId)
(2, 0) (1, 2) (1, 1) (1, 0) (0, 4) (0, 3) (0, 2) (0, 1) (0, 0) (None, -1)
r   r   )Nr   )r   r
  r  )r   r  r!  rh  
numSystemss        r   ra  LayoutScore.getSystemBeforeThis  sQ    . a<a<''{!#aZNTZZ7??@J!>11r    c                   SU R                   ;  a  0 U R                   S'   U R                   S   nX$;  a  0 XB'   XB   nX;   a  XQ   $ U R                  U5      u  pgU R                  X&U5      u  pU R                  XgU5      u  pU R	                  Xg5      nUR
                  nUR                  nU R                  U5      nUR
                  U-   U
-   nUR                  U-   U-   nUR
                  U-   U-   nUR                  U-   U	-   nUR                  nUR                  nSnUS:X  a
  UU4UU4U4nOW[        U5      n[        U5      n[        U5      U-  n[        U5      U-  n[        U5      U-  n[        U5      U-  nUU4UU4U4nUXQ'   U$ )a  
Given a layoutScore from divideByPages, a staffId, and a measureNumber,
returns a tuple of ((top, left), (bottom, right), pageId)
allowing an exact position for the measure on the page.
If returnFormat is "tenths", then it will be returned in tenths.

If returnFormat is "float", returns each as a number from 0 to 1 where 0 is the
top or left of the page, and 1 is the bottom or right of the page.


>>> lt = corpus.parse('demos/layoutTest.xml')
>>> ls = layout.divideByPages(lt, fastMeasures = True)

The first measure of staff one begins at 336 tenths from the top (125 for the
margin top and 211 for the top-staff-distance).  It begins 170.0 from the
left (100 for the page-margin-left, 70 for staff-margin-left).  It ends
40.0 below that (staffHeight) and 247.0 to the right (measure width)

>>> ls.getPositionForStaffMeasure(0, 1)
((336.0, 170.0), (376.0, 417.0), 0)

The other staves for the same measure are below this one:

>>> ls.getPositionForStaffMeasure(1, 1)
((469.0, 170.0), (501.0, 417.0), 0)
>>> ls.getPositionForStaffMeasure(2, 1)
((602.0, 170.0), (642.0, 417.0), 0)


If float is requested for returning, then the numbers are the fraction of
the distance across the page.

>>> ls.getPositionForStaffMeasure(0, 1, returnFormat='float')
((0.152..., 0.0996...), (0.170..., 0.244...), 0)


Moving over the page boundary:

>>> ls.getPositionForStaffMeasure(0, 23)
((1703.0, 1345.0), (1743.0, 1606.0), 0)
>>> ls.getPositionForStaffMeasure(1, 23)  # hidden
((1743.0, 1345.0), (1743.0, 1606.0), 0)
>>> ls.getPositionForStaffMeasure(0, 24)
((195.0, 100.0), (235.0, 431.0), 1)
>>> ls.getPositionForStaffMeasure(1, 24)
((328.0, 100.0), (360.0, 431.0), 1)
positionForPartMeasureNr=   )r  r"  measurePositionWithinSystemr5  r3  r   r   r-  r   r   rf   )r   rE  r   returnFormatpositionForPartMeasureCacher  r  r!  startXMeasureendXMeasurestaffToprM  	systemPos	systemTop
systemLeftpageSizer   r   r   r   rP   rO   r,  topRatio	leftRatiobottomRatio
rightRatios                              r   getPositionForStaffMeasure&LayoutScore.getPositionForStaffMeasure/  s   ` $4;;646DKK01&*kk2J&K#;9;'6/>	%%GGV%)%E%E8&-" $ 8 87 S--f?	MM	^^
226:llY&1}}z)M9	)K7
*[8NN	__
	8#tvuov>Ii(Iz*JSzJ.Hdi/I-*4Ku	1J"I.j0I6RI&	r    c                   Ub  Uc  U R                  U5      u  p#U R                  U   R                  U   nSnSnUR                  nUS   R	                  [
        R                  5      n[        U5       H  u  pU
R                  nUcm  [        S[        U5      5       HT  nX|   nUR                  5       nUR	                  [
        R                  5      U	   nUR                  c  MH  UR                  n  O   Uc&  [        R                  SU
R                   S35        SnO[        U5      nU
R                  U:X  a  Un  OX[-  nM     XUU-   4$ )a  
Given a measure number, find the start and end X positions (with respect to
the system margins) for the measure.

if pageId and systemId are given, then it will speed up the search. But not necessary

no staffId is needed since (at least for now) all measures begin and end at the same
X position


>>> l = corpus.parse('demos/layoutTest.xml')
>>> ls = layout.divideByPages(l, fastMeasures = True)
>>> ls.measurePositionWithinSystem(1, 0, 0)
(0.0, 247.0)
>>> ls.measurePositionWithinSystem(2, 0, 0)
(247.0, 544.0)
>>> ls.measurePositionWithinSystem(3, 0, 0)
(544.0, 841.0)

Measure positions reset at the start of a new system

>>> ls.measurePositionWithinSystem(6)
(0.0, 331.0)
>>> ls.measurePositionWithinSystem(7)
(331.0, 549.0)
Nr:   r   r   z Could not get width for measure z, using default of 300g     r@)r"  r
  r  r4  r   r
   r   r  layoutWidthranger   iterrU  rV  r   rf   )r   r   r  r!  r   startOffsetr   thisSystemStavesmeasureStreamrX  mcurrentWidthjsearchOtherStaffForWidth
searchItersearchOtherStaffMeasures                   r   rw  'LayoutScore.measurePositionWithinSystem  sO   6 >X-#KKMZFZZ'//9
%,,(+>>v~~Nm,DA==L#q#&6"78A/?/B,!9!>!>!@J.8.K.KFNN.[\].^+.::F'>'J'J 9 #!!6qxxj@VWY$$\2xx=($+- -0 %///r    c                   [        U R                  S   R                  S   R                  5      n/ n[	        U R
                  U R                  S-   5       H  nUSL a  [        SU5        / n[	        U5       HO  nU R                  XuU5      nUUS-
  UUS   S   US   S   US   S   US   S   US   S.n	UR                  U	5        MQ     UR                  U5        M     U$ )z
returns a list of dictionaries, where each dictionary gives the measure number
and other information, etc. in the document.


# >>> g = corpus.parse('luca/gloria')
# >>> gl = layout.divideByPages(g)
# >>> gl.getAllMeasurePositionsInDocument()
r   r   TzDoing measure r   )measureNumberActualr   ra   r   r   r   r   rJ   )
r   r
  r  r4  r  r   r   r   r  r   )
r   rx  r   r;  
allRetInfomNummListstaffNum	tupleInfoinfoDicts
             r    getAllMeasurePositionsInDocument,LayoutScore.getAllMeasurePositionsInDocument  s     

1--a0778	
$++T__q-@ADt#&-E!), ;;HLY	+/%)AX#+$Q<?%aLO'l1o&q\!_"+A,	 X& - e$# B$ r    )r   r   r   rz   NN)r  intr!  r  rD   r   )r  r  r!  r  rE  r  rD   rf   )r  r  r!  r  rE  r  rD   bool)r  r  r!  r  rD   ztuple[int | None, int])r=   )r=   F)r!   r"   r#   r$   r%   r5   r   r
  r  r"  r-  r3  r5  rC  rD  rB  ra  r  rw  r  r'   rE   rF   s   @r   r   r     s    	 - -E/,bENVp@D@)D>@+Z22 2 
	2@Wv;0z r    c                  P   ^  \ rS rSrSrSU 4S jjrS r\S 5       rS	S jr	Sr
U =r$ )
r   i  zQ
Designation that all the music in this Stream
belongs on a single notated page.
c                |   > [         TU ]  " U40 UD6  SU l        S U l        S U l        S U l        S U l        S U l        g )Nr   )r4   r5   rJ   r   r   r   r   r/   r  s      r   r5   Page.__init__  sA    3(3 r    c                     SU R                    3$ )Nzp.)rJ   r   s    r   r   Page._reprInternal  s    DOO$%%r    c                ,    U R                  [        5      $ rz   )r   r   r   s    r   r  Page.systems  s    &&v..r    c                H    [         R                  R                  " U 4XS.UD6$ )z
Borrows stream.Score.show

>>> ls = layout.System()
>>> lp = layout.Page()
>>> lp.append(ls)
>>> lp.show('text')
{0.0} <music21.layout.System 0: p.0, sys.0>
<BLANKLINE>
r  r  r  s       r   r  	Page.show  r  r    )r   r   r/   rJ   r   r   rz   r  )r!   r"   r#   r$   r%   r5   r   r   r  r  r'   rE   rF   s   @r   r   r     s2    
& / /E Er    c                  F   ^  \ rS rSrSrSU 4S jjrS r\S 5       rSr	U =r
$ )r   i  z
Designation that all the music in this Stream
belongs on a single notated system.

Attribute systemNumbering says at what point the numbering of
systems resets.  It can be either "Score" (default), "Opus", or "Page".
c                   > [         TU ]  " U40 UD6  SU l        SU l        SU l        S U l        SU l        S U l        S U l        g )Nr   r  )	r4   r5   r   rJ   r   r0   systemNumberingr   r   r  s      r   r5   System.__init__  sK    3(3 ! & r    c                R    U R                    SU R                   SU R                   3$ )Nz: p.z, sys.)r   rJ   r   r   s    r   r   System._reprInternal!  s,    ##$D(9@U@U?VWWr    c                ,    U R                  [        5      $ rz   )r   r   r   s    r   r4  System.staves$  s    &&u--r    )r   r   rJ   r   r0   r   r  rz   )r!   r"   r#   r$   r%   r5   r   r   r4  r'   rE   rF   s   @r   r   r     s'    
X . .r    r   c                  6   ^  \ rS rSrSrSU 4S jjrS rSrU =r$ )r   i)  zJ
Designation that all the music in this Stream
belongs on a single Staff.
c                   > [         TU ]  " U40 UD6  SU l        SU l        SU l        SU l        SU l        S U l        S U l        S U l	        g )Nr   r   )
r4   r5   ra   r   rJ   r   	optimizedr   inheritedHeightr   r  s      r   r5   Staff.__init__/  sR    3(3 ! !#r    c                z    SR                  U R                  U R                  U R                  U R                  5      $ )Nz{0}: p.{1}, sys.{2}, st.{3})formatr   rJ   r   ra   r   s    r   r   Staff._reprInternal<  s8    ,33D4I4I48OO484I4I484D4DF 	Fr    )r   r  r  rJ   r   r   r   ra   rz   )	r!   r"   r#   r$   r%   r5   r   r'   rE   rF   s   @r   r   r   )  s    
 F Fr    r   c                  &    \ rS rSrS rS rS rSrg)TestiH  c                n   SSK Jn  SSKJn  [        R
                  " 5       n[        SS5       HP  n[        R                  " 5       nXEl        UR                  5       nUR                  U5        UR                  U5        MR     [        5       nSUl        SUl        UR                  [        R                  5      S   R                  SU5        [        5       nSUl        SUl        SUl        S	Ul        UR                  [        R                  5      S
   R                  SU5        [        5       nSUl        SUl        UR                  [        R                  5      S   R                  SU5        [        5       nSUl        SUl        SUl        SUl        UR                  [        R                  5      S   R                  SU5        [        5       nSUl        SUl        SUl        UR                  [        R                  5      S   R                  SU5        UR%                  5       R'                  U5      ng )Nr   )note)m21ToXmlr      i,  T   (   r      r2  <   r;      )music21r  music21.musicxmlr  r
   rT  r  r   r   Noter   rX   rK   rL   r   r   rQ   rY   GeneralObjectExporterparse)	r   r  r  srX  r  nr   
unused_raws	            r   	testBasicTest.testBasicJ  s    -MMOq"A AH		AHHQKHHQK  ^ 	V^^,Q/66q"=^	V^^,Q/66q"=^	V^^,Q/66q"=^	V^^,Q/66q"=^	V^^,Q/66q"= 335;;A>
r    c                .   SSK Jn  UR                  S5      R                  S   nSnUR	                  5        HI  nSUR
                  ;   d  M  U[        UR                  5      S-   [        UR                  5      -   S-   -  nMK     U R                  US5        g )	Nr   corpuszluca/gloriar   rH   z: ri   z#1: 1, 2: 23, 3: 50, 4: 80, 5: 103, )
r  r  r  r   r   r   r   rJ   r   assertEqual)r   r  cretStrxs        r   x_testGetPageMeasureNumbers Test.x_testGetPageMeasureNumbers|  s}    "LL'--a0Aqyy(#all+d2S5IIDPP  	!FGr    c                L   SSK Jn  SSK Jn  UR                  S5      nUR	                  USS9nUR
                  S   R                  S   R                  S   nU R                  [        U5      R                  S	5      [        U5      5        U R                  UR                  5        g
)z4
we have had problems with attributes disappearing.
r   r  )r   zdemos/layoutTest.xmlT)r      r   zStaff 11: p.1, sys.4, st.2>N)r  r  r   r  r   r
  r  r4  
assertTruereprendswithassertIsNotNoner   )r   r  r   ltlsrG  s         r   testGetStaffLayoutFromStaff Test.testGetStaffLayoutFromStaff  s     	#"\\01!!"4!8hhqk))!,33A6[)223PQ[)	+[445r    r   N)r!   r"   r#   r$   r  r  r  r'   r   r    r   r  r  H  s    0?d	H6r    r  __main__)FF)r   zstream.Scorer   r  r   r  rD   r   )r   )3r%   
__future__r   r   unittestcollectionsr   typingr   r  r   music21.common.enumsr   r   r   r	   r
   music21.stream.enumsr   EnvironmentrU  r   r   Music21Objectr   r)   rH   rX   r^   Music21Exceptionrn   SpannerExceptionrr   Spannerrt   r   r   r   r   Opusr   r   r  r   r   r   
_DOC_ORDERTestCaser  r!   mainTestr   r    r   <module>r     s  KX #   "   /      *&&x0 &HI
j"WX## "I6* I6Z. .h(: (VTS* TSr	l33 		'22 	
d dX DDD D 	DN46 Fz&++ zz!E6;; !EJ.V\\ .8FFKK F4 :|[*40

K68 K6^ zT r    