File: backend_ipe.py

package info (click to toggle)
ipe-tools 1%3A7.2.29.2-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 940 kB
  • sloc: python: 2,840; cpp: 2,752; ansic: 1,052; sh: 224; makefile: 95; xml: 39
file content (308 lines) | stat: -rw-r--r-- 10,802 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
"""
This is a matplotlib backend to save in the Ipe file format.
(ipe7.sourceforge.net).

(c) 2014 Soyeon Baek, Otfried Cheong

You can find the most current version at:
http://www.github.com/otfried/ipe-tools/matplotlib

You can use this backend by saving it anywhere on your PYTHONPATH.
Use it as an external backend from matplotlib like this:

  import matplotlib
  matplotlib.use('module://backend_ipe')

"""
from codecs import getwriter
from math import cos, radians, sin
from os.path import exists
from re import compile

from matplotlib import cbook, rcParams
from matplotlib.backend_bases import (
    FigureCanvasBase, FigureManagerBase, RendererBase)
from matplotlib.backends.backend_pgf import LatexManager, _tex_escape
from matplotlib.backends.backend_svg import XMLWriter
from matplotlib.backends.backend_mixed import MixedModeRenderer
from matplotlib.figure import Figure
from matplotlib.path import Path
from matplotlib.rcsetup import validate_bool, validate_string


class XMLWriterIpe(XMLWriter):
    def insertSheet(self, fname):
        self._XMLWriter__flush()
        with open(fname, "r", encoding="utf-8") as f:
            data = f.read()
            if (i := data.find("<ipestyle")) >= 0:
                self._XMLWriter__write(data[i:])


rcParams.validate["ipe.preamble"] = validate_string
rcParams.validate["ipe.stylesheet"] = lambda s: s if s and exists(s) else None
rcParams.validate["ipe.textsize"] = validate_bool
_negative_number = compile(r"^\u2212([0-9]+)(\.[0-9]*)?$")


