Skip to content

Commit

Permalink
fix external viewers (fixes #3901)
Browse files Browse the repository at this point in the history
  • Loading branch information
Krzysztof Kowalczyk committed Nov 23, 2023
1 parent 9c64f0b commit 9abca70
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 19 deletions.
3 changes: 2 additions & 1 deletion ext/mupdf_load_system_font.c
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,8 @@ static void parseTTF(fz_context* ctx, fz_stream* file, int offset, int index, co

// check if this is a TrueType font of version 1.0 or an OpenType font
if (BEtoHl(ttOffsetTableBE.uVersion) != TTC_VERSION1 && BEtoHl(ttOffsetTableBE.uVersion) != TTAG_OTTO) {
fz_throw(ctx, FZ_ERROR_GENERIC, "fonterror : invalid font version %x", BEtoHl(ttOffsetTableBE.uVersion));
fz_throw(ctx, FZ_ERROR_GENERIC, "fonterror : invalid font '%s', invalid version %x", path,
BEtoHl(ttOffsetTableBE.uVersion));
}

// determine the name table's offset by iterating through the offset table
Expand Down
50 changes: 32 additions & 18 deletions src/ExternalViewers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "WindowTab.h"
#include "ExternalViewers.h"
#include "Commands.h"
#include "Translations.h"

#include "utils/Log.h"

Expand Down Expand Up @@ -338,31 +339,30 @@ bool CouldBePDFDoc(WindowTab* tab) {
// %1 : file path (else the file path is appended)
// %d : directory in which file is
// %p : current page number
static TempStr FormatParamsTemp(const char* cmdLine, WindowTab* tab) {
if (cmdLine == nullptr) {
cmdLine = R"("%1")";
}
if (str::Find(cmdLine, "%p")) {
static TempStr FormatParamTemp(char* arg, WindowTab* tab) {
if (str::Find(arg, "%p")) {
int pageNo = tab->ctrl ? tab->ctrl->CurrentPageNo() : 0;
TempStr pageNoStr = str::FormatTemp("%d", pageNo);
cmdLine = str::ReplaceTemp(cmdLine, "%p", pageNoStr);
arg = str::ReplaceTemp(arg, "%p", pageNoStr);
}
bool appendPath = true;
char* path = tab->filePath;
if (str::Find(cmdLine, "%d")) {
if (str::Find(arg, "%d")) {
TempStr dir = path::GetDirTemp(path);
cmdLine = str::ReplaceTemp(cmdLine, "%d", dir);
appendPath = false;
arg = str::ReplaceTemp(arg, "%d", dir);
}
if (str::Find(cmdLine, "%1")) {
if (str::Find(arg, "%1")) {
// TODO: if %1 is un-quoted, we should quote it but it's complicated because
// it could be part of a pattern like %1.Page%p.txt
// (as in https://github.com/sumatrapdfreader/sumatrapdf/issues/3868)
cmdLine = str::ReplaceTemp(cmdLine, "%1", path);
} else if (appendPath) {
cmdLine = str::FormatTemp(R"(%s "%s")", cmdLine, path);
arg = str::ReplaceTemp(arg, "%1", path);
}
return (char*)cmdLine;
return (char*)arg;
}

static TempStr GetDocumentPathQuoted(WindowTab* tab) {
TempStr path = tab->filePath;
path = str::JoinTemp("\"", path, "\"");
return path;
}

bool ViewWithKnownExternalViewer(WindowTab* tab, int cmd) {
Expand All @@ -377,8 +377,14 @@ bool ViewWithKnownExternalViewer(WindowTab* tab, int cmd) {
if (ev->exeFullPath == nullptr) {
return false;
}
TempStr params = FormatParamsTemp(ev->launchArgs, tab);
return LaunchFile(ev->exeFullPath, params);
char* origArgs = (char*)ev->launchArgs;
TempStr args;
if (origArgs) {
args = FormatParamTemp(origArgs, tab);
} else {
args = GetDocumentPathQuoted(tab);
}
return LaunchFile(ev->exeFullPath, args);
}

bool PathMatchFilter(const char* path, char* filter) {
Expand Down Expand Up @@ -421,12 +427,20 @@ bool ViewWithExternalViewer(WindowTab* tab, size_t idx) {
}
const char* exePath = args.at(0);
if (!file::Exists(exePath)) {
TempStr msg =
str::Format("External viewer executable not found: %s. Fix ExternalViewers in advanced settings.", exePath);
TempWStr msgw = ToWStrTemp(msg);
auto caption = _TR("Error");
MessageBoxExW(nullptr, msgw, caption, MB_OK | MB_ICONERROR, 0);
return false;
}
StrVec argsQuoted;
if (nArgs == 1) {
return LaunchFile(exePath, tab->filePath);
}
for (int i = 1; i < nArgs; i++) {
char* s = args.at(i);
TempStr param = FormatParamsTemp(s, tab);
TempStr param = FormatParamTemp(s, tab);
TempStr paramQuoted = QuoteCmdLineArgTemp(param);
argsQuoted.Append(paramQuoted);
}
Expand Down

0 comments on commit 9abca70

Please sign in to comment.