| 1 | /* Copyright (C) 2026 by Maxim Iorsh <iorsh@[email protected]> |
| 2 | * |
| 3 | * Redistribution and use in source and binary forms, with or without |
| 4 | * modification, are permitted provided that the following conditions are met: |
| 5 | |
| 6 | * Redistributions of source code must retain the above copyright notice, this |
| 7 | * list of conditions and the following disclaimer. |
| 8 | |
| 9 | * Redistributions in binary form must reproduce the above copyright notice, |
| 10 | * this list of conditions and the following disclaimer in the documentation |
| 11 | * and/or other materials provided with the distribution. |
| 12 | |
| 13 | * The name of the author may not be used to endorse or promote products |
| 14 | * derived from this software without specific prior written permission. |
| 15 | |
| 16 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
| 17 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 18 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
| 19 | * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 20 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 21 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
| 22 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 23 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 24 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
| 25 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
| 27 | |
| 28 | #include "show_histogram.hpp" |
| 29 | |
| 30 | #include <sstream> |
| 31 | #include <set> |
| 32 | |
| 33 | #include "application.hpp" |
| 34 | #include "utils.hpp" |
| 35 | |
| 36 | extern "C" { |
| 37 | #include "ustring.h" |
| 38 | } |
| 39 | |
| 40 | namespace ff::dlg { |
| 41 | |
| 42 | namespace { |
| 43 | int s_bar_width = 6; |
| 44 | int s_moving_average = 1; |
| 45 | |
| 46 | std::set<int> parse_values(std::string current, int* error_pos_start = nullptr, |
| 47 | int* error_pos_end = nullptr) { |
| 48 | std::set<int> values; |
| 49 | bool substring = false; |
| 50 | if (current.empty()) { |
| 51 | if (error_pos_start) *error_pos_start = -1; |
| 52 | if (error_pos_end) *error_pos_end = -1; |
| 53 | return values; |
| 54 | } |
| 55 | |
| 56 | if (current.front() == '[' && current.back() == ']') { |
| 57 | current = current.substr(1, current.size() - 2); |
| 58 | substring = true; |
| 59 | } |
| 60 | |
| 61 | std::istringstream in(current); |
| 62 | int value = 0; |
| 63 | while (in >> value) { |
| 64 | values.insert(value); |
| 65 | } |
| 66 | |
| 67 | if (error_pos_start && error_pos_end) { |
| 68 | if (in.fail() && !in.eof()) { |
| 69 | in.clear(); |
| 70 | std::string token; |
| 71 | *error_pos_start = static_cast<int>(in.tellg()); |
| 72 | in >> token; |
| 73 | *error_pos_end = *error_pos_start + token.size(); |
| 74 | if (substring) { |
| 75 | // Adjust error positions to account for removed brackets. |
| 76 | *error_pos_start += 1; |
| 77 | *error_pos_end += 1; |
| 78 | } |
| 79 | } else { |
| 80 | *error_pos_start = -1; |
| 81 | *error_pos_end = -1; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | return values; |
| 86 | } |
| 87 | |
| 88 | std::string combine_values(const std::set<int>& values, |
| 89 | int* latest_value = nullptr) { |
| 90 | std::ostringstream out; |
| 91 | out << '['; |
| 92 | bool first = true; |
| 93 | for (int value : values) { |
| 94 | if (!first) { |
| 95 | out << ' '; |
| 96 | } |
| 97 | out << value; |
| 98 | first = false; |
| 99 | |
| 100 | // Add a placeholder after the most recent value. |
| 101 | if (latest_value && value == *latest_value) { |
| 102 | out << " ?"; |
| 103 | } |
| 104 | } |
| 105 | out << ']'; |
| 106 | return out.str(); |
| 107 | } |
| 108 | |
| 109 | class ErrorLabel : public Gtk::Label { |
| 110 | public: |
| 111 | ErrorLabel() { |
| 112 | auto error_css_provider = Gtk::CssProvider::create(); |
| 113 | error_css_provider->load_from_data("label { color: @error_color; }"); |
| 114 | get_style_context()->add_provider(error_css_provider, |
| 115 | GTK_STYLE_PROVIDER_PRIORITY_USER - 1); |
| 116 | } |
| 117 | }; |
| 118 | |
| 119 | } // namespace |
| 120 | |
| 121 | ShowHistogramDlg::ShowHistogramDlg(GWindow parent, const HistogramData& data) |
| 122 | : DialogBase(parent), data_(data) { |
| 123 | const UiStrings& ui_strings = kHistogramUiStrings.at(data.type); |
| 124 | |
| 125 | app::ColorManager::instance().register_colors({ |
| 126 | {"ff_histogram_bg", {"theme_base_color", Gdk::RGBA("white")}}, |
| 127 | {"ff_histogram_axis", {"theme_fg_color", Gdk::RGBA("black")}}, |
| 128 | {"ff_histogram_bars", {"theme_selected_bg_color", Gdk::RGBA("blue")}}, |
| 129 | {"ff_histogram_moving_average", {"error_color", Gdk::RGBA("red")}}, |
| 130 | }); |
| 131 | |
| 132 | set_title(ui_strings.title); |
| 133 | set_help_context("ui/dialogs/histogram.html"); |
| 134 | |
| 135 | std::vector<int> bar_values; |
| 136 | for (const auto& bar : data.bars) { |
| 137 | bar_values.push_back(static_cast<int>(bar.count)); |
| 138 | } |
| 139 | if (!bar_values.empty()) { |
| 140 | max_value_ = *std::max_element(bar_values.cbegin(), bar_values.cend()); |
| 141 | } |
| 142 | histogram_.set_values(bar_values); |
| 143 | histogram_.set_lower_bound(data.lower_bound); |
| 144 | histogram_.set_tooltip_text_callback([this](int index, double average) { |
| 145 | return get_tooltip_text(index, average); |
| 146 | }); |
| 147 | if (data.type == hist_blues) { |
| 148 | histogram_.set_bar_click_callback( |
| 149 | [this](int index, bool shift_pressed) { |
| 150 | on_blues_bar_click(index, shift_pressed); |
| 151 | }); |
| 152 | } else { |
| 153 | histogram_.set_bar_click_callback( |
| 154 | [this](int index, bool shift_pressed) { |
| 155 | on_stem_bar_click(index, shift_pressed); |
| 156 | }); |
| 157 | } |
| 158 | histogram_.set_bar_width(s_bar_width); |
| 159 | histogram_.set_moving_average_window(s_moving_average); |
| 160 | |
| 161 | auto histogram_scroll = Gtk::make_managed<Gtk::ScrolledWindow>(); |
| 162 | histogram_scroll->set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_NEVER); |
| 163 | histogram_scroll->set_overlay_scrolling(false); |
| 164 | histogram_scroll->add(histogram_); |
| 165 | get_content_area()->pack_start(*histogram_scroll, Gtk::PACK_EXPAND_WIDGET); |
| 166 | get_content_area()->pack_start(*build_control_box(), Gtk::PACK_SHRINK); |
| 167 | |
| 168 | auto label_group = Gtk::SizeGroup::create(Gtk::SIZE_GROUP_HORIZONTAL); |
| 169 | auto primary_box = |
| 170 | build_entry_box(ui_strings.primary_label, data_.initial_values.primary, |
| 171 | primary_entry_, label_group); |
| 172 | get_content_area()->pack_start(*primary_box, Gtk::PACK_SHRINK); |
| 173 | |
| 174 | auto secondary_box = build_entry_box(ui_strings.secondary_label, |
| 175 | data_.initial_values.secondary, |
| 176 | secondary_entry_, label_group); |
| 177 | get_content_area()->pack_start(*secondary_box, Gtk::PACK_SHRINK); |
| 178 | |
| 179 | if (data.small_selection_warning) { |
| 180 | auto warning_label = Gtk::make_managed<ErrorLabel>(); |
| 181 | warning_label->set_text( |
| 182 | _("There are so few glyphs selected that it seems unlikely to me " |
| 183 | "that you will get a representative sample of this aspect of " |
| 184 | "your font. If you deselect everything the command will apply to " |
| 185 | "all glyphs in the font")); |
| 186 | warning_label->set_line_wrap(); |
| 187 | // Prevent label from asking for a lot of horizontal space. |
| 188 | warning_label->set_max_width_chars(1); |
| 189 | warning_label->set_xalign(0.0); |
| 190 | get_content_area()->pack_start(*warning_label, Gtk::PACK_SHRINK); |
| 191 | } |
| 192 | |
| 193 | signal_response().connect( |
| 194 | [this](int response_id) { |
| 195 | if (response_id == Gtk::RESPONSE_OK && |
| 196 | (!primary_entry_.verify() || !secondary_entry_.verify())) { |
| 197 | signal_response().emission_stop(); |
| 198 | } |
| 199 | }, |
| 200 | false); |
| 201 | |
| 202 | show_all(); |
| 203 | } |
| 204 | |
| 205 | Gtk::Box* ShowHistogramDlg::build_control_box() { |
| 206 | auto controls_box = Gtk::make_managed<Gtk::Box>( |
| 207 | Gtk::ORIENTATION_HORIZONTAL, 0.5 * ff::ui_utils::ui_font_em_size()); |
| 208 | |
| 209 | auto average_label = Gtk::make_managed<Gtk::Label>(_("Smoothing window:")); |
| 210 | average_label->set_halign(Gtk::ALIGN_START); |
| 211 | average_label->set_valign(Gtk::ALIGN_CENTER); |
| 212 | controls_box->pack_start(*average_label, Gtk::PACK_SHRINK); |
| 213 | |
| 214 | average_entry_.configure( |
| 215 | Gtk::Adjustment::create(s_moving_average, 1, 99, 2, 10, 0), 1, 0); |
| 216 | average_entry_.set_numeric(true); |
| 217 | average_entry_.set_snap_to_ticks(true); |
| 218 | average_entry_.set_width_chars(2); |
| 219 | average_entry_.set_valign(Gtk::ALIGN_CENTER); |
| 220 | average_entry_.set_activates_default(); |
| 221 | average_entry_.signal_value_changed().connect([this]() { |
| 222 | s_moving_average = average_entry_.get_value_as_int(); |
| 223 | histogram_.set_moving_average_window(s_moving_average); |
| 224 | }); |
| 225 | controls_box->pack_start(average_entry_, Gtk::PACK_SHRINK); |
| 226 | |
| 227 | auto bar_width_label = Gtk::make_managed<Gtk::Label>(_("Bar width:")); |
| 228 | bar_width_label->set_halign(Gtk::ALIGN_START); |
| 229 | bar_width_label->set_valign(Gtk::ALIGN_CENTER); |
| 230 | controls_box->pack_start(*bar_width_label, Gtk::PACK_SHRINK); |
| 231 | |
| 232 | auto bar_width_entry = Gtk::make_managed<Gtk::SpinButton>( |
| 233 | Gtk::Adjustment::create(s_bar_width, 1, 100, 1, 5, 0), 1, 0); |
| 234 | bar_width_entry->set_numeric(true); |
| 235 | bar_width_entry->set_width_chars(2); |
| 236 | bar_width_entry->set_valign(Gtk::ALIGN_CENTER); |
| 237 | bar_width_entry->set_activates_default(); |
| 238 | bar_width_entry->signal_value_changed().connect([bar_width_entry, this]() { |
| 239 | s_bar_width = bar_width_entry->get_value_as_int(); |
| 240 | histogram_.set_bar_width(s_bar_width); |
| 241 | }); |
| 242 | controls_box->pack_start(*bar_width_entry, Gtk::PACK_SHRINK); |
| 243 | |
| 244 | return controls_box; |
| 245 | } |
| 246 | |
| 247 | Gtk::Box* ShowHistogramDlg::build_entry_box( |
| 248 | const Glib::ustring& label, const Glib::ustring& value, |
| 249 | widgets::VerifiedEntry& entry, Glib::RefPtr<Gtk::SizeGroup> label_group) { |
| 250 | auto entry_box = Gtk::make_managed<Gtk::Box>( |
| 251 | Gtk::ORIENTATION_HORIZONTAL, 0.5 * ff::ui_utils::ui_font_em_size()); |
| 252 | auto label_widget = Gtk::make_managed<Gtk::Label>(label + ":"); |
| 253 | label_widget->set_xalign(0.0); |
| 254 | label_widget->set_valign(Gtk::ALIGN_CENTER); |
| 255 | label_group->add_widget(*label_widget); |
| 256 | entry_box->pack_start(*label_widget, Gtk::PACK_SHRINK); |
| 257 | |
| 258 | entry.set_text(value); |
| 259 | entry.set_hexpand(true); |
| 260 | entry.set_activates_default(); |
| 261 | entry.set_verifier(dict_value_verifier); |
| 262 | entry_box->pack_start(entry, Gtk::PACK_EXPAND_WIDGET); |
| 263 | |
| 264 | return entry_box; |
| 265 | } |
| 266 | |
| 267 | std::string ShowHistogramDlg::get_tooltip_text(int bar_index, |
| 268 | double average) const { |
| 269 | if (bar_index >= (int)data_.bars.size() + data_.lower_bound) { |
| 270 | return ""; |
| 271 | } |
| 272 | const auto& bar = data_.bars[bar_index - data_.lower_bound]; |
| 273 | |
| 274 | const char* label_fmt = |
| 275 | (data_.type == hist_blues) ? _("Position: %d") : _("Width: %d"); |
| 276 | char* p_width_label = smprintf(label_fmt, bar_index); |
| 277 | char* p_count_label; |
| 278 | if (average_entry_.get_value_as_int() > 1) { |
| 279 | p_count_label = |
| 280 | smprintf(_("Count: %u (average: %.2f)"), bar.count, average); |
| 281 | } else { |
| 282 | p_count_label = smprintf(_("Count: %u"), bar.count); |
| 283 | } |
| 284 | std::string tooltip_text(p_width_label); |
| 285 | tooltip_text += "\n" + std::string(p_count_label); |
| 286 | free(p_width_label); |
| 287 | free(p_count_label); |
| 288 | |
| 289 | if (data_.type != hist_blues && max_value_ > 0) { |
| 290 | char* p_percentage_label = |
| 291 | smprintf(_("Percentage of Max: %d%%"), |
| 292 | static_cast<int>(rint(bar.count * 100.0 / max_value_))); |
| 293 | tooltip_text += "\n" + std::string(p_percentage_label); |
| 294 | free(p_percentage_label); |
| 295 | } |
| 296 | |
| 297 | if (!bar.glyph_names.empty()) { |
| 298 | tooltip_text += "\n"; |
| 299 | } |
| 300 | |
| 301 | for (const auto& name : bar.glyph_names) { |
| 302 | if (tooltip_text.back() != '\n') { |
| 303 | tooltip_text += " "; |
| 304 | } |
| 305 | tooltip_text += name; |
| 306 | // Limit tooltip length to prevent it from growing too big if there are |
| 307 | // many glyphs. |
| 308 | if (tooltip_text.size() > 300) { |
| 309 | tooltip_text += " ..."; |
| 310 | break; |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | return tooltip_text; |
| 315 | } |
| 316 | |
| 317 | void ShowHistogramDlg::on_stem_bar_click(int bar_index, bool shift_pressed) { |
| 318 | if (!shift_pressed) { |
| 319 | const std::string clicked = combine_values({bar_index}); |
| 320 | primary_entry_.set_text(clicked); |
| 321 | secondary_entry_.set_text(clicked); |
| 322 | return; |
| 323 | } |
| 324 | |
| 325 | std::set<int> values = parse_values(secondary_entry_.get_text()); |
| 326 | values.insert(bar_index); |
| 327 | secondary_entry_.set_text(combine_values(values)); |
| 328 | } |
| 329 | |
| 330 | void ShowHistogramDlg::on_blues_bar_click(int bar_index, |
| 331 | bool /*shift_pressed*/) { |
| 332 | static const std::string placeholder = "?"; |
| 333 | size_t primary_placeholder = primary_entry_.get_text().find(placeholder); |
| 334 | size_t secondary_placeholder = |
| 335 | secondary_entry_.get_text().find(placeholder); |
| 336 | // If some entry has a placeholder, the click should replace that |
| 337 | // placeholder. Otherwise we consider the value sign (positive to |
| 338 | // BlueValues, negative to OtherBlues). |
| 339 | bool is_primary = |
| 340 | primary_placeholder != std::string::npos || |
| 341 | (secondary_placeholder == std::string::npos && bar_index >= 0); |
| 342 | |
| 343 | Gtk::Entry* target_entry = is_primary ? &primary_entry_ : &secondary_entry_; |
| 344 | size_t placeholder_pos = |
| 345 | is_primary ? primary_placeholder : secondary_placeholder; |
| 346 | |
| 347 | histogram_.get_window()->set_cursor(); |
| 348 | primary_entry_.set_has_tooltip(false); |
| 349 | secondary_entry_.set_has_tooltip(false); |
| 350 | |
| 351 | // Strip placeholder before parsing |
| 352 | std::string current_text = target_entry->get_text(); |
| 353 | if (placeholder_pos != std::string::npos) { |
| 354 | current_text.erase(placeholder_pos, placeholder.size()); |
| 355 | } |
| 356 | std::set<int> values = parse_values(current_text); |
| 357 | values.insert(bar_index); |
| 358 | bool need_placeholder = values.size() % 2 == 1; |
| 359 | target_entry->set_text( |
| 360 | combine_values(values, need_placeholder ? &bar_index : nullptr)); |
| 361 | |
| 362 | // Highlight the placeholder if it was added. |
| 363 | int error_start = target_entry->get_text().find(placeholder); |
| 364 | if (error_start != std::string::npos) { |
| 365 | target_entry->select_region(error_start, error_start + 1); |
| 366 | target_entry->set_tooltip_text( |
| 367 | _("BlueValues come in pairs. Select another.")); |
| 368 | |
| 369 | histogram_.get_window()->set_cursor( |
| 370 | Gdk::Cursor::create(histogram_.get_display(), "crosshair")); |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | bool ShowHistogramDlg::dict_value_verifier(const Glib::ustring& text, |
| 375 | int& start_pos, int& end_pos) { |
| 376 | parse_values(text, &start_pos, &end_pos); |
| 377 | return start_pos == -1 && end_pos == -1; |
| 378 | } |
| 379 | |
| 380 | std::optional<PrivateDictValues> ShowHistogramDlg::show( |
| 381 | GWindow parent, const HistogramData& data) { |
| 382 | ShowHistogramDlg dialog(parent, data); |
| 383 | |
| 384 | if (dialog.run() == Gtk::RESPONSE_OK) { |
| 385 | PrivateDictValues result; |
| 386 | result.primary = dialog.primary_entry_.get_text(); |
| 387 | result.secondary = dialog.secondary_entry_.get_text(); |
| 388 | return result; |
| 389 | } else { |
| 390 | return {}; |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | std::optional<PrivateDictValues> show_histogram_dialog( |
| 395 | GWindow parent, const HistogramData& data) { |
| 396 | ff::app::GtkApp(); |
| 397 | return ShowHistogramDlg::show(parent, data); |
| 398 | } |
| 399 | |
| 400 | } // namespace ff::dlg |