#!/usr/bin/env python3
"""
生成测试图片（更大字体）
"""
from PIL import Image, ImageDraw, ImageFont
import os

# 字体设置
font_paths = [
    "C:/Windows/Fonts/msyh.ttc",
    "C:/Windows/Fonts/simhei.ttf",
    "C:/Windows/Fonts/simsun.ttc"
]
font_path = None
for fp in font_paths:
    if os.path.exists(fp):
        font_path = fp
        break

if not font_path:
    print("错误: 找不到中文字体")
    exit(1)

# 字体大小（再加大一号）
title_font = ImageFont.truetype(font_path, 48)
subtitle_font = ImageFont.truetype(font_path, 32)
cn_font = ImageFont.truetype(font_path, 46)
en_font = ImageFont.truetype(font_path, 34)

def wrap_text(text, font, max_width):
    """自动换行"""
    words = list(text)
    lines = []
    current_line = []
    for word in words:
        test_line = ''.join(current_line + [word])
        bbox = font.getbbox(test_line)
        text_width = bbox[2] - bbox[0]
        if text_width <= max_width:
            current_line.append(word)
        else:
            if current_line:
                lines.append(''.join(current_line))
            current_line = [word]
    if current_line:
        lines.append(''.join(current_line))
    return lines

def create_frame(title, subtitle, cn_text, en_text, output_path):
    """生成测试图片"""
    width, height = 1080, 1920
    img = Image.new('RGB', (width, height), color='#1a1a2e')
    draw = ImageDraw.Draw(img)

    # 标题（黄色）
    title_bbox = draw.textbbox((0, 0), title, font=title_font)
    title_width = title_bbox[2] - title_bbox[0]
    draw.text(((width - title_width) // 2, 180), title, font=title_font, fill='#FFD700')

    # 副标题（白色）
    sub_bbox = draw.textbbox((0, 0), subtitle, font=subtitle_font)
    sub_width = sub_bbox[2] - sub_bbox[0]
    draw.text(((width - sub_width) // 2, 280), subtitle, font=subtitle_font, fill='white')

    # 中文（黄色，自动换行）
    max_width = width - 100
    cn_lines = wrap_text(cn_text, cn_font, max_width)
    y_offset = 750
    for line in cn_lines:
        line_bbox = draw.textbbox((0, 0), line, font=cn_font)
        line_width = line_bbox[2] - line_bbox[0]
        draw.text(((width - line_width) // 2, y_offset), line, font=cn_font, fill='#FFD700')
        y_offset += 65

    # 英文（白色，自动换行）
    y_offset += 30
    en_lines = wrap_text(en_text, en_font, max_width)
    for line in en_lines:
        line_bbox = draw.textbbox((0, 0), line, font=en_font)
        line_width = line_bbox[2] - line_bbox[0]
        draw.text(((width - line_width) // 2, y_offset), line, font=en_font, fill='white')
        y_offset += 55

    img.save(output_path, quality=95)

# 测试文本
title = "师徒一场_张雪峰走好_上"
subtitle = "源自：@梁老师的故事汇"
cn_text = "我确定了啊二十五号要去苏州访友加上办事的行程。确定了行程之后呢，我给这个张雪峰啊发了微信。"
en_text = "I've confirmed that I'll be visiting Suzhou on the 25th for both social calls and business. After finalizing the itinerary, I sent a WeChat message to Zhang Xuefeng."

output_path = "test_frame_large.jpg"
create_frame(title, subtitle, cn_text, en_text, output_path)
print(f"测试图片已生成: {output_path}")

# 打开图片
os.startfile(output_path)
