Skip to content

Commit

Permalink
Fix the text selection bug with very small lines.
Browse files Browse the repository at this point in the history
  • Loading branch information
ahrm committed Jul 19, 2021
1 parent caf6ab4 commit 09d423e
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 5 deletions.
11 changes: 11 additions & 0 deletions pdf_viewer/document.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,16 @@ void Document::reload() {
fz_drop_document(context, doc);
cached_num_pages = {};

for (auto [_, cached_small_pixmap] : cached_small_pixmaps) {
fz_drop_pixmap(context, cached_small_pixmap);
}
cached_small_pixmaps.clear();

for (auto [_, cached_stext_page] : cached_stext_pages) {
fz_drop_stext_page(context, cached_stext_page);
}
cached_stext_pages.clear();

for (auto page_link_pair : cached_page_links) {
fz_drop_link(context, page_link_pair.second);
}
Expand All @@ -389,6 +399,7 @@ void Document::reload() {
cached_toc_model = nullptr;

doc = nullptr;

open(invalid_flag_pointer);
}

Expand Down
2 changes: 0 additions & 2 deletions pdf_viewer/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
//todo: make it so that all commands that change document state (for example goto_offset_withing_page, goto_link, etc.) do not change the document
// state, instead they return a DocumentViewState object that is then applied using push_state and chnage_state functions
// (chnage state should be a function that just applies the state without pushing it to history)
//todo: add "repeat last command" command
//todo: fix selection logic so tutorial is not annoying!
//todo: pdf live reload has some weird bugs. probably caused by inappropriate use of mupdf.

#include <iostream>
Expand Down
12 changes: 12 additions & 0 deletions pdf_viewer/pdf_renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,9 @@ void PdfRenderer::delete_old_pages(bool force_all) {
for (int i = 0; i < cached_responses.size(); i++) {
indices_to_delete.push_back(i);
}
are_documents_invalidated = true;
}

else if (cached_response_times.size() > N) {
// we never delete N most recent pages
// todo: make this configurable
Expand Down Expand Up @@ -333,6 +335,7 @@ PdfRenderer::~PdfRenderer() {
}

fz_document* PdfRenderer::get_document_with_path(int thread_index, fz_context* mupdf_context, std::wstring path) {

std::pair<int, std::wstring> document_id = std::make_pair(thread_index, path);

if (opened_documents.find(document_id) != opened_documents.end()) {
Expand Down Expand Up @@ -390,6 +393,15 @@ void PdfRenderer::run(int thread_index) {

// if the request is already rendered, just return the previous result
cached_response_mutex.lock();

if (are_documents_invalidated) {
for (auto [_, document] : opened_documents) {
fz_drop_document(mupdf_context, document);
}
opened_documents.clear();
are_documents_invalidated = false;
}

bool is_already_rendered = false;
for (const auto& cached_rep : cached_responses) {
if (cached_rep.request == req) is_already_rendered = true;
Expand Down
1 change: 1 addition & 0 deletions pdf_viewer/pdf_renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ class PdfRenderer : public QObject{
QTimer garbage_collect_timer;

bool* should_quit_pointer = nullptr;
bool are_documents_invalidated = false;

int num_threads;

Expand Down
21 changes: 19 additions & 2 deletions pdf_viewer/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,19 @@ bool is_consequtive(fz_rect rect1, fz_rect rect2) {
float ydist2 = abs(rect1.y1 - rect2.y1);
float ydist = std::min(ydist1, ydist2);

if (xdist < 40.0f && ydist < 40.0f) {
float rect1_width = rect1.x1 - rect1.x0;
float rect2_width = rect2.x1 - rect2.x0;
float average_width = (rect1_width + rect2_width) / 2.0f;

float rect1_height = rect1.y1 - rect1.y0;
float rect2_height = rect2.y1 - rect2.y0;
float average_height = (rect1_height + rect2_height) / 2.0f;

//if (xdist < 40.0f && ydist < 40.0f) {
//if (xdist < 20.0f && ydist < 20.0f) {
// return true;
//}
if (xdist < 2*average_width && ydist < 2*average_height) {
return true;
}
//if ( ydist < 10.0f) {
Expand Down Expand Up @@ -557,7 +569,7 @@ fz_rect bound_rects(const std::vector<fz_rect>& rects) {
return res;

}
void merge_selected_character_rects(std::vector<fz_rect> selected_character_rects, std::vector<fz_rect>& resulting_rects) {
void merge_selected_character_rects(const std::vector<fz_rect>& selected_character_rects, std::vector<fz_rect>& resulting_rects) {
/*
This function merges the bounding boxes of all selected characters into large line chunks.
*/
Expand Down Expand Up @@ -585,6 +597,11 @@ void merge_selected_character_rects(std::vector<fz_rect> selected_character_rect
}
}

//for (auto rect : line_rects) {
// resulting_rects.push_back(rect);
//}
//return;

if (line_rects.size() > 0) {
fz_rect bounding_rect = bound_rects(line_rects);
resulting_rects.push_back(bounding_rect);
Expand Down
2 changes: 1 addition & 1 deletion pdf_viewer/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ void get_stext_page_string(fz_stext_page* page, std::wstring& res);
bool does_stext_block_starts_with_string(fz_stext_block* block, const std::wstring& str);
bool does_stext_block_starts_with_string_case_insensitive(fz_stext_block* block, const std::wstring& str);
std::wstring get_figure_string_from_raw_string(const std::wstring& raw_string);
void merge_selected_character_rects(std::vector<fz_rect> selected_character_rects, std::vector<fz_rect>& resulting_rects);
void merge_selected_character_rects(const std::vector<fz_rect>& selected_character_rects, std::vector<fz_rect>& resulting_rects);
void string_split(std::string haystack, const std::string& needle, std::vector<std::string>& res);
void run_command(std::wstring command, std::wstring parameters);

Expand Down

0 comments on commit 09d423e

Please sign in to comment.