// SPDX-License-Identifier: AGPL-3.0-only // Ported from Folia (https://github.com/chthollyphile/folia-major, v0.8.2), // src/components/visualizer/VisualizerSubtitleOverlay.tsx, subtitleFontSizes.ts and the helpers of runtime.ts. // The band at the foot of the stage. Folia shows the sung line's translation there; a line that has none (every // line of the site's lyrics) previews the next two lines instead, small and a little out of focus. // // Changed from Folia: only the default layout (a translation row, else the preview) is carried over. import type { Line, Theme } from './types'; import { colorWithAlpha } from './color'; import { getLineRenderEndTime } from './lyrics/renderHints'; import { Motion } from './motion'; /** A string is shown only if it holds a letter or digit: marker lines ("//", dashes, the interlude dots) never are. */ const hasReadableText = (text?: string | null): boolean => !!text && /[\p{L}\p{N}]/u.test(text); /** With no line active: the last line whose render window has closed (the band keeps its translation up in small gaps). */ export const getRecentCompletedLine = (lines: Line[], currentLineIndex: number, currentTime: number): Line | null => { if (currentLineIndex !== -1 || lines.length === 0) return null; for (let index = lines.length - 1; index >= 0; index -= 1) { if (currentTime > getLineRenderEndTime(lines[index])) return lines[index]; } return null; }; /** The line after the active one; with none active, the first line still ahead. */ export const getUpcomingLine = (lines: Line[], currentLineIndex: number, currentTime: number): Line | null => { if (lines[currentLineIndex]) return lines[currentLineIndex + 1] ?? null; for (const line of lines) if (line.startTime > currentTime) return line; return null; }; export const getUpcomingLines = (lines: Line[], currentLineIndex: number, count = 2): Line[] => ( currentLineIndex < 0 ? [] : lines.slice(currentLineIndex + 1, currentLineIndex + 1 + count) ); const clampSize = (min: number, preferred: number, max: number, scale: number): string => ( `clamp(${(min * scale).toFixed(3)}rem, ${(preferred * scale).toFixed(3)}cqw, ${(max * scale).toFixed(3)}rem)` ); /** The two sizes of the band, the same in every style so it does not jump when the style changes. */ export const resolveSubtitleFontSizes = (lyricsFontScale = 1) => ({ translationFontSize: clampSize(1.125, 2.6, 1.25, lyricsFontScale), upcomingFontSize: clampSize(0.875, 2, 1, lyricsFontScale), }); export class SubtitleOverlay { readonly el: HTMLDivElement; #motion: Motion<'opacity' | 'y'>; #content: HTMLDivElement; #theme: Theme; #options: { hidden?: boolean; opacity?: number; background?: boolean; blur?: boolean; scale?: number } = {}; #key = ''; #shown = false; constructor(theme: Theme) { this.#theme = theme; const el = this.el = document.createElement('div'); el.className = 'folia-subtitle'; el.setAttribute('aria-hidden', 'true'); this.#content = document.createElement('div'); this.#content.className = 'folia-subtitle__content'; el.append(this.#content); this.#motion = new Motion({ opacity: 0, y: 20 }, (values) => { el.style.opacity = String(values.opacity); el.style.transform = `translateY(${values.y}px)`; }); } setTheme(theme: Theme) { this.#theme = theme; this.#key = ''; } /** Folia's settings for the band: hidden, its opacity (0.6), the patch behind the words, the blur of coming lines, its size. */ setOptions(options: { hidden?: boolean; opacity?: number; background?: boolean; blur?: boolean; scale?: number }) { this.#options = options; this.#key = ''; this.el.style.display = options.hidden ? 'none' : ''; if (this.#shown) this.#motion.set({ opacity: options.opacity ?? 0.6 }); } /** Called when the line on screen changes (or the text is switched on or off). */ update(lines: Line[], currentLineIndex: number, time: number, showText = true) { const activeLine = lines[currentLineIndex] ?? null; const recent = getRecentCompletedLine(lines, currentLineIndex, time); const line = activeLine ?? recent; const translation = line?.translation?.trim(); const rows = translation && hasReadableText(translation) && translation !== line?.fullText.trim() ? [translation] : []; const from = currentLineIndex >= 0 || !recent ? currentLineIndex : lines.indexOf(recent); const upcoming = rows.length === 0 && activeLine ? getUpcomingLines(lines, from).filter((next) => hasReadableText(next.fullText)).map((next) => next.fullText) : []; const key = `${showText ? 1 : 0}|${rows.join('\u0001')}|${upcoming.join('\u0001')}`; if (key === this.#key) return; this.#key = key; if (!this.#shown && showText) { this.#shown = true; void this.#motion.to({ opacity: this.#options.opacity ?? 0.6, y: 0 }, { duration: 0.24, ease: 'easeOut' }); } else if (this.#shown && !showText) { this.#shown = false; void this.#motion.to({ opacity: 0, y: 20 }, { duration: 0.24, ease: 'easeOut' }); } const theme = this.#theme; const options = this.#options; const sizes = resolveSubtitleFontSizes(options.scale && options.scale > 0 ? options.scale : 1); const texts = rows.length ? rows : upcoming; this.#content.replaceChildren(); if (!texts.length) return; // A soft patch of the background colour behind the words keeps them readable over the shapes. const glow = document.createElement('div'); glow.className = 'folia-subtitle__glow'; glow.style.background = `radial-gradient(ellipse 115% 130% at center, ${colorWithAlpha(theme.backgroundColor, 0.96)} 0%, ${colorWithAlpha(theme.backgroundColor, 0.78)} 62%, transparent 100%)`; const rowsBox = document.createElement('div'); rowsBox.className = 'folia-subtitle__rows'; for (const text of texts) { const row = document.createElement('p'); row.className = rows.length ? 'folia-subtitle__row' : 'folia-subtitle__row folia-subtitle__row--upcoming'; if (!rows.length && options.blur === false) row.style.filter = 'none'; row.textContent = text; row.style.color = theme.secondaryColor; row.style.fontSize = rows.length ? sizes.translationFontSize : sizes.upcomingFontSize; row.style.fontWeight = rows.length ? '500' : '400'; row.style.textShadow = `0 1px 2px ${colorWithAlpha(theme.backgroundColor, 0.24)}`; rowsBox.append(row); } if (options.background === false) this.#content.append(rowsBox); else this.#content.append(glow, rowsBox); if (rows.length) { rowsBox.animate([{ opacity: 0, transform: 'translateY(10px)' }, { opacity: 1, transform: 'none' }], { duration: 300, easing: 'cubic-bezier(.25, .1, .35, 1)' }); } } destroy() { this.#motion.destroy(); this.el.remove(); } }