def draw(self, ctx: Context, square: bool) -> None:
        point_diameter_current = self.point_diameter_initial

        ctx.set_source_rgb(self.color[0], self.color[1], self.color[2])

        points_per_row = math.floor(self.target_width / self.point_margin)
        i = 0

        while ((self.point_diameter_check_is_greater
                and point_diameter_current > self.point_diameter_final)
               or (not self.point_diameter_check_is_greater
                   and point_diameter_current < self.point_diameter_final)):

            col = i % points_per_row
            row = math.floor(i / points_per_row)
            x = self.position[
                0] + col * self.point_margin + self.point_margin / 2
            y = self.position[
                1] + row * self.point_margin + self.point_margin / 2

            radius = point_diameter_current / 2

            if square:
                ctx.move_to(x - radius, y - radius)
                ctx.line_to(x + radius, y - radius)
                ctx.line_to(x + radius, y + radius)
                ctx.line_to(x - radius, y + radius)
                ctx.fill()
            else:
                ctx.arc(x, y, radius, 0, 2 * math.pi)
                ctx.fill()

            i += 1
            point_diameter_current += self.point_diameter_delta
示例#2
0
class Plot(object):
    red = (1., 0., 0.)
    green = (0., 1., 0.)
    blue = (0., 0., 1.)
    yellow = (1., 1., 0.)
    cyan = (0., 1., 1.)
    magenta = (1., 0., 1.)

    def __init__(self, width, height):
        from cairocffi import SVGSurface, Context
        self.plot_id = next(_debug_generator)
        self._surface = SVGSurface("debug_plot-%05d.svg" % self.plot_id, width,
                                   height)
        self._ctx = Context(self._surface)
        self._ctx.set_source_rgb(0., 0., 0.)
        self._ctx.paint()

    def line(self, line, color):
        ctx = self._ctx
        ctx.move_to(line.p1.x, line.p1.y)
        ctx.line_to(line.p2.x, line.p2.y)
        ctx.set_source_rgb(*color)
        ctx.stroke()

    def circle(self, center, radius, stroke_color, fill_color=None):
        ctx = self._ctx
        ctx.new_sub_path()
        ctx.arc(center.x, center.y, radius, 0, math.pi * 2)
        ctx.set_source_rgb(*stroke_color)
        if fill_color:
            ctx.stroke_preserve()
            ctx.set_source_rgb(*fill_color)
            ctx.fill()
        else:
            ctx.stroke()
    def draw(self, ctx: Context, polygon: bool) -> None:
        line_width_current = self.line_width_initial

        ctx.set_source_rgb(self.color[0], self.color[1], self.color[2])

        x_start = self.position[0]
        x_end = x_start + self.target_width
        y = self.position[1]

        while ((self.line_width_check_is_greater
                and line_width_current > self.line_width_final)
               or (not self.line_width_check_is_greater
                   and line_width_current < self.line_width_final)):
            ctx.set_line_width(line_width_current)
            y += line_width_current / 2

            if polygon:
                ctx.move_to(x_start, y - line_width_current / 2)
                ctx.line_to(x_end, y - line_width_current / 2)
                ctx.line_to(x_end, y + line_width_current / 2)
                ctx.line_to(x_start, y + line_width_current / 2)
                ctx.fill()
            else:
                ctx.move_to(x_start, y)
                ctx.line_to(x_end, y)
                ctx.stroke()

            # Add the white space
            y += line_width_current

            # prepare next line width
            y += line_width_current / 2
            line_width_current += self.line_width_delta
 def draw(self, ctx: Context, polygons: List[Union[Polygon, MultiPolygon]]):
     if self.high_quality:
         ctx.set_source_rgb(0, 0, 0)
         ctx.set_line_width(self.line_width)
         self._draw_iterator(ctx, polygons, self._draw_water)
     else:
         ctx.set_source_rgba(0, 0, 0, 0.3)
         self._draw_iterator(ctx, polygons, self._draw_water_area)
