2006-08-08 10:42:59 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2003-08-22 10:50:02 +00:00
|
|
|
|
2006-06-26 14:17:12 +00:00
|
|
|
# file convertDefault.py
|
2003-08-22 10:50:02 +00:00
|
|
|
# This file is part of LyX, the document processor.
|
|
|
|
# Licence details can be found in the file COPYING.
|
|
|
|
|
2006-08-08 10:42:59 +00:00
|
|
|
# \author Herbert Voß
|
2006-06-26 14:17:12 +00:00
|
|
|
# \author Bo Peng
|
2003-08-22 10:50:02 +00:00
|
|
|
|
|
|
|
# Full author contact details are available in file CREDITS.
|
|
|
|
|
2003-01-13 23:35:18 +00:00
|
|
|
# The default converter if no other has been defined by the user from the
|
|
|
|
# Conversion->Converter tab of the Preferences dialog.
|
2003-08-22 10:50:02 +00:00
|
|
|
|
2003-01-13 23:35:18 +00:00
|
|
|
# The user can also redefine this default converter, placing their
|
|
|
|
# replacement in ~/.lyx/scripts
|
2003-08-22 10:50:02 +00:00
|
|
|
|
2015-03-22 09:22:31 +00:00
|
|
|
# converts an image $2 (format $1) to $4 (format $3)
|
2018-04-28 09:51:35 +00:00
|
|
|
from __future__ import print_function
|
2008-07-21 08:53:06 +00:00
|
|
|
import os, re, sys
|
|
|
|
|
|
|
|
# We may need some extra options only supported by recent convert versions
|
|
|
|
re_version = re.compile(r'^Version:.*ImageMagick\s*(\d*)\.(\d*)\.(\d*).*$')
|
2016-05-10 19:27:57 +00:00
|
|
|
# imagemagick 7
|
|
|
|
command = 'magick'
|
|
|
|
fout = os.popen('magick -version 2>&1')
|
2008-07-21 08:53:06 +00:00
|
|
|
output = fout.readline()
|
2016-05-10 19:27:57 +00:00
|
|
|
if fout.close() != None:
|
|
|
|
# older versions
|
|
|
|
# caution: windows has a convert.exe for converting file systems
|
|
|
|
command = 'convert'
|
|
|
|
fout = os.popen('convert -version 2>&1')
|
|
|
|
output = fout.readline()
|
|
|
|
fout.close()
|
2018-04-28 09:51:35 +00:00
|
|
|
|
2008-07-21 08:53:06 +00:00
|
|
|
version = re_version.match(output)
|
2009-08-09 14:38:49 +00:00
|
|
|
|
|
|
|
# Imagemagick by default
|
2017-09-30 16:13:21 +00:00
|
|
|
im = False
|
2011-02-07 23:48:29 +00:00
|
|
|
gm = False
|
2009-08-09 14:38:49 +00:00
|
|
|
|
|
|
|
if version != None:
|
|
|
|
major = int(version.group(1))
|
|
|
|
minor = int(version.group(2))
|
|
|
|
patch = int(version.group(3))
|
2019-06-03 18:07:20 +00:00
|
|
|
version = (major, minor, patch)
|
2017-09-30 16:13:21 +00:00
|
|
|
im = True
|
2009-08-09 14:38:49 +00:00
|
|
|
else:
|
|
|
|
# Try GraphicsMagick
|
|
|
|
re_version = re.compile(r'^GraphicsMagick.*http:..www.GraphicsMagick.org.*$')
|
|
|
|
version = re_version.match(output)
|
|
|
|
if version != None:
|
2011-02-07 23:48:29 +00:00
|
|
|
gm = True
|
2019-06-03 18:07:20 +00:00
|
|
|
# we need version to be a valid integer 3-tuple
|
|
|
|
version = (1,0,0)
|
2007-03-31 18:21:54 +00:00
|
|
|
|
2018-07-13 08:20:26 +00:00
|
|
|
# IM >= 5.5.8 separates options for source and target files
|
|
|
|
# See http://www.imagemagick.org/Usage/basics/#why
|
2017-09-30 16:13:21 +00:00
|
|
|
if im or gm:
|
2019-03-21 01:35:51 +00:00
|
|
|
sopts = ""
|
2018-07-13 08:20:26 +00:00
|
|
|
topts = ""
|
2017-09-30 16:13:21 +00:00
|
|
|
elif sys.platform == 'darwin':
|
|
|
|
command = 'lyxconvert'
|
2008-07-21 08:53:06 +00:00
|
|
|
|
2015-03-22 09:22:31 +00:00
|
|
|
# If supported, add the -define option for pdf source formats
|
2019-06-03 18:07:20 +00:00
|
|
|
if sys.argv[1] == 'pdf' and (version >= (6,2,6) or gm):
|
2018-07-13 08:20:26 +00:00
|
|
|
sopts = '-define pdf:use-cropbox=true ' + sopts
|
2008-07-21 08:53:06 +00:00
|
|
|
|
|
|
|
# If supported, add the -flatten option for ppm target formats (see bug 4749)
|
2019-06-03 18:07:20 +00:00
|
|
|
if sys.argv[3] == 'ppm' and (im and version >= (6,3,5) or gm):
|
2018-07-13 08:20:26 +00:00
|
|
|
topts = '-flatten'
|
2008-07-07 15:09:44 +00:00
|
|
|
|
2018-04-28 09:51:35 +00:00
|
|
|
# print (command, sys.argv[2], sys.argv[4], file= sys.stdout)
|
2018-07-13 08:20:26 +00:00
|
|
|
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:
|
2018-04-28 09:51:35 +00:00
|
|
|
print (sys.argv[0], 'ERROR', file= sys.stderr)
|
|
|
|
print ('Execution of "%s" failed.' % command, file= sys.stderr)
|
2017-09-30 16:13:21 +00:00
|
|
|
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:
|
2018-04-28 09:51:35 +00:00
|
|
|
print (sys.argv[0], 'ERROR', file= sys.stderr)
|
|
|
|
print ('Execution of "%s" failed.' % command, file= sys.stderr)
|
2006-06-27 21:08:54 +00:00
|
|
|
sys.exit(1)
|