feat: 添加图片适配模式支持
- 支持四种图片适配模式:stretch、contain、cover、center - 支持背景色填充功能(contain 和 center 模式) - 支持文档级 DPI 配置(metadata.dpi) - PPTX 渲染器集成 Pillow 实现高质量图片处理 - HTML 渲染器使用 CSS object-fit 实现相同效果 - 添加完整的单元测试、集成测试和端到端测试 - 更新 README 文档和架构文档 - 模块化设计:utils/image_utils.py 图片处理工具模块 - 添加图片配置验证器:validators/image_config.py - 向后兼容:未指定 fit 时默认使用 stretch 模式
This commit is contained in:
101
validators/image_config.py
Normal file
101
validators/image_config.py
Normal file
@@ -0,0 +1,101 @@
|
||||
"""
|
||||
图片配置验证器
|
||||
|
||||
验证图片元素的 fit、background、dpi 等参数。
|
||||
"""
|
||||
|
||||
from typing import List
|
||||
from validators.result import ValidationIssue
|
||||
import re
|
||||
|
||||
|
||||
def validate_fit_value(fit: str) -> List[ValidationIssue]:
|
||||
"""
|
||||
验证 fit 参数值
|
||||
|
||||
Args:
|
||||
fit: fit 参数值
|
||||
|
||||
Returns:
|
||||
验证问题列表
|
||||
"""
|
||||
issues = []
|
||||
valid_fits = ['stretch', 'contain', 'cover', 'center']
|
||||
|
||||
if fit not in valid_fits:
|
||||
issues.append(ValidationIssue(
|
||||
level="ERROR",
|
||||
message=f"无效的 fit 值: {fit} (支持: {', '.join(valid_fits)})",
|
||||
location="",
|
||||
code="INVALID_FIT_VALUE"
|
||||
))
|
||||
|
||||
return issues
|
||||
|
||||
|
||||
def validate_background_color(color: str) -> List[ValidationIssue]:
|
||||
"""
|
||||
验证背景颜色格式
|
||||
|
||||
Args:
|
||||
color: 颜色字符串
|
||||
|
||||
Returns:
|
||||
验证问题列表
|
||||
"""
|
||||
issues = []
|
||||
|
||||
# 检查类型
|
||||
if not isinstance(color, str):
|
||||
issues.append(ValidationIssue(
|
||||
level="ERROR",
|
||||
message=f"无效的背景颜色格式: {color} (应为 #RRGGBB 或 #RGB)",
|
||||
location="",
|
||||
code="INVALID_COLOR_FORMAT"
|
||||
))
|
||||
return issues
|
||||
|
||||
pattern = r'^#([0-9A-Fa-f]{3}|[0-9A-Fa-f]{6})$'
|
||||
|
||||
if not re.match(pattern, color):
|
||||
issues.append(ValidationIssue(
|
||||
level="ERROR",
|
||||
message=f"无效的背景颜色格式: {color} (应为 #RRGGBB 或 #RGB)",
|
||||
location="",
|
||||
code="INVALID_COLOR_FORMAT"
|
||||
))
|
||||
|
||||
return issues
|
||||
|
||||
|
||||
def validate_dpi_value(dpi: int) -> List[ValidationIssue]:
|
||||
"""
|
||||
验证 DPI 值
|
||||
|
||||
Args:
|
||||
dpi: DPI 值
|
||||
|
||||
Returns:
|
||||
验证问题列表
|
||||
"""
|
||||
issues = []
|
||||
|
||||
# 检查类型
|
||||
if not isinstance(dpi, int):
|
||||
issues.append(ValidationIssue(
|
||||
level="ERROR",
|
||||
message=f"DPI 值必须是整数: {dpi}",
|
||||
location="",
|
||||
code="INVALID_DPI_TYPE"
|
||||
))
|
||||
return issues
|
||||
|
||||
if dpi < 72 or dpi > 300:
|
||||
issues.append(ValidationIssue(
|
||||
level="WARNING",
|
||||
message=f"DPI 值可能不合适: {dpi} (建议范围: 72-300)",
|
||||
location="",
|
||||
code="DPI_OUT_OF_RANGE"
|
||||
))
|
||||
|
||||
return issues
|
||||
@@ -9,6 +9,7 @@ from loaders.yaml_loader import load_yaml_file, validate_presentation_yaml, YAML
|
||||
from validators.result import ValidationResult, ValidationIssue
|
||||
from validators.geometry import GeometryValidator
|
||||
from validators.resource import ResourceValidator
|
||||
from validators.image_config import validate_dpi_value
|
||||
from core.elements import create_element
|
||||
|
||||
|
||||
@@ -70,6 +71,12 @@ class Validator:
|
||||
size_str = data.get("metadata", {}).get("size", "16:9")
|
||||
slide_width, slide_height = self.SLIDE_SIZES.get(size_str, (10, 5.625))
|
||||
|
||||
# 验证 DPI 配置
|
||||
dpi = data.get("metadata", {}).get("dpi")
|
||||
if dpi is not None:
|
||||
dpi_issues = validate_dpi_value(dpi)
|
||||
self._categorize_issues(dpi_issues, errors, warnings, infos)
|
||||
|
||||
# 初始化子验证器
|
||||
geometry_validator = GeometryValidator(slide_width, slide_height)
|
||||
resource_validator = ResourceValidator(
|
||||
|
||||
Reference in New Issue
Block a user