# S02: Post-Processing + Output Formats (SVG, DXF, JSON)

**Goal:** Add post-processing (RDP simplification, island detection, open path repair, node counting) and all three output formats (SVG, DXF, JSON) plus the /engine/simplify endpoint.
**Demo:** After this: /engine/trace returns valid DXF and JSON output; /engine/simplify reduces node count on complex SVG

## Tasks
- [x] **T01: Post-processing: RDP simplification, island detection, open path repair** — 1. Create engine/pipeline/postprocess.py
2. Implement RDP path simplification:
   - Parse SVG path data into coordinate arrays
   - Apply Ramer-Douglas-Peucker algorithm with tunable epsilon
   - Return simplified path data
3. Implement island detection:
   - Identify interior counter paths (holes in letters, enclosed shapes)
   - Flag with metadata for proper DXF nesting
4. Implement open path detection:
   - Check if path start/end points match
   - Flag open paths in metadata
   - Optional auto-close with straight line segment
5. Implement node count reporting per path
6. Unit tests for each function
  - Estimate: 60min
  - Files: engine/pipeline/postprocess.py, engine/tests/test_postprocess.py
  - Verify: cd engine && python -m pytest tests/test_postprocess.py -v
- [x] **T02: Output format generators: DXF (AC1015+), JSON, SVG** — 1. Create engine/output/dxf.py
2. Generate AC1015+ DXF format:
   - Use ezdxf library (Python-native, well-maintained)
   - Convert SVG path data to DXF LWPOLYLINE/SPLINE entities
   - Handle bezier curves → polyline approximation
   - Proper island/counter-path nesting
3. Create engine/output/json_output.py
4. JSON format: array of path objects with bezier/polyline commands
5. Update engine/output/svg.py for clean SVG serialization
6. Test DXF output opens in Inkscape (if available) or validate structure
  - Estimate: 90min
  - Files: engine/output/__init__.py, engine/output/dxf.py, engine/output/json_output.py, engine/output/svg.py, engine/tests/test_output.py
  - Verify: cd engine && python -m pytest tests/test_output.py -v
- [x] **T03: Wire post-processing into /engine/trace, add output_format routing for svg/dxf/json, and create /engine/simplify endpoint with 35 passing integration tests** — 1. Integrate post-processing into /engine/trace pipeline
2. Add output_format parameter routing (svg/dxf/json)
3. Create /engine/simplify endpoint:
   - Accepts SVG file upload + epsilon + output_format
   - Parses SVG, applies RDP simplification, outputs result
   - Same response shape as /engine/trace
4. Update metadata to include all fields: format, path_count, node_count_total, open_paths, warnings, processing_ms
5. Integration tests for all format combinations
  - Estimate: 45min
  - Files: engine/api/routes.py, engine/tests/test_api.py
  - Verify: cd engine && python -m pytest tests/test_api.py -v
