View Single Post
Old 09-27-2015, 04:37 AM   #1
sherman
Guru
sherman ought to be getting tired of karma fortunes by now.sherman ought to be getting tired of karma fortunes by now.sherman ought to be getting tired of karma fortunes by now.sherman ought to be getting tired of karma fortunes by now.sherman ought to be getting tired of karma fortunes by now.sherman ought to be getting tired of karma fortunes by now.sherman ought to be getting tired of karma fortunes by now.sherman ought to be getting tired of karma fortunes by now.sherman ought to be getting tired of karma fortunes by now.sherman ought to be getting tired of karma fortunes by now.sherman ought to be getting tired of karma fortunes by now.
 
Posts: 875
Karma: 2676800
Join Date: Aug 2008
Location: Taranaki - NZ
Device: Kobo Aura H2O, Kobo Forma
[Script] Koboify Fonts

I wanted a quicker way of modifying fonts for use on my Kobo, so I decided to cook up a script this weekend.

I thought I would share this with you folks. Hopefully someone else may also get some use out of it.

Without further ado, may I present to you...

Koboify Fonts
Updated to v0.3.1
Fixes a major (and silly) issue with the batch file, and changes how the script should be called. See readme for details.

New! Workaround for font naming issue
Please see readme for more information

New! Now with included batch file for Windows 7/8/10 users
With the version update to 0.2, I have also included a fancy "Drag'n'Drop" batch file for Windows users. Please view the readme for usage instructions.

This is a script that takes one or more sub-families of a font (Regular, Italic, Bold, Bold Italic), performs some modifications using FontForge, and generates new TTF files.

It will rename the font, and you can chose to:
  • Remove PANOSE information
  • Remove Hinting
  • Add legacy kerning tables
  • Darken fonts, with an optional modification to glyph bearings

You can choose any one, or all of these tasks

It should be cross platform, but I have only tested it on Windows 10 64-bit

it is a Python script, however Python is NOT required to be installed. It is designed to be used with the FontForge embedded Python Interpreter. The only requirement is that FontForge must be installed.

Usage instructions are given in the readme, which I will duplicate at the end of this post.

I hope someone finds a use for this! If you have any suggestions or improvements, please let me know. I can't guarantee that I will spend a lot more time on this however.


Please not, I am not a developer. I don't program very often. Therefore this script will be buggy, not terribly efficient, and may not follow best python/coding practices...


Known Bugs:

None at the moment. I have created a workaround for the font naming issue as of version 0.3


Code:
Koboify Fonts by Sherman Perry

This is a script that takes one or more font files (of the same font family),
performs some modifications, and generates new TTF file(s). It uses FontForge
to perform the modifications.

It is provided AS-IS. This is very rough. I may or may not make improvements
in the future.
=========================================================================
CHANGELOG:
_0.3.1_2015-10-18_
- Fixed an error with the batch file where there was a problem if the path to
  the script file contained spaces.
- Changed how the script is called, from running "ffpython.exr" to 
  "fontforge.exe -script " 

_0.3_2015-09-29_
- Created a somewhat ugly workaround to handle fonts that will not rename
  properly. This is set in a new commandline option, [-n]
- Fixed a fairly major bug in the batch file--it would break if less than
  four files were dragged onto it.
- Moved font name processing to a seperate function

_0.2 2015-09-28_

Reworked a few internals, especially the layer curve type handling
Created a Drag'n'Drop batch file for Windows 7-10 users

_0.1_2015-09-27_
Initial release
=========================================================================
REQUIREMENTS:
FontForge must be installed on your system.
The script will not run using the system installed Python unless FontForge
was specifically compiled to allow this.

This script should now be called by using:
path/to/fontforge/fontforge(.exe) -script KoboifyFonts-0.3.1.py [OPTIONS]

FontForge is located (by default) in the following location on Windows 
64-bit systems:
"C:\Program Files (x86)\FontForgeBuilds\bin\fontforge.exe"

