1 Huazhong University of Science and Technology, China
2 Peng Cheng National Laboratory, China
The official code repository for "Few-Shot Font Generation via Attribute-Guided Diffusion with Style Contrastive Learning" in PyTorch.
[08/2025]🎉 Our paper has been accepted by PRCV 2025.
Font generation remains a challenging task due to the complexity of character structures and the demand for style consistency across large glyph sets. Existing methods based on generative adversarial networks often suffer from training instability and mode collapse, limiting their scalability to high-quality few-shot font generation. To address these limitations, we propose a novel diffusion-based framework with attribute-guided generation and style contrastive learning. Specifically, we decouple content and style representations through pre-trained encoders and integrate them as conditional inputs into a diffusion model. This design enables stable training and generalization to both unseen content and unseen styles. We propose a multi-reference style fusion strategy that inputs more style images into the encoder and averages their latent codes to reduces style encoding variance. To enhance style fidelity, we introduce a style contrastive loss that explicitly distinguishes target styles from negative samples, ensuring robust style encoding even with limited references. Extensive experiments on a dataset of 400 fonts demonstrate that our method outperforms state-of-the-art approaches by achieving superior quantitative metrics and qualitative realism. Additionally, our model supports cross-lingual generation, paving the way for broader applications in multilingual typography.
Clone this repo:
git clone https://github.com/HAIV-Lab/AGDFont.gitCreate a conda environment and activate it.
conda create -n agdfont python=3.9 -y
conda activate agdfontInstall the required packages.
pip install -r requirements.txtThe training data files tree should be organized as follows:
├──data_examples
│ └── ContentImage
│ ├── 0000.png
│ ├── 0001.png
│ └── ...
│ └── train
│ ├── font0
│ │ ├──0000.png
│ │ ├──0001.png
│ │ └── ...
│ ├── font1
│ │ ├──0000.png
│ │ ├──0001.png
│ │ └── ...
│ └── test
│ ├── font0
│ │ ├──0000.png
│ │ ├──0001.png
│ │ └── ...
│ ├── font1
│ │ ├──0000.png
│ │ ├──0001.png
│ │ └── ...
python image_train.py --model_save_dir {your_model_dir} --con_encoder_path {con_encoder_dir} --sty_encoder_path {sty_encoder_dir} --data_dir {data_examples/train} --con_path {data_examples/ContentImage}python image_sample.py --model_path {your_model_path} --style_path {your_testfont_dir} --content_path {data_examples/ContentImage} --total_txt {your_total_character_json} --gen_txt {your_gen_character_txt} --img_save_path {your_save_dir}