Skip to content

Commit

Permalink
fix: set line-height with \fs
Browse files Browse the repository at this point in the history
  • Loading branch information
weizhenye committed Jul 7, 2024
1 parent e0f3d8f commit c26e7ec
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
1 change: 1 addition & 0 deletions src/global.css
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
}
.ASS-fix-font-size {
font-size: 2048px;
font-family: Arial;
line-height: normal;
width: 0;
height: 0;
Expand Down
5 changes: 3 additions & 2 deletions src/renderer/dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ export function createDialogue(dialogue, store) {
let cssText = 'display:inline-block;';
const cssVars = [];
if (!drawing) {
cssText += `line-height:normal;font-family:"${tag.fn}",Arial;`;
cssText += `font-family:"${tag.fn}",Arial;`;
cssText += `font-size:calc(var(--ass-scale) * ${getRealFontSize(tag.fn, tag.fs)}px);`;
cssText += `line-height:calc(var(--ass-scale) * ${tag.fs}px);`;
cssText += `color:${color2rgba(tag.a1 + tag.c1)};`;
const scale = /yes/i.test(info.ScaledBorderAndShadow) ? store.scale : 1;
if (borderStyle === 1) {
Expand Down Expand Up @@ -64,7 +65,7 @@ export function createDialogue(dialogue, store) {
cssText += 'text-wrap:balance;';
}
if (tag.q === 1) {
cssText += 'word-break:break-all;white-space:normal;';
cssText += 'word-break:break-word;white-space:normal;';
}
if (tag.q === 2) {
cssText += 'word-break:normal;white-space:nowrap;';
Expand Down
11 changes: 6 additions & 5 deletions src/renderer/font-size.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ const $span = document.createElement('span');
$span.textContent = '0';
$fixFontSize.append($span);

const cache = Object.create(null);
const unitsPerEm = 2048;
const lineSpacing = Object.create(null);

export function getRealFontSize(fn, fs) {
if (!cache[fn]) {
$fixFontSize.style.fontFamily = `"${fn}",Arial`;
cache[fn] = fs * 2048 / $span.clientHeight;
if (!lineSpacing[fn]) {
$span.style.fontFamily = fn;
lineSpacing[fn] = $span.clientHeight;
}
return cache[fn];
return fs * unitsPerEm / lineSpacing[fn];
}

0 comments on commit c26e7ec

Please sign in to comment.