Use fmtlib for ScrollView::AddMessage

Signed-off-by: Stefan Weil <sw@weilnetz.de>
This commit is contained in:
Stefan Weil 2021-12-22 20:00:45 +01:00
parent d388446661
commit 69e9ba7bb6
5 changed files with 26 additions and 23 deletions

View File

@ -86,6 +86,7 @@ AM_CXXFLAGS = $(OPENMP_CXXFLAGS)
libtesseract_la_CPPFLAGS = $(AM_CPPFLAGS)
libtesseract_la_CPPFLAGS += -DTESS_COMMON_TRAINING_API=
libtesseract_la_CPPFLAGS += -I$(top_srcdir)/src/fmt/include
libtesseract_la_CPPFLAGS += -I$(top_srcdir)/src/arch
libtesseract_la_CPPFLAGS += -I$(top_srcdir)/src/ccmain
libtesseract_la_CPPFLAGS += -I$(top_srcdir)/src/ccstruct
@ -508,6 +509,7 @@ libtesseract_lstm_la_CPPFLAGS += -I$(top_srcdir)/src/cutil
libtesseract_lstm_la_CPPFLAGS += -I$(top_srcdir)/src/dict
libtesseract_lstm_la_CPPFLAGS += -I$(top_srcdir)/src/lstm
libtesseract_lstm_la_CPPFLAGS += -I$(top_srcdir)/src/viewer
libtesseract_lstm_la_CPPFLAGS += -I$(top_srcdir)/src/fmt/include
if TENSORFLOW
libtesseract_lstm_la_CPPFLAGS += -DINCLUDE_TENSORFLOW
libtesseract_lstm_la_CPPFLAGS += -I/usr/include/tensorflow
@ -728,6 +730,13 @@ libtesseract_la_SOURCES += src/wordrec/segsearch.cpp
libtesseract_la_SOURCES += src/wordrec/wordclass.cpp
endif
# Rules for thirdparty software src/fmt.
noinst_HEADERS += src/fmt/include/fmt/core.h
noinst_HEADERS += src/fmt/include/fmt/format.h
noinst_HEADERS += src/fmt/include/fmt/format-inl.h
libtesseract_la_SOURCES += src/fmt/src/format.cc
# Rules for tesseract executable.
bin_PROGRAMS = tesseract
@ -741,6 +750,7 @@ tesseract_CPPFLAGS += -I$(top_srcdir)/src/viewer
if OPENCL
tesseract_CPPFLAGS += -I$(top_srcdir)/src/opencl
endif
tesseract_CPPFLAGS += -I$(top_srcdir)/src/fmt/include
tesseract_CPPFLAGS += $(AM_CPPFLAGS)
if VISIBILITY
tesseract_CPPFLAGS += -DTESS_IMPORTS
@ -813,6 +823,7 @@ training_CPPFLAGS += -I$(top_srcdir)/src/dict
training_CPPFLAGS += -I$(top_srcdir)/src/classify
training_CPPFLAGS += -I$(top_srcdir)/src/wordrec
training_CPPFLAGS += -I$(top_srcdir)/src/cutil
training_CPPFLAGS += -I$(top_srcdir)/src/fmt/include
training_CPPFLAGS += $(ICU_UC_CFLAGS) $(ICU_I18N_CFLAGS)
training_CPPFLAGS += $(pango_CFLAGS)
training_CPPFLAGS += $(cairo_CFLAGS)

View File

@ -278,7 +278,7 @@ void ParamsEditor::Notify(const SVEvent *sve) {
} else {
ParamContent *vc = ParamContent::GetParamContentById(sve->command_id);
vc->SetValue(param);
sv_window_->AddMessageF("Setting %s to %s", vc->GetName(), vc->GetValue().c_str());
sv_window_->AddMessage("Setting {} to {}", vc->GetName(), vc->GetValue());
}
}
}
@ -336,7 +336,7 @@ void ParamsEditor::WriteParams(char *filename, bool changes_only) {
fp = fopen(filename, "wb"); // can we write to it?
if (fp == nullptr) {
sv_window_->AddMessageF("Can't write to file %s", filename);
sv_window_->AddMessage("Can't write to file {}", filename);
return;
}
for (auto &iter : vcMap) {

View File

@ -391,7 +391,6 @@ bool Tesseract::process_cmd_win_event( // UI command semantics
int32_t cmd_event, // which menu item?
char *new_value // any prompt data
) {
char msg[160];
bool exit = false;
color_mode = CM_RAINBOW;
@ -547,8 +546,7 @@ bool Tesseract::process_cmd_win_event( // UI command semantics
break;
default:
snprintf(msg, sizeof(msg), "Unrecognised event %" PRId32 "(%s)", cmd_event, new_value);
image_win->AddMessage(msg);
image_win->AddMessage("Unrecognised event {} ({})", cmd_event, new_value);
break;
}
return exit;
@ -570,7 +568,6 @@ void Tesseract::process_image_event( // action in image win
static ICOORD down;
ICOORD up;
TBOX selection_box;
char msg[80];
switch (event.type) {
case SVET_SELECTION:
@ -622,8 +619,7 @@ void Tesseract::process_image_event( // action in image win
break;
default:
sprintf(msg, "Mode %d not yet implemented", mode);
image_win->AddMessage(msg);
image_win->AddMessage("Mode {} not yet implemented", mode);
break;
}
default:

View File

@ -530,26 +530,17 @@ void ScrollView::AlwaysOnTop(bool b) {
}
// Adds a message entry to the message box.
void ScrollView::AddMessage(const char *message) {
void ScrollView::vAddMessage(fmt::string_view format, fmt::format_args args) {
auto message = fmt::vformat(format, args);
char form[kMaxMsgSize];
snprintf(form, sizeof(form), "w%u:%s", window_id_, message);
snprintf(form, sizeof(form), "w%u:%s", window_id_, message.c_str());
char *esc = AddEscapeChars(form);
SendMsg("addMessage(\"%s\")", esc);
delete[] esc;
}
void ScrollView::AddMessageF(const char *format, ...) {
va_list args;
char message[kMaxMsgSize - 4];
va_start(args, format); // variable list
vsnprintf(message, sizeof(message), format, args);
va_end(args);
AddMessage(message);
}
// Set a messagebox.
void ScrollView::AddMessageBox() {
SendMsg("addMessageBox()");

View File

@ -33,6 +33,7 @@
#include "image.h"
#include <fmt/format.h>
#include <tesseract/export.h>
#include <cstdio>
@ -295,10 +296,14 @@ public:
// Adds a messagebox to the SVWindow. This way, it can show the messages...
void AddMessageBox();
void vAddMessage(fmt::string_view format, fmt::format_args args);
// ...which can be added by this command.
// This is intended as an "debug" output window.
void AddMessage(const char *message);
void AddMessageF(const char *format, ...) __attribute__((format(printf, 2, 3)));
template <typename S, typename... Args>
void AddMessage(const S &format, Args&&... args) {
vAddMessage(format, fmt::make_args_checked<Args...>(format, args...));
}
// Zoom the window to the rectangle given upper left corner and
// lower right corner.