Skip to content

Commit

Permalink
staverepetition migrated
Browse files Browse the repository at this point in the history
  • Loading branch information
rvilarl committed May 19, 2021
1 parent a0f8826 commit 183fd27
Showing 1 changed file with 46 additions and 37 deletions.
83 changes: 46 additions & 37 deletions src/staverepetition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,35 @@

import { StaveModifier } from './stavemodifier';
import { Glyph } from './glyph';
import { FontInfo } from './types/common';
import { Stave } from './stave';

export class Repetition extends StaveModifier {
static get CATEGORY() {
protected symbol_type: number;

protected x_shift: number;
protected y_shift: number;
protected font: FontInfo;

static get CATEGORY(): string {
return 'repetitions';
}
static get type() {
return {
NONE: 1, // no coda or segno
CODA_LEFT: 2, // coda at beginning of stave
CODA_RIGHT: 3, // coda at end of stave
SEGNO_LEFT: 4, // segno at beginning of stave
SEGNO_RIGHT: 5, // segno at end of stave
DC: 6, // D.C. at end of stave
DC_AL_CODA: 7, // D.C. al coda at end of stave
DC_AL_FINE: 8, // D.C. al Fine end of stave
DS: 9, // D.S. at end of stave
DS_AL_CODA: 10, // D.S. al coda at end of stave
DS_AL_FINE: 11, // D.S. al Fine at end of stave
FINE: 12, // Fine at end of stave
};
}
static readonly type = {
NONE: 1, // no coda or segno
CODA_LEFT: 2, // coda at beginning of stave
CODA_RIGHT: 3, // coda at end of stave
SEGNO_LEFT: 4, // segno at beginning of stave
SEGNO_RIGHT: 5, // segno at end of stave
DC: 6, // D.C. at end of stave
DC_AL_CODA: 7, // D.C. al coda at end of stave
DC_AL_FINE: 8, // D.C. al Fine end of stave
DS: 9, // D.S. at end of stave
DS_AL_CODA: 10, // D.S. al coda at end of stave
DS_AL_FINE: 11, // D.S. al Fine at end of stave
FINE: 12, // Fine at end of stave
};

constructor(type, x, y_shift) {
constructor(type: number, x: number, y_shift: number) {
super();
this.setAttribute('type', 'Repetition');

Expand All @@ -36,28 +42,31 @@ export class Repetition extends StaveModifier {
this.font = {
family: 'times',
size: 12,
weight: 'bold italic',
weight: 'bold',
style: 'italic',
};
}

getCategory() {
getCategory(): string {
return Repetition.CATEGORY;
}
setShiftX(x) {

setShiftX(x: number): this {
this.x_shift = x;
return this;
}
setShiftY(y) {

setShiftY(y: number): this {
this.y_shift = y;
return this;
}

draw(stave, x) {
draw(stave: Stave, x: number): this {
this.setRendered();

switch (this.symbol_type) {
case Repetition.type.CODA_RIGHT:
this.drawCodaFixed(stave, x + stave.width);
this.drawCodaFixed(stave, x + stave.getWidth());
break;
case Repetition.type.CODA_LEFT:
this.drawSymbolText(stave, x, 'Coda', true);
Expand All @@ -66,7 +75,7 @@ export class Repetition extends StaveModifier {
this.drawSignoFixed(stave, x);
break;
case Repetition.type.SEGNO_RIGHT:
this.drawSignoFixed(stave, x + stave.width);
this.drawSignoFixed(stave, x + stave.getWidth());
break;
case Repetition.type.DC:
this.drawSymbolText(stave, x, 'D.C.', false);
Expand Down Expand Up @@ -96,19 +105,19 @@ export class Repetition extends StaveModifier {
return this;
}

drawCodaFixed(stave, x) {
const y = stave.getYForTopText(stave.options.num_lines) + this.y_shift;
Glyph.renderGlyph(stave.context, this.x + x + this.x_shift, y + 25, 40, 'coda', { category: 'coda' });
drawCodaFixed(stave: Stave, x: number): this {
const y = stave.getYForTopText(stave.getNumLines()) + this.y_shift;
Glyph.renderGlyph(stave.checkContext(), this.x + x + this.x_shift, y + 25, 40, 'coda', { category: 'coda' });
return this;
}

drawSignoFixed(stave, x) {
const y = stave.getYForTopText(stave.options.num_lines) + this.y_shift;
Glyph.renderGlyph(stave.context, this.x + x + this.x_shift, y + 25, 30, 'segno', { category: 'segno' });
drawSignoFixed(stave: Stave, x: number): this {
const y = stave.getYForTopText(stave.getNumLines()) + this.y_shift;
Glyph.renderGlyph(stave.checkContext(), this.x + x + this.x_shift, y + 25, 30, 'segno', { category: 'segno' });
return this;
}

drawSymbolText(stave, x, text, draw_coda) {
drawSymbolText(stave: Stave, x: number, text: string, draw_coda: boolean): this {
const ctx = stave.checkContext();

ctx.save();
Expand All @@ -118,19 +127,19 @@ export class Repetition extends StaveModifier {
let symbol_x = x + this.x_shift;
if (this.symbol_type === Repetition.type.CODA_LEFT) {
// Offset Coda text to right of stave beginning
text_x = this.x + stave.options.vertical_bar_width;
text_x = this.x + stave.getOptions().vertical_bar_width;
symbol_x = text_x + ctx.measureText(text).width + 12;
} else if (this.symbol_type === Repetition.type.DS) {
const modifierWidth = stave.start_x - this.x;
text_x = this.x + x + this.x_shift + stave.width - 5 - modifierWidth - ctx.measureText(text).width;
const modifierWidth = stave.getNoteStartX() - this.x;
text_x = this.x + x + this.x_shift + stave.getWidth() - 5 - modifierWidth - ctx.measureText(text).width;
// TODO this is weird. setting the x position should probably be refactored, parameters aren't clear here.
} else {
// Offset Signo text to left stave end
symbol_x = this.x + x + stave.width - 5 + this.x_shift;
symbol_x = this.x + x + stave.getWidth() - 5 + this.x_shift;
text_x = symbol_x - +ctx.measureText(text).width - 12;
}

const y = stave.getYForTopText(stave.options.num_lines) + this.y_shift;
const y = stave.getYForTopText(stave.getNumLines()) + this.y_shift;
if (draw_coda) {
Glyph.renderGlyph(ctx, symbol_x, y, 40, 'coda', { category: 'coda' });
}
Expand Down

0 comments on commit 183fd27

Please sign in to comment.