You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
50 lines
1.3 KiB
JavaScript
50 lines
1.3 KiB
JavaScript
'use strict';
|
|
|
|
module.exports = function layoutItems(lines, options = {}) {
|
|
let currentYOffset = options.initialY;
|
|
let currentXOffset = 0;
|
|
let debugYOffset = 0;
|
|
let debugLines = [];
|
|
|
|
let positionedItems = lines.reduce((items, line, i) => {
|
|
debugLines.push({color: "red", y: debugYOffset, lineNumber: i});
|
|
debugLines.push({color: "green", y: currentYOffset, lineNumber: i});
|
|
debugLines.push({color: "orange", y: currentYOffset + line.adjustedHeight, lineNumber: i});
|
|
|
|
let alignmentOffset;
|
|
|
|
if (line.alignment === "center") {
|
|
alignmentOffset = (options.totalTextWidth - line.width) / 2;
|
|
} else if (line.alignment === "right") {
|
|
alignmentOffset = (options.totalTextWidth - line.width);
|
|
} else {
|
|
alignmentOffset = 0;
|
|
}
|
|
|
|
let newItems = items.concat(line.items.map((item) => {
|
|
let x = currentXOffset + alignmentOffset;
|
|
let y = currentYOffset - (line.minAscender / 2) + (line.adjustedHeight / 2)
|
|
|
|
debugLines.push({color: "blue", y: y, x1: x, x2: x + item.measurements.width, lineNumber: i});
|
|
|
|
currentXOffset += item.measurements.width;
|
|
|
|
return Object.assign({
|
|
x: x,
|
|
y: y
|
|
}, item);
|
|
}));
|
|
|
|
currentYOffset += line.adjustedHeight;
|
|
debugYOffset += line.adjustedHeight;
|
|
currentXOffset = 0;
|
|
|
|
return newItems;
|
|
}, []);
|
|
|
|
return {
|
|
positionedItems: positionedItems,
|
|
debugLines: debugLines
|
|
}
|
|
};
|