Skip to content

Commit

Permalink
fix: avoid .ASS-fix-font-size overflow
Browse files Browse the repository at this point in the history
and some refactors
  • Loading branch information
weizhenye committed Jun 24, 2024
1 parent fede529 commit 5ff583d
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 29 deletions.
6 changes: 6 additions & 0 deletions src/global.css
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,14 @@
.ASS-fix-font-size {
font-size: 2048px;
line-height: normal;
width: 0;
height: 0;
position: absolute;
visibility: hidden;
overflow: hidden;
}
.ASS-fix-font-size span {
position: absolute;
}
.ASS-fix-objectBoundingBox {
width: 100%;
Expand Down
5 changes: 5 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* eslint-disable max-len */
import { compile } from 'ass-compiler';
import { setKeyframes } from './renderer/animation.js';
import { $fixFontSize } from './renderer/font-size.js';
import { clear, createResize, createPlay, createPause, createSeek } from './internal.js';
import { createSVGEl, addGlobalStyle } from './utils.js';
Expand Down Expand Up @@ -144,6 +145,10 @@ export default class ASS {
this.#resize();
this.resampling = resampling;

dialogues.forEach((dialogue) => {
setKeyframes(dialogue, styles);
});

const observer = new ResizeObserver(this.#resize);
observer.observe(video);
this.#store.observer = observer;
Expand Down
7 changes: 1 addition & 6 deletions src/internal.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/* eslint-disable no-param-reassign */
import { renderer } from './renderer/renderer.js';
import { setKeyframes } from './renderer/animation.js';
import { batchAnimate } from './utils.js';

export function clear(store) {
Expand Down Expand Up @@ -93,7 +92,7 @@ export function createPause(store) {
}

export function createResize(that, store) {
const { video, box, svg, dialogues } = store;
const { video, box, svg } = store;
return function resize() {
const cw = video.clientWidth;
const ch = video.clientHeight;
Expand Down Expand Up @@ -134,10 +133,6 @@ export function createResize(that, store) {
svg.style.cssText = cssText;
svg.setAttributeNS(null, 'viewBox', `0 0 ${sw} ${sh}`);

dialogues.forEach((dialogue) => {
setKeyframes(dialogue, store);
});

createSeek(store)();
};
}
4 changes: 2 additions & 2 deletions src/renderer/animation.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ function createTransformKeyframes({ fromTag, tag, fragment }) {
}

// TODO: accel is not implemented yet, maybe it can be simulated by cubic-bezier?
export function setKeyframes(dialogue, store) {
export function setKeyframes(dialogue, styles) {
const { start, end, effect, move, fade, slices } = dialogue;
const duration = (end - start) * 1000;
const keyframes = [
Expand All @@ -111,7 +111,7 @@ export function setKeyframes(dialogue, store) {
Object.assign(dialogue, { keyframes });
}
slices.forEach((slice) => {
const sliceTag = store.styles[slice.style].tag;
const sliceTag = styles[slice.style].tag;
slice.fragments.forEach((fragment) => {
if (!fragment.tag.t || fragment.tag.t.length === 0) {
return;
Expand Down
4 changes: 2 additions & 2 deletions src/renderer/clip.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createSVGEl, uuid, vendor } from '../utils.js';
import { createSVGEl, uuid } from '../utils.js';

export function createClipPath(clip, store) {
const sw = store.scriptRes.width;
Expand Down Expand Up @@ -34,7 +34,7 @@ export function createClipPath(clip, store) {
store.defs.append($clipPath);
return {
$clipPath,
cssText: `${vendor.clipPath}clip-path:url(#${id});`,
cssText: `clip-path:url(#${id});`,
};
}

Expand Down
3 changes: 1 addition & 2 deletions src/renderer/dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@ export function createDialogue(dialogue, store) {
if (dialogue.keyframes) {
animations.push(initAnimation($div, dialogue.keyframes, animationOptions));
}
dialogue.animations = animations;
$div.append(df);
return $div;
return { $div, animations };
}
6 changes: 4 additions & 2 deletions src/renderer/font-size.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@

export const $fixFontSize = document.createElement('div');
$fixFontSize.className = 'ASS-fix-font-size';
$fixFontSize.textContent = '0';
const $span = document.createElement('span');
$span.textContent = '0';
$fixFontSize.append($span);

const cache = Object.create(null);

export function getRealFontSize(fn, fs) {
if (!cache[fn]) {
$fixFontSize.style.fontFamily = `font-family:"${fn}",Arial;`;
cache[fn] = fs * 2048 / $fixFontSize.clientHeight;
cache[fn] = fs * 2048 / $span.clientHeight;
}
return cache[fn];
}
4 changes: 2 additions & 2 deletions src/renderer/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import { setTransformOrigin } from './transform.js';
import { getScrollEffect } from './scroll.js';

export function renderer(dialogue, store) {
const $div = createDialogue(dialogue, store);
Object.assign(dialogue, { $div });
const { $div, animations } = createDialogue(dialogue, store);
Object.assign(dialogue, { $div, animations });
store.box.append($div);
const { width } = $div.getBoundingClientRect();
Object.assign(dialogue, { width });
Expand Down
13 changes: 0 additions & 13 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,6 @@ export function createSVGEl(name, attrs = []) {
return $el;
}

function getVendor(prop) {
const { style } = document.body;
const Prop = prop.replace(/^\w/, (x) => x.toUpperCase());
if (prop in style) return '';
if (`webkit${Prop}` in style) return '-webkit-';
if (`moz${Prop}` in style) return '-moz-';
return '';
}

export const vendor = {
clipPath: getVendor('clipPath'),
};

const GLOBAL_CSS = '__GLOBAL_CSS__';
/**
* @param {HTMLElement} container
Expand Down

0 comments on commit 5ff583d

Please sign in to comment.