]> git.lyx.org Git - lyx.git/blob - src/convert/lyxconvert.cpp
Provide proper fallback if a bibliography processor is not found
[lyx.git] / src / convert / lyxconvert.cpp
1 /**
2  * \file lyxconvert.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Stephan Witt
7  * \author Enrico Forestieri
8  *
9  * Full author contact details are available in file CREDITS.
10  *
11  * The code implements an utility program using the Qt-Framework
12  * to convert an image from a given format to another one.
13  * The image format of the files is either auto detected by Qt or
14  * may explicitely specified with command line arguments.
15  *
16  * Syntax:
17  * lyxconvert [-d] [-f infmt] [-t outfmt] inputfile outputfile
18  *  -d   turn on debug messages
19  *  -f   format of input file (from)
20  *  -t   format of output file (to)
21  *
22  * Example to convert a compressed SVG image to PNG:
23  * lyxconvert image.svgz image.png
24  */
25
26 #include <iostream>
27 #include <QApplication>
28 #include <QImage>
29 #include <QFile>
30 #include <QPainter>
31 #if (QT_VERSION >= 0x050300)
32 #include <QPdfWriter>
33 #endif
34
35
36 const char * basename(const char * name)
37 {
38 #ifdef Q_OS_WIN
39         const char * slashpos = strrchr(name, '\\');
40 #else
41         const char * slashpos = strrchr(name, '/');
42 #endif
43
44         if (NULL != slashpos) name = ++slashpos ;
45         return name;
46 }
47
48
49 void usage(const char * name)
50 {
51         std::cerr << "Usage: " << name
52                 << " [-f infmt] [-t outfmt] input output" << std::endl;
53         exit(1);
54 }
55
56
57 void version(const char * name)
58 {
59         std::cerr << name << ": version 1.0" << std::endl;
60         exit(0);
61 }
62
63
64 bool isFileExt(const char * name, const char * ext)
65 {
66         const char * dotpos = strrchr(name, '.');
67         return NULL != dotpos && !strcmp(++dotpos, ext);
68 }
69
70
71 int main(int argc, char **argv)
72 {
73         int arg = 1;
74         const char * iformat  = NULL;
75         const char * oformat  = NULL;
76         const char * infile   = NULL;
77         const char * outfile  = NULL;
78         const char * myname   = basename(argv[0]);
79         char * qtargs[] = {
80                 argv[0],
81                 (char*)"-platform", (char*)"minimal",
82                 NULL };
83         int  qtargsc = sizeof(qtargs) / sizeof(qtargs[0]) - 1;
84         bool debug = (1 == 0);
85
86         while (arg < argc) {
87                 if ('-' == argv[arg][0] && !strcmp(argv[arg], "-platform")) {
88                         qtargs[2] = argv[++arg]; arg++ ;
89                 } else if ('-' == argv[arg][0] && 'f' == argv[arg][1]) {
90                         iformat = argv[++arg]; arg++ ;
91                 } else if ('-' == argv[arg][0] && 't' == argv[arg][1]) {
92                         oformat = argv[++arg]; arg++ ;
93                 } else if ('-' == argv[arg][0] && 'd' == argv[arg][1]) {
94                         debug = (1 == 1); arg++;
95                 } else if ('-' == argv[arg][0] && 'V' == argv[arg][1]) {
96                         version(myname);
97                 } else if ('-' == argv[arg][0]) {
98                         usage(myname);
99                 } else if (NULL == infile) {
100                         infile = argv[arg++];
101                 } else if (NULL == outfile) {
102                         outfile = argv[arg++];
103                         if (NULL == oformat) {
104                                 if (isFileExt(outfile, "pdf")) {
105                                         oformat = "pdf";
106                                 } else if (isFileExt(outfile, "eps")) {
107                                         oformat = "eps";
108                                 }
109                         }
110                 } else {
111                         usage(myname);
112                 }
113         }
114         if (NULL == infile || NULL == outfile) {
115                 usage(myname);
116         }
117
118         QApplication app(qtargsc, &qtargs[0]);
119         QFile ifile(QString::fromLocal8Bit(infile));
120         QImage img;
121
122         if (debug) {
123                 std::cerr << myname << ": platform is " << (NULL == qtargs[2] ? "default" : qtargs[2]) << std::endl;
124         }
125
126         if (debug) {
127                 std::cerr << myname << ": Load file '" << infile <<
128                         "', infmt is '" << (NULL == iformat ? "auto" : iformat) << "'" << std::endl;
129         }
130         if (!ifile.exists()) {
131                 std::cerr << myname << ": Image file '" << infile << "' doesn't exist" << std::endl;
132                 return 2;
133         } else if (!img.load(ifile.fileName(), iformat)) {
134                 std::cerr << myname << ": Cannot load image '" << infile << "'" << std::endl;
135                 return 3;
136         }
137
138         if (debug) {
139                 std::cerr << myname << ": Save converted image to file '" << outfile <<
140                         "', outfmt is '" << (NULL == oformat ? "auto" : oformat) << "'" << std::endl;
141         }
142         if (NULL != oformat && !strcmp(oformat, "eps")) {
143                 std::cerr << myname << ": Conversion of images to format '" << oformat << "' is not supported" << std::endl;
144                 return 4;
145         } else if (NULL != oformat && !strcmp(oformat, "pdf")) {
146 #if (QT_VERSION >= 0x050300)
147                 QSize size = img.size();
148                 QPdfWriter pdfwriter(QString::fromLocal8Bit(outfile));
149                 int dpi = pdfwriter.logicalDpiX();
150                 QPageSize pagesize(size * qreal(72.0 / dpi));
151                 QMarginsF margins(0, 0, 0, 0);
152                 QPageLayout pagelayout(pagesize, QPageLayout::Portrait, margins);
153                 pdfwriter.setPageLayout(pagelayout);
154                 QPainter painter(&pdfwriter);
155                 painter.drawImage(0, 0, img);
156                 painter.end();
157 #else
158                 std::cerr << myname << ": Conversion of images to format '" << oformat << "' is not supported" << std::endl;
159                 return 4;
160 #endif
161         } else if (!img.save(QString::fromLocal8Bit(outfile), oformat)) {
162                 std::cerr << myname << ": Cannot save converted image to '" << outfile << "'" << std::endl;
163                 return 5;
164         }
165         return 0;
166 }
167