vurtun / nuklear

A single-header ANSI C gui library
13.68k stars 1.1k forks source link

Cyrillic #611

Closed RomanJdikin777 closed 6 years ago

RomanJdikin777 commented 6 years ago

Hello, I can not print the text with the Cyrillic. I do everything as in the examples, but instead of the text, empty symbols.

struct nk_font_atlas *atlas = nullptr;
struct nk_font_config cfg = nk_font_config(14);
cfg.oversample_h = 1;
cfg.oversample_v = 1;
cfg.range = nk_font_cyrillic_glyph_ranges();
nk_font *font = nk_font_atlas_add_from_file(&atlas, "Font.ttf", 14, &cfg);

Font.ttf - supports the Cyrillic, tried and other fonts.

Please, tell me what's the matter, how do I use the Cyrillic?

vurtun commented 6 years ago

You have to make sure that your font has cyrillic glypes. I loaded roboto from extra_fonts/ and set the correct glyph ranges:

    struct nk_font_atlas *atlas;
    struct nk_font_config cfg = nk_font_config(14);
    nk_font_stash_begin(&atlas);
    cfg.range = nk_font_cyrillic_glyph_ranges();
    struct nk_font *roboto = nk_font_atlas_add_from_file(atlas, "../../../extra_font/Roboto-Regular.ttf", 14, &cfg);
    nk_font_stash_end();

As soon as above code ran correctly you can just use cryillic for test:

if (nk_begin(ctx, "Demo", nk_rect(50, 50, 230, 250),
    NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|NK_WINDOW_SCALABLE|
    NK_WINDOW_MINIMIZABLE|NK_WINDOW_TITLE))
{
    nk_layout_row_static(ctx, 30, 80, 1);
    if (nk_button_label(ctx, "Привет!"))
        printf("button pressed!\n");
}

With following output:

cryillic

RomanJdikin777 commented 6 years ago

There is still no text. I provide the code, you can find my error.

#define NK_INCLUDE_FIXED_TYPES
#define NK_INCLUDE_STANDARD_IO
#define NK_INCLUDE_STANDARD_VARARGS
#define NK_INCLUDE_DEFAULT_ALLOCATOR
#define NK_INCLUDE_VERTEX_BUFFER_OUTPUT
#define NK_INCLUDE_FONT_BAKING
#define NK_INCLUDE_DEFAULT_FONT
#define NK_IMPLEMENTATION

#include "nuklear.h"

nk_context *ctx;

NK_INTERN void gui_device_upload_atlas(const void *image, int width, int height)
{
    struct gui_device *dev = &glgui.ogl;
    glGenTextures(1, &dev->font_tex);
    glBindTexture(GL_TEXTURE_2D, dev->font_tex);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, (GLsizei)width, (GLsizei)height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image);
    glBindTexture(GL_TEXTURE_2D, 0);

    // Here the texture contains the Cyrillic, checked "xxx.bmp"
    // void *color = malloc(width * height * 4);
    // glBindTexture(GL_TEXTURE_2D, dev->font_tex);
    // glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE, color);
    // glBindTexture(GL_TEXTURE_2D, 0);
    // save_image("xxx.bmp", width, height, 4, color);
    // free(color);
}

struct nk_context *gui_init(nk_user_font *font)
{
    nk_init_default(&glgui.ctx, font);
    glgui.ctx.clip.copy = gui_clipbard_copy;
    glgui.ctx.clip.paste = gui_clipbard_paste;
    glgui.ctx.clip.userdata = nk_handle_ptr(0);
    nk_buffer_init_default(&glgui.ogl.cmds);

    glgui.is_double_click_down = nk_false;
    glgui.double_click_pos = nk_vec2(0, 0);

    return &glgui.ctx;
}

void gui_font_stash_begin(struct nk_font_atlas **atlas)
{
    nk_font_atlas_init_default(&glgui.atlas);
    nk_font_atlas_begin(&glgui.atlas);
    *atlas = &glgui.atlas;
}

void gui_font_stash_end()
{
    int w = 0, h = 0;
    const void *image = NULL;
    image = nk_font_atlas_bake(&glgui.atlas, &w, &h, NK_FONT_ATLAS_RGBA32);
    gui_device_upload_atlas(image, w, h);
    nk_font_atlas_end(&glgui.atlas, nk_handle_id((int)glgui.ogl.font_tex), &glgui.ogl.null);
    if(glgui.atlas.default_font) nk_style_set_font(&glgui.ctx, &glgui.atlas.default_font->handle);
}

void CreateGUI()
{
    ctx = gui_init(NULL);

    struct nk_font_atlas *atlas;
    struct nk_font_config cfg = nk_font_config(14);
    gui_font_stash_begin(&atlas);
    cfg.range = nk_font_cyrillic_glyph_ranges();
    struct nk_font *font = nk_font_atlas_add_from_file(atlas, "Roboto-Regular.ttf", 14, &cfg);
    gui_font_stash_end();

    nk_style_set_font(ctx, &font->handle);
}

void RenderGUI()
{
    if(nk_begin(ctx, "Окно", nk_rect(50, 50, 200, 200), 31))
    {
        nk_layout_row_static(ctx, 30, 80, 1);
        if(nk_button_label(ctx, "Кнопка"))
            printf("button pressed!\n");
    }
}

I hope for your help, for a long time I have not been able to display the text with the Cyrillic. image