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