MobileRead Forums

MobileRead Forums (https://www.mobileread.com/forums/index.php)
-   Plugins (https://www.mobileread.com/forums/forumdisplay.php?f=268)
-   -   Create a CSS-file from fonts (https://www.mobileread.com/forums/showthread.php?t=259980)

rubeus 05-03-2015 02:22 PM

Create a CSS-file from fonts
 
Hi,

i created a very very simple plugin. It just iterates through the font files and creates a StyleFont.css file in the book.

DiapDealer 05-03-2015 03:05 PM

Cool. I added your plugin to the sticky list of plugins. Thanks for contributing.

I don't know much about fonts/fontfiles in general, but it does seem like the most straightforward way to manipulate/query font files in Python is through third-party library-modules -- which makes things harder to distribute to other users. I do know calibre does some font manipulation stuff (subsetting and such) perhaps there's something there that can offer some enlightenment.

rubeus 05-09-2015 09:37 AM

New Version in #1

I'm trying to guess some values derived from the filename

rubeus 05-23-2015 06:24 AM

New Version 020 in #1

With the help of Doitso the plugin is able to read the fontname directly from the file. Weight and style are derived from the fontname. If the fontname cant be read, the filename is taken into account.

rubeus 11-30-2015 10:54 AM

The new version 0.3.0 requires Python 3.x and the PIL libraries. This is present if you are using sigil from version 0.9.1 or newer and use the built-in python interpreter

I will leave the old one(0.2.0 for Python 2.7) in the first post, but it will not be maintained any longer (by me)

Leonatus 12-18-2015 09:16 AM

Useful tool, thank you! I hope to avoid error messages by implementing fonts that sometimes occur, in the future!

rubeus 12-19-2015 02:03 PM

Minor Bugfixes in #1

rubeus 01-11-2016 03:51 PM

Additinal bugfix in #1

hantsaniala 01-20-2016 02:05 AM

Hello everyone,

I have following error and I don't know how to fix it :

:help::help::help:

Code:

Status: failed

Traceback (most recent call last):
  File "C:\Python34\lib\site-packages\PIL\ImageFont.py", line 239, in truetype
    return FreeTypeFont(font, size, index, encoding)
  File "C:\Python34\lib\site-packages\PIL\ImageFont.py", line 132, in __init__
    "", size, index, encoding, self.font_bytes)
OSError: unknown file format

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Program Files (x86)\Sigil8\plugin_launchers\\python\launcher.py", line 135, in launch
    self.exitcode = target_script.run(container)
  File "C:\Users\Andry\AppData\Local\sigil-ebook\sigil\plugins\AddStyleFont\plugin.py", line 17, in run
    font_object = ImageFont.truetype(BytesIO(bk.readfile(id)))
  File "C:\Python34\lib\site-packages\PIL\ImageFont.py", line 241, in truetype
    ttf_filename = os.path.basename(font)
  File "C:\Python34\lib\ntpath.py", line 246, in basename
    return split(p)[1]
  File "C:\Python34\lib\ntpath.py", line 217, in split
    d, p = splitdrive(p)
  File "C:\Python34\lib\ntpath.py", line 159, in splitdrive
    if len(p) > 1:
TypeError: object of type '_io.BytesIO' has no len()
Error: object of type '_io.BytesIO' has no len()

Sigil version : 0.8.4 x86
Python version : 3.4.0 x86
OS : Windows 7 x64

Can anyone help me please ? Only one computer generate this error. Others works fine.

Thanks for answer !

rubeus 01-20-2016 03:14 AM

The one throwing the exception does not have the library PIL installed or is not using the internal Python interpreter or has a wrong version of Sigil.

hantsaniala 01-20-2016 04:03 AM

Quote:

Originally Posted by rubeus (Post 3243562)
The one throwing the exception does not have the library PIL installed or is not using the internal Python interpreter or has a wrong version of Sigil.

before using AddStyleFont I have installed PIL via
Code:

pip install Pillow
or is it wrong ?
Other computer with the same installation works fine.

rubeus 01-20-2016 05:21 AM

If they work fine they don't have the same installation.

ErikG 06-10-2016 11:03 AM

There is no longer a download link.
Reason?

KevinH 06-10-2016 11:09 AM

rubeus was banned from MR because of his repeated attacks against newbie posters contrary to repeated requests that he stop. My guess he has removed all of his plugins which is of course his right. I will remove them from the plugin index.
So unless he has posted them on some personal page someplace, you are out of luck.

Sorry about that but we simply can not have newbie questions being verbally attacked in our own help forums.

Doitsu 06-10-2016 12:50 PM

Quote:

Originally Posted by ErikG (Post 3332504)
There is no longer a download link.
Reason?

Some time ago, I designed a similar plugin that is not as fancy as the one by rubeus, but it basically does the same job.

If you want, I can attach it to this thread.

KevinH 06-10-2016 01:38 PM

Hi Doitsu,
Yes, please do.
KevinH

Doitsu 06-10-2016 01:53 PM

1 Attachment(s)
Quote:

Originally Posted by KevinH (Post 3332580)
Hi Doitsu,
Yes, please do.
KevinH

Here you go:

The result should be pretty much similar to rubeus's plugin, but this version will either insert the font css at the beginning of the first stylesheet or create a new stylesheet.

Instructions:
  1. Select Manage Plugins, enable Use Bundled Python, if it isn't already selected, click Add Plugin and the select the zip file.
  2. Select the plugin via Plugins > Edit > CSSFromFonts.

Note: This is a Python 3-only plugin, if you're using the Linux version of Sigil, please download the updated version, which'll automatically select the Python 3 interpreter.

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

theducks 06-10-2016 01:58 PM

I always wondered why an auto @font was not part of the Add (existing files) Fonts process.

The only question was: Where
For me, I like it in page_styles.css

st_albert 06-11-2016 07:55 PM

***** Edited to add: *****
OK, never mind. I ran it on a different Kubuntu version, with a built-from-scratch Sigil (0.9.4, Qt-5.4.1), and it ran fine. I guess I'll be uninstalling that quirky sigil package.

I'll leave this post here just in case someone else runs into the same problem, but moderators please feel free to delete it if you want.

Edit #2. It seems the sigil package was not guilty after all. See below.
***********************


Hmm, I'm having trouble with this plugin. Here's the error message:
Code:

Status: failed

Traceback (most recent call last):
  File "/usr/share/sigil/plugin_launchers/python/launcher.py", line 134, in launch
    target_script = __import__(script_module)
  File "/home/u838190/.local/share/sigil-ebook/sigil/plugins/CSSFromFonts/plugin.py", line 4, in <module>
    from PIL import ImageFont
ImportError: No module named PIL
Error: No module named PIL

But testplugin says I do have PIL installed (as I should).

Code:

...
Verifying proper Python packages are available
  Python Package: PIL Found
  Python Package: cssselect Found
  Python Package: cssutils Found
  Python Package: html5lib Found
  Python Package: lxml Found
  Python Package: regex Found
  Python Package: chardet Found
  Python Package: six Found
...

I am using Sigil version 0.9.5, Qt 5.5.1, from the Ubuntu package for Kubuntu 16.04 LTS (Xenial Xerus).

Code:

apt-cache policy sigil
sigil:
  Installed: 0.9.5+dfsg-0ubuntu1

This Sigil package is known to be somewhat quirky, so maybe that's part of it. Otherwise, could be PEBCAK (i.e. operator error).

Has anyone else run into this?

Albert

DiapDealer 06-11-2016 08:24 PM

It's not a Python 2 vs 3 thing is it? The order the supported engine attribute appears for a plugin affects which Python will be used (should both Py2 and Py3 interpreters be configured). In other words: the plugin in this thread could be using a different Python interpreter than the test plugin is if the supported engine attributes are reversed (and both pythons are configured).

That may not be what's happening here, but that's why I only ever configure one python interpreter at a time.

EDIT: The test plugin only supports python3, and Doitsu's will default to python2 if both are configured in the plugin manager. I get the "no PIL module" error myself if I have both 2 & 3 interpreters configured (since my python 2 environment doesn't have pillow installed. If I unconfigure the python 2 interpreter (plugin manager) it allows Doitsu's plugin to use Python 3 instead -- which does have pillow installed.

Doitsu 06-11-2016 08:24 PM

@Albert: Even though the error message says PIL it's actually looking for Pillow, which is a PIL compatible fork.

Code:

pip list
should list:

Code:

Pillow (3.2.0)
It could also be a Python2/3 issue. If you have paths for both interpreters defined, try deleting the Python 2.7 path and or the Python 3.4 path.

st_albert 06-11-2016 08:57 PM

Quote:

Originally Posted by Doitsu (Post 3333134)
@Albert: Even though the error message says PIL it's actually looking for Pillow, which is a PIL compatible fork.

Code:

pip list
should list:

Code:

Pillow (3.2.0)
It could also be a Python2/3 issue. If you have paths for both interpreters defined, try deleting the Python 2.7 path and or the Python 3.4 path.

Yes, I think it was a python2/3 issue. I had installed python3-pil but not python-pil. After installing the latter, it seems to work.

But I think I'll keep my home-built 0.9.5 for now.

Thanks for the input, both you and DiapDealer!

Albert

Doitsu 06-14-2016 04:28 AM

I got reports that the plugin won't work with Python 2.7 and changed plugin.xml so that it'll select the Python 3.4 interpreter.

If you're a Linux user please download the updated version.

(If you're using the Windows version and the embedded interpreter, you won't have to download this update.)

DiapDealer 06-14-2016 08:48 AM

For the record ... it seemed to work fine for me with Python 2.7. (Linux or Windows) * shrug*

But since Python 3 has been required for (and/or included with) Sigil for quite some time now, I can't really blame anyone for dropping Python 2 support for their plugins either. I did it with my latest; and can't foresee me continuing to jump through the hoops necessary to support both in future plugins.

Those sticking with older versions of Sigil still have the option of installing an external Python 3 interpreter if they can't live without a Python 3-only plugin. So it's not like anyone is being completely left behind or anything.

KevinH 06-14-2016 02:15 PM

Yes I think it is time we retire python 2.7 support. Almost all of the plugins we have will run on both, but our internal python 3.4 (or later) (and linked version on Linux) comes with lxml, html5lib, cssutils, cssselect, Pillow, tk, six, regex and chardet all guaranteed to be there and available for the user.

Supporting Python 2.7 code does not seem to add any real value over and above what python 3.4 provides and removing it would certainly simplify things. Older Sigil users can still use an external Python 3 interface (after manually adding the right modules) to run plugins that are only python3.

So since Doitsu, DiapDealer, Toxaris and myself probably account for the majority of current Sigil plugin developers, are you all okay with moving to Python 3 only after our upcoming Sigil 0.9.6 release?

Please let me know what you think?

Thanks,

KevinH

Doitsu 06-14-2016 02:30 PM

Quote:

Originally Posted by KevinH (Post 3334544)
So since Doitsu, DiapDealer, Toxaris and myself probably account for the majority of current Sigil plugin developers, are you all okay with moving to Python 3 only after our upcoming Sigil 0.9.6 release?

I'm OK with that, because there really is no benefit in maintaining Python 2.7 compatibility, especially since the Windows version comes with a Python 3.4 interpreter and all required libraries.

Also the last time I checked, only one or two very old plugins with relatively low download counts supported only Python 2.7.
I.e., this move won't cause problems for the majority of end users, because all existing plugins will automatically fall back to Python 3.4 in the absence of a Python 2.7 path.

DiapDealer 06-14-2016 02:50 PM

The only conceivable snag I can think of would be people who may be using custom--but unshared--plugins that require Python 2.7.x. If there are any, though, I can't imagine it affecting very many at all. And I'd be more than willing to help anyone convert such plugins to Python 3 if they came forward.

The only question would be how to handle python2.7-only plugins moving forward. Do we simply allow them to error out, or display a message that the plugin needs to be updated to Python 3 before it will work (or perhaps simply display them in the Plugin Manager in red and allow them to error)?

If we do remove 2.7 support (and I think I'm for it), I'm assuming the "engine" tag in the plugin.xml file would become superfluous? It certainly wouldn't need to be displayed in the Plugin Manager anymore.

Doitsu 06-14-2016 03:02 PM

Quote:

Originally Posted by DiapDealer (Post 3334576)
The only question would be how to handle python2.7-only plugins moving forward. Do we simply allow them to error out, or display a message that the plugin needs to be updated to Python 3 before it will work (or perhaps simply display them in the Plugin Manager in red and allow them to error)?


@DiapDealer:
I had another look at the official plugin list and found only one plugin that requires Python 2.7: smoothRemove, which the author seems to have abandoned. (He hasn't logged in since 2014.)

It's also a tad too dangerous for my taste, but that's a different matter.
I'm sure that either you or KevinH could write a much safer Python 3.4-compatible replacement in 10 minutes or so. :-)

KevinH 06-14-2016 03:20 PM

Yes, I will happily help fix any python 2.7 plugins to work with python 3 (even private ones).

If we remove "engine" from the Plugin Management table, it would free up enough space to add either "oslist" (which is kind of meaningless since that is checked to make sure it matches when the plugin is installed) or better I think to add the "description" field to help users remember just what the hell that plugin does.

We could then add in support for auto sorting (and remember to disable it when adding or deleting items in the same row to prevent the row form moving out from under you) when we update things.

Also removing the >= Python 2.7 lines in the Dialog will free up enough space to add one or even two more user-assignable icons to plugins (and therefore shortcut keys to launch them).

How does that sound?

KevinH

DiapDealer 06-14-2016 04:11 PM

Sounds fine to me.

Doitsu 06-14-2016 04:15 PM

Sounds fine to me, too.

Ghod 03-01-2018 02:29 AM

Hmmm...just installed and ran this plugin, but after running it, clicking on close just made it run again. Clicking on the X to close the window did the same. What did work, was to hit ESC (a few times--perhaps overkill, but it was preventing me from checking its work and saving the file).

I'm running Sigil 0.9.9 on Win10 x64.

This is actually the very first time I've installed and run a plugin. Mostly because it's taken me forever to get around to try building an epub from scratch. I'm trying to get my act together so I can provide my blind friend books he has no access to otherwise.

Doitsu 03-01-2018 04:38 AM

Quote:

Originally Posted by Ghod (Post 3663756)
Hmmm...just installed and ran this plugin, but after running it, clicking on close just made it run again.

This is a rare bug that only occurs if you install a plugin for the first time, assign it to a button and then click that button.
For the time being, the only way to fix it is to restart Sigil.

Quote:

Originally Posted by Ghod (Post 3663756)
I'm trying to get my act together so I can provide my blind friend books he has no access to otherwise.

You might want to check out KevinH's Access-Aide plugin, which'll automatically add DAISY-recommended attributes for screen readers.

Ghod 04-09-2018 07:41 PM

Quote:

Originally Posted by Doitsu (Post 3663785)
This is a rare bug that only occurs if you install a plugin for the first time, assign it to a button and then click that button.
For the time being, the only way to fix it is to restart Sigil.

So I probably shouldn't see it pop up again...

You might want to check out KevinH's Access-Aide plugin, which'll automatically add DAISY-recommended attributes for screen readers.

I will certainly do that. Thanks.

Leonatus 04-14-2019 11:48 AM

Sigil v. 0.9.13, css_from_fonts v. 0.1.3 phthon v. 3.4.
The plugin worked all right until now, but recent- and steadily I receive this message:
Code:

Status: failed

Traceback (most recent call last):
  File "C:\Program Files\Sigil\plugin_launchers\python\launcher.py", line 135, in launch
    self.exitcode = target_script.run(container)
  File "C:\Users\Jürgen\AppData\Local\sigil-ebook\sigil\plugins\CSSFromFonts\plugin.py", line 16, in run
    font_object = ImageFont.truetype(font_path)
  File "site-packages\PIL\ImageFont.py", line 280, in truetype
  File "site-packages\PIL\ImageFont.py", line 145, in __init__
OSError: cannot open resource
Error: cannot open resource

No idea what it is. Might someone help, please?
Edit: Inserting @font-face manually works.

Doitsu 04-14-2019 12:29 PM

Quote:

Originally Posted by Leonatus (Post 3831462)
Sigil v. 0.9.13, css_from_fonts v. 0.1.3 phthon v. 3.4.
The plugin worked all right until now, but recent- and steadily I receive this message:

The plugin uses PIL for getting font properties. In rare cases PIL fails to retrieve font properties. PIL will most likely also fail to get the font properties of obfuscated fonts, which are sometimes embedded in commercial epubs.

What's the name of the font and where did you download it from?

Leonatus 04-15-2019 05:35 AM

Quote:

Originally Posted by Doitsu (Post 3831472)
What's the name of the font and where did you download it from?

The font is called FoglihtenNo04 by glukfonts.pl. It' an otf font.
I use it for years and without problems.
What is PIL?

Doitsu 04-15-2019 06:03 AM

Quote:

Originally Posted by Leonatus (Post 3831668)
What is PIL?

PIL is a Python Image Library.

Quote:

Originally Posted by Leonatus (Post 3831668)
The font is called FoglihtenNo04 by glukfonts.pl.

The plugin worked fine with this font on my Windows machine and generated the following CSS declaration:

Code:

@font-face {
    font-family: 'FoglihtenNo04';
    font-weight: normal;
    font-style: normal;
    src: url('../Fonts/FoglihtenNo04-070.otf');
}

If the plugin previously worked with FoglihtenNo04, maybe the font file that you downloaded got corrupted or was locked by another process, e.g. a virus scanner or a backup tool, when you ran the plugin.

Does the plugin work with other fonts or do you get the same error message for all fonts?

Leonatus 04-15-2019 12:36 PM

I tried the same font on my other computer (both Win 10, 32 bit), where I had Sigil v. 0.9.10 installed, and everything is exactly as in your example (as it used to be with my first computer as well). I installed Sigil v. 0.9.13, and everything is o.k. as well.
Whereas, on my first computer, I reloaded the font from another source, and there came the same error message. I tried it with a different font, same error.
Edit: I tried it on a different ebook with the same result. Nota bene: adding the @font-face manually, everything is all right.
And, strange enough, after having transferred the origiginal epub to my second computer with the same loaded font, the plugin works without any issue.

Doitsu 04-15-2019 01:24 PM

Quote:

Originally Posted by Leonatus (Post 3831754)
Whereas, on my first computer, I reloaded the font from another source, and there came the same error message.

Most likely the font that causes the error is corrupted. Unless you can reproduce this error with a well-known font downloaded from the font developer's website, there's nothing I can do about it.


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

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