Commit 8ccd0569 authored by Akira Yokosawa's avatar Akira Yokosawa Committed by Jonathan Corbet

docs: sphinx/kfigure.py: Use inkscape(1) for SVG -> PDF conversion

Using convert(1) of ImageMagick for SVG -> PDF conversion results in
PDFs containing raster (bitmap) images which sometimes look blurry.

Ideally speaking, SVG to PDF conversion should retain vector graphics
in SVG.

rsvg-convert(1) can do such conversions with regard to SVG files
generated by dot(1).

Unfortunately, rsvg-convert(1) does not cover some of SVG features
specific to Inkscape.
inkscape(1) of Inkscape naturally covers such SVG features.

So add a route in svg2pdf() so that inkscape(1) is used when it is
available.

Note:
    After this change, if you have Inkscape installed, ImageMagick nor
    librsvg are not required.
Signed-off-by: default avatarAkira Yokosawa <akiyks@gmail.com>
Cc: Jonathan Corbet <corbet@lwn.net>
Cc: Mauro Carvalho Chehab <mchehab@kernel.org>
Link: https://lore.kernel.org/r/3eea2a8d-c52d-ee07-cf7b-83784c6f6e4b@gmail.comSigned-off-by: default avatarJonathan Corbet <corbet@lwn.net>
parent ecf5fb58
...@@ -37,6 +37,7 @@ u""" ...@@ -37,6 +37,7 @@ u"""
* SVG to PDF: To generate PDF, you need at least one of this tools: * SVG to PDF: To generate PDF, you need at least one of this tools:
- ``convert(1)``: ImageMagick (https://www.imagemagick.org) - ``convert(1)``: ImageMagick (https://www.imagemagick.org)
- ``inkscape(1)``: Inkscape (https://inkscape.org/)
List of customizations: List of customizations:
...@@ -121,6 +122,11 @@ convert_cmd = None ...@@ -121,6 +122,11 @@ convert_cmd = None
# librsvg's rsvg-convert(1) support # librsvg's rsvg-convert(1) support
rsvg_convert_cmd = None rsvg_convert_cmd = None
# Inkscape's inkscape(1) support
inkscape_cmd = None
# Inkscape prior to 1.0 uses different command options
inkscape_ver_one = False
def setup(app): def setup(app):
# check toolchain first # check toolchain first
...@@ -169,11 +175,13 @@ def setupTools(app): ...@@ -169,11 +175,13 @@ def setupTools(app):
This function is called once, when the builder is initiated. This function is called once, when the builder is initiated.
""" """
global dot_cmd, dot_Tpdf, convert_cmd, rsvg_convert_cmd # pylint: disable=W0603 global dot_cmd, dot_Tpdf, convert_cmd, rsvg_convert_cmd # pylint: disable=W0603
global inkscape_cmd, inkscape_ver_one # pylint: disable=W0603
kernellog.verbose(app, "kfigure: check installed tools ...") kernellog.verbose(app, "kfigure: check installed tools ...")
dot_cmd = which('dot') dot_cmd = which('dot')
convert_cmd = which('convert') convert_cmd = which('convert')
rsvg_convert_cmd = which('rsvg-convert') rsvg_convert_cmd = which('rsvg-convert')
inkscape_cmd = which('inkscape')
if dot_cmd: if dot_cmd:
kernellog.verbose(app, "use dot(1) from: " + dot_cmd) kernellog.verbose(app, "use dot(1) from: " + dot_cmd)
...@@ -190,12 +198,25 @@ def setupTools(app): ...@@ -190,12 +198,25 @@ def setupTools(app):
else: else:
kernellog.warn(app, "dot(1) not found, for better output quality install " kernellog.warn(app, "dot(1) not found, for better output quality install "
"graphviz from https://www.graphviz.org") "graphviz from https://www.graphviz.org")
if inkscape_cmd:
kernellog.verbose(app, "use inkscape(1) from: " + inkscape_cmd)
inkscape_ver = subprocess.check_output([inkscape_cmd, '--version'])
ver_one_ptn = b'Inkscape 1'
inkscape_ver_one = re.search(ver_one_ptn, inkscape_ver)
convert_cmd = None
rsvg_convert_cmd = None
dot_Tpdf = False
else:
if convert_cmd: if convert_cmd:
kernellog.verbose(app, "use convert(1) from: " + convert_cmd) kernellog.verbose(app, "use convert(1) from: " + convert_cmd)
else: else:
kernellog.warn(app, kernellog.warn(app,
"convert(1) not found, for SVG to PDF conversion install " "Neither inkscape(1) nor convert(1) found.\n"
"For SVG to PDF conversion, "
"install either Inkscape (https://inkscape.org/) (preferred) or\n"
"ImageMagick (https://www.imagemagick.org)") "ImageMagick (https://www.imagemagick.org)")
if rsvg_convert_cmd: if rsvg_convert_cmd:
kernellog.verbose(app, "use rsvg-convert(1) from: " + rsvg_convert_cmd) kernellog.verbose(app, "use rsvg-convert(1) from: " + rsvg_convert_cmd)
kernellog.verbose(app, "use 'dot -Tsvg' and rsvg-convert(1) for DOT -> PDF conversion") kernellog.verbose(app, "use 'dot -Tsvg' and rsvg-convert(1) for DOT -> PDF conversion")
...@@ -203,8 +224,7 @@ def setupTools(app): ...@@ -203,8 +224,7 @@ def setupTools(app):
else: else:
kernellog.verbose(app, kernellog.verbose(app,
"rsvg-convert(1) not found.\n" "rsvg-convert(1) not found.\n"
" SVG -> PDF conversion by convert() can be poor quality.\n" " SVG rendering of convert(1) is done by ImageMagick-native renderer.")
" Install librsvg (https://gitlab.gnome.org/GNOME/librsvg)")
if dot_Tpdf: if dot_Tpdf:
kernellog.verbose(app, "use 'dot -Tpdf' for DOT -> PDF conversion") kernellog.verbose(app, "use 'dot -Tpdf' for DOT -> PDF conversion")
else: else:
...@@ -274,7 +294,7 @@ def convert_image(img_node, translator, src_fname=None): ...@@ -274,7 +294,7 @@ def convert_image(img_node, translator, src_fname=None):
elif in_ext == '.svg': elif in_ext == '.svg':
if translator.builder.format == 'latex': if translator.builder.format == 'latex':
if convert_cmd is None: if not inkscape_cmd and convert_cmd is None:
kernellog.verbose(app, kernellog.verbose(app,
"no SVG to PDF conversion available / include SVG raw.") "no SVG to PDF conversion available / include SVG raw.")
img_node.replace_self(file2literal(src_fname)) img_node.replace_self(file2literal(src_fname))
...@@ -342,16 +362,24 @@ def dot2format(app, dot_fname, out_fname): ...@@ -342,16 +362,24 @@ def dot2format(app, dot_fname, out_fname):
return bool(exit_code == 0) return bool(exit_code == 0)
def svg2pdf(app, svg_fname, pdf_fname): def svg2pdf(app, svg_fname, pdf_fname):
"""Converts SVG to PDF with ``convert(1)`` command. """Converts SVG to PDF with ``inkscape(1)`` or ``convert(1)`` command.
Uses ``convert(1)`` from ImageMagick (https://www.imagemagick.org) for Uses ``inkscape(1)`` from Inkscape (https://inkscape.org/) or ``convert(1)``
conversion. Returns ``True`` on success and ``False`` if an error occurred. from ImageMagick (https://www.imagemagick.org) for conversion.
Returns ``True`` on success and ``False`` if an error occurred.
* ``svg_fname`` pathname of the input SVG file with extension (``.svg``) * ``svg_fname`` pathname of the input SVG file with extension (``.svg``)
* ``pdf_name`` pathname of the output PDF file with extension (``.pdf``) * ``pdf_name`` pathname of the output PDF file with extension (``.pdf``)
""" """
cmd = [convert_cmd, svg_fname, pdf_fname] cmd = [convert_cmd, svg_fname, pdf_fname]
if inkscape_cmd:
if inkscape_ver_one:
cmd = [inkscape_cmd, '-o', pdf_fname, svg_fname]
else:
cmd = [inkscape_cmd, '-z', '--export-pdf=%s' % pdf_fname, svg_fname]
# use stdout and stderr from parent # use stdout and stderr from parent
exit_code = subprocess.call(cmd) exit_code = subprocess.call(cmd)
if exit_code != 0: if exit_code != 0:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment