Skip to content

Commit

Permalink
osmd patch stave & stavesection
Browse files Browse the repository at this point in the history
  • Loading branch information
rvilarl committed Jan 8, 2022
1 parent 2256186 commit a8191ab
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
6 changes: 4 additions & 2 deletions src/stave.ts
Original file line number Diff line number Diff line change
Expand Up @@ -310,8 +310,10 @@ export class Stave extends Element {
}

// Section functions
setSection(section: string, y: number): this {
this.modifiers.push(new StaveSection(section, this.x, y));
setSection(section: string, y: number, xOffset = 0, fontSize = 12) {
const staveSection = new StaveSection(section, this.x + xOffset, y);
staveSection.setFontSize(fontSize);
this.modifiers.push(staveSection);
return this;
}

Expand Down
11 changes: 7 additions & 4 deletions src/stavesection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export class StaveSection extends StaveModifier {
this.x = x;
this.shift_x = 0;
this.shift_y = shift_y;
this.resetFont();
this.setFont('bold 12pt sans-serif');
}

setStaveSection(section: string): this {
Expand All @@ -55,16 +55,19 @@ export class StaveSection extends StaveModifier {
ctx.setLineWidth(2);
ctx.setFont(this.textFont);

const textWidth = ctx.measureText('' + this.section).width;
const textMeasurements = ctx.measureText('' + this.section);
const textWidth = textMeasurements.width;
const textHeight = textMeasurements.height;
let width = textWidth + 6; // add left & right padding
if (width < 18) width = 18;
const height = 20;
const height = textHeight;

// Seems to be a good default y
const y = stave.getYForTopText(3) + this.shift_y;
let x = this.x + shift_x;
ctx.beginPath();
ctx.setLineWidth(2);
ctx.rect(x, y, width, height);
ctx.rect(x, y + textHeight / 8, width, height);
ctx.stroke();
x += (width - textWidth) / 2;
ctx.fillText('' + this.section, x, y + 16);
Expand Down

0 comments on commit a8191ab

Please sign in to comment.