On Windows, the best way to run the script is to probably call it in a
batch file. As of version 0.2 I have created a batch file called 
'DragFontFilesHere.bat' Details on how to use this are below.

I reccommend running the script/batch in the same directory as the font
files you wish to convert.
=========================================================================
OSX INSTALLATION:

The following instructions were kindly provided by mobileread user 'GERGE':

Firstly, you can't use this by just installing FontForge from its page, you 
need command line version of FontForge. That version is much smaller 
(takes 30 MB or so) and needs to be compiled and installed with Homebrew. 
I am not going to explain to you how to use Homebrew, there are literally 
hundreds of guides out there. Just don't use HEAD version, brew install 
fonforge is the command you need to install it.

Secondly, you can't use it by internal Python of FontForge, you don't even 
have an internal Python. But OS X's own Python works perfectly well, just 
use versions of this command I used with EB Garamond:

python ./KoboifyFonts-0.3.py -r /Users/msimsek/Downloads/EBGaramond08-Regular.otf 
	-i /Users/msimsek/Downloads/EBGaramond08-Italic.otf -s -k -p -d . 'EB Garamond'
=========================================================================
USAGE: KoboifyFonts-0.3.1.py [-h] [-r REGULAR] [-i ITALIC] [-b BOLD]
                           [-B BOLDITALIC] [-s] [-k] [-d OUTPUTDIRECTORY]
                           [-w ADDWEIGHT] [-p] [-m] [-n]
                           fontname

positional arguments:
  fontname              Specify new font name

optional arguments:
  -h, --help            show this help message and exit
  -r REGULAR, --regular REGULAR
                        Regular font file
  -i ITALIC, --italic ITALIC
                        Italic font file
  -b BOLD, --bold BOLD  bold font file
  -B BOLDITALIC, --bolditalic BOLDITALIC
                        Bold Italic font file
  -s, --striphint       Strip hints from font
  -k, --legacykern      Include legacy kerning table
  -d OUTPUTDIRECTORY, --outputdirectory OUTPUTDIRECTORY
                        Output directory if set. Default is "./kobomodified/"
  -w ADDWEIGHT, --addweight ADDWEIGHT
                        Add weight to font. Values around 8-15 seems suitable.
                        50 is bold
  -p, --panosestrip     Strip PANOSE data from font
  -m, --modifybearings  Modify bearings when adding weight. This has no affect
                        when not adding weight. Only use it for subtle weight
                        changes
  -n, --namehack        If the fonts generated have internal names different
                        to what you specified, try this option to enable an
                        ugly workaround. It basically generates the font
                        twice.
============================================================================
USING INCLUDED BATCH FILE
-------------------------
The included batch file allows font files to be dragged onto it. The font files
should include anywhere in their filenames the following:
	regular
	bold
	italic
	bolditalic/bold italic/bold-italic etc.
This will tell the batch file how to feed the files to the python script.

When files are dragged onto the batch file, you will be asked a number of questions
about whether to enable the options (see above usage section).

Fonts will be generated in a 'koboified/' subdirectory where the original font
files are located.

The batch file is set to use the FontForge python binary 'ffpython.exe' in the
default FontForge installation directory. If FontForge is installed elsewhere,
you will need to change this in the batch file (variable %ffpythonpath%).

The batch file and the python script should be located in the same directory.
============================================================================
There has been stability issues observed with --modifybearings (Windows 10 x64). 

It seems to have happened more with longer output file paths

I have not determined the root cause of this, 
as the python interpreter crashes without 
providing a traceback
	This stability issue may be Windows related. 
Or (more likely) I've made a big mistake somewhere...
============================================================================
Attached Files
File Type: zip KoboifyFonts-v0.3.1.zip (6.8 KB, 1626 views)

Last edited by sherman; 10-17-2015 at 06:29 PM. Reason: Updated readme to include OSX installation/usage instructions
sherman is offline   Reply With Quote