示例#5
0
 def draw(self, context: cairo.Context, x: float, y: float, fxy: FixXY):
     if debug:
         context.set_source_rgb(1 if self.no_page_break else 0, 0, 0)
         context.rectangle(*fxy(x - 5, y), *fxy(2, 16))
         context.fill()
         context.set_source_rgb(0.5, 0.5, 0.5)
         context.rectangle(*fxy(self.indent + x, y),
                           *fxy(self.width, self.height))
         context.stroke()
示例#6
0
def label_interface(ctx: cairo.Context,
                    face: trains.Interface,
                    radius_factor=1):
    """Draw text saying ROC, and a line from the edge of the interface to the text.

    Args:
        radius_factor (scalar): Factor by which interface radius is multiplied to get text x position.
    """
    x = radius_factor * face.radius
    y = functions.calc_sphere_sag(face.roc, face.radius)
    string = 'ROC %.3f mm' % (face.roc * 1e3)
    ctx.set_source_rgb(0, 0, 0)
    ctx.save()
    ctx.translate(x, y)
    ctx.show_text(string)
    ctx.new_path()
    ctx.restore()
    if radius_factor > 1:
        draw_polyline(ctx, ((face.radius, y), (x, y)))
        ctx.stroke()
    def draw_labels(self, ctx: Context) -> None:
        point_diameter_current = self.point_diameter_initial
        text_height_room = 0

        ctx.set_source_rgb(self.color[0], self.color[1], self.color[2])

        points_per_row = math.floor(self.target_width / self.point_margin)
        i = 0

        while ((self.point_diameter_check_is_greater
                and point_diameter_current > self.point_diameter_final)
               or (not self.point_diameter_check_is_greater
                   and point_diameter_current < self.point_diameter_final)):

            col = i % points_per_row
            row = math.floor(i / points_per_row)
            x = self.position[
                0] + col * self.point_margin + self.point_margin / 2
            y = self.position[
                1] + row * self.point_margin + self.point_margin / 2

            first_point_diameter_in_row = point_diameter_current
            last_point_diameter_in_row = point_diameter_current + self.point_diameter_delta * (
                points_per_row - 1)
            print(first_point_diameter_in_row)
            print(last_point_diameter_in_row)

            if text_height_room > 2:
                ctx.move_to(x + 0, y)
                ctx.line_to(x + 2, y)
                ctx.stroke()
                self.draw_text(
                    ctx,
                    "{0:.3f} - {1:.3f}".format(first_point_diameter_in_row,
                                               last_point_diameter_in_row) +
                    " mm", x + 3, y, 2)
                text_height_room = 0

            i += points_per_row
            point_diameter_current += self.point_diameter_delta * points_per_row
            text_height_room += self.point_margin
    def draw_labels(self, ctx: Context):
        ctx.set_line_width(0.1)

        line_width_current = self.line_width_initial
        text_height_room = 0

        ctx.set_source_rgb(self.color[0], self.color[1], self.color[2])

        x = self.position[0]
        y = self.position[1]

        while ((self.line_width_check_is_greater
                and line_width_current > self.line_width_final)
               or (not self.line_width_check_is_greater
                   and line_width_current < self.line_width_final)):
            y += line_width_current / 2
            text_height_room += line_width_current / 2

            # Draw the line annotating the row
            if text_height_room > 2:
                ctx.move_to(x + 0, y)
                ctx.line_to(x + 2, y)
                ctx.stroke()
                self.draw_text(ctx,
                               "{0:.2f}".format(line_width_current) + " mm",
                               x + 3, y, 2)
                text_height_room = 0

            # Add the white space
            y += line_width_current
            text_height_room += line_width_current

            # prepare next line width
            y += line_width_current / 2
            text_height_room += line_width_current / 2
            line_width_current += self.line_width_delta
示例#9
0
 def draw(self, context: cairo.Context, x: float, y: float, fxy: FixXY):
     super().draw(context, x, y, fxy)
     if debug:
         context.set_source_rgb(1 if self.no_page_break else 0, 0, 0)
         context.rectangle(*fxy(x, y), *fxy(10, 10))
         context.fill()
