MobileRead Forums

MobileRead Forums (https://www.mobileread.com/forums/index.php)
-   Plugins (https://www.mobileread.com/forums/forumdisplay.php?f=268)
-   -   [Plugin] SubsetFonts - Simple font subsetter (https://www.mobileread.com/forums/showthread.php?t=294150)

Doitsu 01-22-2018 06:14 PM

[Plugin] SubsetFonts - Simple font subsetter
 
1 Attachment(s)
Last update: January 5, 2021
Current Version: "0.2.4"

This plugin is a simple fontTools wrapper, which'll subset all fonts in an epub.

Please note that the plugin will delete the original fonts and replace them with the subset fonts. Make sure to create a backup copy before running this plugin!

Credits: The plugin uses Alberto Pettarin's glyphIgo library to calculate the number of uninque characters in the book and the fontTools library to do the actual subsetting. The plugin icon (for Sigil 0.9.9.1 and higher) was created by BeckyEbook.

System requirements:

This plugin requires Sigil 0.9.5 or higher. If you're using an external Python interpreter, you'll also need to install the fontTools Python library.

Installation:

1. Select Manage Plugins from the Plugins menu and select Use Bundled Python, if it isn't already selected.

2. Click Add Plugin and select SubsetFonts_v0.2.4.zip. This will install the SubsetFonts plugin, which you can select via Plugins > Edit > SubsetFonts.

Troubleshooting:

By default, the plugin will check the fontTools PyPi website for updates and automatically update the fontTools package bundled with the plugin.
If the update check causes problems on your machine or if you'd rather update the embedded fontTools library yourself, change the following setting in SubsetFonts.json:

Code:

  "update_check": false,
If the update fails, visit the fontTools website, download the latest *.whl file, rename it to *.zip, unzip it and overwrite the plugin fontTools folder with the fontTools folder from the .whl file.

(Linux users may need to manually update the installed fontTool package with pip.)

License: GNU General Public License v3 (GPL-3)

roger64 01-22-2018 10:56 PM

:thumbsup:

Thanks for making these two efficient tools so easily available!!

I allowed myself to add some commentaries on the "Workshop" forum to signal that research is still on its way.

PoP 01-26-2018 02:29 PM

Very nice. Works better at subsetting than anything I had used before. Thank you.

st_albert 02-10-2018 01:54 PM

I got curious about font subsetting, which I usually don't do, because I usually don't embed fonts in the first place.

I got the following error(s):
Spoiler:
Code:

Status: failed

Using external Python interpreter.

fontTools version: 3.21.2

Characters used in this epub:

 !"#$%'()*,-./0123456789:;?@ABCDEFGHIJKLMNOPQRSTUVWXY[]abcdefghijklmnopqrstuvwxyz —

Processing font:  DejaVuSans.ttf...
Original file size:  756072
/usr/local/share/sigil/plugin_launchers/python/sigil_bs4/__init__.py:187: UserWarning: No parser was explicitly specified, so I'm using the best available HTML parser for this system ("lxml"). This usually isn't a problem, but if you run this code on another system, or in a different virtual environment, it may use a different parser and behave differently.

To get rid of this warning, change this:

 BeautifulSoup([your markup])

to this:

 BeautifulSoup([your markup], "lxml")

  markup_type=markup_type))
Traceback (most recent call last):
  File "/usr/local/share/sigil/plugin_launchers/python/launcher.py", line 135, in launch
    self.exitcode = target_script.run(container)
  File "/home/u838190/.local/share/sigil-ebook/sigil/plugins/SubsetFonts/plugin.py", line 89, in run
    subset_font_size = os.path.getsize(subset_font_path)
  File "/usr/lib/python3.5/genericpath.py", line 50, in getsize
    return os.stat(filename).st_size
FileNotFoundError: [Errno 2] No such file or directory: '/tmp/Sigil-6pXaXW/OEBPS/Fonts/DejaVuSans.subset.ttf'
Error: [Errno 2] No such file or directory: '/tmp/Sigil-6pXaXW/OEBPS/Fonts/DejaVuSans.subset.ttf'



I'm running linux, Kubuntu 16.04.3, Sigil 0.9.9 compiled from source, Qt 5.6.1. I believe I have all the needed python modules installed.

Running pyftsubset from the command line, using the above character string supplied in a file, runs successfully.

I must be missing something. Probably something obvious.

Albert

Doitsu 02-10-2018 02:42 PM

Quote:

Originally Posted by st_albert (Post 3655019)
I must be missing something. Probably something obvious.

No, this is definitely my fault. I mostly tested this plugin on my Windows machine and didn't check for Linux pyftsubset conversion errors.
Can you please zip up your version of DejaVuSans and attach it to your post?
(My DejaVuSans version has a different file size.)

st_albert 02-10-2018 06:20 PM

1 Attachment(s)
Quote:

Originally Posted by Doitsu (Post 3655034)
Can you please zip up your version of DejaVuSans and attach it to your post?
(My DejaVuSans version has a different file size.)

Here you go. Note that ark crashed at the end, after creating the archive, but the font seems OK.

Doitsu 02-10-2018 07:05 PM

Quote:

Originally Posted by st_albert (Post 3655113)
Here you go. Note that ark crashed at the end, after creating the archive, but the font seems OK.

Unfortunately, I'm not able to reproduce this error. I'm getting the following message:

Spoiler:
Code:

Status: success

Using external Python interpreter.

fontTools version: 3.21.2

Characters used in this epub:

 !"#$%'()*,-./0123456789:;?@ABCDEFGHIJKLMNOPQRSTUVWXY[]abcdefghijklmnopqrstuvwxyz—

Processing font:  DejaVuSans.ttf...
Original file size:  756072
Subset file size:  22752 (3.01%)
Original font replaced with subset font.

Click OK to close.



Try the following:

change line #51 in plugin.py from:

Code:

text += BeautifulSoup(html).text
to:
Code:

text += BeautifulSoup(html,'html.parser').text
This should suppress the phony bs4 error message. Also please check if your DejaVuSans font files are read-only and/or obfuscated or if your temp folder requires special permissions.

IIRC, you also have a Windows machine. Please try to subset the same file on your Windows machine. This will use a Python library instead of the pyftsubset command line tool and you'll hopefully get a meaningful error message.

I'll look into this in more detail either tomorrow or on Monday.

st_albert 02-10-2018 11:22 PM

Thanks. Will do, probably tomorrow.

Albert

st_albert 02-11-2018 01:26 PM

After making the suggested change in plugin.py and changing permissions on DejaVuSans.ttf to 666 (was 664) I still get the file not found error...
Spoiler:
Code:

Status: failed

Using external Python interpreter.

fontTools version: 3.21.2

Characters used in this epub:

 !"#$%'()*,-./0123456789:;?@ABCDEFGHIJKLMNOPQRSTUVWXY[]abcdefghijklmnopqrstuvwxyz —

Processing font:  DejaVuSans.ttf...
Original file size:  756072
Traceback (most recent call last):
  File "/usr/local/share/sigil/plugin_launchers/python/launcher.py", line 135, in launch
    self.exitcode = target_script.run(container)
  File "/home/u838190/.local/share/sigil-ebook/sigil/plugins/SubsetFonts/plugin.py", line 89, in run
    subset_font_size = os.path.getsize(subset_font_path)
  File "/usr/lib/python3.5/genericpath.py", line 50, in getsize
    return os.stat(filename).st_size
FileNotFoundError: [Errno 2] No such file or directory: '/tmp/Sigil-2PHn8A/OEBPS/Fonts/DejaVuSans.subset.ttf'
Error: [Errno 2] No such file or directory: '/tmp/Sigil-2PHn8A/OEBPS/Fonts/DejaVuSans.subset.ttf'



but checking the temporary files,
Code:

$ ls -la /tmp/Sigil-2PHn8A/OEBPS/Fonts
total 772
drwxrwxr-x 2 u838190 u838190  4096 Feb 11 11:41 .
drwxrwxr-x 9 u838190 u838190  4096 Feb 11 11:37 ..
-rw-rw-rw- 1 u838190 u838190 756072 Feb 11 11:38 DejaVuSans.ttf
-rw-rw-r-- 1 u838190 u838190  21652 Feb 11 11:41 DejaVuSans.ttf.subset

so evidently pyftsubset was able to write the subsetted font to the disk, using its default naming.

so at line 88 in plugin.py I changed:
Code:

subset_font_path = original_font_path.replace(ext, '.subset' + ext)
to:
Code:

subset_font_path = original_font_path.replace(ext, ext + '.subset')]
and lo and behold,
Code:

