-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSmartleggioExport.qml
More file actions
1212 lines (1003 loc) · 43.4 KB
/
SmartleggioExport.qml
File metadata and controls
1212 lines (1003 loc) · 43.4 KB
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
/*
* Copyright © 2024 Dmitri Ovodok
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 2 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <https://www.gnu.org/licenses/>.
*/
import QtQuick 2.9
import QtQuick.Controls 1.4
import QtQuick.Dialogs 1.2
import QtQuick.Layouts 1.3
import Qt.labs.settings 1.0
import MuseScore 3.0
import FileIO 3.0
MuseScore {
id: plugin
menuPath: "Plugins.Smartleggio Export"
version: "0.9.0"
description: "Export the current score to Smartleggio format"
requiresScore: true
readonly property string name: "Smartleggio Export"
readonly property string internalName: "SmartleggioExport"
readonly property string exportedFileSuffix: ".smlgz"
Settings {
id: pluginSettings
category: "plugins/smartleggio/export"
property string selectedLayoutMode: ""
property string selectedAspectRatio: ""
property string customAspectRatioX: ""
property string customAspectRatioY: ""
property string selectedLayoutBreakMode: ""
}
FileIO {
id: fileIO
onError: {
throw new Error("File operation error: " + msg);
}
}
QProcess {
id: qprocess
function isWindows() {
var os = Qt.platform.os;
return os == "windows" || os == "winrt";
}
function checkFileExists(path) {
var oldPath = fileIO.source;
fileIO.source = path;
var exists = fileIO.exists();
fileIO.source = oldPath;
return exists;
}
function executeCommandTemplate(cmd, args) {
for (var key in args) {
cmd = cmd.replace(new RegExp("\\{" + key + "\\}", "g"), args[key]);
}
console.log("Execute:", cmd);
qprocess.start(cmd);
var cmdOk = qprocess.waitForFinished();
// TODO: how to check the exit code? waitForFinished doesn't seem to do it.
if (!cmdOk) {
throw new Error("Failed to execute external process:\n" + cmd + "\n" + readAllStandardOutput().toString());
}
}
function makeGzipFile(srcPath, dstPath) {
var cmd;
if (isWindows()) {
srcPath = srcPath.replace(/\//g, "\\");
dstPath = dstPath.replace(/\//g, "\\");
cmd = 'powershell -Command "$fIn = New-Object System.IO.FileStream("""{srcPath}""", [System.IO.FileMode]::Open); $fOut = New-Object System.IO.FileStream("""{dstPath}""", [System.IO.FileMode]::Create); $gzStream = New-Object System.IO.Compression.GZipStream($fOut, [System.IO.Compression.CompressionMode]::Compress); $buffer = New-Object byte[](4096); do { $readCount = $fIn.Read($buffer, 0, 4096); $gzStream.Write($buffer, 0, $readCount); } while($readCount -ne 0); $fIn.Close(); $fIn.Dispose(); $gzStream.Close(); $gzStream.Dispose(); $fOut.Close(); $fOut.Dispose();"'
} else {
srcPath = srcPath.replace(/'/g, "'\\''");
dstPath = dstPath.replace(/'/g, "'\\''");
cmd = 'bash -c "gzip -c \'{srcPath}\' > \'{dstPath}\'"'
}
executeCommandTemplate(cmd, {"srcPath": srcPath, "dstPath": dstPath});
if (!checkFileExists(dstPath)) {
throw new Error("Failed to compress file");
}
}
function moveFile(srcPath, dstPath) {
var cmd;
if (isWindows()) {
srcPath = srcPath.replace(/^\//, "").replace(/\//g, "\\");
dstPath = dstPath.replace(/^\//, "").replace(/\//g, "\\");
cmd = 'cmd /c MOVE /Y "{srcPath}" "{dstPath}"'
} else {
srcPath = srcPath.replace(/'/g, "'\\''");
dstPath = dstPath.replace(/'/g, "'\\''");
cmd = 'bash -c "mv -f \'{srcPath}\' \'{dstPath}\'"'
}
executeCommandTemplate(cmd, {"srcPath": srcPath, "dstPath": dstPath});
// Move command removes the source file if and only if the operation
// is successful, so we check existense of the source file.
if (checkFileExists(srcPath)) {
throw new Error("Failed to write a file at the chosen location.\nPlease choose a different location.");
}
}
}
FileDialog {
id: exportFileDialog
title: plugin.name
modality: Qt.ApplicationModal
selectExisting: false
selectFolder: false
selectMultiple: false
nameFilters: ["Exported scores (*" + exportedFileSuffix + ")"]
property var exportSettings: null
property var score: null
property var platformDialog: null
onAccepted: onDialogAccepted(fileUrl)
function onDialogAccepted(fileUrl) {
var filePath = fileUrl.toString().replace(/file:\/\//, "");
if (!filePath.endsWith(exportedFileSuffix)) {
if (exportSettings.layoutMode != "preserve") {
var aspectRatio = exportSettings.aspectRatio;
filePath += "." + aspectRatio.y + "x" + aspectRatio.x;
}
filePath += exportedFileSuffix;
}
plugin.exportScore(score, filePath, exportSettings);
}
function openSaveFileDialog() {
if (Qt.platform.os == "osx") {
// A usual FileDialog doesn't show from QML on MacOS, use a
// dialog with the DontUseNativeDialog flag to work around this.
// Qt.labs.platform is unavailable in the AppImage for Linux on
// MuseScore 3.6.2, so it has to be created dynamically.
platformDialog = Qt.createQmlObject('import Qt.labs.platform 1.0 as Platform; Platform.FileDialog { fileMode: Platform.FileDialog.SaveFile; onAccepted: exportFileDialog.onDialogAccepted(file); }', plugin, "dynamicPlatformFileDialog");
platformDialog.title = title;
platformDialog.defaultSuffix = exportedFileSuffix;
platformDialog.nameFilters = nameFilters;
platformDialog.options = (16) // DontUseNativeDialog. On Qt 6 the value is 8 and exposed to QML in a usual (non-Labs) FileDialog.
platformDialog.open()
} else {
open();
}
}
}
Dialog {
id: exportSettingsDialog
title: plugin.name
modality: Qt.ApplicationModal
standardButtons: StandardButton.Save | StandardButton.Cancel
Column {
spacing: 4
RowLayout {
ExclusiveGroup { id: layoutModeGroup }
RadioButton {
id: layoutModeRadioButtonAdapt
text: "Adapt to aspect ratio"
checked: true
exclusiveGroup: layoutModeGroup
property string settingsId: "adapt"
}
RadioButton {
id: layoutModeRadioButtonPreserve
text: "Preserve original layout"
exclusiveGroup: layoutModeGroup
property string settingsId: "preserve"
}
Layout.fillWidth: true
}
Label {
width: parent.width
text: layoutModeRadioButtonAdapt.checked
? "The score will automatically be adapted to the selected screen aspect ratio."
: "The score will be exported with its current layout without any automatic changes."
wrapMode: Text.Wrap
}
GridLayout {
anchors.left: parent.left
anchors.right: parent.right
visible: layoutModeRadioButtonAdapt.checked
columns: 2
columnSpacing: 8
rowSpacing: 4
Label {
height: aspectRatioComboBox.height
text: "Aspect ratio:"
verticalAlignment: Text.AlignVCenter
}
ComboBox {
id: aspectRatioComboBox
model: ListModel {
id: aspectRatioOptionsModel
ListElement { settingsId: "4x3"; text: "4:3 (iPad)"; y: 4; x: 3 }
ListElement { settingsId: "16x10"; text: "16:10 (Most Android tablets)"; y: 16; x: 10 }
ListElement { settingsId: "16x9"; text: "16:9 (Most desktop screens, some smartphones)"; y: 16; x: 9 }
ListElement { settingsId: "2x1"; text: "2:1 (Most smartphones)"; y: 2; x: 1 }
ListElement { settingsId: "custom"; text: "Custom"; y: 0; x: 0 }
}
Layout.fillWidth: true
}
Row {
visible: aspectRatioOptionsModel.get(aspectRatioComboBox.currentIndex).settingsId === "custom"
spacing: 4
Label {
height: customAspectRatioYTextField.height
text: "Y:"
verticalAlignment: Text.AlignVCenter
}
TextField {
id: customAspectRatioYTextField
placeholderText: "Y (e.g. 16)"
validator: IntValidator { bottom: 1 }
}
}
Row {
visible: aspectRatioOptionsModel.get(aspectRatioComboBox.currentIndex).settingsId === "custom"
spacing: 4
Label {
height: customAspectRatioXTextField.height
text: "X:"
verticalAlignment: Text.AlignVCenter
}
TextField {
id: customAspectRatioXTextField
placeholderText: "X (e.g. 9)"
validator: IntValidator { bottom: 1 }
}
}
Label {
height: layoutBreaksModeComboBox.height
text: "Line/Page breaks:"
verticalAlignment: Text.AlignVCenter
}
ComboBox {
id: layoutBreaksModeComboBox
currentIndex: 1
model: ListModel {
id: layoutBreaksModeOptionsModel
ListElement { settingsId: "remove"; text: "Don't export"; mode: "remove" }
ListElement { settingsId: "reflow"; text: "Export line breaks, reflow other layout"; mode: "reflow" }
ListElement { settingsId: "keep"; text: "Export the original line and page breaks structure (usually not recommended)"; mode: "keep" }
}
Layout.fillWidth: true
}
}
}
function findModelIndexBySettingsId(model, settingsId) {
var count = model.count;
for (var i = 0; i < count; ++i) {
if (model.get(i).settingsId == settingsId) {
return i;
}
}
return 0;
}
onVisibleChanged: {
if (visible) {
if (pluginSettings.selectedLayoutMode == layoutModeRadioButtonAdapt.settingsId) {
layoutModeRadioButtonAdapt.checked = true;
} else if (pluginSettings.selectedLayoutMode == layoutModeRadioButtonPreserve.settingsId) {
layoutModeRadioButtonPreserve.checked = true;
}
aspectRatioComboBox.currentIndex = findModelIndexBySettingsId(aspectRatioComboBox.model, pluginSettings.selectedAspectRatio);
customAspectRatioXTextField.text = pluginSettings.customAspectRatioX;
customAspectRatioYTextField.text = pluginSettings.customAspectRatioY;
layoutBreaksModeComboBox.currentIndex = findModelIndexBySettingsId(layoutBreaksModeComboBox.model, pluginSettings.selectedLayoutBreakMode);
}
}
onAccepted: {
var layoutMode = layoutModeGroup.current.settingsId;
var aspectRatio = aspectRatioOptionsModel.get(aspectRatioComboBox.currentIndex);
var layoutBreaksMode = layoutBreaksModeOptionsModel.get(layoutBreaksModeComboBox.currentIndex);
var settings = {
"layoutMode": layoutMode,
"aspectRatio": {
"x": aspectRatio.x ? aspectRatio.x : +customAspectRatioXTextField.text,
"y": aspectRatio.y ? aspectRatio.y : +customAspectRatioYTextField.text,
},
"layoutBreaksMode": layoutBreaksMode.mode,
};
if (settings.aspectRatio.x <= 0 || settings.aspectRatio.y <= 0) {
messageDialog.showError("Invalid aspect ratio", "Please enter valid aspect ratio.", function() { open(); });
} else {
// If called without callLater(), the dialog opens
// somewhere behind the main window.
Qt.callLater(function() { plugin.openSaveFileDialog(settings) });
}
// Save the selected export settings.
pluginSettings.selectedLayoutMode = layoutMode;
pluginSettings.selectedAspectRatio = aspectRatio.settingsId;
pluginSettings.customAspectRatioX = customAspectRatioXTextField.text;
pluginSettings.customAspectRatioY = customAspectRatioYTextField.text;
pluginSettings.selectedLayoutBreakMode = layoutBreaksMode.settingsId;
}
}
MessageDialog {
id: messageDialog
icon: StandardIcon.Warning
title: dialogTitle
modality: Qt.ApplicationModal
standardButtons: StandardButton.Ok
// For some reason the dialog's title setter doesn't work,
// so we update it via a property binding.
property string dialogTitle: plugin.name
property var onClosedCallback: null
function showError(title, message, callback) {
icon = StandardIcon.Critical;
dialogTitle = title;
text = message;
onClosedCallback = callback ? callback : null;
open();
}
function showInfo(title, message, callback) {
icon = StandardIcon.Information;
dialogTitle = title;
text = message;
onClosedCallback = callback ? callback : null;
open();
}
onAccepted: {
if (onClosedCallback) {
onClosedCallback();
}
}
}
function findPage(e) {
while (e && e.type != Element.PAGE)
e = e.parent;
return e;
}
function findSystem(e) {
while (e && e.type != Element.SYSTEM)
e = e.parent;
return e;
}
function formatTagWithAttributes(name, attributes, standalone /* = false */) {
var parts = ["<", name];
if (attributes) {
for (var attr in attributes) {
parts.push(" ");
parts.push(attr);
parts.push("=\"");
parts.push(attributes[attr]);
parts.push("\"");
}
}
if (standalone)
parts.push(" />");
else
parts.push(">");
return parts.join("");
}
function formatStartTag(name, attributes) {
return formatTagWithAttributes(name, attributes, false);
}
function formatStandaloneTag(name, attributes) {
return formatTagWithAttributes(name, attributes, true);
}
function formatEndTag(name) {
return "</" + name + ">";
}
function formatTag(name, value) {
var parts = ["<", name, ">", value, "</", name, ">"];
return parts.join("");
}
function formatCDataTag(name, data) {
// Escape possible CDATA entries in data.
data = data.replace(/]]>/g, "]]]]><![CDATA[>");
var tagDataParts = ["<![CDATA[", data, "]]>"];
return formatTag(name, tagDataParts.join(""));
}
function getMuseScoreVersionString() {
return mscoreMajorVersion + "." + mscoreMinorVersion + "." + mscoreUpdateVersion;
}
function generateTemporaryFilePath() {
var randomString = Math.random().toString(36).substring(2);
var basename = plugin.internalName + "_" + randomString;
var tmpdir = fileIO.tempPath();
return tmpdir + "/" + basename;
}
function getConvertedScoreData(score, format, multipleFiles) {
var fullBasePath = generateTemporaryFilePath();
var ok = plugin.writeScore(score, fullBasePath, format);
if (!ok)
throw new Error("Failed to export score to '" + format + "'");
function processTemporaryFile(filePath) {
fileIO.source = filePath;
if (!fileIO.exists())
return;
var data = fileIO.read();
// Cleanup the temporary file.
fileIO.remove();
return data;
}
var filesContentList = []
if (multipleFiles) {
function padNumber(n, size) {
var str = n.toString();
while (str.length < size)
str = "0" + str;
return str;
}
function getFilename(n, padding) {
var numStr = padNumber(i, padding);
return [fullBasePath, "-", numStr, ".", format].join("");
}
var numberPadding = 1;
// Probe for padding.
// TODO: better use FolderListModel? It works asynchronously though.
for (var i = 1; i < 5; ++i) {
fileIO.source = getFilename(1, i);
if (fileIO.exists()) {
numberPadding = i;
break;
}
}
var maxPages = 10000;
// MuseScore saves images to files like <score_name>-1.svg, <score_name>-2.svg etc.
for (var i = 1; i < maxPages; ++i) {
var filePath = getFilename(i, numberPadding);
var fileContent = processTemporaryFile(filePath);
if (fileContent)
filesContentList.push(fileContent);
else
break;
}
} else {
var filePath = [fullBasePath, ".", format].join("");
var fileContent = processTemporaryFile(filePath);
if (fileContent)
filesContentList.push(fileContent);
}
if (!filesContentList.length)
throw new Error("Failed to get data for '" + format + "' format");
return filesContentList;
}
function getMusicXmlForScore(score) {
var dataList = getConvertedScoreData(score, "musicxml", false);
return dataList[0];
}
function getSvgsForScore(score) {
return getConvertedScoreData(score, "svg", true);
}
function getCreatorXmlLines() {
var lines = [
"<creator>",
" <source>",
" <name>MuseScore</name>",
" <version>" + getMuseScoreVersionString() + "</version>",
" </source>",
" <exporter>",
" <name>" + plugin.name + "</name>",
" <version>" + plugin.version + "</version>",
" </exporter>",
"</creator>",
];
return lines;
}
function getMetadataXmlLines(score) {
var lines = [];
function addTagIfPresent(muName, smlName) {
var value = score.metaTag(muName);
if (value != "") {
var tag = "tag";
var tokens = [formatStartTag(tag, {"name": smlName}), value, formatEndTag(tag)];
lines.push(tokens.join(""));
}
}
var metadataTag = "metadata";
lines.push(formatStartTag(metadataTag));
// It seems there is no way to get a list of available
// tags, so add only those we will most likely need.
addTagIfPresent("workTitle", "title");
addTagIfPresent("composer", "composer");
addTagIfPresent("copyright", "copyright");
addTagIfPresent("source", "source");
lines.push(formatEndTag(metadataTag));
// Don't add empty metadata.
return (lines.length > 2) ? lines : [];
}
function getMeasureBbox(m, score) {
var mBbox = m.bbox;
var mPagePos = m.pagePos;
// Convert to a JS object to be able to change its properties.
var bbox = {
"x": mBbox.x + mPagePos.x,
"y": mBbox.y + mPagePos.y,
"width": mBbox.width,
"height": mBbox.height
};
var bboxTop = bbox.y;
var bboxBottom = bboxTop + bbox.height;
var ntracks = score.ntracks;
function addToBbox(e) {
if (!e || !e.visible)
return;
var eBbox = e.bbox;
var ePagePos = e.pagePos;
// Change only vertical boundaries: horizontal are OK in the standard bbox.
var eTop = eBbox.y + ePagePos.y;
var eBottom = eTop + eBbox.height;
if (eTop < bboxTop)
bboxTop = eTop;
if (eBottom > bboxBottom)
bboxBottom = eBottom;
}
function addListToBbox(elements) {
for (var ei = 0; ei < elements.length; ++ei) {
addToBbox(elements[ei]);
}
}
function addNoteToBbox(note) {
addToBbox(note);
addToBbox(note.accidental);
addListToBbox(note.dots);
addListToBbox(note.elements);
}
for (var seg = m.firstSegment; seg; seg = seg.nextInMeasure) {
for (var track = 0; track < ntracks; ++track) {
var e = seg.elementAt(track);
var type = e ? e.type : Element.INVALID;
switch (type) {
case Element.CHORD:
var notes = e.notes;
for (var ni = 0; ni < notes.length; ++ni) {
addNoteToBbox(notes[ni]);
}
var graceChords = e.graceNotes;
for (var gi = 0; gi < graceChords.length; ++gi) {
var graceChord = graceChords[gi];
var graceNotes = graceChord.notes;
for (var gni = 0; gni < graceNotes.length; ++gni) {
addNoteToBbox(graceNotes[gni]);
}
}
addToBbox(e.stem);
addToBbox(e.hook);
addToBbox(e.tuplet);
addToBbox(e.beam);
addListToBbox(e.lyrics);
break;
case Element.REST:
addToBbox(e);
addToBbox(e.tuplet);
addToBbox(e.beam);
addListToBbox(e.lyrics);
break;
case Element.CLEF:
case Element.KEYSIG:
case Element.TIMESIG:
addToBbox(e);
}
}
var annotations = seg.annotations;
for (var ai = 0; ai < annotations.length; ++ai) {
addToBbox(annotations[ai]);
}
}
addListToBbox(m.elements);
// Add extra space above and below the element boxes.
bboxTop -= 1.0;
bboxBottom += 1.0;
bbox.y = bboxTop;
bbox.height = bboxBottom - bboxTop;
return bbox;
}
function alignSystemBoxes(measures) {
var prevSystem = [];
var curSystem = [];
var curSystemIndex = 0;
function alignSystems(curSystem, prevSystem) {
if (curSystem.length > 0) {
// Define top and bottom for the current system.
var sysTop = 1e30;
var sysBottom = -1e30;
for (var ci = 0; ci < curSystem.length; ++ci) {
var cmTop = curSystem[ci].bbox.y;
var cmBottom = cmTop + curSystem[ci].bbox.height;
if (cmTop < sysTop)
sysTop = cmTop;
if (cmBottom > sysBottom)
sysBottom = cmBottom;
}
// Check a possible overlap with the previous system.
// TODO: how to handle it better?
if (prevSystem.length > 0 && prevSystem[0].page == curSystem[0].page) {
var prevSystemTop = prevSystem[0].bbox.y;
var prevSystemBottom = prevSystemTop + prevSystem[0].bbox.height;
if (sysTop < prevSystemBottom) {
prevSystemBottom = Math.max(prevSystemTop, (sysTop + prevSystemBottom) / 2);
sysTop = prevSystemBottom;
// Apply the corrected boundary for the previous system.
for (var pi = 0; pi < prevSystem.length; ++pi) {
prevSystem[pi].bbox.height = prevSystemBottom - prevSystemTop;
}
}
}
// Apply the final system boundaries.
for (var ci = 0; ci < curSystem.length; ++ci) {
var cm = curSystem[ci];
cm.bbox.y = sysTop;
cm.bbox.height = sysBottom - sysTop;
}
}
}
for (var mi = 0; mi < measures.length; ++mi) {
var m = measures[mi];
if (m.system == curSystemIndex) {
curSystem.push(m);
} else {
alignSystems(curSystem, prevSystem);
prevSystem = curSystem;
curSystem = [m];
curSystemIndex = m.system;
}
}
// Align the last system.
alignSystems(curSystem, prevSystem);
}
function addLayoutXmlLines(lines, score) {
var layoutTagName = "layout";
lines.push(formatStartTag(layoutTagName));
var style = score.style;
var pixelsPerSpatium = style.value("spatium"); // in internal DPI pixels
var pixelsPerInch = plugin.mscoreDPI; // internal DPI in MuseScore
var pointsPerInch = 72;
var pointsPerSpatium = pointsPerInch / pixelsPerInch * pixelsPerSpatium;
var pageWidthInches = style.value("pageWidth");
var pageHeightInches = style.value("pageHeight");
lines.push(formatStartTag("page-format"));
lines.push(formatTag("width", pageWidthInches * pointsPerInch));
lines.push(formatTag("height", pageHeightInches * pointsPerInch));
lines.push(formatTag("margin-left", style.value("pageOddLeftMargin") * pointsPerInch));
lines.push(formatTag("margin-right", (style.value("pageWidth") - style.value("pageOddLeftMargin") - style.value("pagePrintableWidth")) * pointsPerInch));
lines.push(formatTag("margin-top", style.value("pageOddTopMargin") * pointsPerInch));
lines.push(formatTag("margin-bottom", style.value("pageOddBottomMargin") * pointsPerInch));
lines.push(formatEndTag("page-format"));
if (style.value("pageEvenLeftMargin") != style.value("pageOddLeftMargin")
|| style.value("pageEvenTopMargin") != style.value("pageOddTopMargin")
|| style.value("pageEvenBottomMargin") != style.value("pageOddBottomMargin")) {
var iExceptionalPageStart = Math.abs(1 + score.pageNumberOffset) % 2;
var nPages = score.npages;
for (var iPage = iExceptionalPageStart; iPage < nPages; iPage += 2) {
lines.push(formatStartTag("page-format", {"page": iPage}));
lines.push(formatTag("margin-left", style.value("pageEvenLeftMargin") * pointsPerInch));
lines.push(formatTag("margin-right", (style.value("pageWidth") - style.value("pageEvenLeftMargin") - style.value("pagePrintableWidth")) * pointsPerInch));
lines.push(formatTag("margin-top", style.value("pageEvenTopMargin") * pointsPerInch));
lines.push(formatTag("margin-bottom", style.value("pageEvenBottomMargin") * pointsPerInch));
lines.push(formatEndTag("page-format"));
}
}
lines.push(formatStartTag("measures"));
var prevSystem = null;
var systemNumber = -1;
var measures = [];
for (var m = score.firstMeasureMM; m; m = m.nextMeasureMM) {
var bbox = getMeasureBbox(m, score);
var fs = m.firstSegment;
var tick = fs ? fs.tick : -1;
var system = findSystem(m);
if (!system.is(prevSystem)) {
++systemNumber;
prevSystem = system;
}
var measure = {
"tick": tick,
"bbox": {
"x": bbox.x * pointsPerSpatium,
"y": bbox.y * pointsPerSpatium,
"width": bbox.width * pointsPerSpatium,
"height": bbox.height * pointsPerSpatium,
},
"page": findPage(system).pagenumber,
"system": systemNumber,
};
measures.push(measure);
}
alignSystemBoxes(measures);
for (var mi = 0; mi < measures.length; ++mi) {
var m = measures[mi];
lines.push(formatStartTag("measure", {"page": m.page, "system": m.system}));
lines.push(formatTag("tick", m.tick));
lines.push(formatStandaloneTag("page-rect", m.bbox));
lines.push(formatEndTag("measure"));
}
lines.push(formatEndTag("measures"));
lines.push(formatStartTag("images"));
var images = getSvgsForScore(score);
for (var i = 0; i < images.length; ++i) {
lines.push(formatCDataTag("svg", images[i]));
}
lines.push(formatEndTag("images"));
lines.push(formatEndTag(layoutTagName));
return lines;
}
function preprocessScore(score, exportSettings) {
var undoLevel = 0;
var elementsToRemove = [];
if (exportSettings.layoutBreaksMode == "remove") {
for (var m = score.firstMeasureMM; m; m = m.nextMeasureMM) {
var elements = m.elements;
for (var i = 0; i < elements.length; ++i) {
var e = elements[i];
if (e.type == Element.LAYOUT_BREAK && e.layoutBreakType != LayoutBreak.SECTION && e.layoutBreakType != LayoutBreak.NOBREAK) {
elementsToRemove.push(e);
}
}
}
}
if (elementsToRemove.length > 0) {
score.startCmd();
for (var i = 0; i < elementsToRemove.length; ++i) {
removeElement(elementsToRemove[i]);
}
score.endCmd();
++undoLevel;
}
return undoLevel;
}
function reflowMeasuresBetweenBreaks(score) {
var undoLevel = 0;
var measures = [];
var lineBreaksToRemove = [];
var measuresToBreak = [];
function collectLastSystem(measures) {
if (measures.length == 0) {
return [];
}
var system = findSystem(measures[measures.length - 1]);
var systemMeasures = [];
for (var i = measures.length - 1; i >= 0; --i) {
var m = measures[i];
if (!findSystem(m).is(system))
break;
systemMeasures.push(m);
}
systemMeasures.reverse();
return systemMeasures;
}
function reflowMeasures(measures) {
var lastSystemMeasures = collectLastSystem(measures);
var reflowMeasures = measures;
while (lastSystemMeasures.length > 0) {
reflowMeasures = reflowMeasures.slice(0, -lastSystemMeasures.length);
var prevSystemMeasures = collectLastSystem(reflowMeasures);
var measureCountDiff = prevSystemMeasures.length - lastSystemMeasures.length;
var minMeasureCountDiff = lastSystemMeasures.length / 2 + 0.001;
var measuresToMoveCount = Math.floor(measureCountDiff / 2);
if (measureCountDiff >= minMeasureCountDiff) {
var breakMeasure = prevSystemMeasures[prevSystemMeasures.length - 1 - measuresToMoveCount];
// A measure may have a "no break" element. In that case respect it.
var isLayoutBreakForbidden = false;
var breakMeasureElements = breakMeasure.elements;
for (var i = 0; i < breakMeasureElements.length; ++i) {
var e = breakMeasureElements[i];
if (e.type == Element.LAYOUT_BREAK && e.layoutBreakType == LayoutBreak.NOBREAK) {
isLayoutBreakForbidden = true;
break;
}
}
if (!isLayoutBreakForbidden) {
measuresToBreak.push(breakMeasure);
}
}
lastSystemMeasures = prevSystemMeasures;
}
}
for (var m = score.firstMeasureMM; m; m = m.nextMeasureMM) {
measures.push(m);
var layoutBreak = null;
var measureElements = m.elements;
for (var i = 0; i < measureElements.length; ++i) {
var e = measureElements[i];
if (e.type == Element.LAYOUT_BREAK && e.layoutBreakType != LayoutBreak.NOBREAK) {
layoutBreak = e;
// Page breaks should be replaced by line breaks.
if (layoutBreak.layoutBreakType == LayoutBreak.PAGE) {
lineBreaksToRemove.push(layoutBreak);
measuresToBreak.push(m);
}
break;
}
}
if (layoutBreak) {
reflowMeasures(measures);
measures = [];
}
}
// Handle the last set of measures.
reflowMeasures(measures);
if (lineBreaksToRemove.length > 0 || measuresToBreak.length > 0) {
score.startCmd();
for (var i = 0; i < lineBreaksToRemove.length; ++i) {
removeElement(lineBreaksToRemove[i]);
}
var cursor = score.newCursor();
for (var i = 0; i < measuresToBreak.length; ++i) {
var seg = measuresToBreak[i].firstSegment;
if (!seg)
continue;
cursor.rewindToTick(seg.tick);
var lb = newElement(Element.LAYOUT_BREAK);
lb.layoutBreakType = LayoutBreak.LINE;
cursor.add(lb);
}
score.endCmd();
++undoLevel;
}
return undoLevel;
}
function preprocessScoreLayout(score, layout, exportSettings) {
var undoLevel = 0;
var layoutSettingsCount = Object.keys(layout).length;
if (layoutSettingsCount) {
score.startCmd();
var style = score.style;
for (var key in layout) {
style.setValue(key, layout[key]);
}
score.endCmd();
++undoLevel;
if (exportSettings.layoutBreaksMode == "reflow") {
undoLevel += reflowMeasuresBetweenBreaks(score);
}
}
return undoLevel;
}
function addLayouts(lines, score, layouts, exportSettings) {
if (!layouts || !layouts.length) {
addLayoutXmlLines(lines, score);
return;