File: read206.cpp

package info (click to toggle)
musescore3 3.2.3%2Bdfsg2-11
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 210,672 kB
  • sloc: cpp: 291,093; xml: 200,238; sh: 3,779; ansic: 1,447; python: 393; makefile: 240; perl: 82; pascal: 79
file content (4039 lines) | stat: -rw-r--r-- 176,088 bytes parent folder | download | duplicates (4)
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
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
//=============================================================================
//  MuseScore
//  Music Composition & Notation
//
//  Copyright (C) 2016 Werner Schweer and others
//
//  This program is free software; you can redistribute it and/or modify
//  it under the terms of the GNU General Public License version 2
//  as published by the Free Software Foundation and appearing in
//  the file LICENSE.GPL
//=============================================================================

#include "xml.h"
#include "score.h"
#include "staff.h"
#include "revisions.h"
#include "part.h"
#include "page.h"
#include "style.h"
#include "sym.h"
#include "arpeggio.h"
#include "audio.h"
#include "sig.h"
#include "barline.h"
#include "measure.h"
#include "ambitus.h"
#include "bend.h"
#include "chordline.h"
#include "hook.h"
#include "tuplet.h"
#include "systemdivider.h"
#include "spacer.h"
#include "keysig.h"
#include "stafftext.h"
#include "dynamic.h"
#include "drumset.h"
#include "timesig.h"
#include "slur.h"
#include "tie.h"
#include "chord.h"
#include "rest.h"
#include "breath.h"
#include "repeat.h"
#include "utils.h"
#include "read206.h"
#include "excerpt.h"
#include "articulation.h"
#include "volta.h"
#include "pedal.h"
#include "hairpin.h"
#include "glissando.h"
#include "ottava.h"
#include "trill.h"
#include "rehearsalmark.h"
#include "box.h"
#include "textframe.h"
#include "textline.h"
#include "fingering.h"
#include "fermata.h"
#include "image.h"
#include "stem.h"
#include "stemslash.h"
#include "undo.h"
#include "lyrics.h"
#include "tempotext.h"
#include "measurenumber.h"
#include "marker.h"

#ifdef OMR
#include "omr/omr.h"
#include "omr/omrpage.h"
#endif