Status: success

Using external Python interpreter.

fontTools version: 3.21.2

Characters used in this epub:

 !"#$%'()*,-./0123456789:;?@ABCDEFGHIJKLMNOPQRSTUVWXY[]abcdefghijklmnopqrstuvwxyz —

Processing font:  DejaVuSans.ttf...
Original file size:  756072
Subset file size:  21652 (2.86%)
Original font replaced with subset font.

Click OK to close.

The epub size was reduced from ~408 kB before subsetting to ~52.4 kB afterwards.
For comparison, subsetting with Calibre's epub editor gave ~111.5 kB
and with no fonts embedded, it was 38.6 kB

Thanks for the pointers, without which I probably wouldn't have known where to begin.

Albert

Doitsu 02-11-2018 01:47 PM

Quote:

Originally Posted by st_albert (Post 3655442)
Thanks for the pointers, without which I probably wouldn't have known where to begin.

Thank you for your investigation! :thanks: I'm glad that you got it working.

I'll have to run some more tests tomorrow to figure out why the original code didn't work for you but worked fine on my Arch Linux and Elementary OS machines.

Maybe your pyftsubset version was compiled differently or, more likely, on my Linux machines the embedded Python library was used instead of the command line tool. Either way I'll post a new version on Monday.

st_albert 02-11-2018 03:15 PM

