r44771 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r44770‎ | r44771 | r44772 >
Date:17:50, 18 December 2008
Author:nikerabbit
Status:ok
Tags:
Comment:
* Use fontconfig if available
Modified paths:
  • /trunk/extensions/Translate/Stats.php (modified) (history)
  • /trunk/extensions/Translate/_autoload.php (modified) (history)
  • /trunk/extensions/Translate/utils/Font.php (added) (history)

Diff [purge]

Index: trunk/extensions/Translate/Stats.php
@@ -239,7 +239,7 @@
240240
241241 public function draw( FormOptions $opts ) {
242242 wfLoadExtensionMessages( 'Translate' );
243 - global $wgTranslatePHPlotFont;
 243+ global $wgTranslatePHPlotFont, $wgLang;
244244
245245 $width = $opts->getValue( 'width' );
246246 $height = $opts->getValue( 'height' );
@@ -262,8 +262,12 @@
263263 $i--;
264264 }
265265
266 - $plot->SetDefaultTTFont( $wgTranslatePHPlotFont );
267 -
 266+ $font = FCFontFinder::find( $wgLang->getCode() );
 267+ if ( $font ) {
 268+ $plot->SetDefaultTTFont( $font );
 269+ } else {
 270+ $plot->SetDefaultTTFont( $wgTranslatePHPlotFont );
 271+ }
268272 $plot->SetDataValues( $data );
269273
270274 if ( $legend !== null )
Index: trunk/extensions/Translate/_autoload.php
@@ -55,6 +55,7 @@
5656 # utils
5757 $wgAutoloadClasses['ResourceLoader'] = $dir . 'utils/ResourceLoader.php';
5858 $wgAutoloadClasses['StringMatcher'] = $dir . 'utils/StringMatcher.php';
 59+$wgAutoloadClasses['FCFontFinder'] = $dir . 'utils/Font.php';
5960
6061 $wgAutoloadClasses['StringMangler'] = $dir . 'utils/StringMangler.php';
6162 $wgAutoloadClasses['SmItem'] = $dir . 'utils/StringMangler.php';
Index: trunk/extensions/Translate/utils/Font.php
@@ -0,0 +1,57 @@
 2+<?php
 3+
 4+/**
 5+ * Wrapper around font-config to get useful ttf font given a language code.
 6+ * Uses wfShellExec, wfEscapeShellArg and wfDebugLog from MediaWiki.
 7+ * @author Niklas Laxström, 2008
 8+ * @license PD
 9+ */
 10+class FCFontFinder {
 11+
 12+ public static function find( $code ) {
 13+ $code = wfEscapeShellArg( ":lang=$code" );
 14+ $ok = 0;
 15+ $cmd = "fc-match $code";
 16+ $suggestion = wfShellExec( $cmd, $ok );
 17+ wfDebugLog( 'fcfont', "$cmd returned $ok" );
 18+ if ( $ok !== 0 ) {
 19+ wfDebugLog( 'fcfont', "fc-match error output: $suggestion" );
 20+ return false;
 21+ }
 22+
 23+ $pattern = '/^(.*?): "(.*)" "(.*)"$/';
 24+ $matches = array();
 25+ if ( !preg_match( $pattern, $suggestion, $matches ) ) {
 26+ wfDebugLog( 'fcfont', "fc-match: return format not understood: $suggestion" );
 27+ return false;
 28+ }
 29+
 30+ list( , $file, $family, $type ) = $matches;
 31+ wfDebugLog( 'fcfont', "fc-match: got $file: $family $type" );
 32+
 33+ $file = wfEscapeShellArg( $file );
 34+ $family = wfEscapeShellArg( $family );
 35+ $type = wfEscapeShellArg( $type );
 36+ $cmd = "fc-list $family $type $code file | grep $file";
 37+
 38+ $candidates = trim( wfShellExec( $cmd, $ok ) );
 39+
 40+ wfDebugLog( 'fcfont', "$cmd returned $ok" );
 41+ if ( $ok !== 0 ) {
 42+ wfDebugLog( 'fcfont', "fc-list error output: $candidates" );
 43+ return false;
 44+ }
 45+
 46+ # trim spaces
 47+ $files = array_map( 'trim', explode( "\n", $candidates ) );
 48+ $count = count($files);
 49+ if ( !$count ) wfDebugLog( 'fcfont', "fc-list got zero canditates: $candidates" );
 50+
 51+ # remove the trailing ":"
 52+ $chosen = substr( $files[0], 0, -1 );
 53+
 54+ wfDebugLog( 'fcfont', "fc-list got $count candidates; using $chosen" );
 55+ return $chosen;
 56+ }
 57+
 58+}
\ No newline at end of file
Property changes on: trunk/extensions/Translate/utils/Font.php
___________________________________________________________________
Added: svn:eol-style
159 + native

Status & tagging log