| 1 |
|
| 2 |
|
| 3 |
import cairo, math |
| 4 |
from gi.repository import Gdk, GdkPixbuf |
| 5 |
from .message_dialog import DrMessageDialog |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
def utilities_get_rgba_for_xy(surface, x, y): |
| 10 |
|
| 11 |
if x < 0 or x > surface.get_width() or y < 0 or y > surface.get_height(): |
| 12 |
return None |
| 13 |
screenshot = Gdk.pixbuf_get_from_surface(surface, float(x), float(y), 1, 1) |
| 14 |
rgba_vals = screenshot.get_pixels() |
| 15 |
return rgba_vals |
| 16 |
|
| 17 |
def utilities_get_magic_path(surface, x, y, window, coef): |
| 18 |
"""This method tries to build a path defining an area of the same color. It |
| 19 |
will mainly be used to paint this area, or to select it.""" |
| 20 |
cairo_context = cairo.Context(surface) |
| 21 |
old_color = utilities_get_rgba_for_xy(surface, x, y) |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
while (utilities_get_rgba_for_xy(surface, x, y) == old_color) and y > 0: |
| 28 |
y = y - 1 |
| 29 |
y = y + 1 ? |
| 30 |
cairo_context.move_to(x, y) |
| 31 |
(first_x, first_y) = (x, y) |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
direction = 5 |
| 38 |
should_stop = False |
| 39 |
i = 0 |
| 40 |
|
| 41 |
x_shift = [-1 * coef, 0, coef, coef, coef, 0, -1 * coef, -1 * coef] |
| 42 |
y_shift = [-1 * coef, -1 * coef, -1 * coef, 0, coef, coef, coef, 0] |
| 43 |
|
| 44 |
while (not should_stop and i < 50000): |
| 45 |
new_x = -10 |
| 46 |
new_y = -10 |
| 47 |
end_circle = False |
| 48 |
j = 0 |
| 49 |
while (not end_circle) or (j < 8): |
| 50 |
future_x = x+x_shift[direction] |
| 51 |
future_y = y+y_shift[direction] |
| 52 |
if (utilities_get_rgba_for_xy(surface, future_x, future_y) == old_color) \ |
| 53 |
and (future_x > 0) and (future_y > 0) \ |
| 54 |
and (future_x < surface.get_width()) \ |
| 55 |
and (future_y < surface.get_height()-2): |
| 56 |
new_x = future_x |
| 57 |
new_y = future_y |
| 58 |
direction = (direction+1) % 8 |
| 59 |
elif (x != new_x or y != new_y): |
| 60 |
x = new_x+x_shift[direction] |
| 61 |
y = new_y+y_shift[direction] |
| 62 |
end_circle = True |
| 63 |
|
| 64 |
|
| 65 |
j = j+1 |
| 66 |
|
| 67 |
direction = (direction+4) % 8 |
| 68 |
|
| 69 |
if (new_x != -10): |
| 70 |
cairo_context.line_to(x, y) |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
if (i > 10) and (first_x-5 < x < first_x+5) and (first_y-5 < y < first_y+5): |
| 76 |
should_stop = True |
| 77 |
|
| 78 |
i = i + 1 |
| 79 |
if i == 2000: |
| 80 |
dialog, continue_id = launch_infinite_loop_dialog(window) |
| 81 |
result = dialog.run() |
| 82 |
if result == continue_id: |
| 83 |
dialog.destroy() |
| 84 |
else: |
| 85 |
dialog.destroy() |
| 86 |
return |
| 87 |
|
| 88 |
cairo_context.close_path() |
| 89 |
|
| 90 |
return cairo_context.copy_path() |
| 91 |
|
| 92 |
def launch_infinite_loop_dialog(window): |
| 93 |
dialog = DrMessageDialog(window) |
| 94 |
cancel_id = dialog.set_action(_("Cancel"), None) |
| 95 |
continue_id = dialog.set_action(_("Continue"), None, True) |
| 96 |
dialog.add_string(_("The area seems poorly delimited, or is very complex.")) |
| 97 |
dialog.add_string(_("This algorithm may not be able to manage the wanted area.")) |
| 98 |
dialog.add_string(_("Do you want to abort the operation, or to let the tool struggle ?")) |
| 99 |
return dialog, continue_id |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
ARROW_TRIANGLE = [ |
| 106 |
(0.0, 0.0), |
| 107 |
(-2.747, 1.0), |
| 108 |
(-2.747, -1.0), |
| 109 |
] |
| 110 |
|
| 111 |
MIN_ARROW_SCALE = 3 |
| 112 |
|
| 113 |
def utilities_add_arrow_triangle(cairo_context, x2, y2, x1, y1, line_width): |
| 114 |
"""Adds a triangular head to the current path.""" |
| 115 |
if x1 == x2 and y1 == y2: |
| 116 |
line_angle = 0 |
| 117 |
else: |
| 118 |
line_angle = math.atan2(y2 - y1, x2 - x1) |
| 119 |
sin, cos = math.sin(line_angle), math.cos(line_angle) |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
scale = max(line_width * 1.1, MIN_ARROW_SCALE) |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
tête |
| 130 |
|
| 131 |
head = [] |
| 132 |
|
| 133 |
for x, y in ARROW_TRIANGLE: |
| 134 |
p = (x * scale, y * scale) |
| 135 |
p = (p[0] * cos - p[1] * sin, p[0] * sin + p[1] * cos) |
| 136 |
p = (p[0] + x2, p[1] + y2) |
| 137 |
head.append(p) |
| 138 |
|
| 139 |
|
| 140 |
_draw_head(cairo_context, head, True) |
| 141 |
|
| 142 |
|
| 143 |
if cairo_context.get_dash_count() > 0: |
| 144 |
_draw_head(cairo_context, head, False) |
| 145 |
_draw_head(cairo_context, head, False) |
| 146 |
_draw_head(cairo_context, head, False) |
| 147 |
_draw_head(cairo_context, head, False) |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
cairo_context.close_path() |
| 152 |
|
| 153 |
|
| 154 |
def _draw_head(cairo_context, head, first_try): |
| 155 |
if first_try: |
| 156 |
cairo_context.move_to(*head[0]) |
| 157 |
else: |
| 158 |
cairo_context.line_to(*head[0]) |
| 159 |
cairo_context.line_to(*head[1]) |
| 160 |
cairo_context.line_to(*head[2]) |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
def utilities_smooth_path(cairo_context, cairo_path): |
| 166 |
"""Extrapolate a path made of straight lines into a path made of curves. New |
| 167 |
points are added according to the length of the line it replaces, the length |
| 168 |
of the previous one, and the length of the next one.""" |
| 169 |
x1 = y1 = None |
| 170 |
x2 = y2 = None |
| 171 |
x3 = y3 = None |
| 172 |
x4 = y4 = None |
| 173 |
for pts in cairo_path: |
| 174 |
if pts[1] == (): |
| 175 |
continue |
| 176 |
x1, y1, x2, y2, x3, y3, x4, y4 = _next_arc(cairo_context, \ |
| 177 |
x2, y2, x3, y3, x4, y4, pts[1][0], pts[1][1]) |
| 178 |
_next_arc(cairo_context, x2, y2, x3, y3, x4, y4, None, None) |
| 179 |
|
| 180 |
|
| 181 |
def _next_point(x1, y1, x2, y2, dist): |
| 182 |
coef = 0.1 |
| 183 |
dx = x2 - x1 |
| 184 |
dy = y2 - y1 |
| 185 |
angle = math.atan2(dy, dx) |
| 186 |
nx = x2 + math.cos(angle) * dist * coef |
| 187 |
ny = y2 + math.sin(angle) * dist * coef |
| 188 |
return nx, ny |
| 189 |
|
| 190 |
def _next_arc(cairo_context, x1, y1, x2, y2, x3, y3, x4, y4): |
| 191 |
if x2 is None or x3 is None: |
| 192 |
|
| 193 |
return x1, y1, x2, y2, x3, y3, x4, y4 |
| 194 |
dist = math.sqrt( (x2 - x3) * (x2 - x3) + (y2 - y3) * (y2 - y3) ) |
| 195 |
if x1 is None and x4 is None: |
| 196 |
cairo_context.move_to(x2, y2) |
| 197 |
cairo_context.line_to(x3, y3) |
| 198 |
return x1, y1, x2, y2, x3, y3, x4, y4 |
| 199 |
elif x1 is None: |
| 200 |
nx1, ny1 = x2, y2 |
| 201 |
nx2, ny2 = _next_point(x4, y4, x3, y3, dist) |
| 202 |
elif x4 is None: |
| 203 |
nx1, ny1 = _next_point(x1, y1, x2, y2, dist) |
| 204 |
nx2, ny2 = x3, y3 |
| 205 |
else: |
| 206 |
nx1, ny1 = _next_point(x1, y1, x2, y2, dist) |
| 207 |
nx2, ny2 = _next_point(x4, y4, x3, y3, dist) |
| 208 |
cairo_context.curve_to(nx1, ny1, nx2, ny2, x3, y3) |
| 209 |
return x1, y1, x2, y2, x3, y3, x4, y4 |
| 210 |
|
| 211 |
|
| 212 |
|