~s-cecilio/lomse/master

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
//---------------------------------------------------------------------------------------
//  This file is part of the Lomse library.
//  Copyright (c) 2010 Lomse project
//
//  Lomse is free software; you can redistribute it and/or modify it under the
//  terms of the GNU General Public License as published by the Free Software Foundation,
//  either version 3 of the License, or (at your option) any later version.
//
//  Lomse is distributed in the hope that it will be useful, but WITHOUT ANY
//  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
//  PARTICULAR PURPOSE.  See the GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License along
//  with Lomse; if not, see <http://www.gnu.org/licenses/>.
//
//  For any comment, suggestion or feature request, please contact the manager of
//  the project at cecilios@users.sourceforge.net
//
//---------------------------------------------------------------------------------------

#include "lomse_score_layouter.h"

#include "lomse_basic_model.h"
#include "lomse_calligrapher.h"
#include "lomse_gm_basic.h"
#include "lomse_internal_model.h"
#include "lomse_box_system.h"
#include "lomse_box_slice.h"
#include "lomse_box_slice_instr.h"
#include "lomse_shape_staff.h"
#include "lomse_instrument_engraver.h"
#include "lomse_system_layouter.h"
#include "lomse_clef_engraver.h"


namespace lomse
{

//---------------------------------------------------------------------------------------
// ScoreLayouter implementation
//---------------------------------------------------------------------------------------

ScoreLayouter::ScoreLayouter(ImoDocObj* pImo, GraphicModel* pGModel, TextMeter* pMeter)
    : ContentLayouter(pImo, pGModel)
    , m_pTextMeter(pMeter)
    , m_scoreIt( get_imo_score()->get_staffobjs_table() )
//    , m_pSysCursor((SystemCursor*)NULL)
    , m_pStubScore(NULL)
    , m_pCurBoxPage(NULL)
    , m_pCurBoxSystem(NULL)
{
}

//---------------------------------------------------------------------------------------
ScoreLayouter::~ScoreLayouter()
{
    //delete_system_cursor();
    delete_instrument_layouters();
    delete_system_layouters();
}

//---------------------------------------------------------------------------------------
ImoScore* ScoreLayouter::get_imo_score()
{
    return dynamic_cast<ImoScore*>(m_pImo);
}

//---------------------------------------------------------------------------------------
void ScoreLayouter::prepare_to_start_layout()
{
    ContentLayouter::prepare_to_start_layout();

    create_stub_for_score();
    create_instrument_engravers();
 //   GetScoreRenderizationOptions();
 //   PrepareFontsThatMatchesStavesSizes();
    decide_systems_indentation();
 //   DecideSpaceBeforeProlog();

	m_nCurrentPageNumber = 0;
    m_nCurSystem = 0;
    m_uLastSystemHeight = 0.0f;
    m_nAbsColumn = 0;
}

//---------------------------------------------------------------------------------------
void ScoreLayouter::create_stub_for_score()
{
    m_pStubScore = new GmoStubScore( get_imo_score() );
    m_pGModel->add_stub(m_pStubScore);
}

//---------------------------------------------------------------------------------------
void ScoreLayouter::create_instrument_engravers()
{
    ImoScore* pScore = get_imo_score();
    for (int iInstr = 0; iInstr < pScore->get_num_instruments(); iInstr++)
    {
        ImoInstrument* pInstr = pScore->get_instrument(iInstr);
        m_instrEngravers.push_back( new InstrumentEngraver(pInstr, m_pTextMeter) );
    }
}

//---------------------------------------------------------------------------------------
void ScoreLayouter::delete_instrument_layouters()
{
    std::vector<InstrumentEngraver*>::iterator it;
    for (it = m_instrEngravers.begin(); it != m_instrEngravers.end(); ++it)
        delete *it;
    m_instrEngravers.clear();
}

//---------------------------------------------------------------------------------------
void ScoreLayouter::delete_system_layouters()
{
    std::vector<SystemLayouter*>::iterator it;
    for (it = m_sysLayouters.begin(); it != m_sysLayouters.end(); ++it)
        delete *it;
    m_sysLayouters.clear();
}

//---------------------------------------------------------------------------------------
void ScoreLayouter::decide_systems_indentation()
{
    m_uFirstSystemIndent = 0.0f;
    m_uOtherSystemIndent = 0.0f;
    std::vector<InstrumentEngraver*>::iterator it;
    for (it = m_instrEngravers.begin(); it != m_instrEngravers.end(); ++it)
    {
        (*it)->measure_indents();
        m_uFirstSystemIndent = max(m_uFirstSystemIndent, (*it)->get_indent_first());
        m_uOtherSystemIndent = max(m_uOtherSystemIndent, (*it)->get_indent_other());
    }
}

//---------------------------------------------------------------------------------------
void ScoreLayouter::layout_in_page(GmoBox* pContainerBox)
{
    //AWARE: This method is invoked to layout a page. If there are more pages to
    //layout, it will be invoked more times. Therefore, this method must not initialize
    //anything. All initializations must be done in 'prepare_to_start_layout()'.
    //layout_in_page() method must allways continue layouting from current state.

    page_initializations(pContainerBox);
    move_cursor_to_top_left_corner();
    add_titles_if_first_page();

    while(more_systems_to_add() && enough_space_in_page())
    {
        add_next_system();
    }

    set_layout_is_finished( !more_systems_to_add() );
}

//---------------------------------------------------------------------------------------
void ScoreLayouter::page_initializations(GmoBox* pContainerBox)
{
    m_pCurBoxPage = dynamic_cast<GmoBoxScorePage*>( pContainerBox );
    is_first_system_in_page(true);
    more_systems_to_add(true);
}

//---------------------------------------------------------------------------------------
GmoBox* ScoreLayouter::create_pagebox(GmoBox* pParentBox)
{
    GmoBox* pBox = new GmoBoxScorePage(m_pStubScore, pParentBox);
    pBox->set_left( pParentBox->get_left() );
    pBox->set_top( pParentBox->get_top() );
    return pBox;
}

//---------------------------------------------------------------------------------------
void ScoreLayouter::add_titles_if_first_page()
{
    if (is_first_page())
    {
        add_score_titles();
        move_cursor_after_headers();
    }
}

//---------------------------------------------------------------------------------------
void ScoreLayouter::add_next_system()
{
    create_system_layouter();
    //create_system_cursor();
    create_system_box();
    fill_current_system_with_columns();
    justify_current_system();

    if (!more_systems_to_add())  // && m_fStopStaffLinesAtFinalBarline)
        truncate_current_system();
    //AddInitialLineJoiningAllStavesInSystem();
    //UpdateBoxSlicesSizes();
    set_system_height_and_advance_paper_cursor();
    is_first_system_in_page(false);
}

////---------------------------------------------------------------------------------------
//void ScoreLayouter::create_system_cursor()
//{
//    delete_system_cursor();
//    m_pSysCursor = new SystemCursor(m_pScore);
//}

////---------------------------------------------------------------------------------------
//void ScoreLayouter::delete_system_cursor()
//{
//    if (m_pSysCursor)
//        delete m_pSysCursor;
//}

//---------------------------------------------------------------------------------------
bool ScoreLayouter::enough_space_in_page()
{
    //TODO ************
    //  If paper height is smaller than system height it is impossible to fit
    //  one system in a page. We have to split system horizontally (some staves in
    //  one page and the others in next page).

    //TODO ************
    //By using m_uLastSystemHeight to determine if there is enough space we are
    //assuming that next system height will be equal to last finished system.

    return remaining_height() >= m_uLastSystemHeight;
}

//---------------------------------------------------------------------------------------
LUnits ScoreLayouter::remaining_height()
{
    return m_pCurBoxPage->get_height() - m_pageCursor.y;
}

//---------------------------------------------------------------------------------------
void ScoreLayouter::move_cursor_to_top_left_corner()
{
    m_pageCursor.x = m_pCurBoxPage->get_left();
    m_pageCursor.y = m_pCurBoxPage->get_top();
}

//---------------------------------------------------------------------------------------
void ScoreLayouter::add_score_titles()
{
    //TODO
    //m_pScore->LayoutAttachedObjects(m_pStubScore->GetCurrentPage(), m_pPaper);
}

//---------------------------------------------------------------------------------------
void ScoreLayouter::move_cursor_after_headers()
{
    //TODO
    move_cursor_to_top_left_corner();
    //m_pageCursor.y += m_pScore->GetHeadersHeight();
}

//---------------------------------------------------------------------------------------
void ScoreLayouter::create_system_layouter()
{
    m_sysLayouters.push_back(
        new SystemLayouter() );   //m_rSpacingFactor, m_nSpacingMethod, m_nSpacingValue) );
}

//---------------------------------------------------------------------------------------
void ScoreLayouter::create_system_box()
{
    //m_nColumnsInSystem = 0;

    //create system box
    m_pCurBoxSystem = m_pCurBoxPage->add_system(m_nCurSystem++);
    //m_pCurBoxSystem->SetFirstMeasure(m_nAbsColumn);

    //set origin
    m_pCurBoxSystem->set_origin(m_pageCursor.x, m_pageCursor.y);

    //left margin
    LUnits left = 500.0f; //pScore->get_system_left_space(iSystem);
    m_pCurBoxSystem->set_left_margin(left);

    //width
    m_pCurBoxSystem->set_width( m_pCurBoxPage->get_width() );

    //move x cursor to system left marging
    m_pageCursor.x += left;
}

//---------------------------------------------------------------------------------------
void ScoreLayouter::set_system_height_and_advance_paper_cursor()
{
    //TODO
//    //A system has been layouted. Update last system heigh record (without bottom space)
//    //and advance paper cursor to next system position
//
//    //AWARE: In GetSystemDistance() we are using m_nCurSystem instead of
//    //m_nCurSystem-1. This is to get the system distance between this system
//    //and next one.
//    LUnits uSystemBottomSpace = m_pScore->GetSystemDistance(m_nCurSystem, false) / 2.0;
//    m_pCurBoxSystem->SetBottomSpace(uSystemBottomSpace);
      m_uLastSystemHeight = m_pCurBoxSystem->get_height();

    //advance paper in system bottom space
    m_pageCursor.x = m_pCurBoxPage->get_left();
    m_pageCursor.y = m_pCurBoxSystem->get_bottom();
}

//---------------------------------------------------------------------------------------
void ScoreLayouter::fill_current_system_with_columns()
{
    m_nRelColumn = 0;
    do
    {
        create_column_and_add_it_to_current_system();
    }
    while(!m_scoreIt.is_end() && !must_terminate_system());

    more_systems_to_add( !m_scoreIt.is_end() );
}

//---------------------------------------------------------------------------------------
void ScoreLayouter::justify_current_system()
{
    //ComputeMeasuresSizesToJustifyCurrentSystem(fThisIsLastSystem);
    //RepositionStaffObjs();
}

//---------------------------------------------------------------------------------------
void ScoreLayouter::create_column_and_add_it_to_current_system()
{
    //Creates a column and adds it to current system, if enough space.
    //The column is sized and this space discunted from available line space.
    //Returns true if either:
    //  - current system is completed,
    //  - there is not enough space tor include this column in current system, or
    //  - if a newSystem tag is found.
    //If not enough space for adding the column, SystemCursor is repositined again at
    //start of this column and nothing is added to current system.

//    //reposition paper vertically at the start of the system. It has been advanced
//    //when sizing the previous column
//    m_pageCursor.y = m_uStartOfCurrentSystem;
//

    create_column_boxes();
    must_terminate_system(false);
    collect_content_for_this_bar();
    measure_this_bar();

//    //if this is the first column compute the space available in
//    //this system. The method is a little tricky. The total space available
//    //is (pScore->GetPageRightMargin() - pScore->GetCursorX()). But we have
//    //to take into account the space that will be used by the prolog. As the
//    //left position of the first column has taken all this into account,
//    //it is posible to use that value by just doing:
//    if (is_first_column_in_system())
//    {
//        m_uFreeSpace = m_pScore->GetRightMarginXPos()
//                       - m_pScore->GetSystemLeftSpace(m_nCurSystem - 1)
//                       - m_sysLayouters[m_nCurSystem-1]->GetStartPositionForColumn(m_nRelColumn);
//    }
//
//    //check if there is enough space to add this column to current system
//    if(m_uFreeSpace < m_sysLayouters[m_nCurSystem-1]->GetMinimumSize(m_nRelColumn))
//	{
//        //there is no enough space for this column.
//
//        //restore cursors to re-process this column
//        m_pSysCursor->GoBackPrevPosition();
//
//        //if no column added to system, the line width is not enough for drawing
//        //just one measure or no measures in score (i.e. no time signature).
//        //We have to split the current column and reprocess it
//        if (m_nColumnsInSystem == 0)
//        {
//            //determine break time to split this column
//            SplitColumn(m_uFreeSpace);
//        }
//
//        //discard measurements for current column
//        m_sysLayouters[m_nCurSystem-1]->DiscardMeasurementsForColumn(m_nRelColumn);
//        m_pCurBoxSystem->DeleteLastSlice();
//
//        //if at least one column in current system, the system is finished
//        if (m_nColumnsInSystem > 0)
//            return true;    //terminate system
//    }
//    else
//    {
//        //there is enough space for this column. Add it to system
        add_column_to_system();
//    }
}

//---------------------------------------------------------------------------------------
void ScoreLayouter::add_slice_box()
{
    m_pCurSlice = m_pCurBoxSystem->add_slice(m_nAbsColumn);

	m_pCurSlice->set_left(m_pCurBoxSystem->get_content_left());
	m_pCurSlice->set_top(m_pCurBoxSystem->get_top());
	m_pCurSlice->set_width(m_pCurBoxSystem->get_content_width());

    m_pageCursor.y = m_pCurSlice->get_top();
}

//---------------------------------------------------------------------------------------
void ScoreLayouter::create_column_boxes()
{
    m_sliceInstrBoxes.clear();
    add_slice_box();
    ImoScore* pScore = get_imo_score();
    m_pCurBSI = NULL;
    ImoInstrument* pInstr = NULL;
    int maxInstr = pScore->get_num_instruments() - 1;

    for (int iInstr = 0; iInstr <= maxInstr; iInstr++)
    {
        m_pageCursor.x = m_pCurSlice->get_left();    //align start of all staves
        pInstr = pScore->get_instrument(iInstr);
        LUnits uMargin = determine_top_space(iInstr, pInstr);

        if (iInstr > 0)
            terminate_slice_instr(iInstr-1, uMargin);

        start_slice_instr(pInstr, iInstr, uMargin);
    }

    //set last SliceInstr height
    LUnits uBottomMargin = pInstr->get_staff(0)->get_staff_margin() / 2.0f;
    terminate_slice_instr(maxInstr, uBottomMargin);

    //set slice and system height
    LUnits uTotalHeight = m_pageCursor.y - m_pCurSlice->get_top();
    m_pCurSlice->set_height(uTotalHeight);
    if (is_first_column_in_system())
        m_pCurBoxSystem->set_height(uTotalHeight);
}

void ScoreLayouter::measure_this_bar()
{

        //    //restore x cursor to paper left margin
//    m_pageCursor.x = m_pScore->GetPageLeftMargin();
//
//    //all measures in column number m_nAbsColumn have been sized. The information is stored in
//    //object SystemLayouter. Now proced to re-position the StaffObjs so that all StaffObjs
//    //sounding at the same time will have the same x coordinate.
//    bool fTrace =  m_fDebugMode && (m_nTraceMeasure == 0  || m_nTraceMeasure == m_nAbsColumn);
//
//    m_sysLayouters[m_nCurSystem-1]->EndOfSystemMeasurements();
//    m_sysLayouters[m_nCurSystem-1]->DoColumnSpacing(m_nRelColumn, fTrace);
}

//---------------------------------------------------------------------------------------
LUnits ScoreLayouter::determine_top_space(int nInstr, ImoInstrument* pInstr)
{
    if (nInstr == 0)
    {
        ImoScore* pScore = get_imo_score();
        if (is_first_system_in_page())
        {
            ImoSystemInfo* pInfo = pScore->get_first_system_info();
            return pInfo->get_top_system_distance();
        }
        else
        {
            ImoSystemInfo* pInfo = pScore->get_other_system_info();
            return pInfo->get_system_distance() / 2.0f;
        }
    }
    else
        return pInstr->get_staff(0)->get_staff_margin() / 2.0f;
}

//---------------------------------------------------------------------------------------
void ScoreLayouter::start_slice_instr(ImoInstrument* pInstr, int iInstr, LUnits uTopMargin)
{
    m_pCurBSI = m_pCurSlice->add_box_for_instrument(pInstr);
    m_sliceInstrBoxes.push_back( m_pCurBSI );
	m_pCurBSI->set_top( m_pageCursor.y );
	m_pCurBSI->set_left( m_pCurSlice->get_left() );
	m_pCurBSI->set_width( m_pCurSlice->get_width() );

    m_pageCursor.y += uTopMargin;

	if (is_first_column_in_system())
        add_staff_lines_name_and_bracket(iInstr, uTopMargin);
}

//---------------------------------------------------------------------------------------
void ScoreLayouter::terminate_slice_instr(int iInstr, LUnits uBottomMargin)
{
    m_pageCursor.y += uBottomMargin;
	m_pCurBSI->set_height( m_pageCursor.y - m_pCurBSI->get_top() );
}

//---------------------------------------------------------------------------------------
void ScoreLayouter::add_staff_lines_name_and_bracket(int iInstr, LUnits uTopMargin)
{
    LUnits indent = get_system_indent();
    InstrumentEngraver* engraver = get_instrument_engraver(iInstr);
    engraver->add_staff_lines(m_pCurBoxSystem, m_pageCursor.x, m_pageCursor.y, indent);
    engraver->add_name_abbrev(m_pCurBSI, m_nCurSystem);
    engraver->add_brace_bracket(m_pCurBSI);
    m_pageCursor.y = engraver->get_staff_bottom();
}

//---------------------------------------------------------------------------------------
InstrumentEngraver* ScoreLayouter::get_instrument_engraver(int iInstr)
{
    std::vector<InstrumentEngraver*>::iterator it = m_instrEngravers.begin();
    for (; it != m_instrEngravers.end() && iInstr > 0; ++it, --iInstr);
    return *it;
}

//---------------------------------------------------------------------------------------
void ScoreLayouter::truncate_current_system()
{
    //system must be truncated at final barline if requested. Otherwise it must go
    //to right margin. Set here the applicable lenght.

//    if (fThisIsLastSystem && m_fStopStaffLinesAtFinalBarline)
//    {
//        //this is the last system and it has been requested to stop staff lines
//        //in last measure. So, set final x so staff lines go to final bar line
//        LUnits xFinalPos = 0.0f;
//        LUnits yFinalPos = 0.0f;
//        ImoInstrument *pI;
//        for (pI = m_pScore->GetFirstInstrument(); pI; pI=m_pScore->GetNextInstrument())
//        {
//            LUnits xPos, yPos;
//            pI->GetVStaff()->GetBarlineOfLastNonEmptyMeasure(&xPos, &yPos);
//            if (yPos > yFinalPos)
//            {
//                yFinalPos = yPos;
//                xFinalPos = xPos;
//            }
//        }
//        if (xFinalPos > 0.0f)
//            m_pCurBoxSystem->UpdateXRight( xFinalPos - 1 );
//        else
//            m_pCurBoxSystem->UpdateXRight( m_pScore->GetRightMarginXPos() );
//    }
}

//---------------------------------------------------------------------------------------
void ScoreLayouter::collect_content_for_this_bar()  //ImoInstrument* pInstr, int iInstr)
{
//    // Compute the width of the current measure (or remaining part of it) of the lmVStaff
//    // Input variables:
//    //   pVStaff - lmVStaff to process
//	//   nInstr - instrument number 0..n
//    //   m_pSysCursor is pointing to current position
//    //   m_nRelColumn and m_nAbsColumn have the relative and absolute numbers for current
//    //      column beign measured
//    //   flag is_first_column_in_system() signals if this is the first column of current system
//    //
//    // Results:
//    //   all measurements are stored in SystemLayouter
//    //   Return bool: true if newSystem tag found in this measure
//
//    //add some space at start of measure, if necessary
    LUnits uSpaceAfterStart = 0.0f;
//    if (is_first_column_in_system())
//    {
//        //if first measure of system, add some space before prolog
//        uSpaceAfterStart = m_uSpaceBeforeProlog;
//    }
//    else
//    {
//        //Not first measure of system. Get the previous barline and add some space if
//        //the previous barline is visible.
//        ImoBarline* pBar = m_pSysCursor->GetPreviousBarline(nInstr);
//        if (pBar)
//        {
//            if (pBar->IsVisible())
//                uSpaceAfterStart = pVStaff->TenthsToLogical(20.0f);    // TODO: user options
//        }
//    }
//
//    //ask system formatter to prepare to receive data for this instrument objects in this column
    LUnits uxStart = m_pageCursor.x;

    SystemLayouter* pSysLayouter = m_sysLayouters[m_nCurSystem-1];
    pSysLayouter->start_bar_measurements(m_nRelColumn, uxStart, uSpaceAfterStart);
//
//    //The prolog (clef and key signature) must be rendered on each system, but the
//    //matching StaffObjs only exist in the first system. In the first system the prolog
//	//is rendered as part as the normal StaffObj rendering process, so there is nothig
//	//special to do to render the prolog. But for the other systems we must force the
//	//rendering of the prolog because there are no StaffObjs representing the prolog.
    bool fProlog = (is_first_column_in_system());        //fProlog -> we are adding prolog objects
//    if (m_nAbsColumn != 1 && is_first_column_in_system())
//	{
//		AddProlog(m_pCurBSI, false, pVStaff, nInstr);
//        fProlog = false;                    //prolog added
//	}

    //Pre-allocate common engravers
    ClefEngraver clefEngrv;


//    //loop to process all StaffObjs in this measure
    bool fNewSystemTagFound = false;                //newSystem tag found
    ImoStaffObj* pSO = NULL;
    //ScoreIterator sit = m_pSysCursor->get_iterator(iInstr);
////    m_scoreIt.ResetFlags();
    while(!m_scoreIt.is_end() )   //&& !m_scoreIt.change_of_measure())
    {
        pSO = dynamic_cast<ImoStaffObj*>( (*m_scoreIt)->imo_object() );
        int iInstr = (*m_scoreIt)->num_instrument();
        int iStaff = (*m_scoreIt)->staff();
        LUnits lineSpacing = get_line_spacing(iInstr, iStaff);
//
//        if (pSO->IsBarline() || IsHigherTime(pSO->GetTimePos(), m_pSysCursor->GetBreakTime()) )
//        {
//             break;         //End of measure: exit loop.
//        }
//
//        else if (pSO->IsControl())
//        {
//            ESOCtrolType nCtrolType = ((lmSOControl*)pSO)->GetCtrolType();
//            if(lmNEW_SYSTEM == nCtrolType)
//            {
//                //new system tag found in this measure
//                fNewSystemTagFound = true;
//            }
//			else {
//				wxLogMessage(_T("ScoreLayouter::collect_content_for_this_bar] Bad SOControl type"));
//				wxASSERT(false);
//			}
//        }
//
        /*else*/ if (pSO->is_clef())
		{
			m_pageCursor.x = uxStart;
			//pSO->Layout(m_pCurBSI, m_pPaper);
			//GmoShape* pShape = pSO->GetShape();
            ImoClef* pClef = dynamic_cast<ImoClef*>(pSO);
            GmoShape* pShape = clefEngrv.create_shape(pClef, m_pCurBSI, m_pageCursor,
                                                      lineSpacing);
            pSysLayouter->include_object(m_nRelColumn, iInstr, pSO, fProlog,
                                         iStaff, pShape);
		}
//        else if (pSO->IsKeySignature())
//		{
//			m_pageCursor.x =uxStart);
//			AddKey((lmKeySignature*)pSO, m_pCurBSI, pVStaff, nInstr, fProlog);
//        }
//
//        else if (pSO->IsTimeSignature())
//		{
//			m_pageCursor.x =uxStart);
//			AddTime((TimeSignature*)pSO, m_pCurBSI, pVStaff, nInstr, fProlog);
//		}

		else
		{
//            //it is neither clef, key signature nor time signature. Finish prologue
//            fProlog = false;
//
//			//create this lmStaffObj shape and add to table
//			m_pageCursor.x =uxStart);
//			pSO->Layout(m_pCurBSI, m_pPaper);
//			GmoShape* pShape = pSO->GetShape();
//            pSysLayouter->IncludeObject(m_nRelColumn, nInstr, pSO, pShape, fProlog);
        }

        ++m_scoreIt;
    }

//    //The barline lmStaffObj is not included in the loop as it might not exist in the last
//    //bar of a score. In theses cases, the loop is exited because the end of the score is
//    //reached. In any case we have to close the line
//    if (pSO && pSO->IsBarline())
//    {
//        ++sit;    //leave cursor pointing to next measure
//
//        m_pageCursor.x =uxStart);
//        pSO->Layout(m_pCurBSI, m_pPaper);
//        GmoShape* pShape = pSO->GetShape();
//        pSysLayouter->include_barline_and_terminate_bar_measurements(m_nRelColumn, pSO, uxStart);
//    }
//    else
//	{
//        // no barline at the end of the measure.
        pSysLayouter->terminate_bar_measurements_without_barline(m_nRelColumn, uxStart);

//        //force new system if a break point reached
//        if (pSO && IsHigherTime(pSO->GetTimePos(), m_pSysCursor->GetBreakTime()))
//            fNewSystemTagFound = true;
//    }

    must_terminate_system( fNewSystemTagFound );
}

//---------------------------------------------------------------------------------------
void ScoreLayouter::add_column_to_system()
{
//    //A column has been processed, it has been checked that there is enough space to include it
//    //in current system and, finally, decided to include it. This method does whatever
//    //is necessary to include the column and consolidate the situation.
//
//    //Add column to current system and discount the space that the measure will take
//    m_uFreeSpace -= m_sysLayouters[m_nCurSystem-1]->GetMinimumSize(m_nRelColumn);
//    m_nColumnsInSystem++;
//    m_pSysCursor->CommitCursors();
    add_shapes_for_score_objs();

//
//    //mark all objects in column as 'non dirty'
//    m_sysLayouters[m_nCurSystem-1]->ClearDirtyFlags(m_nRelColumn);
//
//    //prepare to create a new column
//    m_nRelColumn++;
//    m_nAbsColumn++;
}

//---------------------------------------------------------------------------------------
void ScoreLayouter::add_shapes_for_score_objs()
{
    m_sysLayouters[m_nCurSystem-1]->add_shapes(m_sliceInstrBoxes);
}

//---------------------------------------------------------------------------------------
LUnits ScoreLayouter::get_line_spacing(int iInstr, int iStaff)
{
    ImoScore* pScore = get_imo_score();
    ImoInstrument* pInstr = pScore->get_instrument(iInstr);
    return pInstr->get_staff(iStaff)->get_line_spacing();
}



////-----------------------------------------------------------------------------------------
//// ScoreLayouter implementation OLD CODE TO REVIEW
////-----------------------------------------------------------------------------------------
//
//---------------------------------------------------------------------------------------
//void ScoreLayouter::DeleteSystemLayouters()
//{
//    std::vector<SystemLayouter*>::iterator it;
//    for (it=m_sysLayouters.begin(); it != m_sysLayouters.end(); ++it)
//        delete *it;
//    m_sysLayouters.clear();
//
//}
//
//---------------------------------------------------------------------------------------
//void ScoreLayouter::SplitColumn(LUnits uAvailable)
//{
//    //We have measured a column and it doesn't fit in current system. Split it.
//    //All information about column to split is stored in the SystemLayouter.
//    //Parameter uAvailable is the available space in current system
//
//    float rTime;
//    LUnits uWidth;
//    if (m_sysLayouters[m_nCurSystem-1]->GetOptimumBreakPoint(m_nRelColumn, uAvailable, &rTime, &uWidth))
//    {
//        wxString sMsg = _("Program failure: not enough space for drawing just one bar.");
//        ::wxLogFatalError(sMsg);
//    }
//
//    m_pSysCursor->SetBreakTime(rTime);
//}
//
//---------------------------------------------------------------------------------------
//LUnits ScoreLayouter::AddEmptySystem(int nSystem, GmoBoxSystem* pBoxSystem)
//{
//    //Add staff lines to received GmoBoxSystem. Returns system height including bottom margin.
//    //Paper is left positioned at next staff position
//
//    LUnits uSystemHeight = - m_pageCursor.y;
//
//    // explore all instruments in the score
//    LUnits xStartPos = m_pageCursor.x;
//    LUnits uyBottom;
//    int nInstr = 0;
//    ImoInstrument* pInstr = m_pScore->GetFirstInstrument();
//    while (pInstr)
//    {
//        m_pageCursor.x = xStartPos );    //align start of staves in this system
//
//        LUnits yPaperPos = m_pageCursor.y;
//
//        lmVStaff* pVStaff = pInstr->GetVStaff();
//
//        //Top space
//        LUnits uTopMargin;
//        if (nInstr == 0)
//        {
//            //First instrument of this system
//            uTopMargin = pBoxSystem->get_top_space();
//
//        }
//        else
//        {
//            //Not first instrument of system. Split distance: half for each instrument
//            uTopMargin = pVStaff->GetFirstStaff()->GetStaffDistance() / 2.0f;
//
//            //advance paper to top limit of current instrument
//            m_pageCursor.y += uTopMargin;
//            yPaperPos = m_pageCursor.y;
//        }
//
//        //Here paper is positioned at top limit of current instrument
//
//        //advance paper to top line of first staff (instrument top bounds)
//        m_pageCursor.y += uTopMargin;
//        yPaperPos = m_pageCursor.y;
//
//        //Add staff lines for current instrument. As final xPos is yet unknown, so I use zero.
//		//It will be updated when the system is completed
//		uyBottom = pVStaff->LayoutStaffLines(pBoxSystem, pInstr, xStartPos,
//                                             0.0f, m_pageCursor.y);
//
//        //advance paper in height of this lmVStaff
//        m_pageCursor.y =uyBottom );
//
//        //proceed with next instrument
//        pInstr = m_pScore->GetNextInstrument();
//        ++nInstr;
//    }
//
//    //restore x cursor to paper left margin
//    m_pageCursor.x = m_pScore->GetPageLeftMargin() );
//
//    //set system bottom position
//    pBoxSystem->SetYBottom(uyBottom);
//
//
//    uSystemHeight += m_pageCursor.y;
//    return uSystemHeight;
//}
//
//---------------------------------------------------------------------------------------
//void ScoreLayouter::RedistributeFreeSpace(LUnits uAvailable, bool fLastSystem)
//{
//    //Step 3: Justify measures (distribute remainnig space across all measures)
//    //-------------------------------------------------------------------------------
//    //Redistributes the space to try to have all columns with equal witdh.
//    //The system is not justified if this is the last system and there is no barline
//    //in the last measure.
//    //
//    //on entering in this function:
//    // - object SystemLayouter stores the minimum size for each column for
//    //   the current system.
//    // - uAvailable stores the free space remaining at the end of this system
//    //
//    //on exit:
//    // - the values stored in SystemLayouter are modified to reflect the new size
//    //   for the bar columns, so that the line get justified.
//    //
//    //-------------------------------------------------------------------------------------
//
//    if (uAvailable <= 0.0f) return;       //no space to distribute
//
//    //The system must not be justified if this is the last system and there is no barline
//    //in the last bar. Check this.
//    SystemLayouter* pSysFmt = m_sysLayouters[m_nCurSystem-1];
//    if (fLastSystem && !pSysFmt->ColumnHasBarline(m_nColumnsInSystem-1))
//            return;     //no need to justify
//
//   //compute average column size and total occupied
//    LUnits uTotal = 0.0f;
//    for (int i = 0; i < m_nColumnsInSystem; i++)
//    {
//         uTotal += pSysFmt->GetMinimumSize(i);
//    }
//    LUnits uAverage = (uTotal + uAvailable) / m_nColumnsInSystem;
//
//    //for each column, compute the diference between its size and the average target size
//    //sum up all the diferences in uDifTotal
//    std::vector<LUnits> uDif(m_nColumnsInSystem, 0.0f);
//    LUnits uDifTotal = 0;
//    int nNumSmallerColumns = 0;      //num of columns smaller than average
//    for (int i = 0; i < m_nColumnsInSystem; i++)
//    {
//        uDif[i] = uAverage - pSysFmt->GetMinimumSize(i);
//        if (uDif[i] > 0.0f)
//        {
//            uDifTotal += uDif[i];
//            nNumSmallerColumns++;
//        }
//    }
//
//    //distribute space
//    if (uDifTotal > uAvailable)
//    {
//        //not enough space to make all equal
//        LUnits uReduce = (uDifTotal - uAvailable) / nNumSmallerColumns;
//        for (int i = 0; i < m_nColumnsInSystem; i++)
//        {
//            if (uDif[i] > 0.0f)
//            {
//                uDif[i] -= uReduce;
//                pSysFmt->IncrementColumnSize(i, uDif[i]);
//            }
//        }
//    }
//    else
//    {
//        //enough space to make all columns equal size
//        for (int i = 0; i < m_nColumnsInSystem; i++)
//        {
//            if (uDif[i] > 0.0f)
//            {
//                pSysFmt->IncrementColumnSize(i, uDif[i]);
//            }
//        }
//    }
//
//}
//
//
////=========================================================================================
//// Methods to deal with measures
////=========================================================================================
//---------------------------------------------------------------------------------------
//void ScoreLayouter::AddProlog(GmoBoxSliceInstr* pBSI, bool fDrawTimekey, lmVStaff* pVStaff,
//                             int nInstr)
//{
//    // The prolog (clef and key signature) must be rendered on each system,
//    // but the matching StaffObjs only exist in the first system. Therefore, in the
//    // normal staffobj rendering process, the prolog would be rendered only in
//    // the first system.
//    // So, for the other systems it is necessary to force the rendering
//    // of the prolog because there are no StaffObjs representing it.
//    // This method does it.
//    //
//    // To know what clef, key and time signature to draw we take this information from the
//    // context associated to first note of the measure on each staff. If there are no notes,
//    // the context is taken from the barline. If, finally, no context is found, no prolog
//    // is drawn.
//
//    LUnits uPrologWidth = 0.0f;
//    lmClef* pClef = (lmClef*)NULL;
//    lmEClefType nClef = lmE_Undefined;
//    lmKeySignature* pKey = (lmKeySignature*)NULL;
//    TimeSignature* pTime = (TimeSignature*)NULL;
//
//    //AWARE when this method is invoked the paper position must be at the left marging,
//    //at the start of a new system.
//    LUnits xStartPos = m_pageCursor.x;      //Save x to align all clefs
//    LUnits yStartPos = m_pageCursor.y;
//
//    //iterate over the collection of lmStaff objects to draw current clef and key signature
//
//    lmStaff* pStaff = pVStaff->GetFirstStaff();
//    LUnits uyOffset = 0.0f;
//    LUnits xPos = 0.0f;
//
//    SystemLayouter* pSysLayouter = m_sysLayouters[m_nCurSystem-1];
//    lmContext* pContext = (lmContext*)NULL;
//    for (int nStaff=1; nStaff <= pVStaff->GetNumStaves(); pStaff = pVStaff->GetNextStaff(), nStaff++)
//    {
//        xPos = xStartPos;
//        if (nStaff > 1)
//            uyOffset += pStaff->GetStaffDistance();
//
//            //locate context for first note in this staff
//        pContext = m_pSysCursor->GetStartOfColumnContext(nInstr, nStaff);
//
//        if (pContext)
//        {
//            pClef = pContext->GetClef();
//            pKey = pContext->GetKey();
//            pTime = pContext->GetTime();
//
//            //render clef
//            if (pClef)
//            {
//                nClef = pClef->GetClefType();
//				if (pClef->IsVisible())
//                {
//					lmUPoint uPos = lmUPoint(xPos, yStartPos+uyOffset);        //absolute position
//					GmoShape* pShape = pClef->CreateShape(pBSI, m_pPaper, uPos);
//                    pShape->SetShapeLevel(lm_ePrologShape);
//					xPos += pShape->GetWidth();
//                    pSysLayouter->IncludeObject(m_nRelColumn, nInstr, pClef, pShape, true);
//				}
//            }
//
//            //render key signature
//            if (pKey && pKey->IsVisible())
//            {
//                lmUPoint uPos = lmUPoint(xPos, yStartPos+uyOffset);        //absolute position
//                GmoShape* pShape = pKey->CreateShape(pBSI, m_pPaper, uPos, nClef, pStaff);
//                pShape->SetShapeLevel(lm_ePrologShape);
//				xPos += pShape->GetWidth();
//                pSysLayouter->IncludeObject(m_nRelColumn, nInstr, pKey, pShape, true, nStaff);
//            }
//
//        }
//
//        //compute prolog width
//        uPrologWidth = wxMax(uPrologWidth, xPos - xStartPos);
//
//        //compute vertical displacement for next staff
//        uyOffset += pStaff->GetHeight();
//
//    }
//
//    // update paper cursor position
//    m_pageCursor.x =xStartPos + uPrologWidth);
//
//}
//
//---------------------------------------------------------------------------------------
//void ScoreLayouter::AddKey(lmKeySignature* pKey, Box* pBox,
//						  lmVStaff* pVStaff, int nInstr, bool fProlog)
//{
//    // This method is responsible for creating the key signature shapes for
//    // all staves of this instrument. And also, of adding them to the graphical
//    // model and to the Timepos table
//
//    //create the shapes
//    pKey->Layout(pBox, m_pPaper);
//
//	//add the shapes to the timepos table
//    SystemLayouter* pSysLayouter = m_sysLayouters[m_nCurSystem-1];
//	GmoShape* pMainShape = ((lmStaffObj*)pKey)->GetShape();          //cast forced because otherwise the compiler complains
//    for (int nStaff=1; nStaff <= pVStaff->GetNumStaves(); nStaff++)
//    {
//        GmoShape* pShape = pKey->GetShape(nStaff);
//        pSysLayouter->IncludeObject(m_nRelColumn, nInstr, pKey, pShape, fProlog, nStaff);
//    }
//
//}
//
//
//---------------------------------------------------------------------------------------
//void ScoreLayouter::AddTime(TimeSignature* pTime, Box* pBox,
//						   lmVStaff* pVStaff, int nInstr, bool fProlog)
//{
//    // This method is responsible for creating the time signature shapes for
//    // all staves of this instrument. And also, of adding them to the graphical
//    // model and to the Timepos table
//
//    //create the shapes
//    pTime->Layout(pBox, m_pPaper);
//
//	//add the shapes to the timepos table
//    SystemLayouter* pSysLayouter = m_sysLayouters[m_nCurSystem-1];
//	GmoShape* pMainShape = ((lmStaffObj*)pTime)->GetShape();          //cast forced because otherwise the compiler complains
//    for (int nStaff=1; nStaff <= pVStaff->GetNumStaves(); nStaff++)
//    {
//        GmoShape* pShape = pTime->GetShape(nStaff);
//        pSysLayouter->IncludeObject(m_nRelColumn, nInstr, pTime, pShape, fProlog, nStaff);
//    }
//
//}
//
//---------------------------------------------------------------------------------------
//void ScoreLayouter::RepositionStaffObjs()
//{
//    //SystemLayouter stores the final size that must have each column
//    //of this system. This method changes StaffObjs locations so that they are evenly
//    //distributed across the the bar.
//
//    //dbg ------------------------------------------------------------------------------
//    if (m_fDebugMode) {
//        wxLogMessage(_T("Before repositioning objects"));
//        wxLogMessage(_T("***************************************\n"));
//        wxLogMessage( m_pScore->Dump() );
//    }
//    //dbg ------------------------------------------------------------------------------
//
//    LUnits uxStartOfMeasure = m_sysLayouters[m_nCurSystem-1]->GetStartPositionForColumn(0);
//    for (int i=0; i < m_nColumnsInSystem; i++)
//    {
//        GmoBoxSlice* pBSlice = (GmoBoxSlice*)m_pCurBoxSystem->GetChildBox(i);
//        uxStartOfMeasure = m_sysLayouters[m_nCurSystem-1]->RedistributeSpace(i, uxStartOfMeasure);
//        m_sysLayouters[m_nCurSystem-1]->AddTimeGridToBoxSlice(i, pBSlice);
//    }
//
//    //dbg ------------------------------------------------------------------------------
//    if (m_fDebugMode) {
//        wxLogMessage(_T("After repositioning objects"));
//        wxLogMessage(_T("***************************************\n"));
//        wxLogMessage( m_pScore->Dump() );
//    }
//    //dbg ------------------------------------------------------------------------------
//
//}
//
//---------------------------------------------------------------------------------------
//void ScoreLayouter::GetScoreRenderizationOptions()
//{
//    // get options for renderization
//
//    m_fStopStaffLinesAtFinalBarline = m_pScore->GetOptionBool(_T("StaffLines.StopAtFinalBarline"));
//    m_fJustifyFinalBarline = m_pScore->GetOptionBool(_T("Score.JustifyFinalBarline"));
//    m_rSpacingFactor = (float) m_pScore->GetOptionDouble(_T("Render.SpacingFactor"));
//    m_nSpacingMethod = (ESpacingMethod) m_pScore->GetOptionLong(_T("Render.SpacingMethod"));
//    m_nSpacingValue = (Tenths) m_pScore->GetOptionLong(_T("Render.SpacingValue"));
//}
//
//---------------------------------------------------------------------------------------
//void ScoreLayouter::PrepareFontsThatMatchesStavesSizes()
//{
//    //for each staff size, setup fonts of right point size for that staff size
//    ImoInstrument *pInstr;
//    for (pInstr = m_pScore->GetFirstInstrument(); pInstr; pInstr=m_pScore->GetNextInstrument())
//    {
//        pInstr->GetVStaff()->SetUpFonts(m_pPaper);
//    }
//}
//
//---------------------------------------------------------------------------------------
//void ScoreLayouter::DecideSpaceBeforeProlog()
//{
//    //TODO. Now a fixed value of 7.5 tenths is used. User options ?
//
//    Tenths rSpaceBeforeProlog = 7.5f;
//	ImoInstrument* pInstr = m_pScore->GetFirstInstrument();
//	lmVStaff* pVStaff = pInstr->GetVStaff();
//	m_uSpaceBeforeProlog = pVStaff->TenthsToLogical(rSpaceBeforeProlog, 1);
//}
//
//---------------------------------------------------------------------------------------
//void ScoreLayouter::ComputeMeasuresSizesToJustifyCurrentSystem(bool fThisIsLastSystem)
//{
//    //TO ComputeMeasuresSizesToJustifyCurrentSystem divide up the remaining space
//    //between all bars, except if current system is the last one and flag
//    //"JustifyFinalBarline" is not set or there is no final barline.
//
//    //At this point the number of measures to include in current system has been computed
//    //and some data is stored in the following global variables:
//    //
//    //   SystemLayouter - positioning information for columns and
//    //          minimum size for each column for current system.
//    //   m_uFreeSpace - free space available on this system
//    //   m_nColumnsInSystem  - the number of measures that fit in this system
//    //
//    //Now we are going to compute new column sizes and store results
//    //in SystemLayouter but changes nothing in the StaffObjs
//
//    if (!fThisIsLastSystem || (fThisIsLastSystem && m_fJustifyFinalBarline))
//        RedistributeFreeSpace(m_uFreeSpace, fThisIsLastSystem);
//
//}
//
//---------------------------------------------------------------------------------------
//void ScoreLayouter::AddInitialLineJoiningAllStavesInSystem()
//{
//    //TODO: In current code, the decision about joining staves depends only on first
//    //instrument. This should be changed and the line should go from first visible
//    //staff to last visible one.
//
//    //TODO: For empty scores (no staffobj on any instrument) this initial line should
//    //not be drawn
//
//	//Add the shape for the initial barline that joins all staves in a system
//    lmVStaff* pVStaff = m_pScore->GetFirstInstrument()->GetVStaff();
//	if (m_pScore->GetOptionBool(_T("Staff.DrawLeftBarline")) && !pVStaff->HideStaffLines() )
//	{
//		LUnits uxPos = m_pCurBoxSystem->get_left() + get_system_indent();
//		LUnits uLineThickness = lmToLogicalUnits(0.2, lmMILLIMETERS);        // thin line width will be 0.2 mm TODO user options
//        GmoShapeSimpleLine* pLine =
//            new GmoShapeSimpleLine(pVStaff, uxPos, m_pCurBoxSystem->GetYTopFirstStaff(),
//						uxPos, m_pCurBoxSystem->GetYBottom(),
//						uLineThickness, 0.0, *wxBLACK, _T("System joining line"),
//						lm_eEdgeHorizontal);
//	    m_pCurBoxSystem->AddShape(pLine, lm_eLayerBarlines);
//	}
//}
//
//---------------------------------------------------------------------------------------
//void ScoreLayouter::UpdateBoxSlicesSizes()
//{
//    //update BoxSlices with the final measures sizes, except for last
//    //measure, as its length has been already set up
//
//    LUnits xEnd = m_sysLayouters[m_nCurSystem-1]->GetStartPositionForColumn(0);
//    for (int iRel=0; iRel < m_nColumnsInSystem; iRel++)
//    {
//        LUnits xStart = xEnd;
//        xEnd = xStart + m_sysLayouters[m_nCurSystem-1]->GetMinimumSize(iRel);
//        GmoBoxSlice* pBoxSlice = m_pCurBoxSystem->get_slice(iRel);
//		pBoxSlice->UpdateXLeft(xStart);
//        if (iRel < m_nColumnsInSystem)
//			pBoxSlice->UpdateXRight(xEnd);
//    }
//}
//
//---------------------------------------------------------------------------------------
//bool ScoreLayouter::RequestedToFillScoreWithEmptyStaves()
//{
//    return (!m_fStopStaffLinesAtFinalBarline
//            && m_pScore->GetOptionBool(_T("Score.FillPageWithEmptyStaves")) );
//}
//
//---------------------------------------------------------------------------------------
//void ScoreLayouter::FillPageWithEmptyStaves()
//{
//    //First system has been always added before arriving here. Fill the remaining
//    //page space with empty staves
//
//    //advance vertically the previous system bottom space
//    m_pageCursor.y +=  m_pScore->GetSystemDistance(m_nCurSystem, false) / 2.0 ;
//
//    while (true)      //loop is exited when reaching end of page
//    {
//        //Here Paper is positioned at the start of the new current system.
//
//        bool fFirstSystemInPage = false;
//
//        //TODO ************
//        //  By using nSystemHeight we are assuming that next system height is going
//        //  to be equal to last finished system. In this test it is necessary
//        //  to compute and use next system height
//        LUnits nNextSystemHeight = m_uLastSystemHeight;
//        LUnits yNew = m_pageCursor.y + nNextSystemHeight;
//        if ( yNew > m_pScore->GetMaximumY() )
//            break;        //exit loop
//
//        //create the system container
//        m_uStartOfCurrentSystem = m_pageCursor.y;      //save start of system position
//        m_pCurBoxSystem =
//            m_pCurBoxPage->add_system(m_nCurSystem, m_pageCursor.x,
//                                         m_uStartOfCurrentSystem, fFirstSystemInPage);
//        m_pCurBoxSystem->SetFirstMeasure(m_nAbsColumn);
//        m_pageCursor.x += m_pCurBoxSystem->get_left_margin();
//        m_pCurBoxSystem->SetIndent(get_system_indent());
//
//
//        m_nRelColumn = 0;          //first column of this system
//        m_nAbsColumn++;
//        m_uLastSystemHeight = AddEmptySystem(m_nCurSystem, m_pCurBoxSystem);     //Add the staff lines
//
//        //staff lines go to the rigth margin
//        m_pCurBoxSystem->UpdateXRight( m_pScore->GetRightMarginXPos() );
//
//        // compute system bottom space
//        //AWARE:
//        //  In GetSystemDistance() we are using m_nCurSystem instead of m_nCurSystem-1.
//        //  This is to get the system distance between this system and next one.
//        LUnits uSystemBottomSpace = m_pScore->GetSystemDistance(m_nCurSystem, false) / 2.0;
//        m_pCurBoxSystem->SetBottomSpace(uSystemBottomSpace);
//
//        //advance paper in system bottom space
//        m_pageCursor.y += uSystemBottomSpace;
//
//        //increment loop information
//        m_nCurSystem++;
//    }
//}
//
////=========================================================================================
//// methods coded only for Unit Tests
////=========================================================================================

//int ScoreLayouter::GetNumSystemLayouters() { return (int)m_sysLayouters.size(); }
//int ScoreLayouter::GetNumColumns(int iSys) { return m_sysLayouters[iSys]->GetNumColumns(); }
//int ScoreLayouter::GetNumLines(int iSys, int iCol)
//        { return m_sysLayouters[iSys]->GetNumLinesInColumn(iCol); }
//
//SystemLayouter* ScoreLayouter::get_system_layouter(int iSys)
//{
//    //iSys=[0..n-1]
//    return m_sysLayouters[iSys];
//}


}  //namespace lomse