Ver código fonte

merge osmd-public/develop: fix x-alignment of staves with different key signatures

see https://github.com/opensheetmusicdisplay/opensheetmusicdisplay/pull/1427

in this commit, this is actually broken because of a fix gone wrong,
so see the next commit to fix this (disable the resetLayout() in MusicSystemBuilder:512)
sschmidTU 1 ano atrás
pai
commit
a3ff5f9522

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

@@ -449,8 +449,12 @@ export class MusicSystemBuilder {
         }
         let totalBeginInstructionLengthX: number = 0.0;
         const sourceMeasure: SourceMeasure = measures[0].parentSourceMeasure;
+        const staves: any[] = []; // VF.Stave. generally don't want to reference Vexflow classes here.
         for (let idx: number = 0; idx < measureCount; ++idx) {
             const measure: GraphicalMeasure = measures[idx];
+            if (measure) { // MultiRestMeasure may be undefined
+                staves.push((measure as any).getVFStave()); // as VexFlowMeasure
+            }
             const staffIndex: number = this.visibleStaffIndices[idx];
             const beginInstructionsStaffEntry: SourceStaffEntry = sourceMeasure.FirstInstructionsStaffEntries[staffIndex];
             const beginInstructionLengthX: number = this.AddInstructionsAtMeasureBegin(
@@ -460,6 +464,7 @@ export class MusicSystemBuilder {
             );
             totalBeginInstructionLengthX = Math.max(totalBeginInstructionLengthX, beginInstructionLengthX);
         }
+        staves[0].formatBegModifiers(staves);
         return totalBeginInstructionLengthX;
     }
 

+ 72 - 5
src/VexFlowPatch/src/stave.js

@@ -72,14 +72,72 @@ export class Stave extends Element {
     this.options.bottom_text_position = this.options.num_lines;
   }
 
+  /** align modifiers like time signature for vertical staves with different key signatures / time signature xs
+   * This method should be static, but that makes using it with `any` usage more difficult.
+   */
+  formatBegModifiers(staves) {
+    let maxX = 0;
+    // align note start
+    staves.forEach((stave) => {
+      if (stave.getNoteStartX() > maxX) maxX = stave.getNoteStartX();
+    });
+    staves.forEach((stave) => {
+      stave.setNoteStartX(maxX);
+    });
+
+    maxX = 0;
+    // align REPEAT_BEGIN
+    staves.forEach((stave) => {
+      const modifiers = stave.getModifiers(StaveModifier.Position.BEGIN, Barline.CATEGORY);
+      modifiers.forEach((modifier) => {
+        if (modifier.getType() == Barline.type.REPEAT_BEGIN)
+          if (modifier.getX() > maxX) maxX = modifier.getX();
+      });
+    });
+    staves.forEach((stave) => {
+      const modifiers = stave.getModifiers(StaveModifier.Position.BEGIN, Barline.CATEGORY);
+      modifiers.forEach((modifier) => {
+        if (modifier.getType() == Barline.type.REPEAT_BEGIN) modifier.setX(maxX);
+      });
+    });
+
+    maxX = 0;
+    // Align time signatures
+    staves.forEach((stave) => {
+      const modifiers = stave.getModifiers(StaveModifier.Position.BEGIN, TimeSignature.CATEGORY);
+      modifiers.forEach((modifier) => {
+        if (modifier.getX() > maxX) maxX = modifier.getX();
+      });
+    });
+    staves.forEach((stave) => {
+      const modifiers = stave.getModifiers(StaveModifier.Position.BEGIN, TimeSignature.CATEGORY);
+      modifiers.forEach((modifier) => {
+        modifier.setX(maxX);
+      });
+    });
+    // Align key signatures
+    // staves.forEach((stave) => {
+    //   const modifiers = stave.getModifiers(StaveModifier.Position.BEGIN, KeySignature.CATEGORY);
+    //   modifiers.forEach((modifier) => {
+    //     if (modifier.getX() > maxX) maxX = modifier.getX();
+    //   });
+    // });
+    // staves.forEach((stave) => {
+    //   const modifiers = stave.getModifiers(StaveModifier.Position.BEGIN, KeySignature.CATEGORY);
+    //   modifiers.forEach((modifier) => {
+    //     modifier.setX(maxX);
+    //   });
+    // });
+  }
+
   getOptions() { return this.options; }
 
   setNoteStartX(x) {
     if (!this.formatted) this.format();
 
     this.setStartX(x);
-    const begBarline = this.modifiers[0];
-    begBarline.setX(this.start_x - begBarline.getWidth());
+    // const begBarline = this.modifiers[0];
+    // begBarline.setX(this.start_x - begBarline.getWidth());
     return this;
   }
   setStartX(x) {
@@ -493,14 +551,23 @@ export class Stave extends Element {
       }
 
       x += padding;
-      modifier.setX(x);
+      const modifierX = modifier.getX();
+      if (modifierX > x) { // e.g. when beginning modifiers already x-aligned, different key signatures
+        x = modifierX;
+        // VexFlowPatch: don't overwrite modifier.x when it was already shifted further, e.g. to align time signatures.
+        //   see formatBegModifiers().
+      } else {
+        modifier.setX(x);
+      }
       x += width;
 
       if (padding + width === 0) offset--;
     }
 
