Merge "Refactor font repo compiler so it can be reused"

This commit is contained in:
jenkins-bot
2016-05-02 04:37:53 +00:00
committed by Gerrit Code Review
5 changed files with 198 additions and 119 deletions

View File

@@ -1,114 +0,0 @@
<?php
if ( isset( $_SERVER['REQUEST_METHOD'] ) ) {
exit( "compile.php should be run from the command line\n" );
}
if ( !is_dir( '../fonts/' ) ) {
exit( "compile.php should be run from the data/fontrepo/scripts directory\n" );
}
$list = array();
$list['base'] = '../data/fontrepo/fonts/';
foreach ( glob( '../fonts/*/font.ini' ) as $inifile ) {
$conf = parse_ini_file( $inifile, true );
$languages = array();
$version = null;
$dir = dirname( $inifile );
foreach ( $conf as $fontname => $font ) {
if ( isset( $font['languages'] ) ) {
$languages = explode( ',', $font['languages'] );
foreach ( $languages as $rcode ) {
$rcode = trim( $rcode );
$code = str_replace( '*', '', $rcode );
if ( !isset( $list['languages'][$code] ) ) {
$list['languages'][$code] = array( 'system' );
}
if ( strpos( $rcode, '*' ) !== false ) {
unset( $list['languages'][$code][0] );
array_unshift( $list['languages'][$code], $fontname );
} else {
$list['languages'][$code][] = $fontname;
}
}
}
if ( isset( $font['version'] ) ) {
$version = $font['version'];
}
$list['fonts'][$fontname] = array(
'version' => $version,
);
if ( isset( $font['fontweight'] ) ) {
$list['fonts'][$fontname]['fontweight'] = $font['fontweight'];
}
if ( isset( $font['fontstyle'] ) ) {
$list['fonts'][$fontname]['fontstyle'] = $font['fontstyle'];
}
if ( isset( $font['ttf'] ) ) {
$list['fonts'][$fontname]['ttf'] = basename( $dir ) . '/' . $font['ttf'];
}
if ( isset( $font['svg'] ) ) {
$list['fonts'][$fontname]['svg'] = basename( $dir ) . '/' . $font['svg'];
}
if ( isset( $font['eot'] ) ) {
$list['fonts'][$fontname]['eot'] = basename( $dir ) . '/' . $font['eot'];
}
if ( isset( $font['woff'] ) ) {
$list['fonts'][$fontname]['woff'] = basename( $dir ) . '/' . $font['woff'];
}
if ( isset( $font['woff2'] ) ) {
$list['fonts'][$fontname]['woff2'] = basename( $dir ) . '/' . $font['woff2'];
}
// If font formats are not explicitly defined, scan the directory.
if ( !isset( $list['fonts'][$fontname]['ttf'] ) ) {
foreach ( glob( "$dir/*.{eot,ttf,woff,woff2,svg}", GLOB_BRACE ) as $fontfile ) {
$type = substr( $fontfile, strrpos( $fontfile, '.' ) + 1 );
$list['fonts'][$fontname][$type] = str_replace( dirname( $dir ) . '/', '', $fontfile );
}
}
// Font variants
if ( isset( $font['bold'] ) ) {
$list['fonts'][$fontname]['variants']['bold'] = $font['bold'];
}
if ( isset( $font['bolditalic'] ) ) {
$list['fonts'][$fontname]['variants']['bolditalic'] = $font['bolditalic'];
}
if ( isset( $font['italic'] ) ) {
$list['fonts'][$fontname]['variants']['italic'] = $font['italic'];
}
}
}
ksort( $list['languages'] );
ksort( $list['fonts'] );
$json = json_encode( $list );
$js = <<<JAVASCRIPT
// Do not edit! This file is generated from data/fontrepo by data/fontrepo/scripts/compile.php
( function ( $ ) {
$.webfonts = $.webfonts || {};
$.webfonts.repository = $json;
}( jQuery ) );
JAVASCRIPT;
file_put_contents( '../../../resources/js/ext.uls.webfonts.repository.js', $js );
echo "Done.\n";

View File

