Example #1
0
    def bezier(self, cr: cairo.Context, x0, y0, x1, y1, x2, y2, x3, y3):
        mesh_f = 35
        cr.set_line_width(0.5e-2)
        p = [[x0, y0], [x1, y1], [x2, y2], [x3, y3]]
        cr.move_to(*p[0])
        cr.curve_to(*p[1], *p[2], *p[3])
        cr.stroke()

        # cr.move_to(*p[0])
        #
        # for j in p[1:]:
        #     cr.line_to(*j)
        # cr.stroke()

        l = []
        for i in range(len(p) - 1):
            lx = np.linspace(p[i][0], p[i + 1][0], mesh_f)
            ly = np.linspace(p[i][1], p[i + 1][1], mesh_f)
            l.append([lx, ly])
        cr.set_line_width(0.5e-3)
        for l1, l2 in zip(l[:-1], l[1:]):
            for ix, iy, jx, jy in zip(l1[0], l1[1], l2[0], l2[1]):
                cr.move_to(ix, iy)
                cr.line_to(jx, jy)
        cr.stroke()
Example #2
0
 def draw_precip_curve(
     self,
     context: cairo.Context,
     points: List[Tuple[int, int]],
     bottom: int,
     color,
     curviness: float = 7,
 ):
     # Draw the top curves
     for i, point in enumerate(points):
         if i == 0:
             context.move_to(*point)
         else:
             last_point = points[i - 1]
             context.curve_to(
                 last_point[0] + curviness,
                 last_point[1],
                 point[0] - curviness,
                 point[1],
                 point[0],
                 point[1],
             )
     # Draw the rest and fill
     context.line_to(points[-1][0], bottom)
     context.line_to(points[0][0], bottom)
     context.close_path()
     context.set_source_rgb(*color)
     context.fill()
    def do_render(self, model: Dazzle.GraphModel, x_begin: int, x_end: int,
                  y_begin: float, y_end: float, cairo_context: cairo.Context,
                  area: cairo.RectangleInt) -> None:
        model_iter = Dazzle.GraphModelIter()
        cairo_context.save()

        if model.get_iter_first(model_iter):
            chunk = area.width / (model.props.max_samples - 1) / 2.0
            last_x = self._calc_x(model_iter, x_begin, x_end, area.width)
            last_y = float(area.height)

            cairo_context.move_to(last_x, area.height)

            while Dazzle.GraphModel.iter_next(model_iter):
                x = self._calc_x(model_iter, x_begin, x_end, area.width)
                y = self._calc_y(model_iter, y_begin, y_end, area.height,
                                 self._column)

                cairo_context.curve_to(last_x + chunk, last_y, last_x + chunk,
                                       y, x, y)

                last_x = x
                last_y = y

        cairo_context.set_line_width(self._line_width)
        cairo_context.set_source_rgba(self._stacked_color_rgba.red,
                                      self._stacked_color_rgba.green,
                                      self._stacked_color_rgba.blue,
                                      self._stacked_color_rgba.alpha)
        cairo_context.rel_line_to(0, area.height)
        cairo_context.stroke_preserve()
        cairo_context.close_path()
        cairo_context.fill()

        if model.get_iter_first(model_iter):
            chunk = area.width / (model.props.max_samples - 1) / 2.0
            last_x = self._calc_x(model_iter, x_begin, x_end, area.width)
            last_y = float(area.height)

            cairo_context.move_to(last_x, last_y)

            while Dazzle.GraphModel.iter_next(model_iter):
                x = self._calc_x(model_iter, x_begin, x_end, area.width)
                y = self._calc_y(model_iter, y_begin, y_end, area.height,
                                 self._column)

                cairo_context.curve_to(last_x + chunk, last_y, last_x + chunk,
                                       y, x, y)

                last_x = x
                last_y = y

        cairo_context.set_source_rgba(self._stroke_color_rgba.red,
                                      self._stroke_color_rgba.green,
                                      self._stroke_color_rgba.blue,
                                      self._stacked_color_rgba.alpha)
        cairo_context.stroke()
        cairo_context.restore()
Example #4
0
 def draw_rails_path(self, cr: cairo.Context, anchor_name: str):
     """Creates a path for the rails from anchor 'in' to the given anchor name"""
     cr.move_to(0, 0)
     if anchor_name == "out":
         # For some reason line_to doesn't play nice with cairo.OPERATOR_CLEAR
         # cr.line_to(32, 0)
         cr.curve_to(1, 0, 31, 0, 32, 0)
     elif anchor_name == "branch":
         cr.curve_to(*self.control_points[0], *self.control_points[1],
                     *self.branch_point)
     else:
         raise AssertionError
            [tags.pop() for x in range(len(segment) - 1)]
        VERTS.extend(verts)
        CODES.extend(codes)
        start = end+1

    ctx.new_path()
    ctx.set_source_rgba(1,1,0, 0.5)
    i = 0
    while (i < len(CODES)):
        if (CODES[i] == MOVETO):
            ctx.move_to(VERTS[i][0],VERTS[i][1])
            i += 1
        elif (CODES[i] == LINETO):
            ctx.line_to(VERTS[i][0],VERTS[i][1])
            i += 1
        elif (CODES[i] == CURVE3):
            ctx.curve_to(VERTS[i][0],VERTS[i][1],
                         VERTS[i+1][0],VERTS[i+1][1], # undocumented
                         VERTS[i+1][0],VERTS[i+1][1])
            i += 2
        elif (CODES[i] == CURVE4):
            ctx.curve_to(VERTS[i][0],VERTS[i][1],
                         VERTS[i+1][0],VERTS[i+1][1],
                         VERTS[i+2][0],VERTS[i+2][1])
            i += 3
    ctx.fill()

    surface.flush()
    surface.write_to_png("glyph-vector-cairo.png")
    Image.open("glyph-vector-cairo.png").show()