Subject: Re: bug#50660: 28.0.50; Text artifacting when the cursor moves over text under mouse face that originally displayed a box

From: Eli Zaretskii <[email-masked]>
Date: Fri, 15 Oct 2021 16:43:56 +0300

> From: Po Lu <[email-masked]>
> Cc: [email-masked],  [email-masked]
> Date: Fri, 15 Oct 2021 09:28:17 +0800
> 
> @@ -28281,6 +28303,16 @@ fill_glyph_string (struct glyph_string *s, int face_id,
>  	break;
>      }
>  
> +  if (s->hl == DRAW_MOUSE_FACE
> +      || (s->hl == DRAW_CURSOR && cursor_in_mouse_face_p (s->w)))
> +    {
> +      Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (s->f);
> +      struct face *face
> +        = FACE_FROM_ID (s->f, hlinfo->mouse_face_face_id);
> +      s->face
> +        = FACE_FROM_ID (s->f, FACE_FOR_CHAR (s->f, face,
> +					     s->first_glyph->u.ch, -1, Qnil));
> +    }
>    s->font = s->face->font;

This part doesn't look right to me: FACE_FOR_CHAR could potentially
yield a face with a different font, but the glyph codes in the glyph
string will reference the previous font, because
get_glyph_face_and_encoding was called before the face was changed.

Also, why did you not follow the more cautious code of xterm.c:

> -  /* What face has to be used last for the mouse face?  */
> -  face_id = MOUSE_HL_INFO (s->f)->mouse_face_face_id;
> -  face = FACE_FROM_ID_OR_NULL (s->f, face_id);
> -  if (face == NULL)
> -    face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);

FACE_FROM_ID can abort if the face is not in the face cache.

> @@ -28620,7 +28679,67 @@ set_glyph_string_background_width (struct glyph_string *s, int start, int last_x
>    if (s->extends_to_end_of_line_p)
>      s->background_width = last_x - s->x + 1;
>    else
> -    s->background_width = s->width;
> +    {
> +      s->background_width = s->width;
> +#ifdef HAVE_WINDOW_SYSTEM
> +      if (FRAME_WINDOW_P (s->f)
> +	  && s->hl == DRAW_CURSOR
> +	  && cursor_in_mouse_face_p (s->w))
> +	{
> +	  /* We will have to adjust the background width of the string
> +	     in this situation, because the glyph's pixel_width might
> +	     be inconsistent with the box of the mouse face, which
> +	     leads to an ugly over-wide cursor. */
> +
> +	  struct glyph *g = s->first_glyph;
> +	  struct face *regular_face = FACE_FROM_ID (s->f, g->face_id);
> +
> +          bool do_left_box_p = g->left_box_line_p;
> +          bool do_right_box_p = g->right_box_line_p;
> +
> +          /* This is required because we test some parameters
> +             of the image slice before applying the box in
> +             produce_image_glyph. */
> +
> +          if (g->type == IMAGE_GLYPH)
> +            {
> +	      if (!s->row->reversed_p)
> +		{
> +		  struct image *img = IMAGE_FROM_ID (s->f, g->u.img_id);
> +		  do_left_box_p = g->left_box_line_p &&
> +		    g->slice.img.x == 0;
> +		  do_right_box_p = g->right_box_line_p &&
> +		    g->slice.img.x + g->slice.img.width == img->width;
> +		}
> +	      else
> +		{
> +		  struct image *img = IMAGE_FROM_ID (s->f, g->u.img_id);
> +		  do_left_box_p = g->left_box_line_p &&
> +		    g->slice.img.x + g->slice.img.width == img->width;
> +		  do_right_box_p = g->right_box_line_p &&
> +		    g->slice.img.x == 0;
> +		}
> +            }
> +
> +          /* If the glyph has a left box line, subtract it from the
> +	     offset.  */
> +          if (do_left_box_p)
> +            s->background_width -= max (0, regular_face->box_vertical_line_width);
> +          /* Likewise with the right box line, as there may be a
> +             box there as well.  */
> +          if (do_right_box_p)
> +            s->background_width -= max (0, regular_face->box_vertical_line_width);
> +          /* Now add the line widths from the new face.  */
> +          if (g->left_box_line_p)
> +            s->background_width += max (0, s->face->box_vertical_line_width);
> +          if (g->right_box_line_p)
> +            s->background_width += max (0, s->face->box_vertical_line_width);
> +
> +	  /* s->width is probably worth adjusting here as well. */
> +	  s->width = s->background_width;
> +        }
> +#endif

This looks like the same code we have elsewhere, so can't we have a
function to call in both places?

Also, the indentation with/without TABs seems wrong here.

And finally, please always leave TWO spaces after the final period in
a comment, like this:

        /* s->width is probably worth adjusting here as well.  */
                                                            ^^^