Font.php

Go to the documentation of this file.
00001 <?php
00019 class FCFontFinder {
00025     public static function findFile( $code ) {
00026         $data = self::callFontConfig( $code );
00027         if ( is_array( $data ) ) {
00028             return $data['file'];
00029         }
00030 
00031         return false;
00032     }
00033 
00039     public static function findFamily( $code ) {
00040         $data = self::callFontConfig( $code );
00041         if ( is_array( $data ) ) {
00042             return $data['family'];
00043         }
00044 
00045         return false;
00046     }
00047 
00048     protected static function callFontConfig( $code ) {
00049         if ( ini_get( 'open_basedir' ) ) {
00050             wfDebugLog( 'fcfont', 'Disabled because of open_basedir is active' );
00051 
00052             // Most likely we can't access any fonts we might find
00053             return false;
00054         }
00055 
00056         $cache = self::getCache();
00057         $cachekey = wfMemckey( 'fcfont', $code );
00058         $timeout = 60 * 60 * 12;
00059 
00060         $cached = $cache->get( $cachekey );
00061         if ( is_array( $cached ) ) {
00062             return $cached;
00063         } elseif ( $cached === 'NEGATIVE' ) {
00064             return false;
00065         }
00066 
00067         $code = wfEscapeShellArg( ":lang=$code" );
00068         $ok = 0;
00069         $cmd = "fc-match $code";
00070         $suggestion = wfShellExec( $cmd, $ok );
00071 
00072         wfDebugLog( 'fcfont', "$cmd returned $ok" );
00073 
00074         if ( $ok !== 0 ) {
00075             wfDebugLog( 'fcfont', "fc-match error output: $suggestion" );
00076             $cache->set( $cachekey, 'NEGATIVE', $timeout );
00077 
00078             return false;
00079         }
00080 
00081         $pattern = '/^(.*?): "(.*)" "(.*)"$/';
00082         $matches = array();
00083 
00084         if ( !preg_match( $pattern, $suggestion, $matches ) ) {
00085             wfDebugLog( 'fcfont', "fc-match: return format not understood: $suggestion" );
00086             $cache->set( $cachekey, 'NEGATIVE', $timeout );
00087 
00088             return false;
00089         }
00090 
00091         list( , $file, $family, $type ) = $matches;
00092         wfDebugLog( 'fcfont', "fc-match: got $file: $family $type" );
00093 
00094         $file = wfEscapeShellArg( $file );
00095         $family = wfEscapeShellArg( $family );
00096         $type = wfEscapeShellArg( $type );
00097         $cmd = "fc-list $family $type $code file | grep $file";
00098 
00099         $candidates = trim( wfShellExec( $cmd, $ok ) );
00100 
00101         wfDebugLog( 'fcfont', "$cmd returned $ok" );
00102 
00103         if ( $ok !== 0 ) {
00104             wfDebugLog( 'fcfont', "fc-list error output: $candidates" );
00105             $cache->set( $cachekey, 'NEGATIVE', $timeout );
00106 
00107             return false;
00108         }
00109 
00110         # trim spaces
00111         $files = array_map( 'trim', explode( "\n", $candidates ) );
00112         $count = count( $files );
00113         if ( !$count ) {
00114             wfDebugLog( 'fcfont', "fc-list got zero canditates: $candidates" );
00115         }
00116 
00117         # remove the trailing ":"
00118         $chosen = substr( $files[0], 0, -1 );
00119 
00120         wfDebugLog( 'fcfont', "fc-list got $count candidates; using $chosen" );
00121 
00122         $data = array(
00123             'family' => $family,
00124             'type' => $type,
00125             'file' => $chosen,
00126         );
00127 
00128         $cache->set( $cachekey, $data, $timeout );
00129 
00130         return $data;
00131     }
00132 
00136     protected static function getCache() {
00137         return wfGetCache( CACHE_ANYTHING );
00138     }
00139 }
Generated on Tue Oct 29 00:00:25 2013 for MediaWiki Translate Extension by  doxygen 1.6.3