Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix copied string corrupt when contain CJK glyphs #543

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 18 additions & 7 deletions demo/glfw_opengl2/nuklear_glfw_gl2.h
Original file line number Diff line number Diff line change
Expand Up @@ -240,15 +240,26 @@ nk_glfw3_clipboard_paste(nk_handle usr, struct nk_text_edit *edit)
}

NK_INTERN void
nk_glfw3_clipboard_copy(nk_handle usr, const char *text, int len)
nk_glfw3_clipboard_copy(nk_handle usr, const char *text, int glyphs)
{
char *str = 0;
(void)usr;
if (!len) return;
str = (char*)malloc((size_t)len+1);
NK_UNUSED(usr);
if (!glyphs) return;

int bytes = 0;
for (int i = 0; i < glyphs; i++)
{
int glyph_bytes;
// Find out how many bytes in a glyph
for (glyph_bytes = 1; (nk_byte)text[bytes] >= nk_utfmask[glyph_bytes]; glyph_bytes++)
;
bytes += glyph_bytes;
}

char* str = (char*)malloc(bytes + 1);
if (!str) return;
memcpy(str, text, (size_t)len);
str[len] = '\0';

memcpy(str, text, bytes);
str[bytes] = '\0';
glfwSetClipboardString(glfw.win, str);
free(str);
}
Expand Down
27 changes: 19 additions & 8 deletions demo/glfw_opengl3/nuklear_glfw_gl3.h
Original file line number Diff line number Diff line change
Expand Up @@ -353,16 +353,27 @@ nk_glfw3_clipboard_paste(nk_handle usr, struct nk_text_edit *edit)
}

NK_INTERN void
nk_glfw3_clipboard_copy(nk_handle usr, const char *text, int len)
nk_glfw3_clipboard_copy(nk_handle usr, const char *text, int glyphs)
{
struct nk_glfw* glfw = (struct nk_glfw*)usr.ptr;
char *str = 0;
if (!len) return;
str = (char*)malloc((size_t)len+1);
NK_UNUSED(usr);
if (!glyphs) return;

int bytes = 0;
for (int i = 0; i < glyphs; i++)
{
int glyph_bytes;
// Find out how many bytes in a glyph
for (glyph_bytes = 1; (nk_byte)text[bytes] >= nk_utfmask[glyph_bytes]; glyph_bytes++)
;
bytes += glyph_bytes;
}

char* str = (char*)malloc(bytes + 1);
if (!str) return;
memcpy(str, text, (size_t)len);
str[len] = '\0';
glfwSetClipboardString(glfw->win, str);

memcpy(str, text, bytes);
str[bytes] = '\0';
glfwSetClipboardString(glfw.win, str);
free(str);
}

Expand Down
25 changes: 18 additions & 7 deletions demo/glfw_opengl4/nuklear_glfw_gl4.h
Original file line number Diff line number Diff line change
Expand Up @@ -499,15 +499,26 @@ nk_glfw3_clipboard_paste(nk_handle usr, struct nk_text_edit *edit)
}

NK_INTERN void
nk_glfw3_clipboard_copy(nk_handle usr, const char *text, int len)
nk_glfw3_clipboard_copy(nk_handle usr, const char *text, int glyphs)
{
char *str = 0;
(void)usr;
if (!len) return;
str = (char*)malloc((size_t)len+1);
NK_UNUSED(usr);
if (!glyphs) return;

int bytes = 0;
for (int i = 0; i < glyphs; i++)
{
int glyph_bytes;
// Find out how many bytes in a glyph
for (glyph_bytes = 1; (nk_byte)text[bytes] >= nk_utfmask[glyph_bytes]; glyph_bytes++)
;
bytes += glyph_bytes;
}

char* str = (char*)malloc(bytes + 1);
if (!str) return;
memcpy(str, text, (size_t)len);
str[len] = '\0';

memcpy(str, text, bytes);
str[bytes] = '\0';
glfwSetClipboardString(glfw.win, str);
free(str);
}
Expand Down