summaryrefslogtreecommitdiff
blob: 5f851969c6a85ddab77182a22f8bd9cb194cf0f8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<?php
/**
 *
 * @author Niklas Laxström
 * @license GPL-2.0-or-later
 * @file
 */

// Standard boilerplate to define $IP
if ( getenv( 'MW_INSTALL_PATH' ) !== false ) {
	$IP = getenv( 'MW_INSTALL_PATH' );
} else {
	$dir = __DIR__;
	$IP = "$dir/../../..";
}
require_once "$IP/maintenance/Maintenance.php";

class GenerateFontTestPage extends Maintenance {
	public function __construct() {
		parent::__construct();
		$this->addDescription( 'Creates a HTML page with text for all fonts.' );
	}

	public function execute() {
		$base = dirname( __DIR__ );

		$relpath = '../data/fontrepo/fonts';
		$compiler = new FontRepoCompiler( "$base/data/fontrepo/fonts", $relpath );

		$list = $compiler->getRepository();

		$corpus = file_get_contents( __DIR__ . '/../data/langsamples.json' );
		$corpus = FormatJson::decode( $corpus, true );

		$body = '';

		foreach ( $list['languages'] as $code => $fonts ) {
			foreach ( $fonts as $fontname ) {
				if ( $fontname === 'system' ) {
					continue;
				}

				$class = 'font-' . substr( md5( $fontname ), 0, 6 );
				$body .= Html::element(
					'div',
					[ 'class' => "$class sax" ],
					"[$code/$fontname] {$corpus[$code]}"
				);
			}
		}

		$css = ".sax { white-space: nowrap; overflow: hidden; }\n\n";
		$formats = [ 'woff2', 'woff', 'ttf' ];

		foreach ( $list['fonts'] as $fontname => $font ) {
			$class = 'font-' . substr( md5( $fontname ), 0, 6 );

			$css .= "@font-face {\n\tfont-family: '$fontname';\n\tsrc:\n";

			$xus = [];
			foreach ( $formats as $format ) {
				if ( !isset( $font[$format] ) ) {
					continue;
				}
				$xus[] = "\turl('$relpath/{$font[$format]}') format('$format')";
			}

			$css .= implode( ",\n", $xus );

			$css .= ";\n}\n\n";

			$css .= ".$class {\n\tfont-family: '$fontname';\n}\n\n";
		}

		// Charset is needed, because Edge is so brilliant that it thinks this page full of UTF-8
		// is actually in some legacy encoding and does not provide way to change it ;)
		$html = <<<HTML
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<style>
$css
</style>
</head>
<body>
$body
</body>
HTML;

		file_put_contents( "$base/tests/all-fonts.html", $html );
		$this->output( "Done.\n" );
	}
}

$maintClass = 'GenerateFontTestPage';
require_once RUN_MAINTENANCE_IF_MAIN;