FontEditor.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "FontEditor.h"
  27. #include "GlyphEditorWidget.h"
  28. #include "GlyphMapWidget.h"
  29. #include <AK/StringBuilder.h>
  30. #include <LibGUI/BoxLayout.h>
  31. #include <LibGUI/Button.h>
  32. #include <LibGUI/CheckBox.h>
  33. #include <LibGUI/GroupBox.h>
  34. #include <LibGUI/Label.h>
  35. #include <LibGUI/MessageBox.h>
  36. #include <LibGUI/Painter.h>
  37. #include <LibGUI/SpinBox.h>
  38. #include <LibGUI/TextBox.h>
  39. #include <LibGUI/Window.h>
  40. #include <LibGfx/BitmapFont.h>
  41. #include <LibGfx/Palette.h>
  42. #include <stdlib.h>
  43. FontEditorWidget::FontEditorWidget(const String& path, RefPtr<Gfx::BitmapFont>&& edited_font)
  44. : m_edited_font(move(edited_font))
  45. , m_path(path)
  46. {
  47. set_fill_with_background_color(true);
  48. set_layout<GUI::VerticalBoxLayout>();
  49. // Top
  50. auto& main_container = add<GUI::Widget>();
  51. main_container.set_layout<GUI::HorizontalBoxLayout>();
  52. main_container.layout()->set_margins({ 4, 4, 4, 4 });
  53. main_container.set_background_role(Gfx::ColorRole::SyntaxKeyword);
  54. // Top-Left Glyph Editor and info
  55. auto& editor_container = main_container.add<GUI::Widget>();
  56. editor_container.set_layout<GUI::VerticalBoxLayout>();
  57. editor_container.layout()->set_margins({ 4, 4, 4, 4 });
  58. editor_container.set_background_role(Gfx::ColorRole::SyntaxKeyword);
  59. m_glyph_editor_widget = editor_container.add<GlyphEditorWidget>(*m_edited_font);
  60. m_glyph_editor_widget->set_fixed_size(m_glyph_editor_widget->preferred_width(), m_glyph_editor_widget->preferred_height());
  61. editor_container.set_fixed_width(m_glyph_editor_widget->preferred_width());
  62. auto& glyph_width_label = editor_container.add<GUI::Label>();
  63. glyph_width_label.set_fixed_height(22);
  64. glyph_width_label.set_text_alignment(Gfx::TextAlignment::CenterLeft);
  65. glyph_width_label.set_text("Glyph width:");
  66. auto& glyph_width_spinbox = editor_container.add<GUI::SpinBox>();
  67. glyph_width_spinbox.set_min(0);
  68. glyph_width_spinbox.set_max(32);
  69. glyph_width_spinbox.set_value(0);
  70. glyph_width_spinbox.set_enabled(!m_edited_font->is_fixed_width());
  71. auto& info_label = editor_container.add<GUI::Label>();
  72. info_label.set_fixed_height(22);
  73. info_label.set_text_alignment(Gfx::TextAlignment::CenterLeft);
  74. info_label.set_text("info_label");
  75. /// Top-Right glyph map and font meta data
  76. auto& map_and_test_container = main_container.add<GUI::Widget>();
  77. map_and_test_container.set_layout<GUI::VerticalBoxLayout>();
  78. map_and_test_container.layout()->set_margins({ 4, 4, 4, 4 });
  79. m_glyph_map_widget = map_and_test_container.add<GlyphMapWidget>(*m_edited_font);
  80. m_glyph_map_widget->set_fixed_size(m_glyph_map_widget->preferred_width(), m_glyph_map_widget->preferred_height());
  81. auto& font_mtest_group_box = map_and_test_container.add<GUI::GroupBox>();
  82. font_mtest_group_box.set_layout<GUI::VerticalBoxLayout>();
  83. font_mtest_group_box.layout()->set_margins({ 5, 15, 5, 5 });
  84. font_mtest_group_box.set_fixed_height(2 * m_edited_font->glyph_height() + 50);
  85. font_mtest_group_box.set_title("Test");
  86. auto& demo_label_1 = font_mtest_group_box.add<GUI::Label>();
  87. demo_label_1.set_font(m_edited_font);
  88. demo_label_1.set_text("quick fox jumps nightly above wizard.");
  89. auto& demo_label_2 = font_mtest_group_box.add<GUI::Label>();
  90. demo_label_2.set_font(m_edited_font);
  91. demo_label_2.set_text("QUICK FOX JUMPS NIGHTLY ABOVE WIZARD!");
  92. auto& font_metadata_group_box = map_and_test_container.add<GUI::GroupBox>();
  93. font_metadata_group_box.set_layout<GUI::VerticalBoxLayout>();
  94. font_metadata_group_box.layout()->set_margins({ 5, 15, 5, 5 });
  95. font_metadata_group_box.set_fixed_height(275);
  96. font_metadata_group_box.set_title("Font metadata");
  97. //// Name Row
  98. auto& namecontainer = font_metadata_group_box.add<GUI::Widget>();
  99. namecontainer.set_layout<GUI::HorizontalBoxLayout>();
  100. namecontainer.set_fixed_height(22);
  101. auto& name_label = namecontainer.add<GUI::Label>();
  102. name_label.set_fixed_width(100);
  103. name_label.set_text_alignment(Gfx::TextAlignment::CenterLeft);
  104. name_label.set_text("Name:");
  105. auto& name_textbox = namecontainer.add<GUI::TextBox>();
  106. name_textbox.set_text(m_edited_font->name());
  107. name_textbox.on_change = [&] {
  108. m_edited_font->set_name(name_textbox.text());
  109. };
  110. //// Family Row
  111. auto& family_container = font_metadata_group_box.add<GUI::Widget>();
  112. family_container.set_layout<GUI::HorizontalBoxLayout>();
  113. family_container.set_fixed_height(22);
  114. auto& family_label = family_container.add<GUI::Label>();
  115. family_label.set_fixed_width(100);
  116. family_label.set_text_alignment(Gfx::TextAlignment::CenterLeft);
  117. family_label.set_text("Family:");
  118. auto& family_textbox = family_container.add<GUI::TextBox>();
  119. family_textbox.set_text(m_edited_font->family());
  120. family_textbox.on_change = [&] {
  121. m_edited_font->set_family(family_textbox.text());
  122. };
  123. //// Presentation size Row
  124. auto& presentation_size_container = font_metadata_group_box.add<GUI::Widget>();
  125. presentation_size_container.set_layout<GUI::HorizontalBoxLayout>();
  126. presentation_size_container.set_fixed_height(22);
  127. auto& presentation_size_label = presentation_size_container.add<GUI::Label>();
  128. presentation_size_label.set_fixed_width(100);
  129. presentation_size_label.set_text_alignment(Gfx::TextAlignment::CenterLeft);
  130. presentation_size_label.set_text("Presentation size:");
  131. auto& presentation_size_spinbox = presentation_size_container.add<GUI::SpinBox>();
  132. presentation_size_spinbox.set_min(0);
  133. presentation_size_spinbox.set_max(255);
  134. presentation_size_spinbox.set_value(m_edited_font->presentation_size());
  135. //// Weight Row
  136. auto& weight_container = font_metadata_group_box.add<GUI::Widget>();
  137. weight_container.set_layout<GUI::HorizontalBoxLayout>();
  138. weight_container.set_fixed_height(22);
  139. auto& weight_label = weight_container.add<GUI::Label>();
  140. weight_label.set_fixed_width(100);
  141. weight_label.set_text_alignment(Gfx::TextAlignment::CenterLeft);
  142. weight_label.set_text("Weight:");
  143. auto& weight_spinbox = weight_container.add<GUI::SpinBox>();
  144. weight_spinbox.set_min(0);
  145. weight_spinbox.set_max(65535);
  146. weight_spinbox.set_value(m_edited_font->weight());
  147. //// Glyph spacing Row
  148. auto& glyph_spacing_container = font_metadata_group_box.add<GUI::Widget>();
  149. glyph_spacing_container.set_layout<GUI::HorizontalBoxLayout>();
  150. glyph_spacing_container.set_fixed_height(22);
  151. auto& glyph_spacing = glyph_spacing_container.add<GUI::Label>();
  152. glyph_spacing.set_fixed_width(100);
  153. glyph_spacing.set_text_alignment(Gfx::TextAlignment::CenterLeft);
  154. glyph_spacing.set_text("Glyph spacing:");
  155. auto& spacing_spinbox = glyph_spacing_container.add<GUI::SpinBox>();
  156. spacing_spinbox.set_min(0);
  157. spacing_spinbox.set_max(255);
  158. spacing_spinbox.set_value(m_edited_font->glyph_spacing());
  159. //// Glyph Height Row
  160. auto& glyph_height_container = font_metadata_group_box.add<GUI::Widget>();
  161. glyph_height_container.set_layout<GUI::HorizontalBoxLayout>();
  162. glyph_height_container.set_fixed_height(22);
  163. auto& glyph_height = glyph_height_container.add<GUI::Label>();
  164. glyph_height.set_fixed_width(100);
  165. glyph_height.set_text_alignment(Gfx::TextAlignment::CenterLeft);
  166. glyph_height.set_text("Glyph height:");
  167. auto& glyph_height_spinbox = glyph_height_container.add<GUI::SpinBox>();
  168. glyph_height_spinbox.set_min(0);
  169. glyph_height_spinbox.set_max(255);
  170. glyph_height_spinbox.set_value(m_edited_font->glyph_height());
  171. glyph_height_spinbox.set_enabled(false);
  172. //// Glyph width Row
  173. auto& glyph_weight_container = font_metadata_group_box.add<GUI::Widget>();
  174. glyph_weight_container.set_layout<GUI::HorizontalBoxLayout>();
  175. glyph_weight_container.set_fixed_height(22);
  176. auto& glyph_header_width_label = glyph_weight_container.add<GUI::Label>();
  177. glyph_header_width_label.set_fixed_width(100);
  178. glyph_header_width_label.set_text_alignment(Gfx::TextAlignment::CenterLeft);
  179. glyph_header_width_label.set_text("Glyph width:");
  180. auto& glyph_header_width_spinbox = glyph_weight_container.add<GUI::SpinBox>();
  181. glyph_header_width_spinbox.set_min(0);
  182. glyph_header_width_spinbox.set_max(255);
  183. glyph_header_width_spinbox.set_value(m_edited_font->glyph_fixed_width());
  184. glyph_header_width_spinbox.set_enabled(false);
  185. //// Baseline Row
  186. auto& baseline_container = font_metadata_group_box.add<GUI::Widget>();
  187. baseline_container.set_layout<GUI::HorizontalBoxLayout>();
  188. baseline_container.set_fixed_height(22);
  189. auto& baseline_label = baseline_container.add<GUI::Label>();
  190. baseline_label.set_fixed_width(100);
  191. baseline_label.set_text_alignment(Gfx::TextAlignment::CenterLeft);
  192. baseline_label.set_text("Baseline:");
  193. auto& baseline_spinbox = baseline_container.add<GUI::SpinBox>();
  194. baseline_spinbox.set_min(0);
  195. baseline_spinbox.set_max(m_edited_font->glyph_height() - 1);
  196. baseline_spinbox.set_value(m_edited_font->baseline());
  197. //// Mean line Row
  198. auto& mean_line_container = font_metadata_group_box.add<GUI::Widget>();
  199. mean_line_container.set_layout<GUI::HorizontalBoxLayout>();
  200. mean_line_container.set_fixed_height(22);
  201. auto& mean_line_label = mean_line_container.add<GUI::Label>();
  202. mean_line_label.set_fixed_width(100);
  203. mean_line_label.set_text_alignment(Gfx::TextAlignment::CenterLeft);
  204. mean_line_label.set_text("Mean Line:");
  205. auto& mean_line_spinbox = mean_line_container.add<GUI::SpinBox>();
  206. mean_line_spinbox.set_min(0);
  207. mean_line_spinbox.set_max(m_edited_font->glyph_height() - 1);
  208. mean_line_spinbox.set_value(m_edited_font->mean_line());
  209. //// Fixed checkbox Row
  210. auto& fixed_width_checkbox = font_metadata_group_box.add<GUI::CheckBox>();
  211. fixed_width_checkbox.set_text("Fixed width");
  212. fixed_width_checkbox.set_checked(m_edited_font->is_fixed_width());
  213. // Bottom
  214. auto& bottom_container = add<GUI::Widget>();
  215. bottom_container.set_layout<GUI::HorizontalBoxLayout>();
  216. bottom_container.layout()->set_margins({ 8, 0, 8, 8 });
  217. bottom_container.set_fixed_height(32);
  218. bottom_container.layout()->add_spacer();
  219. auto& save_button = bottom_container.add<GUI::Button>();
  220. save_button.set_fixed_size(80, 22);
  221. save_button.set_text("Save");
  222. save_button.on_click = [this](auto) { save_as(m_path); };
  223. auto& quit_button = bottom_container.add<GUI::Button>();
  224. quit_button.set_fixed_size(80, 22);
  225. quit_button.set_text("Quit");
  226. quit_button.on_click = [](auto) {
  227. exit(0);
  228. };
  229. // Event hanglers
  230. auto update_demo = [&] {
  231. demo_label_1.update();
  232. demo_label_2.update();
  233. };
  234. auto calculate_prefed_sizes = [&] {
  235. int right_site_width = m_edited_font->width("QUICK FOX JUMPS NIGHTLY ABOVE WIZARD!") + 20;
  236. right_site_width = max(right_site_width, m_glyph_map_widget->preferred_width());
  237. m_preferred_width = m_glyph_editor_widget->width() + right_site_width + 20;
  238. m_preferred_height = m_glyph_map_widget->relative_rect().height() + 2 * m_edited_font->glyph_height() + 380;
  239. };
  240. m_glyph_editor_widget->on_glyph_altered = [this, update_demo](u8 glyph) {
  241. m_glyph_map_widget->update_glyph(glyph);
  242. update_demo();
  243. };
  244. m_glyph_map_widget->on_glyph_selected = [&](size_t glyph) {
  245. m_glyph_editor_widget->set_glyph(glyph);
  246. glyph_width_spinbox.set_value(m_edited_font->glyph_width(m_glyph_map_widget->selected_glyph()));
  247. StringBuilder builder;
  248. builder.appendff("{:#02x} (", glyph);
  249. if (glyph < 128) {
  250. builder.append(glyph);
  251. } else {
  252. builder.append(128 | 64 | (glyph / 64));
  253. builder.append(128 | (glyph % 64));
  254. }
  255. builder.append(')');
  256. info_label.set_text(builder.to_string());
  257. };
  258. fixed_width_checkbox.on_checked = [&, update_demo](bool checked) {
  259. m_edited_font->set_fixed_width(checked);
  260. glyph_width_spinbox.set_enabled(!m_edited_font->is_fixed_width());
  261. glyph_width_spinbox.set_value(m_edited_font->glyph_width(m_glyph_map_widget->selected_glyph()));
  262. m_glyph_editor_widget->update();
  263. update_demo();
  264. };
  265. glyph_width_spinbox.on_change = [this, update_demo](int value) {
  266. m_edited_font->set_glyph_width(m_glyph_map_widget->selected_glyph(), value);
  267. m_glyph_editor_widget->update();
  268. m_glyph_map_widget->update_glyph(m_glyph_map_widget->selected_glyph());
  269. update_demo();
  270. };
  271. weight_spinbox.on_change = [this, update_demo](int value) {
  272. m_edited_font->set_weight(value);
  273. update_demo();
  274. };
  275. presentation_size_spinbox.on_change = [this, update_demo](int value) {
  276. m_edited_font->set_presentation_size(value);
  277. update_demo();
  278. };
  279. spacing_spinbox.on_change = [this, update_demo](int value) {
  280. m_edited_font->set_glyph_spacing(value);
  281. update_demo();
  282. };
  283. baseline_spinbox.on_change = [this, update_demo](int value) {
  284. m_edited_font->set_baseline(value);
  285. m_glyph_editor_widget->update();
  286. update_demo();
  287. };
  288. mean_line_spinbox.on_change = [this, update_demo](int value) {
  289. m_edited_font->set_mean_line(value);
  290. m_glyph_editor_widget->update();
  291. update_demo();
  292. };
  293. // init widget
  294. calculate_prefed_sizes();
  295. m_glyph_map_widget->set_selected_glyph('A');
  296. }
  297. FontEditorWidget::~FontEditorWidget()
  298. {
  299. }
  300. bool FontEditorWidget::save_as(const String& path)
  301. {
  302. auto ret_val = m_edited_font->write_to_file(path);
  303. if (!ret_val) {
  304. GUI::MessageBox::show(window(), "The font file could not be saved.", "Save failed", GUI::MessageBox::Type::Error);
  305. return false;
  306. }
  307. m_path = path;
  308. return true;
  309. }