Archive for October, 2015

So fond of fonts

Tuesday, October 6th, 2015

I can’t say that I’m in love with typography, but I do enjoy writing (code or otherwise) using a good editor and… a good looking font.

I’ve recently stumbled upon the Hack font, which has its roots in the open source world and derives from Bitstream Vera & DejaVu. I immediately liked it; it feels good to change stuff once in a while… :)  I might still choose to switch back to Consolas, but for now I’m very pleased with Hack and it gave me a reason to mess around with Bash yet again ^^.

Of course, this alone is not a sufficient justification for a blog post! As I’ve described in an earlier post, I always try to maximize the ‘portability’ of my development environment and overall configuration and changing fonts should be no exception ;-)

I do not install fonts manually in the OS; I prefer to put my fonts in a central folder of my CloudStation share (i.e., along with the rest of my configuration & tools) so that it gets replicated on all my devices (I also do the same with tons of other stuff including wallpapers).

A major issue with this is that customizing fonts can be done in plenty applications but each has its own specificities, either they give you a lot of control or you have to go through hoops to achieve what you want. More specifically, many applications will only allow you to select fonts that are available through the OS’s font system (i.e., that are registered) while others will require additional flags or even worse, will want you to copy the font files around.

Under Windows, installing new fonts requires administrator privileges due to the security risks (laugh all you want :p). Thus even if I was to register the fonts, I couldn’t do so at work which is a bummer.

Fortunately, there are programmatic ways to register fonts in a user’s session without administrator privileges. I’ve found two programs that can do this from the command line:

I’ve found regfont to be better  as it is a bit more *nix friendly, comes with a 64-bit executable and is less verbose than RegisterFont (it could use a –silent switch though). 

Using regfont, you can easily register a new font in your user session using the following:

regfont.exe --add cool.ttf

You can also add a complete folder in one go using a wildcard. As you might already know, I’m a bit of a bash fan, so indeed I added a few more aliases to my profile to automate the registration of my custom fonts whenever my bash profile is loaded. It adds a bit to the overall startup time but it’s still quite reasonable.

First things first, since I wanted to keep a clean organization in my fonts folder, I couldn’t use the wildcard flag of regfont as it doesn’t look for font files recursively. For this reason, I needed to find the files myself (using the find command) and execute regfont once for each file.

Since the find command returns *NIX paths, I needed to convert those to WIN* paths; this was easy enough with the help of StackOverflow (as always ^^):

winpath() {
	if [ ${#} -eq 0 ]; then
		: skip
	elif [ -f "$1" ]; then
		local dirname=$(dirname "$1")
		local basename=$(basename "$1")
		echo "$(cd "$dirname" && pwd -W)/$basename" \
		| sed \
		  -e 's|/|\\|g';
	elif [ -d "$1" ]; then
		echo "$(cd "$1" && pwd -W)" \
		| sed \
		  -e 's|/|\\|g';
		echo "$1" \
		| sed \
		  -e 's|^/\(.\)/|\1:\\|g' \
		  -e 's|/|\\|g'

Later in my profile, I’ve added the following for registering the fonts:

export MY_FONTS_FOLDER=$CLOUDSTATION_HOME/Configuration/Dev/Fonts
append_to_path $REGISTER_FONT_HOME

register_font(){ ("$REGISTER_FONT_HOME/regfont" "--add" "$1")& } # alternative "RegisterFont.exe" "add"
alias registerfont='register_font'
# Register all my fonts for the current user session
# Works also if the user is not local administrator
# Reference:
	SAVEIFS=$IFS # save the internal field separator (IFS) (reference:$IFS)
	IFS=$(echo -en "\n\b") # change it to newline
	fontsToRegister=`find $MY_FONTS_FOLDER -type f -name "*.ttf"` # recursively find all files matching the original extension

	for fontToRegister in $fontsToRegister; do
		fontToRegisterWinPath=`winpath $fontToRegister`
		#echo $fontToRegisterWinPath
		register_font $fontToRegister
	unset fontToRegisterWinPath
	unset fontToRegister
	unset fontsToRegister
	IFS=$SAVEIFS # restore the internal field separator (IFS)

I then simply invoke the register_fonts function near the end of my profile, just before I call clear.

With this in place, whenever my profile is loaded, I know that my fonts are registered and usable in most applications.

Just as a side note, here’s how you can manually install a custom font for use with Java-based applications such as IntelliJ, WebStorm, Netbeans, etc: you need to copy the font files to the jre/jdk lib/fonts folder.

As a second side note, ConEmu will load the first ttf file it encounters in its folder and make that one available for use.

As a third and last side node, I couldn’t find a way to load a custom font with Sublime Text 3, it only seems to be able to list system-registered ones…

So.. which font are you most fond of?