Files
wordpress-theme-aksrt/tools/generate-fa5-icons.php
T
kate522 4740078135 fix(icons): repair the FA5 generator and drop the 404 font references
generate-fa5-icons.php read fa-{brands,regular,solid}.svg from
webfonts/, but only woff/woff2 ship with the theme, so the generator
died on file_get_contents() and the committed fa5-icons.php could not
be regenerated from what the theme actually distributes.

Decode the style split from each shipped woff's cmap table instead.
The split has to come from the font: FA5 assigns different drawings to
the same codepoint per style, so fas/far/fab membership is unanswerable
from the CSS alone. Output is byte-identical to the committed catalog
(1001/151/456), so the picker is unaffected.

all.min.css also declared eot / eot?#iefix / ttf / svg fallbacks for all
three families while webfonts/ holds only woff2 + woff, so a browser
that stops at woff requested six files that do not exist. Add
tools/strip-fa5-legacy-fonts.mjs to keep the src: list to what ships;
it is idempotent and touches nothing but the @font-face blocks.
2026-11-01 11:52:14 +08:00

184 lines
5.3 KiB
PHP

<?php
/**
* One-shot generator for src/Icons/fa5-icons.php.
*
* Sources (shipped with the theme):
* - assets/fontawesome5/css/all.min.css → glyph class names + codepoints
* - assets/fontawesome5/webfonts/fa-*.woff → per-style codepoints (cmap table)
*
* Usage: php tools/generate-fa5-icons.php
*
* The style split comes from each font's own cmap subtable rather than from the
* CSS: FA5 assigns the same codepoint to different glyphs per style (e.g. far
* "address-book" and fas "address-book" are different drawings), so membership
* in fas / far / fab can only be answered by the font itself.
*/
defined('ABSPATH') || define('ABSPATH', __DIR__ . '/');
$root = dirname(__DIR__);
$css_path = $root . '/assets/fontawesome5/css/all.min.css';
$fonts_path = $root . '/assets/fontawesome5/webfonts';
$out_path = $root . '/src/Icons/fa5-icons.php';
/** Font file per style group, matching the FA5 family naming. */
const FA5_FONTS = [
'fas' => 'fa-solid-900.woff',
'far' => 'fa-regular-400.woff',
'fab' => 'fa-brands-400.woff',
];
/**
* Extract one table's uncompressed bytes from a WOFF container.
*
* WOFF stores each table zlib-deflated when compLen differs from origLen.
*/
function fa5_woff_table(string $raw, string $tag): ?string {
$num_tables = (int) (unpack('n', substr($raw, 12, 2))[1] ?? 0);
if ($num_tables < 1 || strlen($raw) < 44 + $num_tables * 20) {
return null;
}
for ($i = 0; $i < $num_tables; $i++) {
$entry = substr($raw, 44 + $i * 20, 20);
if (substr($entry, 0, 4) !== $tag) {
continue;
}
$p = unpack('Noffset/NcompLen/NorigLen', substr($entry, 4, 12));
$data = substr($raw, $p['offset'], $p['compLen']);
if ($p['compLen'] !== $p['origLen']) {
$data = gzuncompress($data);
if ($data === false) {
return null;
}
}
return $data;
}
return null;
}
/**
* Codepoints mapped by a cmap table, keyed for O(1) membership tests.
*
* Only format-4 (Unicode BMP) subtables are decoded — the (3,1) record is
* preferred, falling back to the first (3,x) then (0,x) record so a font that
* ships a different platform order still resolves.
*/
function fa5_cmap_codepoints(string $cmap): array {
$records = (int) (unpack('n', substr($cmap, 2, 2))[1] ?? 0);
$best = null;
$fallback = null;
for ($i = 0; $i < $records; $i++) {
$rec = unpack('nplatform/nencoding/Noffset', substr($cmap, 4 + $i * 8, 8));
if ($rec['platform'] === 3 && $rec['encoding'] === 1) {
$best = $rec['offset'];
break;
}
if ($fallback === null && ($rec['platform'] === 3 || $rec['platform'] === 0)) {
$fallback = $rec['offset'];
}
}
$offset = $best ?? $fallback;
if ($offset === null || $offset + 14 > strlen($cmap)) {
return [];
}
$table = substr($cmap, $offset);
$format = (int) (unpack('n', substr($table, 0, 2))[1] ?? 0);
if ($format !== 4) {
fwrite(STDERR, "Unsupported cmap format {$format}; only format 4 is decoded\n");
exit(1);
}
$seg_x2 = (int) (unpack('n', substr($table, 6, 2))[1] ?? 0);
$seg_count = intdiv($seg_x2, 2);
if ($seg_count < 1) {
return [];
}
$end_base = 14;
$start_base = $end_base + $seg_x2 + 2;
$codes = [];
for ($i = 0; $i < $seg_count; $i++) {
$start = (int) (unpack('n', substr($table, $start_base + $i * 2, 2))[1] ?? 0);
$end = (int) (unpack('n', substr($table, $end_base + $i * 2, 2))[1] ?? 0);
if ($start > $end || $end === 0xFFFF) {
continue;
}
for ($code = $start; $code <= $end; $code++) {
$codes[$code] = true;
}
}
return $codes;
}
$css = (string) file_get_contents($css_path);
// class name → codepoint (e.g. 'home' => 0xf015)
if (!preg_match_all('/\.fa-([a-z0-9-]+):before\{content:"\\\\([0-9a-f]{4})"\}/i', $css, $m, PREG_SET_ORDER)) {
fwrite(STDERR, "No glyph rules matched in all.min.css\n");
exit(1);
}
$name_to_code = [];
foreach ($m as $hit) {
$name_to_code[$hit[1]] = hexdec($hit[2]);
}
// style → set of codepoints, decoded from the shipped woff cmaps
$code_to_style = [];
foreach (FA5_FONTS as $style => $file) {
$path = $fonts_path . '/' . $file;
if (!is_readable($path)) {
fwrite(STDERR, "Missing font: {$path}\n");
exit(1);
}
$cmap = fa5_woff_table((string) file_get_contents($path), 'cmap');
if ($cmap === null) {
fwrite(STDERR, "No readable cmap table in {$path}\n");
exit(1);
}
$code_to_style[$style] = fa5_cmap_codepoints($cmap);
}
$groups = ['fas' => [], 'far' => [], 'fab' => []];
foreach ($name_to_code as $name => $code) {
foreach ($groups as $style => $unused) {
if (!empty($code_to_style[$style][$code])) {
$groups[$style][] = $name;
}
}
}
foreach ($groups as $style => $names) {
sort($groups[$style], SORT_NATURAL | SORT_FLAG_CASE);
}
$export = var_export($groups, true);
$php = <<<PHP
<?php
/**
* Font Awesome 5 Free (5.15.4) icon catalog, grouped by style.
*
* Generated by tools/generate-fa5-icons.php — do not edit by hand.
* Bare glyph names; the picker composes "fa {fas|far|fab} fa-{name}".
*
* @package AKSRT
*/
defined('ABSPATH') || exit;
return {$export}
PHP;
// .gitattributes declares *.php eol=crlf for the working tree; emit CRLF so the
// regenerated file matches its neighbours instead of tripping the normalize warning.
file_put_contents($out_path, str_replace("\n", "\r\n", $php . ";\n"));
printf(
"fas=%d far=%d fab=%d (css glyph classes=%d)\nWrote %s\n",
count($groups['fas']),
count($groups['far']),
count($groups['fab']),
count($name_to_code),
$out_path
);