TextTool.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /*
  2. * Copyright (c) 2022, Timothy Slater <tslater2006@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "TextTool.h"
  7. #include "../ImageEditor.h"
  8. #include "../Layer.h"
  9. #include <LibGUI/Action.h>
  10. #include <LibGUI/BoxLayout.h>
  11. #include <LibGUI/Button.h>
  12. #include <LibGUI/FontPicker.h>
  13. #include <LibGUI/Label.h>
  14. #include <LibGUI/Menu.h>
  15. #include <LibGUI/Painter.h>
  16. #include <LibGUI/TextBox.h>
  17. #include <LibGfx/Bitmap.h>
  18. #include <LibGfx/Palette.h>
  19. namespace PixelPaint {
  20. TextToolEditor::TextToolEditor()
  21. : TextEditor(TextEditor::MultiLine)
  22. {
  23. }
  24. void TextToolEditor::handle_keyevent(Badge<TextTool>, GUI::KeyEvent& event)
  25. {
  26. TextEditor::keydown_event(event);
  27. }
  28. Vector<NonnullRefPtr<GUI::Action>> TextToolEditor::actions()
  29. {
  30. static Vector<NonnullRefPtr<GUI::Action>> actions = { cut_action(), copy_action(), paste_action(), undo_action(), redo_action(), select_all_action() };
  31. return actions;
  32. }
  33. TextTool::TextTool()
  34. {
  35. m_text_editor = TextToolEditor::construct();
  36. m_text_editor->set_wrapping_mode(GUI::TextEditor::WrappingMode::NoWrap);
  37. m_selected_font = Gfx::FontDatabase::default_font();
  38. m_text_editor->set_font(m_selected_font);
  39. m_cursor_blink_timer = Core::Timer::create_repeating(500, [&]() {
  40. m_cursor_blink_state = !m_cursor_blink_state;
  41. }).release_value_but_fixme_should_propagate_errors();
  42. }
  43. void TextTool::on_primary_color_change(Color color)
  44. {
  45. m_text_color = color;
  46. }
  47. void TextTool::on_tool_deactivation()
  48. {
  49. reset_tool();
  50. }
  51. void TextTool::on_mousemove(Layer*, MouseEvent& event)
  52. {
  53. if (m_text_input_is_active) {
  54. auto mouse_position = editor_stroke_position(event.layer_event().position(), 1);
  55. m_mouse_is_over_text = m_ants_rect.contains(mouse_position);
  56. m_editor->update_tool_cursor();
  57. }
  58. if (m_is_dragging) {
  59. auto new_position = event.layer_event().position();
  60. m_add_text_position = m_add_text_position + (new_position - m_drag_start_point);
  61. m_drag_start_point = new_position;
  62. }
  63. }
  64. void TextTool::on_mouseup(Layer*, MouseEvent&)
  65. {
  66. m_is_dragging = false;
  67. }
  68. void TextTool::on_mousedown(Layer*, MouseEvent& event)
  69. {
  70. auto start_text_region = [&] {
  71. m_text_color = m_editor->color_for(event.layer_event());
  72. m_text_input_is_active = true;
  73. m_text_editor->set_text(""sv);
  74. m_add_text_position = event.layer_event().position();
  75. m_editor->image().selection().begin_interactive_selection();
  76. m_cursor_blink_timer->start();
  77. m_editor->update();
  78. };
  79. if (!m_text_input_is_active) {
  80. start_text_region();
  81. return;
  82. }
  83. if (m_mouse_is_over_text) {
  84. m_is_dragging = true;
  85. m_drag_start_point = event.layer_event().position();
  86. } else {
  87. // User clicked somewhere outside the currently edited text region
  88. // apply the current text and then start a new one where they clicked.
  89. apply_text_to_layer();
  90. reset_tool();
  91. start_text_region();
  92. }
  93. }
  94. NonnullRefPtr<GUI::Widget> TextTool::get_properties_widget()
  95. {
  96. if (m_properties_widget)
  97. return *m_properties_widget.ptr();
  98. auto properties_widget = GUI::Widget::construct();
  99. properties_widget->set_layout<GUI::VerticalBoxLayout>();
  100. auto& font_header = properties_widget->add<GUI::Label>("Current Font:"_string);
  101. font_header.set_text_alignment(Gfx::TextAlignment::CenterLeft);
  102. m_font_label = properties_widget->add<GUI::Label>(m_selected_font->human_readable_name());
  103. auto& change_font_button = properties_widget->add<GUI::Button>("Change Font..."_string);
  104. change_font_button.on_click = [this](auto) {
  105. auto picker = GUI::FontPicker::construct(nullptr, m_selected_font, false);
  106. if (picker->exec() == GUI::Dialog::ExecResult::OK) {
  107. m_font_label->set_text(picker->font()->human_readable_name());
  108. m_selected_font = picker->font();
  109. m_text_editor->set_font(m_selected_font);
  110. m_editor->set_focus(true);
  111. }
  112. };
  113. m_properties_widget = properties_widget;
  114. return *m_properties_widget;
  115. }
  116. void TextTool::on_second_paint(Layer const* layer, GUI::PaintEvent& event)
  117. {
  118. if (!m_text_input_is_active)
  119. return;
  120. GUI::Painter painter(*m_editor);
  121. painter.add_clip_rect(event.rect());
  122. painter.translate(editor_layer_location(*layer));
  123. auto typed_text = m_text_editor->text();
  124. auto text_width = max<int>(m_selected_font->width(typed_text), m_selected_font->width(" "sv));
  125. auto text_height = static_cast<int>(ceilf(m_selected_font->preferred_line_height() * max<int>(static_cast<int>(m_text_editor->line_count()), 1)));
  126. auto text_location = editor_stroke_position(m_add_text_position, 1);
  127. // Since ImageEditor can be zoomed in/out, we need to be able to render the preview properly scaled
  128. // GUI::Painter doesn't have a way to draw a font scaled directly, so we draw the text to a bitmap
  129. // and then scale the bitmap and blit the result to the ImageEditor.
  130. auto text_bitmap_result = Gfx::Bitmap::create(Gfx::BitmapFormat::BGRA8888, { text_width, text_height });
  131. if (text_bitmap_result.is_error())
  132. return;
  133. auto text_bitmap = text_bitmap_result.release_value();
  134. auto text_painter = GUI::Painter(text_bitmap);
  135. text_painter.set_font(*m_selected_font);
  136. text_painter.draw_text(Gfx::IntRect { 0, 0, text_width, text_height }, typed_text, Gfx::TextAlignment::TopLeft, m_text_color);
  137. m_text_editor->update();
  138. // Draw selected text (if any)
  139. if (m_text_editor->has_selection()) {
  140. auto selection = m_text_editor->selection();
  141. // Draw selected text for each line...
  142. auto selection_start_line = selection.normalized().start().line();
  143. auto selection_end_line = selection.normalized().end().line();
  144. for (auto i = selection_start_line; i <= selection_end_line; ++i) {
  145. auto start_col = 0;
  146. auto end_col = 0;
  147. // First line of selection.
  148. if (i == selection_start_line) {
  149. if (i < selection_end_line) {
  150. // multiple lines selected. we select from selection start to the end of the line
  151. start_col = m_text_editor->selection().normalized().start().column();
  152. end_col = m_text_editor->line(i).length();
  153. } else {
  154. // only a single line in the selection
  155. start_col = m_text_editor->selection().normalized().start().column();
  156. end_col = m_text_editor->selection().normalized().end().column();
  157. }
  158. } else if (i == selection_end_line) {
  159. // We are highlighting the final line of the selection.
  160. // Start from first char and continue to selection end.
  161. start_col = 0;
  162. end_col = m_text_editor->selection().normalized().end().column();
  163. } else {
  164. // We are between the start and end lines, highlight the whole thing.
  165. start_col = 0;
  166. end_col = m_text_editor->line(i).length();
  167. }
  168. auto line_selection_length = end_col - start_col;
  169. auto selected_string = m_text_editor->line(i).view().substring_view(start_col, line_selection_length);
  170. auto text_before_selection = m_text_editor->line(i).view().substring_view(0, start_col);
  171. auto selected_width = m_selected_font->width(selected_string);
  172. auto selection_x_offset = m_selected_font->width(text_before_selection);
  173. // the + 4 here is because that's how Painter::do_draw_text calculates line height, instead of asking
  174. // the font it's preferred line height. If we don't replicate that here, the letters jump around when they
  175. // get selected.
  176. auto selection_y_offset = static_cast<int>((m_selected_font->pixel_size() + 4) * i);
  177. auto selection_rect = Gfx::IntRect(selection_x_offset, selection_y_offset, selected_width, m_selected_font->preferred_line_height());
  178. text_painter.fill_rect(selection_rect, m_text_editor->palette().selection());
  179. text_painter.draw_text(selection_rect, selected_string, Gfx::TextAlignment::TopLeft, m_text_editor->palette().selection_text());
  180. }
  181. }
  182. auto scaled_width = static_cast<int>(ceilf(m_editor->scale() * static_cast<float>(text_bitmap->width())));
  183. auto scaled_height = static_cast<int>(ceilf(m_editor->scale() * static_cast<float>(text_bitmap->height())));
  184. auto scaled_rect = Gfx::IntRect(text_location.x(), text_location.y(), scaled_width, scaled_height);
  185. scaled_rect.set_location({ text_location.x(), text_location.y() });
  186. painter.draw_scaled_bitmap(scaled_rect, text_bitmap, text_bitmap->rect(), 1.0);
  187. // marching ants box
  188. auto right_padding = static_cast<int>(ceilf(m_selected_font->width(" "sv)));
  189. m_ants_rect = Gfx::IntRect(text_location.translated(-4, -2), { scaled_rect.width() + 4 + right_padding, scaled_rect.height() + 4 });
  190. m_editor->draw_marching_ants(painter, m_ants_rect);
  191. // Draw the blinking cursor.
  192. if (m_cursor_blink_state) {
  193. auto editor_cursor_rect = m_text_editor->cursor_content_rect();
  194. // TextEditor starts left most at 3, for TextTool this ends up putting the cursor in the middle of the letter.
  195. // Looks better if we treat 0 as left most here, so we just translate it to the left.
  196. editor_cursor_rect.translate_by(-3, 0);
  197. // ImageEditor scale is a float, but we are working with int and IntRects.
  198. auto scaled_cursor_x = static_cast<int>(ceilf(m_editor->scale() * static_cast<float>(editor_cursor_rect.x())));
  199. auto scaled_cursor_y = static_cast<int>(ceilf(m_editor->scale() * static_cast<float>(editor_cursor_rect.y())));
  200. auto scaled_cursor_width = static_cast<int>(ceilf(m_editor->scale() * static_cast<float>(editor_cursor_rect.width())));
  201. auto scaled_cursor_height = static_cast<int>(ceilf(m_editor->scale() * static_cast<float>(editor_cursor_rect.height())));
  202. auto scaled_cursor_rect = Gfx::IntRect { scaled_cursor_x + text_location.x(),
  203. scaled_cursor_y + text_location.y(),
  204. scaled_cursor_width,
  205. scaled_cursor_height };
  206. painter.fill_rect(scaled_cursor_rect, m_text_color);
  207. }
  208. }
  209. void TextTool::apply_text_to_layer()
  210. {
  211. auto layer = m_editor->active_layer();
  212. GUI::Painter painter(layer->get_scratch_edited_bitmap());
  213. auto demo_text = m_text_editor->text();
  214. auto text_width = m_selected_font->width(demo_text);
  215. auto text_height = static_cast<int>(ceilf(m_selected_font->preferred_line_height() * static_cast<int>(m_text_editor->line_count())));
  216. painter.set_font(*m_selected_font);
  217. auto text_rect = Gfx::Rect<int>(m_add_text_position, { static_cast<int>(ceilf(text_width)), text_height });
  218. painter.draw_text(text_rect, demo_text, Gfx::TextAlignment::TopLeft, m_text_color);
  219. m_editor->did_complete_action(tool_name());
  220. layer->did_modify_bitmap(text_rect);
  221. }
  222. void TextTool::reset_tool()
  223. {
  224. // This puts the tool back into initial state between text additions (except for selected font/color)
  225. m_text_input_is_active = false;
  226. m_is_dragging = false;
  227. m_mouse_is_over_text = false;
  228. m_text_editor->set_text(""sv);
  229. m_editor->image().selection().end_interactive_selection();
  230. m_cursor_blink_timer->stop();
  231. m_editor->update();
  232. m_editor->update_tool_cursor();
  233. }
  234. bool TextTool::on_keydown(GUI::KeyEvent& event)
  235. {
  236. if (!m_text_input_is_active)
  237. return false;
  238. // Cancels current text entry
  239. if (event.key() == Key_Escape) {
  240. reset_tool();
  241. return true;
  242. }
  243. // A plain Return is treated as accepting the current state and rasterizing to the layer.
  244. // For multi-line text Shift + Enter will add new lines.
  245. if (event.modifiers() == Mod_None && event.key() == Key_Return) {
  246. apply_text_to_layer();
  247. reset_tool();
  248. return true;
  249. }
  250. // Pass key events that would normally be handled by menu shortcuts to our TextEditor subclass.
  251. for (auto& action : m_text_editor->actions()) {
  252. auto const& shortcut = action->shortcut();
  253. if (event.key() == shortcut.key() && event.modifiers() == shortcut.modifiers()) {
  254. action->activate(m_text_editor);
  255. return true;
  256. }
  257. }
  258. // Pass the key event off to our TextEditor subclass which handles all text entry features like
  259. // caret navigation, backspace/delete, etc.
  260. m_text_editor->handle_keyevent({}, event);
  261. m_editor->update();
  262. return true;
  263. }
  264. Variant<Gfx::StandardCursor, NonnullRefPtr<Gfx::Bitmap const>> TextTool::cursor()
  265. {
  266. if (m_mouse_is_over_text)
  267. return Gfx::StandardCursor::Move;
  268. return Gfx::StandardCursor::Arrow;
  269. }
  270. }