Debian Codegraph @

src/tools/classic_tools/tool_highlight.py

raw symbols SCIP
1 # tool_highlight.py
2 #
3 # Copyright 2018-2023 Romain F. T.
4 #
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 import cairo
19 from .tool_pencil import ToolPencil
20 from .abstract_classic_tool import AbstractClassicTool
21
22 class ToolHighlighter(ToolPencil):
23 __gtype_name__ = 'ToolHighlighter'
24
25 def __init__(self, window, **kwargs):
26 # Context: this is the name of a tool, a thick pencil dedicated to
27 # highlight text, for example in screenshots
28 AbstractClassicTool.__init__(self, 'highlight', _("Highlighter"), \
29 'tool-highlight-symbolic', window)
30 self.use_operator = False
31 self._path = None
32 self.add_tool_action_boolean('highlight-alpha', True)
33 self.add_tool_action_boolean('highlight-rigid', True)
34 self.add_tool_action_enum('highlight-bg', 'light')
35
36 def get_editing_tips(self):
37 self._bg_type = self.get_option_value('highlight-bg')
38 self._force_alpha = self.get_option_value('highlight-alpha')
39 self._is_rigid = self.get_option_value('highlight-rigid')
40
41 label_options = self.label + " - "
42 if self._bg_type == 'light':
43 label_options += _("Dark text on light background")
44 label_modifier_shift = _("Press <Shift> to temporarily highlight" + \
45 " on dark background instead")
46 else:
47 label_options += _("Light text on dark background")
48 label_modifier_shift = _("Press <Shift> to temporarily highlight" + \
49 " on light background instead")
50 if self.get_image().get_mouse_is_pressed():
51 label_modifier_shift = None
52
53 full_list = [label_options, label_modifier_shift]
54 return list(filter(None, full_list))
55
56 def get_options_label(self):
57 return _("Highlighter options")
58
59 ############################################################################
60
61 def on_press_on_area(self, event, surface, event_x, event_y):
62 self.set_common_values(event.button, event_x, event_y)
63 self._path = None
64
65 self.update_modifier_state(event.state)
66 if 'SHIFT' in self._modifier_keys:
67 if self._bg_type == 'light':
68 self._bg_type = 'dark'
69 else:
70 self._bg_type = 'light'
71
72 def _add_point(self, event_x, event_y):
73 cairo_context = self.get_context()
74 if self._path is None:
75 cairo_context.move_to(self.x_press, self.y_press)
76 elif self._didnt_really_move(cairo_context, event_x, event_y):
77 length = -1
78 for pts in self._path:
79 if pts[1] == ():
80 continue
81 length += 1
82 # a better technique to find the length probably exists
83 for index, pts in enumerate(self._path):
84 if pts[1] == ():
85 continue
86 if pts[0] == cairo.PathDataType.MOVE_TO:
87 cairo_context.move_to(pts[1][0], pts[1][1])
88 elif index == length:
89 event_x = (pts[1][0] + event_x) / 2
90 event_y = (pts[1][1] + event_y) / 2
91 break
92 else: # if pts[0] == cairo.PathDataType.LINE_TO:
93 cairo_context.line_to(pts[1][0], pts[1][1])
94 cairo_context.line_to(event_x, event_y)
95 self._path = cairo_context.copy_path()
96
97 def _didnt_really_move(self, cairo_context, event_x, event_y):
98 """Tells if the pointer has moved enough to add a new point, otherwise
99 the last point will be changed.
100 It's an option that can be disabled.
101 The context of an highlighter tool means the direction is biased: i will
102 assume the underlying text is written horizontally, and in straight
103 lines; so the highlighting will also be straight, but the chosen line
104 may change during the stroke."""
105 cairo_context.append_path(self._path)
106 if not self._is_rigid:
107 return False
108
109 rigidity = min(self.tool_width, 10.0)
110 if abs(cairo_context.get_current_point()[0] - event_x) > rigidity:
111 return False
112 if abs(cairo_context.get_current_point()[1] - event_y) > rigidity / 5:
113 return False
114
115 cairo_context.new_path()
116 return True
117
118 def on_motion_on_area(self, event, surface, event_x, event_y, render=True):
119 self._add_point(event_x, event_y)
120 if not render:
121 return
122 operation = self.build_operation()
123 self.do_tool_operation(operation)
124
125 def on_release_on_area(self, event, surface, event_x, event_y):
126 self._add_point(event_x, event_y)
127 operation = self.build_operation()
128 self.apply_operation(operation)
129
130 ############################################################################
131
132 def build_operation(self):
133 operation = {
134 'tool_id': self.id,
135 'rgba': self.main_color,
136 'width': self.tool_width,
137 'path': self._path,
138 'bg-type': self._bg_type,
139 'halpha': self._force_alpha
140 }
141 return operation
142
143 def do_tool_operation(self, operation):
144 self.start_tool_operation(operation)
145 if operation['path'] is None:
146 return
147 ccontext = self.get_context()
148 ccontext.set_line_cap(cairo.LineCap.SQUARE)
149 ccontext.set_line_join(cairo.LineJoin.ROUND)
150 ccontext.set_line_width(operation['width'])
151
152 if operation['bg-type'] == 'light':
153 operator = cairo.Operator.MULTIPLY
154 else:
155 operator = cairo.Operator.SCREEN
156 ccontext.set_operator(operator)
157
158 main_color = operation['rgba']
159 if operation['halpha']:
160 main_color[3] = 0.5
161 ccontext.set_source_rgba(*main_color)
162
163 ccontext.append_path(operation['path'])
164 ccontext.stroke()
165
166 ############################################################################
167 ################################################################################
168