summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/UniversalLanguageSelector/includes/FontRepoCompiler.php
blob: 35d56fe8ad8880602b7cb9a4d030678ad3b2191c (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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php

/**
 * This class parses font specification ini files to a central list.
 * @author Niklas Laxström
 * @since 2016.04
 */
class FontRepoCompiler {
	protected $fsPath;
	protected $webPath;

	public function __construct( $fsPath, $webPath ) {
		$this->fsPath = $fsPath;
		$this->webPath = $webPath;
	}

	public function getRepository() {
		$files = $this->getFilesFromPath( $this->fsPath );

		$fonts = [];
		$languages = [];

		foreach ( $files as $file ) {
			$conf = $this->parseFile( $file );
			$fontPath = dirname( $file );

			foreach ( $conf as $fontname => $font ) {
				$fontLanguages = $this->getLanguages( $font );
				$this->appendLanguages( $languages, $fontLanguages, $fontname );
				$fonts[$fontname] = $this->getFontInfo( $font, $fontPath );
			}
		}

		ksort( $languages );
		ksort( $fonts );

		return [
			'base' => $this->webPath,
			'languages' => $languages,
			'fonts' => $fonts
		];
	}

	public function getFilesFromPath( $fspath ) {
		return glob( "$fspath/*/font.ini" );
	}

	public function parseFile( $filepath ) {
		return parse_ini_file( $filepath, true );
	}

	public function getLanguages( array $font ) {
		if ( !isset( $font['languages'] ) ) {
			return [];
		}

		$languages = explode( ',', $font['languages'] );
		$languages = array_map( 'trim', $languages );

		return $languages;
	}

	public function appendLanguages( &$languages, $fontLanguages, $fontname ) {
		foreach ( $fontLanguages as $rcode ) {
			$code = str_replace( '*', '', $rcode );

			if ( !isset( $languages[$code] ) ) {
				$languages[$code] = [ 'system' ];
			}

			if ( strpos( $rcode, '*' ) !== false ) {
				if ( $languages[$code][0] === 'system' ) {
					unset( $languages[$code][0] );
				}
				array_unshift( $languages[$code], $fontname );
			} else {
				$languages[$code][] = $fontname;
			}
		}
	}

	public function getFontInfo( $font, $fontpath ) {
		$info = [];
		$fontdir = basename( $fontpath );

		if ( isset( $font['fontweight'] ) ) {
			$info['fontweight'] = $font['fontweight'];
		}

		if ( isset( $font['fontstyle'] ) ) {
			$info['fontstyle'] = $font['fontstyle'];
		}

		foreach ( [ 'woff', 'woff2' ] as $format ) {
			if ( isset( $font[$format] ) ) {
				$info[$format] = OutputPage::transformFilePath( $fontdir, $fontpath, $font[$format] );
			}
		}

		// If font formats are not explicitly defined, scan the directory.
		if ( !isset( $info['woff'] ) ) {
			foreach ( glob( "$fontpath/*.{woff,woff2}", GLOB_BRACE ) as $fontfile ) {
				$type = substr( $fontfile, strrpos( $fontfile, '.' ) + 1 );
				$info[$type] = OutputPage::transformFilePath( $fontdir, $fontpath, basename( $fontfile ) );
			}
		}

		// Font variants
		if ( isset( $font['bold'] ) ) {
			$info['variants']['bold'] = $font['bold'];
		}

		if ( isset( $font['bolditalic'] ) ) {
			$info['variants']['bolditalic'] = $font['bolditalic'];
		}

		if ( isset( $font['italic'] ) ) {
			$info['variants']['italic'] = $font['italic'];
		}

		return $info;
	}
}