]> _ Git - fluidbook_tools.git/commitdiff
outils : font vers woff
authorsoufiane <soufiane@cubedesigners.com>
Tue, 16 Apr 2024 10:31:24 +0000 (12:31 +0200)
committersoufiane <soufiane@cubedesigners.com>
Tue, 16 Apr 2024 10:31:24 +0000 (12:31 +0200)
src/FluidbookFontToWoff.php [new file with mode: 0644]

diff --git a/src/FluidbookFontToWoff.php b/src/FluidbookFontToWoff.php
new file mode 100644 (file)
index 0000000..8054d4d
--- /dev/null
@@ -0,0 +1,72 @@
+<?php
+
+namespace Fluidbook\Tools;
+
+use Cubist\Util\CommandLine;
+use Cubist\Util\Graphics\Color;
+
+class FluidbookFontToWoff
+{
+    public static function fontforge($dest, $filepath) {
+        if (!file_exists($dest) || filemtime($dest) < filemtime($filepath)) {
+            $script = resource_path('tools/fonts/convertrn.pe');
+            if (!is_executable($script)) {
+                chmod($script, 755);
+            }
+            $fontforge = new CommandLine('fontforge');
+            $fontforge->setArg('-script', $script);
+            $fontforge->setArg(null, $filepath);
+            $fontforge->setArg(null, $dest);
+            $fontforge->execute();
+            //$fontforge->debug();
+        }
+    }
+
+    /**
+     * @param $f
+     * $f = filepath
+     * @return CommandLine
+     */
+    public static function fontline($f) {
+        $fontline = new CommandLine('font-line');
+        $fontline->setArg(null, 'report');
+        $fontline->setArg(null, $f);
+        $fontline->execute();
+
+        return $fontline;
+    }
+
+    public static function configFont($fontFile, $hash = null, $filepath = null) {
+        $fontline = self::fontline($filepath);
+        if(!$hash) {
+            $hash = 'fb_' . substr(md5($fontFile), 0, 10);
+        }
+        $report = explode("\n", $fontline->getOutput());
+
+        foreach ($report as $item) {
+            $item = trim($item);
+            if (!stristr($item, ':')) {
+                continue;
+            }
+            list($k, $v) = explode(':', $item, 2);
+            $v = trim($v);
+            if ($k == '[head] Units per Em') {
+                $fontHeight = $v;
+            }
+            if ($k == '[OS/2] CapHeight') {
+                $fontCapHeight = $v;
+            }
+            if ($k == '[OS/2] TypoAscender') {
+                $ascender = abs($v);
+            }
+            if ($k == '[OS/2] TypoDescender') {
+                $descender = abs($v);
+            }
+        }
+        $capHeight = 1;
+        if (isset($fontCapHeight) && isset($fontHeight) && is_numeric($fontCapHeight) && is_numeric($fontHeight)) {
+            $capHeight = $fontCapHeight / $fontHeight;
+        }
+        return ['family' => $hash, 'capHeight' => $capHeight, 'ascender' => $fontHeight > 0 ? $ascender / $fontHeight : 1, 'descender' => $fontHeight > 0 ? $descender / $fontHeight : 1];
+    }
+}