"""
Test script to verify ReportLab and Chinese font rendering
"""
from reportlab.pdfgen import canvas
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
from pathlib import Path
import sys

def test_chinese_rendering():
    """Test if Chinese characters can be rendered in PDF"""

    # Font path
    font_path = "/home/egg/project/Tool_OCR/backend/fonts/NotoSansSC-Regular.ttf"

    # Check if font file exists
    if not Path(font_path).exists():
        print(f"❌ Font file not found: {font_path}")
        return False

    print(f"✓ Font file found: {font_path}")

    try:
        # Register Chinese font
        pdfmetrics.registerFont(TTFont('NotoSansSC', font_path))
        print("✓ Font registered successfully")

        # Create test PDF
        test_pdf = "/tmp/test_chinese.pdf"
        c = canvas.Canvas(test_pdf)

        # Set Chinese font
        c.setFont('NotoSansSC', 14)

        # Draw test text
        c.drawString(100, 750, "測試中文字符渲染 - Test Chinese Character Rendering")
        c.drawString(100, 730, "HTD-S1 技術數據表")
        c.drawString(100, 710, "這是一個 PDF 生成測試")

        c.save()
        print(f"✓ Test PDF created: {test_pdf}")

        # Check file size
        file_size = Path(test_pdf).stat().st_size
        print(f"✓ PDF file size: {file_size} bytes")

        if file_size > 0:
            print("\n✅ Chinese font rendering test PASSED")
            return True
        else:
            print("\n❌ PDF file is empty")
            return False

    except Exception as e:
        print(f"❌ Error during testing: {e}")
        import traceback
        traceback.print_exc()
        return False

if __name__ == "__main__":
    success = test_chinese_rendering()
    sys.exit(0 if success else 1)