class RendererIpe(RendererBase):
    def __init__(self, figure, ipewriter, image_dpi):
        width, height = figure.get_size_inches()
        self.dpi = figure.dpi
        self.width = width * self.dpi
        self.height = height * self.dpi
        self.writer = XMLWriterIpe(ipewriter)
        self.image_dpi = image_dpi # actual dpi at which we resterize stuff

        super().__init__()
        rcParams["pgf.texsystem"] = "pdflatex"  # use same latex as Ipe
        self.__write_header()
        self.writer.start("page")

    def __write_header(self):
        self._start_id = self.writer.start(
            "ipe",
            version="70006",
            creator="Matplotlib")
        self.__stylesheet()

    def __stylesheet(self):
        if "ipe.stylesheet" in rcParams and rcParams["ipe.stylesheet"]:
            self.writer.insertSheet(rcParams["ipe.stylesheet"])
        self.writer.start("ipestyle", name="opacity")
        self.__preamble()
        for i in range(10, 100, 10):
            self.writer.element("opacity", name=f"{i}%", value=f"{i / 100.0}")
        self.writer.end()

    def __preamble(self):
        if "ipe.preamble" in rcParams:
            self.writer.start("preamble")
            self.writer.data(rcParams["ipe.preamble"])
            self.writer.end(indent=False)

    def finalize(self):
        self.writer.close(self._start_id)
        self.writer.flush()

    def draw_path(self, gc, path, transform, rgbFace=None):
        cap = ("butt", "round", "projecting").index(gc.get_capstyle())
        join = ("miter", "round", "bevel").index(gc.get_joinstyle())
        offs, dl = gc.get_dashes()
        attrib = {}
        if offs is not None and dl is not None:
            attrib["dash"] = (f"[{dl}] {offs}" if type(dl) is float else
                              f"[{' '.join([str(x) for x in dl])}] {offs}")
        opaq = gc.get_rgb()[3]
        if rgbFace is not None:     # filling
            r, g, b, opaq = rgbFace
            attrib["fill"] = f"{r} {g} {b}"
        self.__gen_opacity(attrib, opaq)
        self._print_ipe_clip(gc)
        _r, _g, _b, _ = gc.get_rgb()
        self.writer.start(
            "path",
            attrib=attrib,
            stroke=f"{_r} {_g} {_b}",
            pen=f"{gc.get_linewidth()}",
            cap=f"{cap}",
            join=f"{join}",
            fillrule="wind"
        )
        self.writer.data(self._make_ipe_path(gc, path, transform))
        self.writer.end()
        self._print_ipe_clip_end()

    def _make_ipe_path(self, gc, path, transform):
        elem = ""
        for points, code in path.iter_segments(transform):
            if code == Path.MOVETO:
                x, y = tuple(points)
                elem += f"{x} {y} m\n"
            elif code == Path.CLOSEPOLY:
                elem += "h\n"
            elif code == Path.LINETO:
                x, y = tuple(points)
                elem += f"{x} {y} l\n"
            elif code == Path.CURVE3:
                cx, cy, px, py = tuple(points)
                elem += f"{cx} {cy} {px} {py} q\n"
            elif code == Path.CURVE4:
                c1x, c1y, c2x, c2y, px, py = tuple(points)
                elem += (f"{c1x} {c1y} {c2x} {c2y} {px} {py} c\n")
        return elem

    def option_scale_image(self):
        return True

    def get_image_magnification(self):
        return self.image_dpi / 72.0

    def draw_image(self, gc, x, y, im, transform=None):
        h, w = im.shape[:2]
        if w == 0 or h == 0:
            return

        isgray = (im[..., :3] == im[..., 0, None]).all()
        istransp = (im[..., 3] < 255).any()

        # attempt to squash dimensions of the image
        # maybe shouldn't be done?
        # (allows to reduce colorbars to 1 row or column of pixels)
        isvertical = (im[:, [0], :] == im).all()
        ishorizontal = (im[[0], :, :] == im).all()

        colorspace = "DeviceGray" if isgray else "DeviceRGB"
        if istransp:
            colorspace += "Alpha"

        f = self.get_image_magnification()
        if transform is None:
            tr1, tr2, tr3, tr4, tr5, tr6 = w / f, 0, 0, h / f, 0, 0
        else:
            tr1, tr2, tr3, tr4, tr5, tr6 = transform.frozen().to_values()

        if isvertical:
            w = 1
            im = im[:, [0], :]
        if ishorizontal:
            h = 1
            im = im[[0], :, :]

        self._print_ipe_clip(gc)
        self.writer.start(
            "image",
            width=f"{w}",
            height=f"{h}",
            ColorSpace=colorspace,
            BitsPerComponent="8",
            matrix=r"%f %f %f %f %f %f"%(tr1, tr2, tr3, tr4, tr5 + x, tr6 + y),
            rect="0 1 1 0",
            length=f"{w * h * (1 if isgray else 3)}",
            alphaLength=f"{w * h if istransp else 0}",
        )
        for row in im[::-1]:
            for r, g, b, a in row:
                if isgray:
                    self.writer.data(f"{r:02x}")
                else:
                    self.writer.data(f"{r:02x}{g:02x}{b:02x}")
        if istransp:
            for row in im[::-1]:
                for r, g, b, a in row:
                    self.writer.data(f"{a:02x}")
        self.writer.end()
        self._print_ipe_clip_end()

    def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
        amap = { # matplotlib -> ipe alignment correspondences, add as needed
                'center_baseline': 'center',
               }
        if _negative_number.match(s):
            s = f"${s.replace('\u2212', '-')}$"
        attrib = {}
        if mtext:
            x, y = mtext.get_transform().transform_point(mtext.get_position())
            ha, va = mtext.get_ha(), mtext.get_va()
            attrib["halign"] = amap[ha] if ha in amap.keys() else ha
            attrib["valign"] = amap[va] if va in amap.keys() else va

        if angle != 0.0:
            ra = radians(angle)
            sa, ca = sin(ra), cos(ra)
            attrib["matrix"] = f"{ca} {sa} -{sa} {ca} {x} {y}"
            x, y = 0, 0

        self.__gen_opacity(attrib, gc.get_rgb()[3])
        _r, _g, _b, _ = gc.get_rgb()
        self.writer.start(
            "text",
            stroke=f"{_r} {_g} {_b}",
            type="label",
            size=f"{prop.get_size_in_points()}",
            pos=f"{x} {y}",
            attrib=attrib
        )
        self.writer.data(f"{_tex_escape(s)}")
        self.writer.end(indent=False)

    def _print_ipe_clip(self, gc):
        bbox = gc.get_clip_rectangle()
        self.use_clip_box = bbox is not None
        if self.use_clip_box:
            p1, p2 = bbox.get_points()
            x1, y1 = p1
            x2, y2 = p2
            self.writer.start(
                "group",
                clip=f"{x1} {y1} m {x2} {y1} l {x2} {y2} l {x1} {y2} l h"
            )
        clippath, clippath_trans = gc.get_clip_path()   # check for clip path
        self.use_clip_group = clippath is not None
        if self.use_clip_group:
            self.writer.start(
                "group",
                clip=f"{self._make_ipe_path(gc, clippath, clippath_trans)}")

    def _print_ipe_clip_end(self):
        if self.use_clip_group:
            self.writer.end()
        if self.use_clip_box:
            self.writer.end()

    def get_canvas_width_height(self):
        return self.width, self.height

    def get_text_width_height_descent(self, s, prop, ismath):
        if ("ipe.textsize" in rcParams and rcParams["ipe.textsize"] is True):
            w, h, d = (LatexManager._get_cached_or_new()
                       .get_width_height_descent(s, prop))
            return w * (f := (1/72) * self.dpi), h * f, d * f
        return 1, 1, 1

    @staticmethod
    def __gen_opacity(attrib, opaq):
        if opaq < 0.05:
            attrib["opacity"] = "10%"
        elif opaq < 0.95:
            o = round(opaq+10**(-len(str(opaq))-1), 1)
            attrib["opacity"] = f"{int(o * 100)}%"


