示例#1
0
def export_svg(fn, paths, size, line_with=0.1, scale_factor=None):

  from cairocffi import SVGSurface, Context
  from .ddd import spatial_sort_2d as sort

  if not scale_factor:
    scale_factor = size

  s = SVGSurface(fn, size, size)
  c = Context(s)

  c.set_line_width(0.1)

  paths = sort(paths)

  for path in paths:
    path *= scale_factor

    c.new_path()
    c.move_to(*path[0,:])
    for p in path[1:]:
      c.line_to(*p)
    c.stroke()

  c.save()
    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
示例#3
0
    def draw_polygon(ctx: Context, polygon: Polygon):
        if hasattr(polygon.exterior, 'coords'):
            start = True
            ctx.new_path()
            for x, y in polygon.exterior.coords:
                if start:
                    ctx.move_to(x, y)
                else:
                    ctx.line_to(x, y)

                start = False

        for interior in polygon.interiors:
            ctx.new_sub_path()
            start = True
            for x, y in interior.coords:
                if start:
                    ctx.move_to(x, y)
                else:
                    ctx.line_to(x, y)

                start = False
            ctx.close_path()

        ctx.close_path()
示例#4
0
文件: cairo.py 项目: draustin/otk
def _(d: Line, ctx: cairo.Context, scale: float):
    ctx.save()
    ctx.move_to(d.x0 * scale, d.y0 * scale)
    ctx.line_to(d.x1 * scale, d.y1 * scale)
    ctx.stroke()
    # ctx.fill()
    ctx.restore()
示例#5
0
文件: line.py 项目: Mr-Clear/Clear19
 def paint_foreground(self, ctx: Context):
     ctx.set_line_width(self.line_width)
     if self.orientation == Line.Orientation.HORIZONTAL:
         ctx.move_to(*self.size.position(Anchor.CENTER_LEFT))
         ctx.line_to(*self.size.position(Anchor.CENTER_RIGHT))
     else:
         ctx.move_to(*self.size.position(Anchor.TOP_CENTER))
         ctx.line_to(*self.size.position(Anchor.BOTTOM_CENTER))
     ctx.stroke()
示例#6
0
    def draw_line_string(ctx: Context, line_string: LineString):
        start = True
        for x, y in line_string.coords:
            if start:
                ctx.move_to(x, y)
            else:
                ctx.line_to(x, y)

            start = False
示例#7
0
 def paint_scale_background(self, ctx: Context):
     cpu_count = Global.system_data.cpu_count
     for i in range(cpu_count):
         if i % 2:
             ctx.set_source_rgba(*Color.GRAY33)
         else:
             ctx.set_source_rgba(*Color.GRAY50)
         ctx.move_to(0, self.height - self.height / cpu_count * i)
         ctx.line_to(self.width, self.height - self.height / cpu_count * i)
         ctx.stroke()
示例#8
0
def draw_ray_trace(r0: Sequence[float], elements: Sequence[Element],
                   ctx: cairo.Context, scale: float):
    ctx.save()
    r = r0
    for element in elements:
        ctx.move_to(0, r[0] * scale)
        ctx.translate(element.thickness * scale, 0)
        rp = element.matrix.dot(r)
        ctx.line_to(0, rp[0] * scale)
        ctx.stroke()
        r = rp
    ctx.restore()
示例#9
0
def _coordinate_path(
        context: cairocffi.Context,
        point: Tuple[int, int],
        radius: float = 1
):
    for i in range(0, 8):
        p_x = pangocffi.units_to_double(point[0]) + \
            math.sin(i/4 * math.pi) * radius
        p_y = pangocffi.units_to_double(point[1]) + \
            math.cos(i/4 * math.pi) * radius
        if i == 0:
            context.move_to(p_x, p_y)
        else:
            context.line_to(p_x, p_y)
示例#10
0
 def paint_foreground(self, ctx: Context):
     if self._image:
         sx = self.width / self._image.get_width()
         sy = self.height / self._image.get_height()
         s = min(sx, sy)
         sw = self._image.get_width() * s
         sh = self._image.get_height() * s
         ctx.scale(s, s)
         x = 0
         y = 0
         if self.alignment == Anchor.TOP_LEFT:
             x = 0
             y = 0
         elif self.alignment == Anchor.TOP_CENTER:
             x = (self.width - sw) / 2
             y = 0
         elif self.alignment == Anchor.TOP_RIGHT:
             x = self.width - sw
             y = 0
         elif self.alignment == Anchor.CENTER_LEFT:
             x = 0
             y = (self.height - sh) / 2
         elif self.alignment == Anchor.CENTER_CENTER:
             x = (self.width - sw) / 2
             y = (self.height - sh) / 2
         elif self.alignment == Anchor.CENTER_RIGHT:
             x = (self.width - sw)
             y = (self.height - sh) / 2
         elif self.alignment == Anchor.BOTTOM_LEFT:
             x = 0
             y = self.height - sh
         elif self.alignment == Anchor.BOTTOM_CENTER:
             x = (self.width - sw) / 2
             y = self.height - sh
         elif self.alignment == Anchor.BOTTOM_RIGHT:
             x = self.width - sw
             y = self.height - sh
         x /= s
         y /= s
         ctx.set_source_surface(self._image, x, y)
         ctx.paint()
     else:
         ctx.move_to(0, 0)
         ctx.line_to(self.size.width, self.size.height)
         ctx.stroke()
         ctx.move_to(0, self.size.height)
         ctx.line_to(self.size.width, 0)
         ctx.stroke()
