@stringsync/vexml
Version:
MusicXML to Vexflow
99 lines (98 loc) • 4.34 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.DefaultFormatter = void 0;
const rendering = __importStar(require("../rendering"));
const util = __importStar(require("../util"));
const config_1 = require("../config");
const debug_1 = require("../debug");
const panoramicformatter_1 = require("./panoramicformatter");
/**
* A formatter that splits the score into systems based on the width of the measures.
*/
class DefaultFormatter {
config;
log;
constructor(opts) {
this.config = { ...config_1.DEFAULT_CONFIG, ...opts?.config };
this.log = opts?.logger ?? new debug_1.NoopLogger();
util.assertNotNull(this.config.WIDTH, 'WIDTH must be set for DefaultFormatter');
}
format(document) {
const clone = document.clone();
// First, ensure the document is formatted for infinite x-scrolling. This will allow us to measure the width of the
// measures and make decisions on how to group them into systems.
const panoramicConfig = { ...this.config, WIDTH: null, HEIGHT: null };
const panoramicFormatter = new panoramicformatter_1.PanoramicFormatter({ config: panoramicConfig });
const panoramicDocument = new rendering.Document(panoramicFormatter.format(document));
const panoramicScoreRender = new rendering.Score(panoramicConfig, this.log, panoramicDocument, null).render();
const slices = this.getSystemSlices(this.config, panoramicScoreRender);
this.applySystemSlices(clone, slices);
return clone;
}
getSystemSlices(config, scoreRender) {
const slices = [{ from: 0, to: 0 }];
let remaining = config.WIDTH;
let count = 0;
const measureRenders = scoreRender.systemRenders.flatMap((systemRender) => systemRender.measureRenders);
for (let measureIndex = 0; measureIndex < measureRenders.length; measureIndex++) {
const measure = measureRenders[measureIndex];
const required = measure.rect.w;
if (required > remaining && count > 0) {
slices.push({ from: measure.absoluteIndex, to: measure.absoluteIndex });
remaining = config.WIDTH;
count = 0;
}
slices.at(-1).to = measure.absoluteIndex;
remaining -= required;
count++;
}
this.log.debug(`grouped ${measureRenders.length} measures into ${slices.length} system(s)`);
return slices;
}
applySystemSlices(document, slices) {
const measures = document.score.systems.flatMap((s) => s.measures);
document.score.systems = [];
for (const slice of slices) {
const system = {
type: 'system',
measures: new Array(),
};
system.measures = measures.slice(slice.from, slice.to + 1);
document.score.systems.push(system);
}
}
}
exports.DefaultFormatter = DefaultFormatter;