aboutsummaryrefslogtreecommitdiff
path: root/src/fonts.php
diff options
context:
space:
mode:
authorKyle Javier [kj_sh604]2026-03-16 14:24:34 -0400
committerGitHub2026-03-16 14:24:34 -0400
commit64d8fc9ce9922f38780cc677478cbfb47b21a87e (patch)
tree5b244c18193da100a88904536bf1496392339f65 /src/fonts.php
parent17a970b067fcdf6758668c23184fb2112370bb94 (diff)
merge: python rewrite (#1)
* refactor: Dockerfile * refactor: docker-compose.yml * refactor: docker-entrypoint.sh * refactor: src/font.php * refactor: src/fonts.php * refactor: src/index.php * refactor: src/upload.php * refactor: src/app.py * refactor: src/index.html * refactor: src/requirements.txt * refactor: 24.04 vps compatibility and README re-write * refactor: python .gitignore update * refactor: README.md
Diffstat (limited to 'src/fonts.php')
-rw-r--r--src/fonts.php77
1 files changed, 0 insertions, 77 deletions
diff --git a/src/fonts.php b/src/fonts.php
deleted file mode 100644
index 6d7912b..0000000
--- a/src/fonts.php
+++ /dev/null
@@ -1,77 +0,0 @@
-<?php
-// fonts.php — LIST server-side fonts via fontconfig
-
-header('Content-Type: application/json');
-header('Cache-Control: public, max-age=3600');
-
-/* 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)));
-$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, 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)) continue;
-
- $style = trim(explode(',', $parts[1])[0]);
- $file = trim($parts[2]);
- if (!file_exists($file)) continue;
-
- $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',
- 'otf' => 'opentype',
- 'woff' => 'woff',
- 'woff2' => 'woff2',
- default => 'truetype',
- };
-
- $fonts[] = [
- 'family' => $family,
- 'file' => base64_encode($file),
- 'format' => $format,
- ];
-}
-
-usort($fonts, fn($a, $b) => strcasecmp($a['family'], $b['family']));
-echo json_encode($fonts);