Browse Source

Merge Experiment/refactor: Add drawUpToPageNumber and drawUpToSystemNumber options (#883)

* Preserving Current Progress

* Added drawUpToPageNumber and drawUpToSystemNumber options (#832)

* refactor DrawUpToPageNumber and DrawUpToSystemNumber options (#832)

* refactor EngravingRules, fix temp infinite loop

refactor forgotten to commit in 65441b8b3d43f56aad3e89db953cd21db91a8a83
or polishes to the new drawUpToSystemNumber and drawUpToPageNumber options

remove a console.log in generateImages

* fix container height for multiple pages when not all pages are drawn (drawUpToPageNumber) (#832)

Co-authored-by: anita2822 <68724826+anita2822@users.noreply.github.com>
Co-authored-by: sschmid <s.schmid@phonicscore.com>
Simon 4 years ago
parent
commit
dcc4dbe1db

+ 5 - 0
src/MusicalScore/Graphical/EngravingRules.ts

@@ -224,6 +224,9 @@ export class EngravingRules {
     public DefaultVexFlowNoteFont: string;
     public MaxMeasureToDrawIndex: number;
     public MinMeasureToDrawIndex: number;
+    public MaxPageToDrawNumber: number;
+    public MaxSystemToDrawNumber: number;
+
     /** Whether to render a label for the composer of the piece at the top of the sheet. */
     public RenderComposer: boolean;
     public RenderTitle: boolean;
@@ -499,6 +502,8 @@ export class EngravingRules {
         this.DefaultVexFlowNoteFont = "gonville"; // was the default vexflow font up to vexflow 1.2.93, now it's Bravura, which is more cursive/bold
         this.MaxMeasureToDrawIndex = Number.MAX_VALUE;
         this.MinMeasureToDrawIndex = 0;
+        this.MaxSystemToDrawNumber = Number.MAX_VALUE;
+        this.MaxPageToDrawNumber = Number.MAX_VALUE;
         this.RenderComposer = true;
         this.RenderTitle = true;
         this.RenderSubtitle = true;

+ 4 - 2
src/MusicalScore/Graphical/MusicSheetDrawer.ts

@@ -90,8 +90,10 @@ export abstract class MusicSheetDrawer {
         if (this.drawingParameters.drawScrollIndicator) {
             this.drawScrollIndicator();
         }
-        // Draw all the pages
-        for (const page of this.graphicalMusicSheet.MusicPages) {
+        // Draw the pages
+        const pagesToDraw: number = Math.min(this.graphicalMusicSheet.MusicPages.length, this.rules.MaxPageToDrawNumber);
+        for (let i: number = 0; i < pagesToDraw; i ++) {
+            const page: GraphicalMusicPage = this.graphicalMusicSheet.MusicPages[i];
             this.drawPage(page);
         }
     }

+ 24 - 5
src/MusicalScore/Graphical/MusicSystemBuilder.ts

@@ -142,10 +142,18 @@ export class MusicSystemBuilder {
                 this.updateActiveClefs(sourceMeasure, graphicalMeasures);
                 this.measureListIndex++;
                 if (sourceMeasureBreaksSystem) {
+                    if (this.rules.MaxSystemToDrawNumber === this.musicSystems.length) {
+                        this.finalizeCurrentSystem(graphicalMeasures, !this.rules.StretchLastSystemLine, false);
+                        return this.musicSystems;
+                    }
                     this.finalizeCurrentAndCreateNewSystem(graphicalMeasures, !this.rules.StretchLastSystemLine, false);
                 }
                 prevMeasureEndsPart = sourceMeasureEndsPart;
             } else {
+                if (this.rules.MaxSystemToDrawNumber === this.musicSystems.length) {
+                    this.finalizeCurrentSystem(graphicalMeasures, false, true, doXmlPageBreak);
+                    return this.musicSystems;
+                }
                 // finalize current system and prepare a new one
                 this.finalizeCurrentAndCreateNewSystem(graphicalMeasures, false, true, doXmlPageBreak);
                 // don't increase measure index to check this measure now again
@@ -153,6 +161,10 @@ export class MusicSystemBuilder {
             }
         }
         if (this.currentSystemParams.systemMeasures.length > 0) {
+            if (this.rules.MaxSystemToDrawNumber === this.musicSystems.length) {
+                this.finalizeCurrentSystem(this.measureList[this.measureList.length - 1], !this.rules.StretchLastSystemLine, false);
+                return this.musicSystems;
+            }
             this.finalizeCurrentAndCreateNewSystem(this.measureList[this.measureList.length - 1], !this.rules.StretchLastSystemLine, false);
         }
         return this.musicSystems;
@@ -201,6 +213,18 @@ export class MusicSystemBuilder {
                                                 systemEndsPart: boolean = false,
                                                 checkExtraInstructionMeasure: boolean = true,
                                                 startNewPage: boolean = false): void {
+        this.finalizeCurrentSystem(measures, systemEndsPart, checkExtraInstructionMeasure, startNewPage);
+        this.currentSystemParams = new SystemBuildParameters(); // this and the following used to be in finalizeCurrentSystem after stretchMusicSystem
+        if (measures !== undefined &&
+            this.measureListIndex < this.measureList.length) {
+            this.currentSystemParams.currentSystem = this.initMusicSystem();
+        }
+    }
+
+    protected finalizeCurrentSystem(measures: GraphicalMeasure[],
+                                    systemEndsPart: boolean = false,
+                                    checkExtraInstructionMeasure: boolean = true,
+                                    startNewPage: boolean = false): void {
         this.currentSystemParams.currentSystem.breaksPage = startNewPage;
         this.adaptRepetitionLineWithIfNeeded();
         if (measures !== undefined &&
@@ -208,11 +232,6 @@ export class MusicSystemBuilder {
             this.checkAndCreateExtraInstructionMeasure(measures);
         }
         this.stretchMusicSystem(systemEndsPart);
-        this.currentSystemParams = new SystemBuildParameters();
-        if (measures !== undefined &&
-            this.measureListIndex < this.measureList.length) {
-            this.currentSystemParams.currentSystem = this.initMusicSystem();
-        }
     }
 
     /**

+ 6 - 0
src/MusicalScore/Graphical/VexFlow/VexFlowMusicSheetDrawer.ts

@@ -65,6 +65,9 @@ export class VexFlowMusicSheetDrawer extends MusicSheetDrawer {
 
         this.pageIdx = 0;
         for (const graphicalMusicPage of graphicalMusicSheet.MusicPages) {
+            if (graphicalMusicPage.PageNumber > this.rules.MaxPageToDrawNumber) {
+                break;
+            }
             const backend: VexFlowBackend = this.backends[this.pageIdx];
             backend.graphicalMusicPage = graphicalMusicPage;
             backend.scale(this.zoom);
@@ -79,6 +82,9 @@ export class VexFlowMusicSheetDrawer extends MusicSheetDrawer {
     }
 
     protected drawPage(page: GraphicalMusicPage): void {
+        if (!page) {
+            return;
+        }
         this.backend = this.backends[page.PageNumber - 1]; // TODO we may need to set this in a couple of other places. this.pageIdx is a bad solution
         super.drawPage(page);
         this.pageIdx += 1;

+ 5 - 1
src/OpenSheetMusicDisplay/OSMDOptions.ts

@@ -108,8 +108,12 @@ export interface IOSMDOptions {
     drawLyrics?: boolean;
     /** Whether to calculate extra slurs with bezier curves not covered by Vexflow slurs. Default true. */
     drawSlurs?: boolean;
-    /** Only draw measure n to m, where m is the number you specify. */
+    /** Only draw measure n to m, where m is the number specified. */
     drawUpToMeasureNumber?: number;
+    /** Only draw the first n systems, where n is the number specified. */
+    drawUpToSystemNumber?: number;
+    /** Only draw the first n pages, where n is the number specified. */
+    drawUpToPageNumber?: number;
     /** Only draw measure n to m, where n is the number you specify. */
     drawFromMeasureNumber?: number;
     /** Whether to fill measures that don't have notes given in the XML with whole rests (visible = 1, invisible = 2, for layouting). Default No (0). */

+ 9 - 0
src/OpenSheetMusicDisplay/OpenSheetMusicDisplay.ts

@@ -282,6 +282,9 @@ export class OpenSheetMusicDisplay {
 
         // TODO check if resize is necessary. set needResize or something when size was changed
         for (const page of this.graphic.MusicPages) {
+            if (page.PageNumber > this.rules.MaxPageToDrawNumber) {
+                break; // don't add the bounding boxes of pages that aren't drawn to the container height etc
+            }
             const backend: VexFlowBackend = this.createBackend(this.backendType, page);
             const sizeWarningPartTwo: string = " exceeds CanvasBackend limit of 32767. Cutting off score.";
             if (backend.getOSMDBackendType() === BackendType.Canvas && width > canvasDimensionsLimit) {
@@ -504,6 +507,12 @@ export class OpenSheetMusicDisplay {
         if (options.drawFromMeasureNumber) {
             this.rules.MinMeasureToDrawIndex = options.drawFromMeasureNumber - 1;
         }
+        if (options.drawUpToPageNumber) {
+            this.rules.MaxPageToDrawNumber = options.drawUpToPageNumber;
+        }
+        if (options.drawUpToSystemNumber) {
+            this.rules.MaxSystemToDrawNumber = options.drawUpToSystemNumber;
+        }
         if (options.tupletsRatioed) {
             this.rules.TupletsRatioed = true;
         }

+ 1 - 2
test/Util/generateImages_browserless.js

@@ -245,13 +245,12 @@ async function generateSampleImage (sampleFilename, directory, osmdInstance, osm
         const isFunctionTestSystemAndPageBreaks = sampleFilename.startsWith('OSMD_Function_Test_System_and_Page_Breaks')
         const isFunctionTestDrawingRange = sampleFilename.startsWith('OSMD_function_test_measuresToDraw_')
         const defaultOrCompactTightMode = sampleFilename.startsWith('OSMD_Function_Test_Container_height') ? 'compacttight' : 'default'
-        console.log('compacttightMode: ' + defaultOrCompactTightMode)
         osmdInstance.setOptions({
             autoBeam: isFunctionTestAutobeam, // only set to true for function test autobeam
             coloringMode: isFunctionTestAutoColoring ? 2 : 0,
             coloringSetCustom: isFunctionTestAutoColoring ? ['#d82c6b', '#F89D15', '#FFE21A', '#4dbd5c', '#009D96', '#43469d', '#76429c', '#ff0000'] : undefined,
             colorStemsLikeNoteheads: isFunctionTestAutoColoring,
-            drawingParameters: defaultOrCompactTightMode, // default resets all EngravingRules
+            drawingParameters: defaultOrCompactTightMode, // note: default resets all EngravingRules. could be solved differently
             drawFromMeasureNumber: isFunctionTestDrawingRange ? 9 : 1,
             drawUpToMeasureNumber: isFunctionTestDrawingRange ? 12 : Number.MAX_SAFE_INTEGER,
             newSystemFromXML: isFunctionTestSystemAndPageBreaks,