Beispiel #1
0
    def draw(self, cr: cairo.Context) -> None:
        polyline = self._polyline
        stroke_width = self._stroke_width
        stroke_color = self._stroke_color

        if polyline is None or len(polyline) == 0:
            self._last_drawn_region = None
            return

        if self._path_cache is not None:
            cr.append_path(self._path_cache)
        else:
            self._show_polyline(cr, polyline)
            self._path_cache = cr.copy_path()
        extents = Rect2(cr.path_extents())

        dx = 1 / cr.get_matrix().xx
        dy = 1 / cr.get_matrix().yy

        cr.save()

        if self._scale_strokes:
            stroke_scale = max(dx, dy)
            cr.identity_matrix()
        else:
            stroke_scale = 1.0

        cr.set_line_width(stroke_width)
        cr.set_source_rgba(*stroke_color)
        cr.stroke()

        cr.restore()

        extents = expand_rect(extents, max(stroke_width * stroke_scale, dx,
                                           dy))
        self._last_drawn_region = cairo.Region(
            cairo.RectangleInt(
                int(math.floor(extents.x)),
                int(math.floor(extents.y)),
                int(math.ceil(extents.w)),
                int(math.ceil(extents.h)),
            ))
Beispiel #2
0
    def draw(self, cr: cairo.Context) -> None:
        polyline = self._polyline
        if polyline is None:
            return

        stroke_width = self.props.stroke_width
        stroke_color = self.props.stroke_color

        with cairo_saved(cr):
            cr.translate(*self._parent._widget_coord_from_canvas((0, 0)))
            cr.scale(*self._parent._widget_dist_from_canvas((1, 1)))

            if self._cache is not None:
                cr.append_path(self._cache)
            else:
                self._draw_paths(cr, polyline)
                self._cache = cr.copy_path()

        cr.set_source_rgb(*stroke_color)
        cr.set_line_width(stroke_width)
        cr.stroke()