dvirenderer_prescan.cpp.svn-base

来自「okular」· SVN-BASE 代码 · 共 801 行 · 第 1/2 页

SVN-BASE
801
字号
// -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil; c-brace-offset: 0; -*-// dviRenderer_prescan.cpp//// Part of KDVI - A DVI previewer for the KDE desktop environment//// (C) 2003--2004 Stefan Kebekus// Distributed under the GPL#include <config.h>#include "dviRenderer.h"#include "dvi.h"#include "dviFile.h"#include "kvs_debug.h"#include "prebookmark.h"#include "psgs.h"#include "TeXFont.h"#include <klocale.h>#include <kmessagebox.h>#include <kmimetype.h>#include <kprocio.h>#include <kprogressbar.h>#include <Q3MemArray>#include <QApplication>#include <QDir>#include <QFileInfo>#include <QImage>#include <QPaintDevice>#include <QTextStream>extern QPainter foreGroundPaint;extern void parse_special_argument(const QString& strg, const char* argument_name, int* variable);//#define DEBUG_PRESCANvoid dviRenderer::prescan_embedPS(char *cp, quint8 *beginningOfSpecialCommand){#ifdef  DEBUG_PRESCAN  kDebug(kvs::dvi) << "dviRenderer::prescan_embedPS( cp = " << cp << " ) " << endl;#endif  // Encapsulated Postscript File  if (strncasecmp(cp, "PSfile=", 7) != 0)    return;  QString command(cp+7);  QString include_command = command.simplified();  // The line is supposed to start with "..ile=", and then comes the  // filename. Figure out what the filename is and stow it away. Of  // course, this does not work if the filename contains spaces  // (already the simplified() above is wrong). If you have  // files like this, go away.  QString EPSfilename = include_command;  EPSfilename.truncate(EPSfilename.indexOf(' '));  // Strip enclosing quotation marks which are included by some LaTeX  // macro packages (but not by others). This probably means that  // graphic files are no longer found if the filename really does  // contain quotes, but we don't really care that much.  if ((EPSfilename.at(0) == '\"') && (EPSfilename.at(EPSfilename.length()-1) == '\"'))    EPSfilename = EPSfilename.mid(1,EPSfilename.length()-2);  // Now locate the Gfx file on the hard disk...  EPSfilename = ghostscript_interface::locateEPSfile(EPSfilename, baseURL);  // If the file is neither a PostScript not a PDF file, we exit here.  // The graphic file is later read when the page is rendered.  KMimeType::Ptr const mime_type =    KMimeType::findByFileContent(EPSfilename);  QString const & mime_type_name = mime_type?mime_type->name():"";  bool const is_ps_file  = (mime_type_name == "application/postscript" ||                            mime_type_name == "image/x-eps");  bool const is_pdf_file = (!is_ps_file &&                            mime_type_name == "application/pdf");  if (!(is_ps_file || is_pdf_file))    return;  QString originalFName = EPSfilename;  embedPS_progress->setLabel(i18n("Embedding %1", EPSfilename));  qApp->processEvents();  // If the EPSfilename really points to a PDF file, convert that file now.  if (is_pdf_file)    EPSfilename = dviFile->convertPDFtoPS(EPSfilename);  if (!QFile::exists(EPSfilename)) {    // Find the number of the page    quint32 currentOffset = beginningOfSpecialCommand - dviFile->dvi_Data();    int page=0;    for(; page < dviFile->total_pages; page++)      if ((dviFile->page_offset[page] <= currentOffset) && (currentOffset <= dviFile->page_offset[page+1]))        break;    if (is_pdf_file)      errorMsg += i18n("Page %1: The PDF file <strong>%2</strong> could not be converted to PostScript.<br>", page+1, originalFName);    else      errorMsg += i18n("Page %1: The PostScript file <strong>%2</strong> could not be found.<br>", page+1, originalFName);    embedPS_progress->progressBar()->advance(1);    qApp->processEvents();    return;  }  // Now parse the arguments.  int  llx     = 0;  int  lly     = 0;  int  urx     = 0;  int  ury     = 0;  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.indexOf(' '));  parse_special_argument(include_command, "llx=", &llx);  parse_special_argument(include_command, "lly=", &lly);  parse_special_argument(include_command, "urx=", &urx);  parse_special_argument(include_command, "ury=", &ury);  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.indexOf(" clip"); // -1 if clip keyword is not present, >= 0 otherwise  // Generate the PostScript commands to be included  QString PS = QString("ps: @beginspecial %1 @llx %2 @lly %3 @urx %4 @ury").arg(llx).arg(lly).arg(urx).arg(ury);  if (rwi != 0)    PS.append( QString(" %1 @rwi").arg(rwi) );  if (rhi != 0)    PS.append( QString(" %1 @rhi").arg(rhi) );  if (angle != 0)    PS.append( QString(" %1 @angle").arg(angle) );  if (clip != -1)    PS.append(" @clip");  PS.append( " @setspecial\n" );  QFile file( EPSfilename );  if ( file.open( QIODevice::ReadOnly ) ) {    QTextStream stream( &file );    while ( !stream.atEnd() ) {      PS += stream.readLine().section( '%', 0, 0);      PS += "\n";    }    file.close();  }  PS.append( "@endspecial" );  PS = PS.simplified();  _isModified = true;  quint32 lengthOfOldSpecial = command_pointer - beginningOfSpecialCommand;  quint32 lengthOfNewSpecial = PS.length()+5;  Q3MemArray<quint8> newDVI(dviFile->size_of_file + lengthOfNewSpecial-lengthOfOldSpecial);  quint8 *commandPtrSav = command_pointer;  quint8 *endPtrSav = end_pointer;  end_pointer = newDVI.data() + dviFile->size_of_file + lengthOfNewSpecial-lengthOfOldSpecial;  memcpy(newDVI.data(), dviFile->dvi_Data(), beginningOfSpecialCommand-dviFile->dvi_Data());  command_pointer = newDVI.data()+(beginningOfSpecialCommand-dviFile->dvi_Data());  command_pointer[0] = XXX4;  command_pointer++;  writeUINT32(PS.length());  memcpy(newDVI.data()+(beginningOfSpecialCommand-dviFile->dvi_Data())+5, PS.toLatin1(), PS.length() );  memcpy(newDVI.data()+(beginningOfSpecialCommand-dviFile->dvi_Data())+lengthOfNewSpecial, beginningOfSpecialCommand+lengthOfOldSpecial,         dviFile->size_of_file-(beginningOfSpecialCommand-dviFile->dvi_Data())-lengthOfOldSpecial );  // Adjust page pointers in the DVI file  dviFile->size_of_file = dviFile->size_of_file + lengthOfNewSpecial-lengthOfOldSpecial;  end_pointer = newDVI.data() + dviFile->size_of_file;  quint32 currentOffset = beginningOfSpecialCommand-dviFile->dvi_Data();  for(int i=0; i < dviFile->total_pages; i++) {    if (dviFile->page_offset[i] > currentOffset) {      dviFile->page_offset[i] = dviFile->page_offset[i] + lengthOfNewSpecial-lengthOfOldSpecial;      command_pointer = dviFile->page_offset[i] + newDVI.data() + 4*10 + 1;      quint32 a = readUINT32();      if (a > currentOffset) {        a = a + lengthOfNewSpecial-lengthOfOldSpecial;        command_pointer = dviFile->page_offset[i] + newDVI.data() + 4*10 + 1;        writeUINT32(a);      }    }  }  dviFile->beginning_of_postamble            = dviFile->beginning_of_postamble + lengthOfNewSpecial - lengthOfOldSpecial;  dviFile->page_offset[int(dviFile->total_pages)] = dviFile->beginning_of_postamble;  command_pointer = newDVI.data() + dviFile->beginning_of_postamble + 1;  quint32 a = readUINT32();  if (a > currentOffset) {    a = a + lengthOfNewSpecial - lengthOfOldSpecial;    command_pointer = newDVI.data() + dviFile->beginning_of_postamble + 1;    writeUINT32(a);  }  command_pointer = newDVI.data() + dviFile->size_of_file - 1;  while((*command_pointer == TRAILER) && (command_pointer > newDVI.data()))    command_pointer--;  command_pointer -= 4;  writeUINT32(dviFile->beginning_of_postamble);  command_pointer -= 4;  command_pointer = commandPtrSav;  end_pointer     = endPtrSav;  // Modify all pointers to point to the newly allocated memory  command_pointer = newDVI.data() + (command_pointer - dviFile->dvi_Data()) + lengthOfNewSpecial-lengthOfOldSpecial;  end_pointer = newDVI.data() + (end_pointer - dviFile->dvi_Data())  + lengthOfNewSpecial-lengthOfOldSpecial;  dviFile->setNewData(newDVI);  embedPS_progress->progressBar()->advance(1);  qApp->processEvents();  return;}void dviRenderer::prescan_removePageSizeInfo(char *cp, quint8 *beginningOfSpecialCommand){#ifdef  DEBUG_PRESCAN  kDebug(kvs::dvi) << "dviRenderer::prescan_embedPS( cp = " << cp << " ) " << endl;#endif  // Encapsulated Postscript File  if (strncasecmp(cp, "papersize=", 10) != 0)    return;  for (quint8 *ptr=beginningOfSpecialCommand; ptr<command_pointer; ptr++)    *ptr = NOP;}void dviRenderer::prescan_ParsePapersizeSpecial(const QString& _cp){#ifdef DEBUG_PRESCAN  kDebug(kvs::dvi) << "Papersize-Special : papersize" << _cp << endl;#endif  QString cp = _cp.simplified();  if (cp[0] == '=') {    cp = cp.mid(1);    dviFile->suggestedPageSize = new pageSize;    dviFile->suggestedPageSize->setPageSize(cp);  } else    printErrorMsgForSpecials(i18n("The papersize data '%1' could not be parsed.", cp));  return;}void dviRenderer::prescan_ParseBackgroundSpecial(const QString& cp){  QColor col = parseColorSpecification(cp.trimmed());  if (col.isValid())    for(quint16 page=current_page; page < dviFile->total_pages; page++)      PS_interface->setBackgroundColor(page, col);  return;}void dviRenderer::prescan_ParseHTMLAnchorSpecial(const QString& _cp){  QString cp = _cp;  cp.truncate(cp.indexOf('"'));  Length l;  l.setLength_in_inch(currinf.data.dvi_v/(resolutionInDPI*shrinkfactor));  anchorList[cp] = Anchor(current_page+1, l);}void dviRenderer::prescan_ParsePSHeaderSpecial(const QString& cp){#ifdef DEBUG_PRESCAN  kDebug(kvs::dvi) << "PostScript-special, header " << cp.latin1() << endl;#endif  QString _file = cp;  // If the file is not found in the current directory, use kpsewhich  // to find it.  if (!QFile::exists(_file)) {    // Otherwise, use kpsewhich to find the eps file.    KProcIO proc;    proc << "kpsewhich" << cp;    proc.start(KProcess::Block);    proc.readln(_file);  }  if (QFile::exists(_file))    PS_interface->PostScriptHeaderString->append( QString(" (%1) run\n").arg(_file) );}void dviRenderer::prescan_ParsePSBangSpecial(const QString& cp){#ifdef DEBUG_PRESCAN  kDebug(kvs::dvi) << "PostScript-special, literal header " << cp.latin1() << endl;#endif  PS_interface->PostScriptHeaderString->append( " @defspecial \n" );  PS_interface->PostScriptHeaderString->append( cp );  PS_interface->PostScriptHeaderString->append( " @fedspecial \n" );}void dviRenderer::prescan_ParsePSQuoteSpecial(const QString& cp){#ifdef DEBUG_PRESCAN  kError(kvs::dvi) << "PostScript-special, literal PostScript " << cp.latin1() << endl;#endif  double PS_H = (currinf.data.dvi_h*300.0)/(65536*1200)-300;  double PS_V = (currinf.data.dvi_v*300.0)/1200 - 300;  PostScriptOutPutString->append( QString(" %1 %2 moveto\n").arg(PS_H).arg(PS_V) );  PostScriptOutPutString->append( " @beginspecial @setspecial \n" );  PostScriptOutPutString->append( cp );  PostScriptOutPutString->append( " @endspecial \n" );}void dviRenderer::prescan_ParsePSSpecial(const QString& cp){#ifdef DEBUG_PRESCAN  kDebug(kvs::dvi) << "PostScript-special, direct PostScript " << cp << endl;#endif  // Unfortunately, in some TeX distribution the hyperref package uses  // the dvips driver by default, rather than the hypertex driver. As  // a result, the DVI files produced are full of PostScript that  // specifies links and anchors, and KDVI would call the ghostscript  // interpreter for every page which makes it really slow. This is a  // major nuisance, so that we try to filter and interpret the  // hypertex generated PostScript here.  if (cp.startsWith("ps:SDict begin")) {    // We suspect this may be hyperref generated nonsense. Let's check    // for some known code that hyperref generates.    if (cp == "ps:SDict begin H.S end")      return; // start of hyperref rectangle    if (cp == "ps:SDict begin H.R end")      return; // end of hyperref rectangle    if (cp.endsWith("H.A end"))      return; // end of hyperref anchor    if (cp.endsWith("H.L end"))      return; // end of hyperref link    if (cp.startsWith("ps:SDict begin /product where{pop product(Distiller)"))      return; // hyperref tries to work around Distiller bug    if (cp.startsWith("ps:SDict begin [") && cp.endsWith(" pdfmark end")) {  // hyperref definition of link/anchor/bookmark/etc      if (cp.contains("/DEST")) { // The PostScript code defines an anchor        QString anchorName = cp.section('(', 1, 1).section(')', 0, 0);        Length l;        l.setLength_in_inch(currinf.data.dvi_v/(resolutionInDPI*shrinkfactor));        anchorList[anchorName] = Anchor(current_page+1, l);      }      // The PostScript code defines a bookmark      if (cp.contains("/Dest") && cp.contains("/Title"))        prebookmarks.append(PreBookmark(PDFencodingToQString(cp.section('(', 2, 2).section(')', 0, 0)),                                        cp.section('(', 1, 1).section(')', 0, 0),                                        cp.section('-', 1, 1).section(' ', 0, 0).toUInt()                                        ));      return;    }  }  double PS_H = (currinf.data.dvi_h*300.0)/(65536*1200)-300;  double PS_V = (currinf.data.dvi_v*300.0)/1200 - 300;  if (cp.indexOf("ps::[begin]", 0, Qt::CaseInsensitive) == 0) {    PostScriptOutPutString->append( QString(" %1 %2 moveto\n").arg(PS_H).arg(PS_V) );    PostScriptOutPutString->append( QString(" %1\n").arg(cp.mid(11)) );  } else {    if (cp.indexOf("ps::[end]", 0, Qt::CaseInsensitive) == 0) {      PostScriptOutPutString->append( QString(" %1\n").arg(cp.mid(9)) );    } else {      if (cp.indexOf("ps::", 0, Qt::CaseInsensitive) == 0) {        PostScriptOutPutString->append( QString(" %1\n").arg(cp.mid(4)) );      } else {        PostScriptOutPutString->append( QString(" %1 %2 moveto\n").arg(PS_H).arg(PS_V) );        PostScriptOutPutString->append( QString(" %1\n").arg(cp.mid(3)) );      }    }  }}void dviRenderer::prescan_ParsePSFileSpecial(const QString& cp){#ifdef DEBUG_PRESCAN  kDebug(kvs::dvi) << "epsf-special: psfile=" << cp <<endl;#endif  QString include_command = cp.simplified();

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?