Browse Source

fix(pdfOptions): only change option when value given. create default PageFormat.Undefined value. Always check if format is undefined.

sschmid 5 years ago
parent
commit
e580c727c8

+ 16 - 1
src/MusicalScore/Graphical/EngravingRules.ts

@@ -441,6 +441,8 @@ export class EngravingRules {
 
 
         this.fixStafflineBoundingBox = false; // TODO temporary workaround
         this.fixStafflineBoundingBox = false; // TODO temporary workaround
 
 
+        this.pageFormat = PageFormat.UndefinedPageFormat; // default: undefined / 'infinite' height page, using the canvas'/container's width and height
+
         this.populateDictionaries();
         this.populateDictionaries();
         try {
         try {
             this.maxInstructionsConstValue = this.ClefLeftMargin + this.ClefRightMargin + this.KeyRightMargin + this.RhythmRightMargin + 11;
             this.maxInstructionsConstValue = this.ClefLeftMargin + this.ClefRightMargin + this.KeyRightMargin + this.RhythmRightMargin + 11;
@@ -1620,6 +1622,7 @@ export class EngravingRules {
     }
     }
 }
 }
 
 
+// TODO maybe this should be moved to OSMDOptions. Also see OpenSheetMusicDisplay.PageFormatStandards
 export class PageFormat {
 export class PageFormat {
     constructor(width: number, height: number) {
     constructor(width: number, height: number) {
         this.width = width;
         this.width = width;
@@ -1628,6 +1631,18 @@ export class PageFormat {
     public width: number;
     public width: number;
     public height: number;
     public height: number;
     public get aspectRatio(): number {
     public get aspectRatio(): number {
-        return this.width / this.height;
+        if (!this.IsUndefined) {
+            return this.width / this.height;
+        } else {
+            return 0; // infinite page height
+        }
+    }
+    /** Undefined page format: use default page format. */
+    public get IsUndefined(): boolean {
+        return this.width === undefined || this.height === undefined || this.height === 0 || this.width === 0;
+    }
+
+    public static get UndefinedPageFormat(): PageFormat {
+        return new PageFormat(0, 0);
     }
     }
 }
 }

+ 1 - 0
src/OpenSheetMusicDisplay/OSMDOptions.ts

@@ -112,6 +112,7 @@ export interface IOSMDOptions {
     /** Whether to draw hidden/invisible notes (print-object="no" in XML). Default false. Not yet supported. */ // TODO
     /** Whether to draw hidden/invisible notes (print-object="no" in XML). Default false. Not yet supported. */ // TODO
     drawHiddenNotes?: boolean;
     drawHiddenNotes?: boolean;
 
 
+    /**  See OpenSheetMusicDisplay.PageFormatStandards for standard options like OpenSheetMusicDisplay.PageFormatStandards["A4 P"]. */
     pageFormat?: PageFormat;
     pageFormat?: PageFormat;
 }
 }
 
 

+ 13 - 11
src/OpenSheetMusicDisplay/OpenSheetMusicDisplay.ts

@@ -178,10 +178,10 @@ export class OpenSheetMusicDisplay {
         // Set page width
         // Set page width
         const width: number = this.container.offsetWidth;
         const width: number = this.container.offsetWidth;
         this.sheet.pageWidth = width / this.zoom / 10.0;
         this.sheet.pageWidth = width / this.zoom / 10.0;
-        if (EngravingRules.Rules.PageFormat) {
+        if (EngravingRules.Rules.PageFormat && !EngravingRules.Rules.PageFormat.IsUndefined) {
             EngravingRules.Rules.PageHeight = this.sheet.pageWidth / EngravingRules.Rules.PageFormat.aspectRatio;
             EngravingRules.Rules.PageHeight = this.sheet.pageWidth / EngravingRules.Rules.PageFormat.aspectRatio;
         } else {
         } else {
-            EngravingRules.Rules.PageHeight = 100001.0;
+            EngravingRules.Rules.PageHeight = 100001; // infinite page height // TODO Number.MAX_SAFE_INTEGER ?
         }
         }
 
 
         // Before introducing the following optimization (maybe irrelevant), tests
         // Before introducing the following optimization (maybe irrelevant), tests
@@ -206,7 +206,7 @@ export class OpenSheetMusicDisplay {
         // create new backends
         // create new backends
         for (const page of this.graphic.MusicPages) {
         for (const page of this.graphic.MusicPages) {
             const backend: VexFlowBackend = this.createBackend(this.backendType);
             const backend: VexFlowBackend = this.createBackend(this.backendType);
-            if (EngravingRules.Rules.PageFormat) {
+            if (EngravingRules.Rules.PageFormat && !EngravingRules.Rules.PageFormat.IsUndefined) {
                 backend.resize(width, width / EngravingRules.Rules.PageFormat.aspectRatio);
                 backend.resize(width, width / EngravingRules.Rules.PageFormat.aspectRatio);
             } else {
             } else {
                 backend.resize(width, (page.PositionAndShape.Size.height + 15) * this.zoom * 10.0);
                 backend.resize(width, (page.PositionAndShape.Size.height + 15) * this.zoom * 10.0);
@@ -281,7 +281,7 @@ export class OpenSheetMusicDisplay {
         this.drawer = new VexFlowMusicSheetDrawer(this.drawingParameters);
         this.drawer = new VexFlowMusicSheetDrawer(this.drawingParameters);
 
 
         // individual drawing parameters options
         // individual drawing parameters options
-        if (options.autoBeam !== undefined) {
+        if (options.autoBeam !== undefined) { // only change an option if it was given in options, otherwise it will be undefined
             EngravingRules.Rules.AutoBeamNotes = options.autoBeam;
             EngravingRules.Rules.AutoBeamNotes = options.autoBeam;
         }
         }
         const autoBeamOptions: AutoBeamOptions = options.autoBeamOptions;
         const autoBeamOptions: AutoBeamOptions = options.autoBeamOptions;
@@ -413,9 +413,9 @@ export class OpenSheetMusicDisplay {
             this.autoResizeEnabled = false;
             this.autoResizeEnabled = false;
             // we could remove the window EventListener here, but not necessary.
             // we could remove the window EventListener here, but not necessary.
         }
         }
-
-        // no if -> shall also be set to undefined:
-        EngravingRules.Rules.PageFormat = options.pageFormat;
+        if (options.pageFormat !== undefined) { // only change this option if it was given, see above
+            EngravingRules.Rules.PageFormat = options.pageFormat;
+        }
     }
     }
 
 
     public setColoringMode(options: IOSMDOptions): void {
     public setColoringMode(options: IOSMDOptions): void {
@@ -607,6 +607,7 @@ export class OpenSheetMusicDisplay {
         return backend;
         return backend;
     }
     }
 
 
+    /** Standard page format options like A4 or Letter, in portrait and landscape. E.g. PageFormatStandards["A4 P"] or PageFormatStandards["Letter L"]. */
     public static PageFormatStandards: {[type: string]: PageFormat} = {
     public static PageFormatStandards: {[type: string]: PageFormat} = {
         "A3 L": new PageFormat(420, 297),
         "A3 L": new PageFormat(420, 297),
         "A3 P": new PageFormat(297, 420),
         "A3 P": new PageFormat(297, 420),
@@ -621,7 +622,7 @@ export class OpenSheetMusicDisplay {
     };
     };
 
 
     public setPageFormat(formatId: string): void {
     public setPageFormat(formatId: string): void {
-        let f: PageFormat = undefined;
+        let f: PageFormat = PageFormat.UndefinedPageFormat; // default: 'endless' page height, take canvas/container width
         if (OpenSheetMusicDisplay.PageFormatStandards.hasOwnProperty(formatId)) {
         if (OpenSheetMusicDisplay.PageFormatStandards.hasOwnProperty(formatId)) {
             f = OpenSheetMusicDisplay.PageFormatStandards[formatId];
             f = OpenSheetMusicDisplay.PageFormatStandards[formatId];
         }
         }
@@ -656,9 +657,10 @@ export class OpenSheetMusicDisplay {
 
 
         let pageWidth: number = 210;
         let pageWidth: number = 210;
         let pageHeight: number = 297;
         let pageHeight: number = 297;
-        if (EngravingRules.Rules.PageFormat) {
-            pageWidth = EngravingRules.Rules.PageFormat.width;
-            pageHeight = EngravingRules.Rules.PageFormat.height;
+        const engravingRulesPageFormat: PageFormat = EngravingRules.Rules.PageFormat;
+        if (engravingRulesPageFormat && !engravingRulesPageFormat.IsUndefined) {
+            pageWidth = engravingRulesPageFormat.width;
+            pageHeight = engravingRulesPageFormat.height;
         } else {
         } else {
             pageHeight = pageWidth * svgElement.clientHeight / svgElement.clientWidth;
             pageHeight = pageWidth * svgElement.clientHeight / svgElement.clientWidth;
         }
         }