Skip to content

Commit

Permalink
wip: extend selection start and end positions to coincide with highlight
Browse files Browse the repository at this point in the history
At the moment this often extends the selection to due overlapping
character boxes. They probably need to be shrunk such that there is no
overlap, which may not always be possible.

RTL support is not implemented either.
  • Loading branch information
hrdl-github committed Jun 10, 2023
1 parent 0d441ff commit 09f05db
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
2 changes: 1 addition & 1 deletion pdf_viewer/document.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ void Document::fill_highlight_rects(fz_context* ctx, fz_document* doc_) {
std::vector<fz_rect> highlight_rects;
std::vector<fz_rect> merged_rects;
std::wstring highlight_text;
get_text_selection(ctx, highlight.selection_begin, highlight.selection_end, true, highlight_rects, highlight_text, doc_);
get_text_selection(ctx, highlight.selection_begin, highlight.selection_end, false, highlight_rects, highlight_text, doc_);
merge_selected_character_rects(highlight_rects, merged_rects);

if (i < highlights.size()) {
Expand Down
8 changes: 7 additions & 1 deletion pdf_viewer/document_view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,13 @@ void DocumentView::add_highlight(AbsoluteDocumentPos selection_begin, AbsoluteDo
get_text_selection(selection_begin, selection_end, is_word_selection, selected_characters, selected_text);
merge_selected_character_rects(selected_characters, merged_characters);
if (selected_text.size() > 0) {
current_document->add_highlight(selected_text, merged_characters, selection_begin, selection_end, type);
const fz_rect& selection_rect = std::reduce(merged_characters.begin(), merged_characters.end(), merged_characters.at(0),
[] (const fz_rect& pos1, const fz_rect& pos2)->fz_rect {
const fz_rect& pos_tl = (pos1.y0 <= pos2.y0 && pos1.x0 <= pos2.x0) ? pos1 : pos2;
const fz_rect& pos_br = (pos1.y1 >= pos2.y1 && pos1.x1 >= pos2.x1) ? pos1 : pos2;
return { pos_tl.x0, pos_tl.y0, pos_br.x1, pos_br.y1 };
});
current_document->add_highlight(selected_text, merged_characters, { selection_rect.x0, selection_rect.y0 }, { selection_rect.x1, selection_rect.y1 }, type);
}
}
}
Expand Down

0 comments on commit 09f05db

Please sign in to comment.