add method xy_to_index and use it to calculate wrap position
This commit is contained in:
parent
6d670f11c6
commit
63497a9d27
4 changed files with 18 additions and 15 deletions
|
@ -317,6 +317,18 @@ point pango_text::get_column_line(const point& position) const
|
|||
}
|
||||
}
|
||||
|
||||
int pango_text::xy_to_index(const point& position) const
|
||||
{
|
||||
this->recalculate();
|
||||
|
||||
// Get the index of the character.
|
||||
int index, trailing;
|
||||
pango_layout_xy_to_index(layout_.get(), position.x * PANGO_SCALE,
|
||||
position.y * PANGO_SCALE, &index, &trailing);
|
||||
|
||||
return index;
|
||||
}
|
||||
|
||||
void pango_text::add_attribute_size(const unsigned start_offset, const unsigned end_offset, int size)
|
||||
{
|
||||
attribute_start_offset_ = start_offset;
|
||||
|
|
|
@ -224,6 +224,8 @@ public:
|
|||
*/
|
||||
point get_column_line(const point& position) const;
|
||||
|
||||
int xy_to_index(const point& position) const;
|
||||
|
||||
/**
|
||||
* Retrieves a list of strings with contents for each rendered line.
|
||||
*
|
||||
|
|
|
@ -255,20 +255,9 @@ void rich_label::add_link(config& curr_item, std::string name, std::string dest,
|
|||
size_t rich_label::get_split_location(std::string text, const point& pos) {
|
||||
font::get_text_renderer().set_maximum_width(pos.x);
|
||||
font::get_text_renderer().set_text(text, true);
|
||||
point wrap_position = get_column_line(pos);
|
||||
|
||||
size_t len = 0;
|
||||
for (int i = 0; i < wrap_position.y; i++) {
|
||||
PangoLayoutLine* line = font::get_text_renderer().get_line(i);
|
||||
if (line != nullptr) {
|
||||
len += line->length;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
len += wrap_position.x;
|
||||
// size() and utf::size() can return different values
|
||||
len = len > (text.size()-1) ? text.size()-1 : len;
|
||||
size_t len = get_offset_from_xy(pos);
|
||||
len = (len > text.size()-1) ? text.size()-1 : len;
|
||||
|
||||
// break only at word boundary
|
||||
char c;
|
||||
|
|
|
@ -238,9 +238,9 @@ private:
|
|||
|
||||
std::function<void(std::string)> link_handler_;
|
||||
|
||||
point get_column_line(const point& position) const
|
||||
int get_offset_from_xy(const point& position) const
|
||||
{
|
||||
return font::get_text_renderer().get_column_line(position);
|
||||
return font::get_text_renderer().xy_to_index(position);
|
||||
}
|
||||
|
||||
point get_xy_from_offset(const unsigned offset) const
|
||||
|
|
Loading…
Add table
Reference in a new issue