class FigureCanvasIpe(FigureCanvasBase):
    filetypes = FigureCanvasBase.filetypes.copy()
    filetypes["ipe"] = "Ipe 7 file format"

    def print_ipe(self, filename, *args, **kwargs):
        with cbook.open_file_cm(filename, "w", encoding="utf-8") as writer:
            if not cbook.file_requires_unicode(writer):
                writer = getwriter("utf-8")(writer)
            self._print_ipe(filename, writer, **kwargs)

    def _print_ipe(self, filename, ipewriter, bbox_inches_restore=None, **kwargs):
        dpi = self.figure.dpi
        self.figure.set_dpi(72.0)
        w, h = self.figure.get_figwidth(), self.figure.get_figheight()
        renderer = MixedModeRenderer(self.figure, w, h, dpi,
                                     RendererIpe(self.figure, ipewriter, image_dpi=dpi),
                                     bbox_inches_restore=bbox_inches_restore)
        self.figure.draw(renderer)
        renderer.finalize()

    def get_default_filetype(self):
        return "ipe"


class FigureManagerIpe(FigureManagerBase):
    def __init__(self, *args):
        FigureManagerBase.__init__(self, *args)


FigureCanvas = FigureCanvasIpe
FigureManager = FigureManagerIpe


def new_figure_manager(num, *args, **kwargs):
    FigureClass = kwargs.pop("FigureClass", Figure)
    fig = FigureClass(*args, **kwargs)
    return FigureManagerIpe(FigureCanvasIpe(fig), num)