FYI:

$ apt-cache policy fonttools
fonttools:
Installed: 3.0-1
Candidate: 3.0-1
Version table:
*** 3.0-1 500
500 http://us.archive.ubuntu.com/ubuntu xenial/universe amd64 Packages
500 http://us.archive.ubuntu.com/ubuntu xenial/universe i386 Packages
100 /var/lib/dpkg/status

Should have mentioned this earlier.

Doitsu 02-12-2018 11:08 AM

@st_albert: Apparently the fontTools developers changed the default subset font file naming scheme in fontTools version 3.21.2, which I used for testing.

The fontTools 3.0.1 Debian package provided by Ubuntu uses DejaVuSans.ttf.subset, however, the latest fontTools 3.21.2 version (installed via pip3) uses DejaVuSans.subset.ttf. I've updated the plugin code to also look for subset font files with the older naming scheme.

BTW, since the Ubuntu fontTools package is totally outdated, you might want to consider uninstalling it and installing the latest fontTools version via pip/pip3.

st_albert 02-14-2018 01:37 PM

Quote:

Originally Posted by Doitsu (Post 3655859)
BTW, since the Ubuntu fontTools package is totally outdated, you might want to consider uninstalling it and installing the latest fontTools version via pip/pip3.

Yes, much better. It works with both the original 0.1.0 version and the newer 01.1.

Thanks again!

Albert.

Doitsu 04-08-2018 01:52 AM

I've attached a new version with the latest fontTools version (3.25.0) to the first post. The new version will also check the fontTools GitHub website for updates and automatically update the bundled fontTools library.

bravosx 11-08-2018 07:27 AM

The plugin does not work. From 7 days there is a new version of fonttools 3.32.0 but there is no * whl file
Doitsu, can you help it?
Thanks you for the plugin, it is very useful for me.

bravosx


All times are GMT -4. The time now is 06:50 PM.

Powered by: vBulletin
Copyright ©2000 - 3.8.5, Jelsoft Enterprises Ltd.
MobileRead.com is a privately owned, operated and funded community.