PageRenderTime 48ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/PaperSizeScroll/_snip_pieces/xsl-fo_open/srcm/struct/GSInterlace.cpp

http://fop-miniscribus.googlecode.com/
C++ | 428 lines | 322 code | 57 blank | 49 comment | 46 complexity | 84789f2be3764a9853be18d8bd479b37 MD5 | raw file
Possible License(s): LGPL-3.0, GPL-2.0
  1. #include "GSInterlace.h"
  2. /* encode to name */
  3. QString encodeBase64( QString xml )
  4. {
  5. QByteArray text;
  6. text.append(xml);
  7. return text.toBase64();
  8. }
  9. /* decode to name */
  10. QString decodeBase64( QString xml )
  11. {
  12. QByteArray xcode("");;
  13. xcode.append(xml);
  14. QByteArray precode(QByteArray::fromBase64(xcode));
  15. QString notetxt = precode.data();
  16. return notetxt;
  17. }
  18. /* read the contenet of a local file as qstring */
  19. QString ReadFile(const QString fullFileName)
  20. {
  21. QString inside = "";
  22. QFile file(fullFileName);
  23. if (file.exists()) {
  24. if (file.open(QFile::ReadOnly | QFile::Text)) {
  25. inside = QString::fromUtf8(file.readAll());
  26. file.close();
  27. }
  28. }
  29. return inside;
  30. }
  31. void OpenDeskBrowser( QUrl loc )
  32. {
  33. #if defined Q_WS_MAC
  34. QProcess *m = new QProcess();
  35. QStringList macs;
  36. macs << loc.toString(); /* oeffnet der default browser */
  37. m->startDetached(QString("open") , macs );
  38. ///////std::cout << "OpenDesktop [" << qPrintable(macs.join(" ")) << "]" << std::endl;
  39. return;
  40. #endif
  41. #if defined Q_WS_WIN
  42. QString fullFileName = loc.toString();
  43. if (fullFileName.startsWith("http://", Qt::CaseInsensitive) ||
  44. fullFileName.startsWith("https://", Qt::CaseInsensitive) ||
  45. fullFileName.startsWith("ftp://", Qt::CaseInsensitive) ||
  46. fullFileName.startsWith("news://", Qt::CaseInsensitive) ||
  47. fullFileName.startsWith("mailto:", Qt::CaseInsensitive) ||
  48. fullFileName.startsWith("webdav://", Qt::CaseInsensitive) )
  49. {
  50. /* all ok */
  51. } else {
  52. fullFileName.prepend("file:///");
  53. }
  54. bool wr = QDesktopServices::openUrl(QUrl(fullFileName));
  55. if (!wr) {
  56. QMessageBox::warning(0, qApp->tr("Error"),qApp->tr("Window Unable to open action file or dir %1").arg(loc.toString()));
  57. }
  58. return;
  59. #endif
  60. /* linux */
  61. bool r = QDesktopServices::openUrl(loc);
  62. if (!r) {
  63. QMessageBox::warning(0, qApp->tr("Error"),qApp->tr("Linux Unable to open action file or dir %1").arg(loc.toString()));
  64. }
  65. }
  66. /* simple mkdir */
  67. bool Cache( const QString dirpath )
  68. {
  69. QDir dira(dirpath);
  70. if ( dira.mkpath(dirpath) ) {
  71. return true;
  72. } else {
  73. return false;
  74. }
  75. }
  76. /* os path from sistem */
  77. QString PathConvert( QString path )
  78. {
  79. return QDir::toNativeSeparators ( path );
  80. }
  81. /* write a file to utf-8 format */
  82. bool fwriteutf8( const QString file ,QString xml)
  83. {
  84. if (file.contains("/", Qt::CaseInsensitive)) {
  85. QString ultimacartellaaperta = file.left(file.lastIndexOf("/"))+"/";
  86. QDir dira(ultimacartellaaperta);
  87. if ( dira.mkpath(ultimacartellaaperta) ) { } else {
  88. return false;
  89. }
  90. }
  91. QTextCodec *codecx;
  92. codecx = QTextCodec::codecForMib(106);
  93. QFile f( file );
  94. if ( f.open( QFile::WriteOnly | QFile::Text ) )
  95. {
  96. QTextStream sw( &f );
  97. sw.setCodec(codecx);
  98. sw << xml;
  99. f.close();
  100. return true;
  101. }
  102. return false;
  103. }
  104. QString getGSLinuxPath( QString apps )
  105. {
  106. QStringList potential_paths;
  107. potential_paths.append("/usr/local/bin");
  108. potential_paths.append("/sw/bin"); /* to use on mac as same */
  109. potential_paths.append("/opt/bin");
  110. QProcess *process = new QProcess(NULL);
  111. process->setReadChannelMode(QProcess::MergedChannels);
  112. QStringList env = process->systemEnvironment();
  113. env.replaceInStrings(QRegExp("^PATH=(.*)", Qt::CaseInsensitive), "PATH=\\1;"+potential_paths.join(";"));
  114. process->setEnvironment(env);
  115. process->start( QString("which") , QStringList() << apps , QIODevice::ReadOnly );
  116. if (!process->waitForFinished()) {
  117. return QString();
  118. } else {
  119. QString finder = process->readAll().trimmed();
  120. if (finder.endsWith(apps,Qt::CaseInsensitive)) {
  121. ///////////// qDebug() << "### finder " << finder;
  122. return finder;
  123. } else {
  124. return QString();
  125. }
  126. }
  127. }
  128. /* find gpl GhostScript path or exe */
  129. QString getGSDefaultExeName()
  130. {
  131. QString gsName;
  132. QString gVersion;
  133. #if defined Q_WS_WIN
  134. QFileInfo Pinfo;
  135. // Try to locate GhostScript thanks to the qsetting
  136. gsName = "gswin32c.exe";
  137. QSettings softs("HKEY_LOCAL_MACHINE\\Software",QSettings::NativeFormat);
  138. QStringList allsoftware = softs.childGroups();
  139. QStringList gsonly = allsoftware.filter(QRegExp("Ghostscript"));
  140. //////////qDebug() << "### gsonly " << gsonly;
  141. for (int i = 0; i < gsonly.size(); ++i) {
  142. const QString RealName = gsonly.at(i); /* realpath */
  143. if (RealName.contains("Ghostscript")) {
  144. //////////qDebug() << "### soft " << RealName;
  145. for(int e=1;e<99;e++) {
  146. /* check version 8 ++ 99 down */
  147. gVersion = QString("8.%1").arg(100 - e);
  148. if (softs.value(RealName+"/"+gVersion+"/GS_DLL").toString().size() > 6 ) {
  149. Pinfo.setFile(softs.value(RealName+"/"+gVersion+"/GS_DLL").toString());
  150. return gsName.prepend(Pinfo.absolutePath()+"/");
  151. }
  152. /* check version 7 ++ 99 down */
  153. gVersion = QString("7.%1").arg(100 - e);
  154. if (softs.value(RealName+"/"+gVersion+"/GS_DLL").toString().size() > 6 ) {
  155. Pinfo.setFile(softs.value(RealName+"/"+gVersion+"/GS_DLL").toString());
  156. return gsName.prepend(Pinfo.absolutePath()+"/");
  157. }
  158. }
  159. }
  160. }
  161. /* win not having GPL Ghostscript ! */
  162. gsName = "";
  163. return gsName;
  164. #endif
  165. #if defined Q_WS_MAC
  166. QString pathfinder = getGSLinuxPath(QString("gs")); /* which simple append systems path mac + linux */
  167. if (pathfinder.size() > 2 && pathfinder.endsWith("gs",Qt::CaseInsensitive)) {
  168. return pathfinder;
  169. }
  170. QStringList paths;
  171. paths << "/usr/local/bin/gs" << "/usr/bin/gs" << "/bin/gs" << "/sw/bin/gs" << "/opt/bin/gs";
  172. QFileInfo gsp;
  173. for (int i = 0; i < paths.size(); ++i) {
  174. gsp.setFile (paths.at(i));
  175. if (gsp.exists()) {
  176. return paths.at(i);
  177. }
  178. }
  179. gsName = "gs";
  180. return gsName;
  181. #endif
  182. #if defined Q_WS_X11
  183. return getGSLinuxPath();
  184. #endif
  185. /* forum http://www.qtcentre.org/forum/f-qt-programming-2/t-qsettings-read-only-avaiable-10254.html */
  186. }
  187. /* find gpl GhostScript version */
  188. double getGSVersion()
  189. {
  190. QProcess process;
  191. QString Stversion;
  192. bool ok;
  193. double GS_version = 0.00;
  194. QRegExp regExp1(" (\\d+)\\.(\\d+).*");
  195. process.setReadChannelMode(QProcess::MergedChannels);
  196. process.start( getGSDefaultExeName() , QStringList() << "-v");
  197. if (!process.waitForFinished()) {
  198. return GS_version;
  199. } else {
  200. QString pu = process.readAll();
  201. QStringList line1 = pu.split("\n");
  202. int pos1 = regExp1.indexIn(line1.at(0)); /* only first line */
  203. if (pos1 > 0) {
  204. Stversion = QString("%1.%2").arg(regExp1.cap(1).toInt()).arg(regExp1.cap(2).toInt());
  205. double GS_version = Stversion.toDouble(&ok);
  206. if (ok && GS_version > 5) {
  207. return GS_version;
  208. }
  209. } else {
  210. return GS_version;
  211. }
  212. }
  213. return GS_version;
  214. }
  215. double JavaVersion()
  216. {
  217. QProcess process;
  218. QString Stversion;
  219. bool ok;
  220. double js_version = 0.00;
  221. QRegExp regExp1(" (\\d+)\\.(\\d+).*");
  222. process.setReadChannelMode(QProcess::MergedChannels);
  223. process.start( "java" , QStringList() << "-version");
  224. if (!process.waitForFinished()) {
  225. return js_version;
  226. } else {
  227. QString pu = process.readAll();
  228. /////QStringList line1 = pu.split("\n");
  229. ////////qDebug() << "### jsversion " << pu;
  230. int pos1 = regExp1.indexIn(pu); /* only first line */
  231. if (pos1 > 0) {
  232. Stversion = QString("%1.%2").arg(regExp1.cap(1).toInt()).arg(regExp1.cap(2).toInt());
  233. double js_version = Stversion.toDouble(&ok);
  234. if (ok && js_version > 1) {
  235. return js_version;
  236. }
  237. } else {
  238. return js_version;
  239. }
  240. }
  241. return js_version;
  242. }
  243. /*
  244. #define _GSCACHE_ \
  245. QString("%1/.ghosti_CACHE/").arg(QDir::homePath())
  246. remove dir after works
  247. */
  248. int callGS( const QStringList args )
  249. {
  250. const QString startnow = QDir::currentPath();
  251. const QString GhostScriptPath = getGSDefaultExeName();
  252. QDir::setCurrent(_GSCACHE_);
  253. QString cmd1 = GhostScriptPath + " ";
  254. cmd1 += args.join(" ");
  255. int fax = -1;
  256. #if defined Q_WS_MAC
  257. fax = system(cmd1.toLocal8Bit());
  258. QDir::setCurrent(startnow);
  259. return fax;
  260. #endif
  261. QProcess *process = new QProcess(NULL);
  262. process->setReadChannelMode(QProcess::MergedChannels);
  263. process->start( GhostScriptPath , args , QIODevice::ReadOnly );
  264. if (!process->waitForFinished()) {
  265. fax = -1;
  266. } else {
  267. QString ghostcomment = process->readAll().trimmed();
  268. //////qDebug() << "ghostcomment-> " << ghostcomment;
  269. fax = 0;
  270. }
  271. QDir::setCurrent(startnow);
  272. return fax;
  273. }
  274. /*
  275. extern inline int Funzionaaaaaaa_______lGS( const QStringList args )
  276. {
  277. const QString startnow = QDir::currentPath();
  278. QDir::setCurrent(_GSCACHE_);
  279. QString cmd1 = getGSDefaultExeName() + " ";
  280. cmd1 += args.join(" ");
  281. int fax = -1;
  282. fax = system(cmd1.toLocal8Bit());
  283. QDir::setCurrent(startnow);
  284. return fax;
  285. }
  286. */
  287. QPixmap LoadPS( QString fn , const QString arguments_append )
  288. {
  289. const QString pdfFile = PathConvert(fn);
  290. QChar letter('A' + (qrand() % 26));
  291. QDateTime timer1( QDateTime::currentDateTime() );
  292. const QString qttmpFile = _GSCACHE_+QString("%2_%1.png").arg( timer1.toString("yyyy-MM-dd-HH-mm-ss-zzz") ).arg(letter);
  293. QFileInfo fitmp(qttmpFile);
  294. const int VersionGS = getGSVersion();
  295. QFile lastaction(fitmp.absoluteFilePath());
  296. lastaction.remove();
  297. QPixmap pm;
  298. int ret = -1;
  299. QStringList args;
  300. if (arguments_append.size() > 3) {
  301. args.append(arguments_append);
  302. }
  303. if (VersionGS >=8) {
  304. args.append("-sDEVICE=png16m");
  305. args.append("-dGraphicsAlphaBits=4");
  306. args.append("-r72");
  307. args.append("-o");
  308. args.append(fitmp.fileName());
  309. args.append(pdfFile);
  310. } else {
  311. args.append("-sDEVICE=pnggray");
  312. args.append("-r72x72");
  313. args.append("-sOutputFile="+fitmp.fileName());
  314. args.append("-q");
  315. args.append(pdfFile);
  316. }
  317. ret = callGS(args);
  318. //////////qDebug() << "### ret " << ret << " VersionGS->" << VersionGS;
  319. if (ret == 0)
  320. {
  321. QPixmap tmpimage(fitmp.absoluteFilePath());
  322. lastaction.remove();
  323. return tmpimage;
  324. }
  325. return pm;
  326. }
  327. /*
  328. version < 8
  329. args.append("-r72x72");
  330. args.append("-sOutputFile="+imageiosFileCache);
  331. args.append("-q");
  332. args.append(pdfFile);
  333. */
  334. QPixmap LoadPDF(QString fn, int Page, int w )
  335. {
  336. QString tmp, cmd1, cmd2;
  337. const QString pdfFile = PathConvert(fn);
  338. const QString tmpFile = PathConvert(QDir::homePath()+"/sctodaytmps.png");
  339. const QString qttmpFile = QDir::homePath()+"/sctodaytmps.png";
  340. QPixmap pm;
  341. tmp.setNum(Page);
  342. int ret = -1;
  343. tmp.setNum(Page);
  344. QStringList args;
  345. args.append("-sDEVICE=png16m");
  346. args.append("-r72");
  347. args.append("-dGraphicsAlphaBits=4");
  348. args.append("-o");
  349. args.append(tmpFile);
  350. args.append("-dFirstPage="+tmp);
  351. args.append("-dLastPage="+tmp);
  352. args.append(pdfFile);
  353. ret = callGS(args);
  354. ////////qDebug() << "### ret " << ret;
  355. if (ret == 0)
  356. {
  357. QPixmap tmpimage(qttmpFile);
  358. QPixmap penna = tmpimage.scaledToWidth(w);
  359. tmpimage.detach();
  360. QFile lastaction(qttmpFile);
  361. lastaction.remove();
  362. QPainter p;
  363. p.begin(&penna);
  364. p.setBrush(Qt::NoBrush);
  365. p.setPen(QPen(QBrush(Qt::black),2,Qt::SolidLine));
  366. p.drawRect(0, 0, penna.width(), penna.height());
  367. p.end();
  368. return penna;
  369. }
  370. return pm;
  371. }