Пример #1
0
def draw_elements(elements: Sequence[Element], ctx: cairo.Context,
                  scale: float):
    ctx.save()
    for element in elements:
        if element.drawing is not None:
            draw(element.drawing, ctx, scale)
        ctx.translate(element.thickness * scale, 0)
    ctx.restore()
Пример #2
0
 def paint_children(self, ctx: Context):
     for child in self.children:
         ctx.save()
         ctx.translate(*child.position(Anchor.TOP_LEFT))
         ctx.rectangle(0, 0, *child.size)
         ctx.clip()
         child.paint(ctx)
         ctx.restore()
Пример #3
0
def apply_viewbox(ctx: cairo.Context, width: float, height: float,
                  viewbox) -> float:
    viewbox_width = viewbox[2]
    viewbox_height = viewbox[3]
    scale = min(width / viewbox_width, height / viewbox_height)
    ctx.translate(-viewbox[0] * scale, -viewbox[1] * scale)
    ctx.scale(scale, scale)
    translate_x = (width / scale - viewbox_width) / 2
    translate_y = (height / scale - viewbox_height) / 2
    ctx.translate(translate_x, translate_y)
    return scale
Пример #4
0
    def draw(self, context: Context):
        text_path_glyph_items = self._compute_text_path_glyph_items()
        for text_path_glyph_item in text_path_glyph_items:
            glyph_position = text_path_glyph_item.position
            glyph_rotation = text_path_glyph_item.rotation

            context.save()
            context.translate(glyph_position.x, glyph_position.y)
            context.rotate(glyph_rotation)
            show_glyph_item(context, self._layout_text,
                            text_path_glyph_item.glyph_item)
            context.restore()
Пример #5
0
 def draw(self, ctx: Context, surface: Surface):
     ctx.save()
     ctx.translate(self.translate_x, self.translate_y)
     ctx.rotate(self.rotation)
     ctx.scale(self.width / self.svg_width, self.height / self.svg_height)
     ctx.translate(-self.origin_x, -self.origin_y)
     CairoSVGSurface(self.tree,
                     None,
                     96,
                     output_cairo=surface,
                     output_cairo_context=ctx)
     ctx.restore()
Пример #6
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()
Пример #7
0
    def draw(self, ctx: Context):

        text_baseline = self.get_text_baseline(ctx)

        default_font_max_height = ctx.get_scaled_font().extents()[0]

        text_max_length = float('inf')
        line_string_bottom_length = text_baseline.length

        trimmed = False
        while text_max_length > line_string_bottom_length:
            text_max_length = ctx.text_extents(self.text)[4] / default_font_max_height * self.text_height + \
                              (len(self.text)-1) * self.text_spacing
            if text_max_length > line_string_bottom_length:
                self.text = self.text[:-1].strip()
                trimmed = True

        text_start_interp_pos_min = 0
        text_start_interp_pos_max = line_string_bottom_length - text_max_length
        if self.text_alignment == 'left':
            text_start_interp_pos = self.text_alignment_offset
        elif self.text_alignment == 'center':
            text_start_interp_pos = line_string_bottom_length / 2 - text_max_length / 2 - self.text_alignment_offset
        else:
            text_start_interp_pos = line_string_bottom_length - text_max_length - self.text_alignment_offset
        text_start_interp_pos = min(
            text_start_interp_pos_max,
            max(text_start_interp_pos_min, text_start_interp_pos))

        text_interp_pos = text_start_interp_pos
        for char in self.text:
            char_pos_origin = text_baseline.interpolate(text_interp_pos)
            char_advance_x = ctx.text_extents(
                char)[4] / default_font_max_height * self.text_height
            char_pos_end = text_baseline.interpolate(text_interp_pos +
                                                     char_advance_x)

            ctx.save()
            ctx.translate(char_pos_origin.x, char_pos_origin.y)
            text_angle = math.atan2(char_pos_end.y - char_pos_origin.y,
                                    char_pos_end.x - char_pos_origin.x)
            ctx.rotate(text_angle)
            ctx.scale(1 / default_font_max_height * self.text_height)
            if not trimmed:
                ctx.set_source_rgba(0, 0, 0, 1)
            else:
                ctx.set_source_rgba(0, 0, 0.2, 1)
            ctx.show_text(char)
            ctx.fill()
            ctx.restore()

            text_interp_pos += char_advance_x + self.text_spacing
Пример #8
0
def _show_label(
        context: cairocffi.Context,
        position: Tuple[float, float],
        width: float,
        text: str
):
    context.translate(position[0], position[1])
    label = pangocairocffi.create_layout(context)
    label.set_width(pangocffi.units_from_double(width))
    label.set_alignment(pangocffi.Alignment.LEFT)
    label.set_markup('<span font-family="sans-serif">%s</span>' % text)
    pangocairocffi.show_layout(context, label)
    context.translate(-position[0], -position[1])
Пример #9
0
def transform(
    ctx: cairo.Context,
    point: Tuple[float, float],
    rotation: float = None,
    scale_x=1.0,
    scale_y=1.0,
):
    ctx.translate(point[0], point[1])
    if rotation is not None:
        ctx.rotate(math.radians(rotation))
    if scale_x != 1.0 or scale_y != 1.0:
        ctx.scale(sx=scale_x, sy=scale_y)
    ctx.translate(-point[0], -point[1])
Пример #10
0
def render_cluster_glyph_items(
        ctx: cairocffi.Context,
        layout: pangocffi.Layout
):
    """
    Renders each cluster within a layout with a unique rotation and color.

    Warning: Does not support bidirectional text.

    :param ctx:
        a Cairo context
    :param layout:
        a Pango layout
    """
    layout_run_iter = layout.get_iter()
    layout_cluster_iter = layout.get_iter()
    layout_text = layout.get_text()

    alternate = False
    while True:

        layout_run = layout_run_iter.get_run()
        layout_line_baseline = layout_run_iter.get_baseline()

        if layout_run is None:
            if not layout_run_iter.next_run():
                break
            continue

        clusters = get_clusters_from_glyph_item(layout_run, layout_text)
        for cluster in clusters:
            cluster_extents = layout_cluster_iter.get_cluster_extents()[1]
            layout_cluster_iter.next_cluster()
            alternate = not alternate
            ctx.set_source_rgba(0.5, 0, 1 if alternate else 0.5, 0.9)
            ctx.save()
            ctx.translate(
                pangocffi.units_to_double(cluster_extents.x),
                pangocffi.units_to_double(layout_line_baseline)
            )
            ctx.rotate(-0.05 if alternate else 0.05)
            pangocairocffi.show_glyph_item(
                ctx,
                layout_text,
                cluster
            )
            ctx.restore()

        if not layout_run_iter.next_run():
            break
Пример #11
0
def label_train_interfaces(ctx: cairo.Context,
                           train: trains.Train,
                           radius_factors=1,
                           broken_spaces: Sequence = None):
    spaces, brokens = break_spaces(train.spaces, broken_spaces)
    radius_factors = np.broadcast_to(radius_factors, (len(train.interfaces), ))

    ctx.save()
    ctx.translate(0, spaces[0])
    for interface, space, radius_factor in zip(train.interfaces, spaces[1:],
                                               radius_factors):
        label_interface(ctx, interface, radius_factor)
        ctx.translate(0, space)
    ctx.restore()
Пример #12
0
def label_train_spaces(ctx: cairo.Context,
                       train: trains.Train,
                       radius_factors=1,
                       broken_spaces: Sequence = None):
    """Draw material name and thickness of the spaces.

    Args:
        radius_factors (scalar or sequence): Factor by which the average interface radii is multiplied to place the text. If
            a scalar is given it is broadcast.
    """
    spaces, brokens = break_spaces(train.spaces, broken_spaces)
    radius_factors = np.broadcast_to(radius_factors, (len(spaces), ))
    radius_last = train.interfaces[0].radius
    y_last = 0
    h_last = 0
    n = train.interfaces[0].n1
    for space, next_interface, radius_factor in zip(
            spaces, train.interfaces + (None, ), radius_factors):
        if next_interface is None:
            radius_next = radius_last
            h_next = 0
        else:
            radius_next = next_interface.radius
            h_next = functions.calc_sphere_sag(next_interface.roc, radius_next)
        y_next = y_last + space
        if space != 0:
            string = '%.3f mm %s' % (space * 1e3, n.name)
            radius = (radius_last + radius_next) / 2
            x = radius_factor * radius
            y = (y_next + h_next + y_last + h_last) / 2

            ctx.save()
            ctx.translate(x, y)
            ctx.show_text(string)
            ctx.new_path()
            ctx.restore()

            if radius_factor > 1:
                draw_polyline(ctx, ((radius, y), (x, y)))
                ctx.stroke()

        y_last = y_next
        h_last = h_next
        if next_interface is not None:
            n = next_interface.n2
            radius_last = next_interface.radius
Пример #13
0
def make_train_interfaces(ctx: cairo.Context,
                          train: trains.Train,
                          broken_spaces: Sequence = None):
    """
    Propagation is along positive y axis, starting from origin.

    Args:
        size (scalar): Full transverse dimension of interfaces.
    """
    spaces, brokens = break_spaces(train.spaces, broken_spaces)

    ctx.save()
    ctx.translate(0, spaces[0])
    for interface, space in zip(train.interfaces, spaces[1:]):
        make_interface(ctx, interface)
        ctx.translate(0, space)
    ctx.restore()
Пример #14
0
    def draw(self, g: Graphics) -> None:
        if self.visible:
            g.save()

            (dx,
             dy) = self.parent.map(lambda p: p.location).value_or(Point(0, 0))
            (cx, cy, cw, ch) = self.ui.clip_bounds(self).tuple

            g.translate(dx, dy)
            g.rectangle(cx, cy, cw, ch)

            g.clip()

            try:
                self.draw_component(g)
            except BaseException as e:
                self.error_handler(e)

            g.restore()
Пример #15
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()
Пример #16
0
def render_run_glyph_items(
        ctx: cairocffi.Context,
        layout: pangocffi.Layout
) -> None:
    """
    Renders each layout run within a layout with a unique rotation and color.

    :param ctx:
        a Cairo context
    :param layout:
        a Pango layout
    """
    layout_iter = layout.get_iter()
    layout_text = layout.get_text()

    alternate = False
    while True:
        layout_run = layout_iter.get_run()
        layout_run_extents = layout_iter.get_run_extents()[1]
        layout_line_baseline = layout_iter.get_baseline()

        if layout_run is None:
            if not layout_iter.next_run():
                break
            continue

        alternate = not alternate
        ctx.set_source_rgba(0, 0.5, 1 if alternate else 0.5, 0.9)
        ctx.save()
        ctx.translate(
            pangocffi.units_to_double(layout_run_extents.x),
            pangocffi.units_to_double(layout_line_baseline)
        )
        ctx.rotate(0.05 if alternate else -0.05)
        pangocairocffi.show_glyph_item(ctx, layout_text, layout_run)
        ctx.restore()

        if not layout_iter.next_run():
            break
Пример #17
0
 def draw(self, ctx: Context, surface: Surface):
     ctx.save()
     ctx.translate(self.position_x, self.position_y)
     ctx.scale(self.width / self.svg_width, self.height / self.svg_height)
     CairoSVGSurface(self.tree, surface, ctx, 96)
     ctx.restore()
Пример #18
0
 def draw_text(ctx: Context, text: str, x: float, y: float, scale: float):
     original = ctx.get_matrix()
     ctx.translate(x, y)
     ctx.scale(scale / 10)  # Default font size is 10 mm (1cm)
     ctx.show_text(text)
     ctx.set_matrix(original)
Пример #19
0
def _(d: Translation, ctx: cairo.Context, scale: float):
    ctx.save()
    ctx.translate(d.x * scale, d.y * scale)
    draw(d.child, ctx, scale)
    ctx.restore()