Skip to content

Commit 9435873

Browse files
committed
Added font glyph ranges option
1 parent dfbaef0 commit 9435873

File tree

3 files changed

+89
-10
lines changed

3 files changed

+89
-10
lines changed

example/example.script

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -112,23 +112,23 @@ function init(self)
112112
local fontsize = 12.0 * scale
113113
local fontsizebase = 12.0 * scale
114114
self.fonts = {}
115-
local regular_data, error = sys.load_resource("/example/bundle/fonts/Montserrat-Regular.ttf")
116-
self.fonts["Regular"] = imgui.font_add_ttf_data(regular_data, #regular_data, fontsize, fontsizebase)
117-
local bold_data, error = sys.load_resource("/example/bundle/fonts/Montserrat-Bold.ttf")
115+
local regular_data = assert(sys.load_resource("/example/bundle/fonts/Montserrat-Regular.ttf"))
116+
self.fonts["Regular"] = imgui.font_add_ttf_data(regular_data, #regular_data, fontsize, fontsizebase, imgui.GLYPH_RANGES_CYRILLIC)
117+
local bold_data = assert(sys.load_resource("/example/bundle/fonts/Montserrat-Bold.ttf"))
118118
self.fonts["Bold"] = imgui.font_add_ttf_data(bold_data, #bold_data, fontsize, fontsizebase)
119-
local italic_data, error = sys.load_resource("/example/bundle/fonts/Montserrat-Italic.ttf")
119+
local italic_data = assert(sys.load_resource("/example/bundle/fonts/Montserrat-Italic.ttf"))
120120
self.fonts["Italic"] = imgui.font_add_ttf_data(italic_data, #italic_data, fontsize, fontsizebase)
121-
local bolditalic_data, error = sys.load_resource("/example/bundle/fonts/Montserrat-BoldItalic.ttf")
121+
local bolditalic_data = assert(sys.load_resource("/example/bundle/fonts/Montserrat-BoldItalic.ttf"))
122122
self.fonts["BoldItalic"] = imgui.font_add_ttf_data(bolditalic_data, #bolditalic_data, fontsize, fontsizebase)
123123

124124
-- Resource based Image
125-
local img1_data, error = sys.load_resource("/example/bundle/images/image1.png")
125+
local img1_data = assert(sys.load_resource("/example/bundle/images/image1.png"))
126126
self.image1 = imgui.image_load_data("image1", img1_data, #img1_data)
127127

128128
-- File based Image can be used on desktop platforms
129129
-- self.image1 = imgui.image_load("example/images/image1.png")
130130

131-
local img2_data, error = sys.load_resource("/example/bundle/images/image2.png")
131+
local img2_data = assert(sys.load_resource("/example/bundle/images/image2.png"))
132132
self.image2 = imgui.image_load_data("image2", img2_data, #img2_data)
133133
end
134134

@@ -345,7 +345,7 @@ end
345345
local function update_tab4(self)
346346

347347
imgui.font_push(self.fonts["Regular"])
348-
imgui.text_colored("Example Text Regular", 1, 1, 1, 1 )
348+
imgui.text_colored("Example Text Regular - Привіт Світ", 1, 1, 1, 1 )
349349
imgui.font_pop()
350350
imgui.separator()
351351
imgui.font_push(self.fonts["Bold"])

imgui/api/imgui.script_api

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,8 @@
173173
type: string
174174
- name: fontsize
175175
type: number
176+
- name: glyphranges
177+
type: number
176178

177179
return:
178180
- name: fontid
@@ -192,6 +194,8 @@
192194
type: number
193195
- name: fontpixels
194196
type: number
197+
- name: glyphranges
198+
type: number
195199

196200
return:
197201
- name: fontid
@@ -1958,3 +1962,13 @@
19581962
- name: KEY_X
19591963
- name: KEY_Y
19601964
- name: KEY_Z
1965+
1966+
- name: GLYPH_RANGES_DEFAULT
1967+
- name: GLYPH_RANGES_GREEK
1968+
- name: GLYPH_RANGES_KOREAN
1969+
- name: GLYPH_RANGES_JAPANESE
1970+
- name: GLYPH_RANGES_CHINESEFULL
1971+
- name: GLYPH_RANGES_CHINESESIMPLIFIEDCOMMON
1972+
- name: GLYPH_RANGES_CYRILLIC
1973+
- name: GLYPH_RANGES_THAI
1974+
- name: GLYPH_RANGES_VIETNAMESE

imgui/src/extension_imgui.cpp

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,17 @@ typedef struct ImgObject
3737
unsigned char * data;
3838
} ImgObject;
3939

40+
enum ExtImGuiGlyphRanges {
41+
ExtImGuiGlyphRanges_Default,
42+
ExtImGuiGlyphRanges_Greek,
43+
ExtImGuiGlyphRanges_Korean,
44+
ExtImGuiGlyphRanges_Japanese,
45+
ExtImGuiGlyphRanges_ChineseFull,
46+
ExtImGuiGlyphRanges_ChineseSimplifiedCommon,
47+
ExtImGuiGlyphRanges_Cyrillic,
48+
ExtImGuiGlyphRanges_Thai,
49+
ExtImGuiGlyphRanges_Vietnamese
50+
};
4051

4152
static bool g_imgui_NewFrame = false;
4253
static char* g_imgui_TextBuffer = 0;
@@ -2242,14 +2253,55 @@ static ImFont* imgui_GetFont(int index)
22422253
return 0;
22432254
}
22442255

2256+
ImWchar* LuaToGlyphRanges(lua_State * L, int index) {
2257+
const ImWchar* glyph_ranges = NULL;
2258+
if (!lua_isnil(L, index)) {
2259+
ImGuiIO& io = ImGui::GetIO();
2260+
ExtImGuiGlyphRanges range = (ExtImGuiGlyphRanges)lua_tointeger(L, index);
2261+
switch (range) {
2262+
case ExtImGuiGlyphRanges_Greek:
2263+
glyph_ranges = io.Fonts->GetGlyphRangesGreek();
2264+
break;
2265+
case ExtImGuiGlyphRanges_Korean:
2266+
glyph_ranges = io.Fonts->GetGlyphRangesKorean();
2267+
break;
2268+
case ExtImGuiGlyphRanges_Japanese:
2269+
glyph_ranges = io.Fonts->GetGlyphRangesJapanese();
2270+
break;
2271+
case ExtImGuiGlyphRanges_ChineseFull:
2272+
glyph_ranges = io.Fonts->GetGlyphRangesChineseFull();
2273+
break;
2274+
case ExtImGuiGlyphRanges_ChineseSimplifiedCommon:
2275+
glyph_ranges = io.Fonts->GetGlyphRangesChineseSimplifiedCommon();
2276+
break;
2277+
case ExtImGuiGlyphRanges_Cyrillic:
2278+
glyph_ranges = io.Fonts->GetGlyphRangesCyrillic();
2279+
break;
2280+
case ExtImGuiGlyphRanges_Thai:
2281+
glyph_ranges = io.Fonts->GetGlyphRangesThai();
2282+
break;
2283+
case ExtImGuiGlyphRanges_Vietnamese:
2284+
glyph_ranges = io.Fonts->GetGlyphRangesVietnamese();
2285+
break;
2286+
default:
2287+
case ExtImGuiGlyphRanges_Default:
2288+
glyph_ranges = io.Fonts->GetGlyphRangesDefault();
2289+
break;
2290+
}
2291+
}
2292+
return (ImWchar*)glyph_ranges;
2293+
}
2294+
22452295
static int imgui_FontAddTTFFile(lua_State * L)
22462296
{
22472297
DM_LUA_STACK_CHECK(L, 1);
22482298
const char * ttf_filename = luaL_checkstring(L, 1);
22492299
float font_size = luaL_checknumber(L, 2);
2300+
const ImFontConfig* font_cfg = NULL;
2301+
const ImWchar* glyph_ranges = LuaToGlyphRanges(L, 3);
22502302

22512303
ImGuiIO& io = ImGui::GetIO();
2252-
ImFont* font = io.Fonts->AddFontFromFileTTF(ttf_filename, font_size);
2304+
ImFont* font = io.Fonts->AddFontFromFileTTF(ttf_filename, font_size, font_cfg, glyph_ranges);
22532305
// Put font in map.
22542306
if(font != NULL)
22552307
{
@@ -2270,12 +2322,14 @@ static int imgui_FontAddTTFData(lua_State * L)
22702322
int ttf_data_size = luaL_checknumber(L, 2);
22712323
float font_size = luaL_checknumber(L, 3);
22722324
int font_pixels = luaL_checknumber(L, 4);
2325+
const ImFontConfig* font_cfg = NULL;
2326+
const ImWchar* glyph_ranges = LuaToGlyphRanges(L, 5);
22732327

22742328
char *ttf_data_cpy = (char *)calloc(ttf_data_size, sizeof(char));
22752329
memcpy(ttf_data_cpy, ttf_data, ttf_data_size);
22762330

22772331
ImGuiIO& io = ImGui::GetIO();
2278-
ImFont* font = io.Fonts->AddFontFromMemoryTTF((void *)ttf_data_cpy, ttf_data_size, font_pixels);
2332+
ImFont* font = io.Fonts->AddFontFromMemoryTTF((void *)ttf_data_cpy, ttf_data_size, font_pixels, font_cfg, glyph_ranges);
22792333
// Put font in map.
22802334
if(font != NULL)
22812335
{
@@ -2670,6 +2724,7 @@ static const luaL_reg Module_methods[] =
26702724
{"set_key_modifier_alt", imgui_SetKeyModifierAlt},
26712725
{"set_key_modifier_super", imgui_SetKeyModifierSuper},
26722726
{"add_input_character", imgui_AddInputCharacter},
2727+
{"add_input_characters", imgui_AddInputCharacters},
26732728
{"want_mouse_input", imgui_WantCaptureMouse},
26742729
{"want_keyboard_input", imgui_WantCaptureKeyboard},
26752730
{"want_text_input", imgui_WantCaptureText},
@@ -2988,6 +3043,16 @@ static void LuaInit(lua_State* L)
29883043
lua_setfieldstringint(L, "DIR_UP", ImGuiDir_Up);
29893044
lua_setfieldstringint(L, "DIR_DOWN", ImGuiDir_Down);
29903045

3046+
lua_setfieldstringint(L, "GLYPH_RANGES_DEFAULT", ExtImGuiGlyphRanges_Default); // Basic Latin, Extended Latin
3047+
lua_setfieldstringint(L, "GLYPH_RANGES_GREEK", ExtImGuiGlyphRanges_Greek); // Default + Greek and Coptic
3048+
lua_setfieldstringint(L, "GLYPH_RANGES_KOREAN", ExtImGuiGlyphRanges_Korean); // Default + Korean characters
3049+
lua_setfieldstringint(L, "GLYPH_RANGES_JAPANESE", ExtImGuiGlyphRanges_Japanese); // Default + Hiragana, Katakana, Half-Width, Selection of 2999 Ideographs
3050+
lua_setfieldstringint(L, "GLYPH_RANGES_CHINESEFULL", ExtImGuiGlyphRanges_ChineseFull); // Default + Half-Width + Japanese Hiragana/Katakana + full set of about 21000 CJK Unified Ideographs
3051+
lua_setfieldstringint(L, "GLYPH_RANGES_CHINESESIMPLIFIEDCOMMON", ExtImGuiGlyphRanges_ChineseSimplifiedCommon);// Default + Half-Width + Japanese Hiragana/Katakana + set of 2500 CJK Unified Ideographs for common simplified Chinese
3052+
lua_setfieldstringint(L, "GLYPH_RANGES_CYRILLIC", ExtImGuiGlyphRanges_Cyrillic); // Default + about 400 Cyrillic characters
3053+
lua_setfieldstringint(L, "GLYPH_RANGES_THAI", ExtImGuiGlyphRanges_Thai); // Default + Thai characters
3054+
lua_setfieldstringint(L, "GLYPH_RANGES_VIETNAMESE", ExtImGuiGlyphRanges_Vietnamese); // Default + Vietnamese characters
3055+
29913056
lua_pop(L, 1);
29923057
assert(top == lua_gettop(L));
29933058
}

0 commit comments

Comments
 (0)