2 set cflags=`env PKG_CONFIG_PATH=/usr/local/qt5/lib/pkgconfig pkg-config --cflags Qt5Widgets`
3 set libs=`env PKG_CONFIG_PATH=/usr/local/qt5/lib/pkgconfig pkg-config --libs --static Qt5Widgets`
4 g++ -std=gnu++11 $cflags lyxconvert.cpp -o lyxconvert $libs
7 #include <QApplication>
11 #if (QT_VERSION >= 0x050300)
16 const char * basename(const char * name)
19 const char * slashpos = strrchr(name, '\\');
21 const char * slashpos = strrchr(name, '/');
24 if (NULL != slashpos) name = ++slashpos ;
29 void usage(const char * name)
31 std::cerr << "Usage: " << name
32 << " [-f infmt] [-t outfmt] input output" << std::endl;
37 void version(const char * name)
39 std::cerr << name << ": version 1.0" << std::endl;
44 bool isFileExt(const char * name, const char * ext)
46 const char * dotpos = strrchr(name, '.');
47 return NULL != dotpos && !strcmp(++dotpos, ext);
51 int main(int argc, char **argv)
54 const char * iformat = NULL;
55 const char * oformat = NULL;
56 const char * infile = NULL;
57 const char * outfile = NULL;
58 const char * myname = basename(argv[0]);
61 (char*)"-platform", (char*)"minimal",
63 int qtargsc = sizeof(qtargs) / sizeof(qtargs[0]) - 1;
64 bool debug = (1 == 0);
67 if ('-' == argv[arg][0] && !strcmp(argv[arg], "-platform")) {
68 qtargs[2] = argv[++arg]; arg++ ;
69 } else if ('-' == argv[arg][0] && 'f' == argv[arg][1]) {
70 iformat = argv[++arg]; arg++ ;
71 } else if ('-' == argv[arg][0] && 't' == argv[arg][1]) {
72 oformat = argv[++arg]; arg++ ;
73 } else if ('-' == argv[arg][0] && 'd' == argv[arg][1]) {
74 debug = (1 == 1); arg++;
75 } else if ('-' == argv[arg][0] && 'V' == argv[arg][1]) {
77 } else if ('-' == argv[arg][0]) {
79 } else if (NULL == infile) {
81 } else if (NULL == outfile) {
82 outfile = argv[arg++];
83 if (NULL == oformat) {
84 if (isFileExt(outfile, "pdf")) {
86 } else if (isFileExt(outfile, "eps")) {
94 if (NULL == infile || NULL == outfile) {
98 QApplication app(qtargsc, &qtargs[0]);
99 QFile ifile(QString::fromLocal8Bit(infile));
103 std::cerr << myname << ": platform is " << (NULL == qtargs[2] ? "default" : qtargs[2]) << std::endl;
107 std::cerr << myname << ": Load file '" << infile <<
108 "', infmt is '" << (NULL == iformat ? "auto" : iformat) << "'" << std::endl;
110 if (!ifile.exists()) {
111 std::cerr << myname << ": Image file '" << infile << "' doesn't exist" << std::endl;
113 } else if (!img.load(ifile.fileName(), iformat)) {
114 std::cerr << myname << ": Cannot load image '" << infile << "'" << std::endl;
119 std::cerr << myname << ": Save converted image to file '" << outfile <<
120 "', outfmt is '" << (NULL == oformat ? "auto" : oformat) << "'" << std::endl;
122 if (NULL != oformat && !strcmp(oformat, "eps")) {
123 std::cerr << myname << ": Conversion of images to format '" << oformat << "' is not supported" << std::endl;
125 } else if (NULL != oformat && !strcmp(oformat, "pdf")) {
126 #if (QT_VERSION >= 0x050300)
127 QSize size = img.size();
128 QPdfWriter pdfwriter(QString::fromLocal8Bit(outfile));
129 int dpi = pdfwriter.logicalDpiX();
130 QPageSize pagesize(size * qreal(72.0 / dpi));
131 QMarginsF margins(0, 0, 0, 0);
132 QPageLayout pagelayout(pagesize, QPageLayout::Portrait, margins);
133 pdfwriter.setPageLayout(pagelayout);
134 QPainter painter(&pdfwriter);
135 painter.drawImage(0, 0, img);
138 std::cerr << myname << ": Conversion of images to format '" << oformat << "' is not supported" << std::endl;
141 } else if (!img.save(QString::fromLocal8Bit(outfile), oformat)) {
142 std::cerr << myname << ": Cannot save converted image to '" << outfile << "'" << std::endl;