-    this.setStartX(x);
-    // this.start_x = x;
+    if (x > this.start_x) {
+      this.setStartX(x);
+      // VexFlowPatch: don't overwrite start_x if it's already bigger (alignment, see formatBegModifiers())
+    }
     x = this.x + this.width;
 
     const widths = {

+ 157 - 0
test/data/test_noteStartX_key_signatures.musicxml

@@ -0,0 +1,157 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE score-partwise PUBLIC "-//Recordare//DTD MusicXML 3.1 Partwise//EN" "http://www.musicxml.org/dtds/partwise.dtd">
+<score-partwise version="3.1">
+  <work>
+    <work-title>test_noteStartX_key_signatures</work-title>
+    </work>
+  <identification>
+    <encoding>
+      <software>MuseScore 3.6.2</software>
+      <encoding-date>2023-07-13</encoding-date>
+      <supports element="accidental" type="yes"/>
+      <supports element="beam" type="yes"/>
+      <supports element="print" attribute="new-page" type="yes" value="yes"/>
+      <supports element="print" attribute="new-system" type="yes" value="yes"/>
+      <supports element="stem" type="yes"/>
+      </encoding>
+    </identification>
+  <defaults>
+    <scaling>
+      <millimeters>6.99911</millimeters>
+      <tenths>40</tenths>
+      </scaling>
+    <page-layout>
+      <page-height>1696.94</page-height>
+      <page-width>1200.48</page-width>
+      <page-margins type="even">
+        <left-margin>85.7252</left-margin>
+        <right-margin>85.7252</right-margin>
+        <top-margin>85.7252</top-margin>
+        <bottom-margin>85.7252</bottom-margin>
+        </page-margins>
+      <page-margins type="odd">
+        <left-margin>85.7252</left-margin>
+        <right-margin>85.7252</right-margin>
+        <top-margin>85.7252</top-margin>
+        <bottom-margin>85.7252</bottom-margin>
+        </page-margins>
+      </page-layout>
+    <word-font font-family="Edwin" font-size="10"/>
+    <lyric-font font-family="Edwin" font-size="10"/>
+    </defaults>
+  <credit page="1">
+    <credit-type>title</credit-type>
+    <credit-words default-x="600.242" default-y="1611.21" justify="center" valign="top" font-size="22">test_noteStartX_key_signatures</credit-words>
+    </credit>
+  <part-list>
+    <score-part id="P1">
+      <part-name>B♭ Clarinet</part-name>
+      <part-abbreviation>B♭ Cl.</part-abbreviation>
+      <score-instrument id="P1-I1">
+        <instrument-name>B♭ Clarinet</instrument-name>
+        </score-instrument>
+      <midi-device id="P1-I1" port="1"></midi-device>
+      <midi-instrument id="P1-I1">
+        <midi-channel>1</midi-channel>
+        <midi-program>72</midi-program>
+        <volume>78.7402</volume>
+        <pan>0</pan>
+        </midi-instrument>
+      </score-part>
+    <score-part id="P2">
+      <part-name>Violin</part-name>
+      <part-abbreviation>Vln.</part-abbreviation>
+      <score-instrument id="P2-I1">
+        <instrument-name>Violin</instrument-name>
+        </score-instrument>
+      <midi-device id="P2-I1" port="1"></midi-device>
+      <midi-instrument id="P2-I1">
+        <midi-channel>2</midi-channel>
+        <midi-program>41</midi-program>
+        <volume>78.7402</volume>
+        <pan>0</pan>
+        </midi-instrument>
+      </score-part>
+    </part-list>
+  <part id="P1">
+    <measure number="1" width="419.15">
+      <print>
+        <system-layout>
+          <system-margins>
+            <left-margin>114.98</left-margin>
+            <right-margin>494.57</right-margin>
+            </system-margins>
+          <top-system-distance>170.00</top-system-distance>
+          </system-layout>
+        </print>
+      <attributes>
+        <divisions>1</divisions>
+        <key>
+          <fifths>0</fifths>
+          </key>
+        <time>
+          <beats>2</beats>
+          <beat-type>4</beat-type>
+          </time>
+        <clef>
+          <sign>G</sign>
+          <line>2</line>
+          </clef>
+        <transpose>
+          <diatonic>-1</diatonic>
+          <chromatic>-2</chromatic>
+          </transpose>
+        </attributes>
+      <note default-x="110.64" default-y="-30.00">
+        <pitch>
+          <step>G</step>
+          <octave>4</octave>
+          </pitch>
+        <duration>2</duration>
+        <voice>1</voice>
+        <type>half</type>
+        <stem>up</stem>
+        </note>
+      <barline location="right">
+        <bar-style>light-heavy</bar-style>
+        </barline>
+      </measure>
+    </part>
+  <part id="P2">
+    <measure number="1" width="419.15">
+      <print>
+        <staff-layout number="1">
+          <staff-distance>96.19</staff-distance>
+          </staff-layout>
+        </print>
+      <attributes>
+        <divisions>1</divisions>
+        <key>
+          <fifths>-2</fifths>
+          </key>
+        <time>
+          <beats>2</beats>
+          <beat-type>4</beat-type>
+          </time>
+        <clef>
+          <sign>G</sign>
+          <line>2</line>
+          </clef>
+        </attributes>
+      <note default-x="110.64" default-y="-156.19">
+        <pitch>
+          <step>B</step>
+          <alter>-1</alter>
+          <octave>4</octave>
+          </pitch>
+        <duration>2</duration>
+        <voice>1</voice>
+        <type>half</type>
+        <stem>down</stem>
+        </note>
+      <barline location="right">
+        <bar-style>light-heavy</bar-style>
+        </barline>
+      </measure>
+    </part>
+  </score-partwise>