Back to index

scribus-ng  1.3.4.dfsg+svn20071115
Functions
gsutil.h File Reference
#include <qpixmap.h>
#include <qstring.h>
#include <qstringlist.h>
#include "scribusapi.h"
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

QPixmap SCRIBUS_API LoadPDF (QString fn, int Page, int Size, int *w, int *h)
 GS utility fucntions Moved functions written or modified by various people over the time from util.cpp.
int SCRIBUS_API callGS (const QStringList &args_in, const QString device="")
 Call GhostScript synchronously and store output.
int SCRIBUS_API callGS (const QString &args_in, const QString device="")
int SCRIBUS_API convertPS2PS (QString in, QString out, const QStringList &opts, int level)
int SCRIBUS_API testGSAvailability (void)
int SCRIBUS_API testGSAvailability (QString gsPath)
int SCRIBUS_API testGSDeviceAvailability (QString device)
QString SCRIBUS_API getGSVersion ()
 Return gs version.
bool SCRIBUS_API getNumericGSVersion (int &major, int &minor)
QString SCRIBUS_API getGSDefaultExeName (void)

Function Documentation

int SCRIBUS_API callGS ( const QStringList &  args_in,
const QString  device = "" 
)

Call GhostScript synchronously and store output.

The gs commands are all similar and consist of a few constant arguments, the variable arguments and the end arguments which are also invariant. It will always use -q -dNOPAUSE and will always end with -c showpage -c quit. It also does automatic device selection unless overridden, and uses the user's antialiasing preferences and font search path.

Shell metacharacters are not expanded - that includes quotes.

See also:
System .
Parameters:
args_inCustom arguments to GhostScript
deviceGS device to use (defaults to an image device if omitted)

Definition at line 50 of file gsutil.cpp.

{
       QString cmd;
       QStringList args;
       PrefsManager* prefsManager = PrefsManager::instance();
       args.append( getShortPathName(prefsManager->ghostscriptExecutable()) );
       args.append( "-q" );
       args.append( "-dNOPAUSE" );
       args.append( "-dQUIET" );
       args.append( "-dPARANOIDSAFER" );
       args.append( "-dBATCH" );
       // Choose rendering device
       if (!device.isEmpty())
              args.append( QString("-sDEVICE=%1").arg(device) ); // user specified device
       else if (ScCore->havePNGAlpha() != 0)
              args.append( "-sDEVICE=png16m" );
       else
              args.append( "-sDEVICE=pngalpha" );
       // and antialiasing
       if (prefsManager->appPrefs.gs_AntiAliasText)
              args.append( "-dTextAlphaBits=4" );
       if (prefsManager->appPrefs.gs_AntiAliasGraphics)
              args.append( "-dGraphicsAlphaBits=4" );

       // Add any extra font paths being used by Scribus to gs's font search path
       PrefsContext *pc = PrefsManager::instance()->prefsFile->getContext("Fonts");
       PrefsTable *extraFonts = pc->getTable("ExtraFontDirs");
       const char sep = ScPaths::envPathSeparator;
       if (extraFonts->getRowCount() >= 1)
              cmd = QString("-sFONTPATH=%1").arg(QDir::convertSeparators(extraFonts->get(0,0)));
       for (int i = 1; i < extraFonts->getRowCount(); ++i)
              cmd += QString("%1%2").arg(sep).arg(QDir::convertSeparators(extraFonts->get(i,0)));
       if( !cmd.isEmpty() )
              args.append( cmd );

       args += args_in;
       args.append("-c");
       args.append("showpage");
//     qDebug(args.join(" "));
       return System( args );
}

Here is the call graph for this function:

Here is the caller graph for this function:

int SCRIBUS_API callGS ( const QString &  args_in,
const QString  device = "" 
)

Definition at line 92 of file gsutil.cpp.

