Debian Codegraph @

fontforgeexe/gtk/bitmaps_dlg.cpp

raw symbols SCIP
1 /* Copyright (C) 2025 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 "bitmaps_dlg.hpp"
29
30 #include <numeric>
31
32 #include "utils.hpp"
33 #include "intl.h"
34
35 extern "C" {
36 #include "ustring.h"
37 }
38
39 namespace ff::dlg {
40
41 static const uint16_t kSizeError = 0xffffu;
42
43 static std::string FormatBitmapSize(int px_size) {
44 return std::to_string(px_size);
45 }
46
47 static std::string FormatBitmapSize(int px_size, double scale) {
48 char decimal_point = ui_utils::get_decimal_point();
49 char buffer[10];
50 snprintf(buffer, sizeof(buffer), "%.1f", px_size * scale);
51 return buffer;
52 }
53
54 template <typename... ARGS>
55 static std::string SizeString(const BitmapSizes& sizes, ARGS... args) {
56 std::string list_separator =
57 ui_utils::get_list_separator() + std::string(" ");
58 std::string size_list;
59 for (auto [px_size, depth] : sizes) {
60 if (!size_list.empty()) size_list += list_separator;
61 if (depth == 1) {
62 size_list += FormatBitmapSize(px_size, args...);
63 } else {
64 size_list += FormatBitmapSize(px_size, args...) + "@" +
65 std::to_string(depth);
66 }
67 }
68 return size_list;
69 }
70
71 unsigned long int strict_strtoul(const char* nptr, char** endptr, int base) {
72 unsigned long int result = strtoul(nptr, endptr, base);
73 // Reject negative numbers
74 if (((long int)result) < 0 || result > kSizeError) {
75 if (endptr) {
76 *endptr = (char*)nptr;
77 }
78 return 0;
79 }
80 return result;
81 }
82
83 static BitmapSizes ParseList(const Glib::ustring& str) {
84 BitmapSizes sizes;
85 uint16_t size, depth;
86 char* end = nullptr;
87
88 for (const char* pt = str.c_str(); *pt != '\0';) {
89 size = strict_strtoul(pt, &end, 10);
90 if (*end == '@')
91 depth = strict_strtoul(end + 1, &end, 10);
92 else
93 depth = 1u;
94 if (size > 0 && depth > 0) sizes.emplace_back(size, depth);
95 if (*end != ' ' && *end != ',' && *end != ';' && *end != '\0') {
96 // If the token is bad, we seek to its end and report its position
97 // in a slight abuse of the return value.
98 while (*end != ' ' && *end != ',' && *end != ';' && *end != '\0')
99 ++end;
100 return {{kSizeError, kSizeError},
101 {pt - str.c_str(), end - str.c_str()}};
102 }
103 // We are pedantic with list separators, but the user could be not.
104 while (*end == ' ' || *end == ',' || *end == ';') ++end;
105 pt = end;
106 }
107
108 return sizes;
109 }
110
111 Glib::ustring BitmapsDlg::last_scope_ = "selection";
112
113 BitmapsDlg::BitmapsDlg(GWindow parent, BitmapsDlgMode mode,
114 const BitmapSizes& sizes, bool bitmaps_only,
115 bool has_current_char)
116 : DialogBase(parent) {
117 set_help_context("ui/menus/elementmenu.html", "#elementmenu-bitmaps");
118 Glib::ustring title;
119 std::vector<Glib::ustring> headings;
120
121 if (mode == bitmaps_dlg_avail) {
122 title = _("Bitmap Strikes Available");
123 headings = {_("The list of current pixel bitmap sizes"),
124 _(" Removing a size will delete it."),
125 bitmaps_only
126 ? _(" Adding a size will create it by scaling.")
127 : _(" Adding a size will create it.")};
128 } else if (mode == bitmaps_dlg_regen) {
129 title = _("Regenerate Bitmap Glyphs");
130 headings = {_("Specify bitmap sizes to be regenerated")};
131 } else {
132 title = _("Remove Bitmap Glyphs");
133 headings = {_("Specify bitmap sizes to be removed")};
134 }
135
136 set_title(title);
137 set_hints_horizontal_resize_only();
138
139 Glib::ustring heading = std::accumulate(
140 headings.begin() + 1, headings.end(), headings[0],
141 [](const auto& a, const auto& b) { return a + "\n• " + b; });
142 auto heading_label = Gtk::make_managed<Gtk::Label>(heading);
143 heading_label->set_halign(Gtk::ALIGN_START);
144 get_content_area()->pack_start(*heading_label);
145
146 if (mode != bitmaps_dlg_avail) {
147 glyphs_combo_ = build_glyphs_combo(has_current_char);
148 get_content_area()->pack_start(glyphs_combo_);
149 }
150
151 pixels_entry_.set_text(SizeString(sizes));
152 pixels_entry_.set_activates_default();
153 pixels_entry_.set_verifier(pixel_size_verifier);
154 auto pixels_frame = Gtk::make_managed<Gtk::Frame>(_("Pixel Sizes:"));
155 pixels_frame->add(pixels_entry_);
156 pixels_frame->set_shadow_type(Gtk::SHADOW_NONE);
157 get_content_area()->pack_start(*pixels_frame);
158
159 auto pt_this_frame = Gtk::make_managed<Gtk::Frame>();
160 auto pt_this_entry = Gtk::make_managed<Gtk::Entry>();
161 pt_this_entry->set_editable(false);
162 pt_this_entry->set_can_focus(false);
163 pt_this_frame->add(*pt_this_entry);
164 pt_this_frame->set_shadow_type(Gtk::SHADOW_NONE);
165 pt_this_frame->signal_realize().connect([pt_this_frame, pt_this_entry,
166 sizes]() {
167 // Set the PPI here, as the monitor is not available before the
168 // realization.
169 int current_ppi = ui_utils::get_current_ppi(pt_this_frame);
170 char* pt_this_label =
171 smprintf(_("Point sizes on this monitor (%d PPI)"), current_ppi);
172 pt_this_frame->set_label(pt_this_label);
173 pt_this_entry->set_text(SizeString(sizes, 72.0 / current_ppi));
174 free(pt_this_label);
175 });
176 get_content_area()->pack_start(*pt_this_frame);
177
178 auto pt_96_frame =
179 Gtk::make_managed<Gtk::Frame>(_("Point sizes on a 96 PPI monitor"));
180 auto pt_96_entry = Gtk::make_managed<Gtk::Entry>();
181 pt_96_entry->set_text(SizeString(sizes, 72.0 / 96.0));
182 pt_96_entry->set_editable(false);
183 pt_96_entry->set_can_focus(false);
184 pt_96_frame->add(*pt_96_entry);
185 pt_96_frame->set_shadow_type(Gtk::SHADOW_NONE);
186 get_content_area()->pack_start(*pt_96_frame);
187
188 pixels_entry_.signal_changed().connect(
189 [this, pt_96_entry, pt_this_entry]() {
190 auto sizes = ParseList(pixels_entry_.get_text());
191 if (!sizes.empty() && sizes[0].first == kSizeError) {
192 return;
193 }
194
195 int current_ppi = ui_utils::get_current_ppi(pt_this_entry);
196 pt_this_entry->set_text(SizeString(sizes, 72.0 / current_ppi));
197 pt_96_entry->set_text(SizeString(sizes, 72.0 / 96.0));
198 });
199
200 if (mode == bitmaps_dlg_avail) {
201 rasterize_check_.set_label(
202 _("Create Rasterized Strikes (Not empty ones)"));
203 rasterize_check_.set_halign(Gtk::ALIGN_START);
204 rasterize_check_.set_active(true);
205 get_content_area()->pack_start(rasterize_check_);
206 }
207
208 signal_response().connect(
209 [this](int response_id) {
210 if (response_id == Gtk::RESPONSE_OK && !pixels_entry_.verify()) {
211 signal_response().emission_stop();
212 }
213 },
214 false);
215
216 get_content_area()->set_spacing(0.5 * ui_utils::ui_font_eX_size());
217 show_all();
218 }
219
220 Gtk::ComboBoxText BitmapsDlg::build_glyphs_combo(bool has_current_char) const {
221 static const std::vector<std::pair<Glib::ustring, Glib::ustring>>
222 glyphs_combo_items = {
223 {"all", _("All Glyphs")},
224 {"selection", _("Selected Glyphs")},
225 {"current", _("Current Glyph")},
226 };
227
228 Gtk::ComboBoxText glyphs_combo;
229 for (const auto& [item_id, item_label] : glyphs_combo_items) {
230 glyphs_combo.append(item_id, item_label);
231 }
232
233 // In Font View the current character is not applicable, and we need to
234 // disable its entry in the drop-down list. Conversely, in Char and Metrics
235 // Views the current character is applicable, but the selection belongs to
236 // an obscured Font View, and acting on it would be counterintuitive.
237 Glib::ustring disabled_item = has_current_char ? "selection" : "current";
238
239 Glib::ustring active_item = (last_scope_ == "all") ? last_scope_
240 : has_current_char ? "current"
241 : "selection";
242 glyphs_combo.set_active_id(active_item);
243
244 Gtk::CellRenderer* renderer = glyphs_combo.get_first_cell();
245 glyphs_combo.set_cell_data_func(
246 *renderer,
247 [renderer, disabled_item](const Gtk::TreeModel::const_iterator& it) {
248 // In the Gtk::ComboBoxText, the second column of the TreeModel
249 // contains the item id as a string. It's a sacred knowledge, we
250 // must not question it.
251 Glib::ustring item_id;
252 it->get_value(1, item_id);
253 renderer->set_sensitive(item_id != disabled_item);
254 });
255
256 return glyphs_combo;
257 }
258
259 bool BitmapsDlg::pixel_size_verifier(const Glib::ustring& text, int& start_pos,
260 int& end_pos) {
261 auto sizes = ParseList(text);
262 bool is_valid = true;
263 if (sizes.size() == 2 && sizes[0].first == kSizeError) {
264 is_valid = false;
265 start_pos = sizes[1].first;
266 end_pos = sizes[1].second;
267 }
268 return is_valid;
269 }
270
271 bool BitmapsDlg::show() {
272 Gtk::ResponseType result = run();
273 if (result == Gtk::RESPONSE_OK) {
274 last_scope_ = get_active_scope();
275 return true;
276 }
277 return false;
278 }
279
280 BitmapSizes BitmapsDlg::get_sizes() const {
281 return ParseList(pixels_entry_.get_text());
282 }
283
284 bool BitmapsDlg::get_rasterize() const { return rasterize_check_.get_active(); }
285
286 Glib::ustring BitmapsDlg::get_active_scope() const {
287 return glyphs_combo_.get_active_id();
288 }
289
290 } // namespace ff::dlg