Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A Mako converter that uses the IDistiller class for speedy conversion of PostScript to PDF
/*
* Copyright (C) 2020-2021 Global Graphics Software Ltd. All rights reserved
*
* Simple sample application for converting PS to PDF using the JawsMako APIs.
*/
#include <algorithm>
#include <exception>
#include <iostream>
#include <stdexcept>
#include <wctype.h>
#include <jawsmako/jawsmako.h>
#include <jawsmako/distiller.h>
#include <edl/iabort.h>
using namespace JawsMako;
using namespace EDL;
// Print usage
static void usage(const String &argv0)
{
std::wcerr << L"Usage: " << argv0 << L" <source file> <output file>"; // required arguments
std::wcerr << L" [ parameter=value parameter=value ... ]" << std::endl; // all the other parameters
std::wcerr << L"Where: " << std::endl;
}
static void progressFunc(void *priv, float progress)
{
uint32 *lastProgress = (uint32 *) priv;
if (!lastProgress)
return;
uint32 iProgress = (uint32) (progress * 100);
if (iProgress - *lastProgress >= 25)
{
std::wcout << L"\t" << iProgress << L"%..." << std::flush;
*lastProgress = iProgress;
}
}
#ifdef _WIN32
int wmain(int argc, wchar_t *argv[])
#else
int main(int argc, char *argv[])
#endif
{
try
{
// Create our JawsMako instance.
// Currently only one instance is allowed at any one time.
IJawsMakoPtr jawsMako = IJawsMako::create();
uint32 progress = 0;
IAbortPtr abort = IAbort::create ();
IProgressTickPtr progressTick = IProgressTick::create ((IProgressTick::FloatProgressCallbackFunc) progressFunc, &progress);
IProgressMonitorPtr progressMonitor = IProgressMonitor::create (progressTick, abort);
int paramIdx;
String paramStr;
String paramStrLower;
// See if there are any optional arguments that modify how the harness behaves
for (paramIdx = 1; paramIdx < argc; paramIdx++)
{
#ifdef _WIN32
paramStr = argv[paramIdx];
#else
paramStr = U8StringToString(U8String(argv[paramIdx]));
#endif
paramStrLower = paramStr;
std::transform (paramStrLower.begin(), paramStrLower.end(), paramStrLower.begin(), ::tolower);
// Look for optional arguments
if (paramStrLower[0] == '-')
{
// It starts with a - but we don't recognize it so it's an invalid option
std::wcerr << L"Unknown option : " << paramStr << std::endl;
#ifdef _WIN32
usage(argv[0]);
#else
usage(U8StringToString(U8String(argv[0])));
#endif
return 1;
}
else
{
/* not an option, proceed to filenames */
break;
}
}
// There should be at least two arguments; a source file and a destination file.
if ((paramIdx + 2) > argc)
{
#ifdef _WIN32
usage(argv[0]);
#else
usage(U8StringToString(U8String(argv[0])));
#endif
return 1;
}
// Most JawsMako APIs can take UTF8 or Wide Character paths.
// To keep this cross-platform example simple, we'll use wchars in both cases.
String inputFilePath;
String outputFilePath;
#ifdef _WIN32
inputFilePath = argv[paramIdx++];
outputFilePath = argv[paramIdx++];
#else
inputFilePath = U8StringToString(U8String(argv[paramIdx++]));
outputFilePath = U8StringToString(U8String(argv[paramIdx++]));
#endif
// Create a distiller
IDistillerPtr distiller = IDistiller::create(jawsMako);
// Default parameters
distiller->setResolution(72.0f);
distiller->setCompressPages(false);
distiller->setSubsetFonts(false);
distiller->setEmbedFonts(false);
distiller->setColorImageCompression(IDistiller::eICNone);
distiller->setGrayImageCompression(IDistiller::eICNone);
distiller->setMonoImageCompression(IDistiller::eICNone);
distiller->setTransfers(IDistiller::eTRRemove);
// Set any parameters
for (; paramIdx < argc; paramIdx++)
{
// Split at the first equals sign
#ifdef _WIN32
U8String argument = StringToU8String(String(argv[paramIdx]));
#else
U8String argument = argv[paramIdx];
#endif
size_t equalsPos = argument.find('=');
if (equalsPos == U8String::npos)
{
std::wcerr << L"Bad argument - expected '=' - " << argv[paramIdx] << std::endl;
#ifdef _WIN32
usage(argv[0]);
#else
usage(U8StringToString(U8String(argv[0])));
#endif
return 1;
}
U8String parameter = argument.substr(0, equalsPos);
U8String value = argument.substr(equalsPos + 1);
// Set the output parameter
distiller->setParameter(parameter, value);
}
// Distill
distiller->distill(IInputStream::createFromFile(jawsMako, inputFilePath), IOutputStream::createToFile(jawsMako, outputFilePath), progressMonitor);
std::wcout << L"\tDone" << std::endl;
}
catch (IError &e)
{
String errorFormatString = getEDLErrorString(e.getErrorCode());
std::wcerr << L"Exception thrown: " << e.getErrorDescription(errorFormatString) << std::endl;
#ifdef _WIN32
// On windows, the return code allows larger numbers, and we can return the error code
return e.getErrorCode();
#else
// On other platforms, the exit code is masked to the low 8 bits. So here we just return
// a fixed value.
return 1;
#endif
}
catch (std::exception &e)
{
std::wcerr << L"std::exception thrown: " << e.what() << std::endl;
return 1;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment