Skip to content

Commit 39f0bea

Browse files
authored
feat(css) add "Nunito" css font (PR #2035)
1 parent ec61bea commit 39f0bea

File tree

6 files changed

+70
-0
lines changed

6 files changed

+70
-0
lines changed

src/renderer/assets/fonts/nunito.ttf

556 KB
Binary file not shown.

src/renderer/library/components/App.tsx

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import { toastActions } from "readium-desktop/common/redux/actions";
3030
import { ToastType } from "readium-desktop/common/models/toast";
3131

3232
import { acceptedExtensionArray } from "readium-desktop/common/extension";
33+
import Nunito from "readium-desktop/renderer/assets/fonts/nunito.ttf";
3334

3435
export default class App extends React.Component<{}, undefined> {
3536

@@ -89,6 +90,32 @@ export default class App extends React.Component<{}, undefined> {
8990
const history = diLibraryGet("history"); // diRendererSymbolTable.history
9091
const translator = diLibraryGet("translator"); // diRendererSymbolTable.translator
9192

93+
94+
// FIXME: try a better way to import Nunito in CSS font face instead of in React render function.
95+
// One possibility is to add css font in ejs html template file from webpack
96+
try {
97+
const nunitoFontStyleID = "nunitoFontStyleID";
98+
const el = document.getElementById(nunitoFontStyleID);
99+
if (!el) {
100+
const css = `
101+
@font-face {
102+
font-family: "Nunito";
103+
font-style: normal;
104+
font-weight: normal;
105+
src: local("Nunito"),
106+
url("${Nunito}") format("truetype");
107+
}
108+
109+
`;
110+
const el = document.createElement("style");
111+
el.setAttribute("type", "text/css");
112+
el.appendChild(document.createTextNode(css));
113+
document.head.appendChild(el);
114+
}
115+
} catch (e) {
116+
console.error("Nunito font face error", e);
117+
}
118+
92119
return (
93120
<Provider store={store} >
94121
<TranslatorContext.Provider value={translator}>

src/renderer/reader/components/App.tsx

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import ToastManager from "readium-desktop/renderer/common/components/toast/Toast
1717
import { TranslatorContext } from "readium-desktop/renderer/common/translator.context";
1818
import DialogManager from "readium-desktop/renderer/reader/components/dialog/DialogManager";
1919
import { diReaderGet } from "readium-desktop/renderer/reader/di";
20+
import Nunito from "readium-desktop/renderer/assets/fonts/nunito.ttf";
2021

2122
import Reader from "./Reader";
2223

@@ -79,6 +80,8 @@ font-style: normal;
7980
font-weight: normal;
8081
src: local("iAWriterDuospace-Regular"),
8182
url("${rcssPath}/fonts/iAWriterDuospace-Regular.ttf") format("truetype");
83+
}
84+
8285
`;
8386
el = document.createElement("style");
8487
el.setAttribute("id", readiumCssFontFaceStyleID);
@@ -90,6 +93,31 @@ url("${rcssPath}/fonts/iAWriterDuospace-Regular.ttf") format("truetype");
9093
console.log("PROBLEM LOADING READER FONT FACE? ", e);
9194
}
9295

96+
// FIXME: try a better way to import Nunito in CSS font face instead of in React render function.
97+
// One possibility is to add css font in ejs html template file from webpack
98+
try {
99+
const nunitoFontStyleID = "nunitoFontStyleID";
100+
const el = document.getElementById(nunitoFontStyleID);
101+
if (!el) {
102+
const css = `
103+
@font-face {
104+
font-family: "Nunito";
105+
font-style: normal;
106+
font-weight: normal;
107+
src: local("Nunito"),
108+
url("${Nunito}") format("truetype");
109+
}
110+
111+
`;
112+
const el = document.createElement("style");
113+
el.setAttribute("type", "text/css");
114+
el.appendChild(document.createTextNode(css));
115+
document.head.appendChild(el);
116+
}
117+
} catch (e) {
118+
console.error("Nunito font face error", e);
119+
}
120+
93121
return (
94122
<Provider store={store}>
95123
<TranslatorContext.Provider value={translator}>

typings.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,8 @@ declare module "*.svg" {
3535
const props: IProps;
3636
export default props;
3737
}
38+
39+
declare module "*.ttf" {
40+
const value: any;
41+
export default value;
42+
}

webpack.config.renderer-library.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,11 @@ let config = Object.assign(
295295
loader: "svg-sprite-loader",
296296
test: /\.svg$/,
297297
},
298+
{
299+
test: /\.ttf$/,
300+
type: "asset/resource",
301+
},
302+
// useful ?
298303
{
299304
exclude: /src/,
300305
// loader: "file-loader?name=assets/[name].[md5:hash].[ext]",

webpack.config.renderer-reader.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,11 @@ let config = Object.assign(
293293
loader: "svg-sprite-loader",
294294
test: /\.svg$/,
295295
},
296+
{
297+
test: /\.ttf$/,
298+
type: "asset/resource",
299+
},
300+
// useful ?
296301
{
297302
exclude: /src/,
298303
// loader: "file-loader?name=assets/[name].[md5:hash].[ext]",

0 commit comments

Comments
 (0)