editor: more intuitive cursor positioning
Compute the nearest glyph edge instead of taking the one to the left of the cursor. Also fixes a segfault when trying to compute the position for an empty buffer.
This commit is contained in:
committed by
Kristian Høgsberg
parent
9f897c7a5f
commit
70f83679ee
+13
-5
@@ -153,18 +153,26 @@ text_layout_xy_to_index(struct text_layout *layout, double x, double y)
|
|||||||
{
|
{
|
||||||
cairo_text_extents_t extents;
|
cairo_text_extents_t extents;
|
||||||
int i;
|
int i;
|
||||||
|
double d;
|
||||||
|
|
||||||
|
if (layout->num_glyphs == 0)
|
||||||
|
return 0;
|
||||||
|
|
||||||
cairo_scaled_font_glyph_extents(layout->font,
|
cairo_scaled_font_glyph_extents(layout->font,
|
||||||
layout->glyphs, layout->num_glyphs,
|
layout->glyphs, layout->num_glyphs,
|
||||||
&extents);
|
&extents);
|
||||||
|
|
||||||
for (i = 1; i < layout->num_glyphs; i++) {
|
if (x < 0)
|
||||||
if (layout->glyphs[i].x >= x) {
|
return 0;
|
||||||
return i - 1;
|
|
||||||
}
|
for (i = 0; i < layout->num_glyphs - 1; ++i) {
|
||||||
|
d = layout->glyphs[i + 1].x - layout->glyphs[i].x;
|
||||||
|
if (x < layout->glyphs[i].x + d/2)
|
||||||
|
return i;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (x >= layout->glyphs[layout->num_glyphs - 1].x && x < extents.width)
|
d = extents.width - layout->glyphs[layout->num_glyphs - 1].x;
|
||||||
|
if (x < layout->glyphs[layout->num_glyphs - 1].x + d/2)
|
||||||
return layout->num_glyphs - 1;
|
return layout->num_glyphs - 1;
|
||||||
|
|
||||||
return layout->num_glyphs;
|
return layout->num_glyphs;
|
||||||
|
|||||||
Reference in New Issue
Block a user