|
|
|
|
|
"""
|
|
|
Test script for dynamic font sizing functionality
|
|
|
This script tests the new smart font sizing system for Arabic names
|
|
|
"""
|
|
|
|
|
|
import os
|
|
|
import sys
|
|
|
import tempfile
|
|
|
import shutil
|
|
|
from pathlib import Path
|
|
|
|
|
|
|
|
|
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
|
|
|
|
|
from app import (
|
|
|
calculate_optimal_font_size,
|
|
|
extract_placeholder_contexts,
|
|
|
create_dynamic_font_sizing_rules,
|
|
|
apply_dynamic_font_sizing,
|
|
|
validate_docx_structure,
|
|
|
apply_template_font_settings
|
|
|
)
|
|
|
|
|
|
|
|
|
def test_font_size_calculation():
|
|
|
"""Test the font size calculation function"""
|
|
|
print("🧪 Testing font size calculation...")
|
|
|
|
|
|
|
|
|
test_cases = [
|
|
|
("محمد", 20, 10, "Short name"),
|
|
|
("محمد أحمد", 20, 10, "Medium name"),
|
|
|
("محمد عبدالله أحمد", 20, 10, "Long name"),
|
|
|
("محمد عبدالله أحمد الخالدي", 20, 10, "Very long name"),
|
|
|
("عبدالرحمن محمد سليمان عبدالعزيز الفهد", 20, 10, "Extremely long name")
|
|
|
]
|
|
|
|
|
|
for name, max_chars, base_size, description in test_cases:
|
|
|
optimal_size = calculate_optimal_font_size(name, max_chars, base_size)
|
|
|
print(f" • {description}: '{name}' ({len(name)} chars) → {optimal_size}pt")
|
|
|
|
|
|
print("✅ Font size calculation tests completed\n")
|
|
|
|
|
|
|
|
|
def test_with_sample_names():
|
|
|
"""Test with realistic Arabic names"""
|
|
|
print("🧪 Testing with realistic Arabic names...")
|
|
|
|
|
|
sample_names = [
|
|
|
"علي",
|
|
|
"محمد أحمد",
|
|
|
"فاطمة سعد",
|
|
|
"عبدالله محمد أحمد",
|
|
|
"محمد عبدالله الخالدي",
|
|
|
"فاطمة سعد محمد العتيبي",
|
|
|
"عبدالرحمن خالد سليمان",
|
|
|
"محمد عبدالله أحمد سليمان الفهد",
|
|
|
"عبدالرحمن محمد سليمان عبدالعزيز الخالدي"
|
|
|
]
|
|
|
|
|
|
|
|
|
print(" 📊 Table context (max 15 chars):")
|
|
|
for name in sample_names:
|
|
|
optimal_size = calculate_optimal_font_size(name, 15, 10)
|
|
|
print(f" • '{name}' ({len(name)} chars) → {optimal_size}pt")
|
|
|
|
|
|
print("\n 📄 Paragraph context (max 25 chars):")
|
|
|
for name in sample_names:
|
|
|
optimal_size = calculate_optimal_font_size(name, 25, 11)
|
|
|
print(f" • '{name}' ({len(name)} chars) → {optimal_size}pt")
|
|
|
|
|
|
print("✅ Realistic names tests completed\n")
|
|
|
|
|
|
|
|
|
def create_test_docx():
|
|
|
"""Create a test DOCX file with placeholders"""
|
|
|
print("📄 Creating test DOCX file...")
|
|
|
|
|
|
|
|
|
test_content = '''<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
|
|
|
<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
|
|
|
<w:body>
|
|
|
<w:tbl>
|
|
|
<w:tr>
|
|
|
<w:tc>
|
|
|
<w:p>
|
|
|
<w:r>
|
|
|
<w:rPr>
|
|
|
<w:rFonts w:ascii="Arial" w:hAnsi="Arial"/>
|
|
|
<w:sz w:val="20"/>
|
|
|
</w:rPr>
|
|
|
<w:t>الاسم: {{name_1}}</w:t>
|
|
|
</w:r>
|
|
|
</w:p>
|
|
|
</w:tc>
|
|
|
<w:tc>
|
|
|
<w:p>
|
|
|
<w:r>
|
|
|
<w:rPr>
|
|
|
<w:rFonts w:ascii="Arial" w:hAnsi="Arial"/>
|
|
|
<w:sz w:val="20"/>
|
|
|
</w:rPr>
|
|
|
<w:t>رقم الهوية: {{id_1}}</w:t>
|
|
|
</w:r>
|
|
|
</w:p>
|
|
|
</w:tc>
|
|
|
</w:tr>
|
|
|
</w:tbl>
|
|
|
<w:p>
|
|
|
<w:r>
|
|
|
<w:rPr>
|
|
|
<w:rFonts w:ascii="Arial" w:hAnsi="Arial"/>
|
|
|
<w:sz w:val="22"/>
|
|
|
</w:rPr>
|
|
|
<w:t>الطرف الثاني: {{name_2}}</w:t>
|
|
|
</w:r>
|
|
|
</w:p>
|
|
|
</w:body>
|
|
|
</w:document>'''
|
|
|
|
|
|
print("✅ Test DOCX content created\n")
|
|
|
return test_content
|
|
|
|
|
|
|
|
|
def test_placeholder_extraction():
|
|
|
"""Test placeholder context extraction"""
|
|
|
print("🧪 Testing placeholder extraction...")
|
|
|
|
|
|
test_content = create_test_docx()
|
|
|
|
|
|
|
|
|
placeholders = ["name_1", "id_1", "name_2"]
|
|
|
|
|
|
print(f" • Found placeholders: {placeholders}")
|
|
|
|
|
|
|
|
|
sample_rules = {
|
|
|
'name_1': {
|
|
|
'max_chars': 15,
|
|
|
'context': 'table_cell',
|
|
|
'base_font_size': 10,
|
|
|
'min_font_size': 7
|
|
|
},
|
|
|
'id_1': {
|
|
|
'max_chars': 15,
|
|
|
'context': 'table_cell',
|
|
|
'base_font_size': 10,
|
|
|
'min_font_size': 7
|
|
|
},
|
|
|
'name_2': {
|
|
|
'max_chars': 25,
|
|
|
'context': 'paragraph',
|
|
|
'base_font_size': 11,
|
|
|
'min_font_size': 8
|
|
|
}
|
|
|
}
|
|
|
|
|
|
print(" • Sample dynamic rules created:")
|
|
|
for placeholder, rules in sample_rules.items():
|
|
|
print(f" - {placeholder}: {rules}")
|
|
|
|
|
|
print("✅ Placeholder extraction tests completed\n")
|
|
|
|
|
|
|
|
|
def test_complete_workflow():
|
|
|
"""Test the complete dynamic sizing workflow"""
|
|
|
print("🧪 Testing complete workflow...")
|
|
|
|
|
|
|
|
|
sample_data = {
|
|
|
'name_1': 'محمد عبدالله أحمد الخالدي',
|
|
|
'name_2': 'فاطمة سعد',
|
|
|
'name_3': 'عبدالرحمن خالد سليمان',
|
|
|
'id_1': '1234567890',
|
|
|
'id_2': '0987654321'
|
|
|
}
|
|
|
|
|
|
|
|
|
dynamic_rules = {
|
|
|
'name_1': {'max_chars': 15, 'context': 'table_cell', 'base_font_size': 10, 'min_font_size': 7},
|
|
|
'name_2': {'max_chars': 25, 'context': 'paragraph', 'base_font_size': 11, 'min_font_size': 8},
|
|
|
'name_3': {'max_chars': 20, 'context': 'table_cell', 'base_font_size': 10, 'min_font_size': 7},
|
|
|
'id_1': {'max_chars': 15, 'context': 'table_cell', 'base_font_size': 9, 'min_font_size': 7},
|
|
|
'id_2': {'max_chars': 15, 'context': 'table_cell', 'base_font_size': 9, 'min_font_size': 7}
|
|
|
}
|
|
|
|
|
|
print(" 📊 Calculating optimal sizes for sample data:")
|
|
|
for placeholder, data in sample_data.items():
|
|
|
if placeholder in dynamic_rules:
|
|
|
rules = dynamic_rules[placeholder]
|
|
|
optimal_size = calculate_optimal_font_size(
|
|
|
data,
|
|
|
max_width_chars=rules['max_chars'],
|
|
|
base_font_size=rules['base_font_size']
|
|
|
)
|
|
|
optimal_size = max(optimal_size, rules['min_font_size'])
|
|
|
|
|
|
print(f" • {placeholder}: '{data}' → {optimal_size}pt (context: {rules['context']})")
|
|
|
|
|
|
print("✅ Complete workflow tests completed\n")
|
|
|
|
|
|
|
|
|
def main():
|
|
|
"""Run all tests"""
|
|
|
print("🚀 Starting Dynamic Font Sizing Tests\n")
|
|
|
print("=" * 60)
|
|
|
|
|
|
test_font_size_calculation()
|
|
|
test_with_sample_names()
|
|
|
test_placeholder_extraction()
|
|
|
test_complete_workflow()
|
|
|
|
|
|
print("=" * 60)
|
|
|
print("🎉 All tests completed successfully!")
|
|
|
print("\n💡 Key Benefits of the New System:")
|
|
|
print(" • ✅ Automatic font size adjustment based on text length")
|
|
|
print(" • ✅ Context-aware sizing (table vs paragraph)")
|
|
|
print(" • ✅ Maintains Arial font consistency")
|
|
|
print(" • ✅ Preserves exact positioning of placeholders")
|
|
|
print(" • ✅ Handles Arabic names of any length")
|
|
|
print(" • ✅ Prevents text overflow and layout breaks")
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
main()
|
|
|
|