示例#10
0
    w, h_bg = bg.get_width(), bg.get_height()
    h = int(h_bg * (1 - y_offset))

    base_labels = board_info["variants"]["_base"]

    for variant_name, variant_info in board_info["variants"].items():
        if variant_name.startswith("_"):
            # skip "_base"
            continue

        # create canvas
        w_p = int(w * (1 + 2 * pad_sides))
        canvas = ImageSurface(bg.get_format(), w_p, h)
        ctx = Context(canvas)

        ctx.set_source_rgb(1, 1, 1)
        ctx.rectangle(0, 0, w_p, h)
        ctx.fill()

        ctx.set_source_surface(bg, (w_p - w) // 2, -h_bg * y_offset)
        ctx.rectangle((w_p - w) // 2, 0, w, h_bg)
        ctx.fill()

        # make B&W

        ctx.set_source_rgb(0.5, 0.5, 0.5)
        ctx.set_operator(OPERATOR_HSL_SATURATION)
        ctx.rectangle(0, 0, w_p, h)
        ctx.fill()

        if True:
示例#11
0
    def draw(self, ctx: Context):

        if not self.canvas_set or not self.margin_set or not self.legend_set:
            raise Exception("Canvas, margin or legend not set!")

        margin_exterior_top_left = (0, 0)
        margin_exterior_top_right = (self.canvas_width, 0)
        margin_exterior_bottom_left = (0, self.canvas_height)
        margin_exterior_bottom_right = (self.canvas_width, self.canvas_height)
        legend_exterior_top_left = (self.margin_left, self.margin_top)
        legend_exterior_top_right = (self.canvas_width - self.margin_right,
                                     self.margin_top)
        legend_exterior_bottom_left = (self.margin_left,
                                       self.canvas_height - self.margin_bottom)
        legend_exterior_bottom_right = (self.canvas_width - self.margin_right,
                                        self.canvas_height -
                                        self.margin_bottom)
        legend_interior_top_left = (self.margin_left + self.legend_left,
                                    self.margin_top + self.legend_top)
        legend_interior_top_right = (self.canvas_width - self.margin_right -
                                     self.legend_right,
                                     self.margin_top + self.legend_top)
        legend_interior_bottom_left = (self.margin_left + self.legend_left,
                                       self.canvas_height -
                                       self.margin_bottom - self.legend_bottom)
        legend_interior_bottom_right = (self.canvas_width - self.margin_right -
                                        self.legend_right, self.canvas_height -
                                        self.margin_bottom -
                                        self.legend_bottom)

        margin_and_legend = Polygon([
            margin_exterior_top_left, margin_exterior_top_right,
            margin_exterior_bottom_right, margin_exterior_bottom_left,
            margin_exterior_top_left
        ], [
            list(
                reversed([
                    legend_interior_top_left, legend_interior_top_right,
                    legend_interior_bottom_right, legend_interior_bottom_left,
                    legend_interior_top_left
                ]))
        ])

        legend_exterior = Polygon([
            legend_exterior_top_left, legend_exterior_top_right,
            legend_exterior_bottom_right, legend_exterior_bottom_left,
            legend_exterior_top_left
        ])

        legend_interior = Polygon([
            legend_exterior_top_left, legend_exterior_top_right,
            legend_exterior_bottom_right, legend_exterior_bottom_left,
            legend_exterior_top_left
        ], [
            list(
                reversed([
                    legend_interior_top_left, legend_interior_top_right,
                    legend_interior_bottom_right, legend_interior_bottom_left,
                    legend_interior_top_left
                ]))
        ])

        ctx.set_line_width(0.05)

        ctx.set_source_rgb(1, 1, 1)
        CairoHelper.draw_polygon(ctx, margin_and_legend)
        ctx.fill()

        # ctx.set_line_width(self.exterior_stroke_width)
        ctx.set_source_rgb(0, 0, 0)
        CairoHelper.draw_polygon(ctx, legend_exterior)
        ctx.stroke()
        # ctx.set_line_width(self.interior_stroke_width)
        CairoHelper.draw_polygon(ctx, legend_interior)
        ctx.stroke()