~ubuntu-branches/ubuntu/trusty/python-enable/trusty

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
""" An Enable component to render SVG documents.
"""

import sys
import time

from enthought.enable.api import Component
from enthought.traits.api import Any, Array, Bool, Float
from enthought.kiva.fonttools import Font


if sys.platform == 'win32':
    now = time.clock
else:
    now = time.time


class SVGComponent(Component):
    """ An Enable component to render SVG documents.
    """

    # The SVGDocument.
    document = Any()

    # The number of seconds it took to do the last draw.
    last_render = Float()

    # The profile manager.
    profile_this = Any()
    should_profile = Bool(False)


    def _draw_mainlayer(self, gc, view_bounds=None, mode='default'):
        if self.should_profile and self.profile_this is not None:
            # Only profile the first draw.
            self.should_profile = False
            self.profile_this.start('Drawing')
        start = now()
        gc.clear()

        width, height = self.bounds
        gc.save_state()
        if self.document is None:
            # fixme: The Mac backend doesn't accept style/width as non-integers
            #        in set_font, but does for select_font...
            if sys.platform == 'darwin':
                gc.select_font("Helvetica", 36)
            else:
                gc.set_font(Font("Helvetica", 36))
            gc.show_text_at_point("Could not parse document.", 20, height-56)
            gc.restore_state()
            if self.profile_this is not None:
                self.profile_this.stop()
            return

        try:
            # SVG origin is upper right with y positive is down.
            # Set up the transforms to fix this up.
            # FIXME: if the rendering stage fails, all subsequent renders are vertically flipped
            gc.translate_ctm(0, height)
            # TODO: bother with zoom?
            # TODO: inspect the view bounds and scale to the shape of the
            # component?
            scale = 1.0
            gc.scale_ctm(scale, -scale)
            self.document.render(gc)
            self.last_render = now() - start

        finally:
            gc.restore_state()
            if self.profile_this is not None:
                self.profile_this.stop()

    def _document_changed(self):
        self.should_profile = True
        self.invalidate_and_redraw()


class ImageComponent(Component):
    """ Simple component that just renders an RGB(A) array in the upper left
    hand corner of the component.
    """

    # The RGB(A) data.
    image = Array()


    def _draw_mainlayer(self, gc, view_bounds=None, mode='default'):
        gc.clear()
        if len(self.image.shape) != 3:
            # No image.
            return
        gc.save_state()
        try:
            width, height = self.bounds
            img_height, img_width = self.image.shape[:2]
            gc.draw_image(self.image, (0.0,height-img_height,img_width,img_height))
        finally:
            gc.restore_state()

    def _image_changed(self):
        self.invalidate_and_redraw()