Skip to content

Commit

Permalink
Barlines
Browse files Browse the repository at this point in the history
  • Loading branch information
l3v5y committed Jul 7, 2013
1 parent 8b8a48e commit 6f5d4ed
Show file tree
Hide file tree
Showing 4 changed files with 225 additions and 41 deletions.
11 changes: 11 additions & 0 deletions src/barnote.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,17 @@ Vex.Flow.BarNote.prototype.init = function() {
this.metrics.widths[TYPE.REPEAT_BOTH] = 18;
this.metrics.widths[TYPE.NONE] = 0;

this.metrics.widths[TYPE.DOTTED] = 8;
this.metrics.widths[TYPE.DASH] = 8;
this.metrics.widths[TYPE.HEAVY] = 10;
this.metrics.widths[TYPE.DOUBLE_L] = 12;
this.metrics.widths[TYPE.DOUBLE_LH] = 15;
this.metrics.widths[TYPE.DOUBLE_HL] = 15;
this.metrics.widths[TYPE.DOUBLE_H] = 15;

this.metrics.widths[TYPE.TICK] = 8;
this.metrics.widths[TYPE.SHORT] = 8;

// Note properties
this.ignore_ticks = true;
this.type = TYPE.SINGLE;
Expand Down
143 changes: 131 additions & 12 deletions src/documentformatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -352,18 +352,137 @@ Vex.Flow.DocumentFormatter.prototype.drawPart =
var voices = part.getVoices();
// OWN: letztendlich hier bars anhängen

vfStaves.forEach(function(stave) {
// if(typeof part.bars == "object") {
// if(part.bars.location == "right" && part.bars.direction == "backward") {
// stave.setEndBarType(Vex.Flow.Barline.type.REPEAT_END);
// }
// if(part.bars.location == "left" && part.bars.direction == "forward") {
// stave.setBegBarType(Vex.Flow.Barline.type.REPEAT_BEGIN);
// }
// }
stave.setEndBarType(Vex.Flow.Barline.type.REPEAT_END);
stave.setBegBarType(Vex.Flow.Barline.type.REPEAT_BEGIN);
stave.setContext(context).draw();
vfStaves.forEach(function(stave) {
if(typeof part.bars == "object") {
switch(part.bars.barStyle)
{
case "light-heavy":
if(part.bars.location == "right" && part.bars.repeatDirection == "backward")
{
stave.setEndBarType(Vex.Flow.Barline.type.REPEAT_END);
}
else if(part.bars.location == "left")
{
stave.setBegBarType(Vex.Flow.Barline.type.DOUBLE_LH);
}
else
{
stave.setEndBarType(Vex.Flow.Barline.type.DOUBLE_LH);
}
break;
case "heavy-light":
if(part.bars.location == "left" && part.bars.repeatDirection == "forward")
{
stave.setBegBarType(Vex.Flow.Barline.type.REPEAT_BEGIN);
}
else if(part.bars.location == "left")
{
stave.setBegBarType(Vex.Flow.Barline.type.DOUBLE_HL);
}
else
{
stave.setEndBarType(Vex.Flow.Barline.type.DOUBLE_HL);
}
break;

case "dotted":
if(part.bars.location == "left")
{
stave.setBegBarType(Vex.Flow.Barline.type.DOTTED);
}
else
{
stave.setEndBarType(Vex.Flow.Barline.type.DOTTED);
}
break;
case "dashed":
if(part.bars.location == "left")
{
stave.setBegBarType(Vex.Flow.Barline.type.DASHED);
}
else
{
stave.setEndBarType(Vex.Flow.Barline.type.DASHED);
}
break;
case "heavy":
if(part.bars.location == "left")
{
stave.setBegBarType(Vex.Flow.Barline.type.HEAVY);
}
else
{
stave.setEndBarType(Vex.Flow.Barline.type.HEAVY);
}
break;
case "tick":
if(part.bars.location == "left")
{
stave.setBegBarType(Vex.Flow.Barline.type.TICK);
}
else
{
stave.setEndBarType(Vex.Flow.Barline.type.TICK);
}
break;
case "short":
if(part.bars.location == "left")
{
stave.setBegBarType(Vex.Flow.Barline.type.SHORT);
}
else
{
stave.setEndBarType(Vex.Flow.Barline.type.SHORT);
}
break;
case "none":
if(part.bars.location == "left")
{
stave.setBegBarType(Vex.Flow.Barline.type.NONE);
}
else
{
stave.setEndBarType(Vex.Flow.Barline.type.NONE);
}
break;


case "light-light":
if(part.bars.location == "left")
{
stave.setBegBarType(Vex.Flow.Barline.type.DOUBLE_L);
}
else
{
stave.setEndBarType(Vex.Flow.Barline.type.DOUBLE_L);
}
break;
case "heavy-heavy":
if(part.bars.location == "left")
{
stave.setBegBarType(Vex.Flow.Barline.type.DOUBLE_H);
}
else
{
stave.setEndBarType(Vex.Flow.Barline.type.DOUBLE_H);
}
break;

case "regular":
if(part.bars.location == "left")
{
stave.setBegBarType(Vex.Flow.Barline.type.SINGLE);
}
else
{
stave.setEndBarType(Vex.Flow.Barline.type.SINGLE);
}
break;
default:
break;
}
}
stave.setContext(context).draw();
});

var allVfObjects = new Array();
Expand Down
46 changes: 25 additions & 21 deletions src/musicxml.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ Vex.Flow.Backend.MusicXML.prototype.getNumberOfMeasures = function() {
Vex.Flow.Backend.MusicXML.prototype.getMeasureNumber = function(m) {
var num = this.measureNumbers[m];
return isNaN(num) ? null : num;
}
};

Vex.Flow.Backend.MusicXML.prototype.getMeasure = function(m) {
var measure_attrs = this.getAttributes(m, 0);
Expand All @@ -185,29 +185,33 @@ Vex.Flow.Backend.MusicXML.prototype.getMeasure = function(m) {
var numVoices = 1; // can expand dynamically
var barlines = this.measures[m][p].getElementsByTagName("barline");
// BARLINES
if(barlines.length > 0) {
if(barlines.length > 0)
{
var bars = {};
for(var b=0; b<barlines.length; b++){
for(var b=0; b<barlines.length; b++)
{
var bar = barlines[b];
var barObj = {};

if(bar.getAttribute("location")) barObj.location = bar.getAttribute("location");
Array.prototype.forEach.call(bar.childNodes,function(node){
switch(node.nodeName){
case "bar-style":
barObj.barStyle = node.textContent;
break;
case "repeat":
barObj.direction = node.getAttribute("direction");
break;
}
});

if(bar.getAttribute("location"))
{
barObj.location = bar.getAttribute("location");
}
Array.prototype.forEach.call(
bar.childNodes,
function(node)
{
switch(node.nodeName)
{
case "bar-style":
barObj.barStyle = node.textContent;
break;
case "repeat":
barObj.repeatDirection = node.getAttribute("direction");
break;
}
});
part.bars = barObj;
}

part.bars = barObj;
}
var noteElems = this.measures[m][p].getElementsByTagName("note");
var voiceObjects = new Array(); // array of arrays
Expand Down Expand Up @@ -464,7 +468,7 @@ Vex.Flow.Backend.MusicXML.prototype.parseNote = function(noteElem, attrs) {
case "bass": noteObj.keys = ["D/3"]; break;
case "tenor": noteObj.keys = ["A/3"]; break;
case "alto": noteObj.keys = ["C/4"]; break;
case "treble": default: noteObj.keys = ["B/4"]; break;
case "treble": default: noteObj.keys = ["D/5"]; break;
}
}

Expand Down
66 changes: 58 additions & 8 deletions src/stavebarline.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,25 @@ Vex.Flow.Barline = function(type, x) {
}

Vex.Flow.Barline.type = {

/*default (no setting), tick, short, none.*/
SINGLE: 1,
DOUBLE: 2,
END: 3,
REPEAT_BEGIN: 4,
REPEAT_END: 5,
REPEAT_BOTH: 6,
NONE: 7
NONE: 7,

DOTTED: 8,
DASHED: 9,
HEAVY: 10,
DOUBLE_L: 11,
DOUBLE_LH: 12,
DOUBLE_HL: 13,
DOUBLE_H: 14,
TICK: 15,
SHORT: 16
};

Vex.Flow.Barline.prototype = new Vex.Flow.StaveModifier();
Expand All @@ -38,16 +50,32 @@ Vex.Flow.Barline.prototype.setX = function(x) {this.x = x; return this;}

// Draw barlines
Vex.Flow.Barline.prototype.draw = function(stave, x) {
if (!stave.context) throw new Vex.RERR("NoCanvasContext",
"Can't draw stave without canvas context.");
// x :: the right shift if the stave has clef, time sig, etc.
switch (this.barline) {
case Vex.Flow.Barline.type.DOTTED:
case Vex.Flow.Barline.type.TICK:
case Vex.Flow.Barline.type.SHORT:
case Vex.Flow.Barline.type.DASHED:
case Vex.Flow.Barline.type.SINGLE:
this.drawVerticalBar(stave, this.x, false);
break;
case Vex.Flow.Barline.type.HEAVY:
this.drawHeavyVerticalBar(stave, this.x, false);
break;
case Vex.Flow.Barline.type.DOUBLE:
case Vex.Flow.Barline.type.DOUBLE_L:
this.drawVerticalBar(stave, this.x, true);
break;
case Vex.Flow.Barline.type.DOUBLE_H:
this.drawHeavyVerticalBar(stave, this.x, true);
break;
case Vex.Flow.Barline.type.END:
this.drawVerticalEndBar(stave, this.x);
case Vex.Flow.Barline.type.DOUBLE_LH:
this.drawVerticalEndBar(stave, this.x);
break;
case Vex.Flow.Barline.type.DOUBLE_HL:
break;
case Vex.Flow.Barline.type.REPEAT_BEGIN:
this.drawRepeatBar(stave, this.x, true);
Expand All @@ -63,7 +91,7 @@ Vex.Flow.Barline.prototype.draw = function(stave, x) {
// Default is NONE, so nothing to draw
break;
}
}
};

Vex.Flow.Barline.prototype.drawVerticalBar = function(stave, x, double_bar) {
if (!stave.context) throw new Vex.RERR("NoCanvasContext",
Expand All @@ -73,7 +101,17 @@ Vex.Flow.Barline.prototype.drawVerticalBar = function(stave, x, double_bar) {
if (double_bar)
stave.context.fillRect(x - 3, top_line, 1, bottom_line - top_line + 1);
stave.context.fillRect(x, top_line, 1, bottom_line - top_line + 1);
}
};

Vex.Flow.Barline.prototype.drawHeavyVerticalBar = function(stave, x, double_bar) {
if (!stave.context) throw new Vex.RERR("NoCanvasContext",
"Can't draw stave without canvas context.");
var top_line = stave.getYForLine(0);
var bottom_line = stave.getYForLine(stave.options.num_lines - 1);
if (double_bar)
stave.context.fillRect(x - 5, top_line, 3, bottom_line - top_line + 1);
stave.context.fillRect(x, top_line, 3, bottom_line - top_line + 1);
};

Vex.Flow.Barline.prototype.drawVerticalEndBar = function(stave, x) {
if (!stave.context) throw new Vex.RERR("NoCanvasContext",
Expand All @@ -83,7 +121,18 @@ Vex.Flow.Barline.prototype.drawVerticalEndBar = function(stave, x) {
var bottom_line = stave.getYForLine(stave.options.num_lines - 1);
stave.context.fillRect(x - 5, top_line, 1, bottom_line - top_line + 1);
stave.context.fillRect(x - 2, top_line, 3, bottom_line - top_line + 1);
}
};

Vex.Flow.Barline.prototype.drawVerticalStartBar = function(stave, x) {
if (!stave.context) throw new Vex.RERR("NoCanvasContext",
"Can't draw stave without canvas context.");

var top_line = stave.getYForLine(0);
var bottom_line = stave.getYForLine(stave.options.num_lines - 1);
stave.context.fillRect(x - 5, top_line, 3, bottom_line - top_line + 1);
stave.context.fillRect(x, top_line, 1, bottom_line - top_line + 1);
};


Vex.Flow.Barline.prototype.drawRepeatBar = function(stave, x, begin) {
if (!stave.context) throw new Vex.RERR("NoCanvasContext",
Expand Down Expand Up @@ -118,14 +167,15 @@ Vex.Flow.Barline.prototype.drawRepeatBar = function(stave, x, begin) {
(stave.options.spacing_between_lines_px / 2);
var dot_y = top_line + y_offset + (dot_radius / 2);

// draw the top repeat dot
stave.context.beginPath();
stave.context.arc(dot_x, dot_y, dot_radius, 0, 6.283185 , false);
stave.context.arc(dot_x, dot_y, dot_radius, 0, Math.PI * 2, false);
stave.context.fill();

//draw the bottom repeat dot
dot_y += stave.options.spacing_between_lines_px;
stave.context.beginPath();
stave.context.arc(dot_x, dot_y, dot_radius, 0, 6.283185, false);
stave.context.arc(dot_x, dot_y, dot_radius, 0, Math.PI * 2, false);
stave.context.fill();

}
};

0 comments on commit 6f5d4ed

Please sign in to comment.