namespace Ms {

static void readText206(XmlReader& e, TextBase* t, Element* be);

//---------------------------------------------------------
//   StyleVal206
//    this is a list of default style values which are
//    different in 3.x
//
// TODO: remove style values which are equal
//---------------------------------------------------------

struct StyleVal2 {
            Sid idx;
            QVariant val;
            };
      static const StyleVal2 style206[] = {
      { Sid::staffUpperBorder,            Spatium(7.0)  },
      { Sid::staffLowerBorder,            Spatium(7.0)  },
      { Sid::staffDistance,               Spatium(6.5)  },
      { Sid::akkoladeDistance,            Spatium(6.5)  },
      { Sid::minSystemDistance,           Spatium(8.5)  },
      { Sid::maxSystemDistance,           Spatium(15.0) },

//      { Sid::lyricsMinBottomDistance,     Spatium(4.0)  },      // no longer makes sense
      { Sid::lyricsLineHeight,            QVariant(1.0) },
      { Sid::lyricsDashForce,             QVariant(false) },
      { Sid::figuredBassFontFamily,       QVariant(QString("MScoreBC")) },
      { Sid::figuredBassFontSize,         QVariant(8.0) },
      { Sid::figuredBassYOffset,          QVariant(6.0) },
      { Sid::figuredBassLineHeight,       QVariant(1.0) },
      { Sid::figuredBassAlignment,        QVariant(0) },
      { Sid::figuredBassStyle,            QVariant(0) },
      { Sid::systemFrameDistance,         Spatium(7.0) },
      { Sid::frameSystemDistance,         Spatium(7.0) },
      { Sid::minMeasureWidth,             Spatium(5.0) },
      { Sid::barWidth,                    Spatium(0.16) },      // 0.1875
      { Sid::doubleBarWidth,              Spatium(0.16) },
      { Sid::endBarWidth,                 Spatium(0.5) },       // 0.5
      { Sid::doubleBarDistance,           Spatium(0.46) },     // 0.3 + doubleBarWidth
      { Sid::endBarDistance,              Spatium(0.65) },     // 0.3
      { Sid::repeatBarTips,               QVariant(false) },
      { Sid::startBarlineSingle,          QVariant(false) },
      { Sid::startBarlineMultiple,        QVariant(true) },
      { Sid::bracketWidth,                Spatium(0.45) },
      { Sid::bracketDistance,             Spatium(0.1) },
      { Sid::akkoladeWidth,               Spatium(1.6) },
      { Sid::akkoladeBarDistance,         Spatium(.4) },
      { Sid::clefLeftMargin,              Spatium(0.64) },
      { Sid::keysigLeftMargin,            Spatium(0.5) },
      { Sid::timesigLeftMargin,           Spatium(0.5) },
      { Sid::clefKeyRightMargin,          Spatium(1.75) },
      { Sid::clefBarlineDistance,         Spatium(0.5) },
      { Sid::stemWidth,                   Spatium(0.13) },      // 0.09375
      { Sid::shortenStem,                 QVariant(true) },
      { Sid::shortStemProgression,        Spatium(0.25) },
      { Sid::shortestStem,                Spatium(2.25) },
      { Sid::beginRepeatLeftMargin,       Spatium(1.0) },
      { Sid::minNoteDistance,             Spatium(0.25) },      // 0.4
      { Sid::barNoteDistance,             Spatium(1.2) },
      { Sid::barAccidentalDistance,       Spatium(.3) },
      { Sid::multiMeasureRestMargin,      Spatium(1.2) },
      { Sid::noteBarDistance,             Spatium(1.0) },
      { Sid::measureSpacing,              QVariant(1.2) },
      { Sid::staffLineWidth,              Spatium(0.08) },      // 0.09375
      { Sid::ledgerLineWidth,             Spatium(0.16) },     // 0.1875
      { Sid::ledgerLineLength,            Spatium(.6) },     // notehead width + this value
      { Sid::accidentalDistance,          Spatium(0.22) },
      { Sid::accidentalNoteDistance,      Spatium(0.22) },
      { Sid::beamWidth,                   Spatium(0.5) },           // was 0.48
      { Sid::beamDistance,                QVariant(0.5) },          // 0.25sp
      { Sid::beamMinLen,                  QVariant(1.32) },      // 1.316178 exactly notehead width
      { Sid::beamNoSlope,                 QVariant(false) },
      { Sid::dotMag,                      QVariant(1.0) },
      { Sid::dotNoteDistance,             QVariant(0.35) },
      { Sid::dotRestDistance,             QVariant(0.25) },
      { Sid::dotDotDistance,              QVariant(0.5) },
      { Sid::propertyDistanceHead,        QVariant(1.0) },
      { Sid::propertyDistanceStem,        QVariant(1.8) },
      { Sid::propertyDistance,            QVariant(1.0) },
      { Sid::articulationMag,             QVariant(1.0) },
      { Sid::lastSystemFillLimit,         QVariant(0.3) },
      { Sid::hairpinPosBelow,             QPointF(0.0, 3.5) },
      { Sid::hairpinHeight,               QVariant(1.2) },
      { Sid::hairpinContHeight,           QVariant(0.5) },
      { Sid::hairpinLineWidth,            QVariant(0.13) },
      { Sid::pedalPosBelow,               QPointF(0.0, 4) },
      { Sid::pedalLineWidth,              QVariant(.15) },
      { Sid::pedalLineStyle,              QVariant(int(Qt::SolidLine)) },
      { Sid::trillPosAbove,               QPointF(0.0, -1) },
      { Sid::harmonyFretDist,             QVariant(0.5) },
      { Sid::minHarmonyDistance,          QVariant(0.5) },
      { Sid::maxHarmonyBarDistance,       QVariant(3.0) },
      { Sid::capoPosition,                QVariant(0) },
      { Sid::fretNumMag,                  QVariant(2.0) },
      { Sid::fretNumPos,                  QVariant(0) },
      { Sid::fretY,                       QVariant(2.0) },
      { Sid::showPageNumber,              QVariant(true) },
      { Sid::showPageNumberOne,           QVariant(false) },
      { Sid::pageNumberOddEven,           QVariant(true) },
      { Sid::showMeasureNumber,           QVariant(true) },
      { Sid::showMeasureNumberOne,        QVariant(false) },
      { Sid::measureNumberInterval,       QVariant(5) },
      { Sid::measureNumberSystem,         QVariant(true) },
      { Sid::measureNumberAllStaffs,      QVariant(false) },
      { Sid::smallNoteMag,                QVariant(.7) },
      { Sid::graceNoteMag,                QVariant(0.7) },
      { Sid::smallStaffMag,               QVariant(0.7) },
      { Sid::smallClefMag,                QVariant(0.8) },
      { Sid::genClef,                     QVariant(true) },
      { Sid::genKeysig,                   QVariant(true) },
      { Sid::genCourtesyTimesig,          QVariant(true) },
      { Sid::genCourtesyKeysig,           QVariant(true) },
      { Sid::genCourtesyClef,             QVariant(true) },
      { Sid::swingRatio,                  QVariant(60)   },
      { Sid::swingUnit,                   QVariant(QString("")) },
      { Sid::useStandardNoteNames,        QVariant(true) },
      { Sid::useGermanNoteNames,          QVariant(false) },
      { Sid::useFullGermanNoteNames,      QVariant(false) },
      { Sid::useSolfeggioNoteNames,       QVariant(false) },
      { Sid::useFrenchNoteNames,          QVariant(false) },
      { Sid::automaticCapitalization,     QVariant(true) },
      { Sid::lowerCaseMinorChords,        QVariant(false) },
      { Sid::lowerCaseBassNotes,          QVariant(false) },
      { Sid::allCapsNoteNames,            QVariant(false) },
      { Sid::chordStyle,                  QVariant(QString("std")) },
      { Sid::chordsXmlFile,               QVariant(false) },
      { Sid::chordDescriptionFile,        QVariant(QString("chords_std.xml")) },
      { Sid::concertPitch,                QVariant(false) },
      { Sid::createMultiMeasureRests,     QVariant(false) },
      { Sid::minEmptyMeasures,            QVariant(2) },
      { Sid::minMMRestWidth,              Spatium(4) },
      { Sid::hideEmptyStaves,             QVariant(false) },
      { Sid::dontHideStavesInFirstSystem, QVariant(true) },
      { Sid::hideInstrumentNameIfOneInstrument, QVariant(true) },
      { Sid::gateTime,                    QVariant(100) },
      { Sid::tenutoGateTime,              QVariant(100) },
      { Sid::staccatoGateTime,            QVariant(50) },
      { Sid::slurGateTime,                QVariant(100) },
      { Sid::ArpeggioNoteDistance,        QVariant(.5) },
      { Sid::ArpeggioLineWidth,           QVariant(.18) },
      { Sid::ArpeggioHookLen,             QVariant(.8) },
      { Sid::SlurEndWidth,                QVariant(.07) },
      { Sid::SlurMidWidth,                QVariant(.15) },
      { Sid::SlurDottedWidth,             QVariant(.1) },
      { Sid::MinTieLength,                QVariant(1.0) },
      { Sid::SectionPause,                QVariant(qreal(3.0)) },
      { Sid::MusicalSymbolFont,           QVariant(QString("Emmentaler")) },
      { Sid::MusicalTextFont,             QVariant(QString("MScore Text")) },
      { Sid::showHeader,                  QVariant(false) },
      { Sid::headerFirstPage,             QVariant(false) },
      { Sid::headerOddEven,               QVariant(true) },
      { Sid::evenHeaderL,                 QVariant(QString()) },
      { Sid::evenHeaderC,                 QVariant(QString()) },
      { Sid::evenHeaderR,                 QVariant(QString()) },
      { Sid::oddHeaderL,                  QVariant(QString()) },
      { Sid::oddHeaderC,                  QVariant(QString()) },
      { Sid::oddHeaderR,                  QVariant(QString()) },
      { Sid::showFooter,                  QVariant(true) },
      { Sid::footerFirstPage,             QVariant(true) },
      { Sid::footerOddEven,               QVariant(true) },
      { Sid::evenFooterL,                 QVariant(QString("$p")) },
      { Sid::evenFooterC,                 QVariant(QString("$:copyright:")) },
      { Sid::evenFooterR,                 QVariant(QString()) },
      { Sid::oddFooterL,                  QVariant(QString()) },
      { Sid::oddFooterC,                  QVariant(QString("$:copyright:")) },
      { Sid::oddFooterR,                  QVariant(QString("$p")) },
      { Sid::voltaPosAbove,               QPointF(0.0, -3.0) },
      { Sid::voltaHook,                   QVariant(1.9) },
      { Sid::voltaLineWidth,              QVariant(.1) },
      { Sid::voltaLineStyle,              QVariant(int(Qt::SolidLine)) },
      { Sid::ottavaPosAbove,              QPointF(0.0, -3.0) },
      { Sid::ottavaHookAbove,             QVariant(1.9) },
      { Sid::ottavaHookBelow,             QVariant(-1.9) },
      { Sid::ottavaLineWidth,             QVariant(.1) },
      { Sid::ottavaLineStyle,             QVariant(int(Qt::DashLine)) },
      { Sid::ottavaNumbersOnly,           true },
      { Sid::tabClef,                     QVariant(int(ClefType::TAB)) },
      { Sid::tremoloWidth,                QVariant(1.2) },  // tremolo stroke width: notehead width
      { Sid::tremoloBoxHeight,            QVariant(0.65) },
      { Sid::tremoloStrokeWidth,          QVariant(0.5) },  // was 0.35
      { Sid::tremoloDistance,             QVariant(0.8) },
      // TODO { Sid::tremoloBeamLengthMultiplier, QVariant(0.62) },
      // TODO { Sid::tremoloMaxBeamLength,        QVariant(12.0) },
      { Sid::linearStretch,               QVariant(qreal(1.5)) },
      { Sid::crossMeasureValues,          QVariant(false) },
      { Sid::keySigNaturals,              QVariant(int(KeySigNatural::NONE)) },

      { Sid::tupletMaxSlope,              QVariant(qreal(0.5)) },
      { Sid::tupletOufOfStaff,            QVariant(true) },
      { Sid::tupletVHeadDistance,         QVariant(.5) },
      { Sid::tupletVStemDistance,         QVariant(.25) },
      { Sid::tupletStemLeftDistance,      QVariant(.5) },
      { Sid::tupletStemRightDistance,     QVariant(.5) },
      { Sid::tupletNoteLeftDistance,      QVariant(0.0) },
      { Sid::tupletNoteRightDistance,     QVariant(0.0) },

      { Sid::barreLineWidth,              QVariant(1.0) },
      { Sid::fretMag,                     QVariant(1.0) },
      { Sid::scaleBarlines,               QVariant(true) },
      { Sid::barGraceDistance,            QVariant(.6) },
      { Sid::rehearsalMarkFrameRound,     QVariant(20)    },
      { Sid::dynamicsFontStyle,           int(FontStyle::Italic) },

//      { Sid::staffTextFontFace,           "FreeSerif" },
//      { Sid::staffTextFontSize,           10.0 },
//      { Sid::staffTextFontBold,           false },
//      { Sid::staffTextFontItalic,         false },
//      { Sid::staffTextFontUnderline,      false },
      { Sid::staffTextAlign,              QVariant::fromValue(Align::LEFT | Align::TOP) },      // different from 3.x
//      { Sid::staffTextOffsetType,         int(OffsetType::SPATIUM)   },
//      { Sid::staffTextPlacement,          int(Placement::ABOVE) },
      { Sid::staffTextPosAbove,           QPointF(.0, -4.0) },
//      { Sid::staffTextMinDistance,        Spatium(0.5)  },
//      { Sid::staffTextFrameType,          int(FrameType::NO_FRAME) },
//      { Sid::staffTextFramePadding,       0.2 },
//      { Sid::staffTextFrameWidth,         0.1 },
//      { Sid::staffTextFrameRound,         0  },
//      { Sid::staffTextFrameFgColor,       QColor(0, 0, 0, 255) },
//      { Sid::staffTextFrameBgColor,       QColor(255, 255, 255, 0) },
      { Sid::defaultFrameRound,           QVariant(25) },
      { Sid::defaultFrameWidth,           Spatium(0.2) },
      { Sid::defaultFramePadding,         Spatium(0.5) },

      { Sid::titleFrameRound,             QVariant(25) },
      { Sid::titleFrameWidth,             Spatium(0.2) },
      { Sid::titleFramePadding,           Spatium(0.5) },

      { Sid::subTitleFrameRound,          QVariant(25) },
      { Sid::subTitleFrameWidth,          Spatium(0.2) },
      { Sid::subTitleFramePadding,        Spatium(0.5) },

      { Sid::composerFrameRound,          QVariant(25) },
      { Sid::composerFrameWidth,          Spatium(0.2) },
      { Sid::composerFramePadding,        Spatium(0.5) },

      { Sid::lyricistFrameRound,          QVariant(25) },
      { Sid::lyricistFrameWidth,          Spatium(0.2) },
      { Sid::lyricistFramePadding,        Spatium(0.5) },

      { Sid::fingeringFrameRound,          QVariant(25) },
      { Sid::fingeringFrameWidth,          Spatium(0.2) },
      { Sid::fingeringFramePadding,        Spatium(0.5) },

      { Sid::lhGuitarFingeringFrameRound,       QVariant(25) },
      { Sid::lhGuitarFingeringFrameWidth,       Spatium(0.2) },
      { Sid::lhGuitarFingeringFramePadding,     Spatium(0.5) },

      { Sid::rhGuitarFingeringFrameRound,       QVariant(25) },
      { Sid::rhGuitarFingeringFrameWidth,       Spatium(0.2) },
      { Sid::rhGuitarFingeringFramePadding,     Spatium(0.5) },

      { Sid::partInstrumentFrameRound,    QVariant(25) },
      { Sid::partInstrumentFrameWidth,    Spatium(0.2) },
      { Sid::partInstrumentFramePadding,  Spatium(0.5) },

      { Sid::tempoFrameRound,             QVariant(0)  },
      { Sid::tempoFrameWidth,             Spatium(0.2) },
      { Sid::tempoFramePadding,           Spatium(0.5) },

      { Sid::tempoFrameRound,             QVariant(25) },
      { Sid::tempoFrameWidth,             Spatium(0.2) },
      { Sid::tempoFramePadding,           Spatium(0.5) },

      { Sid::systemTextFrameRound,        QVariant(25) },
      { Sid::systemTextFrameWidth,        Spatium(0.2) },
      { Sid::systemTextFramePadding,      Spatium(0.5) },

      { Sid::staffTextFrameRound,         QVariant(25) },
      { Sid::staffTextFrameWidth,         Spatium(0.2) },
      { Sid::staffTextFramePadding,       Spatium(0.5) },

      { Sid::rehearsalMarkFrameRound,     QVariant(20) },
      { Sid::rehearsalMarkFrameWidth,     Spatium(0.2) },
      { Sid::rehearsalMarkFramePadding,   Spatium(0.5) },

      { Sid::repeatLeftFrameRound,        QVariant(25) },
      { Sid::repeatLeftFrameWidth,        Spatium(0.2) },
      { Sid::repeatLeftFramePadding,      Spatium(0.5) },

      { Sid::repeatRightFrameRound,       QVariant(25) },
      { Sid::repeatRightFrameWidth,       Spatium(0.2) },
      { Sid::repeatRightFramePadding,     Spatium(0.5) },
      };

//---------------------------------------------------------
//   excessTextStyles206
//    The first map has the name of the style as the string
//    The second map has the mapping of each Sid that the style identifies
//    to the default value for that sid.
//---------------------------------------------------------

static std::map<QString, std::map<Sid, QVariant>> excessTextStyles206;

//---------------------------------------------------------
//   setPageFormat
//    set Style from PageFormat
//---------------------------------------------------------

void setPageFormat(MStyle* style, const PageFormat& pf)
      {
      style->set(Sid::pageWidth,            pf.size().width());
      style->set(Sid::pageHeight,           pf.size().height());
      style->set(Sid::pagePrintableWidth,   pf.printableWidth());
      style->set(Sid::pageEvenLeftMargin,   pf.evenLeftMargin());
      style->set(Sid::pageOddLeftMargin,    pf.oddLeftMargin());
      style->set(Sid::pageEvenTopMargin,    pf.evenTopMargin());
      style->set(Sid::pageEvenBottomMargin, pf.evenBottomMargin());
      style->set(Sid::pageOddTopMargin,     pf.oddTopMargin());
      style->set(Sid::pageOddBottomMargin,  pf.oddBottomMargin());
      style->set(Sid::pageTwosided,         pf.twosided());
      }

//---------------------------------------------------------
//   initPageFormat
//    initialize PageFormat from Style
//---------------------------------------------------------

void initPageFormat(MStyle* style, PageFormat* pf)
      {
      QSizeF sz;
      sz.setWidth(style->value(Sid::pageWidth).toReal());
      sz.setHeight(style->value(Sid::pageHeight).toReal());
      pf->setSize(sz);
      pf->setPrintableWidth(style->value(Sid::pagePrintableWidth).toReal());
      pf->setEvenLeftMargin(style->value(Sid::pageEvenLeftMargin).toReal());
      pf->setOddLeftMargin(style->value(Sid::pageOddLeftMargin).toReal());
      pf->setEvenTopMargin(style->value(Sid::pageEvenTopMargin).toReal());
      pf->setEvenBottomMargin(style->value(Sid::pageEvenBottomMargin).toReal());
      pf->setOddTopMargin(style->value(Sid::pageOddTopMargin).toReal());
      pf->setOddBottomMargin(style->value(Sid::pageOddBottomMargin).toReal());
      pf->setTwosided(style->value(Sid::pageTwosided).toBool());
      }

//---------------------------------------------------------
//   readPageFormat
//---------------------------------------------------------

void readPageFormat(MStyle* style, XmlReader& e)
      {
      PageFormat pf;
      initPageFormat(style, &pf);
      pf.read(e);
      setPageFormat(style, pf);
      }

//---------------------------------------------------------
//   readTextStyle206
//---------------------------------------------------------

void readTextStyle206(MStyle* style, XmlReader& e, std::map<QString, std::map<Sid, QVariant>>& excessStyles)
      {
      QString family = "FreeSerif";
      double size = 10;
      bool sizeIsSpatiumDependent = false;
      FontStyle fontStyle = FontStyle::Normal;
      Align align = Align::LEFT;
      QPointF offset;
      OffsetType offsetType = OffsetType::SPATIUM;

      FrameType frameType = FrameType::NO_FRAME;
      Spatium paddingWidth(0.0);
      Spatium frameWidth(0.0);
      QColor foregroundColor = QColor(0, 0, 0, 255);
      QColor backgroundColor = QColor(255, 255, 255, 0);

      Placement placement = Placement::ABOVE;
      bool placementValid = false;

      QString name = e.attribute("name");
      QColor frameColor = QColor(0, 0, 0, 255);

      bool systemFlag = false;
      qreal lineWidth = -1.0;

      while (e.readNextStartElement()) {
            const QStringRef& tag(e.name());

            if (tag == "name")
                  name = e.readElementText();
            else if (tag == "family")
                  family = e.readElementText();
            else if (tag == "size")
                  size = e.readDouble();
            else if (tag == "bold") {
                  if (e.readInt())
                        fontStyle = fontStyle + FontStyle::Bold;
                  }
            else if (tag == "italic") {
                  if (e.readInt())
                        fontStyle = fontStyle + FontStyle::Italic;
                  }
            else if (tag == "underline") {
                  if (e.readInt())
                        fontStyle = fontStyle + FontStyle::Underline;
                  }
            else if (tag == "align")
                  align = Align(e.readInt());
            else if (tag == "anchor")     // obsolete
                  e.skipCurrentElement();

            else if (tag == "halign") {
                  const QString& val(e.readElementText());
                  if (val == "center")
                        align = align | Align::HCENTER;
                  else if (val == "right")
                        align = align | Align::RIGHT;
                  else if (val == "left")
                        ;
                  else
                        qDebug("Text::readProperties: unknown alignment: <%s>", qPrintable(val));
                  }
            else if (tag == "valign") {
                  const QString& val(e.readElementText());
                  if (val == "center")
                        align = align | Align::VCENTER;
                  else if (val == "bottom")
                        align = align | Align::BOTTOM;
                  else if (val == "baseline")
                        align = align | Align::BASELINE;
                  else if (val == "top")
                        ;
                  else
                        qDebug("Text::readProperties: unknown alignment: <%s>", qPrintable(val));
                  }
            else if (tag == "xoffset") {
                  qreal xo = e.readDouble();
                  if (offsetType == OffsetType::ABS)
                        xo /= INCH;
                  offset.setX(xo);
                  }
            else if (tag == "yoffset") {
                  qreal yo = e.readDouble();
                  if (offsetType == OffsetType::ABS)
                        yo /= INCH;
                  offset.setY(yo);
                  }
            else if (tag == "rxoffset" || tag == "ryoffset")         // obsolete
                  e.readDouble();
            else if (tag == "offsetType") {
                  const QString& val(e.readElementText());
                  OffsetType ot = OffsetType::ABS;
                  if (val == "spatium" || val == "1")
                        ot = OffsetType::SPATIUM;
                  if (ot != offsetType) {
                        offsetType = ot;
                        if (ot == OffsetType::ABS)
                              offset /= INCH;  // convert spatium -> inch
                        else
                              offset *= INCH;  // convert inch -> spatium
                        }
                  }
            else if (tag == "sizeIsSpatiumDependent" || tag == "spatiumSizeDependent")
                  sizeIsSpatiumDependent = e.readInt();
            else if (tag == "frameWidth") { // obsolete
                  frameType = FrameType::SQUARE;
                  /*frameWidthMM =*/ e.readDouble();
                  }
            else if (tag == "frameWidthS") {
                  frameType = FrameType::SQUARE;
                  frameWidth = Spatium(e.readDouble());
                  }
            else if (tag == "frame")
                  frameType = e.readInt() ? FrameType::SQUARE : FrameType::NO_FRAME;
            else if (tag == "paddingWidth")          // obsolete
                  /*paddingWidthMM =*/ e.readDouble();
            else if (tag == "paddingWidthS")
                  paddingWidth = Spatium(e.readDouble());
            else if (tag == "frameRound")
                  e.readInt();
            else if (tag == "frameColor")
                  frameColor = e.readColor();
            else if (tag == "foregroundColor")
                  foregroundColor = e.readColor();
            else if (tag == "backgroundColor")
                  backgroundColor = e.readColor();
            else if (tag == "circle")
                  frameType = e.readInt() ? FrameType::CIRCLE : FrameType::NO_FRAME;
            else if (tag == "systemFlag")
                  systemFlag = e.readInt();
            else if (tag == "placement") {
                  QString value(e.readElementText());
                  if (value == "above")
                        placement = Placement::ABOVE;
                  else if (value == "below")
                        placement = Placement::BELOW;
                  placementValid = true;
                  }
            else if (tag == "lineWidth")
                  lineWidth = e.readDouble();
            else
                  e.unknown();
            }
      if (family == "MuseJazz")
            family = "MuseJazz Text";

      struct StyleTable {
            const char* name;
            Tid ss;
            } styleTable[] = {
            { "",                        Tid::DEFAULT },
            { "Title",                   Tid::TITLE },
            { "Subtitle",                Tid::SUBTITLE },
            { "Composer",                Tid::COMPOSER },
            { "Lyricist",                Tid::POET },
            { "Lyrics Odd Lines",        Tid::LYRICS_ODD },
            { "Lyrics Even Lines",       Tid::LYRICS_EVEN },
            { "Fingering",               Tid::FINGERING },
            { "LH Guitar Fingering",     Tid::LH_GUITAR_FINGERING },
            { "RH Guitar Fingering",     Tid::RH_GUITAR_FINGERING },
            { "String Number",           Tid::STRING_NUMBER },
            { "Instrument Name (Long)",  Tid::INSTRUMENT_LONG },
            { "Instrument Name (Short)", Tid::INSTRUMENT_SHORT },
            { "Instrument Name (Part)",  Tid::INSTRUMENT_EXCERPT },
            { "Dynamics",                Tid::DYNAMICS },
            { "Technique",               Tid::EXPRESSION },
            { "Tempo",                   Tid::TEMPO },
            { "Metronome",               Tid::METRONOME },
            { "Measure Number",          Tid::MEASURE_NUMBER },
            { "Translator",              Tid::TRANSLATOR },
            { "Tuplet",                  Tid::TUPLET },
            { "System",                  Tid::SYSTEM },
            { "Staff",                   Tid::STAFF },
            { "Chord Symbol",            Tid::HARMONY_A },
            { "Rehearsal Mark",          Tid::REHEARSAL_MARK },
            { "Repeat Text Left",        Tid::REPEAT_LEFT },
            { "Repeat Text Right",       Tid::REPEAT_RIGHT },
            { "Frame",                   Tid::FRAME },
            { "Text Line",               Tid::TEXTLINE },
            { "Glissando",               Tid::GLISSANDO },
            { "Ottava",                  Tid::OTTAVA },
            { "Pedal",                   Tid::PEDAL },
            { "Hairpin",                 Tid::HAIRPIN },
            { "Bend",                    Tid::BEND },
            { "Header",                  Tid::HEADER },
            { "Footer",                  Tid::FOOTER },
            { "Instrument Change",       Tid::INSTRUMENT_CHANGE },
            { "Figured Bass",            Tid::TEXT_STYLES },            // invalid
            { "Volta",                   Tid::VOLTA },
            };
      Tid ss = Tid::TEXT_STYLES;
      for (const auto& i : styleTable) {
            if (name == i.name) {
                  ss = i.ss;
                  break;
                  }
            }

      bool isExcessStyle = false;
      if (ss == Tid::TEXT_STYLES) {
            ss = e.addUserTextStyle(name);
            if (ss == Tid::TEXT_STYLES) {
                  qDebug("unhandled substyle <%s>", qPrintable(name));
                  isExcessStyle = true;
                  }
            else {
                  int idx = int(ss) - int(Tid::USER1);
                  if ((idx < 0) || (idx > 5)) {
                        qDebug("User style index %d outside of range [0,5].", idx);
                        return;
                        }
                  Sid sid[] = { Sid::user1Name, Sid::user2Name, Sid::user3Name, Sid::user4Name, Sid::user5Name, Sid::user6Name };
                  style->set(sid[idx], name);
                  }
            }

      std::map<Sid, QVariant> excessPairs;
      const TextStyle* ts;
      if (isExcessStyle)
            ts = textStyle("User-1");
      else
            ts = textStyle(ss);
      for (const auto& i : *ts) {
            QVariant value;
            if (i.sid == Sid::NOSTYLE)
                  break;
            switch (i.pid) {
                  case Pid::SUB_STYLE:
                        value = int(ss);
                        break;
                  case Pid::BEGIN_FONT_FACE:
                  case Pid::CONTINUE_FONT_FACE:
                  case Pid::END_FONT_FACE:
                  case Pid::FONT_FACE:
                        value = family;
                        break;
                  case Pid::BEGIN_FONT_SIZE:
                  case Pid::CONTINUE_FONT_SIZE:
                  case Pid::END_FONT_SIZE:
                  case Pid::FONT_SIZE:
                        value = size;
                        break;
                  case Pid::BEGIN_FONT_STYLE:
                  case Pid::CONTINUE_FONT_STYLE:
                  case Pid::END_FONT_STYLE:
                  case Pid::FONT_STYLE:
                        value = int(fontStyle);
                        break;
                  case Pid::FRAME_TYPE:
                        value = int(frameType);
                        break;
                  case Pid::FRAME_WIDTH:
                        value = frameWidth;
                        break;
                  case Pid::FRAME_PADDING:
                        value = paddingWidth;
                        break;
                  case Pid::FRAME_FG_COLOR:
                        value = frameColor;
                        break;
                  case Pid::FRAME_BG_COLOR:
                        value = backgroundColor;
                        break;
                  case Pid::SIZE_SPATIUM_DEPENDENT:
                        value = sizeIsSpatiumDependent;
                        break;
                  case Pid::BEGIN_TEXT_ALIGN:
                  case Pid::CONTINUE_TEXT_ALIGN:
                  case Pid::END_TEXT_ALIGN:
                  case Pid::ALIGN:
                        value = QVariant::fromValue(align);
                        break;
#if 0  //TODO-offset
                  case Pid::OFFSET:
                        if (offsetValid) {
                              if (ss == Tid::TEMPO) {
                                    style->set(Sid::tempoPosAbove, Spatium(offset.y()));
                                    offset = QPointF();
                                    }
                              else if (ss == Tid::STAFF) {
                                    style->set(Sid::staffTextPosAbove, Spatium(offset.y()));
                                    offset = QPointF();
                                    }
                              else if (ss == Tid::REHEARSAL_MARK) {
                                    style->set(Sid::rehearsalMarkPosAbove, Spatium(offset.y()));
                                    offset = QPointF();
                                    }
                              value = offset;
                              }
                        break;
                  case Pid::OFFSET_TYPE:
                        value = int(offsetType);
                        break;
#endif
                  case Pid::SYSTEM_FLAG:
                        value = systemFlag;
                        break;
                  case Pid::BEGIN_HOOK_HEIGHT:
                  case Pid::END_HOOK_HEIGHT:
                        value = QVariant();
                        break;
                  case Pid::PLACEMENT:
                        if (placementValid)
                              value = int(placement);
                        break;
                  case Pid::LINE_WIDTH:
                        if (lineWidth != -1.0)
                              value = lineWidth;
                        break;
                  default:
//                        qDebug("unhandled property <%s>%d", propertyName(i.pid), int (i.pid));
                        break;
                  }
            if (value.isValid()) {
                  if (isExcessStyle)
                        excessPairs[i.sid] = value;
                  else
                        style->set(i.sid, value);
                  }
//            else
//                  qDebug("invalid style value <%s> pid<%s>", MStyle::valueName(i.sid), propertyName(i.pid));
            }

      if (isExcessStyle && excessPairs.size() > 0)
            excessStyles[name] = excessPairs;
      }

//---------------------------------------------------------
//   readAccidental206
//---------------------------------------------------------

void readAccidental206(Accidental* a, XmlReader& e)
      {
      while (e.readNextStartElement()) {
            const QStringRef& tag(e.name());
            if (tag == "bracket") {
                  int i = e.readInt();
                  if (i == 0 || i == 1)
                        a->setBracket(AccidentalBracket(i));
                  }
            else if (tag == "subtype") {
                  QString text = e.readElementText();
                  const static std::map<QString, AccidentalType> accMap = {
                     {"none",               AccidentalType::NONE},
                     {"sharp",              AccidentalType::SHARP},
                     {"flat",               AccidentalType::FLAT},
                     {"natural",            AccidentalType::NATURAL},
                     {"double sharp",       AccidentalType::SHARP2},
                     {"double flat",        AccidentalType::FLAT2},
                     {"flat-slash",         AccidentalType::FLAT_SLASH},
                     {"flat-slash2",        AccidentalType::FLAT_SLASH2},
                     {"mirrored-flat2",     AccidentalType::MIRRORED_FLAT2},
                     {"mirrored-flat",      AccidentalType::MIRRORED_FLAT},
                     {"sharp-slash",        AccidentalType::SHARP_SLASH},
                     {"sharp-slash2",       AccidentalType::SHARP_SLASH2},
                     {"sharp-slash3",       AccidentalType::SHARP_SLASH3},
                     {"sharp-slash4",       AccidentalType::SHARP_SLASH4},
                     {"sharp arrow up",     AccidentalType::SHARP_ARROW_UP},
                     {"sharp arrow down",   AccidentalType::SHARP_ARROW_DOWN},
                     {"flat arrow up",      AccidentalType::FLAT_ARROW_UP},
                     {"flat arrow down",    AccidentalType::FLAT_ARROW_DOWN},
                     {"natural arrow up",   AccidentalType::NATURAL_ARROW_UP},
                     {"natural arrow down", AccidentalType::NATURAL_ARROW_DOWN},
                     {"sori",               AccidentalType::SORI},
                     {"koron",              AccidentalType::KORON}
                     };
                  auto it = accMap.find(text);
                  if (it == accMap.end()) {
                        qDebug("invalid type %s", qPrintable(text));
                        a->setAccidentalType(AccidentalType::NONE);
                        }
                  else
                        a->setAccidentalType(it->second);
                  }
            else if (tag == "role") {
                  AccidentalRole r = AccidentalRole(e.readInt());
                  if (r == AccidentalRole::AUTO || r == AccidentalRole::USER)
                        a->setRole(r);
                  }
            else if (tag == "small")
                  a->setSmall(e.readInt());
            else if (a->Element::readProperties(e))
                  ;
            else
                  e.unknown();
            }
      }

static NoteHead::Group convertHeadGroup(int i)
      {
      NoteHead::Group val;
      switch (i) {
            case 1:
                  val = NoteHead::Group::HEAD_CROSS;
                  break;
            case 2:
                  val = NoteHead::Group::HEAD_DIAMOND;
                  break;
            case 3:
                  val = NoteHead::Group::HEAD_TRIANGLE_DOWN;
                  break;
            case 4:
                  val = NoteHead::Group::HEAD_MI;
                  break;
            case 5:
                  val = NoteHead::Group::HEAD_SLASH;
                  break;
            case 6:
                  val = NoteHead::Group::HEAD_XCIRCLE;
                  break;
            case 7:
                  val = NoteHead::Group::HEAD_DO;
                  break;
            case 8:
                  val = NoteHead::Group::HEAD_RE;
                  break;
            case 9:
                  val = NoteHead::Group::HEAD_FA;
                  break;
            case 10:
                  val = NoteHead::Group::HEAD_LA;
                  break;
            case 11:
                  val = NoteHead::Group::HEAD_TI;
                  break;
            case 12:
                  val = NoteHead::Group::HEAD_SOL;
                  break;
            case 13:
                  val = NoteHead::Group::HEAD_BREVIS_ALT;
                  break;
            case 0:
            default:
                  val = NoteHead::Group::HEAD_NORMAL;
            }
      return val;
      }

static NoteHead::Type convertHeadType(int i)
      {
      NoteHead::Type val;
      switch (i) {
            case 0:
                  val = NoteHead::Type::HEAD_WHOLE;
                  break;
            case 1:
                  val = NoteHead::Type::HEAD_HALF;
                  break;
            case 2:
                  val = NoteHead::Type::HEAD_QUARTER;
                  break;
            case 3:
                  val = NoteHead::Type::HEAD_BREVIS;
                  break;
            default:
                  val = NoteHead::Type::HEAD_AUTO;;
            }
      return val;
      }

//---------------------------------------------------------
//   ArticulationNames
//---------------------------------------------------------

static struct ArticulationNames {
      SymId id;
      const char* name;
      } articulationNames[] = {
      { SymId::fermataAbove,              "fermata",                   },
      { SymId::fermataShortAbove,         "shortfermata",              },
      { SymId::fermataLongAbove,          "longfermata",               },
      { SymId::fermataVeryLongAbove,      "verylongfermata",           },
      { SymId::articAccentAbove,          "sforzato",                  },
      { SymId::articStaccatoAbove,        "staccato",                  },
      { SymId::articStaccatissimoAbove,   "staccatissimo",             },
      { SymId::articTenutoAbove,          "tenuto",                    },
      { SymId::articTenutoStaccatoAbove,  "portato",                   },
      { SymId::articMarcatoAbove,         "marcato",                   },
      { SymId::guitarFadeIn,              "fadein",                    },
      { SymId::guitarFadeOut,             "fadeout",                   },
      { SymId::guitarVolumeSwell,         "volumeswell",               },
      { SymId::wiggleSawtooth,            "wigglesawtooth",            },
      { SymId::wiggleSawtoothWide,        "wigglesawtoothwide",        },
      { SymId::wiggleVibratoLargeFaster,  "wigglevibratolargefaster",  },
      { SymId::wiggleVibratoLargeSlowest, "wigglevibratolargeslowest", },
      { SymId::brassMuteOpen,             "ouvert",                    },
      { SymId::brassMuteClosed,           "plusstop",                  },
      { SymId::stringsUpBow,              "upbow",                     },
      { SymId::stringsDownBow,            "downbow",                   },
      { SymId::ornamentTurnInverted,      "reverseturn",               },
      { SymId::ornamentTurn,              "turn",                      },
      { SymId::ornamentTrill,             "trill",                     },
      { SymId::ornamentMordent,           "prall",                     },
      { SymId::ornamentMordentInverted,   "mordent",                   },
      { SymId::ornamentTremblement,       "prallprall",                },
      { SymId::ornamentPrallMordent,      "prallmordent",              },
      { SymId::ornamentUpPrall,           "upprall",                   },
      { SymId::ornamentUpMordent,         "upmordent",                 },
      { SymId::ornamentDownMordent,       "downmordent",               },
      { SymId::ornamentPrallDown,         "pralldown",                 },
      { SymId::ornamentPrallUp,           "prallup",                   },
      { SymId::ornamentLinePrall,         "lineprall",                 },
      { SymId::ornamentPrecompSlide,      "schleifer",                 },
      { SymId::pluckedSnapPizzicatoAbove, "snappizzicato",             },
      { SymId::stringsThumbPosition,      "thumb",                     },
      { SymId::luteFingeringRHThumb,      "lutefingeringthumb",        },
      { SymId::luteFingeringRHFirst,      "lutefingering1st",          },
      { SymId::luteFingeringRHSecond,     "lutefingering2nd",          },
      { SymId::luteFingeringRHThird,      "lutefingering3rd",          },

      { SymId::ornamentPrecompMordentUpperPrefix, "downprall"   },
      { SymId::ornamentPrecompMordentUpperPrefix, "ornamentDownPrall"   },
      };

//---------------------------------------------------------
//   oldArticulationNames2SymId
//---------------------------------------------------------

SymId oldArticulationNames2SymId(const QString& s)
      {
      for (auto i : articulationNames) {
            if (i.name == s)
                  return i.id;
            }
      return SymId::noSym;
      }

//---------------------------------------------------------
//   readDrumset
//---------------------------------------------------------

static void readDrumset(Drumset* ds, XmlReader& e)
      {
      int pitch = e.intAttribute("pitch", -1);
      if (pitch < 0 || pitch > 127) {
            qDebug("load drumset: invalid pitch %d", pitch);
            return;
            }
      while (e.readNextStartElement()) {
            const QStringRef& tag(e.name());
            if (tag == "head")
                  ds->drum(pitch).notehead = convertHeadGroup(e.readInt());
            else if (tag == "variants") {
                  while(e.readNextStartElement()) {
                        const QStringRef& tagv(e.name());
                        if (tagv == "variant") {
                              DrumInstrumentVariant div;
                              div.pitch = e.attribute("pitch").toInt();
                              while (e.readNextStartElement()) {
                                    const QStringRef& taga(e.name());
                                    if (taga == "articulation") {
                                          QString oldArticulationName = e.readElementText();
                                          SymId oldId = oldArticulationNames2SymId(oldArticulationName);
                                          div.articulationName = Articulation::symId2ArticulationName(oldId);
                                          }
                                    else if (taga == "tremolo") {
                                          div.tremolo = Tremolo::name2Type(e.readElementText());
                                          }
                                    }
                              ds->drum(pitch).addVariant(div);
                              }
                        }
                  }
            else if (ds->readProperties(e, pitch))
                  ;
            else
                  e.unknown();
            }
      }

//---------------------------------------------------------
//   readInstrument
//---------------------------------------------------------

static void readInstrument(Instrument *i, Part* p, XmlReader& e)
      {
      int program = -1;
      int bank    = 0;
      int volume  = 100;
      int pan     = 60;
      int chorus  = 30;
      int reverb  = 30;
      bool customDrumset = false;
      i->clearChannels();       // remove default channel
      while (e.readNextStartElement()) {
            const QStringRef& tag(e.name());
            if (tag == "Drum") {
                  // if we see one of this tags, a custom drumset will
                  // be created
                  if (!i->drumset())
                        i->setDrumset(new Drumset(*smDrumset));
                  if (!customDrumset) {
                        i->drumset()->clear();
                        customDrumset = true;
                        }
                  readDrumset(i->drumset(), e);
                  }

            else if (i->readProperties(e, p, &customDrumset))
                  ;
            else
                 e.unknown();
            }

      // Read single-note dynamics from template
      i->setSingleNoteDynamicsFromTemplate();

      if (i->channel().empty()) {      // for backward compatibility
            Channel* a = new Channel;
            a->setName(Channel::DEFAULT_NAME);
            a->setProgram(program);
            a->setBank(bank);
            a->setVolume(volume);
            a->setPan(pan);
            a->setReverb(reverb);
            a->setChorus(chorus);
            i->appendChannel(a);
            }
      if (i->useDrumset()) {
            if (i->channel()[0]->bank() == 0)
                  i->channel()[0]->setBank(128);
            }
      }

//---------------------------------------------------------
//   readStaff
//---------------------------------------------------------

static void readStaff(Staff* staff, XmlReader& e)
      {
      while (e.readNextStartElement()) {
            const QStringRef& tag(e.name());
            if (tag == "type") {    // obsolete
                  int staffTypeIdx = e.readInt();
                  qDebug("obsolete: Staff::read staffTypeIdx %d", staffTypeIdx);
                  }
            else if (tag == "neverHide") {
                  bool v = e.readInt();
                  if (v)
                        staff->setHideWhenEmpty(Staff::HideMode::NEVER);
                  }
            else if (tag == "barLineSpan") {
                  staff->setBarLineFrom(e.intAttribute("from", 0));
                  staff->setBarLineTo(e.intAttribute("to", 0));
                  int span     = e.readInt();
                  staff->setBarLineSpan(span - 1);
                  }
            else if (staff->readProperties(e))
                  ;
            else
                  e.unknown();
            }
      }

//---------------------------------------------------------
//   readPart
//---------------------------------------------------------

void readPart206(Part* part, XmlReader& e)
      {
      while (e.readNextStartElement()) {
            const QStringRef& tag(e.name());
            if (tag == "Instrument") {
                  Instrument* i = part->_instruments.instrument(/* tick */ -1);
                  readInstrument(i, part, e);
                  Drumset* ds = i->drumset();
                  Staff*   s = part->staff(0);
                  int lld = s ? qRound(s->lineDistance(Fraction(0,1))) : 1;
                  if (ds && s && lld > 1) {
                        for (int j = 0; j < DRUM_INSTRUMENTS; ++j)
                              ds->drum(j).line /= lld;
                        }
                  }
            else if (tag == "Staff") {
                  Staff* staff = new Staff(part->score());
                  staff->setPart(part);
                  part->score()->staves().push_back(staff);
                  part->staves()->push_back(staff);
                  readStaff(staff, e);
                  }
            else if (part->readProperties(e))
                  ;
            else
                  e.unknown();
            }
      }

//---------------------------------------------------------
//   readAmbitus
//---------------------------------------------------------

static void readAmbitus(Ambitus* ambitus, XmlReader& e)
      {
      while (e.readNextStartElement()) {
            const QStringRef& tag(e.name());
            if (tag == "head")
                  ambitus->setNoteHeadGroup(convertHeadGroup(e.readInt()));
            else if (tag == "headType")
                  ambitus->setNoteHeadType(convertHeadType(e.readInt()));
            else if (ambitus->readProperties(e))
                  ;
            else
                  e.unknown();
            }
      }

//---------------------------------------------------------
//   readNote
//---------------------------------------------------------

static void readNote(Note* note, XmlReader& e)
      {
      note->setTpc1(Tpc::TPC_INVALID);
      note->setTpc2(Tpc::TPC_INVALID);

      while (e.readNextStartElement()) {
            const QStringRef& tag(e.name());
            if (tag == "Accidental") {
                  Accidental* a = new Accidental(note->score());
                  a->setTrack(note->track());
                  readAccidental206(a, e);
                  note->add(a);
                  }
            else if (tag == "head") {
                  int i = e.readInt();
                  NoteHead::Group val = convertHeadGroup(i);
                  note->setHeadGroup(val);
                  }
            else if (tag == "headType") {
                  int i = e.readInt();
                  NoteHead::Type val = convertHeadType(i);
                  note->setHeadType(val);
                  }
            else if (readNoteProperties206(note, e))
                  ;
            else
                  e.unknown();
            }
      // ensure sane values:
      note->setPitch(limit(note->pitch(), 0, 127));

      if (!tpcIsValid(note->tpc1()) && !tpcIsValid(note->tpc2())) {
            Key key = (note->staff() && note->chord()) ? note->staff()->key(note->chord()->tick()) : Key::C;
            int tpc = pitch2tpc(note->pitch(), key, Prefer::NEAREST);
            if (note->concertPitch())
                  note->setTpc1(tpc);
            else
                  note->setTpc2(tpc);
            }
      if (!(tpcIsValid(note->tpc1()) && tpcIsValid(note->tpc2()))) {
            Fraction tick = note->chord() ? note->chord()->tick() : Fraction(-1,1);
            Interval v = note->staff() ? note->part()->instrument(tick)->transpose() : Interval();
            if (tpcIsValid(note->tpc1())) {
                  v.flip();
                  if (v.isZero())
                        note->setTpc2(note->tpc1());
                  else
                        note->setTpc2(Ms::transposeTpc(note->tpc1(), v, true));
                  }
            else {
                  if (v.isZero())
                        note->setTpc1(note->tpc2());
                  else
                        note->setTpc1(Ms::transposeTpc(note->tpc2(), v, true));
                  }
            }
#if 0
      // TODO - adapt this code

      // check consistency of pitch, tpc1, tpc2, and transposition
      // see note in InstrumentChange::read() about a known case of tpc corruption produced in 2.0.x
      // but since there are other causes of tpc corruption (eg, https://musescore.org/en/node/74746)
      // including perhaps some we don't know about yet,
      // we will attempt to fix some problems here regardless of version

      if (staff() && !staff()->isDrumStaff(e.tick()) && !e.pasteMode() && !MScore::testMode) {
            int tpc1Pitch = (tpc2pitch(_tpc[0]) + 12) % 12;
            int tpc2Pitch = (tpc2pitch(_tpc[1]) + 12) % 12;
            int soundingPitch = _pitch % 12;
            if (tpc1Pitch != soundingPitch) {
                  qDebug("bad tpc1 - soundingPitch = %d, tpc1 = %d", soundingPitch, tpc1Pitch);
                  _pitch += tpc1Pitch - soundingPitch;
                  }
            if (staff()) {
                  Interval v = staff()->part()->instrument(e.tick())->transpose();
                  int writtenPitch = (_pitch - v.chromatic) % 12;
                  if (tpc2Pitch != writtenPitch) {
                        qDebug("bad tpc2 - writtenPitch = %d, tpc2 = %d", writtenPitch, tpc2Pitch);
                        if (concertPitch()) {
                              // assume we want to keep sounding pitch
                              // so fix written pitch (tpc only)
                              v.flip();
                              _tpc[1] = Ms::transposeTpc(_tpc[0], v, true);
                              }
                        else {
                              // assume we want to keep written pitch
                              // so fix sounding pitch (both tpc and pitch)
                              _tpc[0] = Ms::transposeTpc(_tpc[1], v, true);
                              _pitch += tpc2Pitch - writtenPitch;
                              }
                        }
                  }
            }
#endif
      }

//---------------------------------------------------------
//   adjustPlacement
//---------------------------------------------------------

static void adjustPlacement(Element* e)
      {
      if (!e || !e->staff())
            return;

      // element to use to determine placement
      // for spanners, choose first segment
      Element* ee;
      Spanner* spanner;
      if (e->isSpanner()) {
            spanner = toSpanner(e);
            if (spanner->spannerSegments().empty())
                  return;
            ee = spanner->spannerSegments().front();
            if (!ee)
                  return;
            }
      else {
            spanner = nullptr;
            ee = e;
            }

      // determine placement based on offset
      // anything below staff will be set to below
      qreal staffHeight = e->staff()->height();
      qreal threshold = staffHeight;
      qreal offsetAdjust = 0.0;
      Placement defaultPlacement = Placement(e->propertyDefault(Pid::PLACEMENT).toInt());
      Placement newPlacement;
      // most offsets will be recorded as relative to top staff line
      // exceptions are styled offsets on elements with default placement below
      qreal normalize;
      if (defaultPlacement == Placement::BELOW && ee->propertyFlags(Pid::OFFSET) == PropertyFlags::STYLED)
            normalize = staffHeight;
      else
            normalize = 0.0;
      qreal ypos = ee->offset().y() + normalize;
      if (ypos >= threshold) {
            newPlacement = Placement::BELOW;
            offsetAdjust -= staffHeight;
            }
      else {
            newPlacement = Placement::ABOVE;
            }

      // set placement
      e->setProperty(Pid::PLACEMENT, int(newPlacement));
      if (newPlacement != defaultPlacement)
            e->setPropertyFlags(Pid::PLACEMENT, PropertyFlags::UNSTYLED);

      // adjust offset
      if (spanner) {
            // adjust segments individually
            for (auto a : spanner->spannerSegments()) {
                  // spanner segments share the placement setting of the spanner
                  // just adjust offset
                  if (defaultPlacement == Placement::BELOW && a->propertyFlags(Pid::OFFSET) == PropertyFlags::STYLED)
                        normalize = staffHeight;
                  else
                        normalize = 0.0;
                  qreal yp = a->offset().y() + normalize;
                  a->ryoffset() += normalize + offsetAdjust;

                  // if any segments are offset to opposite side of staff from placement,
                  // or if they are within staff,
                  // disable autoplace
                  bool disableAutoplace;
                  if (yp + a->height() <= 0.0)
                        disableAutoplace = (newPlacement == Placement::BELOW);
                  else if (yp > staffHeight)
                        disableAutoplace = (newPlacement == Placement::ABOVE);
                  else
                        disableAutoplace = true;
                  if (disableAutoplace)
                        a->setAutoplace(false);
                  // needed for https://musescore.org/en/node/281312
                  // ideally we would rebase and calculate new offset
                  // but this may not be possible
                  // since original offset is relative to system
                  a->rxoffset() = 0;
                  }
            }
      else {
            e->ryoffset() += normalize + offsetAdjust;
            // if within staff, disable autoplace
            if (ypos + e->height() > 0.0 && ypos <= staffHeight)
                  e->setAutoplace(false);
            }
      }

//---------------------------------------------------------
//   readNoteProperties206
//---------------------------------------------------------

bool readNoteProperties206(Note* note, XmlReader& e)
      {
      const QStringRef& tag(e.name());

      if (tag == "pitch")
            note->setPitch(e.readInt());
      else if (tag == "tpc") {
            const int tpc = e.readInt();
            note->setTpc1(tpc);
            note->setTpc2(tpc);
            }
      else if (tag == "track")            // for performance
            note->setTrack(e.readInt());
      else if (tag == "Accidental") {
            Accidental* a = new Accidental(note->score());
            a->setTrack(note->track());
            a->read(e);
            note->add(a);
            }
      else if (tag == "Tie") {
            Tie* tie = new Tie(note->score());
            tie->setParent(note);
            tie->setTrack(note->track());
            readTie206(e, tie);
            tie->setStartNote(note);
            note->setTieFor(tie);
            }
      else if (tag == "tpc2")
            note->setTpc2(e.readInt());
      else if (tag == "small")
            note->setSmall(e.readInt());
      else if (tag == "mirror")
            note->readProperty(e, Pid::MIRROR_HEAD);
      else if (tag == "dotPosition")
            note->readProperty(e, Pid::DOT_POSITION);
      else if (tag == "fixed")
            note->setFixed(e.readBool());
      else if (tag == "fixedLine")
            note->setFixedLine(e.readInt());
      else if (tag == "head")
            note->readProperty(e, Pid::HEAD_GROUP);
      else if (tag == "velocity")
            note->setVeloOffset(e.readInt());
      else if (tag == "play")
            note->setPlay(e.readInt());
      else if (tag == "tuning")
            note->setTuning(e.readDouble());
      else if (tag == "fret")
            note->setFret(e.readInt());
      else if (tag == "string")
            note->setString(e.readInt());
      else if (tag == "ghost")
            note->setGhost(e.readInt());
      else if (tag == "headType")
            note->readProperty(e, Pid::HEAD_TYPE);
      else if (tag == "veloType")
            note->readProperty(e, Pid::VELO_TYPE);
      else if (tag == "line")
            note->setLine(e.readInt());
      else if (tag == "Fingering") {
            Fingering* f = new Fingering(note->score());
            f->setTrack(note->track());
            readText206(e, f, note);
            note->add(f);
            }
      else if (tag == "Symbol") {
            Symbol* s = new Symbol(note->score());
            s->setTrack(note->track());
            s->read(e);
            note->add(s);
            }
      else if (tag == "Image") {
            if (MScore::noImages)
                  e.skipCurrentElement();
            else {
                  Image* image = new Image(note->score());
                  image->setTrack(note->track());
                  image->read(e);
                  note->add(image);
                  }
            }
      else if (tag == "Bend") {
            Bend* b = new Bend(note->score());
            b->setTrack(note->track());
            b->read(e);
            note->add(b);
            }
      else if (tag == "NoteDot") {
            NoteDot* dot = new NoteDot(note->score());
            dot->read(e);
            note->add(dot);
            }
      else if (tag == "Events") {
            note->playEvents().clear();    // remove default event
            while (e.readNextStartElement()) {
                  const QStringRef& etag(e.name());
                  if (etag == "Event") {
                        NoteEvent ne;
                        ne.read(e);
                        note->playEvents().append(ne);
                        }
                  else
                        e.unknown();
                  }
            if (Chord* ch = note->chord())
                  ch->setPlayEventType(PlayEventType::User);
            }
      else if (tag == "endSpanner") {
            int id = e.intAttribute("id");
            Spanner* sp = e.findSpanner(id);
            if (sp) {
                  sp->setEndElement(note);
                  if (sp->isTie())
                        note->setTieBack(toTie(sp));
                  else {
                        if (sp->isGlissando() && note->parent() && note->parent()->isChord())
                              toChord(note->parent())->setEndsGlissando(true);
                        note->addSpannerBack(sp);
                        }
                  e.removeSpanner(sp);
                  }
            else {
                  // End of a spanner whose start element will appear later;
                  // may happen for cross-staff spanner from a lower to a higher staff
                  // (for instance a glissando from bass to treble staff of piano).
                  // Create a place-holder spanner with end data
                  // (a TextLine is used only because both Spanner or SLine are abstract,
                  // the actual class does not matter, as long as it is derived from Spanner)
                  int id1 = e.intAttribute("id", -1);
                  Staff* staff = note->staff();
                  if (id1 != -1 &&
                              // DISABLE if pasting into a staff with linked staves
                              // because the glissando is not properly cloned into the linked staves
                              staff && (!e.pasteMode() || !staff->links() || staff->links()->empty())) {
                        Spanner* placeholder = new TextLine(note->score());
                        placeholder->setAnchor(Spanner::Anchor::NOTE);
                        placeholder->setEndElement(note);
                        placeholder->setTrack2(note->track());
                        placeholder->setTick(Fraction(0,1));
                        placeholder->setTick2(e.tick());
                        e.addSpanner(id1, placeholder);
                        }
                  }
            e.readNext();
            }
      else if (tag == "TextLine"
            || tag == "Glissando") {
            Spanner* sp = toSpanner(Element::name2Element(tag, note->score()));
            // check this is not a lower-to-higher cross-staff spanner we already got
            int id = e.intAttribute("id");
            Spanner* placeholder = e.findSpanner(id);
            if (placeholder && placeholder->endElement()) {
                  // if it is, fill end data from place-holder
                  sp->setAnchor(Spanner::Anchor::NOTE);           // make sure we can set a Note as end element
                  sp->setEndElement(placeholder->endElement());
                  sp->setTrack2(placeholder->track2());
                  sp->setTick(e.tick());                          // make sure tick2 will be correct
                  sp->setTick2(placeholder->tick2());
                  toNote(placeholder->endElement())->addSpannerBack(sp);
                  // remove no longer needed place-holder before reading the new spanner,
                  // as reading it also adds it to XML reader list of spanners,
                  // which would overwrite the place-holder
                  e.removeSpanner(placeholder);
                  delete placeholder;
                  }
            sp->setTrack(note->track());
            sp->read(e);
            Staff* staff = note->staff();
            // DISABLE pasting of glissandi into staves with other lionked staves
            // because the glissando is not properly cloned into the linked staves
            if (e.pasteMode() && staff && staff->links() && !staff->links()->empty()) {
                  e.removeSpanner(sp);    // read() added the element to the XMLReader: remove it
                  delete sp;
                  }
            else {
                  sp->setAnchor(Spanner::Anchor::NOTE);
                  sp->setStartElement(note);
                  sp->setTick(e.tick());
                  note->addSpannerFor(sp);
                  sp->setParent(note);
                  }
            adjustPlacement(sp);
            }
      else if (tag == "offset")
            note->Element::readProperties(e);
      else if (note->Element::readProperties(e))
            ;
      else
            return false;
      return true;
      }

//---------------------------------------------------------
//   readTextPropertyStyle206
//    This reads only the 'style' tag, so that it can be read
//    before setting anything else.
//---------------------------------------------------------

static bool readTextPropertyStyle206(XmlReader& e, TextBase* t, Element* be, QStringRef elementName)
      {
      QString s;
      if (e.readAheadAvailable()) {
            e.performReadAhead([&s, &elementName](QIODevice& dev) {
                  const QString closeTag = QString("</").append(elementName.toString()).append(">");
                  QByteArray arrLine = dev.readLine();
                  while (!arrLine.isEmpty()) {
                        QString line(arrLine);
                        if (line.contains("<style>")) {
                              QRegExp re("<style>([^<]+)</style>");
                              if (re.indexIn(line) > -1)
                                    s = re.cap(1);
                              return;
                              }
                        else if (line.contains(closeTag)) {
                              return;
                              }

                        arrLine = dev.readLine();
                        }
                  });
            }
      else
            return false;

      if (s.isEmpty())
            return true;

      if (!be->isTuplet()) {      // Hack
            if (excessTextStyles206.find(s) != excessTextStyles206.end()) {
                  // Init the text with a style that can't be stored as a user style
                  // due to the limit on the number of user styles possible.
                  // Use User-1, since it has all the possible user style pids
                  t->initTid(Tid::DEFAULT);
                  std::map<Sid, QVariant> styleVals = excessTextStyles206[s];
                  for (const StyledProperty& p : *textStyle("User-1")) {
                        if (t->getProperty(p.pid) == t->propertyDefault(p.pid) && styleVals.find(p.sid) != styleVals.end())
                              t->setProperty(p.pid, styleVals[p.sid]);
                        }
                  }
            else {
                  Tid ss;
                  ss = e.lookupUserTextStyle(s);
                  if (ss == Tid::TEXT_STYLES)
                        ss = textStyleFromName(s);
                  if (ss != Tid::TEXT_STYLES)
                        t->initTid(ss);
                  }
            }

      return true;
      }

//---------------------------------------------------------
//   readTextProperties206
//---------------------------------------------------------

static bool readTextProperties206(XmlReader& e, TextBase* t)
      {
      const QStringRef& tag(e.name());
      if (tag == "style") {
            e.skipCurrentElement(); // read in readTextPropertyStyle206
            }
      else if (tag == "foregroundColor")  // same as "color" ?
            e.skipCurrentElement();
      else if (tag == "frame")
            t->setFrameType(e.readBool() ? FrameType::SQUARE : FrameType::NO_FRAME);
      else if (tag == "frameRound")
            t->setFrameRound(e.readInt());
      else if (tag == "circle") {
            if (e.readBool())
                  t->setFrameType(FrameType::CIRCLE);
            else {
                  if (t->circle())
                        t->setFrameType(FrameType::SQUARE);
                  }
            }
      else if (tag == "paddingWidthS")
            t->setPaddingWidth(Spatium(e.readDouble()));
      else if (tag == "frameWidthS")
            t->setFrameWidth(Spatium(e.readDouble()));
      else if (tag == "frameColor")
            t->setFrameColor(e.readColor());
      else if (tag == "backgroundColor")
            t->setBgColor(e.readColor());
      else if (tag == "halign") {
            Align align = Align(int(t->align()) & int(~Align::HMASK));
            const QString& val(e.readElementText());
            if (val == "center")
                  align = align | Align::HCENTER;
            else if (val == "right")
                  align = align | Align::RIGHT;
            else if (val == "left")
                  ;
            else
                  qDebug("unknown alignment: <%s>", qPrintable(val));
            t->setAlign(align);
            }
      else if (tag == "valign") {
            Align align = Align(int(t->align()) & int(~Align::VMASK));
            const QString& val(e.readElementText());
            if (val == "center")
                  align = align | Align::VCENTER;
            else if (val == "bottom")
                  align = align | Align::BOTTOM;
            else if (val == "baseline")
                  align = align | Align::BASELINE;
            else if (val == "top")
                  ;
            else
                  qDebug("unknown alignment: <%s>", qPrintable(val));
            t->setAlign(align);
            }
      else if (tag == "pos") {
            t->readProperty(e, Pid::OFFSET);
            if ((char(t->align()) & char(Align::VMASK)) == char(Align::TOP))
                  t->ryoffset() += .5 * t->score()->spatium();     // HACK: bbox is different in 2.x
            adjustPlacement(t);
            }
      else if (!t->readProperties(e))
            return false;
      return true;
      }

//---------------------------------------------------------
//   readText206
//---------------------------------------------------------

static void readText206(XmlReader& e, TextBase* t, Element* be)
      {
      readTextPropertyStyle206(e, t, be, e.name());
      while (e.readNextStartElement()) {
            if (!readTextProperties206(e, t))
                  e.unknown();
            }
      }

//---------------------------------------------------------
//   read
//---------------------------------------------------------

static void readTempoText(TempoText* t, XmlReader& e)
      {
      readTextPropertyStyle206(e, t, t, e.name());
      while (e.readNextStartElement()) {
            const QStringRef& tag(e.name());
            if (tag == "tempo")
                  t->setTempo(e.readDouble());
            else if (tag == "followText")
                  t->setFollowText(e.readInt());
            else if (!readTextProperties206(e, t))
                  e.unknown();
            }
      // check sanity
      if (t->xmlText().isEmpty()) {
            t->setXmlText(QString("<sym>metNoteQuarterUp</sym> = %1").arg(lrint(60 * t->tempo())));
            t->setVisible(false);
            }
      else
            t->setXmlText(t->xmlText().replace("<sym>unicode", "<sym>met"));
      }

//---------------------------------------------------------
//   readMarker
//---------------------------------------------------------

static void readMarker(Marker* m, XmlReader& e)
      {
      readTextPropertyStyle206(e, m, m, e.name());
      Marker::Type mt = Marker::Type::SEGNO;

      while (e.readNextStartElement()) {
            const QStringRef& tag(e.name());
            if (tag == "label") {
                  QString s(e.readElementText());
                  m->setLabel(s);
                  mt = m->markerType(s);
                  }
            else if (!readTextProperties206(e, m))
                  e.unknown();
            }
      m->setMarkerType(mt);
      }

//---------------------------------------------------------
//   readDynamic
//---------------------------------------------------------

static void readDynamic(Dynamic* d, XmlReader& e)
      {
      readTextPropertyStyle206(e, d, d, e.name());
      while (e.readNextStartElement()) {
            const QStringRef& tag = e.name();
            if (tag == "subtype")
                  d->setDynamicType(e.readElementText());
            else if (tag == "velocity")
                  d->setVelocity(e.readInt());
            else if (tag == "dynType")
                  d->setDynRange(Dynamic::Range(e.readInt()));
            else if (!readTextProperties206(e, d))
                  e.unknown();
            }
      }

//---------------------------------------------------------
//   readTuplet
//---------------------------------------------------------

static void readTuplet(Tuplet* tuplet, XmlReader& e)
      {
      tuplet->setId(e.intAttribute("id", 0));
      while (e.readNextStartElement()) {
            const QStringRef& tag(e.name());
            if (tag == "Number") {
                  Text* _number = new Text(tuplet->score());
                  _number->setParent(tuplet);
                  _number->setComposition(true);
                  tuplet->setNumber(_number);
                  // _number reads property defaults from parent tuplet as "composition" is set:
                  tuplet->resetNumberProperty();
                  readText206(e, _number, tuplet);
                  _number->setVisible(tuplet->visible());     //?? override saved property
                  _number->setTrack(tuplet->track());
                  // move property flags from _number
                  for (auto p : { Pid::FONT_FACE, Pid::FONT_SIZE, Pid::FONT_STYLE, Pid::ALIGN })
                        tuplet->setPropertyFlags(p, _number->propertyFlags(p));
                  }
            else if (!readTupletProperties206(e, tuplet))
                  e.unknown();
            }
      Fraction r = (tuplet->ratio() == Fraction(1,1)) ? tuplet->ratio() : tuplet->ratio().reduced();
      Fraction f(r.denominator(), tuplet->baseLen().fraction().denominator());
      tuplet->setTicks(f.reduced());
      }

//---------------------------------------------------------
//   readLyrics
//---------------------------------------------------------

static void readLyrics(Lyrics* lyrics, XmlReader& e)
      {
      int   iEndTick = 0;           // used for backward compatibility
      Text* _verseNumber = 0;

      while (e.readNextStartElement()) {
            const QStringRef& tag(e.name());
            if (tag == "endTick") {
                  // store <endTick> tag value until a <ticks> tag has been read
                  // which positions this lyrics element in the score
                  iEndTick = e.readInt();
                  }
            else if (tag == "Number") {
                  _verseNumber = new Text(lyrics->score());
                  readText206(e, _verseNumber, lyrics);
                  _verseNumber->setParent(lyrics);
                  }
            else if (tag == "style")
                  e.readElementText();    // ignore style
            else if (!lyrics->readProperties(e))
                  e.unknown();
            }

      // if any endTick, make it relative to current tick
      if (iEndTick)
            lyrics->setTicks(Fraction::fromTicks(iEndTick) - e.tick());
      if (_verseNumber) {
            // TODO: add text to main text
            delete _verseNumber;
            }
      lyrics->setAutoplace(true);
      if (!lyrics->isStyled(Pid::OFFSET) && !e.pasteMode()) {
            // fix offset for pre-3.1 scores
            // 2.x and earlier: y offset was relative to staff; x offset was relative to center of notehead
            lyrics->rxoffset() -= lyrics->symWidth(SymId::noteheadBlack) * 0.5;
            //lyrics->ryoffset() -= lyrics->placeBelow() && lyrics->staff() ? lyrics->staff()->height() : 0.0;
            // temporarily set placement to above, since the original offset is relative to top of staff
            // depend on adjustPlacement() to change the placement if appropriate
            lyrics->setPlacement(Placement::ABOVE);
            adjustPlacement(lyrics);
            }
      }

//---------------------------------------------------------
//   readDurationProperties206
//---------------------------------------------------------

bool readDurationProperties206(XmlReader& e, DurationElement* de)
      {
      if (e.name() == "Tuplet") {
            int i = e.readInt();
            Tuplet* t = e.findTuplet(i);
            if (!t) {
                  qDebug("readDurationProperties206(): Tuplet id %d not found", i);
                  t = de->score()->searchTuplet(e, i);
                  if (t) {
                        qDebug("   ...found outside measure, input file corrupted?");
                        e.addTuplet(t);
                        }
                  }
            if (t) {
                  de->setTuplet(t);
                  if (!de->score()->undoStack()->active())     // HACK, also added in Undo::AddElement()
                        t->add(de);
                  }
            return true;
            }
      else if (de->Element::readProperties(e))
            return true;
      return false;
      }

//---------------------------------------------------------
//   readTupletProperties206
//---------------------------------------------------------

bool readTupletProperties206(XmlReader& e, Tuplet* de)
      {
      const QStringRef& tag(e.name());

      if (de->readStyledProperty(e, tag))
            ;
      else if (tag == "normalNotes")
            de->setProperty(Pid::NORMAL_NOTES, e.readInt());
      else if (tag == "actualNotes")
            de->setProperty(Pid::ACTUAL_NOTES, e.readInt());
      else if (tag == "p1")
            de->setProperty(Pid::P1, e.readPoint() * de->score()->spatium());
      else if (tag == "p2")
            de->setProperty(Pid::P2, e.readPoint() * de->score()->spatium());
      else if (tag == "baseNote")
            de->setBaseLen(TDuration(e.readElementText()));
      else if (tag == "Number") {
            Text* _number = new Text(de->score());
            de->setNumber(_number);
            _number->setComposition(true);
            _number->setParent(de);
//            _number->setSubStyleId(SubStyleId::TUPLET);
//            initSubStyle(SubStyleId::TUPLET);   // hack: initialize number
            for (auto p : { Pid::FONT_FACE, Pid::FONT_SIZE, Pid::FONT_STYLE, Pid::ALIGN })
                  _number->resetProperty(p);
            readText206(e, _number, de);
            _number->setVisible(de->visible());     //?? override saved property
            _number->setTrack(de->track());
            // move property flags from _number
            for (auto p : { Pid::FONT_FACE, Pid::FONT_SIZE, Pid::FONT_STYLE, Pid::ALIGN })
                  de->setPropertyFlags(p, _number->propertyFlags(p));
            }
      else if (!readDurationProperties206(e, de))
            return false;
      return true;
      }

//---------------------------------------------------------
//   readChordRestProperties206
//---------------------------------------------------------

bool readChordRestProperties206(XmlReader& e, ChordRest* ch)
      {
      const QStringRef& tag(e.name());

      if (tag == "durationType") {
            ch->setDurationType(e.readElementText());
            if (ch->actualDurationType().type() != TDuration::DurationType::V_MEASURE) {
                  if (ch->score()->mscVersion() < 112 && (ch->type() == ElementType::REST) &&
                              // for backward compatibility, convert V_WHOLE rests to V_MEASURE
                              // if long enough to fill a measure.
                              // OTOH, freshly created (un-initialized) rests have numerator == 0 (< 4/4)
                              // (see Fraction() constructor in fraction.h; this happens for instance
                              // when pasting selection from clipboard): they should not be converted
                              ch->ticks().numerator() != 0 &&
                              // rest durations are initialized to full measure duration when
                              // created upon reading the <Rest> tag (see Measure::read() )
                              // so a V_WHOLE rest in a measure of 4/4 or less => V_MEASURE
                              (ch->actualDurationType()==TDuration::DurationType::V_WHOLE && ch->ticks() <= Fraction(4, 4)) ) {
                        // old pre 2.0 scores: convert
                        ch->setDurationType(TDuration::DurationType::V_MEASURE);
                        }
                  else  // not from old score: set duration fraction from duration type
                        ch->setTicks(ch->actualDurationType().fraction());
                  }
            else {
                  if (ch->score()->mscVersion() <= 114) {
                        SigEvent event = ch->score()->sigmap()->timesig(e.tick());
                        ch->setTicks(event.timesig());
                        }
                  }
            }
      else if (tag == "BeamMode") {
            QString val(e.readElementText());
            Beam::Mode bm = Beam::Mode::AUTO;
            if (val == "auto")
                  bm = Beam::Mode::AUTO;
            else if (val == "begin")
                  bm = Beam::Mode::BEGIN;
            else if (val == "mid")
                  bm = Beam::Mode::MID;
            else if (val == "end")
                  bm = Beam::Mode::END;
            else if (val == "no")
                  bm = Beam::Mode::NONE;
            else if (val == "begin32")
                  bm = Beam::Mode::BEGIN32;
            else if (val == "begin64")
                  bm = Beam::Mode::BEGIN64;
            else
                  bm = Beam::Mode(val.toInt());
            ch->setBeamMode(bm);
            }
      else if (tag == "Articulation") {
            Element* el = readArticulation(ch, e);
            if (el->isFermata())
                  ch->segment()->add(el);
            else
                  ch->add(el);
            }
      else if (tag == "leadingSpace" || tag == "trailingSpace") {
            qDebug("ChordRest: %s obsolete", tag.toLocal8Bit().data());
            e.skipCurrentElement();
            }
      else if (tag == "Beam") {
            int id = e.readInt();
            Beam* beam = e.findBeam(id);
            if (beam)
                  beam->add(ch);        // also calls ch->setBeam(beam)
            else
                  qDebug("Beam id %d not found", id);
            }
      else if (tag == "small")
            ch->setSmall(e.readInt());
      else if (tag == "duration")
            ch->setTicks(e.readFraction());
      else if (tag == "ticklen") {      // obsolete (version < 1.12)
            int mticks = ch->score()->sigmap()->timesig(e.tick()).timesig().ticks();
            int i = e.readInt();
            if (i == 0)
                  i = mticks;
            if ((ch->type() == ElementType::REST) && (mticks == i)) {
                  ch->setDurationType(TDuration::DurationType::V_MEASURE);
                  ch->setTicks(Fraction::fromTicks(i));
                  }
            else {
                  Fraction f = Fraction::fromTicks(i);
                  ch->setTicks(f);
                  ch->setDurationType(TDuration(f));
                  }
            }
      else if (tag == "dots")
            ch->setDots(e.readInt());
      else if (tag == "move")
            ch->setStaffMove(e.readInt());
      else if (tag == "Slur") {
            int id = e.intAttribute("id");
            if (id == 0)
                  id = e.intAttribute("number");                  // obsolete
            Spanner* spanner = e.findSpanner(id);
            QString atype(e.attribute("type"));

            if (!spanner) {
                  if (atype == "stop") {
                        SpannerValues sv;
                        sv.spannerId = id;
                        sv.track2    = ch->track();
                        sv.tick2     = e.tick();
                        e.addSpannerValues(sv);
                        }
                  else if (atype == "start")
                        qDebug("spanner: start without spanner");
                  }
            else {
                  if (atype == "start") {
                        if (spanner->ticks() > Fraction(0,1) && spanner->tick() == Fraction(-1,1)) // stop has been read first
                              spanner->setTicks(spanner->ticks() - e.tick() - Fraction::fromTicks(1));
                        spanner->setTick(e.tick());
                        spanner->setTrack(ch->track());
                        if (spanner->type() == ElementType::SLUR)
                              spanner->setStartElement(ch);
                        if (e.pasteMode()) {
                              for (ScoreElement* el : spanner->linkList()) {
                                    if (el == spanner)
                                          continue;
                                    Spanner* ls = static_cast<Spanner*>(el);
                                    ls->setTick(spanner->tick());
                                    for (ScoreElement* ee : ch->linkList()) {
                                          ChordRest* cr = toChordRest(ee);
                                          if (cr->score() == ee->score() && cr->staffIdx() == ls->staffIdx()) {
                                                ls->setTrack(cr->track());
                                                if (ls->type() == ElementType::SLUR)
                                                      ls->setStartElement(cr);
                                                break;
                                                }
                                          }
                                    }
                              }
                        }
                  else if (atype == "stop") {
                        spanner->setTick2(e.tick());
                        spanner->setTrack2(ch->track());
                        if (spanner->isSlur())
                              spanner->setEndElement(ch);
                        ChordRest* start = toChordRest(spanner->startElement());
                        if (start)
                              spanner->setTrack(start->track());
                        if (e.pasteMode()) {
                              for (ScoreElement* el : spanner->linkList()) {
                                    if (el == spanner)
                                          continue;
                                    Spanner* ls = static_cast<Spanner*>(el);
                                    ls->setTick2(spanner->tick2());
                                    for (ScoreElement* ee : ch->linkList()) {
                                          ChordRest* cr = toChordRest(ee);
                                          if (cr->score() == ee->score() && cr->staffIdx() == ls->staffIdx()) {
                                                ls->setTrack2(cr->track());
                                                if (ls->type() == ElementType::SLUR)
                                                      ls->setEndElement(cr);
                                                break;
                                                }
                                          }
                                    }
                              }
                        }
                  else
                        qDebug("readChordRestProperties206(): unknown Slur type <%s>", qPrintable(atype));
                  }
            e.readNext();
            }
      else if (tag == "Lyrics") {
            Lyrics* l = new Lyrics(ch->score());
            l->setTrack(e.track());
            readLyrics(l, e);
            ch->add(l);
            }
      else if (tag == "pos") {
            QPointF pt = e.readPoint();
            ch->setOffset(pt * ch->spatium());
            }
      else if (!readDurationProperties206(e, ch))
            return false;
      return true;
      }

//---------------------------------------------------------
//   readChordProperties206
//---------------------------------------------------------

bool readChordProperties206(XmlReader& e, Chord* ch)
      {
      const QStringRef& tag(e.name());

      if (tag == "Note") {
            Note* note = new Note(ch->score());
            // the note needs to know the properties of the track it belongs to
            note->setTrack(ch->track());
            note->setChord(ch);
            readNote(note, e);
            ch->add(note);
            }
      else if (readChordRestProperties206(e, ch))
            ;
      else if (tag == "Stem") {
            Stem* s = new Stem(ch->score());
            s->read(e);
            ch->add(s);
            }
      else if (tag == "Hook") {
            Hook* hook = new Hook(ch->score());
            hook->read(e);
            ch->add(hook);
            }
      else if (tag == "appoggiatura") {
            ch->setNoteType(NoteType::APPOGGIATURA);
            e.readNext();
            }
      else if (tag == "acciaccatura") {
            ch->setNoteType(NoteType::ACCIACCATURA);
            e.readNext();
            }
      else if (tag == "grace4") {
            ch->setNoteType(NoteType::GRACE4);
            e.readNext();
            }
      else if (tag == "grace16") {
            ch->setNoteType(NoteType::GRACE16);
            e.readNext();
            }
      else if (tag == "grace32") {
            ch->setNoteType(NoteType::GRACE32);
            e.readNext();
            }
      else if (tag == "grace8after") {
            ch->setNoteType(NoteType::GRACE8_AFTER);
            e.readNext();
            }
      else if (tag == "grace16after") {
            ch->setNoteType(NoteType::GRACE16_AFTER);
            e.readNext();
            }
      else if (tag == "grace32after") {
            ch->setNoteType(NoteType::GRACE32_AFTER);
            e.readNext();
            }
      else if (tag == "StemSlash") {
            StemSlash* ss = new StemSlash(ch->score());
            ss->read(e);
            ch->add(ss);
            }
      else if (ch->readProperty(tag, e, Pid::STEM_DIRECTION))
            ;
      else if (tag == "noStem")
            ch->setNoStem(e.readInt());
      else if (tag == "Arpeggio") {
            Arpeggio* arpeggio = new Arpeggio(ch->score());
            arpeggio->setTrack(ch->track());
            arpeggio->read(e);
            arpeggio->setParent(ch);
            ch->add(arpeggio);
            }
      // old glissando format, chord-to-chord, attached to its final chord
      else if (tag == "Glissando") {
            // the measure we are reading is not inserted in the score yet
            // as well as, possibly, the glissando intended initial chord;
            // then we cannot fully link the glissando right now;
            // temporarily attach the glissando to its final note as a back spanner;
            // after the whole score is read, Score::connectTies() will look for
            // the suitable initial note
            Note* finalNote = ch->upNote();
            Glissando* gliss = new Glissando(ch->score());
            gliss->read(e);
            gliss->setAnchor(Spanner::Anchor::NOTE);
            gliss->setStartElement(nullptr);
            gliss->setEndElement(nullptr);
            // in TAB, use straight line with no text
            if (ch->score()->staff(e.track() >> 2)->isTabStaff(ch->tick())) {
                  gliss->setGlissandoType(GlissandoType::STRAIGHT);
                  gliss->setShowText(false);
                  }
            finalNote->addSpannerBack(gliss);
            }
      else if (tag == "Tremolo") {
            Tremolo* tremolo = new Tremolo(ch->score());
            tremolo->setTrack(ch->track());
            tremolo->read(e);
            tremolo->setParent(ch);
            tremolo->setDurationType(ch->durationType());
            ch->setTremolo(tremolo);
            }
      else if (tag == "tickOffset")       // obsolete
            ;
      else if (tag == "ChordLine") {
            ChordLine* cl = new ChordLine(ch->score());
            cl->read(e);
            QPointF o = cl->offset();
            cl->setOffset(0.0, 0.0);
            ch->add(cl);
            e.fixOffsets().append({cl, o});
            }
      else
            return false;
      return true;
      }

//---------------------------------------------------------
//   convertDoubleArticulations
//    Replace double articulations with proper SMuFL
//    symbols which were not available for use prior to 3.0
//---------------------------------------------------------

static void convertDoubleArticulations(Chord* chord, XmlReader& e)
      {
      std::vector<Articulation*> pairableArticulations;
      for (Articulation* a : chord->articulations()) {
            if (a->isStaccato() || a->isTenuto()
               || a->isAccent() || a->isMarcato()) {
                  pairableArticulations.push_back(a);
                  };
            }
      if (pairableArticulations.size() != 2)
            // Do not replace triple articulation if this happens
            return;

      SymId newSymId = SymId::noSym;
      for (int i = 0; i < 2; ++i) {
            if (newSymId != SymId::noSym)
                  break;
            Articulation* ai = pairableArticulations[i];
            Articulation* aj = pairableArticulations[(i == 0) ? 1 : 0];
            if (ai->isStaccato()) {
                  if (aj->isAccent())
                        newSymId = SymId::articAccentStaccatoAbove;
                  else if (aj->isMarcato())
                        newSymId = SymId::articMarcatoStaccatoAbove;
                  else if (aj->isTenuto())
                        newSymId = SymId::articTenutoStaccatoAbove;
                  }
            else if (ai->isTenuto()) {
                  if (aj->isAccent())
                        newSymId = SymId::articTenutoAccentAbove;
                  else if (aj->isMarcato())
                        newSymId = SymId::articMarcatoTenutoAbove;
                  }
            }

      if (newSymId != SymId::noSym) {
            // We reuse old articulation and change symbol ID
            // rather than constructing a new articulation
            // in order to preserve its other properties.
            Articulation* newArtic = pairableArticulations[0];
            for (Articulation* a : pairableArticulations) {
                  chord->remove(a);
                  if (a != newArtic) {
                        if (LinkedElements* link = a->links())
                              e.linkIds().remove(link->lid());
                        delete a;
                        }
                  }

            ArticulationAnchor anchor = newArtic->anchor();
            newArtic->setSymId(newSymId);
            newArtic->setAnchor(anchor);
            chord->add(newArtic);
            }
      }

//---------------------------------------------------------
//   readChord
//---------------------------------------------------------

static void readChord(Chord* chord, XmlReader& e)
      {
      while (e.readNextStartElement()) {
            const QStringRef& tag(e.name());
            if (tag == "Note") {
                  Note* note = new Note(chord->score());
                  // the note needs to know the properties of the track it belongs to
                  note->setTrack(chord->track());
                  note->setChord(chord);
                  readNote(note, e);
                  chord->add(note);
                  }
            else if (tag == "Stem") {
                  Stem* stem = new Stem(chord->score());
                  while (e.readNextStartElement()) {
                        const QStringRef& t(e.name());
                        if (t == "subtype")        // obsolete
                              e.skipCurrentElement();
                        else if (!stem->readProperties(e))
                              e.unknown();
                        }
                  chord->add(stem);
                  }
            else if (tag == "Lyrics") {
                  Lyrics* lyrics = new Lyrics(chord->score());
                  lyrics->setTrack(e.track());
                  readLyrics(lyrics, e);
                  chord->add(lyrics);
                  }
            else if (readChordProperties206(e, chord))
                  ;
            else
                  e.unknown();
            }
      convertDoubleArticulations(chord, e);
      }

//---------------------------------------------------------
//   readRest
//---------------------------------------------------------

static void readRest(Rest* rest, XmlReader& e)
      {
      while (e.readNextStartElement()) {
            if (!readChordRestProperties206(e, rest))
                  e.unknown();
            }
      }

//---------------------------------------------------------
//   readTextLineProperties
//---------------------------------------------------------

static bool readTextLineProperties(XmlReader& e, TextLineBase* tl)
      {
      const QStringRef& tag(e.name());

      if (tag == "beginText") {
            Text* text = new Text(tl->score());
            readText206(e, text, tl);
            tl->setBeginText(text->xmlText());
            delete text;
            }
      else if (tag == "continueText") {
            Text* text = new Text(tl->score());
            readText206(e, text, tl);
            tl->setContinueText(text->xmlText());
            delete text;
            }
      else if (tag == "endText") {
            Text* text = new Text(tl->score());
            readText206(e, text, tl);
            tl->setEndText(text->xmlText());
            delete text;
            }
      else if (tag == "beginHook")
            tl->setBeginHookType(e.readBool() ? HookType::HOOK_90 : HookType::NONE);
      else if (tag == "endHook")
            tl->setEndHookType(e.readBool() ? HookType::HOOK_90 : HookType::NONE);
      else if (tag == "beginHookType")
            tl->setBeginHookType(e.readInt() == 0 ? HookType::HOOK_90 : HookType::HOOK_45);
      else if (tag == "endHookType")
            tl->setEndHookType(e.readInt() == 0 ? HookType::HOOK_90 : HookType::HOOK_45);
      else if (tl->readProperties(e))
            return true;
      return true;
      }

//---------------------------------------------------------
//   readVolta206
//---------------------------------------------------------

static void readVolta206(XmlReader& e, Volta* volta)
      {
      while (e.readNextStartElement()) {
            const QStringRef& tag(e.name());
            if (tag == "endings") {
                  QString s = e.readElementText();
                  QStringList sl = s.split(",", QString::SkipEmptyParts);
                  volta->endings().clear();
                  for (const QString& l : sl) {
                        int i = l.simplified().toInt();
                        volta->endings().append(i);
                        }
                  }
            else if (tag == "lineWidth") {
                  volta->setLineWidth(e.readDouble() * volta->spatium());
                  // TODO lineWidthStyle = PropertyStyle::UNSTYLED;
                  }
            else if (!readTextLineProperties(e, volta))
                  e.unknown();
            }
      adjustPlacement(volta);
      }

//---------------------------------------------------------
//   readPedal
//---------------------------------------------------------

static void readPedal(XmlReader& e, Pedal* pedal)
      {
      while (e.readNextStartElement()) {
            if (!readTextLineProperties(e, pedal))
                  e.unknown();
            }
      adjustPlacement(pedal);
      }

//---------------------------------------------------------
//   readOttava
//---------------------------------------------------------

static void readOttava(XmlReader& e, Ottava* ottava)
      {
      while (e.readNextStartElement()) {
            const QStringRef& tag(e.name());
            if (tag == "subtype") {
                  QString s = e.readElementText();
                  bool ok;
                  int idx = s.toInt(&ok);
                  if (!ok) {
                        idx = 0;    // OttavaType::OTTAVA_8VA;
                        int i = 0;
                        for (auto p :  { "8va","8vb","15ma","15mb","22ma","22mb" } ) {
                              if (p == s) {
                                    idx = i;
                                    break;
                                    }
                              ++i;
                              }
                        }
                  ottava->setOttavaType(OttavaType(idx));
                  }
            else if (tag == "numbersOnly") {
                  ottava->setNumbersOnly(e.readBool());
                  //TODO numbersOnlyStyle = PropertyFlags::UNSTYLED;
                  }
            else if (!readTextLineProperties(e, ottava))
                  e.unknown();
            }
      ottava->styleChanged();
      adjustPlacement(ottava);
      }

//---------------------------------------------------------
//   readHairpin206
//---------------------------------------------------------

void readHairpin206(XmlReader& e, Hairpin* h)
      {
      bool useText = false;
      while (e.readNextStartElement()) {
            const QStringRef& tag(e.name());
            if (tag == "subtype")
                  h->setHairpinType(HairpinType(e.readInt()));
            else if (tag == "lineWidth") {
                  h->setLineWidth(e.readDouble() * h->spatium());
                  // lineWidthStyle = PropertyFlags::UNSTYLED;
                  }
            else if (tag == "hairpinHeight") {
                  h->setHairpinHeight(Spatium(e.readDouble()));
                  // hairpinHeightStyle = PropertyFlags::UNSTYLED;
                  }
            else if (tag == "hairpinContHeight") {
                  h->setHairpinContHeight(Spatium(e.readDouble()));
                  // hairpinContHeightStyle = PropertyFlags::UNSTYLED;
                  }
            else if (tag == "hairpinCircledTip")
                  h->setHairpinCircledTip(e.readInt());
            else if (tag == "veloChange")
                  h->setVeloChange(e.readInt());
            else if (tag == "dynType")
                  h->setDynRange(Dynamic::Range(e.readInt()));
            else if (tag == "useTextLine") {      // < 206
                  e.readInt();
                  if (h->hairpinType() == HairpinType::CRESC_HAIRPIN)
                        h->setHairpinType(HairpinType::CRESC_LINE);
                  else if (h->hairpinType() == HairpinType::DECRESC_HAIRPIN)
                        h->setHairpinType(HairpinType::DECRESC_LINE);
                  useText = true;
                  }
            else if (!readTextLineProperties(e, h))
                  e.unknown();
            }
      if (!useText) {
            h->setBeginText("");
            h->setContinueText("");
            h->setEndText("");
            }
      adjustPlacement(h);
      }

//---------------------------------------------------------
//   readTrill206
//---------------------------------------------------------

void readTrill206(XmlReader& e, Trill* t)
      {
      while (e.readNextStartElement()) {
            const QStringRef& tag(e.name());
            if (tag == "subtype")
                  t->setTrillType(e.readElementText());
            else if (tag == "Accidental") {
                  Accidental* _accidental = new Accidental(t->score());
                  readAccidental206(_accidental, e);
                  _accidental->setParent(t);
                  t->setAccidental(_accidental);
                  }
            else if (tag == "ornamentStyle")
                  t->readProperty(e, Pid::ORNAMENT_STYLE);
            else if (tag == "play")
                  t->setPlayArticulation(e.readBool());
            else if (!t->SLine::readProperties(e))
                  e.unknown();
            }
      adjustPlacement(t);
      }

//---------------------------------------------------------
//   readTextLine206
//---------------------------------------------------------

void readTextLine206(XmlReader& e, TextLineBase* tlb)
      {
      while (e.readNextStartElement()) {
            if (!readTextLineProperties(e, tlb))
                  e.unknown();
            }
      adjustPlacement(tlb);
      }

//---------------------------------------------------------
//   setFermataPlacement
//    set fermata placement from old ArticulationAnchor
//    for backwards compatibility
//---------------------------------------------------------

static void setFermataPlacement(Element* el, ArticulationAnchor anchor, Direction direction)
      {
      if (direction == Direction::UP)
            el->setPlacement(Placement::ABOVE);
      else if (direction == Direction::DOWN)
            el->setPlacement(Placement::BELOW);
      else {
            switch (anchor) {
                  case ArticulationAnchor::TOP_STAFF:
                  case ArticulationAnchor::TOP_CHORD:
                        el->setPlacement(Placement::ABOVE);
                        break;

                  case ArticulationAnchor::BOTTOM_STAFF:
                  case ArticulationAnchor::BOTTOM_CHORD:
                        el->setPlacement(Placement::BELOW);
                        break;

                  case ArticulationAnchor::CHORD:
                        break;
                  default:
                        break;
                  }
            }
      }

//---------------------------------------------------------
//   readArticulation
//---------------------------------------------------------

Element* readArticulation(Element* parent, XmlReader& e)
      {
      Element* el = 0;
      SymId sym = SymId::fermataAbove;          // default -- backward compatibility (no type = ufermata in 1.2)
      ArticulationAnchor anchor  = ArticulationAnchor::TOP_STAFF;
      Direction direction = Direction::AUTO;
      Score* score = parent->score();
      int track = parent->track();
      double timeStretch = 0.0;
      bool useDefaultPlacement = true;

      while (e.readNextStartElement()) {
            const QStringRef& tag(e.name());
            if (tag == "subtype") {
                  QString s = e.readElementText();
                  if (s[0].isDigit()) {
                        int oldType = s.toInt();
                        sym = articulationNames[oldType].id;
                        }
                  else {
                        sym = oldArticulationNames2SymId(s);
                        if (sym == SymId::noSym) {
                              struct {
                                    const char* name;
                                    bool up;
                                    SymId id;
                                    } al[] =
                                    {
                                    { "fadein",                    true,  SymId::guitarFadeIn },
                                    { "fadeout",                   true,  SymId::guitarFadeOut },
                                    { "volumeswell",               true,  SymId::guitarVolumeSwell },
                                    { "wigglesawtooth",            true,  SymId::wiggleSawtooth },
                                    { "wigglesawtoothwide",        true,  SymId::wiggleSawtoothWide },
                                    { "wigglevibratolargefaster",  true,  SymId::wiggleVibratoLargeFaster },
                                    { "wigglevibratolargeslowest", true,  SymId::wiggleVibratoLargeSlowest },
                                    { "umarcato",                  true,  SymId::articMarcatoAbove },
                                    { "dmarcato",                  false, SymId::articMarcatoBelow },
                                    { "ufermata",                  true,  SymId::fermataAbove },
                                    { "dfermata",                  false, SymId::fermataBelow },
                                    { "ushortfermata",             true,  SymId::fermataShortAbove },
                                    { "dshortfermata",             false, SymId::fermataShortBelow },
                                    { "ulongfermata",              true,  SymId::fermataLongAbove },
                                    { "dlongfermata",              false, SymId::fermataLongBelow },
                                    { "uverylongfermata",          true,  SymId::fermataVeryLongAbove },
                                    { "dverylongfermata",          false, SymId::fermataVeryLongBelow },

                                    // watch out, bug in 1.2 uportato and dportato are reversed
                                    { "dportato",                  true,  SymId::articTenutoStaccatoAbove },
                                    { "uportato",                  false, SymId::articTenutoStaccatoBelow },
                                    { "ustaccatissimo",            true,  SymId::articStaccatissimoAbove },
                                    { "dstaccatissimo",            false, SymId::articStaccatissimoBelow }
                                    };
                              int i;
                              int n = sizeof(al) / sizeof(*al);
                              for (i = 0; i < n; ++i) {
                                    if (s == al[i].name) {
                                          sym       = al[i].id;
                                          bool up   = al[i].up;
                                          direction = up ? Direction::UP : Direction::DOWN;
                                          if ((direction == Direction::DOWN) != (track & 1))
                                                useDefaultPlacement = false;
                                          break;
                                          }
                                    }
                              if (i == n) {
                                    sym = Sym::name2id(s);
                                    if (sym == SymId::noSym)
                                          qDebug("Articulation: unknown type <%s>", qPrintable(s));
                                    }
                              }
                        }
                  switch (sym) {
                        case SymId::fermataAbove:
                        case SymId::fermataBelow:
                        case SymId::fermataShortAbove:
                        case SymId::fermataShortBelow:
                        case SymId::fermataLongAbove:
                        case SymId::fermataLongBelow:
                        case SymId::fermataVeryLongAbove:
                        case SymId::fermataVeryLongBelow:
                              el = new Fermata(sym, score);
                              break;
                        default:
                              el = new Articulation(sym, score);
                              toArticulation(el)->setDirection(direction);
                              break;
                        };
                  }
            else if (tag == "anchor") {
                  useDefaultPlacement = false;
                  if (!el || el->isFermata())
                        anchor = ArticulationAnchor(e.readInt());
                  else
                        el->readProperties(e);
                  }
            else  if (tag == "direction") {
                  useDefaultPlacement = false;
                  if (!el || el->isFermata())
                        direction = toDirection(e.readElementText());
                  else
                        el->readProperties(e);
                  }
            else if (tag == "timeStretch") {
                  timeStretch = e.readDouble();
                  }
            else {
                  if (!el) {
                        qDebug("not handled <%s>", qPrintable(tag.toString()));
                        }
                  if (!el || !el->readProperties(e))
                        e.unknown();
                  }
            }
      // Special case for "no type" = ufermata, with missing subtype tag
      if (!el)
            el = new Fermata(sym, score);
      if (el->isFermata()) {
            if (timeStretch != 0.0)
                  el->setProperty(Pid::TIME_STRETCH, timeStretch);
            if (useDefaultPlacement)
                  el->setPlacement(track & 1 ? Placement::BELOW : Placement::ABOVE);
            else
                  setFermataPlacement(el, anchor, direction);
            }
      el->setTrack(track);
      return el;
      }

//---------------------------------------------------------
//   readSlurTieProperties
//---------------------------------------------------------

static bool readSlurTieProperties(XmlReader& e, SlurTie* st)
      {
      const QStringRef& tag(e.name());

      if (st->readProperty(tag, e, Pid::SLUR_DIRECTION))
            ;
      else if (tag == "lineType")
            st->setLineType(e.readInt());
      else if (tag == "SlurSegment") {
            SlurTieSegment* s = st->newSlurTieSegment();
            s->read(e);
            st->add(s);
            }
      else if (!st->Element::readProperties(e))
            return false;
      return true;
      }

//---------------------------------------------------------
//   readSlur206
//---------------------------------------------------------

void readSlur206(XmlReader& e, Slur* s)
      {
      s->setTrack(e.track());      // set staff
      e.addSpanner(e.intAttribute("id"), s);
      while (e.readNextStartElement()) {
            const QStringRef& tag(e.name());
            if (tag == "track2")
                  s->setTrack2(e.readInt());
            else if (tag == "startTrack")       // obsolete
                  s->setTrack(e.readInt());
            else if (tag == "endTrack")         // obsolete
                  e.readInt();
            else if (!readSlurTieProperties(e, s))
                  e.unknown();
            }
      if (s->track2() == -1)
            s->setTrack2(s->track());
      }

//---------------------------------------------------------
//   readTie206
//---------------------------------------------------------

void readTie206(XmlReader& e, Tie* t)
      {
      e.addSpanner(e.intAttribute("id"), t);
      while (e.readNextStartElement()) {
            if (readSlurTieProperties(e, t))
                  ;
            else
                  e.unknown();
            }
      if (t->score()->mscVersion() <= 114 && t->spannerSegments().size() == 1) {
            // ignore manual adjustments to single-segment ties in older scores
            TieSegment* ss = t->frontSegment();
            QPointF zeroP;
            ss->ups(Grip::START).off     = zeroP;
            ss->ups(Grip::BEZIER1).off   = zeroP;
            ss->ups(Grip::BEZIER2).off   = zeroP;
            ss->ups(Grip::END).off       = zeroP;
            ss->setOffset(zeroP);
            ss->setUserOff2(zeroP);
            }
      }

//---------------------------------------------------------
//   readMeasure
//---------------------------------------------------------

static void readMeasure(Measure* m, int staffIdx, XmlReader& e)
      {
      Segment* segment = 0;
      qreal _spatium = m->spatium();
      Score* score = m->score();

      QList<Chord*> graceNotes;
      e.tuplets().clear();
      e.setTrack(staffIdx * VOICES);

      m->createStaves(staffIdx);

      // tick is obsolete
      if (e.hasAttribute("tick"))
            e.setTick(Fraction::fromTicks(score->fileDivision(e.intAttribute("tick"))));

      bool irregular;
      if (e.hasAttribute("len")) {
            QStringList sl = e.attribute("len").split('/');
            if (sl.size() == 2)
                  m->setTicks(Fraction(sl[0].toInt(), sl[1].toInt()));
            else
                  qDebug("illegal measure size <%s>", qPrintable(e.attribute("len")));
            irregular = true;
            score->sigmap()->add(m->tick().ticks(), SigEvent(m->ticks(), m->timesig()));
            score->sigmap()->add(m->endTick().ticks(), SigEvent(m->timesig()));
            }
      else
            irregular = false;

      Staff* staff = score->staff(staffIdx);
      Fraction timeStretch(staff->timeStretch(m->tick()));

      // keep track of tick of previous element
      // this allows markings that need to apply to previous element to do so
      // even though we may have already advanced to next tick position
      Fraction lastTick = e.tick();

      while (e.readNextStartElement()) {
            const QStringRef& tag(e.name());

            if (tag == "move")
                  e.setTick(e.readFraction() + m->tick());
            else if (tag == "tick") {
                  e.setTick(Fraction::fromTicks(score->fileDivision(e.readInt())));
                  lastTick = e.tick();
                  }
            else if (tag == "BarLine") {
                  Fermata* fermataAbove = nullptr;
                  Fermata* fermataBelow = nullptr;
                  BarLine* bl = new BarLine(score);
                  bl->setTrack(e.track());
                  while (e.readNextStartElement()) {
                        const QStringRef& t(e.name());
                        if (t == "subtype")
                              bl->setBarLineType(e.readElementText());
                        else if (t == "customSubtype")                      // obsolete
                              e.readInt();
                        else if (t == "span") {
                              //TODO bl->setSpanFrom(e.intAttribute("from", bl->spanFrom()));  // obsolete
                              // bl->setSpanTo(e.intAttribute("to", bl->spanTo()));            // obsolete
                              int span = e.readInt();
                              if (span)
                                    span--;
                              bl->setSpanStaff(span);
                              }
                        else if (t == "spanFromOffset")
                              bl->setSpanFrom(e.readInt());
                        else if (t == "spanToOffset")
                              bl->setSpanTo(e.readInt());
                        else if (t == "Articulation") {
                              Element* el = readArticulation(bl, e);
                              if (el->isFermata()) {
                                    if (el->placement() == Placement::ABOVE)
                                          fermataAbove = toFermata(el);
                                    else {
                                          fermataBelow = toFermata(el);
                                          fermataBelow->setTrack((bl->staffIdx() + bl->spanStaff()) * VOICES);
                                          }
                                    }
                              else
                                    bl->add(el);
                              }
                        else if (!bl->Element::readProperties(e))
                              e.unknown();
                        }
                  //
                  //  StartRepeatBarLine: always at the beginning tick of a measure, always BarLineType::START_REPEAT
                  //  BarLine:            in the middle of a measure, has no semantic
                  //  EndBarLine:         at the end tick of a measure
                  //  BeginBarLine:       first segment of a measure

                  SegmentType st;
                  if ((e.tick() != m->tick()) && (e.tick() != m->endTick()))
                        st = SegmentType::BarLine;
                  else if (bl->barLineType() == BarLineType::START_REPEAT && e.tick() == m->tick())
                        st = SegmentType::StartRepeatBarLine;
                  else if (e.tick() == m->tick() && segment == 0)
                        st = SegmentType::BeginBarLine;
                  else
                        st = SegmentType::EndBarLine;
                  segment = m->getSegment(st, e.tick());
                  segment->add(bl);
                  bl->layout();
                  if (fermataAbove)
                        segment->add(fermataAbove);
                  if (fermataBelow)
                        segment->add(fermataBelow);
                  }
            else if (tag == "Chord") {
                  Chord* chord = new Chord(score);
                  chord->setTrack(e.track());
                  segment = m->getSegment(SegmentType::ChordRest, e.tick());
                  chord->setParent(segment);
                  readChord(chord, e);
                  if (chord->noteType() != NoteType::NORMAL)
                        graceNotes.push_back(chord);
                  else {
                        segment->add(chord);
                        for (int i = 0; i < graceNotes.size(); ++i) {
                              Chord* gc = graceNotes[i];
                              gc->setGraceIndex(i);
                              chord->add(gc);
                              }
                        graceNotes.clear();
                        Fraction crticks = chord->actualTicks();
                        lastTick         = e.tick();
                        e.incTick(crticks);
                        }
                  }
            else if (tag == "Rest") {
                  Rest* rest = new Rest(score);
                  rest->setDurationType(TDuration::DurationType::V_MEASURE);
                  rest->setTicks(m->timesig()/timeStretch);
                  rest->setTrack(e.track());
                  segment = m->getSegment(SegmentType::ChordRest, e.tick());
                  rest->setParent(segment);
                  readRest(rest, e);
                  segment->add(rest);

                  if (!rest->ticks().isValid())     // hack
                        rest->setTicks(m->timesig()/timeStretch);

                  lastTick = e.tick();
                  e.incTick(rest->actualTicks());
                  }
            else if (tag == "Breath") {
                  Breath* breath = new Breath(score);
                  breath->setTrack(e.track());
                  Fraction tick = e.tick();
                  breath->read(e);
                  // older scores placed the breath segment right after the chord to which it applies
                  // rather than before the next chordrest segment with an element for the staff
                  // result would be layout too far left if there are other segments due to notes in other staves
                  // we need to find tick of chord to which this applies, and add its duration
                  Fraction prevTick;
                  if (e.tick() < tick)
                        prevTick = e.tick();    // use our own tick if we explicitly reset to earlier position
                  else
                        prevTick = lastTick;    // otherwise use tick of previous tick/chord/rest tag
                  // find segment
                  Segment* prev = m->findSegment(SegmentType::ChordRest, prevTick);
                  if (prev) {
                        // find chordrest
                        ChordRest* lastCR = toChordRest(prev->element(e.track()));
                        if (lastCR)
                              tick = prevTick + lastCR->actualTicks();
                        }
                  segment = m->getSegment(SegmentType::Breath, tick);
                  segment->add(breath);
                  }
            else if (tag == "endSpanner") {
                  int id = e.attribute("id").toInt();
                  Spanner* spanner = e.findSpanner(id);
                  if (spanner) {
                        spanner->setTicks(e.tick() - spanner->tick());
                        // if (spanner->track2() == -1)
                              // the absence of a track tag [?] means the
                              // track is the same as the beginning of the slur
                        if (spanner->track2() == -1)
                              spanner->setTrack2(spanner->track() ? spanner->track() : e.track());
                        }
                  else {
                        // remember "endSpanner" values
                        SpannerValues sv;
                        sv.spannerId = id;
                        sv.track2    = e.track();
                        sv.tick2     = e.tick();
                        e.addSpannerValues(sv);
                        }
                  e.readNext();
                  }
            else if (tag == "Slur") {
                  Slur *sl = new Slur(score);
                  sl->setTick(e.tick());
                  readSlur206(e, sl);
                  //
                  // check if we already saw "endSpanner"
                  //
                  int id = e.spannerId(sl);
                  const SpannerValues* sv = e.spannerValues(id);
                  if (sv) {
                        sl->setTick2(sv->tick2);
                        sl->setTrack2(sv->track2);
                        }
                  score->addSpanner(sl);
                  }
            else if (tag == "HairPin"
               || tag == "Pedal"
               || tag == "Ottava"
               || tag == "Trill"
               || tag == "TextLine"
               || tag == "Volta") {
                  Spanner* sp = toSpanner(Element::name2Element(tag, score));
                  sp->setTrack(e.track());
                  sp->setTick(e.tick());
                  sp->eraseSpannerSegments();
                  e.addSpanner(e.intAttribute("id", -1), sp);

                  if (tag == "Volta")
                        readVolta206(e, toVolta(sp));
                  else if (tag == "Pedal")
                        readPedal(e, toPedal(sp));
                  else if (tag == "Ottava")
                        readOttava(e, toOttava(sp));
                  else if (tag == "HairPin")
                        readHairpin206(e, toHairpin(sp));
                  else if (tag == "Trill")
                        readTrill206(e, toTrill(sp));
                  else
                        readTextLine206(e, toTextLineBase(sp));
                  score->addSpanner(sp);
                  //
                  // check if we already saw "endSpanner"
                  //
                  int id = e.spannerId(sp);
                  const SpannerValues* sv = e.spannerValues(id);
                  if (sv) {
                        sp->setTicks(sv->tick2 - sp->tick());
                        sp->setTrack2(sv->track2);
                        }
                  }
            else if (tag == "RepeatMeasure") {
                  RepeatMeasure* rm = new RepeatMeasure(score);
                  rm->setTrack(e.track());
                  readRest(rm, e);
                  segment = m->getSegment(SegmentType::ChordRest, e.tick());
                  segment->add(rm);
                  lastTick = e.tick();
                  e.incTick(m->ticks());
                  }
            else if (tag == "Clef") {
                  Clef* clef = new Clef(score);
                  clef->setTrack(e.track());
                  clef->read(e);
                  clef->setGenerated(false);
                  if (e.tick().isZero()) {
                        if (score->staff(staffIdx)->clef(Fraction(0,1)) != clef->clefType())
                              score->staff(staffIdx)->setDefaultClefType(clef->clefType());
                        if (clef->links() && clef->links()->size() == 1) {
                              e.linkIds().remove(clef->links()->lid());
                              qDebug("remove link %d", clef->links()->lid());
                              }
                        delete clef;
                        continue;
                        }
                  // there may be more than one clef segment for same tick position
                  if (!segment) {
                        // this is the first segment of measure
                        segment = m->getSegment(SegmentType::Clef, e.tick());
                        }
                  else {
                        bool firstSegment = false;
                        // the first clef may be missing and is added later in layout
                        for (Segment* s = m->segments().first(); s && s->tick() == e.tick(); s = s->next()) {
                              if (s->segmentType() == SegmentType::Clef
                                    // hack: there may be other segment types which should
                                    // generate a clef at current position
                                 || s->segmentType() == SegmentType::StartRepeatBarLine
                                 ) {
                                    firstSegment = true;
                                    break;
                                    }
                              }
                        if (firstSegment) {
                              Segment* ns = 0;
                              if (segment->next()) {
                                    ns = segment->next();
                                    while (ns && ns->tick() < e.tick())
                                          ns = ns->next();
                                    }
                              segment = 0;
                              for (Segment* s = ns; s && s->tick() == e.tick(); s = s->next()) {
                                    if (s->segmentType() == SegmentType::Clef) {
                                          segment = s;
                                          break;
                                          }
                                    }
                              if (!segment) {
                                    segment = new Segment(m, SegmentType::Clef, e.tick() - m->tick());
                                    m->segments().insert(segment, ns);
                                    }
                              }
                        else {
                              // this is the first clef: move to left
                              segment = m->getSegment(SegmentType::Clef, e.tick());
                              }
                        }
                  if (e.tick() != m->tick())
                        clef->setSmall(true);         // TODO: layout does this ?
                  segment->add(clef);
                  }
            else if (tag == "TimeSig") {
                  TimeSig* ts = new TimeSig(score);
                  ts->setTrack(e.track());
                  ts->read(e);
                  // if time sig not at beginning of measure => courtesy time sig
                  Fraction currTick = e.tick();
                  bool courtesySig = (currTick > m->tick());
                  if (courtesySig) {
                        // if courtesy sig., just add it without map processing
                        segment = m->getSegment(SegmentType::TimeSigAnnounce, currTick);
                        segment->add(ts);
                        }
                  else {
                        // if 'real' time sig., do full process
                        segment = m->getSegment(SegmentType::TimeSig, currTick);
                        segment->add(ts);

                        timeStretch = ts->stretch().reduced();
                        m->setTimesig(ts->sig() / timeStretch);

                        if (irregular) {
                              score->sigmap()->add(m->tick().ticks(), SigEvent(m->ticks(), m->timesig()));
                              score->sigmap()->add(m->endTick().ticks(), SigEvent(m->timesig()));
                              }
                        else {
                              m->setTicks(m->timesig());
                              score->sigmap()->add(m->tick().ticks(), SigEvent(m->timesig()));
                              }
                        }
                  }
            else if (tag == "KeySig") {
                  KeySig* ks = new KeySig(score);
                  ks->setTrack(e.track());
                  ks->read(e);
                  Fraction curTick = e.tick();
                  if (!ks->isCustom() && !ks->isAtonal() && ks->key() == Key::C && curTick.isZero()) {
                        // ignore empty key signature
                        qDebug("remove keysig c at tick 0");
                        if (ks->links()) {
                              if (ks->links()->size() == 1)
                                    e.linkIds().remove(ks->links()->lid());
                              }
                        delete ks;
                        }
                  else {
                        // if key sig not at beginning of measure => courtesy key sig
                        bool courtesySig = (curTick == m->endTick());
                        segment = m->getSegment(courtesySig ? SegmentType::KeySigAnnounce : SegmentType::KeySig, curTick);
                        segment->add(ks);
                        if (!courtesySig)
                              staff->setKey(curTick, ks->keySigEvent());
                        }
                  }
            else if (tag == "Text" || tag == "StaffText") {
                  // MuseScore 3 has different types for system text and
                  // staff text while MuseScore 2 didn't.
                  // We need to decide first which one we should create.
                  QString styleName;
                  if (e.readAheadAvailable()) {
                        e.performReadAhead([&styleName, tag](QIODevice& dev) {
                              const QString closeTag = QString("</").append(tag).append(">");
                              QByteArray arrLine = dev.readLine();
                              while (!arrLine.isEmpty()) {
                                    QString line(arrLine);
                                    if (line.contains("<style>")) {
                                          QRegExp re("<style>([A-z0-9]+)</style>");
                                          if (re.indexIn(line) > -1)
                                                styleName = re.cap(1);
                                          return;
                                          }
                                    if (line.contains(closeTag))
                                          return;
                                    arrLine = dev.readLine();
                                    }
                              });
                        }
                  StaffTextBase* t;
                  if (styleName == "System"   || styleName == "Tempo"
                     || styleName == "Marker" || styleName == "Jump"
                     || styleName == "Volta") // TODO: is it possible to get it from style?
                        t = new SystemText(score);
                  else
                        t = new StaffText(score);
                  t->setTrack(e.track());
                  readText206(e, t, t);
                  if (t->empty()) {
                        if (t->links()) {
                              if (t->links()->size() == 1) {
                                    qDebug("reading empty text: deleted lid = %d", t->links()->lid());
                                    e.linkIds().remove(t->links()->lid());
                                    delete t;
                                    }
                              }
                        }
                  else {
#if 0
                        // This code was added at commit ed5b615
                        // but it seems to no longer be appropriate.
                        // autoplace is usually true,
                        // exception is text within staff,
                        // and in this case offset is already correct without further adjustment.
                        if (!t->autoplace()) {
                              // adjust position
                              qreal userY = t->offset().y() / t->spatium();
                              qreal yo = -(-2.0 - userY) * t->spatium();
                              t->layout();
                              t->setAlign(Align::LEFT | Align::TOP);
                              t->ryoffset() = yo;
                              }
#endif
                        segment = m->getSegment(SegmentType::ChordRest, e.tick());
                        segment->add(t);
                        }
                  }

            //----------------------------------------------------
            // Annotation

            else if (tag == "Dynamic") {
                  Dynamic* dyn = new Dynamic(score);
                  dyn->setTrack(e.track());
                  readDynamic(dyn, e);
                  segment = m->getSegment(SegmentType::ChordRest, e.tick());
                  segment->add(dyn);
                  }
            else if (tag == "RehearsalMark") {
                  RehearsalMark* el = new RehearsalMark(score);
                  el->setTrack(e.track());
                  readText206(e, el, el);
//                  el->setOffset(el->offset() - el->score()->styleValue(Pid::OFFSET, Sid::rehearsalMarkPosAbove).toPointF());
//                  if (el->offset().isNull())
//                        el->setAutoplace(true);
                  segment = m->getSegment(SegmentType::ChordRest, e.tick());
                  segment->add(el);
                  }
#if 0
            else if (tag == "StaffText") {
                  StaffText* el = new StaffText(score);
                  el->setTrack(e.track());

                  while (e.readNextStartElement()) {
                        const QStringRef& tag(e.name());
                        if (tag == "foregroundColor")
                              e.skipCurrentElement();
                        else if (!el->readProperties(e))
                              e.unknown();
                        }
                  TextBase* tt = static_cast<TextBase*>(el);
                  tt->setXmlText(tt->xmlText().replace("<sym>unicode", "<sym>met"));
                  segment = m->getSegment(SegmentType::ChordRest, e.tick());
                  segment->add(el);
                  }
#endif
            else if (tag == "Harmony"
               || tag == "FretDiagram"
               || tag == "TremoloBar"
               || tag == "Symbol"
               || tag == "InstrumentChange"
               || tag == "StaffState"
               || tag == "FiguredBass"
               ) {
                  Element* el = Element::name2Element(tag, score);
                  // hack - needed because tick tags are unreliable in 1.3 scores
                  // for symbols attached to anything but a measure
                  el->setTrack(e.track());
                  el->read(e);
                  if (el->staff() && (el->isHarmony() || el->isFretDiagram() || el->isInstrumentChange()))
                        adjustPlacement(el);
                  segment = m->getSegment(SegmentType::ChordRest, e.tick());
                  segment->add(el);
                  }
            else if (tag == "Tempo") {
                  TempoText* tt = new TempoText(score);
                  // hack - needed because tick tags are unreliable in 1.3 scores
                  // for symbols attached to anything but a measure
                  tt->setTrack(e.track());
                  readTempoText(tt, e);
                  segment = m->getSegment(SegmentType::ChordRest, e.tick());
                  segment->add(tt);
                  }
            else if (tag == "Marker" || tag == "Jump") {
                  Element* el = Element::name2Element(tag, score);
                  el->setTrack(e.track());
                  if (tag == "Marker") {
                        Marker* ma = toMarker(el);
                        readMarker(ma, e);
                        Element* markerEl = toElement(ma);
                        m->add(markerEl);
                        }
                  else {
                        el->read(e);
                        m->add(el);
                        }
                  }
            else if (tag == "Image") {
                  if (MScore::noImages)
                        e.skipCurrentElement();
                  else {
                        Element* el = Element::name2Element(tag, score);
                        el->setTrack(e.track());
                        el->read(e);
                        segment = m->getSegment(SegmentType::ChordRest, e.tick());
                        segment->add(el);
                        }
                  }
            //----------------------------------------------------
            else if (tag == "stretch") {
                  double val = e.readDouble();
                  if (val < 0.0)
                        val = 0;
                  m->setUserStretch(val);
                  }
            else if (tag == "noOffset")
                  m->setNoOffset(e.readInt());
            else if (tag == "measureNumberMode")
                  m->setMeasureNumberMode(MeasureNumberMode(e.readInt()));
            else if (tag == "irregular")
                  m->setIrregular(e.readBool());
            else if (tag == "breakMultiMeasureRest")
                  m->setBreakMultiMeasureRest(e.readBool());
            else if (tag == "sysInitBarLineType") {
                  const QString& val(e.readElementText());
                  BarLine* barLine = new BarLine(score);
                  barLine->setTrack(e.track());
                  barLine->setBarLineType(val);
                  segment = m->getSegment(SegmentType::BeginBarLine, m->tick());
                  segment->add(barLine);
                  }
            else if (tag == "Tuplet") {
                  Tuplet* tuplet = new Tuplet(score);
                  tuplet->setTrack(e.track());
                  tuplet->setTick(e.tick());
                  tuplet->setParent(m);
                  readTuplet(tuplet, e);
                  e.addTuplet(tuplet);
                  }
            else if (tag == "startRepeat") {
                  m->setRepeatStart(true);
                  e.readNext();
                  }
            else if (tag == "endRepeat") {
                  m->setRepeatCount(e.readInt());
                  m->setRepeatEnd(true);
                  }
            else if (tag == "vspacer" || tag == "vspacerDown") {
                  if (!m->vspacerDown(staffIdx)) {
                        Spacer* spacer = new Spacer(score);
                        spacer->setSpacerType(SpacerType::DOWN);
                        spacer->setTrack(staffIdx * VOICES);
                        m->add(spacer);
                        }
                  m->vspacerDown(staffIdx)->setGap(e.readDouble() * _spatium);
                  }
            else if (tag == "vspacer" || tag == "vspacerUp") {
                  if (!m->vspacerUp(staffIdx)) {
                        Spacer* spacer = new Spacer(score);
                        spacer->setSpacerType(SpacerType::UP);
                        spacer->setTrack(staffIdx * VOICES);
                        m->add(spacer);
                        }
                  m->vspacerUp(staffIdx)->setGap(e.readDouble() * _spatium);
                  }
            else if (tag == "visible")
                  m->setStaffVisible(staffIdx, e.readInt());
            else if (tag == "slashStyle")
                  m->setStaffSlashStyle(staffIdx, e.readInt());
            else if (tag == "Beam") {
                  Beam* beam = new Beam(score);
                  beam->setTrack(e.track());
                  beam->read(e);
                  beam->setParent(0);
                  e.addBeam(beam);
                  }
            else if (tag == "Segment")
                  segment->read(e);
            else if (tag == "MeasureNumber") {
                  MeasureNumber* noText = new MeasureNumber(score);
                  readText206(e, noText, m);
                  noText->setTrack(e.track());
                  noText->setParent(m);
                  m->setNoText(noText->staffIdx(), noText);
                  }
            else if (tag == "SystemDivider") {
                  SystemDivider* sd = new SystemDivider(score);
                  sd->read(e);
                  m->add(sd);
                  }
            else if (tag == "Ambitus") {
                  Ambitus* range = new Ambitus(score);
                  readAmbitus(range, e);
                  segment = m->getSegment(SegmentType::Ambitus, e.tick());
                  range->setParent(segment);          // a parent segment is needed for setTrack() to work
                  range->setTrack(trackZeroVoice(e.track()));
                  segment->add(range);
                  }
            else if (tag == "multiMeasureRest") {
                  m->setMMRestCount(e.readInt());
                  // set tick to previous measure
                  m->setTick(e.lastMeasure()->tick());
                  e.setTick(e.lastMeasure()->tick());
                  }
            else if (m->MeasureBase::readProperties(e))
                  ;
            else
                  e.unknown();
            }
      e.checkTuplets();
      m->connectTremolo();
      }

//---------------------------------------------------------
//   readBox
//---------------------------------------------------------

static void readBox(Box* b, XmlReader& e)
      {
      b->setLeftMargin(0.0);
      b->setRightMargin(0.0);
      b->setTopMargin(0.0);
      b->setBottomMargin(0.0);
      b->setTopGap(0.0);
      b->setBottomGap(0.0);
      b->setPropertyFlags(Pid::TOP_GAP, PropertyFlags::UNSTYLED);
      b->setPropertyFlags(Pid::BOTTOM_GAP, PropertyFlags::UNSTYLED);

      b->setBoxHeight(Spatium(0));     // override default set in constructor
      b->setBoxWidth(Spatium(0));
      bool keepMargins = false;        // whether original margins have to be kept when reading old file

      while (e.readNextStartElement()) {
            const QStringRef& tag(e.name());
            if (tag == "HBox") {
                  HBox* hb = new HBox(b->score());
                  hb->read(e);
                  b->add(hb);
                  keepMargins = true;     // in old file, box nesting used outer box margins
                  }
            else if (tag == "VBox") {
                  VBox* vb = new VBox(b->score());
                  vb->read(e);
                  b->add(vb);
                  keepMargins = true;     // in old file, box nesting used outer box margins
                  }
            else if (tag == "Text") {
                  Text* t;
                  if (b->isTBox()) {
                        t = toTBox(b)->text();
                        readText206(e, t, t);
                        }
                  else {
                        t = new Text(b->score());
                        readText206(e, t, t);
                        if (t->empty()) {
                              qDebug("read empty text");
                              }
                        else
                              b->add(t);
                        }
                  }
            else if (!b->readProperties(e))
                  e.unknown();
            }

      // with .msc versions prior to 1.17, box margins were only used when nesting another box inside this box:
      // for backward compatibility set them to 0 in all other cases

      if (b->score()->mscVersion() <= 114 && (b->isHBox() || b->isVBox()) && !keepMargins)  {
            b->setLeftMargin(0.0);
            b->setRightMargin(0.0);
            b->setTopMargin(0.0);
            b->setBottomMargin(0.0);
            }
      }

//---------------------------------------------------------
//   readStaffContent
//---------------------------------------------------------

static void readStaffContent(Score* score, XmlReader& e)
      {
      int staff = e.intAttribute("id", 1) - 1;
      e.setTick(Fraction(0,1));
      e.setTrack(staff * VOICES);
      Box* lastReadBox = nullptr;
      bool readMeasureLast = false;

      if (staff == 0) {
            while (e.readNextStartElement()) {
                  const QStringRef& tag(e.name());

                  if (tag == "Measure") {
                        if (lastReadBox) {
                              lastReadBox->setBottomGap(lastReadBox->bottomGap() + lastReadBox->propertyDefault(Pid::BOTTOM_GAP).toReal());
                              lastReadBox = nullptr;
                              }
                        readMeasureLast = true;

                        Measure* measure = 0;
                        measure = new Measure(score);
                        measure->setTick(e.tick());
                        //
                        // inherit timesig from previous measure
                        //
                        Measure* m = e.lastMeasure(); // measure->prevMeasure();
                        Fraction f(m ? m->timesig() : Fraction(4,4));
                        measure->setTicks(f);
                        measure->setTimesig(f);

                        readMeasure(measure, staff, e);
                        measure->checkMeasure(staff);
                        if (!measure->isMMRest()) {
                              score->measures()->add(measure);
                              e.setLastMeasure(measure);
                              e.setTick(measure->endTick());
                              }
                        else {
                              // this is a multi measure rest
                              // always preceded by the first measure it replaces
                              Measure* lm = e.lastMeasure();

                              if (lm) {
                                    lm->setMMRest(measure);
                                    measure->setTick(lm->tick());
                                    }
                              }
                        }
                  else if (tag == "HBox" || tag == "VBox" || tag == "TBox" || tag == "FBox") {
                        Box* b = toBox(Element::name2Element(tag, score));
                        readBox(b, e);
                        b->setTick(e.tick());
                        score->measures()->add(b);

                        // If it's the first box, and comes before any measures, reset to
                        // 301 default.
                        if (!readMeasureLast && !lastReadBox) {
                              b->setTopGap(b->propertyDefault(Pid::TOP_GAP).toReal());
                              b->setPropertyFlags(Pid::TOP_GAP, PropertyFlags::STYLED);
                              }
                        else if (readMeasureLast)
                              b->setTopGap(b->topGap() + b->propertyDefault(Pid::TOP_GAP).toReal());

                        lastReadBox = b;
                        readMeasureLast = false;
                        }
                  else if (tag == "tick")
                        e.setTick(Fraction::fromTicks(score->fileDivision(e.readInt())));
                  else
                        e.unknown();
                  }
            }
      else {
            Measure* measure = score->firstMeasure();
            while (e.readNextStartElement()) {
                  const QStringRef& tag(e.name());

                  if (tag == "Measure") {
                        if (measure == 0) {
                              qDebug("Score::readStaff(): missing measure!");
                              measure = new Measure(score);
                              measure->setTick(e.tick());
                              score->measures()->add(measure);
                              }
                        e.setTick(measure->tick());
                        readMeasure(measure, staff, e);
                        measure->checkMeasure(staff);
                        if (measure->isMMRest())
                              measure = e.lastMeasure()->nextMeasure();
                        else {
                              e.setLastMeasure(measure);
                              if (measure->mmRest())
                                    measure = measure->mmRest();
                              else
                                    measure = measure->nextMeasure();
                              }
                        }
                  else if (tag == "tick")
                        e.setTick(Fraction::fromTicks(score->fileDivision(e.readInt())));
                  else
                        e.unknown();
                  }
            }
      }

//---------------------------------------------------------
//   readStyle
//---------------------------------------------------------

static void readStyle(MStyle* style, XmlReader& e)
      {
      QString oldChordDescriptionFile = style->value(Sid::chordDescriptionFile).toString();
      bool chordListTag = false;
      excessTextStyles206.clear();
      while (e.readNextStartElement()) {
            QString tag = e.name().toString();
            if (tag == "TextStyle")
                  readTextStyle206(style, e, excessTextStyles206);
            else if (tag == "Spatium")
                  style->set(Sid::spatium, e.readDouble() * DPMM);
            else if (tag == "page-layout")
                  readPageFormat(style, e);
            else if (tag == "displayInConcertPitch")
                  style->set(Sid::concertPitch, QVariant(bool(e.readInt())));
            else if (tag == "pedalY") {
                  qreal y = e.readDouble();
                  style->set(Sid::pedalPosBelow, QPointF(0.0, y));
                  }
            else if (tag == "lyricsDistance") {
                  qreal y = e.readDouble();
                  style->set(Sid::lyricsPosBelow, QPointF(0.0, y));
                  }
            else if (tag == "lyricsMinBottomDistance") {
                  // no longer meaningful since it is now measured from skyline rather than staff
                  //style->set(Sid::lyricsMinBottomDistance, QPointF(0.0, y));
                  e.skipCurrentElement();
                  }
            else if (tag == "ottavaHook") {
                  qreal y = qAbs(e.readDouble());
                  style->set(Sid::ottavaHookAbove, y);
                  style->set(Sid::ottavaHookBelow, -y);
                  }
            else if (tag == "endBarDistance") {
                  double d = e.readDouble();
                  d += style->value(Sid::barWidth).toDouble();
                  d += style->value(Sid::endBarWidth).toDouble();
                  style->set(Sid::endBarDistance, QVariant(d));
                  }
            else if (tag == "ChordList") {
                  style->chordList()->clear();
                  style->chordList()->read(e);
                  style->setCustomChordList(true);
                  for (ChordFont f : style->chordList()->fonts) {
                        if (f.family == "MuseJazz") {
                              f.family = "MuseJazz Text";
                              }
                        }
                  chordListTag = true;
                  }
            else if (tag == "harmonyY") {
                  qreal val = -e.readDouble();
                  if (val > 0.0) {
                        style->set(Sid::harmonyPlacement, int(Placement::BELOW));
                        style->set(Sid::chordSymbolAPosBelow,  QPointF(.0, val));
                        }
                  else {
                        style->set(Sid::harmonyPlacement, int(Placement::ABOVE));
                        style->set(Sid::chordSymbolAPosBelow,  QPointF(.0, val));
                        }
                  }
            else {
                  if (!style->readProperties(e)) {
                        e.skipCurrentElement();
                        }
                  }
            }

      // if we just specified a new chord description file
      // and didn't encounter a ChordList tag
      // then load the chord description file

      QString newChordDescriptionFile = style->value(Sid::chordDescriptionFile).toString();
      if (newChordDescriptionFile != oldChordDescriptionFile && !chordListTag) {
            if (!newChordDescriptionFile.startsWith("chords_") && style->value(Sid::chordStyle).toString() == "std") {
                  // should not normally happen,
                  // but treat as "old" (114) score just in case
                  style->set(Sid::chordStyle, QVariant(QString("custom")));
                  style->set(Sid::chordsXmlFile, QVariant(true));
                  qDebug("StyleData::load: custom chord description file %s with chordStyle == std", qPrintable(newChordDescriptionFile));
                  }
            if (style->value(Sid::chordStyle).toString() == "custom")
                  style->setCustomChordList(true);
            else
                  style->setCustomChordList(false);
            style->chordList()->unload();
            }

      // make sure we have a chordlist
      if (!chordListTag)
            style->checkChordList();
      }

//---------------------------------------------------------
//   readScore
//---------------------------------------------------------

static bool readScore(Score* score, XmlReader& e)
      {
      while (e.readNextStartElement()) {
            e.setTrack(-1);
            const QStringRef& tag(e.name());
            if (tag == "Staff")
                  readStaffContent(score, e);
            else if (tag == "siglist")
                  score->sigmap()->read(e, score->fileDivision());
            else if (tag == "Omr") {
#ifdef OMR
                  score->masterScore()->setOmr(new Omr(score));
                  score->masterScore()->omr()->read(e);
#else
                  e.skipCurrentElement();
#endif
                  }
            else if (tag == "Audio") {
                  score->setAudio(new Audio);
                  score->audio()->read(e);
                  }
            else if (tag == "showOmr")
                  score->masterScore()->setShowOmr(e.readInt());
            else if (tag == "playMode")
                  score->setPlayMode(PlayMode(e.readInt()));
            else if (tag == "LayerTag") {
                  int id = e.intAttribute("id");
                  const QString& t = e.attribute("tag");
                  QString val(e.readElementText());
                  if (id >= 0 && id < 32) {
                        score->layerTags()[id] = t;
                        score->layerTagComments()[id] = val;
                        }
                  }
            else if (tag == "Layer") {
                  Layer layer;
                  layer.name = e.attribute("name");
                  layer.tags = e.attribute("mask").toUInt();
                  score->layer().append(layer);
                  e.readNext();
                  }
            else if (tag == "currentLayer")
                  score->setCurrentLayer(e.readInt());
            else if (tag == "Synthesizer")
                  score->synthesizerState().read(e);
            else if (tag == "page-offset")
                  score->setPageNumberOffset(e.readInt());
            else if (tag == "Division")
                  score->setFileDivision(e.readInt());
            else if (tag == "showInvisible")
                  score->setShowInvisible(e.readInt());
            else if (tag == "showUnprintable")
                  score->setShowUnprintable(e.readInt());
            else if (tag == "showFrames")
                  score->setShowFrames(e.readInt());
            else if (tag == "showMargins")
                  score->setShowPageborders(e.readInt());
            else if (tag == "Style") {
                  qreal sp = score->style().value(Sid::spatium).toDouble();
                  readStyle(&score->style(), e);
                  if (score->style().value(Sid::MusicalTextFont).toString() == "MuseJazz")
                        score->style().set(Sid::MusicalTextFont, "MuseJazz Text");
                  // if (_layoutMode == LayoutMode::FLOAT || _layoutMode == LayoutMode::SYSTEM) {
                  if (score->layoutMode() == LayoutMode::FLOAT) {
                        // style should not change spatium in
                        // float mode
                        score->style().set(Sid::spatium, sp);
                        }
                  score->setScoreFont(ScoreFont::fontFactory(score->style().value(Sid::MusicalSymbolFont).toString()));
                  }
            else if (tag == "copyright" || tag == "rights") {
                  Text* text = new Text(score);
                  readText206(e, text, text);
                  score->setMetaTag("copyright", text->xmlText());
                  delete text;
                  }
            else if (tag == "movement-number")
                  score->setMetaTag("movementNumber", e.readElementText());
            else if (tag == "movement-title")
                  score->setMetaTag("movementTitle", e.readElementText());
            else if (tag == "work-number")
                  score->setMetaTag("workNumber", e.readElementText());
            else if (tag == "work-title")
                  score->setMetaTag("workTitle", e.readElementText());
            else if (tag == "source")
                  score->setMetaTag("source", e.readElementText());
            else if (tag == "metaTag") {
                  QString name = e.attribute("name");
                  score->setMetaTag(name, e.readElementText());
                  }
            else if (tag == "Part") {
                  Part* part = new Part(score);
                  readPart206(part, e);
                  score->parts().push_back(part);
                  }
            else if ((tag == "HairPin")   // TODO: do this elements exist here?
                || (tag == "Ottava")
                || (tag == "TextLine")
                || (tag == "Volta")
                || (tag == "Trill")
                || (tag == "Slur")
                || (tag == "Pedal")) {
                  Spanner* s = toSpanner(Element::name2Element(tag, score));
                  if (tag == "HairPin")
                        readHairpin206(e, toHairpin(s));
                  else if (tag == "Ottava")
                        readOttava(e, toOttava(s));
                  else if (tag == "TextLine")
                        readTextLine206(e, toTextLine(s));
                  else if (tag == "Volta")
                        readVolta206(e, toVolta(s));
                  else if (tag == "Trill")
                        readTrill206(e, toTrill(s));
                  else if (tag == "Slur")
                        readSlur206(e, toSlur(s));
                  else {
                        Q_ASSERT(tag == "Pedal");
                        readPedal(e, toPedal(s));
                        }
                  score->addSpanner(s);
                  }
            else if (tag == "Excerpt") {
                  if (MScore::noExcerpts)
                        e.skipCurrentElement();
                  else {
                        if (score->isMaster()) {
                              Excerpt* ex = new Excerpt(static_cast<MasterScore*>(score));
                              ex->read(e);
                              score->excerpts().append(ex);
                              }
                        else {
                              qDebug("read206: readScore(): part cannot have parts");
                              e.skipCurrentElement();
                              }
                        }
                  }
            else if (tag == "Score") {          // recursion
                  if (MScore::noExcerpts)
                        e.skipCurrentElement();
                  else {
                        e.tracks().clear();
                        MasterScore* m = score->masterScore();
                        Score* s = new Score(m, MScore::baseStyle());
                        Excerpt* ex = new Excerpt(m);

                        ex->setPartScore(s);
                        e.setLastMeasure(nullptr);
                        readScore(s, e);
                        ex->setTracks(e.tracks());
                        m->addExcerpt(ex);
                        }
                  }
            else if (tag == "PageList")
                  e.skipCurrentElement();
            else if (tag == "name") {
                  QString n = e.readElementText();
                  if (!score->isMaster())             //ignore the name if it's not a child score
                        score->excerpt()->setTitle(n);
                  }
            else if (tag == "layoutMode") {
                  QString s = e.readElementText();
                  if (s == "line")
                        score->setLayoutMode(LayoutMode::LINE);
                  else if (s == "system")
                        score->setLayoutMode(LayoutMode::SYSTEM);
                  else
                        qDebug("layoutMode: %s", qPrintable(s));
                  }
            else
                  e.unknown();
            }
      if (e.error() != QXmlStreamReader::NoError) {
            qDebug("%s: xml read error at line %lld col %lld: %s",
               qPrintable(e.getDocName()), e.lineNumber(), e.columnNumber(),
               e.name().toUtf8().data());
            MScore::lastError = QObject::tr("XML read error at line %1, column %2: %3").arg(e.lineNumber()).arg(e.columnNumber()).arg(e.name().toString());
            return false;
            }

      score->connectTies();

      score->setFileDivision(MScore::division);

      //
      //    sanity check for barLineSpan
      //
#if 0 // TODO:barline
      for (Staff* st : score->staves()) {
            int barLineSpan = st->barLineSpan();
            int idx = st->idx();
            int n = score->nstaves();
            if (idx + barLineSpan > n) {
                  qDebug("bad span: idx %d  span %d staves %d", idx, barLineSpan, n);
                  // span until last staff
                  barLineSpan = n - idx;
                  st->setBarLineSpan(barLineSpan);
                  }
            else if (idx == 0 && barLineSpan == 0) {
                  qDebug("bad span: idx %d  span %d staves %d", idx, barLineSpan, n);
                  // span from the first staff until the start of the next span
                  barLineSpan = 1;
                  for (int i = 1; i < n; ++i) {
                        if (score->staff(i)->barLineSpan() == 0)
                              ++barLineSpan;
                        else
                              break;
                        }
                  st->setBarLineSpan(barLineSpan);
                  }
            // check spanFrom
            int minBarLineFrom = st->lines(0) == 1 ? BARLINE_SPAN_1LINESTAFF_FROM : MIN_BARLINE_SPAN_FROMTO;
            if (st->barLineFrom() < minBarLineFrom)
                  st->setBarLineFrom(minBarLineFrom);
            if (st->barLineFrom() > st->lines(0) * 2)
                  st->setBarLineFrom(st->lines(0) * 2);
            // check spanTo
            Staff* stTo = st->barLineSpan() <= 1 ? st : score->staff(idx + st->barLineSpan() - 1);
            // 1-line staves have special bar line spans
            int maxBarLineTo        = stTo->lines(0) == 1 ? BARLINE_SPAN_1LINESTAFF_TO : stTo->lines(0)*2;
            int defaultBarLineTo    = stTo->lines(0) == 1 ? BARLINE_SPAN_1LINESTAFF_TO : (stTo->lines(0) - 1) * 2;
            if (st->barLineTo() == UNKNOWN_BARLINE_TO)
                  st->setBarLineTo(defaultBarLineTo);
            if (st->barLineTo() < MIN_BARLINE_SPAN_FROMTO)
                  st->setBarLineTo(MIN_BARLINE_SPAN_FROMTO);
            if (st->barLineTo() > maxBarLineTo)
                  st->setBarLineTo(maxBarLineTo);
            // on single staff span, check spanFrom and spanTo are distant enough
            if (st->barLineSpan() == 1) {
                  if (st->barLineTo() - st->barLineFrom() < MIN_BARLINE_FROMTO_DIST) {
                        st->setBarLineFrom(0);
                        st->setBarLineTo(defaultBarLineTo);
                        }
                  }
            }
#endif
      score->fixTicks();
      if (score->isMaster()) {
            MasterScore* ms = static_cast<MasterScore*>(score);
            if (!ms->omr())
                  ms->setShowOmr(false);
            ms->rebuildMidiMapping();
            ms->updateChannel();
 //           ms->createPlayEvents();
            }
      return true;
      }

//---------------------------------------------------------
//   read
//  <page-layout>
//      <page-height>
//      <page-width>
//      <landscape>1</landscape>
//      <page-margins type="both">
//         <left-margin>28.3465</left-margin>
//         <right-margin>28.3465</right-margin>
//         <top-margin>28.3465</top-margin>
//         <bottom-margin>56.6929</bottom-margin>
//         </page-margins>
//      </page-layout>
//---------------------------------------------------------

void PageFormat::read(XmlReader& e)
      {
      qreal _oddRightMargin  = 0.0;
      qreal _evenRightMargin = 0.0;
      QString type;

      while (e.readNextStartElement()) {
            const QStringRef& tag(e.name());
            if (tag == "page-margins") {
                  type = e.attribute("type","both");
                  qreal lm = 0.0, rm = 0.0, tm = 0.0, bm = 0.0;
                  while (e.readNextStartElement()) {
                        const QStringRef& t(e.name());
                        qreal val = e.readDouble() * 0.5 / PPI;
                        if (t == "left-margin")
                              lm = val;
                        else if (t == "right-margin")
                              rm = val;
                        else if (t == "top-margin")
                              tm = val;
                        else if (t == "bottom-margin")
                              bm = val;
                        else
                              e.unknown();
                        }
                  _twosided = type == "odd" || type == "even";
                  if (type == "odd" || type == "both") {
                        _oddLeftMargin   = lm;
                        _oddRightMargin  = rm;
                        _oddTopMargin    = tm;
                        _oddBottomMargin = bm;
                        }
                  if (type == "even" || type == "both") {
                        _evenLeftMargin   = lm;
                        _evenRightMargin  = rm;
                        _evenTopMargin    = tm;
                        _evenBottomMargin = bm;
                        }
                  }
            else if (tag == "page-height")
                  _size.rheight() = e.readDouble() * 0.5 / PPI;
            else if (tag == "page-width")
                  _size.rwidth() = e.readDouble() * .5 / PPI;
            else
                  e.unknown();
            }
      qreal w1        = _size.width() - _oddLeftMargin - _oddRightMargin;
      qreal w2        = _size.width() - _evenLeftMargin - _evenRightMargin;
      _printableWidth = qMin(w1, w2);     // silently adjust right margins
      }

//---------------------------------------------------------
//   read206
//    import old version > 1.3  and < 3.x files
//---------------------------------------------------------

Score::FileError MasterScore::read206(XmlReader& e)
      {
      for (unsigned int i = 0; i < sizeof(style206)/sizeof(*style206); ++i)
            style().set(style206[i].idx, style206[i].val);

      while (e.readNextStartElement()) {
            const QStringRef& tag(e.name());
            if (tag == "programVersion") {
                  setMscoreVersion(e.readElementText());
                  parseVersion(mscoreVersion());
                  }
            else if (tag == "programRevision")
                  setMscoreRevision(e.readIntHex());
            else if (tag == "Score") {
                  if (!readScore(this, e))
                        return FileError::FILE_BAD_FORMAT;
                  }
            else if (tag == "Revision") {
                  Revision* revision = new Revision;
                  revision->read(e);
                  revisions()->add(revision);
                  }
            }
      int id = 1;
      for (LinkedElements* le : e.linkIds())
            le->setLid(this, id++);

      for (Staff* s : staves())
            s->updateOttava();

      // fix segment span
      SegmentType st = SegmentType::BarLineType;
      for (Segment* s = firstSegment(st); s; s = s->next1(st)) {
            for (int staffIdx = 0; staffIdx < nstaves(); ++staffIdx) {
                  BarLine* b = toBarLine(s->element(staffIdx * VOICES));
                  if (!b)
                        continue;
                  int sp = b->spanStaff();
                  if (sp <= 0)
                        continue;
                  for (int span = 1; span <= sp; ++span) {
                        BarLine* nb = toBarLine(s->element((staffIdx + span) * VOICES));
                        if (!nb) {
                              nb = b->clone();
                              nb->setTrack((staffIdx + span) * VOICES);
                              s->add(nb);
                              }
                        nb->setSpanStaff(sp - span);
                        }
                  staffIdx += sp;
                  }
            }
      for (int staffIdx = 0; staffIdx < nstaves(); ++staffIdx) {
            Staff* s = staff(staffIdx);
            int sp = s->barLineSpan();
            if (sp <= 0)
                  continue;
            for (int span = 1; span <= sp; ++span) {
                  Staff* ns = staff(staffIdx + span);
                  ns->setBarLineSpan(sp - span);
                  }
            staffIdx += sp;
            }

      // fix positions
      //    offset = saved offset - layout position
      doLayout();
      for (auto i : e.fixOffsets()) {
            i.first->setOffset(i.second - i.first->pos());
            }

      // treat reading a 2.06 file as import
      // on save warn if old file will be overwritten
      setCreated(true);
      // don't autosave (as long as there's no change to the score)
      setAutosaveDirty(false);

      return FileError::FILE_NO_ERROR;
      }

}