feat: 移除图片适配模式功能
移除图片 fit 和 background 参数支持,简化图片渲染逻辑。系统恢复到直接使用 python-pptx 原生图片添加功能,图片将被拉伸到指定尺寸。 变更内容: - 移除 ImageElement 的 fit 和 background 字段 - 移除 metadata.dpi 配置 - 删除 utils/image_utils.py 图片处理工具模块 - 删除 validators/image_config.py 验证器 - 简化 PPTX 和 HTML 渲染器的图片处理逻辑 - HTML 渲染器使用硬编码 DPI=96(Web 标准) - 删除相关测试文件(单元测试、集成测试、e2e 测试) - 更新规格文档和用户文档 - 保留 Pillow 依赖用于未来可能的图片处理需求 影响: - 删除 11 个文件 - 修改 10 个文件 - 净减少 1558 行代码 - 所有 402 个测试通过 Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -89,19 +89,6 @@ class TestImageElement:
|
||||
assert elem.type == 'image'
|
||||
assert elem.src == "test.png"
|
||||
assert elem.box == [1, 1, 4, 3]
|
||||
assert elem.fit is None
|
||||
assert elem.background is None
|
||||
|
||||
def test_create_with_fit_and_background(self):
|
||||
"""测试创建带 fit 和 background 的 ImageElement"""
|
||||
elem = ImageElement(
|
||||
src="test.png",
|
||||
box=[1, 1, 4, 3],
|
||||
fit="contain",
|
||||
background="#ffffff"
|
||||
)
|
||||
assert elem.fit == "contain"
|
||||
assert elem.background == "#ffffff"
|
||||
|
||||
def test_empty_src_raises_error(self):
|
||||
"""测试空 src 会引发错误"""
|
||||
|
||||
@@ -1,120 +0,0 @@
|
||||
"""
|
||||
单元测试:图片处理工具函数
|
||||
|
||||
测试 utils/image_utils.py 中的图片处理函数。
|
||||
"""
|
||||
|
||||
import pytest
|
||||
from PIL import Image
|
||||
from utils.image_utils import (
|
||||
inches_to_pixels,
|
||||
pixels_to_inches,
|
||||
apply_fit_mode,
|
||||
create_canvas_with_background,
|
||||
)
|
||||
|
||||
|
||||
class TestUnitConversion:
|
||||
"""测试单位转换函数"""
|
||||
|
||||
def test_inches_to_pixels_default_dpi(self):
|
||||
"""测试英寸转像素(默认 96 DPI)"""
|
||||
assert inches_to_pixels(1) == 96
|
||||
assert inches_to_pixels(2) == 192
|
||||
assert inches_to_pixels(0.5) == 48
|
||||
|
||||
def test_inches_to_pixels_custom_dpi(self):
|
||||
"""测试英寸转像素(自定义 DPI)"""
|
||||
assert inches_to_pixels(1, dpi=72) == 72
|
||||
assert inches_to_pixels(2, dpi=150) == 300
|
||||
assert inches_to_pixels(0.5, dpi=300) == 150
|
||||
|
||||
def test_pixels_to_inches_default_dpi(self):
|
||||
"""测试像素转英寸(默认 96 DPI)"""
|
||||
assert pixels_to_inches(96) == 1.0
|
||||
assert pixels_to_inches(192) == 2.0
|
||||
assert pixels_to_inches(48) == 0.5
|
||||
|
||||
def test_pixels_to_inches_custom_dpi(self):
|
||||
"""测试像素转英寸(自定义 DPI)"""
|
||||
assert pixels_to_inches(72, dpi=72) == 1.0
|
||||
assert pixels_to_inches(300, dpi=150) == 2.0
|
||||
|
||||
|
||||
class TestApplyFitMode:
|
||||
"""测试图片适配模式函数"""
|
||||
|
||||
@pytest.fixture
|
||||
def test_image(self):
|
||||
"""创建测试图片(100x100)"""
|
||||
return Image.new("RGB", (100, 100), color="red")
|
||||
|
||||
def test_stretch_mode(self, test_image):
|
||||
"""测试 stretch 模式"""
|
||||
result = apply_fit_mode(test_image, (200, 150), "stretch")
|
||||
assert result.size == (200, 150)
|
||||
|
||||
def test_contain_mode_wider_box(self, test_image):
|
||||
"""测试 contain 模式(box 更宽)"""
|
||||
result = apply_fit_mode(test_image, (200, 100), "contain")
|
||||
# 图片应该适配到高度,宽度保持比例
|
||||
assert result.size == (100, 100)
|
||||
|
||||
def test_contain_mode_taller_box(self, test_image):
|
||||
"""测试 contain 模式(box 更高)"""
|
||||
result = apply_fit_mode(test_image, (100, 200), "contain")
|
||||
# 图片应该适配到宽度,高度保持比例
|
||||
assert result.size == (100, 100)
|
||||
|
||||
def test_cover_mode_wider_box(self, test_image):
|
||||
"""测试 cover 模式(box 更宽)"""
|
||||
result = apply_fit_mode(test_image, (200, 100), "cover")
|
||||
# 图片应该覆盖整个 box
|
||||
assert result.size == (200, 100)
|
||||
|
||||
def test_cover_mode_taller_box(self, test_image):
|
||||
"""测试 cover 模式(box 更高)"""
|
||||
result = apply_fit_mode(test_image, (100, 200), "cover")
|
||||
# 图片应该覆盖整个 box
|
||||
assert result.size == (100, 200)
|
||||
|
||||
def test_center_mode(self, test_image):
|
||||
"""测试 center 模式"""
|
||||
result = apply_fit_mode(test_image, (200, 150), "center")
|
||||
# center 模式保持原始尺寸
|
||||
assert result.size == (100, 100)
|
||||
|
||||
def test_invalid_mode(self, test_image):
|
||||
"""测试无效的 fit 模式"""
|
||||
with pytest.raises(ValueError, match="不支持的 fit 模式"):
|
||||
apply_fit_mode(test_image, (200, 150), "invalid")
|
||||
|
||||
|
||||
class TestCreateCanvas:
|
||||
"""测试画布创建函数"""
|
||||
|
||||
def test_create_canvas_with_white_background(self):
|
||||
"""测试创建白色背景画布"""
|
||||
canvas = create_canvas_with_background((200, 150), "#ffffff")
|
||||
assert canvas.size == (200, 150)
|
||||
assert canvas.mode == "RGB"
|
||||
# 检查中心像素是白色
|
||||
assert canvas.getpixel((100, 75)) == (255, 255, 255)
|
||||
|
||||
def test_create_canvas_with_colored_background(self):
|
||||
"""测试创建彩色背景画布"""
|
||||
canvas = create_canvas_with_background((200, 150), "#ff0000")
|
||||
assert canvas.size == (200, 150)
|
||||
# 检查中心像素是红色
|
||||
assert canvas.getpixel((100, 75)) == (255, 0, 0)
|
||||
|
||||
def test_create_canvas_with_short_hex(self):
|
||||
"""测试创建画布(短格式十六进制颜色)"""
|
||||
canvas = create_canvas_with_background((100, 100), "#f00")
|
||||
# 检查中心像素是红色
|
||||
assert canvas.getpixel((50, 50)) == (255, 0, 0)
|
||||
|
||||
def test_create_canvas_with_invalid_color(self):
|
||||
"""测试无效颜色格式"""
|
||||
with pytest.raises(ValueError, match="无效的颜色格式"):
|
||||
create_canvas_with_background((100, 100), "invalid")
|
||||
@@ -6,21 +6,18 @@ HTML 渲染器单元测试
|
||||
|
||||
import pytest
|
||||
from pathlib import Path
|
||||
from renderers.html_renderer import HtmlRenderer, DPI
|
||||
from renderers.html_renderer import HtmlRenderer
|
||||
from core.elements import TextElement, ImageElement, ShapeElement, TableElement
|
||||
|
||||
|
||||
class TestHtmlRenderer:
|
||||
"""HtmlRenderer 测试类"""
|
||||
|
||||
def test_renderer_has_dpi_constant(self):
|
||||
"""测试渲染器有 DPI 常量"""
|
||||
assert DPI == 96
|
||||
|
||||
def test_init_renderer(self):
|
||||
"""测试创建渲染器"""
|
||||
renderer = HtmlRenderer()
|
||||
assert renderer is not None
|
||||
assert renderer.dpi == 96 # 硬编码的 DPI
|
||||
|
||||
|
||||
class TestRenderText:
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
"""
|
||||
|
||||
import pytest
|
||||
from utils import hex_to_rgb, validate_color
|
||||
from utils import hex_to_rgb
|
||||
|
||||
|
||||
class TestHexToRgb:
|
||||
@@ -52,53 +52,3 @@ class TestHexToRgb:
|
||||
"""测试大小写混合"""
|
||||
assert hex_to_rgb("#AbCdEf") == (171, 205, 239)
|
||||
assert hex_to_rgb("#aBcDeF") == (171, 205, 239)
|
||||
|
||||
|
||||
class TestValidateColor:
|
||||
"""validate_color 函数测试类"""
|
||||
|
||||
def test_valid_full_hex_colors(self):
|
||||
"""测试有效的完整十六进制颜色"""
|
||||
assert validate_color("#ffffff") is True
|
||||
assert validate_color("#000000") is True
|
||||
assert validate_color("#4a90e2") is True
|
||||
assert validate_color("#FF0000") is True
|
||||
assert validate_color("#ABCDEF") is True
|
||||
|
||||
def test_valid_short_hex_colors(self):
|
||||
"""测试有效的短格式十六进制颜色"""
|
||||
assert validate_color("#fff") is True
|
||||
assert validate_color("#000") is True
|
||||
assert validate_color("#abc") is True
|
||||
assert validate_color("#ABC") is True
|
||||
|
||||
def test_without_hash_sign(self):
|
||||
"""测试没有 # 号"""
|
||||
assert validate_color("ffffff") is False
|
||||
assert validate_color("fff") is False
|
||||
|
||||
def test_invalid_length(self):
|
||||
"""测试无效长度"""
|
||||
assert validate_color("#ff") is False
|
||||
assert validate_color("#ffff") is False
|
||||
assert validate_color("#fffff") is False
|
||||
assert validate_color("#fffffff") is False
|
||||
|
||||
def test_invalid_characters(self):
|
||||
"""测试无效字符"""
|
||||
assert validate_color("#gggggg") is False
|
||||
assert validate_color("#xyz") is False
|
||||
assert validate_color("red") is False
|
||||
assert validate_color("rgb(255,0,0)") is False
|
||||
|
||||
def test_non_string_input(self):
|
||||
"""测试非字符串输入"""
|
||||
assert validate_color(123) is False
|
||||
assert validate_color(None) is False
|
||||
assert validate_color([]) is False
|
||||
assert validate_color({"color": "#fff"}) is False
|
||||
|
||||
def test_empty_string(self):
|
||||
"""测试空字符串"""
|
||||
assert validate_color("") is False
|
||||
assert validate_color("#") is False
|
||||
|
||||
@@ -1,136 +0,0 @@
|
||||
"""
|
||||
单元测试:图片配置验证器
|
||||
|
||||
测试 validators/image_config.py 中的验证函数。
|
||||
"""
|
||||
|
||||
import pytest
|
||||
from validators.image_config import (
|
||||
validate_fit_value,
|
||||
validate_background_color,
|
||||
validate_dpi_value,
|
||||
)
|
||||
|
||||
|
||||
class TestValidateFitValue:
|
||||
"""测试 fit 值验证"""
|
||||
|
||||
def test_valid_fit_values(self):
|
||||
"""测试有效的 fit 值"""
|
||||
assert validate_fit_value("stretch") == []
|
||||
assert validate_fit_value("contain") == []
|
||||
assert validate_fit_value("cover") == []
|
||||
assert validate_fit_value("center") == []
|
||||
|
||||
def test_none_fit_value(self):
|
||||
"""测试 None 值(默认)"""
|
||||
# None 是无效的,应该返回错误
|
||||
issues = validate_fit_value(None)
|
||||
assert len(issues) > 0
|
||||
|
||||
def test_invalid_fit_value(self):
|
||||
"""测试无效的 fit 值"""
|
||||
issues = validate_fit_value("invalid")
|
||||
assert len(issues) > 0
|
||||
assert issues[0].level == "ERROR"
|
||||
|
||||
issues = validate_fit_value("fill")
|
||||
assert len(issues) > 0
|
||||
|
||||
issues = validate_fit_value("scale")
|
||||
assert len(issues) > 0
|
||||
|
||||
def test_case_sensitive(self):
|
||||
"""测试大小写敏感"""
|
||||
issues = validate_fit_value("STRETCH")
|
||||
assert len(issues) > 0
|
||||
|
||||
issues = validate_fit_value("Contain")
|
||||
assert len(issues) > 0
|
||||
|
||||
|
||||
class TestValidateBackgroundColor:
|
||||
"""测试背景色验证"""
|
||||
|
||||
def test_valid_hex_colors(self):
|
||||
"""测试有效的十六进制颜色"""
|
||||
assert validate_background_color("#ffffff") == []
|
||||
assert validate_background_color("#000000") == []
|
||||
assert validate_background_color("#ff0000") == []
|
||||
assert validate_background_color("#4a90e2") == []
|
||||
|
||||
def test_valid_short_hex_colors(self):
|
||||
"""测试有效的短格式十六进制颜色"""
|
||||
assert validate_background_color("#fff") == []
|
||||
assert validate_background_color("#000") == []
|
||||
assert validate_background_color("#f00") == []
|
||||
|
||||
def test_none_background(self):
|
||||
"""测试 None 值(透明)"""
|
||||
# None 会导致 TypeError,应该返回错误
|
||||
issues = validate_background_color(None)
|
||||
assert len(issues) > 0
|
||||
|
||||
def test_invalid_colors(self):
|
||||
"""测试无效的颜色格式"""
|
||||
issues = validate_background_color("white")
|
||||
assert len(issues) > 0
|
||||
|
||||
issues = validate_background_color("rgb(255,255,255)")
|
||||
assert len(issues) > 0
|
||||
|
||||
issues = validate_background_color("#gggggg")
|
||||
assert len(issues) > 0
|
||||
|
||||
issues = validate_background_color("#ff")
|
||||
assert len(issues) > 0
|
||||
|
||||
issues = validate_background_color("ffffff")
|
||||
assert len(issues) > 0
|
||||
|
||||
|
||||
class TestValidateDpiValue:
|
||||
"""测试 DPI 值验证"""
|
||||
|
||||
def test_valid_dpi_values(self):
|
||||
"""测试有效的 DPI 值"""
|
||||
assert validate_dpi_value(72) == []
|
||||
assert validate_dpi_value(96) == []
|
||||
assert validate_dpi_value(150) == []
|
||||
assert validate_dpi_value(300) == []
|
||||
|
||||
def test_boundary_dpi_values(self):
|
||||
"""测试边界 DPI 值"""
|
||||
# 1 和 1200 超出建议范围,会返回 WARNING
|
||||
issues = validate_dpi_value(1)
|
||||
assert len(issues) > 0
|
||||
assert issues[0].level == "WARNING"
|
||||
|
||||
issues = validate_dpi_value(1200)
|
||||
assert len(issues) > 0
|
||||
assert issues[0].level == "WARNING"
|
||||
|
||||
def test_invalid_dpi_values(self):
|
||||
"""测试无效的 DPI 值"""
|
||||
# 0 和负数会返回 WARNING
|
||||
issues = validate_dpi_value(0)
|
||||
assert len(issues) > 0
|
||||
|
||||
issues = validate_dpi_value(-1)
|
||||
assert len(issues) > 0
|
||||
|
||||
issues = validate_dpi_value(1201)
|
||||
assert len(issues) > 0
|
||||
|
||||
issues = validate_dpi_value(2000)
|
||||
assert len(issues) > 0
|
||||
|
||||
def test_non_integer_dpi(self):
|
||||
"""测试非整数 DPI 值"""
|
||||
# 浮点数 DPI 可能被接受(取决于实现)
|
||||
# 字符串和 None 应该返回错误
|
||||
issues = validate_dpi_value("96")
|
||||
assert len(issues) > 0
|
||||
|
||||
issues = validate_dpi_value(None)
|
||||
assert len(issues) > 0
|
||||
Reference in New Issue
Block a user