Skip to content

Instantly share code, notes, and snippets.

@gingerbeardman
Last active December 8, 2021 13:02
Show Gist options
  • Save gingerbeardman/f07d9e033dab453b56afee9829828ed1 to your computer and use it in GitHub Desktop.
Save gingerbeardman/f07d9e033dab453b56afee9829828ed1 to your computer and use it in GitHub Desktop.
Generates CSS for custom fonts on the Sony Reader PRS series, normalising the size of fonts using the inverse of the Asc+Desc metric
#!/usr/bin/env php
<?php
// requirements: python, pip
// dependencies: font-line
// installation:
// sudo -H python -m ensurepip
// sudo -H pip install font-line
// setup:
// script should sit alongside folders full of font families
// usage:
// call this script without any parameters
$which = (count($argv) == 1) ? "*" : $argv[1];
$dirs = glob($which, GLOB_ONLYDIR);
foreach ($dirs as $folder) {
echo "\n";
echo "Folder:\t$folder\n";
$files = glob("$folder/*");
echo "Files:\t".count($files)."\n";
$regular = @reset(preg_grep('/-Regular\.(ttf|otf)$/i', $files));
$bold = @reset(preg_grep('/-(Bold|SemiBold)\.(ttf|otf)$/i', $files));
if (!$bold) $bold = $regular;
$italic = @reset(preg_grep('/-Italic\.(ttf|otf)$/i', $files));
$bolditalic = @reset(preg_grep('/-(BoldItalic|SemiBoldItalic)\.(ttf|otf)$/i', $files));
if (!$bolditalic) $bolditalic = $italic;
$familyname = $folder;
echo "Ratio:\t";
$ratio = system("bc -l <<< \"scale=2;$(font-line report ".str_replace(' ', '\ ', $folder)."/* | grep --text 'hhea Asc + Desc' | grep -o -E '[0-9\.]+' | paste -s -d+ - | bc)/4\"");
$resize = round(1 / $ratio, 2);
echo "\n";
// begin formatted CSS
$css = <<<CSS
@page {
margin-bottom: 0.5em;
margin-left: 0.5em;
margin-right: 0.5em;
margin-top: 1em;
}
@font-face {
font-family: "$familyname";
font-weight: normal;
font-style: normal;
src: url(res:///Data/fonts/$regular);
}
@font-face {
font-family: "$familyname";
font-weight: bold;
font-style: normal;
src: url(res:///Data/fonts/$bold);
}
@font-face {
font-family: "$familyname";
font-weight: normal;
font-style: italic;
src: url(res:///Data/fonts/$italic);
}
@font-face {
font-family: "$familyname";
font-weight: bold;
font-style: italic;
src: url(res:///Data/fonts/$bolditalic);
}
body {
font-family: "$familyname";
font-size: {$resize}em;
}
CSS;
// end formatted CSS
file_put_contents("../database/system/PRSPlus/epub/$familyname.css", $css);
}
echo "Total:\t".count($dirs)."\n\n";
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment