Ejemplo n.º 1
0
    def get_space_width(self):
        size = self.size * 10

        dir_name = consts.TEXT_DIR
        file_name = os.path.join(dir_name, "space") + '.svg'

        surface = cairo.SVGSurface(file_name, 600, 400)
        context = cairo.Context(surface)
        context.set_font_size(size)
        context.move_to(START_X, START_Y)
        context.select_font_face(self.font, self.str2slant(self.slant),
                                 self.str2weight(self.weight))
        context.move_to(START_X, START_Y)
        context.show_text("_")
        surface.finish()
        svg_with_space = SVGMobject(
            file_name,
            height=self.height,
            width=self.width,
            stroke_width=self.stroke_width,
            should_center=self.should_center,
            unpack_groups=self.unpack_groups,
        )
        space_width = svg_with_space.get_width()
        return space_width
Ejemplo n.º 2
0
    def __init__(self, tex_string, **kwargs):
        super().__init__(**kwargs)
        assert (isinstance(tex_string, str))
        self.tex_string = tex_string
        if tex_string not in tex_string_with_color_to_mob_map:
            with display_during_execution(f" Writing \"{tex_string}\""):
                full_tex = self.get_tex_file_body(tex_string)
                filename = tex_to_svg_file(full_tex)
                svg_mob = SVGMobject(filename,
                                     height=None,
                                     color=self.color,
                                     stroke_width=self.stroke_width,
                                     path_string_config={
                                         "should_subdivide_sharp_curves": True,
                                         "should_remove_null_curves": True,
                                     })
                tex_string_with_color_to_mob_map[(self.color,
                                                  tex_string)] = svg_mob
        self.add(*(sm.copy()
                   for sm in tex_string_with_color_to_mob_map[(self.color,
                                                               tex_string)]))
        self.init_colors(override=False)

        if self.height is None:
            self.scale(SCALE_FACTOR_PER_FONT_POINT * self.font_size)
        if self.organize_left_to_right:
            self.organize_submobjects_left_to_right()
Ejemplo n.º 3
0
    def generate_mobject(self) -> None:
        super().generate_mobject()

        labels_count = len(self.labelled_spans)
        if not labels_count:
            for submob in self.submobjects:
                submob.label = -1
            return

        labelled_content = self.get_content(is_labelled=True)
        file_path = self.get_file_path_by_content(labelled_content)
        labelled_svg = SVGMobject(file_path)
        if len(self.submobjects) != len(labelled_svg.submobjects):
            log.warning("Cannot align submobjects of the labelled svg "
                        "to the original svg. Skip the labelling process.")
            for submob in self.submobjects:
                submob.label = -1
            return

        self.rearrange_submobjects_by_positions(labelled_svg)
        unrecognizable_colors = []
        for submob, labelled_svg_submob in zip(self.submobjects,
                                               labelled_svg.submobjects):
            color_int = self.hex_to_int(
                self.color_to_hex(labelled_svg_submob.get_fill_color()))
            if color_int > labels_count:
                unrecognizable_colors.append(color_int)
                color_int = 0
            submob.label = color_int - 1
        if unrecognizable_colors:
            log.warning(
                "Unrecognizable color labels detected (%s, etc). "
                "The result could be unexpected.",
                self.int_to_hex(unrecognizable_colors[0]))
Ejemplo n.º 4
0
    def construct(self):
        obj = SVGMobject("dvsv").scale(3)

        points = VGroup(*[
            Dot(obj.point_from_proportion(alpha))
            for alpha in np.linspace(0, 1, 33)
        ])

        self.add(obj)
        self.add(points)
        self.wait()
Ejemplo n.º 5
0
    def get_face_card_design(self, value, symbol):
        from for_3b1b_videos.pi_creature import PiCreature
        sub_rect = Rectangle(
            stroke_color=BLACK,
            fill_opacity=0,
            height=0.9 * self.get_height(),
            width=0.6 * self.get_width(),
        )
        sub_rect.move_to(self)

        # pi_color = average_color(symbol.get_color(), GREY)
        pi_color = symbol.get_color()
        pi_mode = {
            "J": "plain",
            "Q": "thinking",
            "K": "hooray"
        }[value]
        pi_creature = PiCreature(
            mode=pi_mode,
            color=pi_color,
        )
        pi_creature.set_width(0.8 * sub_rect.get_width())
        if value in ["Q", "K"]:
            prefix = "king" if value == "K" else "queen"
            crown = SVGMobject(file_name=prefix + "_crown")
            crown.set_stroke(width=0)
            crown.set_fill(YELLOW, 1)
            crown.stretch_to_fit_width(0.5 * sub_rect.get_width())
            crown.stretch_to_fit_height(0.17 * sub_rect.get_height())
            crown.move_to(pi_creature.eyes.get_center(), DOWN)
            pi_creature.add_to_back(crown)
            to_top_buff = 0
        else:
            to_top_buff = SMALL_BUFF * sub_rect.get_height()
        pi_creature.next_to(sub_rect.get_top(), DOWN, to_top_buff)
        # pi_creature.shift(0.05*sub_rect.get_width()*RIGHT)

        pi_copy = pi_creature.copy()
        pi_copy.rotate(np.pi, about_point=sub_rect.get_center())

        return VGroup(sub_rect, pi_creature, pi_copy)