]> git.lyx.org Git - lyx.git/blob - src/convert/lyxconvert.cpp
Add license and author infos
[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
12 #include <iostream>
13 #include <QApplication>
14 #include <QImage>
15 #include <QFile>
16 #include <QPainter>
17 #if (QT_VERSION >= 0x050300)
18 #include <QPdfWriter>
19 #endif
20
21
22 const char * basename(const char * name)
23 {
24 #ifdef Q_OS_WIN
25         const char * slashpos = strrchr(name, '\\');
26 #else
27         const char * slashpos = strrchr(name, '/');
28 #endif
29
30         if (NULL != slashpos) name = ++slashpos ;
31         return name;
32 }
33
34
35 void usage(const char * name)
36 {
37         std::cerr << "Usage: " << name
38                 << " [-f infmt] [-t outfmt] input output" << std::endl;
39         exit(1);
40 }
41
42
43 void version(const char * name)
44 {
45         std::cerr << name << ": version 1.0" << std::endl;
46         exit(0);
47 }
48
49
50 bool isFileExt(const char * name, const char * ext)
51 {
52         const char * dotpos = strrchr(name, '.');
53         return NULL != dotpos && !strcmp(++dotpos, ext);
54 }
55
56
57 int main(int argc, char **argv)
58 {
59         int arg = 1;
60         const char * iformat  = NULL;
61         const char * oformat  = NULL;
62         const char * infile   = NULL;
63         const char * outfile  = NULL;
64         const char * myname   = basename(argv[0]);
65         char * qtargs[] = {
66                 argv[0],
67                 (char*)"-platform", (char*)"minimal",
68                 NULL };
69         int  qtargsc = sizeof(qtargs) / sizeof(qtargs[0]) - 1;
70         bool debug = (1 == 0);
71
72         while (arg < argc) {
73                 if ('-' == argv[arg][0] && !strcmp(argv[arg], "-platform")) {
74                         qtargs[2] = argv[++arg]; arg++ ;
75                 } else if ('-' == argv[arg][0] && 'f' == argv[arg][1]) {
76                         iformat = argv[++arg]; arg++ ;
77                 } else if ('-' == argv[arg][0] && 't' == argv[arg][1]) {
78                         oformat = argv[++arg]; arg++ ;
79                 } else if ('-' == argv[arg][0] && 'd' == argv[arg][1]) {
80                         debug = (1 == 1); arg++;
81                 } else if ('-' == argv[arg][0] && 'V' == argv[arg][1]) {
82                         version(myname);
83                 } else if ('-' == argv[arg][0]) {
84                         usage(myname);
85                 } else if (NULL == infile) {
86                         infile = argv[arg++];
87                 } else if (NULL == outfile) {
88                         outfile = argv[arg++];
89                         if (NULL == oformat) {
90                                 if (isFileExt(outfile, "pdf")) {
91                                         oformat = "pdf";
92                                 } else if (isFileExt(outfile, "eps")) {
93                                         oformat = "eps";
94                                 }
95                         }
96                 } else {
97                         usage(myname);
98                 }
99         }
100         if (NULL == infile || NULL == outfile) {
101                 usage(myname);
102         }
103
104         QApplication app(qtargsc, &qtargs[0]);
105         QFile ifile(QString::fromLocal8Bit(infile));
106         QImage img;
107
108         if (debug) {
109                 std::cerr << myname << ": platform is " << (NULL == qtargs[2] ? "default" : qtargs[2]) << std::endl;
110         }
111
112         if (debug) {
113                 std::cerr << myname << ": Load file '" << infile <<
114                         "', infmt is '" << (NULL == iformat ? "auto" : iformat) << "'" << std::endl;
115         }
116         if (!ifile.exists()) {
117                 std::cerr << myname << ": Image file '" << infile << "' doesn't exist" << std::endl;
118                 return 2;
119         } else if (!img.load(ifile.fileName(), iformat)) {
120                 std::cerr << myname << ": Cannot load image '" << infile << "'" << std::endl;
121                 return 3;
122         }
123
124         if (debug) {
125                 std::cerr << myname << ": Save converted image to file '" << outfile <<
126                         "', outfmt is '" << (NULL == oformat ? "auto" : oformat) << "'" << std::endl;
127         }
128         if (NULL != oformat && !strcmp(oformat, "eps")) {
129                 std::cerr << myname << ": Conversion of images to format '" << oformat << "' is not supported" << std::endl;
130                 return 4;
131         } else if (NULL != oformat && !strcmp(oformat, "pdf")) {
132 #if (QT_VERSION >= 0x050300)
133                 QSize size = img.size();
134                 QPdfWriter pdfwriter(QString::fromLocal8Bit(outfile));
135                 int dpi = pdfwriter.logicalDpiX();
136                 QPageSize pagesize(size * qreal(72.0 / dpi));
137                 QMarginsF margins(0, 0, 0, 0);
138                 QPageLayout pagelayout(pagesize, QPageLayout::Portrait, margins);
139                 pdfwriter.setPageLayout(pagelayout);
140                 QPainter painter(&pdfwriter);
141                 painter.drawImage(0, 0, img);
142                 painter.end();
143 #else
144                 std::cerr << myname << ": Conversion of images to format '" << oformat << "' is not supported" << std::endl;
145                 return 4;
146 #endif
147         } else if (!img.save(QString::fromLocal8Bit(outfile), oformat)) {
148                 std::cerr << myname << ": Cannot save converted image to '" << outfile << "'" << std::endl;
149                 return 5;
150         }
151         return 0;
152 }
153