{
       PrefsManager* prefsManager=PrefsManager::instance();
       QString cmd1 = getShortPathName(prefsManager->ghostscriptExecutable());
       cmd1 += " -q -dNOPAUSE -dQUIET -dPARANOIDSAFER -dBATCH";
       // Choose rendering device
       if (!device.isEmpty())
              // user specified device
              cmd1 += " -sDEVICE="+device;
       else if (ScCore->havePNGAlpha() != 0)
              cmd1 += " -sDEVICE=png16m";
       else
              cmd1 += " -sDEVICE=pngalpha";
       // and antialiasing
       if (prefsManager->appPrefs.gs_AntiAliasText)
              cmd1 += " -dTextAlphaBits=4";
       if (prefsManager->appPrefs.gs_AntiAliasGraphics)
              cmd1 += " -dGraphicsAlphaBits=4";

       // Add any extra font paths being used by Scribus to gs's font search path
       PrefsContext *pc = PrefsManager::instance()->prefsFile->getContext("Fonts");
       PrefsTable *extraFonts = pc->getTable("ExtraFontDirs");
#ifndef _WIN32
       if (extraFonts->getRowCount() >= 1)
              cmd1 += QString(" -sFONTPATH='%1'").arg(extraFonts->get(0,0));
       for (int i = 1; i < extraFonts->getRowCount(); ++i)
              cmd1 += QString(":'%1'").arg(extraFonts->get(i,0));
#else
       if (extraFonts->getRowCount() >= 1)
              cmd1 += QString(" -sFONTPATH=\"%1\"").arg(extraFonts->get(0,0));
       for (int i = 1; i < extraFonts->getRowCount(); ++i)
              cmd1 += QString(";\"%1\"").arg(extraFonts->get(i,0));
#endif

       // then add any user specified args and run gs
       cmd1 += " " + args_in + " -c showpage";
//     qDebug("Calling gs as: %s", cmd1.ascii());
       return system(cmd1.local8Bit());
}

Here is the call graph for this function:

int SCRIBUS_API convertPS2PS ( QString  in,
QString  out,
const QStringList &  opts,
int  level 
)

Definition at line 132 of file gsutil.cpp.

{
       PrefsManager* prefsManager=PrefsManager::instance();
       QStringList args;
       args.append( getShortPathName(prefsManager->ghostscriptExecutable()) );
       args.append( "-q" );
       args.append( "-dQUIET" );
       args.append( "-dNOPAUSE" );
       args.append( "-dPARANOIDSAFER" );
       args.append( "-dBATCH" );
       if( level == 2 )
       {
              int major = 0, minor = 0;
              // ps2write cannot be detected with testGSAvailability()
              // so determine availability according to gs version.
              getNumericGSVersion(major, minor);
              if ((major >=8 && minor >= 53) || major > 8)
                     args.append( "-sDEVICE=ps2write" );
              else
              {
                     args.append( "-sDEVICE=pswrite" );
                     args.append( QString("-dLanguageLevel=%1").arg(level) );
              }
                     
       }
       else
       {
              args.append( "-sDEVICE=pswrite" );
              if(level <= 3)
                     args.append( QString("-dLanguageLevel=%1").arg(level) );
       }
       args += opts;
       args.append( QString("-sOutputFile=%1").arg(QDir::convertSeparators(out)) );
       args.append( QDir::convertSeparators(in) );
       int ret = System( args );
       return ret;
}

Here is the call graph for this function:

Here is the caller graph for this function:

QString SCRIBUS_API getGSDefaultExeName ( void  )

Definition at line 240 of file gsutil.cpp.

{
       QString gsName;
#if defined _WIN32
       // Try to locate GhostScript thanks to the registry
       DWORD size;
       HKEY hKey;
       LONG retValue;
       DWORD regType = REG_SZ;
       char regVersion[MAX_PATH];
       char regPath[MAX_PATH];
       char gsPath[MAX_PATH];

       // Set gsName to its default value
       gsName = "gswin32c.exe";

       // Search AFPL Ghostscript first as it has more chance to be up to date
       if( RegOpenKey(HKEY_LOCAL_MACHINE, "SOFTWARE\\AFPL Ghostscript", &hKey) == ERROR_SUCCESS )
              strcpy(regPath, "SOFTWARE\\AFPL Ghostscript");
       else if( RegOpenKey(HKEY_LOCAL_MACHINE, "SOFTWARE\\GPL Ghostscript", &hKey) == ERROR_SUCCESS )
              strcpy(regPath, "SOFTWARE\\GPL Ghostscript");
       else
              return gsName;

       // Search the first SubKey corresponding to the version key
       size = sizeof(regVersion) - 1;
       retValue = RegEnumKeyEx(hKey, 0, regVersion, &size, NULL, NULL, NULL, NULL);
       RegCloseKey(hKey);
       if( retValue != ERROR_SUCCESS )
              return gsName;

       strcat(regPath, "\\");
       strcat(regPath, regVersion);

       // Get the GS_DLL Value
       if (RegOpenKey(HKEY_LOCAL_MACHINE, regPath, &hKey) != ERROR_SUCCESS)
          return gsName;
       size = sizeof(gsPath) - 1;
       retValue = RegQueryValueEx(hKey, "GS_DLL", 0, &regType, (LPBYTE) gsPath, &size);
       RegCloseKey(hKey);
       if( retValue != ERROR_SUCCESS )
              return gsName;

       // We now have GhostScript dll path, but we want gswin32c.exe
       // Normally gswin32c.exe and gsdll.dll are in the same directory
       gsName = gsPath;
       size = gsName.findRev("\\");
       if(size <= 0)
              return QString("gswin32c.exe");
       gsName = gsName.left(size + 1);
       gsName += "gswin32c.exe";

       // Check GhostScript executable existence.
       QFileInfo fInfo(gsName);
       if( fInfo.exists() )
              gsName.replace("\\", "/"); // Return a qt-styled path
       else
              gsName = "gswin32c.exe";

#else
       gsName = "gs";
#endif
       return gsName;
}

