flash.moe/private/gfontsrip/gfrip.php

71 lines
2.2 KiB
PHP
Raw Normal View History

<?php
define('CSS2_SOURCE', 'https://fonts.googleapis.com/css2?family=Electrolize&family=Hachi+Maru+Pop&family=Victor+Mono:ital,wght@0,100..700;1,100..700&family=Zen+Maru+Gothic:wght@400;700&display=swap');
define('CSS2_TARGET', __DIR__ . '/../../assets/makai.css/fonts.css');
define('WOFF2_TARGET_FS', __DIR__ . '/../../public/fonts');
define('WOFF2_TARGET_PUB', '/fonts/%s');
$css2 = file_get_contents(CSS2_SOURCE);
$lines = explode("\n", $css2);
$info = null;
$files = [];
foreach($lines as $line) {
$line = trim($line);
if($line === '')
continue;
if($line === '}') {
if(!str_starts_with($info->url, 'https://fonts.gstatic.com/'))
continue;
$woff2name = sprintf('%s-%03d-%s-%s.woff2', $info->name, $info->weight, $info->style, $info->hash);
$woff2path = sprintf('%s/%s', WOFF2_TARGET_FS, $woff2name);
if(!is_dir(WOFF2_TARGET_FS))
mkdir(WOFF2_TARGET_FS);
if(!is_file($woff2path))
file_put_contents($woff2path, file_get_contents($info->url));
$files[$info->url] = sprintf(WOFF2_TARGET_PUB, $woff2name);
continue;
}
if(str_starts_with($line, '@font-face')) {
$info = new stdClass;
$info->family = null;
$info->style = null;
$info->weight = null;
$info->hash = null;
$info->url = null;
continue;
}
if(str_starts_with($line, 'font-family:')) {
$info->family = trim(substr($line, 13), "'; ");
$info->name = strtolower(strtr($info->family, [' ' => '-']));
continue;
}
if(str_starts_with($line, 'font-style:')) {
$info->style = trim(substr($line, 12), '; ');
continue;
}
if(str_starts_with($line, 'font-weight:')) {
$info->weight = (int)trim(substr($line, 13), ' ');
continue;
}
if(str_starts_with($line, 'unicode-range:')) {
$info->hash = hash('xxh3', trim(substr($line, 15), '; '));
continue;
}
if(str_starts_with($line, 'src: url(') && str_ends_with($line, ") format('woff2');")) {
$info->url = trim(substr($line, 9, -18), '; ');
continue;
}
}
file_put_contents(CSS2_TARGET, strtr($css2, $files));