X-Git-Url: https://git.heureux-cyclage.org/?a=blobdiff_plain;ds=sidebyside;f=includes%2FDjVuImage.php;h=75df0fd59f09ebbccf331e9cf24f0101dbed8d09;hb=05879eeb55b2412918b0b29509807a1d456336fe;hp=1e423565cd67a41d63652ff032b2b3810c85d88b;hpb=bd23ec29c6f3d34b4ef45a8a70921660de2282aa;p=lhc%2Fweb%2Fwiklou.git
diff --git a/includes/DjVuImage.php b/includes/DjVuImage.php
index 1e423565cd..75df0fd59f 100644
--- a/includes/DjVuImage.php
+++ b/includes/DjVuImage.php
@@ -29,13 +29,13 @@
* File format docs are available in source package for DjVuLibre:
* http://djvulibre.djvuzone.org/
*
- * @addtogroup Media
+ * @ingroup Media
*/
class DjVuImage {
function __construct( $filename ) {
$this->mFilename = $filename;
}
-
+
/**
* Check if the given file is indeed a valid DjVu image file
* @return bool
@@ -44,27 +44,27 @@ class DjVuImage {
$info = $this->getInfo();
return $info !== false;
}
-
-
+
+
/**
* Return data in the style of getimagesize()
* @return array or false on failure
*/
public function getImageSize() {
$data = $this->getInfo();
-
+
if( $data !== false ) {
$width = $data['width'];
$height = $data['height'];
-
+
return array( $width, $height, 'DjVu',
"width=\"$width\" height=\"$height\"" );
}
return false;
}
-
+
// ---------
-
+
/**
* For debugging; dump the IFF chunk structure
*/
@@ -77,7 +77,7 @@ class DjVuImage {
$this->dumpForm( $file, $chunkLength, 1 );
fclose( $file );
}
-
+
private function dumpForm( $file, $length, $indent ) {
$start = ftell( $file );
$secondary = fread( $file, 4 );
@@ -90,7 +90,7 @@ class DjVuImage {
// FIXME: Would be good to replace this extract() call with something that explicitly initializes local variables.
extract( unpack( 'a4chunk/NchunkLength', $chunkHeader ) );
echo str_repeat( ' ', $indent * 4 ) . "$chunk $chunkLength\n";
-
+
if( $chunk == 'FORM' ) {
$this->dumpForm( $file, $chunkLength, $indent + 1 );
} else {
@@ -102,23 +102,25 @@ class DjVuImage {
}
}
}
-
+
function getInfo() {
+ wfSuppressWarnings();
$file = fopen( $this->mFilename, 'rb' );
+ wfRestoreWarnings();
if( $file === false ) {
wfDebug( __METHOD__ . ": missing or failed file read\n" );
return false;
}
-
+
$header = fread( $file, 16 );
$info = false;
-
+
if( strlen( $header ) < 16 ) {
wfDebug( __METHOD__ . ": too short file header\n" );
} else {
// FIXME: Would be good to replace this extract() call with something that explicitly initializes local variables.
extract( unpack( 'a4magic/a4form/NformLength/a4subtype', $header ) );
-
+
if( $magic != 'AT&T' ) {
wfDebug( __METHOD__ . ": not a DjVu file\n" );
} elseif( $subtype == 'DJVU' ) {
@@ -134,7 +136,7 @@ class DjVuImage {
fclose( $file );
return $info;
}
-
+
private function readChunk( $file ) {
$header = fread( $file, 8 );
if( strlen( $header ) < 8 ) {
@@ -145,16 +147,16 @@ class DjVuImage {
return array( $chunk, $length );
}
}
-
+
private function skipChunk( $file, $chunkLength ) {
fseek( $file, $chunkLength, SEEK_CUR );
-
+
if( $chunkLength & 0x01 == 1 && !feof( $file ) ) {
// padding byte
fseek( $file, 1, SEEK_CUR );
}
}
-
+
private function getMultiPageInfo( $file, $formLength ) {
// For now, we'll just look for the first page in the file
// and report its information, hoping others are the same size.
@@ -164,7 +166,7 @@ class DjVuImage {
if( !$chunk ) {
break;
}
-
+
if( $chunk == 'FORM' ) {
$subtype = fread( $file, 4 );
if( $subtype == 'DJVU' ) {
@@ -177,18 +179,18 @@ class DjVuImage {
$this->skipChunk( $file, $length );
}
} while( $length != 0 && !feof( $file ) && ftell( $file ) - $start < $formLength );
-
+
wfDebug( __METHOD__ . ": multi-page DJVU file contained no pages\n" );
return false;
}
-
+
private function getPageInfo( $file, $formLength ) {
list( $chunk, $length ) = $this->readChunk( $file );
if( $chunk != 'INFO' ) {
wfDebug( __METHOD__ . ": expected INFO chunk, got '$chunk'\n" );
return false;
}
-
+
if( $length < 9 ) {
wfDebug( __METHOD__ . ": INFO should be 9 or 10 bytes, found $length\n" );
return false;
@@ -198,7 +200,7 @@ class DjVuImage {
wfDebug( __METHOD__ . ": INFO chunk cut off\n" );
return false;
}
-
+
// FIXME: Would be good to replace this extract() call with something that explicitly initializes local variables.
extract( unpack(
'nwidth/' .
@@ -208,7 +210,7 @@ class DjVuImage {
'vresolution/' .
'Cgamma', $data ) );
# Newer files have rotation info in byte 10, but we don't use it yet.
-
+
return array(
'width' => $width,
'height' => $height,
@@ -222,7 +224,7 @@ class DjVuImage {
* @return string
*/
function retrieveMetaData() {
- global $wgDjvuToXML, $wgDjvuDump;
+ global $wgDjvuToXML, $wgDjvuDump, $wgDjvuTxt;
if ( isset( $wgDjvuDump ) ) {
# djvudump is faster as of version 3.5
# http://sourceforge.net/tracker/index.php?func=detail&aid=1704049&group_id=32953&atid=406583
@@ -240,6 +242,30 @@ class DjVuImage {
} else {
$xml = null;
}
+ # Text layer
+ if ( isset( $wgDjvuTxt ) ) {
+ wfProfileIn( 'djvutxt' );
+ $cmd = wfEscapeShellArg( $wgDjvuTxt ) . ' --detail=page ' . wfEscapeShellArg( $this->mFilename ) ;
+ wfDebug( __METHOD__.": $cmd\n" );
+ $txt = wfShellExec( $cmd, $retval );
+ wfProfileOut( 'djvutxt' );
+ if( $retval == 0) {
+ # Get rid of invalid UTF-8, strip control characters
+ if( is_callable( 'iconv' ) ) {
+ wfSuppressWarnings();
+ $txt = iconv( "UTF-8","UTF-8//IGNORE", $txt );
+ wfRestoreWarnings();
+ } else {
+ $txt = UtfNormal::cleanUp( $txt );
+ }
+ $txt = preg_replace( "/[\013\035\037]/", "", $txt );
+ $txt = htmlspecialchars($txt);
+ $txt = preg_replace( "/\((page\s[\d-]*\s[\d-]*\s[\d-]*\s[\d-]*\s*\"([^<]*?)\"\s*|)\)/s", "", $txt );
+ $txt = "\n\n\n" . $txt . "\n\n";
+ $xml = preg_replace( "//", "", $xml );
+ $xml = $xml . $txt. '' ;
+ }
+ }
return $xml;
}
@@ -318,14 +344,14 @@ EOT;
}
if ( preg_match( '/^ *INFO *\[\d*\] *DjVu *(\d+)x(\d+), *\w*, *(\d+) *dpi, *gamma=([0-9.-]+)/', $line, $m ) ) {
- $xml .= Xml::tags( 'OBJECT',
+ $xml .= Xml::tags( 'OBJECT',
array(
#'data' => '',
#'type' => 'image/x.djvu',
'height' => $m[2],
'width' => $m[1],
#'usemap' => '',
- ),
+ ),
"\n" .
Xml::element( 'PARAM', array( 'name' => 'DPI', 'value' => $m[3] ) ) . "\n" .
Xml::element( 'PARAM', array( 'name' => 'GAMMA', 'value' => $m[4] ) ) . "\n"
@@ -338,6 +364,3 @@ EOT;
return false;
}
}
-
-
-?>