Skip to content

Commit

Permalink
set guiligatures
Browse files Browse the repository at this point in the history
  • Loading branch information
Dusan Popovic committed Sep 29, 2021
1 parent df0dda8 commit 8351819
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 24 deletions.
24 changes: 24 additions & 0 deletions src/gui.c
Expand Up @@ -460,6 +460,10 @@ gui_init_check(void)
gui.scrollbar_width = gui.scrollbar_height = SB_DEFAULT_WIDTH;
gui.prev_wrap = -1;

# ifdef FEAT_GUI_GTK
vim_memset(gui.ligatures_map, 0, 128);
#endif

#if defined(ALWAYS_USE_GUI) || defined(VIMDLL)
result = OK;
#else
Expand Down Expand Up @@ -1065,6 +1069,26 @@ gui_get_wide_font(void)
return OK;
}

/*
* Set list of ascii characters that combined can create ligature.
* Store them in 128 char map for quick access from gui_gtk2_draw_string.
*/
void
gui_set_ligatures(void)
{
char_u *p;
vim_memset(gui.ligatures_map, 0, 128);
if (p_guiligatures != NULL && *p_guiligatures != NUL)
{
for (p = p_guiligatures; *p != NUL; ++p)
{
char_u ch = *p;
if (ch < 128)
gui.ligatures_map[ch] = 1;
}
}
}

static void
gui_set_cursor(int row, int col)
{
Expand Down
2 changes: 2 additions & 0 deletions src/gui.h
Expand Up @@ -409,6 +409,8 @@ typedef struct Gui
char_u *browse_fname; // file name from filedlg

guint32 event_time;

char_u ligatures_map[128]; // ascii map 0-127, ligature char or not
#endif // FEAT_GUI_GTK

#if defined(FEAT_GUI_TABLINE) \
Expand Down
29 changes: 5 additions & 24 deletions src/gui_gtk_x11.c
Expand Up @@ -5654,25 +5654,9 @@ gui_gtk2_draw_string(int row, int col, char_u *s, int len, int flags)
* guaranteed to go trough glyphs as much as possible. Since single ligarute
* char prints as ascii, print it that way.
*/

// **** TEMPORARY BIT HERE, THIS LIST WILL GO TO GLOBAL OPTION AND BE PROCESSED THERE ****

//char *ligatures = " !\"#$%&()*+-./:<=>?@ABCDEFGHIKLMNOPRSTUVWX[]^_{|~"; // pragmata pro full set
char *ligatures = "!\"#$%&()*+-./:<=>?@[]^_{|~"; // default minimal set of ligature characters

char_u ligatures_list[128];
memset(ligatures_list, 0, 128);
for (char *p = ligatures; *p; ++p)
{
char_u ch = *p;
if (ch < 128)
ligatures_list[ch] = 1;
}
// **** TEMPORARY BIT ENDS HERE

int lensum = 0; // return value needs to add up since we are printing substrings
char_u *cs = s; // current *s pointer
int needs_pango = ((*cs & 0x80) || ligatures_list[*cs]); // look ahead, 0=ascii 1=unicode/ligatures
int needs_pango = ((*cs & 0x80) || gui.ligatures_map[*cs]); // look ahead, 0=ascii 1=unicode/ligatures
int should_need_pango;

// split string into ascii and non-ascii (ligatures + utf-8) substrings, print glyphs or use Pango
Expand All @@ -5681,12 +5665,12 @@ gui_gtk2_draw_string(int row, int col, char_u *s, int len, int flags)
int slen = 0;
while (slen < len - lensum)
{
int is_ligature = ligatures_list[*(cs + slen)];
int is_ligature = gui.ligatures_map[*(cs + slen)];
if (is_ligature && !needs_pango) // look ahead, single ligature char between ascii is ascii
{
if ((slen + 1) < (len - lensum))
{
int next_is_ligature = ligatures_list[*(cs + slen + 1)];
int next_is_ligature = gui.ligatures_map[*(cs + slen + 1)];
if (!next_is_ligature)
is_ligature = 0;
}
Expand Down Expand Up @@ -5740,14 +5724,11 @@ gui_gtk2_draw_string(int row, int col, char_u *s, int len, int flags)
// temporarily zero terminate substring, print, restore char, wrap
char_u backup_ch = *(cs + slen);
*(cs + slen) = 0;
//debug: uncomment to see splits in terminal
//printf("substr needs pango: %i: \n!%s@\n", needs_pango, cs);
//fflush(stdout);
int l = gui_gtk2_draw_string_ext(row, col + lensum, cs, slen, flags, needs_pango);
int len = gui_gtk2_draw_string_ext(row, col + lensum, cs, slen, flags, needs_pango);
*(cs + slen) = backup_ch;
cs += slen;
needs_pango = should_need_pango;
lensum += l;
lensum += len;
}
return lensum;
}
Expand Down
3 changes: 3 additions & 0 deletions src/option.h
Expand Up @@ -621,6 +621,9 @@ EXTERN char_u *p_guifontset; // 'guifontset'
EXTERN char_u *p_guifontwide; // 'guifontwide'
EXTERN int p_guipty; // 'guipty'
#endif
#ifdef FEAT_GUI_GTK
EXTERN char_u *p_guiligatures;// 'guiligatures'
# endif
#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11)
EXTERN long p_ghr; // 'guiheadroom'
#endif
Expand Down
13 changes: 13 additions & 0 deletions src/optiondefs.h
Expand Up @@ -1205,6 +1205,19 @@ static struct vimoption options[] =
{(char_u *)NULL, (char_u *)0L}
#endif
SCTX_INIT},


{"guiligatures", "gli", P_STRING|P_VI_DEF|P_RCLR|P_ONECOMMA|P_NODUP,
#if defined(FEAT_GUI_GTK)
(char_u *)&p_guiligatures, PV_NONE,
{(char_u *)"", (char_u *)0L}
#else
(char_u *)NULL, PV_NONE,
{(char_u *)NULL, (char_u *)0L}
#endif
SCTX_INIT},


{"guiheadroom", "ghr", P_NUM|P_VI_DEF,
#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11)
(char_u *)&p_ghr, PV_NONE,
Expand Down
5 changes: 5 additions & 0 deletions src/optionstr.c
Expand Up @@ -1558,6 +1558,11 @@ did_set_string_option(
errmsg = N_("E534: Invalid wide font");
redraw_gui_only = TRUE;
}
else if (varp == &p_guiligatures)
{
gui_set_ligatures();
redraw_gui_only = TRUE;
}
#endif

#ifdef CURSOR_SHAPE
Expand Down
1 change: 1 addition & 0 deletions src/proto/gui.pro
Expand Up @@ -7,6 +7,7 @@ void gui_exit(int rc);
void gui_shell_closed(void);
int gui_init_font(char_u *font_list, int fontset);
int gui_get_wide_font(void);
void gui_set_ligatures(void);
void gui_update_cursor(int force, int clear_selection);
void gui_position_menu(void);
int gui_get_base_width(void);
Expand Down

0 comments on commit 8351819

Please sign in to comment.