处理入口 - 合并所有XML片段 Args: context: 处理上下文,需要包含: - canvas_width, canvas_height: 画布尺寸 - xml_fragments: List[XMLFragment] 或 - elements: List[ElementInfo](每个元素需要有xml_fragment) Returns: Proc
(self, context: ProcessingContext)
| 74 | super().__init__(config) |
| 75 | |
| 76 | def process(self, context: ProcessingContext) -> ProcessingResult: |
| 77 | """ |
| 78 | 处理入口 - 合并所有XML片段 |
| 79 | |
| 80 | Args: |
| 81 | context: 处理上下文,需要包含: |
| 82 | - canvas_width, canvas_height: 画布尺寸 |
| 83 | - xml_fragments: List[XMLFragment] 或 |
| 84 | - elements: List[ElementInfo](每个元素需要有xml_fragment) |
| 85 | |
| 86 | Returns: |
| 87 | ProcessingResult: 包含生成的XML路径 |
| 88 | """ |
| 89 | # 获取 upscale_factor,用于将画布尺寸缩放回原始尺寸 |
| 90 | upscale_factor = 1.0 |
| 91 | if hasattr(context, 'intermediate_results') and context.intermediate_results: |
| 92 | upscale_factor = context.intermediate_results.get('upscale_factor', 1.0) |
| 93 | |
| 94 | # 计算原始画布尺寸 |
| 95 | canvas_width = context.canvas_width |
| 96 | canvas_height = context.canvas_height |
| 97 | if upscale_factor != 1.0: |
| 98 | canvas_width = int(context.canvas_width / upscale_factor) |
| 99 | canvas_height = int(context.canvas_height / upscale_factor) |
| 100 | self._log(f"画布尺寸缩放: {context.canvas_width}x{context.canvas_height} → {canvas_width}x{canvas_height}") |
| 101 | |
| 102 | # 收集所有XML片段 |
| 103 | fragments = self._collect_fragments(context) |
| 104 | |
| 105 | self._log(f"开始合并XML: 共{len(fragments)}个片段") |
| 106 | |
| 107 | if not fragments: |
| 108 | self._log("警告: 没有XML片段需要合并") |
| 109 | return ProcessingResult( |
| 110 | success=True, |
| 111 | canvas_width=canvas_width, |
| 112 | canvas_height=canvas_height, |
| 113 | metadata={'output_path': None, 'fragment_count': 0} |
| 114 | ) |
| 115 | |
| 116 | # 排序片段 |
| 117 | sorted_fragments = self._sort_fragments(fragments) |
| 118 | |
| 119 | # 构建XML结构(使用原始画布尺寸) |
| 120 | xml_root = self._build_xml_structure( |
| 121 | canvas_width, |
| 122 | canvas_height, |
| 123 | sorted_fragments |
| 124 | ) |
| 125 | |
| 126 | # 格式化并保存 |
| 127 | output_dir = context.output_dir or "./output" |
| 128 | os.makedirs(output_dir, exist_ok=True) |
| 129 | |
| 130 | stem = Path(context.image_path).stem if context.image_path else "merged" |
| 131 | output_path = os.path.join(output_dir, f"{stem}_merged.drawio.xml") |
| 132 | |
| 133 | xml_content = self._prettify_xml(xml_root) |
no test coverage detected