Skip to content

Commit

Permalink
Text tool: overlay around unapplied preview #357
Browse files Browse the repository at this point in the history
  • Loading branch information
maoschanz committed Apr 8, 2022
1 parent 7c8e805 commit 7789f21
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/tools/classic_tools/tool_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import cairo
from gi.repository import Gtk, Gdk, GLib, Pango, PangoCairo
from .abstract_classic_tool import AbstractClassicTool
from .utilities_overlay import utilities_show_composite_overlay

class ToolText(AbstractClassicTool):
__gtype_name__ = 'ToolText'
Expand All @@ -28,6 +29,11 @@ def __init__(self, window, **kwargs):
self._should_cancel = False
self._last_click_btn = 1

# These values are found during the operation computation, and are
# re-used when rendering the previewed overlay
self._preview_width = 0
self._preview_height = 0

self.add_tool_action_simple('text-set-font', self._set_font)
self.add_tool_action_boolean('text-bold', False)
self.add_tool_action_boolean('text-italic', False)
Expand Down Expand Up @@ -189,6 +195,26 @@ def _preview_text(self, *args):
operation = self.build_operation()
self.do_tool_operation(operation)

def on_draw_above(self, area, ccontext):
if not self._has_current_text():
return
ccontext.new_path()
ccontext.set_font_size(self.tool_width * 2)
ccontext.move_to(self.x_press, self.y_press)
ext = ccontext.text_extents(self.text_string)

actual_width = self._preview_width
actual_height = self._preview_height

ccontext.move_to(self.x_press, self.y_press)
ccontext.rel_line_to(actual_width, 0)
ccontext.rel_line_to(0, actual_height)
ccontext.rel_line_to(-1 * actual_width, 0)
ccontext.rel_line_to(0, -1 * actual_height)

thickness = self.get_overlay_thickness()
utilities_show_composite_overlay(ccontext, thickness)

def _on_cancel(self, *args):
self._hide_entry()
self._set_string('')
Expand Down Expand Up @@ -285,6 +311,10 @@ def do_tool_operation(self, operation):
self._show_text_at_coords(cairo_context, layout, entire_text, \
text_x, text_y)

ink_rect, logical_rect = layout.get_pixel_extents()
self._preview_width = logical_rect.width
self._preview_height = logical_rect.height

self.non_destructive_show_modif()

def _show_text_at_coords(self, cairo_c, pango_l, text, text_x, text_y):
Expand Down
39 changes: 39 additions & 0 deletions src/utilities/utilities_overlay.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,45 @@
from gi.repository import Gdk, GdkPixbuf
from .selection_manager import NoSelectionPathException

################################################################################
# Classic tools overlay ########################################################

def utilities_show_composite_overlay(ccontext, thickness=1):
ccontext.set_line_width(thickness)
ccontext.set_dash([thickness * 3, thickness * 3])

x1, y1 ,x2, y2 = ccontext.path_extents()
radius = _get_radius((x2 - x1), (y2 - y1), thickness)

radius_with_margin = (radius + 10) * 1.2
ccontext.move_to(x1 - radius_with_margin, y1 - radius_with_margin)
ccontext.line_to(x2 + radius_with_margin, y1 - radius_with_margin)
ccontext.line_to(x2 + radius_with_margin, y2 + radius_with_margin)
ccontext.line_to(x1 - radius_with_margin, y2 + radius_with_margin)
ccontext.close_path()

ccontext.set_fill_rule(cairo.FillRule.EVEN_ODD)

ccontext.set_source_rgba(0.3, 0.3, 0.3, 0.2)
ccontext.fill_preserve()

ccontext.set_source_rgba(0.5, 0.5, 0.8, 1.0)
ccontext.stroke()

# The 4 corner handles
ccontext.set_dash([])
if radius < 4 * thickness:
ccontext.set_line_width(thickness)
elif radius < 8 * thickness:
ccontext.set_line_width(2 * thickness)
else:
ccontext.set_line_width(3 * thickness)

_draw_arc_handle(ccontext, x1, y1, radius, 'nw')
_draw_arc_handle(ccontext, x2, y1, radius, 'ne')
_draw_arc_handle(ccontext, x2, y2, radius, 'se')
_draw_arc_handle(ccontext, x1, y2, radius, 'sw')

################################################################################
# Selection overlay ############################################################

Expand Down

0 comments on commit 7789f21

Please sign in to comment.