@stringsync/vexml
Version:
MusicXML to Vexflow
95 lines (94 loc) • 3.69 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.SystemRenderMover = void 0;
const util = __importStar(require("../util"));
const spatial_1 = require("../spatial");
/**
* After a system is rendered, we may learn there is excess height from its components. This class recursivley moves
* all the rects by the excess height such that we can honor the SYSTEM_MARGIN_BOTTOM configuration without
* re-rendering. This is much faster than re-rendering the system at a different position.
*/
class SystemRenderMover {
moveBy(systemRender, dy) {
const seen = new Set(); // avoid circular references
const move = (obj) => {
if (seen.has(obj)) {
return;
}
seen.add(obj);
if (this.isMovable(obj)) {
obj.rect = obj.rect.translate({ dy });
}
if (this.hasIntrinsicRect(obj)) {
obj.intrinsicRect = obj.intrinsicRect.translate({ dy });
}
if (this.hasPlayableRect(obj)) {
obj.playableRect = obj.playableRect.translate({ dy });
}
if (Array.isArray(obj)) {
for (const item of obj) {
move(item);
}
}
else if (util.isPOJO(obj)) {
for (const key in obj) {
move(obj[key]);
}
}
};
move(systemRender);
// Before finishing, we move the vexflow staves. Since everything is linked to them, this should complete the move.
// Any future supported vexflow object not connected to a stave will need to be moved here.
systemRender.measureRenders
.flatMap((m) => m.fragmentRenders)
.flatMap((e) => e.partRenders)
.flatMap((p) => p.staveRenders)
.map((s) => s.vexflowStave)
.forEach((s) => {
s.setY(s.getY() + dy);
});
}
isMovable(obj) {
return !!obj && obj.rect instanceof spatial_1.Rect;
}
hasIntrinsicRect(obj) {
return !!obj && obj.intrinsicRect instanceof spatial_1.Rect;
}
hasPlayableRect(obj) {
return !!obj && obj.playableRect instanceof spatial_1.Rect;
}
}
exports.SystemRenderMover = SystemRenderMover;