Skip to content

Commit

Permalink
EPUB: Rewrite absolute font sizes
Browse files Browse the repository at this point in the history
  • Loading branch information
AbeJellinek committed Apr 25, 2024
1 parent 224b64e commit 250935a
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 9 deletions.
73 changes: 65 additions & 8 deletions src/dom/epub/lib/sanitize-and-render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export class StyleScoper {
let scopeClass = `__scope_${this._sheets.size}`;
this._sheets.set(css, { scopeClass });

let cssModified = this._rewriteEPUBProperties(css);
let cssModified = rewriteEPUBProperties(css);

let sheet;
let added = false;
Expand Down Expand Up @@ -197,6 +197,11 @@ export class StyleScoper {
if (styleRule.style.fontFamily && /\bmono(space)?\b/i.test(styleRule.style.fontFamily)) {
styleRule.style.setProperty('font-family', styleRule.style.fontFamily, 'important');
}

// If this rule sets a font-size, rewrite it to be relative
if (styleRule.style.fontSize) {
styleRule.style.fontSize = rewriteFontSize(styleRule.style.fontSize);
}
}
else if (rule.constructor.name === 'CSSImportRule') {
let importRule = rule as CSSImportRule;
Expand All @@ -210,13 +215,6 @@ export class StyleScoper {
}
}
}

private _rewriteEPUBProperties(css: string): string {
for (let [re, replacement] of EPUB_CSS_REPLACEMENTS) {
css = css.replace(re, replacement);
}
return css;
}
}

// Mappings based on https://github.com/JayPanoz/postcss-epub-interceptor/blob/1aca86e1f4f996f9ec3a8735bd70e673f3e0f504/index.js
Expand Down Expand Up @@ -260,6 +258,65 @@ const EPUB_CSS_REPLACEMENTS: [RegExp, string][] = Object.entries({
return [[ruleRe, ruleReplacement], ...valueReplacements];
});

function rewriteEPUBProperties(css: string): string {
for (let [re, replacement] of EPUB_CSS_REPLACEMENTS) {
css = css.replace(re, replacement);
}
return css;
}

// Mappings and routine based on
// https://github.com/kovidgoyal/calibre/blob/d1bbe63eb10cbf0abbe56a06fce92ad220de03b0/src/calibre/srv/fast_css_transform.cpp#L191-L210
// Copyright (C) 2008-2024 Kovid Goyal (GPLv3)

/* eslint-disable quote-props */
const BASE_FONT_SIZE = 13;
const DPI = 96;
const PT_TO_PX = DPI / 72;
const PT_TO_REM = PT_TO_PX / BASE_FONT_SIZE;

const FONT_SIZE_REPLACEMENTS: Record<string, string> = {
'xx-small': '0.5rem',
'x-small': '0.625rem',
'small': '0.75rem',
'medium': '1rem',
'large': '1.125rem',
'x-large': '1.5rem',
'xx-large': '2rem',
'xxx-large': '2.55rem',
};

const FONT_SIZE_UNITS: Record<string, number> = {
'mm': 2.8346456693,
'cm': 28.346456693,
'in': 72,
'pc': 12,
'q': 0.708661417325,
'pt': 1,
};
/* eslint-enable quote-props */

function rewriteFontSize(fontSize: string): string {
if (fontSize.toLowerCase() in FONT_SIZE_REPLACEMENTS) {
return FONT_SIZE_REPLACEMENTS[fontSize.toLowerCase()];
}

let match = fontSize.match(/^([0-9.]+)([a-zA-Z]+)$/);
if (match) {
let value = parseFloat(match[1]);
let unit = match[2].toLowerCase();
if (unit === 'px') {
return value / BASE_FONT_SIZE + 'rem';
}
else if (unit in FONT_SIZE_UNITS) {
let factor = FONT_SIZE_UNITS[unit];
return value * factor * PT_TO_REM + 'rem';
}
}

return fontSize;
}

type SheetMetadata = {
sheet?: CSSStyleSheet;
scopeClass: string;
Expand Down
3 changes: 2 additions & 1 deletion src/dom/epub/stylesheets/_content.scss
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
}

:root {
font-size: calc(var(--content-scale) * 13pt) !important;
background-color: var(--background-color) !important;
color: var(--text-color) !important;
}
Expand All @@ -80,7 +81,7 @@ replaced-body {
color: inherit !important;

font-family: var(--content-font-family, "Georgia"), serif;
font-size: calc(var(--content-scale) * 13pt) !important;
font-size: inherit !important;
line-height: var(--content-line-height);
text-align: justify;
text-rendering: optimizeLegibility;
Expand Down

0 comments on commit 250935a

Please sign in to comment.