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