]> git.lyx.org Git - lyx.git/commitdiff
Add Qt-based fallback-converter for Mac to compensate missing ImageMagick convert...
authorStephan Witt <switt@lyx.org>
Sat, 30 Sep 2017 16:13:21 +0000 (18:13 +0200)
committerStephan Witt <switt@lyx.org>
Sat, 30 Sep 2017 16:13:37 +0000 (18:13 +0200)
Makefile.am
configure.ac
development/LyX-Mac-binary-release.sh
lib/scripts/convertDefault.py
src/Makefile.am
src/convert/Makefile.am [new file with mode: 0644]
src/convert/lyxconvert.cpp [new file with mode: 0644]

index c60a282b3941c86a0530b1b08d2b7fad197cc194..af9260dfba5afac5ed87dc5ca5d81b9690664639 100644 (file)
@@ -13,7 +13,7 @@ endif
 
 
 SUBDIRS = autotests config development po 3rdparty src sourcedoc lib \
-       $(CLIENT) src/tex2lyx
+       $(CLIENT) src/tex2lyx src/convert
 
 
 EXTRA_DIST = ANNOUNCE INSTALL.autoconf RELEASE-NOTES UPGRADING \
index 487361eebd20333d06e6269b39a80780c34169a6..7ed38f95a8ddd84462d3fcba610f584042550ba2 100644 (file)
@@ -395,6 +395,7 @@ AC_CONFIG_FILES([Makefile \
       src/Makefile \
       src/tex2lyx/Makefile \
       src/tex2lyx/tex2lyx.1:src/tex2lyx/tex2lyx.1in \
+      src/convert/Makefile \
       src/support/Makefile \
       src/frontends/Makefile \
       src/frontends/qt4/Makefile
index 6379876bba0f058ee938092d0239b588b839a034..075677b37e3e91d84899b5a086787df886b81b17 100644 (file)
@@ -622,7 +622,7 @@ framework_name() {
        echo "Frameworks/${1}.framework"
 }
 
-LYX_FILE_LIST="lyx lyxclient tex2lyx"
+LYX_FILE_LIST="lyx lyxclient tex2lyx lyxconvert"
 BUNDLE_PATH="Contents/MacOS"
 LYX_BUNDLE_PATH="${LyxAppPrefix}/${BUNDLE_PATH}"
 build_lyx() {
@@ -704,7 +704,7 @@ build_lyx() {
                                mv "${LYX_BUNDLE_PATH}/${file}"\
                                        "${LYX_BUNDLE_PATH}/${file}-${arch}" 
                        else
-                               echo ERROR: Cannot build and install LyX for ${arch}.
+                               echo ERROR: Cannot build and install ${file} for ${arch}.
                                exit 1
                        fi
                done
index 16e8f190ee625e4db6f40fd6453c554fd63cd4d3..e54b0668881af03fcd052cbbfabe64d1ffb1e62d 100644 (file)
@@ -34,6 +34,7 @@ if fout.close() != None:
 version = re_version.match(output)
 
 # Imagemagick by default
+im = False
 gm = False
 
 if version != None:
@@ -41,6 +42,7 @@ if version != None:
     minor = int(version.group(2))
     patch = int(version.group(3))
     version = hex(major * 65536 + minor * 256 + patch)
+    im = True
 else:
     # Try GraphicsMagick
     re_version = re.compile(r'^GraphicsMagick.*http:..www.GraphicsMagick.org.*$')
@@ -48,17 +50,25 @@ else:
     if version != None:
         gm = True
 
-opts = "-depth 8"
+if im or gm:
+    opts = "-depth 8"
+elif sys.platform == 'darwin':
+    command = 'lyxconvert'
 
 # If supported, add the -define option for pdf source formats
 if sys.argv[1] == 'pdf' and (version >= 0x060206 or gm):
     opts = '-define pdf:use-cropbox=true ' + opts
 
 # If supported, add the -flatten option for ppm target formats (see bug 4749)
-if sys.argv[3] == 'ppm' and (version >= 0x060305 or gm):
+if sys.argv[3] == 'ppm' and (im and version >= 0x060305 or gm):
     opts = opts + ' -flatten'
 
-if os.system(r'%s %s "%s" "%s"' % (command, opts, sys.argv[2], sys.argv[3] + ':' + sys.argv[4])) != 0:
+# print >> sys.stdout, command, sys.argv[2], sys.argv[4]
+if (im or gm) and os.system(r'%s %s "%s" "%s"' % (command, opts, sys.argv[2], sys.argv[3] + ':' + sys.argv[4])) != 0:
+    print >> sys.stderr, sys.argv[0], 'ERROR'
+    print >> sys.stderr, ('Execution of "%s" failed.' % command)
+    sys.exit(1)
+elif not im and not gm and sys.platform == 'darwin' and os.system(r'%s "%s" "%s"' % (command, sys.argv[2], sys.argv[4])) != 0:
     print >> sys.stderr, sys.argv[0], 'ERROR'
     print >> sys.stderr, ('Execution of "%s" failed.' % command)
     sys.exit(1)
index 0f190488280ebfb1cc3ef1a41fc80623612489ec..905617b4030cb26fa759cd652266ac2bf624b647 100644 (file)
@@ -11,7 +11,7 @@ if BUILD_CLIENT_SUBDIR
 CLIENT = client
 endif
 
-SUBDIRS = support frontends . $(CLIENT) tex2lyx
+SUBDIRS = support frontends . $(CLIENT) tex2lyx convert
 
 EXTRA_DIST = lyx_commit_hash.h.in \
        CMakeLists.txt \
diff --git a/src/convert/Makefile.am b/src/convert/Makefile.am
new file mode 100644 (file)
index 0000000..2457145
--- /dev/null
@@ -0,0 +1,32 @@
+include $(top_srcdir)/config/common.am
+
+#man_MANS = lyxconvert.1
+
+DEFAULT_INCLUDES =
+
+if INSTALL_MACOSX
+
+bin_PROGRAMS = lyxconvert
+
+AM_CPPFLAGS += -I$(top_srcdir)/src/convert \
+       $(QT_CPPFLAGS) \
+       -DQT_NO_CAST_TO_ASCII \
+       -DQT_NO_STL \
+       $(QT_INCLUDES)
+
+lyxconvert_SOURCES = \
+       lyxconvert.cpp
+
+lyxconvert_LDADD = \
+       $(QT_LIB) $(QT_LDFLAGS) \
+       $(ICONV_LIBS) $(ZLIB_LIBS) $(LIBSHLWAPI) $(LIBPSAPI)
+
+lyxconvert_LDFLAGS = -framework AppKit \
+       -Wl,-rpath,@loader_path/../Frameworks \
+       -Wl,-rpath,@executable_path/../Frameworks
+
+else
+
+bin_PROGRAMS =
+
+endif
diff --git a/src/convert/lyxconvert.cpp b/src/convert/lyxconvert.cpp
new file mode 100644 (file)
index 0000000..38ca49f
--- /dev/null
@@ -0,0 +1,145 @@
+/*
+set cflags=`env PKG_CONFIG_PATH=/usr/local/qt5/lib/pkgconfig pkg-config --cflags Qt5Widgets`
+set libs=`env PKG_CONFIG_PATH=/usr/local/qt5/lib/pkgconfig pkg-config --libs --static Qt5Widgets`
+g++ -std=gnu++11 $cflags lyxconvert.cpp -o lyxconvert $libs
+*/
+#include <iostream>
+#include <QApplication>
+#include <QImage>
+#include <QFile>
+#include <QPainter>
+#include <QPdfWriter>
+
+
+const char * basename(const char * name)
+{
+#ifdef Q_OS_WIN
+       const char * slashpos = strrchr(name, '\\');
+#else
+       const char * slashpos = strrchr(name, '/');
+#endif
+
+       if (NULL != slashpos) name = ++slashpos ;
+       return name;
+}
+
+
+void usage(const char * name)
+{
+       std::cerr << "Usage: " << name
+               << " [-f infmt] [-t outfmt] input output" << std::endl;
+       exit(1);
+}
+
+
+void version(const char * name)
+{
+       std::cerr << name << ": version 1.0" << std::endl;
+       exit(0);
+}
+
+
+bool isFileExt(const char * name, const char * ext)
+{
+       const char * dotpos = strrchr(name, '.');
+       return NULL != dotpos && !strcmp(++dotpos, ext);
+}
+
+
+int main(int argc, char **argv)
+{
+       int arg = 1;
+       const char * iformat  = NULL;
+       const char * oformat  = NULL;
+       const char * infile   = NULL;
+       const char * outfile  = NULL;
+       const char * myname   = basename(argv[0]);
+       char * qtargs[] = {
+               argv[0],
+               (char*)"-platform", (char*)"minimal",
+               NULL };
+       int  qtargsc = sizeof(qtargs) / sizeof(qtargs[0]) - 1;
+       bool debug = (1 == 0);
+
+       while (arg < argc) {
+               if ('-' == argv[arg][0] && !strcmp(argv[arg], "-platform")) {
+                       qtargs[2] = argv[++arg]; arg++ ;
+               } else if ('-' == argv[arg][0] && 'f' == argv[arg][1]) {
+                       iformat = argv[++arg]; arg++ ;
+               } else if ('-' == argv[arg][0] && 't' == argv[arg][1]) {
+                       oformat = argv[++arg]; arg++ ;
+               } else if ('-' == argv[arg][0] && 'd' == argv[arg][1]) {
+                       debug = (1 == 1); arg++;
+               } else if ('-' == argv[arg][0] && 'V' == argv[arg][1]) {
+                       version(myname);
+               } else if ('-' == argv[arg][0]) {
+                       usage(myname);
+               } else if (NULL == infile) {
+                       infile = argv[arg++];
+               } else if (NULL == outfile) {
+                       outfile = argv[arg++];
+                       if (NULL == oformat) {
+                               if (isFileExt(outfile, "pdf")) {
+                                       oformat = "pdf";
+                               } else if (isFileExt(outfile, "eps")) {
+                                       oformat = "eps";
+                               }
+                       }
+               } else {
+                       usage(myname);
+               }
+       }
+       if (NULL == infile || NULL == outfile) {
+               usage(myname);
+       }
+
+       QApplication app(qtargsc, &qtargs[0]);
+       QFile ifile(QString::fromLocal8Bit(infile));
+       QImage img;
+
+       if (debug) {
+               std::cerr << myname << ": platform is " << (NULL == qtargs[2] ? "default" : qtargs[2]) << std::endl;
+       }
+
+       if (debug) {
+               std::cerr << myname << ": Load file '" << infile <<
+                       "', infmt is '" << (NULL == iformat ? "auto" : iformat) << "'" << std::endl;
+       }
+       if (!ifile.exists()) {
+               std::cerr << myname << ": Image file '" << infile << "' doesn't exist" << std::endl;
+               return 2;
+       } else if (!img.load(ifile.fileName(), iformat)) {
+               std::cerr << myname << ": Cannot load image '" << infile << "'" << std::endl;
+               return 3;
+       }
+
+       if (debug) {
+               std::cerr << myname << ": Save converted image to file '" << outfile <<
+                       "', outfmt is '" << (NULL == oformat ? "auto" : oformat) << "'" << std::endl;
+       }
+       if (NULL != oformat && !strcmp(oformat, "eps")) {
+               std::cerr << myname << ": Conversion of images to format '" << oformat << "' is not supported" << std::endl;
+               return 4;
+       } else if (NULL != oformat && !strcmp(oformat, "pdf")) {
+#if (QT_VERSION >= 0x050300)
+               QSize size = img.size();
+               QPdfWriter pdfwriter(QString::fromLocal8Bit(outfile));
+               int dpi = pdfwriter.logicalDpiX();
+               QPageSize pagesize(size * qreal(72.0 / dpi));
+               QMarginsF margins(0, 0, 0, 0);
+               QPageLayout pagelayout(pagesize, QPageLayout::Portrait, margins);
+               pdfwriter.setPageLayout(pagelayout);
+               QPainter painter(&pdfwriter);
+               painter.drawImage(0, 0, img);
+               painter.end();
+#else
+               std::cerr << myname << ": Conversion of images to format '" << oformat << "' is not supported" << std::endl;
+               return 4;
+#endif
+       } else if (!img.save(QString::fromLocal8Bit(outfile), oformat)) {
+               std::cerr << myname << ": Cannot save converted image to '" << outfile << "'" << std::endl;
+               return 5;
+       }
+       return 0;
+}
+