@@ -38,13 +38,14 @@
] ]
}, },
"AutoloadClasses": { "AutoloadClasses": {
"UniversalLanguageSelectorHooks": "UniversalLanguageSelector.hooks.php",
"ResourceLoaderULSModule": "includes/ResourceLoaderULSModule.php",
"ResourceLoaderULSJsonMessageModule": "includes/ResourceLoaderULSJsonMessageModule.php",
"ApiLanguageSearch": "api/ApiLanguageSearch.php", "ApiLanguageSearch": "api/ApiLanguageSearch.php",
"ApiULSLocalization": "api/ApiULSLocalization.php", "ApiULSLocalization": "api/ApiULSLocalization.php",
"FontRepoCompiler": "includes/FontRepoCompiler.php",
"LanguageNameSearch": "data/LanguageNameSearch.php",
"ResourceLoaderULSJsonMessageModule": "includes/ResourceLoaderULSJsonMessageModule.php",
"ResourceLoaderULSModule": "includes/ResourceLoaderULSModule.php",
"ULSJsonMessageLoader": "includes/ULSJsonMessageLoader.php", "ULSJsonMessageLoader": "includes/ULSJsonMessageLoader.php",
"LanguageNameSearch": "data/LanguageNameSearch.php" "UniversalLanguageSelectorHooks": "UniversalLanguageSelector.hooks.php"
}, },
"DefaultUserOptions": { "DefaultUserOptions": {
"uls-preferences": "" "uls-preferences": ""

View File

@@ -0,0 +1,142 @@
<?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 = array();
$languages = array();
foreach ( $files as $file ) {
$conf = $this->parseFile( $file );
$fontPath = dirname( $file );
// Ugly hack to populate version to all fonts in a set
$version = null;
foreach ( $conf as $fontname => $font ) {
$fontLanguages = $this->getLanguages( $font );
$this->appendLanguages( $languages, $fontLanguages, $fontname );
$fonts[$fontname] = $this->getFontInfo( $font, $fontPath, $version );
}
}
ksort( $languages );
ksort( $fonts );
return array(
'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 array();
}
$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] = array( '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, &$version ) {
$info = array();
$fontdir = basename( $fontpath );
$version = $info['version'] = isset( $font['version'] ) ? $font['version'] : $version;
if ( isset( $font['fontweight'] ) ) {
$info['fontweight'] = $font['fontweight'];
}
if ( isset( $font['fontstyle'] ) ) {
$info['fontstyle'] = $font['fontstyle'];
}
if ( isset( $font['ttf'] ) ) {
$info['ttf'] = $fontdir . '/' . $font['ttf'];
}
if ( isset( $font['svg'] ) ) {
$info['svg'] = $fontdir . '/' . $font['svg'];
}
if ( isset( $font['eot'] ) ) {
$info['eot'] = $fontdir . '/' . $font['eot'];
}
if ( isset( $font['woff'] ) ) {
$info['woff'] = $fontdir . '/' . $font['woff'];
}
if ( isset( $font['woff2'] ) ) {
$info['woff2'] = $fontdir . '/' . $font['woff2'];
}
// If font formats are not explicitly defined, scan the directory.
if ( !isset( $info['ttf'] ) ) {
foreach ( glob( "$fontpath/*.{eot,ttf,woff,woff2,svg}", GLOB_BRACE ) as $fontfile ) {
$type = substr( $fontfile, strrpos( $fontfile, '.' ) + 1 );
$info[$type] = $fontdir . '/' . 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;
}
}

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,50 @@
<?php
/**
*
* @author Niklas Laxström
* @license GPL-2.0+
* @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 CompileFontRepo extends Maintenance {
public function __construct() {
parent::__construct();
$this->mDescription = 'Creates JavaScript font repository.';
}
public function execute() {
$base = dirname( __DIR__ );
$compiler = new FontRepoCompiler(
"$base/data/fontrepo/fonts",
'../data/fontrepo/fonts/'
);
$list = $compiler->getRepository();
$json = json_encode( $list );
$js = <<<JAVASCRIPT
// Do not edit! This file is generated from data/fontrepo by data/fontrepo/scripts/compile.php
( function ( $ ) {
$.webfonts = $.webfonts || {};
$.webfonts.repository = $json;
}( jQuery ) );
JAVASCRIPT;
file_put_contents( "$base/resources/js/ext.uls.webfonts.repository.js", $js );
$this->output( "Done.\n" );
}
}
$maintClass = 'CompileFontRepo';
require_once DO_MAINTENANCE;