Skip to content

Commit 0a25510

Browse files
committed
Initial commit.
0 parents  commit 0a25510

File tree

4 files changed

+650
-0
lines changed

4 files changed

+650
-0
lines changed

.gitignore

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
**/.ipynb_checkpoints/
2+
*.egg-info/
3+
.cache/
4+
build/
5+
dist/
6+
doc/_build/
7+
doc/_static/
8+
doc/_templates/
9+
htmlcov/
10+
oprofile_data/
11+
.*.swp
12+
*.o
13+
*.pyc
14+
*.so
15+
.coverage
16+
.gdb_history

mpl_cairo/__init__.py

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
__all__ = ["GraphicsContextRendererCairo", "install", "uninstall"]
2+
3+
from matplotlib.backend_bases import GraphicsContextBase, RendererBase
4+
from matplotlib.backends import backend_cairo
5+
from matplotlib.font_manager import ttfFontProperty
6+
from matplotlib.mathtext import MathtextBackendCairo, MathTextParser
7+
from . import _mpl_cairo
8+
9+
10+
class MathtextBackendCairo2(MathtextBackendCairo):
11+
def render_glyph(self, ox, oy, info):
12+
self.glyphs.append(
13+
# Convert to ttfFontProperty here.
14+
(ttfFontProperty(info.font),
15+
info.fontsize,
16+
chr(info.num),
17+
ox,
18+
oy - info.offset - self.height))
19+
20+
21+
MathTextParser._backend_mapping["cairo2"] = MathtextBackendCairo2
22+
23+
24+
class GraphicsContextRendererCairo(
25+
_mpl_cairo.GraphicsContextRendererCairo,
26+
GraphicsContextBase,
27+
RendererBase):
28+
def __init__(self, *args, **kwargs):
29+
super().__init__(*args, **kwargs)
30+
self.mathtext_parser = MathTextParser("cairo2")
31+
32+
33+
# Keep the two classes separate, so that we can figure out who inherits what.
34+
35+
36+
class GraphicsContextCairo(GraphicsContextRendererCairo):
37+
pass
38+
39+
40+
class RendererCairo(GraphicsContextRendererCairo):
41+
pass
42+
43+
44+
_class_pairs = [
45+
(backend_cairo.GraphicsContextCairo, GraphicsContextRendererCairo),
46+
(backend_cairo.RendererCairo, RendererCairo)]
47+
48+
49+
def _swap(class_pairs):
50+
for old, new in class_pairs:
51+
for cls in old.__subclasses__():
52+
idx = cls.__bases__.index(old)
53+
cls.__bases__ = cls.__bases__[:idx] + (new,) + cls.__bases__[idx + 1:]
54+
setattr(backend_cairo, new.__name__, new)
55+
56+
57+
def install():
58+
"""Switch to new cairo backend implementation.
59+
"""
60+
_swap(_class_pairs)
61+
62+
63+
def uninstall():
64+
"""Switch back to upstream cairo backend implementation.
65+
"""
66+
_swap([(new, old) for old, new in _class_pairs])

setup.py

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
from setuptools import setup, Extension
2+
from setuptools.command.build_ext import build_ext
3+
import sys
4+
import setuptools
5+
6+
7+
__version__ = "0.0"
8+
9+
10+
class get_pybind_include(object):
11+
"""Helper class to determine the pybind11 include path.
12+
13+
The purpose of this class is to postpone importing pybind11 until it is
14+
actually installed, so that the ``get_include()`` method can be invoked.
15+
"""
16+
17+
def __init__(self, user=False):
18+
self.user = user
19+
20+
def __str__(self):
21+
import pybind11
22+
return pybind11.get_include(self.user)
23+
24+
25+
ext_modules = [
26+
Extension(
27+
"mpl_cairo._mpl_cairo",
28+
["src/_mpl_cairo.cpp"],
29+
language="c++",
30+
include_dirs=[
31+
get_pybind_include(), get_pybind_include(user=True)
32+
],
33+
libraries=["cairo"],
34+
),
35+
]
36+
37+
38+
class BuildExt(build_ext):
39+
"""A custom build extension for adding compiler-specific options."""
40+
c_opts = {
41+
"msvc": ["/EHsc"],
42+
"unix": ["-std=c++17", "-fvisibility=hidden"],
43+
}
44+
45+
if sys.platform == "darwin":
46+
c_opts["unix"] += ["-stdlib=libc++", "-mmacosx-version-min=10.7"]
47+
48+
def build_extensions(self):
49+
ct = self.compiler.compiler_type
50+
opts = self.c_opts.get(ct, [])
51+
for ext in self.extensions:
52+
ext.extra_compile_args = opts
53+
build_ext.build_extensions(self)
54+
55+
setup(
56+
name="mpl_cairo",
57+
version=__version__,
58+
author="Antony Lee",
59+
description="A cairo backend for matplotlib.",
60+
long_description="",
61+
ext_modules=ext_modules,
62+
install_requires=["pybind11"],
63+
cmdclass={"build_ext": BuildExt},
64+
zip_safe=False,
65+
)

0 commit comments

Comments
 (0)