TextTool.cpp 12 KB

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