示例#11
0
def _rectangle_path(
        context: cairocffi.Context,
        rectangle: pangocffi.Rectangle
):
    x, y, width, height = _convert_rectangle_to_pixels(rectangle)
    context.move_to(x, y)
    context.line_to(x + width, y)
    context.line_to(x + width, y + height)
    context.line_to(x, y + height)
    context.line_to(x, y)
    def __draw_sector (self: 'HueSatWheelWidget', cr: cairocffi.Context,
                       center_x: float, center_y: float) -> None:
        cr.save ()
        cr.set_line_width (1)

        offset = self.rotation

        for idx, sector in enumerate (self.sector[0]):
            half_angle = 2 * numpy.pi * sector / 2
            offset += self.sector[1][idx] * 2 * numpy.pi
            cr.set_source_rgba (0, 0, 0, 1)
            cr.move_to (center_x, center_y)
            cr.arc (center_x, center_y, (self.size - 2) / 2.,
                    offset - half_angle, half_angle + offset)
            cr.line_to (center_x, center_y)
            cr.stroke_preserve ()
            cr.set_source_rgba (0, 0, 0, 0.25)
            cr.fill ()

        cr.restore ()
    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(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
示例#15
0
def _show_layout_y_ranges(
        context: cairocffi.Context,
        layout: pangocffi.Layout
):
    layout_iter = layout.get_iter()
    context.set_line_width(0.5)
    context.set_dash([1, 1])
    alternate = True
    while True:
        alternate = not alternate
        extents = layout_iter.get_line_extents()
        y_ranges = layout_iter.get_line_yrange()

        context.set_source_rgba(0, 0, 1 if alternate else 0.5, 0.9)
        context.move_to(
            pangocffi.units_to_double(extents[0].x),
            pangocffi.units_to_double(y_ranges[0])
        )
        context.line_to(
            pangocffi.units_to_double(extents[0].x + extents[0].width),
            pangocffi.units_to_double(y_ranges[0])
        )
        context.stroke()

        context.move_to(
            pangocffi.units_to_double(extents[0].x),
            pangocffi.units_to_double(y_ranges[1])
        )
        context.line_to(
            pangocffi.units_to_double(extents[0].x + extents[0].width),
            pangocffi.units_to_double(y_ranges[1])
        )
        context.stroke()

        if not layout_iter.next_run():
            break
    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
示例#17
0
def _show_layout_baseline(
        context: cairocffi.Context,
        layout: pangocffi.Layout
):
    layout_iter = layout.get_iter()
    context.set_line_width(0.5)
    context.set_dash([1, 1])
    while True:
        extents = layout_iter.get_line_extents()
        baseline = layout_iter.get_baseline()
        y_ranges = layout_iter.get_line_yrange()

        context.set_source_rgba(1, 0, 0, 0.9)
        context.move_to(
            pangocffi.units_to_double(extents[0].x),
            pangocffi.units_to_double(y_ranges[0])
        )
        context.line_to(
            pangocffi.units_to_double(extents[0].x + extents[0].width),
            pangocffi.units_to_double(y_ranges[0])
        )
        context.stroke()

        context.set_source_rgba(0, 1, 0, 0.9)
        context.stroke()
        context.move_to(
            pangocffi.units_to_double(extents[0].x),
            pangocffi.units_to_double(baseline)
        )
        context.line_to(
            pangocffi.units_to_double(extents[0].x + extents[0].width),
            pangocffi.units_to_double(baseline)
        )
        context.stroke()

        context.set_source_rgba(0, 0, 1, 0.9)
        context.move_to(
            pangocffi.units_to_double(extents[0].x),
            pangocffi.units_to_double(y_ranges[1])
        )
        context.line_to(
            pangocffi.units_to_double(extents[0].x + extents[0].width),
            pangocffi.units_to_double(y_ranges[1])
        )
        context.stroke()

        if not layout_iter.next_run():
            break
示例#18
0
def draw_polyline(ctx: cairo.Context, xys):
    ctx.move_to(*xys[0])
    for x, y in xys[1:]:
        ctx.line_to(x, y)
示例#19
0
                y = mean_pin_y - label_height // 2
                print(f"{text=}\t{y=}")

                if pass_ == 0:
                    ctx.set_source_rgba(*color, 1)
                    draw_rounded_rectangle(ctx, x, y, label_width,
                                           label_height, label_radius)
                    ctx.fill()

                    for pin_name in pin_names:
                        pin_x, pin_y, align = pin_defs[pin_name]
                        pin_x = pin_x * w
                        pin_y = pin_y * h_bg - h_bg * y_offset

                        ctx.move_to(line_origin_x, y + label_height // 2)
                        ctx.line_to(w * pad_sides + pin_x, pin_y)
                        ctx.set_line_width(4)
                        ctx.stroke()
                else:
                    ctx.move_to(x + label_pad[0] - x_bearing,
                                y - y_bearing + label_pad[1])
                    ctx.set_source_rgb(1, 1, 1)
                    ctx.show_text(text)

        # save to file
        canvas.write_to_png(f"{board_name}_{variant_name}.png")

        # stride = ImageSurface.format_stride_for_width(format, width)
        # data = bytearray(stride * height)
        # surface = ImageSurface(format, width, height, data, stride)