#!/usr/bin/env python3
"""
生成枪炮、病菌与钢铁的字幕图片（adid=8096）- 最终版2
"""
import sqlite3
import os
from PIL import Image, ImageDraw, ImageFont

db_path = "\\\\ll\\D\\xtrssvjj\\dpd.db"
frames_dir = "shitu_frames"
if not os.path.exists(frames_dir):
    os.makedirs(frames_dir)

# 字体设置
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, 58)  # 标题58px
subtitle_font = ImageFont.truetype(font_path, 44)  # 副标题44px
cn_font = ImageFont.truetype(font_path, 66)  # 中文66px（更大）
en_font = ImageFont.truetype(font_path, 66)  # 英文66px（和中文一致）

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)

    # 标题（白色，自动换行）
    max_width = width - 100
    title_lines = wrap_text(title, title_font, max_width)
    y_offset = 120
    for line in title_lines:
        line_bbox = draw.textbbox((0, 0), line, font=title_font)
        line_width = line_bbox[2] - line_bbox[0]
        draw.text(((width - line_width) // 2, y_offset), line, font=title_font, fill='white')
        y_offset += 70

    # 副题与标题隔开一行
    y_offset += 40

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

    # 再隔五行显示下面的字幕（之前是三行，现在增加两行）
    y_offset += 40 + 5 * 40  # 40px间隔 + 5行空行

    # 中文（黄色，自动换行）
    cn_lines = wrap_text(cn_text, cn_font, max_width)
    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 += 100

    # 英文（白色，自动换行）
    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 += 100

    img.save(output_path, quality=95)

# 测试生成一张图片
title = "高晓松向马云推荐的三本书之一《枪炮_病菌与钢铁》: 我们人类到底是怎么一步步走到今天这个样子的"
subtitle = "中文源自@晓松故事会"
cn_text = "今天咱们聊一本对我影响特别大的书"
en_text = "Today let's talk about a book that has had a huge impact on me"

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

# 打开图片
os.startfile(output_path)
