# -*- coding: utf-8 -*-
# 示例 2.2：Paragraph 的对齐方向 alignment（对应视频 20240826201304001）
# 本地复现：manim -qh 20240826201304001.py AlignmentDemo
# 知识点：alignment 只管同一段落内各行的水平对齐，取值 "left" / "right" / "center"，
#         默认左对齐。它不改文字内容，也不决定整段在画面上的位置——位置由 move_to 决定。
# 中文注意：Paragraph 基于 Text，含中文必须显式指定中文字体，否则会渲染成白色方块。
from manim import *
# STLiti = 华文隶书（C:\Windows\Fonts\STLITI.TTF）；西文等宽字体不含中文字形。
FONT = "STLiti"
# 三行长度不同，才能看出「左端齐」「右端齐」「中线对称」的区别。
LINES = ("第一行：内容中等内容中等", "第二行：内容短", "第三行：内容较长内容较长内容较长")
# t2c 是 Text 的参数，Paragraph 同样支持：按子串给字符上色，三行一眼可分。
COLORS = {"第一行": RED, "第二行": BLUE, "第三行": GREEN}
class AlignmentDemo(Scene):
    def construct(self):
        title = Text("Paragraph 对齐方向 alignment", font=FONT, font_size=44)
        title.to_edge(UP, buff=0.45)
        frame = Rectangle(width=8.4, height=3.4, color="#9AA0A6", stroke_width=2)
        frame.shift(DOWN * 0.45)
        # x_edge 是这三行文字（32 号字）的自然半宽；两条边线 + 一条中线当标尺，
        # 左对齐贴左线、右对齐贴右线、居中对齐关于中线对称，肉眼即可分辨。
        x_edge = 3.57
        guides = VGroup(
            DashedLine(frame.get_top() + LEFT * x_edge, frame.get_bottom() + LEFT * x_edge,
                       dash_length=0.12, color="#5F6368"),
            DashedLine(frame.get_top() + RIGHT * x_edge, frame.get_bottom() + RIGHT * x_edge,
                       dash_length=0.12, color="#5F6368"),
            DashedLine(frame.get_top(), frame.get_bottom(),
                       dash_length=0.12, color="#3C4043"),
        )
        self.play(FadeIn(title, shift=DOWN * 0.2), Create(frame), Create(guides), run_time=0.7)
        cases = [("left", "左对齐（默认）", "三行左端对齐，右侧参差"),
                 ("right", "右对齐", "三行右端对齐，左侧参差"),
                 ("center", "居中对齐", "三行关于中线对称")]
        for a, label, tip in cases:
            # alignment 同样是构造参数，每档新建一个 Paragraph。
            para = Paragraph(*LINES, alignment=a, font=FONT, font_size=32, t2c=COLORS)
            para.move_to(frame)
            badge = Text("alignment = \"" + a + "\"   " + label, font=FONT, font_size=30, color=YELLOW)
            badge.next_to(frame, DOWN, buff=0.28)
            note = Text(tip, font=FONT, font_size=24, color="#BDC1C6")
            note.next_to(badge, DOWN, buff=0.14)
            self.play(FadeIn(para, shift=UP * 0.12), FadeIn(badge), FadeIn(note), run_time=0.5)
            self.wait(1.0)
            # 最后一档留在屏上作为完成态（封面取第 5 秒帧）。
            if a != "center":
                self.play(FadeOut(para), FadeOut(badge), FadeOut(note), run_time=0.35)
        self.wait(2.2)
