Skip to content

Commit 5e2f9ba

Browse files
committed
Avoid drawing duplicate rests
1 parent bfe34bc commit 5e2f9ba

File tree

2 files changed

+35
-5
lines changed

2 files changed

+35
-5
lines changed

src/rendering/glyphs/ScoreBeatGlyph.ts

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import { PercussionNoteHeadGlyph } from './PercussionNoteHeadGlyph';
2424
import { Logger } from '@src/alphatab';
2525
import { ArticStaccatoAboveGlyph } from './ArticStaccatoAboveGlyph';
2626
import { MusicFontSymbol } from '../../model/MusicFontSymbol';
27-
import { TextBaseline } from '@src/platform/ICanvas';
27+
import { ICanvas, TextBaseline } from '@src/platform/ICanvas';
2828
import { PictEdgeOfCymbalGlyph } from './PictEdgeOfCymbalGlyph';
2929
import { PickStrokeGlyph } from './PickStrokeGlyph';
3030
import { PickStroke } from '@src/model/PickStroke';
@@ -33,7 +33,8 @@ import { BeamingHelper } from '../utils/BeamingHelper';
3333

3434
export class ScoreBeatGlyph extends BeatOnNoteGlyphBase {
3535
private _collisionOffset: number = -1000;
36-
36+
private _skipPaint: boolean = false;
37+
3738
public noteHeads: ScoreNoteChordGlyph | null = null;
3839
public restGlyph: ScoreRestGlyph | null = null;
3940

@@ -57,13 +58,26 @@ export class ScoreBeatGlyph extends BeatOnNoteGlyphBase {
5758
} else if (this.restGlyph) {
5859
this.restGlyph.updateBeamingHelper(this.container.x + this.x);
5960
if (this._collisionOffset === -1000) {
60-
this._collisionOffset = this.renderer.layoutingInfo.applyRestCollisionOffset(this.container.beat, this.restGlyph.y,
61+
this._collisionOffset = this.renderer.layoutingInfo.applyRestCollisionOffset(this.container.beat, this.restGlyph.y,
6162
(this.renderer as ScoreBarRenderer).getScoreHeight(1));
6263
this.y += this._collisionOffset;
64+
const existingRests = this.renderer.layoutingInfo.restDurationsByDisplayTime;
65+
if (existingRests.has(this.container.beat.playbackStart) &&
66+
existingRests.get(this.container.beat.playbackStart)!.has(this.container.beat.playbackDuration) &&
67+
existingRests.get(this.container.beat.playbackStart)!.get(this.container.beat.playbackDuration) !== this.container.beat.id
68+
) {
69+
this._skipPaint = true;
70+
}
6371
}
6472
}
6573
}
6674

75+
public paint(cx: number, cy: number, canvas: ICanvas): void {
76+
if (!this._skipPaint) {
77+
super.paint(cx, cy, canvas);
78+
}
79+
}
80+
6781
public doLayout(): void {
6882
// create glyphs
6983
let sr: ScoreBarRenderer = this.renderer as ScoreBarRenderer;
@@ -136,13 +150,15 @@ export class ScoreBeatGlyph extends BeatOnNoteGlyphBase {
136150
this.restGlyph.beamingHelper = this.beamingHelper;
137151
this.addGlyph(this.restGlyph);
138152

139-
if(this.container.beat.voice.index === 0) {
153+
if (this.container.beat.voice.index === 0) {
140154
const restSizes = BeamingHelper.computeLineHeightsForRest(this.container.beat.duration);
141155
let restTop = this.restGlyph.y - sr.getScoreHeight(restSizes[0]);
142156
let restBottom = this.restGlyph.y + sr.getScoreHeight(restSizes[1]);
143157
this.renderer.layoutingInfo.setBeatYPositions(this.container.beat, restTop, restBottom);
158+
} else {
159+
this.renderer.layoutingInfo.registerRest(this.container.beat);
144160
}
145-
161+
146162
if (this.beamingHelper) {
147163
this.beamingHelper.applyRest(this.container.beat, line);
148164
}

src/rendering/staves/BarLayoutingInfo.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ export class BarLayoutingInfo {
7171
public voiceSize: number = 0;
7272
public minStretchForce: number = 0;
7373
public totalSpringConstant: number = 0;
74+
public restDurationsByDisplayTime: Map<number/*start*/, Map<number/*duration*/, number/*beat id*/>> = new Map();
7475

7576
public updateVoiceSize(size: number): void {
7677
if (size > this.voiceSize) {
@@ -107,6 +108,19 @@ export class BarLayoutingInfo {
107108
this._reservedLayoutAreasByDisplayTime.set(beat.displayStart, new ReservedLayoutArea());
108109
}
109110
this._reservedLayoutAreasByDisplayTime.get(beat.displayStart)!.addSlot(topY, bottomY);
111+
if (beat.isRest) {
112+
this.registerRest(beat);
113+
114+
}
115+
}
116+
117+
public registerRest(beat: Beat) {
118+
if (!this.restDurationsByDisplayTime.has(beat.displayStart)) {
119+
this.restDurationsByDisplayTime.set(beat.displayStart, new Map<number, number>());
120+
}
121+
if (!this.restDurationsByDisplayTime.get(beat.displayStart)!.has(beat.playbackDuration)) {
122+
this.restDurationsByDisplayTime.get(beat.displayStart)!.set(beat.playbackDuration, beat.id);
123+
}
110124
}
111125

112126
public applyRestCollisionOffset(beat: Beat, currentY: number, linesToPixel: number): number {

0 commit comments

Comments
 (0)