#!/bin/bash # # Hanlinize EPUB: make Hanlin and other e-readers display epub files correctly # # Problem: the font for displaying EPUB files doesn't have unicode characters and # even though the reader supports changing fonts, not for the epub format; # this is fixed in newer firmware, but Czech Hanlin reseller didn't update # the localized firmware to either tackle this problem (despite knowing # about it for years) or to at least include the option to select custom # fonts for epub files; shame on you, guys! # # Solution: embed unicode-aware fonts into the epub - Hanlin supports embedded fonts; # beware that this significantly increases the epub size - there is also # the possibility to load external fonts from within the epub file, but not # using this tool # # Usage: ./hanlinize file1.epub file2.epub file3.epub # # Fonts: set fonts and their locations by changing variables FONTS and FONTLOC # note that for the FONTS variable, you need to include 4 fonts (filenames) # in the right order - regular, bold, italic and bold-italic # # Warning: ugly code warning - the tool is very, very far from being perfect, it # uses bad approach and isn't guaranteed to work on all epubs - chances # are it may do weird stuff - use it at your own risk! # # License: DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE # Version 2, December 2004 # # Copyright (C) 2011 Michal Docekal # # Everyone is permitted to copy and distribute verbatim or modified # copies of this license document, and changing it is allowed as long # as the name is changed. # # DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE # TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION # # 0. You just DO WHAT THE FUCK YOU WANT TO. # # # SET THESE VARIABLES TO WHATEVER SUITS YOU FONTS="DejaVuSerifCondensed.ttf DejaVuSerifCondensed-Bold.ttf DejaVuSerifCondensed-Italic.ttf DejaVuSerifCondensed-BoldItalic.ttf" FONTLOC="/usr/share/fonts/TTF" # THE ACTUAL CODE IS BELOW, DON'T MESS WITH IT IF YOU DON'T KNOW WHAT YOU'RE DOING if [ "$#" == "0" ]; then echo 'No parameter entered. Expecting one or more epub files.' exit 0 fi function error { echo echo "Fatal error: $@" echo "Terminating." exit 1 } function gen-css { echo """@font-face { font-family : HanlinEmbedFix; font-weight : normal; font-style: normal; src : url(../Fonts/"""$(echo $FONTS | awk '{print $1}')"""); } @font-face { font-family : HanlinEmbedFix; font-weight : bold; font-style: normal; src : url(../Fonts/"""$(echo $FONTS | awk '{print $2}')"""); } @font-face { font-family : HanlinEmbedFix; font-weight : normal; font-style: italic; src : url(../Fonts/"""$(echo $FONTS | awk '{print $3}')"""); } @font-face { font-family : HanlinEmbedFix; font-weight : bold; font-style: italic; src : url(../Fonts/"""$(echo $FONTS | awk '{print $4}')"""); } body { font-family: "HanlinEmbedFix", serif !important; } strong, b { font-family: "HanlinEmbedFix", serif !important; } em, i { font-family: "HanlinEmbedFix", serif !important; }""" } ORIGIN=$(pwd) while [ "$#" != "0" ]; do TMPDIR=$(mktemp -d) OUTPUT="$(basename $1 .epub)-hanlinized.epub" BACKUP="$(basename $1)~" echo -n "Converting file $1 to $OUTPUT: " unzip "$1" -d "$TMPDIR/" 2>/dev/null >/dev/null || error "unzipping epub failed" cd "$TMPDIR/" || error "changing directory to \"$TMPDIR\"" mkdir -p "$TMPDIR/OEBPS/Styles" || error "creating directory \"$TMPDIR/OEBPS/Styles\"" mkdir -p "$TMPDIR/OEBPS/Fonts" || error "creating directory \"$TMPDIR/OEBPS/Fonts\"" for font in $FONTS; do cp "$FONTLOC/$font" "$TMPDIR/OEBPS/Fonts/" || error "copying /usr/share/fonts/TTF/$font to $TMPDIR/OEBPS/Fonts" done gen-css > "$TMPDIR/OEBPS/Styles/hanlin-embed-fonts-fix.css" # add font files to manifest # sorry for the messy regexps, sed probably has a way to do case insensitive search, but I'm too lazy to keep trying finding it sed -i '/<\/[mM][aA][nN][iI][fF][eE][sS][tT]\>/i '"" "$TMPDIR/OEBPS/content.opf" for font in $FONTS; do sed -i '/<\/[mM][aA][nN][iI][fF][eE][sS][tT]\>/i '"" "$TMPDIR/OEBPS/content.opf" done # apply styles to content ls -1 "$TMPDIR/OEBPS/Text/" | while read line; do sed -i '/<[hH][eE][aA][dD]>/i '"" "$TMPDIR/OEBPS/Text/$line" done zip -Xr "$ORIGIN/$OUTPUT" mimetype META-INF OEBPS 2>/dev/null >/dev/null || error "packing output file \"$ORIGIN/$OUTPUT\"" rm -rf "$TMPDIR" || error "erasing \"$TMPDIR\"" cd "$ORIGIN" || error "changing directory to \"$ORIGIN\"" # switcheroo mv -n "$1" "$BACKUP" || error "error moving \"$1\" to \"$BACKUP\"" mv -n "$OUTPUT" "$1" || error "error moving \"$OUTPUT\" to \"$1\"" echo "Done." shift done