[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    branches/KDE/3.5/kdegraphics/kdvi
From:       Stefan Kebekus <stefan.kebekus () uni-bayreuth ! de>
Date:       2005-09-16 12:22:40
Message-ID: 1126873360.154999.22081.nullmailer () svn ! kde ! org
[Download RAW message or body]

SVN commit 461084 by kebekus:

fixes issue #112446


 M  +38 -0     dviFile.cpp  
 M  +31 -1     dviFile.h  
 M  +12 -5     dviRenderer_prescan.cpp  


--- branches/KDE/3.5/kdegraphics/kdvi/dviFile.cpp #461083:461084
@@ -52,6 +52,7 @@
 
 #include <kdebug.h>
 #include <klocale.h>
+#include <ktempfile.h>
 #include <qdir.h>
 #include <qfileinfo.h>
 #include <stdlib.h>
@@ -315,6 +316,11 @@
   kdDebug(4300) << "destroy dvi-file" << endl;
 #endif
 
+  // Delete converted PDF files
+  QMap<QString, QString>::Iterator it;
+  for ( it = convertedFiles.begin(); it != convertedFiles.end(); ++it ) 
+    QFile::remove(it.data());
+
   if (suggestedPageSize != 0)
     delete suggestedPageSize;
   if (font_pool != 0)
@@ -351,6 +357,38 @@
 }
 
 
+QString dvifile::convertPDFtoPS(const QString &PDFFilename)
+{
+  // Check if the PDFFile is known
+  QMap<QString, QString>::Iterator it =  convertedFiles.find(PDFFilename);
+  if (it != convertedFiles.end()) {
+    // PDF-File is known. Good.
+    return it.data();
+  }
+
+  // Get the name of a temporary file
+  KTempFile tmpfile(QString::null, ".ps");
+  QString convertedFileName = tmpfile.name();
+  tmpfile.close();
+  tmpfile.unlink();
+
+  // Use pdf2ps to do the conversion
+  KProcIO proc;
+  proc << "pdf2ps" << PDFFilename << convertedFileName;
+  if (proc.start(KProcess::Block) == false) 
+    convertedFileName = QString::null; // Indicates that conversion failed, won't try again.
+  if (!QFile::exists(convertedFileName))
+    convertedFileName = QString::null; // Indicates that conversion failed, won't try again.
+
+  // Save name of converted file to buffer, so PDF file won't be
+  // converted again, and files can be deleted when *this is
+  // deconstructed.
+  convertedFiles[PDFFilename] = convertedFileName;
+
+  return convertedFileName;
+}
+
+
 bool dvifile::saveAs(const QString &filename)
 {
   if (dvi_Data() == 0)
--- branches/KDE/3.5/kdegraphics/kdvi/dviFile.h #461083:461084
@@ -5,7 +5,7 @@
 // Class that represents a DVI file. Part of KDVI - A DVI previewing
 // plugin for kviewshell.
 //
-// (C) 2004 Stefan Kebekus. Distributed under the GPL.
+// (C) 2004--2005 Stefan Kebekus. Distributed under the GPL.
 //
 
 #ifndef _DVIFILE_H
@@ -96,7 +96,27 @@
       ordered. Worse, page numbers need not be unique. This method
       renumbers the pages. */
   void           renumber();
+  
+  /** PDF to PS file conversion 
 
+  This utility method takes the name of a PDF-file, and attempts to
+  convert it to a PS file. The dvifile internally keeps a list of
+  converted files, to do two thigs:
+
+  - convert files only once.
+
+  - delete all converted files on destruction
+
+  @warning The internal buffer can lead to difficulties if filenames
+    of PDF-files are not unique: if the content of a PDF file is
+    changed and this method is called a second time with the same file
+    name, the method will then NOT convert the file, but simply return
+    the name from the buffer
+
+  @returns The name of the PS file, or QString::null on failure.
+  */
+  QString convertPDFtoPS(const QString &PDFFilename);
+
  private:
   /** process_preamble reads the information in the preamble and
       stores it into global variables for later use. */
@@ -116,6 +136,16 @@
   double         cmPerDVIunit;
 
   QMemArray<Q_UINT8>  dviData;
+
+
+  /** Map of filenames for converted PDF files
+
+  This map contains names of PDF files that were converted to
+  PostScript. The key is the name of the PDF file, the data the name
+  of the associated PS file, or QString::null, if the file could not
+  be converted. The PS files are deleted when the DVI-file is
+  destructed. */
+  QMap<QString, QString> convertedFiles;
 };
 
 #endif //ifndef _DVIFILE_H
--- branches/KDE/3.5/kdegraphics/kdvi/dviRenderer_prescan.cpp #461083:461084
@@ -81,7 +81,11 @@
   
   // Now locate the Gfx file on the hard disk...
   EPSfilename = ghostscript_interface::locateEPSfile(EPSfilename, dviFile);
-  
+
+  // If the EPSfilename really points to a PDF file, convert that file now.
+  if (ending == "pdf")
+    EPSfilename = dviFile->convertPDFtoPS(EPSfilename);
+
   if (!QFile::exists(EPSfilename)) {
     // Find the number of the page
     Q_UINT32 currentOffset = beginningOfSpecialCommand - dviFile->dvi_Data();
@@ -94,7 +98,6 @@
     qApp->processEvents();
     return;
   }
-  
 
   // Now parse the arguments. 
   int  llx     = 0; 
@@ -406,6 +409,10 @@
   // Now locate the Gfx file on the hard disk...
   EPSfilename = ghostscript_interface::locateEPSfile(EPSfilename, dviFile);
   
+  // If the EPSfilename really points to a PDF file, convert that file now.
+  if (ending == "pdf")
+    EPSfilename = dviFile->convertPDFtoPS(EPSfilename);
+  
   // Now parse the arguments. 
   int  llx     = 0; 
   int  lly     = 0;
@@ -414,7 +421,7 @@
   int  rwi     = 0;
   int  rhi     = 0;
   int  angle   = 0;
-
+  
   // just to avoid ambiguities; the filename could contain keywords
   include_command = include_command.mid(include_command.find(' '));
   
@@ -425,9 +432,9 @@
   parse_special_argument(include_command, "rwi=", &rwi);
   parse_special_argument(include_command, "rhi=", &rhi);
   parse_special_argument(include_command, "angle=", &angle);
-
+  
   int clip=include_command.find(" clip"); // -1 if clip keyword is not present, >= 0 otherwise
-
+  
   if (QFile::exists(EPSfilename)) {
     double PS_H = (currinf.data.dvi_h*300.0)/(65536*1200)-300;
     double PS_V = (currinf.data.dvi_v*300.0)/1200 - 300;
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic