From eb5de0d534f637253a7b34b01048b655d5befc20 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A0=20Arrufat?= <1671644+arrufat@users.noreply.github.com> Date: Fri, 24 Feb 2023 12:34:06 +0900 Subject: [PATCH] Add support for loading custom label fonts in imglab (#2733) --- tools/imglab/src/main.cpp | 7 +++++-- tools/imglab/src/metadata_editor.cpp | 23 ++++++++++++++++++++++- tools/imglab/src/metadata_editor.h | 5 ++++- 3 files changed, 31 insertions(+), 4 deletions(-) diff --git a/tools/imglab/src/main.cpp b/tools/imglab/src/main.cpp index 1f5a9b2cd..57704599a 100644 --- a/tools/imglab/src/main.cpp +++ b/tools/imglab/src/main.cpp @@ -21,7 +21,7 @@ #include -const char* VERSION = "1.19"; +const char* VERSION = "1.20"; @@ -599,6 +599,7 @@ int main(int argc, char** argv) parser.add_option("h","Displays this information."); parser.add_option("v","Display version."); + parser.add_option("font", "Set the label font in the GUI.", 1); parser.set_group_name("Creating XML files"); parser.add_option("c","Create an XML file named listing a set of images.",1); @@ -672,6 +673,8 @@ int main(int argc, char** argv) parser.parse(argc, argv); + const std::string font_path = get_option(parser, "font", ""); + const char* singles[] = {"h","c","r","l","files","convert","parts","rmdiff", "rmtrunc", "rmdupes", "seed", "shuffle", "split", "add", "flip-basic", "flip", "rotate", "tile", "size", "cluster", "resample", "min-object-size", "rmempty", "crop-size", "cropped-object-size", "rmlabel", "rm-other-labels", "rm-if-overlaps", "sort-num-objects", @@ -1263,7 +1266,7 @@ int main(int argc, char** argv) if (parser.number_of_arguments() == 1) { - metadata_editor editor(parser[0]); + metadata_editor editor(parser[0], font_path); if (parser.option("parts")) { std::vector parts = split(parser.option("parts").argument()); diff --git a/tools/imglab/src/metadata_editor.cpp b/tools/imglab/src/metadata_editor.cpp index 794f5a5b3..f3e0e49db 100644 --- a/tools/imglab/src/metadata_editor.cpp +++ b/tools/imglab/src/metadata_editor.cpp @@ -23,7 +23,8 @@ extern const char* VERSION; metadata_editor:: metadata_editor( - const std::string& filename_ + const std::string& filename_, + const std::string& font_path ) : mbar(*this), lb_images(*this), @@ -36,6 +37,26 @@ metadata_editor( { file metadata_file(filename_); filename = metadata_file.full_name(); + + // Set the custom label fonts + if (!font_path.empty()) + { + const auto custom_font = std::make_shared(); + std::ifstream fin(font_path); + if (fin.good()) + { + custom_font->read_bdf_file(fin, 0xFFFF); + custom_font->adjust_metrics(); + font = std::move(custom_font); + } + else + { + std::cerr << "WARNING: could not open file '" + font_path + "', using default font\n."; + } + } + display.set_main_font(font); + overlay_label.set_main_font(font); + // Make our current directory be the one that contains the metadata file. We // do this because that file might contain relative paths to the image files // we are supposed to be loading. diff --git a/tools/imglab/src/metadata_editor.h b/tools/imglab/src/metadata_editor.h index 71aa14ace..eba3ed3a4 100644 --- a/tools/imglab/src/metadata_editor.h +++ b/tools/imglab/src/metadata_editor.h @@ -58,7 +58,8 @@ class metadata_editor : public dlib::drawable_window { public: metadata_editor( - const std::string& filename_ + const std::string& filename_, + const std::string& font_path = "" ); ~metadata_editor(); @@ -107,6 +108,8 @@ private: time_t last_keyboard_jump_pos_update; bool display_equialized_image = false; color_mapper string_to_color; + + std::shared_ptr font = dlib::default_font::get_font(); }; // ----------------------------------------------------------------------------------------