Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // Linedancer (LD4) :: "UI/Font Ripper/Font Ripper.php"
- use FontLib\Font;
- class FontRipper
- {
- public function __construct ( )
- {
- $basePath = rtrim($_SERVER['DOCUMENT_ROOT'], '/');
- $vendor_autoload = null;
- while (!empty($basePath)) {
- // Check if "vendor" exists in the current directory
- if (is_file("{$basePath}/vendor/autoload.php")) {
- $vendor_autoload = "{$basePath}/vendor/autoload.php";
- break;
- }
- // Move up one level
- $basePath = dirname($basePath);
- }
- // Early exit if no vendor directory is found
- if (!is_file($vendor_autoload)) {
- print_r("Missing: \"vendor/autoload.php\"\r\n");
- die();
- }
- else
- {
- if (isset($_GET['debug']))
- print_r("Found: \"{$vendor_autoload}\"\r\n");
- }
- // Include Composer's autoloader
- /* https://github.com/dompdf/php-font-lib */
- require ($vendor_autoload);
- }
- public function calculateWindingDirection($contour) {
- $sum = 0;
- $count = count($contour);
- for ($i = 0; $i < $count; $i++) {
- $current = $contour[$i]['point'];
- $next = $contour[($i + 1) % $count]['point']; // Loop to the first point
- $sum += ($next[0] - $current[0]) * ($next[1] + $current[1]); // Shoelace formula
- }
- return $sum < 0 ? 'clockwise': 'counterclockwise';
- }
- public function extractGlyphData($ttfPath, $characters, $outputFile) {
- // Open the font file
- $font = Font::load($ttfPath);
- $font->parse();
- // Retrieve the glyph data
- $glyphData = [];
- foreach (str_split($characters) as $char) {
- $unicode = ord($char);
- $glyphIndex = $font->getGlyphIndex($unicode);
- if ($glyphIndex === false) {
- continue; // Skip if character is not found
- }
- $glyph = $font->getGlyph($glyphIndex);
- $outline = $glyph->getPathData();
- // Parse contours and Bezier curves
- $contours = [];
- $currentContour = [];
- foreach ($outline['commands'] as $cmd) {
- $type = $cmd['type'];
- if ($type === 'moveto') {
- if (!empty($currentContour)) {
- $contours[] = $currentContour;
- $currentContour = [];
- }
- $currentContour[] = ['type' => 'moveTo', 'point' => $cmd['points'][0]];
- } elseif ($type === 'lineto') {
- $currentContour[] = ['type' => 'lineTo', 'point' => $cmd['points'][0]];
- } elseif ($type === 'quadto') {
- $currentContour[] = [
- 'type' => 'quadraticBezier',
- 'start' => $cmd['startPoint'],
- 'control' => $cmd['controlPoints'][0],
- 'end' => $cmd['points'][0]
- ];
- } elseif ($type === 'cubicto') {
- $currentContour[] = [
- 'type' => 'cubicBezier',
- 'start' => $cmd['startPoint'],
- 'control1' => $cmd['controlPoints'][0],
- 'control2' => $cmd['controlPoints'][1],
- 'end' => $cmd['points'][0]
- ];
- } elseif ($type === 'closepath') {
- $currentContour[] = ['type' => 'closePath'];
- }
- }
- if (!empty($currentContour)) {
- $contours[] = $currentContour;
- }
- // Determine winding direction for each contour
- $processedContours = [];
- foreach ($contours as $contour) {
- $direction = $this->calculateWindingDirection($contour);
- $processedContours[] = [
- 'direction' => $direction,
- 'type' => $direction === 'clockwise' ? 'additive': 'subtractive',
- 'segments' => $contour
- ];
- }
- $glyphData[$char] = [
- 'name' => $char,
- 'unicode' => $unicode,
- 'contours' => $processedContours
- ];
- }
- // Save to JSON
- file_put_contents($outputFile, json_encode($glyphData, JSON_PRETTY_PRINT));
- echo "Glyph data saved to $outputFile\n";
- }
- public function Rip( $Font = 'impact.ttf', $Glyphs = ['A','B','C','X','Y','Z'] )
- {
- // Define fonts folder
- $FontDir = "{$_SERVER['DOCUMENT_ROOT']}/Shared/assets/Fonts/";
- if(!is_dir($FontDir))
- {
- if (isset($_GET['debug']))
- print_r("Creating font directory: \"{$FontDir}\"");
- mkdir(dirname($FontDir), 0777, true);
- }
- // Define valid font extensions (lowercase)
- $ValidTypes = ['ttf','otf','otc','ttc'];
- // Define font file name
- $FontName = pathinfo($Font, PATHINFO_FILENAME);
- // Define font file extension
- $FontType = pathinfo($Font, PATHINFO_EXTENSION);
- // If invalid font extension (early exit)
- if(!in_array(strtolower($FontType),$ValidTypes))
- {
- if (isset($_GET['debug']))
- print_r("Bad font extension: \"{$FontType}\"");
- return;
- }
- $FontNames =
- array_map
- (
- fn($file) =>
- pathinfo
- (
- $file,
- PATHINFO_FILENAME
- ),
- glob
- (
- rtrim
- (
- $FontDir,
- '/'
- ) . "/*.{$FontType}"
- )
- );
- $Fonts = implode("\", \"",$FontNames);
- $Fonts = !empty($Fonts) ? "\"{$Fonts}\"" : "";
- print_r(strtoupper($FontType)." Fonts: {$Fonts}\r\n");
- print_r("Using: \"{$FontName}.{$FontType}\" In: \"{$FontDir}\"\r\n");
- if(!is_dir(dirname($FontDir)))
- {
- if (isset($_GET['debug']))
- print_r("Creating font directory: \"{$FontDir}\"");
- mkdir(dirname($FontDir), 0777, true);
- }
- // If invalid font name (early exit)
- if(!in_array($FontName,$FontNames))
- {
- if (isset($_GET['debug']))
- print_r("Missing: \"{$FontName}\"");
- die();
- }
- $FontPath = "{$_SERVER['DOCUMENT_ROOT']}/Shared/assets/Fonts/{$FontName}.{$FontType}";
- // If invalid font path (early exit)
- if(!is_dir(dirname($FontPath)))
- {
- if (isset($_GET['debug']))
- print_r("Creating font directory: \"".dirname($FontPath)."\"");
- if(!is_dir(dirname($FontPath)))
- mkdir(dirname($FontPath), 0777, true);
- }
- // If invalid font file (early exit)
- if(!is_file($FontPath))
- {
- if (isset($_GET['debug']))
- print_r("Missing font file: \"{$FontPath}\"");
- return;
- }
- // Set brand of font
- $BrandName = ucwords($FontName);
- // Specify list of glyphs to rip
- $Characters = $Glyphs;
- // Cycle thru specified glyphs and rip from given font
- foreach ($Characters as $GlyphName) {
- $RipPath = "{$_SERVER['DOCUMENT_ROOT']}/LD4/Linedancer/scripts/json/UI/Gamepad/Labels/{$BrandName}/{$GlyphName}.json";
- // If rip destination path is missing: create it
- if (!is_dir(dirname($RipPath))) {
- if (isset($_GET['debug']))
- print_r("Creating rip directory: \"".dirname($RipPath)."\"");
- if(!is_dir(dirname($RipPath)))
- mkdir(dirname($RipPath), 0777, true);
- }
- // Extract and save glyph data
- $this->extractGlyphData($FontPath, $GlyphName, $RipPath);
- }
- // (exit)
- return;
- }
- }
- $FontRipper = new FontRipper();
- $FontRipper->Rip('impact.ttf',['A','B','C','X','Y','Z']);
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement