trying to fix in built templates
This commit is contained in:
@@ -530,3 +530,163 @@ class TestTemplateManager:
|
||||
assert scaled[0].text_content == "Test"
|
||||
assert scaled[0].font_settings == font_settings
|
||||
assert scaled[0].alignment == text.alignment
|
||||
|
||||
def test_grid_2x2_stretch_to_square_page(self):
|
||||
"""Test Grid_2x2 template applied to square page with stretch mode"""
|
||||
manager = TemplateManager()
|
||||
|
||||
# Create a 2x2 grid template at 210x210mm (margin-less, fills entire space)
|
||||
template = Template(name="Grid_2x2", page_size_mm=(210, 210))
|
||||
# 4 cells: each 105 x 105mm (half of 210mm)
|
||||
template.add_element(PlaceholderData(x=0, y=0, width=105, height=105))
|
||||
template.add_element(PlaceholderData(x=105, y=0, width=105, height=105))
|
||||
template.add_element(PlaceholderData(x=0, y=105, width=105, height=105))
|
||||
template.add_element(PlaceholderData(x=105, y=105, width=105, height=105))
|
||||
|
||||
# Apply to same size page with stretch mode and 2.5% margin
|
||||
layout = PageLayout(width=210, height=210)
|
||||
page = Page(layout=layout, page_number=1)
|
||||
|
||||
manager.apply_template_to_page(
|
||||
template, page,
|
||||
mode="replace",
|
||||
scale_mode="stretch",
|
||||
margin_percent=2.5
|
||||
)
|
||||
|
||||
# With 2.5% margin on 210mm page: margin = 5.25mm, content area = 199.5mm
|
||||
# Template is 210mm, so scale = 199.5 / 210 = 0.95
|
||||
# Each element should scale by 0.95 and be offset by margin
|
||||
assert len(page.layout.elements) == 4
|
||||
|
||||
# Check first element (top-left)
|
||||
elem = page.layout.elements[0]
|
||||
scale = 199.5 / 210.0 # 0.95
|
||||
expected_x = 0 * scale + 5.25 # 0 + 5.25 = 5.25
|
||||
expected_y = 0 * scale + 5.25 # 0 + 5.25 = 5.25
|
||||
expected_width = 105 * scale # 99.75
|
||||
expected_height = 105 * scale # 99.75
|
||||
|
||||
assert abs(elem.position[0] - expected_x) < 0.1
|
||||
assert abs(elem.position[1] - expected_y) < 0.1
|
||||
assert abs(elem.size[0] - expected_width) < 0.1
|
||||
assert abs(elem.size[1] - expected_height) < 0.1
|
||||
|
||||
def test_grid_2x2_stretch_to_a4_page(self):
|
||||
"""Test Grid_2x2 template applied to A4 page with stretch mode"""
|
||||
manager = TemplateManager()
|
||||
|
||||
# Create Grid_2x2 template (210x210mm, margin-less)
|
||||
template = Template(name="Grid_2x2", page_size_mm=(210, 210))
|
||||
template.add_element(PlaceholderData(x=0, y=0, width=105, height=105))
|
||||
template.add_element(PlaceholderData(x=105, y=0, width=105, height=105))
|
||||
template.add_element(PlaceholderData(x=0, y=105, width=105, height=105))
|
||||
template.add_element(PlaceholderData(x=105, y=105, width=105, height=105))
|
||||
|
||||
# Apply to A4 page (210x297mm) with stretch mode and 2.5% margin
|
||||
layout = PageLayout(width=210, height=297)
|
||||
page = Page(layout=layout, page_number=1)
|
||||
|
||||
manager.apply_template_to_page(
|
||||
template, page,
|
||||
mode="replace",
|
||||
scale_mode="stretch",
|
||||
margin_percent=2.5
|
||||
)
|
||||
|
||||
# With 2.5% margin: x_margin = 5.25mm, y_margin = 7.425mm
|
||||
# Content area: 199.5 x 282.15mm
|
||||
# Scale: x = 199.5/210 = 0.95, y = 282.15/210 = 1.3436
|
||||
assert len(page.layout.elements) == 4
|
||||
|
||||
# First element should stretch
|
||||
elem = page.layout.elements[0]
|
||||
scale_x = 199.5 / 210.0
|
||||
scale_y = 282.15 / 210.0
|
||||
|
||||
expected_x = 0 * scale_x + 5.25 # 5.25
|
||||
expected_y = 0 * scale_y + 7.425 # 7.425
|
||||
expected_width = 105 * scale_x # 99.75
|
||||
expected_height = 105 * scale_y # 141.075
|
||||
|
||||
assert abs(elem.position[0] - expected_x) < 0.1
|
||||
assert abs(elem.position[1] - expected_y) < 0.1
|
||||
assert abs(elem.size[0] - expected_width) < 0.1
|
||||
assert abs(elem.size[1] - expected_height) < 0.1
|
||||
|
||||
def test_grid_2x2_with_different_margins(self):
|
||||
"""Test Grid_2x2 template with different margin percentages"""
|
||||
manager = TemplateManager()
|
||||
|
||||
template = Template(name="Grid_2x2", page_size_mm=(210, 210))
|
||||
template.add_element(PlaceholderData(x=0, y=0, width=105, height=105))
|
||||
|
||||
# Test with 0% margin
|
||||
layout = PageLayout(width=210, height=210)
|
||||
page = Page(layout=layout, page_number=1)
|
||||
|
||||
manager.apply_template_to_page(
|
||||
template, page,
|
||||
mode="replace",
|
||||
scale_mode="stretch",
|
||||
margin_percent=0.0
|
||||
)
|
||||
|
||||
# With 0% margin, template fills entire page (scale = 1.0, offset = 0)
|
||||
elem = page.layout.elements[0]
|
||||
assert abs(elem.position[0] - 0.0) < 0.1
|
||||
assert abs(elem.position[1] - 0.0) < 0.1
|
||||
assert abs(elem.size[0] - 105.0) < 0.1
|
||||
|
||||
# Test with 5% margin
|
||||
layout2 = PageLayout(width=210, height=210)
|
||||
page2 = Page(layout=layout2, page_number=1)
|
||||
|
||||
manager.apply_template_to_page(
|
||||
template, page2,
|
||||
mode="replace",
|
||||
scale_mode="stretch",
|
||||
margin_percent=5.0
|
||||
)
|
||||
|
||||
# With 5% margin: margin = 10.5mm, content = 189mm, scale = 189/210 = 0.9
|
||||
elem2 = page2.layout.elements[0]
|
||||
assert abs(elem2.position[0] - 10.5) < 0.1
|
||||
assert abs(elem2.position[1] - 10.5) < 0.1
|
||||
assert abs(elem2.size[0] - (105 * 0.9)) < 0.1
|
||||
|
||||
def test_grid_2x2_proportional_mode(self):
|
||||
"""Test Grid_2x2 template with proportional scaling"""
|
||||
manager = TemplateManager()
|
||||
|
||||
template = Template(name="Grid_2x2", page_size_mm=(210, 210))
|
||||
template.add_element(PlaceholderData(x=0, y=0, width=105, height=105))
|
||||
|
||||
# Apply to rectangular page with proportional mode
|
||||
layout = PageLayout(width=210, height=297)
|
||||
page = Page(layout=layout, page_number=1)
|
||||
|
||||
manager.apply_template_to_page(
|
||||
template, page,
|
||||
mode="replace",
|
||||
scale_mode="proportional",
|
||||
margin_percent=2.5
|
||||
)
|
||||
|
||||
# With proportional mode on 210x297 page:
|
||||
# Content area: 199.5 x 282.15mm
|
||||
# Template: 210 x 210mm
|
||||
# Scale = min(199.5/210, 282.15/210) = 0.95 (uniform)
|
||||
# Content is centered on page
|
||||
|
||||
elem = page.layout.elements[0]
|
||||
scale = 199.5 / 210.0
|
||||
|
||||
# Should be scaled uniformly
|
||||
expected_width = 105 * scale # 99.75
|
||||
expected_height = 105 * scale # 99.75
|
||||
|
||||
assert abs(elem.size[0] - expected_width) < 0.1
|
||||
assert abs(elem.size[1] - expected_height) < 0.1
|
||||
# Width should equal height (uniform scaling)
|
||||
assert abs(elem.size[0] - elem.size[1]) < 0.1
|
||||
|
||||
Reference in New Issue
Block a user