Return gs version.

If gs couldn't be found or there was a problem parsing output, return false (in which case minor and major have undefined values).

Return values:
QStringversion or false on error

Definition at line 200 of file gsutil.cpp.

{
       QString gsVer;
       QStringList args;
       QString gsExe = getShortPathName(PrefsManager::instance()->ghostscriptExecutable());
       args.append(gsExe.local8Bit());
       args.append(QString("--version").local8Bit());
       QProcess proc(args);
       proc.setCommunication(QProcess::Stdout);
       proc.start();
       while(proc.isRunning())
       {
#ifndef _WIN32
              usleep(5000);
#else
              Sleep(5);
#endif
              qApp->processEvents();
       }
       if(!proc.exitStatus())
              gsVer = proc.readLineStdout();
       return gsVer;
}

Here is the call graph for this function:

Here is the caller graph for this function:

bool SCRIBUS_API getNumericGSVersion ( int major,
int minor 
)

Definition at line 225 of file gsutil.cpp.

{
       QString gs_ver_string(getGSVersion());
       // gs's version string is of the form MAJOR.MINOR, so look for the .
       // then convert to numbers. 7.07 will become (7,7) for example.
       bool success = false;
       major = gs_ver_string.section('.', 0, 0).toInt(&success);
       if (!success)
              return false;
       minor = gs_ver_string.section('.', 1, 1).toInt(&success);
       if (!success)
              return false;
       return true;
}

Here is the call graph for this function:

Here is the caller graph for this function:

QPixmap SCRIBUS_API LoadPDF ( QString  fn,
int  Page,
int  Size,
int w,
int h 
)

GS utility fucntions Moved functions written or modified by various people over the time from util.cpp.

Author:
Craig Bradney

Definition at line 305 of file gsutil.cpp.

{
       QString tmp, cmd1, cmd2;
       QString pdfFile = QDir::convertSeparators(fn);
       QString tmpFile = QDir::convertSeparators(ScPaths::getTempFileDir() + "sc.png");
       QPixmap pm;
       int ret = -1;
       tmp.setNum(Page);
       QStringList args;
       args.append("-r72");
//     args.append("-sOutputFile=\""+tmpFile+"\"");
       args.append("-sOutputFile="+tmpFile);
       args.append("-dFirstPage="+tmp);
       args.append("-dLastPage="+tmp);
//     args.append("\""+pdfFile+"\"");
       args.append(pdfFile);
       ret = callGS(args);
       if (ret == 0)
       {
              QImage image;
              image.load(tmpFile);
              unlink(tmpFile);
              QImage im2;
              *h = image.height();
              *w = image.width();
              double sx = image.width() / static_cast<double>(Size);
              double sy = image.height() / static_cast<double>(Size);
              double t = (sy < sx ? sx : sy);
              im2 = image.smoothScale(static_cast<int>(image.width() / t), static_cast<int>(image.height() / t));
              pm.convertFromImage(im2);
              QPainter p;
              p.begin(&pm);
              p.setBrush(Qt::NoBrush);
              p.setPen(Qt::black);
              p.drawRect(0, 0, pm.width(), pm.height());
              p.end();
              im2.detach();
       }
       return pm;
}

Here is the call graph for this function:

Definition at line 170 of file gsutil.cpp.

{
       QStringList args;
       PrefsManager* prefsManager = PrefsManager::instance();
       int ret = testGSAvailability(prefsManager->ghostscriptExecutable());
       return ret;
}

Here is the call graph for this function:

Here is the caller graph for this function:

int SCRIBUS_API testGSAvailability ( QString  gsPath)

Definition at line 178 of file gsutil.cpp.

{
       QStringList args;
       args.append( getShortPathName(gsPath) );
       args.append( "-h" );
       int ret = System( args );
       return ret;
}

Here is the call graph for this function:

Definition at line 187 of file gsutil.cpp.

{
       QStringList args;
       PrefsManager* prefsManager = PrefsManager::instance();
       args.append( getShortPathName(prefsManager->ghostscriptExecutable()) );
       args.append( QString("-sDEVICE=%1").arg( device ) );
       args.append( "-c" );
       args.append( "quit" );
       int ret = System( args );
       return ret;
}

Here is the call graph for this function: