Skip to content

feat: Add JSON-based font configuration system#114

Open
NekoEpisode wants to merge 4 commits intoaehmttw:masterfrom
NekoEpisode:Optimize-the-font-system
Open

feat: Add JSON-based font configuration system#114
NekoEpisode wants to merge 4 commits intoaehmttw:masterfrom
NekoEpisode:Optimize-the-font-system

Conversation

@NekoEpisode
Copy link
Contributor

Summary

Implemented a flexible font configuration system for easy multi-language support through external JSON config. Fonts are now loaded from fonts/config.json instead of being hardcoded.

Key Features

  • Declarative config: Add new fonts by editing JSON, no code changes needed
  • Multi-language support: Easy to add Chinese, Japanese, Korean, etc.
  • Flexible sizing: Supports both fixed-width and variable-width fonts
  • ID-based lookup: Access fonts by ID for special rendering needs
  • Auto-fallback: Missing characters automatically use default font

Configuration Format

{
  "font_files": {
    "default": {
      "path": "/fonts/default/font.png",
      "chars": ["ABC", "123", "..."],
      "char_size_type": "array",
      "char_size_array": [3, 2, 4, ...],
      "h_space": 2
    },
    "zh_cn_1": {
      "path": "/fonts/zh_cn/font.png",
      "chars": ["汉字", "..."],
      "char_size_type": "fixed",
      "char_size": 8,                      // Use 8 for CJK fonts
      "h_space": 2
    }
  }
}

⚠️ CRITICAL: Path Requirements

Font paths MUST begin with "/" or the game will not load the font correctly!

Correct: "path": "/fonts/default/font.png"
Wrong:   "path": "fonts/default/font.png"

Character Size Guidelines

  • Latin/proportional fonts: Use "char_size_type": "array" with individual widths
  • CJK fonts: Use "char_size_type": "fixed" with "char_size": 8

Changes

  • Added FontRenderer.setDefaultFont() for delayed initialization
  • Added fontById HashMap for O(1) font lookup by ID
  • Added loadFonts() method in LWJGLWindow to parse JSON config
  • Removed hardcoded font initialization from constructor
  • All fonts now loaded at startup with comprehensive error handling

Usage

// Fonts auto-load from config at startup
fontRenderer.drawString(x, y, scale, scale, "Hello 世界");

// Check if font exists
if (fontRenderer.hasFontId("japanese")) { ... }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant