Skip to content

Commit

Permalink
Update xrdp font handling to use new UTF-8 calls
Browse files Browse the repository at this point in the history
  • Loading branch information
matt335672 committed Sep 27, 2023
1 parent 721e160 commit 39cd379
Show file tree
Hide file tree
Showing 8 changed files with 176 additions and 186 deletions.
93 changes: 0 additions & 93 deletions xrdp/funcs.c
Original file line number Diff line number Diff line change
Expand Up @@ -166,80 +166,6 @@ check_bounds(struct xrdp_bitmap *b, int *x, int *y, int *cx, int *cy)
return 1;
}

/*****************************************************************************/
/* add a ch at index position in text, index starts at 0 */
/* if index = -1 add it to the end */
int
add_char_at(char *text, int text_size, twchar ch, int index)
{
int len;
int i;
twchar *wstr;

len = g_mbstowcs(0, text, 0);
wstr = (twchar *)g_malloc((len + 16) * sizeof(twchar), 0);
g_mbstowcs(wstr, text, len + 1);

if ((index >= len) || (index < 0))
{
wstr[len] = ch;
wstr[len + 1] = 0;
g_wcstombs(text, wstr, text_size);
g_free(wstr);
return 0;
}

for (i = (len - 1); i >= index; i--)
{
wstr[i + 1] = wstr[i];
}

wstr[i + 1] = ch;
wstr[len + 1] = 0;
g_wcstombs(text, wstr, text_size);
g_free(wstr);
return 0;
}

/*****************************************************************************/
/* remove a ch at index position in text, index starts at 0 */
/* if index = -1 remove it from the end */
int
remove_char_at(char *text, int text_size, int index)
{
int len;
int i;
twchar *wstr;

len = g_mbstowcs(0, text, 0);

if (len <= 0)
{
return 0;
}

wstr = (twchar *)g_malloc((len + 16) * sizeof(twchar), 0);
g_mbstowcs(wstr, text, len + 1);

if ((index >= (len - 1)) || (index < 0))
{
wstr[len - 1] = 0;
g_wcstombs(text, wstr, text_size);
g_free(wstr);
return 0;
}

for (i = index; i < (len - 1); i++)
{
wstr[i] = wstr[i + 1];
}

wstr[len - 1] = 0;
g_wcstombs(text, wstr, text_size);
g_free(wstr);
return 0;
}

/*****************************************************************************/
int
set_string(char **in_str, const char *in)
Expand All @@ -253,22 +179,3 @@ set_string(char **in_str, const char *in)
*in_str = g_strdup(in);
return 0;
}

/*****************************************************************************/
int
wchar_repeat(twchar *dest, int dest_size_in_wchars, twchar ch, int repeat)
{
int index;

for (index = 0; index < repeat; index++)
{
if (index >= dest_size_in_wchars)
{
break;
}

dest[index] = ch;
}

return 0;
}
4 changes: 2 additions & 2 deletions xrdp/lang.c
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ get_keysym_from_scan_code(int device_flags, int scan_code, int *keys,
}

/*****************************************************************************/
twchar
char32_t
get_char_from_scan_code(int device_flags, int scan_code, int *keys,
int caps_lock, int num_lock, int scroll_lock,
struct xrdp_keymap *keymap)
Expand All @@ -169,7 +169,7 @@ get_char_from_scan_code(int device_flags, int scan_code, int *keys,
return 0;
}

return (twchar)(ki->chr);
return ki->chr;
}

/*****************************************************************************/
Expand Down
24 changes: 17 additions & 7 deletions xrdp/xrdp.h
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,17 @@ xrdp_painter_draw_bitmap(struct xrdp_painter *self,
int x, int y, int cx, int cy);
int
xrdp_painter_text_width(struct xrdp_painter *self, const char *text);

/* As above, but have a maximum Unicode character count for the string */
int
xrdp_painter_text_width_count(struct xrdp_painter *self,
const char *text, unsigned int c32_count);

/* Size of a string composed of a repeated number of Unicode characters */
int
xrdp_painter_repeated_char_width(struct xrdp_painter *self,
char32_t c32, unsigned int repeat_count);

unsigned int
xrdp_painter_font_body_height(const struct xrdp_painter *self);
int
Expand All @@ -349,6 +360,11 @@ xrdp_painter_draw_text2(struct xrdp_painter *self,
int box_right, int box_bottom,
int x, int y, char *data, int data_len);
int
xrdp_painter_draw_char(struct xrdp_painter *self,
struct xrdp_bitmap *bitmap,
int x, int y, char32_t chr,
unsigned int repeat_count);
int
xrdp_painter_copy(struct xrdp_painter *self,
struct xrdp_bitmap *src,
struct xrdp_bitmap *dst,
Expand Down Expand Up @@ -403,13 +419,7 @@ rect_contained_by(struct xrdp_rect *in1, int left, int top,
int
check_bounds(struct xrdp_bitmap *b, int *x, int *y, int *cx, int *cy);
int
add_char_at(char *text, int text_size, twchar ch, int index);
int
remove_char_at(char *text, int text_size, int index);
int
set_string(char **in_str, const char *in);
int
wchar_repeat(twchar *dest, int dest_size_in_wchars, twchar ch, int repeat);

/* in lang.c */
struct xrdp_key_info *
Expand All @@ -420,7 +430,7 @@ int
get_keysym_from_scan_code(int device_flags, int scan_code, int *keys,
int caps_lock, int num_lock, int scroll_lock,
struct xrdp_keymap *keymap);
twchar
char32_t
get_char_from_scan_code(int device_flags, int scan_code, int *keys,
int caps_lock, int num_lock, int scroll_lock,
struct xrdp_keymap *keymap);
Expand Down
53 changes: 22 additions & 31 deletions xrdp/xrdp_bitmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -621,8 +621,6 @@ xrdp_bitmap_invalidate(struct xrdp_bitmap *self, struct xrdp_rect *rect)
struct xrdp_rect r2;
struct xrdp_painter *painter;
unsigned int font_height;
twchar wtext[256];
char text[256];
char *p;

if (self == 0) /* if no bitmap */
Expand Down Expand Up @@ -821,10 +819,9 @@ xrdp_bitmap_invalidate(struct xrdp_bitmap *self, struct xrdp_rect *rect)

if (self->password_char != 0)
{
i = g_mbstowcs(0, self->caption1, 0);
g_memset(text, self->password_char, i);
text[i] = 0;
xrdp_painter_draw_text(painter, self, 4, 2, text);
unsigned int repeat_count = utf8_char_count(self->caption1);
xrdp_painter_draw_char(painter, self, 4, 2, self->password_char,
repeat_count);
}
else
{
Expand All @@ -838,18 +835,16 @@ xrdp_bitmap_invalidate(struct xrdp_bitmap *self, struct xrdp_rect *rect)
{
if (self->password_char != 0)
{
wchar_repeat(wtext, 255, self->password_char, self->edit_pos);
wtext[self->edit_pos] = 0;
g_wcstombs(text, wtext, 255);
w = xrdp_painter_repeated_char_width(painter,
self->password_char,
self->edit_pos);
}
else
{
g_mbstowcs(wtext, self->caption1, 255);
wtext[self->edit_pos] = 0;
g_wcstombs(text, wtext, 255);
w = xrdp_painter_text_width_count(painter, self->caption1,
self->edit_pos);
}

w = xrdp_painter_text_width(painter, text);
painter->fg_color = self->wm->white;
painter->rop = 0x5a;
xrdp_painter_fill_rect(painter, self, 4 + w, 3, 2, self->height - 6);
Expand Down Expand Up @@ -1027,14 +1022,11 @@ int
xrdp_bitmap_def_proc(struct xrdp_bitmap *self, int msg,
int param1, int param2)
{
twchar c;
int n;
int i;
int shift;
int ext;
int scan_code;
int num_bytes;
int num_chars;
struct xrdp_bitmap *b;
struct xrdp_bitmap *focus_out_control;

Expand Down Expand Up @@ -1174,7 +1166,7 @@ xrdp_bitmap_def_proc(struct xrdp_bitmap *self, int msg,
else if ((scan_code == 77 || scan_code == 80) &&
(ext || self->wm->num_lock == 0))
{
if (self->edit_pos < g_mbstowcs(0, self->caption1, 0))
if (self->edit_pos < (int)utf8_char_count(self->caption1))
{
self->edit_pos++;
xrdp_bitmap_invalidate(self, 0);
Expand All @@ -1183,14 +1175,14 @@ xrdp_bitmap_def_proc(struct xrdp_bitmap *self, int msg,
/* backspace */
else if (scan_code == 14)
{
n = g_mbstowcs(0, self->caption1, 0);
n = utf8_char_count(self->caption1);

if (n > 0)
{
if (self->edit_pos > 0)
{
self->edit_pos--;
remove_char_at(self->caption1, 255, self->edit_pos);
utf8_remove_char_at(self->caption1, self->edit_pos);
xrdp_bitmap_invalidate(self, 0);
}
}
Expand All @@ -1199,13 +1191,13 @@ xrdp_bitmap_def_proc(struct xrdp_bitmap *self, int msg,
else if (scan_code == 83 &&
(ext || self->wm->num_lock == 0))
{
n = g_mbstowcs(0, self->caption1, 0);
n = utf8_char_count(self->caption1);

if (n > 0)
{
if (self->edit_pos < n)
{
remove_char_at(self->caption1, 255, self->edit_pos);
utf8_remove_char_at(self->caption1, self->edit_pos);
xrdp_bitmap_invalidate(self, 0);
}
}
Expand All @@ -1214,7 +1206,7 @@ xrdp_bitmap_def_proc(struct xrdp_bitmap *self, int msg,
else if (scan_code == 79 &&
(ext || self->wm->num_lock == 0))
{
n = g_mbstowcs(0, self->caption1, 0);
n = utf8_char_count(self->caption1);

if (self->edit_pos < n)
{
Expand All @@ -1234,16 +1226,15 @@ xrdp_bitmap_def_proc(struct xrdp_bitmap *self, int msg,
}
else
{
c = get_char_from_scan_code
(param2, scan_code, self->wm->keys, self->wm->caps_lock,
self->wm->num_lock, self->wm->scroll_lock,
&(self->wm->keymap));
num_chars = g_mbstowcs(0, self->caption1, 0);
num_bytes = g_strlen(self->caption1);

if ((c >= 32) && (num_chars < 127) && (num_bytes < 250))
char32_t c = get_char_from_scan_code
(param2, scan_code, self->wm->keys, self->wm->caps_lock,
self->wm->num_lock, self->wm->scroll_lock,
&(self->wm->keymap));
// Add a printing character to the string. If successful,
// bump the edit position and re-display the string
if (c >= ' ' &&
utf8_add_char_at(self->caption1, 256, c, self->edit_pos))
{
add_char_at(self->caption1, 255, c, self->edit_pos);
self->edit_pos++;
xrdp_bitmap_invalidate(self, 0);
}
Expand Down
6 changes: 3 additions & 3 deletions xrdp/xrdp_login_wnd.c
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ xrdp_wm_show_edits(struct xrdp_wm *self, struct xrdp_bitmap *combo)
{
g_strncpy(b->caption1, value + ASK_LEN, 255);
}
b->edit_pos = g_mbstowcs(0, b->caption1, 0);
b->edit_pos = utf8_char_count(b->caption1);

if (self->login_window->focused_control == 0)
{
Expand All @@ -486,7 +486,7 @@ xrdp_wm_show_edits(struct xrdp_wm *self, struct xrdp_bitmap *combo)
self->session->client_info->domain,
combo->data_list->count, 0, resultIP);
g_strncpy(b->caption1, resultIP, 255);
b->edit_pos = g_mbstowcs(0, b->caption1, 0);
b->edit_pos = utf8_char_count(b->caption1);
}

}
Expand All @@ -495,7 +495,7 @@ xrdp_wm_show_edits(struct xrdp_wm *self, struct xrdp_bitmap *combo)
self->session->client_info->username[0])
{
g_strncpy(b->caption1, self->session->client_info->username, 255);
b->edit_pos = g_mbstowcs(0, b->caption1, 0);
b->edit_pos = utf8_char_count(b->caption1);

if (b->edit_pos > 0)
{
Expand Down
Loading

0 comments on commit 39cd379

Please sign in to comment.