Skip to content
whkelvin edited this page Dec 23, 2017 · 33 revisions

One of Nuklear significant features is UTF-8 support. For use it you may complete several steps:

Step 1

Obtain font, which contain characters you may use.

Step 2

Create glyph range table for your language. As example for Japanese (thanks to vaiorabbit):

const nk_rune nk_font_japanese_glyph_ranges[] = {
    0x0020, 0x00FF,
    0x2200, 0x22FF, // Mathematical Operators
    0x3000, 0x303F, // CJK Symbols and Punctuation
    0x3040, 0x309F, // Hiragana
    0x30A0, 0x30FF, // Katakana
    0x0370, 0x03FF, // Greek and Coptic
    0xFF00, 0xFFEF, // Halfwidth and Fullwidth Forms
    0x4E00, 0x9FFF, // CJK Unified Ideographs
    0
};

Glyphs ranges for different languages and varied symbol you can find there.

Nuklear have some functions for creating glyph codepoint's by default:

  • nk_font_chinese_glyph_ranges() for Chinese
  • nk_font_cyrillic_glyph_ranges() for Cyrillic
  • nk_font_korean_glyph_ranges() for Korean

Step 3

Create nk_font_config structure:

struct nk_font_config config = nk_font_config(14);
config.oversample_h = 1;
config.oversample_v = 1;

There due to comments in nuklear.h:

  • 14 - baked pixel height of the font;
  • oversample_v, oversample_h - some parameters, which must be set to 1 for rasterize at hight quality for sub-pixel position

Can someone explain it better?

Step 4

Set glyph range table to config

  • for built-in functions
config.range = nk_font_cyrillic_glyph_ranges();
  • for custom table
config.range = &nk_font_japanese_glyph_ranges[0];

Step 5

Load font with customized config. For SDL loader, as example:

nk_sdl_font_stash_begin(&atlas);
struct nk_font *font = nk_font_atlas_add_from_file(atlas, "path_to_your_font", 14, &config);
nk_sdl_font_stash_end();
nk_style_set_font(ctx, &font->handle);

Where 14 - font size.

Thats all!

Now you can use UTF-8 encoding symbols in your's apps.

if (nk_button_label(ctx, "Кнопка"))
    fprintf(stdout, "button pressed\n");

NOTE!
In MS Visual Studio there is problem. As stated here:

If, for whatever reason, you want to have some hard-coded non-ASCII text within the source, you have to be aware that there is a very serious limitation: Visual Studio doesn’t support Unicode strings embedded in the source code. By default, Visual Studio saves all the source files in the local encoding specific to your system.

To resolve this problem, i find at least 3 solutions:

  • store all UTF-8 encoded strings in separate file(s) (with UTF-8 encoding) and load it at runtime
// Load single string from file
FILE* file = fopen("utf8string.txt", "r"); // open file
char text[20];                             // create buffer for string
fscanf(file, "%s", text);                  // read chars from file to buffer
fclose(file);                              // close file

if (nk_button_label(ctx, text))
    fprintf(stdout, "button pressed\n");
  • save source file(s) with UTF-8 encoded symbols with proper encoding by hand - going to "Save As..." and selecting "Save With Encoding..." and selecting "UTF-8 without signature"
  • if you use Visual Studio with installed C++ compiler with C++11 features support (at least Visual Studio 2017, need info about others) you can use u8 literal before string
if (nk_button_label(ctx, u8"Кнопка"))
    fprintf(stdout, "button pressed\n");

You may find other solutions in Google, I think. Don't forget tell us about them!

UTF-8 Demo

MSVS Update (2017-12-22)

I am using MSVS 2015 and this did the trick for me: here

if (nk_button_label(context, "嗨")) {}

UTF-8 Demo

Clone this wiki locally