|
|
var getLeftEdgeOfStaff = require('./get-left-edge-of-staff'); |
|
|
|
|
|
function layoutInGrid(renderer, staffGroup, timeBasedLayout) { |
|
|
var leftEdge = getLeftEdgeOfStaff(renderer, staffGroup.getTextSize, staffGroup.voices, staffGroup.brace, staffGroup.bracket); |
|
|
var ret = getTotalDuration(staffGroup, timeBasedLayout.minPadding) |
|
|
var totalDuration = ret.totalDuration |
|
|
var minSpacing = ret.minSpacing |
|
|
var totalWidth = minSpacing * totalDuration |
|
|
if (timeBasedLayout.minWidth) |
|
|
totalWidth = Math.max(totalWidth, timeBasedLayout.minWidth) |
|
|
var leftAlignPadding = timeBasedLayout.minPadding ? timeBasedLayout.minPadding/2 : 2 |
|
|
|
|
|
staffGroup.startx = leftEdge |
|
|
staffGroup.w = totalWidth + leftEdge |
|
|
for (var i = 0; i < staffGroup.voices.length; i++) { |
|
|
var voice = staffGroup.voices[i] |
|
|
voice.startx = leftEdge |
|
|
voice.w = totalWidth + leftEdge |
|
|
|
|
|
var x = leftEdge |
|
|
var afterFixedLeft = false |
|
|
var durationUnit = 0 |
|
|
for (var j = 0; j < voice.children.length; j++) { |
|
|
var child = voice.children[j] |
|
|
if (!afterFixedLeft) { |
|
|
if (child.duration !== 0) { |
|
|
|
|
|
afterFixedLeft = true |
|
|
durationUnit = (totalWidth + leftEdge - x) / totalDuration |
|
|
staffGroup.gridStart = x |
|
|
} else { |
|
|
|
|
|
child.x = x |
|
|
x += child.w + child.minspacing |
|
|
} |
|
|
} |
|
|
if (afterFixedLeft) { |
|
|
if (timeBasedLayout.align === 'center') |
|
|
child.x = x + (child.duration * durationUnit) / 2 - child.w / 2 |
|
|
else { |
|
|
|
|
|
|
|
|
if (child.duration === 0) { |
|
|
child.x = x + 1 - child.w |
|
|
} else { |
|
|
|
|
|
child.x = x + leftAlignPadding - child.extraw |
|
|
} |
|
|
} |
|
|
x += child.duration * durationUnit |
|
|
} |
|
|
for (var k = 0; k < child.children.length; k++) { |
|
|
var grandchild = child.children[k] |
|
|
|
|
|
var dx = grandchild.dx ? grandchild.dx : 0 |
|
|
grandchild.x = child.x + dx |
|
|
} |
|
|
} |
|
|
staffGroup.gridEnd = x |
|
|
} |
|
|
return totalWidth |
|
|
} |
|
|
|
|
|
function getTotalDuration(staffGroup, timeBasedLayout) { |
|
|
var maxSpacing = 0 |
|
|
var maxCount = 0 |
|
|
for (var i = 0; i < staffGroup.voices.length; i++) { |
|
|
var count = 0 |
|
|
var voice = staffGroup.voices[i] |
|
|
for (var j = 0; j < voice.children.length; j++) { |
|
|
var element = voice.children[j] |
|
|
count += element.duration |
|
|
if (element.duration) { |
|
|
var width = (element.w+timeBasedLayout) / element.duration |
|
|
maxSpacing = Math.max(maxSpacing, width) |
|
|
} |
|
|
} |
|
|
maxCount = Math.max(maxCount, count) |
|
|
} |
|
|
return { totalDuration: maxCount, minSpacing: maxSpacing} |
|
|
} |
|
|
|
|
|
module.exports = layoutInGrid; |
|
|
|