Created
January 28, 2021 16:21
-
-
Save felipelavinz/c60e59fb311d90ce550f7a658a60a822 to your computer and use it in GitHub Desktop.
A simple PHP script that takes TTF files and produces subsetted WOFF2 files. Requires pyftsubset to perform the actual conversion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$subsets = [ | |
"cyrillic-ext" => "U+0460-052F, U+1C80-1C88, U+20B4, U+2DE0-2DFF, U+A640-A69F, U+FE2E-FE2F", | |
"cyrillic" => "+0400-045F, U+0490-0491, U+04B0-04B1, U+2116", | |
"greek-ext" => "U+1F00-1FFF", | |
"greek" => "U+0370-03FF", | |
"latin-ext" => "U+0100-024F, U+0259, U+1E00-1EFF, U+2020, U+20A0-20AB, U+20AD-20CF, U+2113, U+2C60-2C7F, U+A720-A7FF", | |
"latin" => "U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+2000-206F, U+2074, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD" | |
]; | |
$dir_files = scandir( __DIR__ ); | |
$dir_fonts = array_filter( $dir_files, function( $item ){ | |
$extension = strtolower( pathinfo( $item, PATHINFO_EXTENSION ) ); | |
return $extension === 'ttf'; | |
} ); | |
foreach ( $dir_fonts as $font_file ) { | |
$font_filename = pathinfo( $font_file, PATHINFO_FILENAME ); | |
foreach ( $subsets as $subset => $range ) { | |
$output_filename = strtolower( $font_filename .'-'. $subset .'.woff2' ); | |
$cmnd = "pyftsubset {$font_file} --output-file={$output_filename} --flavor=woff2 --layout-features=\"*\" --unicodes=\"{$range}\""; | |
echo $cmnd; | |
`$cmnd`; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment