# font-king/font-pipeline architecture

> Architecture diagram of the GitHub repository font-king/font-pipeline, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-07). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/font-king/font-pipeline
- Repository: https://github.com/font-king/font-pipeline

## Overview

##### Main Components
- API Server (FastAPI): This is the central controller for receiving font generation requests via a REST API. It ties together the processing steps and manages logging and message queuing.
- Image Crop Module (crop): Responsible for extracting individual glyphs from the user’s handwritten image.
- Inference Module (inference): Uses deep learning models to generate or refine glyphs. This module contains deep learning networks (e.g., generator, discriminator, decoders) and related training/inference logic.
- JPG-to-SVG Converter (jpg2svg): Converts bitmap images to vector (SVG) format, handling the image conversion part.
- SVG-to-TTF Converter (svg2ttf): Transforms SVG glyphs into scalable font files (TTF/WOFF) for final output.
- Template Generator (make_template): Prepares a reference template (using reference characters) that can be used across the pipeline.
- AWS SQS and Environment Configuration: The API server leverages AWS SQS for handling asynchronous messaging/triggers. Include environment variables and AWS integrations.
- Client & Testing Interfaces: User inputs are provided (stored in a directory like “written”), and testing is enabled via a dedicated script (test_request.py) or direct curl calls.

##### Relationships and Data Flow
- User Interaction: The client calls the API (using REST endpoints or by sending messages to the SQS queue configured with AWS credentials stored in .env).
- API Server: Receives the request and orchestrates the following sequential pipeline steps.
- Pipeline Execution:
a. Crop: The API triggers the crop module (using crop/cropper.Dockerfile and glyph_cropper.py) to extract original glyph images.
b. Inference: The cropped images then go to the inference module, where a deep learning model (organized under inference/resources/DM and inference/resources/base) processes the images.
c. JPG2SVG: Once inference produces images, the jpg2svg converter is executed (via its Dockerfile and jpg_to_svg_converter.py) to transform JPG images to SVG format.
d. SVG2TTF: Finally, the vector images are converted to TTF/WOFF by the svg2ttf converter.
- Scripts: Execution scripts (found in the scripts directory) coordinate the sequence (from template generation to running the conversion commands).
- Logging and Result Storage: Each stage logs its activities (written to the ./log directory) and final outputs are saved into structured output directories under ./result.

##### Architectural Patterns and Design Principles
- Modular Pipeline Approach: The pipeline is clearly divided into discrete phases, each with its own dedicated module. This encourages separation of concerns.
- Containerization: Several modules use Docker (crop, inference, jpg2svg, make_template, svg2ttf) to isolate environments, ensuring reproducible builds.
- RESTful API Design: FastAPI is used to expose functionality via well-defined HTTP endpoints (with Swagger UI documentation).
- Asynchronous Processing: Integration with AWS SQS enables asynchronous handling of font generation requests and possibly supports scaling when handling bulk requests.
- Script-Based Orchestration: Shell scripts in the scripts directory support sequential processing steps, making it clear how the stages interrelate.
- Logging and Configuration Management: Each component uses logging (with detailed logs in the log folder) and configuration files (e.g., YAML config files for inference models and .env for AWS configuration) to ensure traceability and ease of debugging.

##### Additional Detail
- Mention integration points: how the API server parses the HTTP requests, triggers pipeline orchestrator scripts, and communicates with AWS SQS.
- That component interdependencies are explicit; for instance, the inference module depends on custom deep learning model modules, and each transformation stage outputs to designated directories which are later consumed by subsequent stages.

## Diagram (Mermaid)

```mermaid
graph TD
    A["Client & Testing Interface"]:::client
    B["API Server (FastAPI)"]:::apiserver
    C["AWS SQS & Environment"]:::aws
    D["Scripts Orchestrator"]:::script
    E["Template Generator"]:::module
    F["Glyph Cropper"]:::module
    G["Deep Learning Inference"]:::module
    H["JPG-to-SVG Converter"]:::module
    I["SVG-to-TTF Converter"]:::module
    J["Result / Font Files"]:::output
    K["Logging (./log)"]:::logging

    A -->|"Request"| B
    B -->|"SendSQS"| C
    B -->|"Orchestrate"| D
    D -->|"InitTemplate"| E
    E -->|"GenerateTemplate"| F
    F -->|"CropImages"| G
    G -->|"InferGlyphs"| H
    H -->|"ConvertToSVG"| I
    I -->|"GenerateFont"| J

    %% Logging connections
    B -->|"Log"| K
    F -->|"Log"| K
    G -->|"Log"| K
    H -->|"Log"| K
    I -->|"Log"| K

    %% Styles
    classDef client fill:#ffcccc,stroke:#990000,stroke-width:2px;
    classDef apiserver fill:#cceeff,stroke:#003366,stroke-width:2px;
    classDef aws fill:#d5f5e3,stroke:#1d8348,stroke-width:2px;
    classDef script fill:#fcf3cf,stroke:#b7950b,stroke-width:2px;
    classDef module fill:#e8daef,stroke:#6c3483,stroke-width:2px;
    classDef output fill:#d6eaf8,stroke:#1b4f72,stroke-width:2px;
    classDef logging fill:#f6ddcc,stroke:#af601a,stroke-width:2px;

    %% Click Events for Component Mappings
    click B "https://github.com/font-king/font-pipeline/blob/main/fastAPI/main.py"
    click F "https://github.com/font-king/font-pipeline/blob/main/crop/glyph_cropper.py"
    click F "https://github.com/font-king/font-pipeline/blob/main/crop/cropper.Dockerfile"
    click G "https://github.com/font-king/font-pipeline/tree/main/inference/"
    click H "https://github.com/font-king/font-pipeline/blob/main/jpg2svg/jpg_to_svg_converter.py"
    click H "https://github.com/font-king/font-pipeline/tree/main/jpg2svg/Dockerfile"
    click I "https://github.com/font-king/font-pipeline/blob/main/svg2ttf/svg_to_ttf_converter.py"
    click I "https://github.com/font-king/font-pipeline/tree/main/svg2ttf/Dockerfile"
    click E "https://github.com/font-king/font-pipeline/blob/main/make_template/template_generator.py"
    click E "https://github.com/font-king/font-pipeline/blob/main/make_template/korean_reference_chars.py"
    click E "https://github.com/font-king/font-pipeline/tree/main/make_template/"
    click C "https://github.com/font-king/font-pipeline/blob/main/fastAPI/sqs_utils.py"
    click C "https://github.com/font-king/font-pipeline/blob/main/fastAPI/config.py"
    click A "https://github.com/font-king/font-pipeline/blob/main/test_request.py"
    click A "https://github.com/font-king/font-pipeline/blob/main/fastAPI/test_api.py"
```
