summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkj_sh604 <43.splash@gmail.com>2026-03-02 20:43:40 -0500
committerkj_sh604 <43.splash@gmail.com>2026-03-02 20:43:40 -0500
commitfd01077b3f163cbe05be2cfeacdc45d3d060b8a5 (patch)
tree6767192c49dfb3f8bb76ebed43ea5a9b479d121d
parent1ba021a80cd8ea93efe0994f728c3a25450491f6 (diff)
fix: usage of italics for some font options
-rw-r--r--src/fonts.php44
1 files changed, 36 insertions, 8 deletions
diff --git a/src/fonts.php b/src/fonts.php
index 1959f51..6d7912b 100644
--- a/src/fonts.php
+++ b/src/fonts.php
@@ -4,31 +4,59 @@
header('Content-Type: application/json');
header('Cache-Control: public, max-age=3600');
-$output = shell_exec('fc-list --format="%{family}|%{file}\n" 2>/dev/null');
+/* get list of installed fonts using fc-list */
+$cmd = ['fc-list', '--format=%{family}|%{style}|%{file}\n'];
+$desc = [1 => ['pipe', 'w'], 2 => ['pipe', 'w']];
+$proc = proc_open($cmd, $desc, $pipes);
+
+$output = '';
+if (is_resource($proc)) {
+ $output = stream_get_contents($pipes[1]);
+ fclose($pipes[1]);
+ fclose($pipes[2]);
+ proc_close($proc);
+}
if (!$output) {
echo json_encode([]);
exit;
}
$lines = array_filter(explode("\n", trim($output)));
-$fonts = [];
-$seen = [];
+$best = []; // family => ['file' => ..., 'score' => ...]
+
+/* lower score = higher priority */
+$style_score = static function (string $style): int {
+ $s = strtolower(trim($style));
+ if ($s === 'regular' || $s === 'roman' || $s === 'book' || $s === 'text') return 0;
+ if ($s === 'bold') return 1;
+ if (str_contains($s, 'italic') || str_contains($s, 'oblique')) return 2;
+ return 3;
+};
foreach ($lines as $line) {
- $parts = explode('|', $line, 2);
- if (count($parts) < 2) continue;
+ $parts = explode('|', $line, 3);
+ if (count($parts) < 3) continue;
/* take first family name (some entries are comma-separated) */
$families = explode(',', $parts[0]);
$family = trim($families[0]);
- if (empty($family) || isset($seen[$family])) continue;
+ if (empty($family)) continue;
- $file = trim($parts[1]);
+ $style = trim(explode(',', $parts[1])[0]);
+ $file = trim($parts[2]);
if (!file_exists($file)) continue;
- $seen[$family] = true;
+ $score = $style_score($style);
+ if (!isset($best[$family]) || $score < $best[$family]['score']) {
+ $best[$family] = ['file' => $file, 'score' => $score];
+ }
+}
+
+$fonts = [];
+foreach ($best as $family => $entry) {
+ $file = $entry['file'];
$ext = strtolower(pathinfo($file, PATHINFO_EXTENSION));
$format = match ($ext) {
'ttf' => 'truetype',