Skip to content

Commit 5d7e801

Browse files
committed
example unicode font
A review of the code is required. Since I'm not sure what's written correctly at all.
1 parent a5639bb commit 5d7e801

File tree

2 files changed

+183
-0
lines changed

2 files changed

+183
-0
lines changed
6.78 MB
Binary file not shown.
Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
#include <stdlib.h>
2+
#include <string.h>
3+
#include <raylib.h>
4+
5+
#define SCREEN_WIDTH 800
6+
#define SCREEN_HEIGHT 450
7+
8+
Font LoadUnicodeFont(const char* fileName, int fontSize, int textureFilter)
9+
{
10+
int* cp = NULL; // Array to store Unicode codepoints
11+
int capacity = 65536; // Initial capacity
12+
int count = 0; // Counter for codepoints
13+
14+
// Allocate initial array
15+
cp = (int*)malloc(capacity * sizeof(int));
16+
if (!cp) return GetFontDefault();
17+
18+
// Helper function to add a range of Unicode codepoints
19+
void AddRange(int start, int stop)
20+
{
21+
while (start <= stop)
22+
{
23+
// Expand array if needed
24+
if (count >= capacity)
25+
{
26+
capacity += 1024;
27+
int* newCp = (int*)realloc(cp, capacity * sizeof(int));
28+
if (!newCp)
29+
{
30+
free(cp);
31+
return GetFontDefault();
32+
}
33+
cp = newCp;
34+
}
35+
36+
// Add current codepoint and increment
37+
cp[count] = start;
38+
count++;
39+
start++;
40+
}
41+
}
42+
43+
// --------------------------------------------------
44+
// 1. BASIC ASCII CHARACTERS
45+
// --------------------------------------------------
46+
AddRange(32, 126); // Basic Latin (letters, digits, punctuation)
47+
48+
// --------------------------------------------------
49+
// 2. EUROPEAN LANGUAGES (LATIN SCRIPT)
50+
// --------------------------------------------------
51+
AddRange(0xC0, 0x17F); // Latin-1 Supplement + Latin Extended-A
52+
AddRange(0x180, 0x24F); // Latin Extended-B
53+
AddRange(0x1E00, 0x1EFF); // Latin Extended Additional
54+
AddRange(0x2C60, 0x2C7F); // Latin Extended-C
55+
56+
// --------------------------------------------------
57+
// 3. GREEK AND COPTIC
58+
// --------------------------------------------------
59+
AddRange(0x370, 0x3FF); // Greek and Coptic
60+
AddRange(0x1F00, 0x1FFF); // Greek Extended
61+
62+
// --------------------------------------------------
63+
// 4. CYRILLIC SCRIPTS
64+
// --------------------------------------------------
65+
AddRange(0x400, 0x4FF); // Basic Cyrillic
66+
AddRange(0x500, 0x52F); // Cyrillic Supplement
67+
AddRange(0x2DE0, 0x2DFF); // Cyrillic Extended-A
68+
AddRange(0xA640, 0xA69F); // Cyrillic Extended-B
69+
70+
// --------------------------------------------------
71+
// 5. CJK LANGUAGES (CHINESE, JAPANESE, KOREAN)
72+
// --------------------------------------------------
73+
AddRange(0x4E00, 0x9FFF); // CJK Unified Ideographs
74+
AddRange(0x3400, 0x4DBF); // CJK Extension A
75+
AddRange(0x3000, 0x303F); // CJK Symbols and Punctuation
76+
AddRange(0x3040, 0x309F); // Hiragana (Japanese)
77+
AddRange(0x30A0, 0x30FF); // Katakana (Japanese)
78+
AddRange(0x31F0, 0x31FF); // Katakana Phonetic Extensions
79+
AddRange(0xFF00, 0xFFEF); // Halfwidth and Fullwidth Forms
80+
AddRange(0xAC00, 0xD7AF); // Hangul Syllables (Korean)
81+
AddRange(0x1100, 0x11FF); // Hangul Jamo
82+
83+
// --------------------------------------------------
84+
// 6. SOUTHEAST ASIAN LANGUAGES
85+
// --------------------------------------------------
86+
AddRange(0x0E00, 0x0E7F); // Thai
87+
AddRange(0x0E80, 0x0EFF); // Lao
88+
AddRange(0x1780, 0x17FF); // Khmer
89+
AddRange(0x1000, 0x109F); // Myanmar
90+
AddRange(0x1980, 0x19DF); // New Tai Lue
91+
92+
// --------------------------------------------------
93+
// 7. INDIAN SUBCONTINENT LANGUAGES
94+
// --------------------------------------------------
95+
AddRange(0x900, 0x97F); // Devanagari (Hindi, Sanskrit)
96+
AddRange(0x980, 0x9FF); // Bengali
97+
AddRange(0xA00, 0xA7F); // Gurmukhi (Punjabi)
98+
AddRange(0xA80, 0xAFF); // Gujarati
99+
AddRange(0xB00, 0xB7F); // Oriya
100+
AddRange(0xB80, 0xBFF); // Tamil
101+
AddRange(0xC00, 0xC7F); // Telugu
102+
AddRange(0xC80, 0xCFF); // Kannada
103+
AddRange(0xD00, 0xD7F); // Malayalam
104+
AddRange(0xD80, 0xDFF); // Sinhala
105+
106+
// --------------------------------------------------
107+
// 8. MIDDLE EASTERN LANGUAGES
108+
// --------------------------------------------------
109+
AddRange(0x600, 0x6FF); // Arabic
110+
AddRange(0x750, 0x77F); // Arabic Supplement
111+
AddRange(0x8A0, 0x8FF); // Arabic Extended-A
112+
AddRange(0xFB50, 0xFDFF); // Arabic Presentation Forms-A
113+
AddRange(0x5D0, 0x5EA); // Hebrew
114+
AddRange(0x591, 0x5C7); // Hebrew Extended
115+
AddRange(0x7C0, 0x7FF); // N'Ko
116+
AddRange(0x640, 0x6FF); // Syriac
117+
118+
// --------------------------------------------------
119+
// 9. AFRICAN LANGUAGES
120+
// --------------------------------------------------
121+
AddRange(0x2C80, 0x2CFF); // Coptic
122+
AddRange(0x2D30, 0x2D7F); // Tifinagh
123+
AddRange(0xA6A0, 0xA6FF); // Bamum
124+
AddRange(0xAB00, 0xAB2F); // Ethiopic Extended
125+
126+
// --------------------------------------------------
127+
// 10. SPECIAL CHARACTERS AND SYMBOLS
128+
// --------------------------------------------------
129+
AddRange(0x300, 0x36F); // Combining Diacritical Marks
130+
AddRange(0x1DC0, 0x1DFF); // Combining Diacritical Marks Supplement
131+
AddRange(0x2000, 0x206F); // General Punctuation
132+
AddRange(0x20A0, 0x20CF); // Currency Symbols
133+
AddRange(0x2100, 0x214F); // Letterlike Symbols
134+
AddRange(0x2190, 0x21FF); // Arrows
135+
AddRange(0x2200, 0x22FF); // Mathematical Operators
136+
137+
138+
Font result = {0};
139+
140+
if (FileExists(fileName))
141+
result = LoadFontEx(fileName, fontSize, cp, count);
142+
143+
if (result.texture.id == 0)
144+
result = GetFontDefault();
145+
146+
SetTextureFilter(result.texture, textureFilter);
147+
free(cp);
148+
return result;
149+
}
150+
151+
int main(void)
152+
{
153+
// Initialization
154+
InitWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "Unicode Font Example");
155+
SetTargetFPS(60);
156+
157+
// Load font with Unicode support
158+
Font myFont = LoadUnicodeFont("resources/NotoSansTC-Regular.ttf", 36, TEXTURE_FILTER_BILINEAR);
159+
160+
// Main game loop
161+
while (!WindowShouldClose())
162+
{
163+
BeginDrawing();
164+
ClearBackground(RAYWHITE);
165+
166+
// Draw text
167+
DrawTextEx(myFont, "English: Hello World!", (Vector2){50, 50}, 36, 1, DARKGRAY);
168+
DrawTextEx(myFont, "Русский: Привет мир!", (Vector2){50, 100}, 36, 0, DARKGRAY);
169+
DrawTextEx(myFont, "中文: 你好世界!", (Vector2){50, 150}, 36, 1, DARKGRAY);
170+
DrawTextEx(myFont, "日本語: こんにちは世界!", (Vector2){50, 200}, 36, 1, DARKGRAY);
171+
172+
173+
DrawText("Font: Noto Sans TC Thin. © 2014-2020 Adobe(http://www.adobe.com/). License: SIL Open Font License 1.1", 10, SCREEN_HEIGHT - 20, 10, GRAY);
174+
175+
EndDrawing();
176+
}
177+
178+
// Cleanup
179+
UnloadFont(myFont);
180+
CloseWindow();
181+
182+
return 0;
183+
}

0 commit comments

Comments
 (0)