Skip to content

Commit 2910423

Browse files
authored
Merge pull request #4886 from GuvaCode/master
[examples] Possible improvements for `text_codepoints_loading`, adding ranges
2 parents 44207b2 + 53cbd3c commit 2910423

File tree

2 files changed

+127
-0
lines changed

2 files changed

+127
-0
lines changed
6.78 MB
Binary file not shown.

examples/text/text_unicode_font.c

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
#include <stdlib.h>
2+
#include <raylib.h>
3+
4+
#define SCREEN_WIDTH 800
5+
#define SCREEN_HEIGHT 450
6+
7+
typedef struct {
8+
int* data;
9+
int count;
10+
int capacity;
11+
} CodepointsArray;
12+
13+
static void AddRange(CodepointsArray* array, int start, int stop) {
14+
int rangeSize = stop - start + 1;
15+
16+
if (array->count + rangeSize > array->capacity) {
17+
array->capacity = array->count + rangeSize + 1024;
18+
array->data = (int*)MemRealloc(array->data, array->capacity * sizeof(int));
19+
if (!array->data) {
20+
TraceLog(LOG_ERROR, "FONTUTIL: Memory allocation failed");
21+
exit(1);
22+
}
23+
}
24+
25+
for (int i = start; i <= stop; i++) {
26+
array->data[array->count++] = i;
27+
}
28+
}
29+
30+
Font LoadUnicodeFont(const char* fileName, int fontSize, int textureFilter) {
31+
CodepointsArray cp = {0};
32+
cp.capacity = 2048;
33+
cp.data = (int*)MemAlloc(cp.capacity * sizeof(int));
34+
35+
if (!cp.data) {
36+
TraceLog(LOG_ERROR, "FONTUTIL: Initial allocation failed");
37+
return GetFontDefault();
38+
}
39+
40+
// Basic ASCII
41+
AddRange(&cp, 32, 126);
42+
43+
// European Languages
44+
AddRange(&cp, 0xC0, 0x17F);
45+
AddRange(&cp, 0x180, 0x24F);
46+
AddRange(&cp, 0x1E00, 0x1EFF);
47+
AddRange(&cp, 0x2C60, 0x2C7F);
48+
49+
// Greek
50+
AddRange(&cp, 0x370, 0x3FF);
51+
AddRange(&cp, 0x1F00, 0x1FFF);
52+
53+
// Cyrillic
54+
AddRange(&cp, 0x400, 0x4FF);
55+
AddRange(&cp, 0x500, 0x52F);
56+
AddRange(&cp, 0x2DE0, 0x2DFF);
57+
AddRange(&cp, 0xA640, 0xA69F);
58+
59+
// CJK
60+
AddRange(&cp, 0x4E00, 0x9FFF);
61+
AddRange(&cp, 0x3400, 0x4DBF);
62+
AddRange(&cp, 0x3000, 0x303F);
63+
AddRange(&cp, 0x3040, 0x309F);
64+
AddRange(&cp, 0x30A0, 0x30FF);
65+
AddRange(&cp, 0x31F0, 0x31FF);
66+
AddRange(&cp, 0xFF00, 0xFFEF);
67+
AddRange(&cp, 0xAC00, 0xD7AF);
68+
AddRange(&cp, 0x1100, 0x11FF);
69+
70+
// Other
71+
AddRange(&cp, 0x900, 0x97F); // Devanagari
72+
AddRange(&cp, 0x600, 0x6FF); // Arabic
73+
AddRange(&cp, 0x5D0, 0x5EA); // Hebrew
74+
75+
Font font = {0};
76+
77+
if (FileExists(fileName)) {
78+
font = LoadFontEx(fileName, fontSize, cp.data, cp.count);
79+
}
80+
81+
if (font.texture.id == 0) {
82+
font = GetFontDefault();
83+
TraceLog(LOG_WARNING, "FONTUTIL: Using default font");
84+
}
85+
86+
SetTextureFilter(font.texture, textureFilter);
87+
MemFree(cp.data);
88+
89+
return font;
90+
}
91+
92+
/**
93+
* Main entry point
94+
*/
95+
int main(void)
96+
{
97+
// Initialize window
98+
InitWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "Unicode Font Example");
99+
SetTargetFPS(60);
100+
101+
// Load font with Unicode support
102+
Font myFont = LoadUnicodeFont("resources/NotoSansTC-Regular.ttf", 36, TEXTURE_FILTER_BILINEAR);
103+
104+
// Main render loop
105+
while (!WindowShouldClose())
106+
{
107+
BeginDrawing();
108+
ClearBackground(RAYWHITE);
109+
110+
// Render test strings in different languages
111+
DrawTextEx(myFont, "English: Hello World!", (Vector2){50, 50}, 36, 1, DARKGRAY);
112+
DrawTextEx(myFont, "Русский: Привет мир!", (Vector2){50, 100}, 36, 0, DARKGRAY);
113+
DrawTextEx(myFont, "中文: 你好世界!", (Vector2){50, 150}, 36, 1, DARKGRAY);
114+
DrawTextEx(myFont, "日本語: こんにちは世界!", (Vector2){50, 200}, 36, 1, DARKGRAY);
115+
116+
// Display font attribution
117+
DrawText("Font: Noto Sans TC. License: SIL Open Font License 1.1",
118+
10, SCREEN_HEIGHT - 20, 10, GRAY);
119+
EndDrawing();
120+
}
121+
122+
// Cleanup resources
123+
UnloadFont(myFont);
124+
CloseWindow();
125+
126+
return 0;
127+
}

0 commit comments

Comments
 (0)