Skip to content

Commit

Permalink
Merge branch 'ahrm:development' into development
Browse files Browse the repository at this point in the history
  • Loading branch information
DennisFeldbusch authored Feb 6, 2024
2 parents f83548c + dd8c570 commit cb58f04
Show file tree
Hide file tree
Showing 8 changed files with 137 additions and 15 deletions.
52 changes: 52 additions & 0 deletions pdf_viewer/document.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -974,6 +974,8 @@ void Document::reload(std::string password) {
}

bool Document::open(bool* invalid_flag, bool force_load_dimensions, std::string password, bool temp) {
load_extras();

last_update_time = QDateTime::currentDateTime();
if (doc == nullptr) {
fz_try(context) {
Expand Down Expand Up @@ -2909,6 +2911,10 @@ void Document::load_document_caches(bool* invalid_flag, bool force_now) {

int Document::reflow(int page) {

set_extra("epub_width", EPUB_WIDTH);
set_extra("epub_height", EPUB_HEIGHT);
persist_extras();

fz_bookmark last_position_bookmark = fz_make_bookmark(context,
doc, fz_location_from_page_number(context, doc, page));

Expand Down Expand Up @@ -3225,6 +3231,7 @@ std::wstring Document::get_drawings_file_path() {
QString drawing_file_name = filename + ".sioyek.drawings";
return path.file_parent().slash(drawing_file_name.toStdWString()).get_path();
}

std::wstring Document::get_scratchpad_file_path() {
Path path = Path(file_name);
#ifdef SIOYEK_ANDROID
Expand All @@ -3238,6 +3245,10 @@ std::wstring Document::get_scratchpad_file_path() {
return path.file_parent().slash(drawing_file_name.toStdWString()).get_path();
}

std::wstring Document::get_extras_file_path() {
return get_path_extras_file_name(file_name);
}

bool Document::annotations_file_exists() {
std::wstring file_path = get_annotations_file_path();
return QFileInfo(QString::fromStdWString(file_path)).exists();
Expand Down Expand Up @@ -4252,3 +4263,44 @@ int Document::get_page_from_character_offset(int offset){
}
return res;
}

bool Document::load_extras() {
QString extras_file_path = QString::fromStdWString(get_extras_file_path());
QFileInfo extras_file_info = QFileInfo(extras_file_path);
if (extras_file_info.exists()) {
QFile extras_file = QFile(extras_file_path);
if (extras_file.open(QIODevice::ReadOnly)) {
QByteArray data = extras_file.readAll();
QJsonDocument json_doc = QJsonDocument::fromJson(data);
extras = json_doc.object();
extras_file.close();
return true;
}
}
return false;

}

bool Document::persist_extras() {
QString extras_file_path = QString::fromStdWString(get_extras_file_path());
QFile extras_file = QFile(extras_file_path);
if (!extras.isEmpty()) {
if (extras_file.open(QIODevice::WriteOnly)) {
QJsonDocument doc;
doc.setObject(extras);
QByteArray data = doc.toJson();
extras_file.write(data);
extras_file.close();
return true;
}
}

return false;
}
std::optional<QVariant> Document::get_extra(QString name) {
if (extras.find(name) != extras.end()) {
return extras[name].toVariant();
}
return {};
}

12 changes: 12 additions & 0 deletions pdf_viewer/document.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ class Document {
std::wstring super_fast_search_index;
std::vector<int> super_fast_page_begin_indices;

QJsonObject extras;

// DEPRECATED a page offset which could manually be set to make the page numbers correct
// on PDF files with page numbers that start at a number other than 1. This is now
// unnecessary because mupdf 1.22 allows us to get actual page labels
Expand Down Expand Up @@ -300,6 +302,15 @@ class Document {
std::vector<DocumentPos> find_generic_locations(const std::wstring& type, const std::wstring& name);
bool can_use_highlights();

bool load_extras();
bool persist_extras();
std::optional<QVariant> get_extra(QString name);

template<typename T>
void set_extra(QString name, T value) {
extras[name] = value;
}

std::vector<std::wstring> get_page_bib_candidates(int page_number, std::vector<PagelessDocumentRect>* out_end_rects = nullptr);
std::optional<std::pair<std::wstring, PagelessDocumentRect>> get_page_bib_with_reference(int page_number, std::wstring reference_text);

Expand Down Expand Up @@ -375,6 +386,7 @@ class Document {

std::wstring get_drawings_file_path();
std::wstring get_scratchpad_file_path();
std::wstring get_extras_file_path();
std::wstring get_annotations_file_path();
bool annotations_file_exists();
bool annotations_file_is_newer_than_database();
Expand Down
1 change: 1 addition & 0 deletions pdf_viewer/document_view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -948,6 +948,7 @@ void DocumentView::persist(bool persist_drawings) {
if (persist_drawings) {
current_document->persist_drawings();
current_document->persist_annotations();
current_document->persist_extras();
}
}

Expand Down
23 changes: 18 additions & 5 deletions pdf_viewer/input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6154,11 +6154,26 @@ class MacroCommand : public Command {
if (commands[i]->next_requirement(widget)) {
return commands[i]->next_requirement(widget);
}
else {
if (!performed[i]) {
perform_subcommand(i);
}
}
}
return {};
}
}

void perform_subcommand(int index) {
if (!performed[index]) {
if (commands[index]->pushes_state()) {
widget->push_state();
}
commands[index]->run();
performed[index] = true;
}
}


bool is_enabled() {
if (!is_modal) {
Expand All @@ -6178,13 +6193,11 @@ class MacroCommand : public Command {

void perform() {
if (!is_modal) {
for (std::unique_ptr<Command>& subcommand : commands) {
for (int i = 0; i < commands.size(); i++) {

if (subcommand->pushes_state()) {
widget->push_state();
if (!performed[i]) {
perform_subcommand(i);
}

subcommand->run();
}
}
else {
Expand Down
12 changes: 7 additions & 5 deletions pdf_viewer/pdf_renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -612,11 +612,13 @@ void PdfRenderer::run(int thread_index) {

cached_response_mutex.lock();
int index = get_pending_response_index_with_thread_index(req, thread_index);
cached_responses[index].last_access_time = QDateTime::currentMSecsSinceEpoch();
cached_responses[index].pixmap = rendered_pixmap;
cached_responses[index].width = rendered_pixmap->w;
cached_responses[index].height = rendered_pixmap->h;
cached_responses[index].pending = false;
if (index >= 0) {
cached_responses[index].last_access_time = QDateTime::currentMSecsSinceEpoch();
cached_responses[index].pixmap = rendered_pixmap;
cached_responses[index].width = rendered_pixmap->w;
cached_responses[index].height = rendered_pixmap->h;
cached_responses[index].pending = false;
}

cached_response_mutex.unlock();

Expand Down
9 changes: 5 additions & 4 deletions pdf_viewer/ui.h
Original file line number Diff line number Diff line change
Expand Up @@ -316,11 +316,12 @@ class FilteredSelectTableWindowClass : public BaseSelectorWidget {
}

void set_equal_columns() {
QTableView* table_view = dynamic_cast<QTableView*>(get_view());
for (int i = 0; i < n_cols; i++) {
table_view->horizontalHeader()->setSectionResizeMode(i, QHeaderView::Stretch);
if (string_list_model) {
QTableView* table_view = dynamic_cast<QTableView*>(get_view());
for (int i = 0; i < n_cols; i++) {
table_view->horizontalHeader()->setSectionResizeMode(i, QHeaderView::Stretch);
}
}

}

void set_on_edit_function(std::function<void(T*)> edit_func) {
Expand Down
41 changes: 40 additions & 1 deletion pdf_viewer/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
#include <qscreen.h>
#include <qjsonarray.h>
#include <quuid.h>
#include <qjsondocument.h>
#include "path.h"

#ifdef SIOYEK_ANDROID
#include <QtCore/private/qandroidextras_p.h>
Expand Down Expand Up @@ -83,6 +85,40 @@ std::wstring to_lower(const std::wstring& inp) {
return res;
}

std::wstring get_path_extras_file_name(const std::wstring& path_) {
Path path = Path(path_);
QString filename = QString::fromStdWString(path.filename().value());
QString drawing_file_name = filename + ".sioyek.extras";
std::wstring extras_file_path = path.file_parent().slash(drawing_file_name.toStdWString()).get_path();
return extras_file_path;
}

void get_path_epub_size(const std::wstring& path, float* out_width, float* out_height) {
QString extras_file_path = QString::fromStdWString(get_path_extras_file_name(path));
QFileInfo extras_file_info(extras_file_path);

float width = EPUB_WIDTH;
float height = EPUB_HEIGHT;

if (extras_file_info.exists()) {
QFile extras_file(extras_file_path);
if (extras_file.open(QIODevice::ReadOnly)) {
QByteArray data = extras_file.readAll();
QJsonDocument doc = QJsonDocument::fromJson(data);
QJsonObject object = doc.object();
if (object.find("epub_width") != object.end()) {
width = object["epub_width"].toDouble();
height = object["epub_height"].toDouble();

}
extras_file.close();
}
}

*out_width = width;
*out_height = height;
}

void get_flat_toc(const std::vector<TocNode*>& roots, std::vector<std::wstring>& output, std::vector<int>& pages) {
// Enumerate ToC nodes in DFS order

Expand Down Expand Up @@ -2682,6 +2718,9 @@ fz_document* open_document_with_file_name(fz_context* context, std::wstring file
std::string file_name_str = utf8_encode(file_name);
return fz_open_document_with_stream(context, file_name_str.c_str(), stream);
#else
float epub_width, epub_height;
get_path_epub_size(file_name, &epub_width, &epub_height);

fz_document* doc = fz_open_document(context, utf8_encode(file_name).c_str());
if (fz_is_document_reflowable(context, doc)) {

Expand All @@ -2690,7 +2729,7 @@ fz_document* open_document_with_file_name(fz_context* context, std::wstring file
fz_set_user_css(context, css.c_str());
}

fz_layout_document(context, doc, EPUB_WIDTH, EPUB_HEIGHT, EPUB_FONT_SIZE);
fz_layout_document(context, doc, epub_width, epub_height, EPUB_FONT_SIZE);

//int a = 2;
}
Expand Down
2 changes: 2 additions & 0 deletions pdf_viewer/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -512,3 +512,5 @@ class AndroidTextToSpeechHandler : public TextToSpeechHandler {
virtual void set_on_app_resume_callback(std::function<void(bool, bool, int)>);
};
#endif

std::wstring get_path_extras_file_name(const std::wstring& path);

0 comments on commit cb58f04

Please sign in to comment.