]> git.lyx.org Git - lyx.git/blob - lib/scripts/convertDefault.py
Revert "DocBook: add new layout parameter DocBookWrapperMergeWithPrevious."
[lyx.git] / lib / scripts / convertDefault.py
1 # -*- coding: utf-8 -*-
2
3 # file convertDefault.py
4 # This file is part of LyX, the document processor.
5 # Licence details can be found in the file COPYING.
6
7 # \author Herbert Voß
8 # \author Bo Peng
9
10 # Full author contact details are available in file CREDITS.
11
12 # The default converter if no other has been defined by the user from the
13 # Conversion->Converter tab of the Preferences dialog.
14
15 # The user can also redefine this default converter, placing their
16 # replacement in ~/.lyx/scripts
17
18 # converts an image $2 (format $1) to $4 (format $3)
19 from __future__ import print_function
20 import os, re, sys
21
22 # We may need some extra options only supported by recent convert versions
23 re_version = re.compile(r'^Version:.*ImageMagick\s*(\d*)\.(\d*)\.(\d*).*$')
24 # imagemagick 7
25 command = 'magick'
26 fout = os.popen('magick -version 2>&1')
27 output = fout.readline()
28 if fout.close() != None:
29     # older versions
30     # caution: windows has a convert.exe for converting file systems
31     command = 'convert'
32     fout = os.popen('convert -version 2>&1')
33     output = fout.readline()
34     fout.close()
35
36 version = re_version.match(output)
37
38 # Imagemagick by default
39 im = False
40 gm = False
41
42 if version != None:
43     major = int(version.group(1))
44     minor = int(version.group(2))
45     patch = int(version.group(3))
46     version = (major, minor, patch)
47     im = True
48 else:
49     # Try GraphicsMagick
50     re_version = re.compile(r'^GraphicsMagick.*http:..www.GraphicsMagick.org.*$')
51     version = re_version.match(output)
52     if version != None:
53         gm = True
54         # we need version to be a valid integer 3-tuple
55         version = (1,0,0)
56
57 # IM >= 5.5.8 separates options for source and target files
58 # See http://www.imagemagick.org/Usage/basics/#why
59 if im or gm:
60     sopts = ""
61     topts = ""
62 elif sys.platform == 'darwin':
63     command = 'lyxconvert'
64
65 # If supported, add the -define option for pdf source formats
66 if sys.argv[1] == 'pdf' and (version >= (6,2,6) or gm):
67     sopts = '-define pdf:use-cropbox=true ' + sopts
68
69 # If supported, add the -flatten option for ppm target formats (see bug 4749)
70 if sys.argv[3] == 'ppm' and (im and version >= (6,3,5) or gm):
71     topts = '-flatten'
72
73 # print (command, sys.argv[2], sys.argv[4], file= sys.stdout)
74 if (im or gm) and os.system(r'%s %s "%s" %s "%s"' % (command, sopts, sys.argv[2], topts, sys.argv[3] + ':' + sys.argv[4])) != 0:
75     print (sys.argv[0], 'ERROR', file= sys.stderr)
76     print ('Execution of "%s" failed.' % command, file= sys.stderr)
77     sys.exit(1)
78 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:
79     print (sys.argv[0], 'ERROR', file= sys.stderr)
80     print ('Execution of "%s" failed.' % command, file= sys.stderr)
81     sys.exit(1)