Skip to content

Commit eaac409

Browse files
committed
Switch to Cairo-based SVG 2 PDF converter
We previously used a (terribly nice) Makefile to invoke Inkscape to convert our SVG images to PDF. This was not always reliable. Copying the Cairo-based SVG2PDF Sphinx extension from OpTiMSoC is much nicer.
1 parent 4b4caf7 commit eaac409

File tree

4 files changed

+40
-23
lines changed

4 files changed

+40
-23
lines changed

Makefile

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,6 @@ SPHINXPROJ = OpenSoCDebug
1313
SOURCEDIR = src
1414
BUILDDIR = build
1515

16-
SVG2PDF = inkscape
17-
SVG2PDF_FLAGS =
18-
19-
# Build a list of SVG files to convert to PDFs
20-
IMAGES_SVG_REL := $(shell find $(SOURCEDIR) -iname '*.svg' -printf '%P\n')
21-
IMAGES_PDF := $(addprefix $(BUILDDIR)/,$(IMAGES_SVG_REL:.svg=.pdf))
22-
23-
2416
# Put it first so that "make" without argument is like "make help".
2517
help: .venv
2618
@$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
@@ -31,21 +23,6 @@ help: .venv
3123
.venv/bin/pip -q install --upgrade pip
3224
.venv/bin/pip -q install -r requirements.txt
3325

34-
.SECONDEXPANSION:
35-
$(IMAGES_PDF): %.pdf : $$(subst $$(BUILDDIR),$$(SOURCEDIR),%).svg
36-
mkdir -p $(@D)
37-
$(SVG2PDF) -f $< -A $@
38-
39-
# Convert images from SVG to PDF for LaTeX PDF output
40-
images-pdf: $(IMAGES_PDF)
41-
42-
# Convert images to PDF before running the LaTeX PDF build
43-
latexpdf: .venv Makefile images-pdf
44-
@$(SPHINXBUILD) -M latexpdf "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
45-
46-
clean-images:
47-
-rm $(IMAGES_PDF)
48-
4926
clean: .venv Makefile clean-images
5027
@$(SPHINXBUILD) -M clean "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
5128

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
Sphinx>=1.6
22
breathe
33
sphinx-rtd-theme
4+
cairosvg

sphinx/cairosvgconverter.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
"""
4+
from sphinx.errors import ExtensionError
5+
from sphinx.locale import _
6+
from sphinx.transforms.post_transforms.images import ImageConverter
7+
from sphinx.util import logging
8+
from sphinx.util.osutil import ENOENT, EPIPE, EINVAL
9+
from cairosvg import svg2pdf
10+
11+
logger = logging.getLogger(__name__)
12+
13+
class CairoSvgConverter(ImageConverter):
14+
conversion_rules = [
15+
('image/svg+xml', 'application/pdf'),
16+
]
17+
18+
def is_available(self):
19+
# type: () -> bool
20+
return True
21+
22+
def convert(self, _from, _to):
23+
# type: (unicode, unicode) -> bool
24+
"""Converts the image to expected one."""
25+
svg2pdf(url=_from, write_to=_to)
26+
27+
return True
28+
29+
30+
def setup(app):
31+
# type: (Sphinx) -> Dict[unicode, Any]
32+
app.add_post_transform(CairoSvgConverter)
33+
34+
return {
35+
'version': 'builtin',
36+
'parallel_read_safe': True,
37+
'parallel_write_safe': True,
38+
}

src/conf.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
'sphinx.ext.todo',
3535
'sphinx.ext.autodoc',
3636
'sphinx.ext.mathjax',
37+
'cairosvgconverter',
3738
]
3839

3940
# Add any paths that contain templates here, relative to this directory.

0 commit comments

Comments
 (0)