gerhardsletten / react-reader

An ePub-reader for React, powered by Epub.js
https://react-reader.metabits.no
Apache License 2.0
676 stars 130 forks source link

Overriding Reader Fonts #177

Closed alphonso06 closed 3 months ago

alphonso06 commented 4 months ago

Summary

I'm trying to use custom fonts, both from the source directory and fonts from Google (mostly as a test to see if it will work).

I read both this issue and this one. Based from experience (local machine), I tried using the same Open Dyslexic font which is not installed on my Windows machine, and found that it works.

image

However, using any other fonts that aren't considered web-safe results in the default Times New Roman typeface. Similar case for my Linux machine. All mobile test devices simply don't change fonts at all.

Example Project

This example project is configured to attempt to use the custom Ubuntu Mono font from Google.

Currently it only displays Times New Roman on all my desktop machines, and refuses to change on mobile devices.

Possible Solutions

I'm not sure if it's viable to surgically insert the custom font into every .epub file that requires non-web-safe typefaces. Good alternative might be to just use web-safe fonts, but I might not have this luxury.

Anyway, I'd love to know if I missed anything, or if I could improve the implementation.

--EDIT-- I found that I actually had the Open Dyslexic font installed on my Windows machine. It's now back to Times New Roman when changing fonts.

alphonso06 commented 4 months ago

It might be possible to apply fonts via CDN as demonstrated here.

Though I attempted to do the same in the stackblitz sample project, the Rendition's document body seems to be undefined, causing a white screen of death.

// In the getRendition prop of ReactReader
let el = _rendition
            .getContents()
            .document.body.appendChild(
              _rendition.getContents().document.createElement('link')
            );
          el.setAttribute('rel', 'preload');
          el.setAttribute('as', 'font');
          el.setAttribute('type', 'font/woff');
          el.setAttribute('crossorigin', 'anonymous');
          el.setAttribute(
            'href',
            'https://fonts.cdnfonts.com/s/29616/open-dyslexic.woff'
            );
gerhardsletten commented 3 months ago

@alphonso06

Adding fonts to a html document require that you add a @font-face tag, and not just loading the font-file.

Added an example for this here

Source code

naresh-intra commented 2 months ago

Tried the example, but it doesn't seem to apply the OpenDyslexia font to the reader content @gerhardsletten

gerhardsletten commented 2 months ago

It should look like this:

image
naresh-intra commented 2 months ago
Screenshot 2024-06-21 at 2 46 12 PM

It doesn't seem to apply. Arial renders fine. Am i missing something?

gerhardsletten commented 2 months ago

Strange, works in both Chrome, Safari and Firefox here. Do you see any errors in your console?

Also, check your network-tab to check if the font is loading there

image
naresh-intra commented 2 months ago

The font download is failing with 403

Screenshot 2024-06-21 at 3 08 29 PM

I tried on multiple systems/browsers and mobile browser as well

gerhardsletten commented 2 months ago

Okay, I have moved the font into the repo so its loaded from the same server as the demo, do it work now?

naresh-intra commented 2 months ago

ya the example works now, thanks!