UNPKG

@osbjs/components

Version:
85 lines (84 loc) 4.26 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Lyrics = void 0; const osbjs_1 = require("@osbjs/osbjs"); const txtgen_1 = require("@osbjs/txtgen"); class Lyrics extends osbjs_1.Component { constructor(folderPath, osbFolderPath, subtitlePath, options, registerFontOptions) { super(); this.name = 'Lyrics'; this.options = { fadeDuration: 200, opacity: 1, fontName: 'Arial', fontSize: 72, fontScale: 1, perCharacter: false, y: 400, additive: true, color: { r: 255, g: 255, b: 255, }, }; this.folderPath = folderPath; this.osbFolderPath = osbFolderPath; this.options = { ...this.options, ...options }; this._textureGenerator = new txtgen_1.TextureGenerator(folderPath, osbFolderPath, { fontName: this.options.fontName, fontSize: this.options.fontSize }); if (registerFontOptions) this._textureGenerator.registerFont(registerFontOptions.fontPath, registerFontOptions.family); this._subtitleCollection = new osbjs_1.SubtitleCollection(subtitlePath); } generate() { if (this.options.perCharacter) { this._generatePerChar(); } else { this._generatePerLine(); } } _generatePerChar() { this._subtitleCollection.subtitles.forEach((subtitle) => { let letterY = this.options.y; subtitle.text.split('\n').forEach((line) => { let lineWidth = 0, lineHeight = 0; for (let i = 0; i < line.length; i++) { const letter = line[i]; let texture = this._textureGenerator.generateTexture(letter, this.options.color); lineWidth += texture.width * this.options.fontScale; lineHeight = Math.max(lineHeight, texture.height * this.options.fontScale); } let letterX = 320 - lineWidth * 0.5; for (let i = 0; i < line.length; i++) { const letter = line[i]; let texture = this._textureGenerator.generateTexture(letter, this.options.color); let position = new osbjs_1.OsbVector2(letterX, letterY); let sprite = new osbjs_1.Sprite(texture.osbPath, osbjs_1.Layer.Background, osbjs_1.Origin.TopLeft, position); sprite.ScaleAtTime(subtitle.startTime, this.options.fontScale); sprite.Fade(subtitle.startTime - this.options.fadeDuration, subtitle.startTime, 0, this.options.opacity); sprite.Fade(subtitle.endTime - this.options.fadeDuration, subtitle.endTime, this.options.opacity, 0); if (this.options.additive) sprite.Parameter(subtitle.startTime - this.options.fadeDuration, subtitle.endTime, osbjs_1.Parameter.AdditiveBlending); this.registerComponents(sprite); letterX += texture.width * this.options.fontScale; } letterY += lineHeight; }); }); } _generatePerLine() { this._subtitleCollection.subtitles.forEach((line) => { let texture = this._textureGenerator.generateTexture(line.text, this.options.color); let position = new osbjs_1.OsbVector2(320, this.options.y); let sprite = new osbjs_1.Sprite(texture.osbPath, osbjs_1.Layer.Background, osbjs_1.Origin.Center, position); sprite.ScaleAtTime(line.startTime, this.options.fontScale); sprite.Fade(line.startTime - this.options.fadeDuration, line.startTime, 0, this.options.opacity); sprite.Fade(line.endTime - this.options.fadeDuration, line.endTime, this.options.opacity, 0); if (this.options.additive) sprite.Parameter(line.startTime - this.options.fadeDuration, line.endTime, osbjs_1.Parameter.AdditiveBlending); this.registerComponents(sprite); }); } } exports.Lyrics = Lyrics;