1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252
|
/* GIMP - The GNU Image Manipulation Program
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
*
* gimppath.c
* Copyright (C) 2002 Simon Budig <simon@gimp.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "config.h"
#include <cairo.h>
#include <gegl.h>
#include <gdk-pixbuf/gdk-pixbuf.h>
#include "libgimpcolor/gimpcolor.h"
#include "libgimpmath/gimpmath.h"
#include "vectors-types.h"
#include "core/gimp.h"
#include "core/gimp-transform-utils.h"
#include "core/gimpbezierdesc.h"
#include "core/gimpchannel-select.h"
#include "core/gimpcontainer.h"
#include "core/gimpcontext.h"
#include "core/gimpdrawable-fill.h"
#include "core/gimpdrawable-stroke.h"
#include "core/gimperror.h"
#include "core/gimpimage.h"
#include "core/gimpimage-undo-push.h"
#include "core/gimppaintinfo.h"
#include "core/gimpstrokeoptions.h"
#include "paint/gimppaintcore-stroke.h"
#include "paint/gimppaintoptions.h"
#include "gimpanchor.h"
#include "gimppath.h"
#include "gimppath-preview.h"
#include "gimpstroke.h"
#include "gimp-intl.h"
enum
{
FREEZE,
THAW,
LAST_SIGNAL
};
static void gimp_path_finalize (GObject *object);
static gint64 gimp_path_get_memsize (GimpObject *object,
gint64 *gui_size);
static gboolean gimp_path_is_attached (GimpItem *item);
static GimpItemTree * gimp_path_get_tree (GimpItem *item);
static gboolean gimp_path_bounds (GimpItem *item,
gdouble *x,
gdouble *y,
gdouble *width,
gdouble *height);
static GimpItem * gimp_path_duplicate (GimpItem *item,
GType new_type);
static void gimp_path_convert (GimpItem *item,
GimpImage *dest_image,
GType old_type);
static void gimp_path_translate (GimpItem *item,
gdouble offset_x,
gdouble offset_y,
gboolean push_undo);
static void gimp_path_scale (GimpItem *item,
gint new_width,
gint new_height,
gint new_offset_x,
gint new_offset_y,
GimpInterpolationType interp_type,
GimpProgress *progress);
static void gimp_path_resize (GimpItem *item,
GimpContext *context,
GimpFillType fill_type,
gint new_width,
gint new_height,
gint offset_x,
gint offset_y);
static void gimp_path_flip (GimpItem *item,
GimpContext *context,
GimpOrientationType flip_type,
gdouble axis,
gboolean clip_result);
static void gimp_path_rotate (GimpItem *item,
GimpContext *context,
GimpRotationType rotate_type,
gdouble center_x,
gdouble center_y,
gboolean clip_result);
static void gimp_path_transform (GimpItem *item,
GimpContext *context,
const GimpMatrix3 *matrix,
GimpTransformDirection direction,
GimpInterpolationType interp_type,
GimpTransformResize clip_result,
GimpProgress *progress);
static GimpTransformResize
gimp_path_get_clip (GimpItem *item,
GimpTransformResize clip_result);
static gboolean gimp_path_fill (GimpItem *item,
GimpDrawable *drawable,
GimpFillOptions *fill_options,
gboolean push_undo,
GimpProgress *progress,
GError **error);
static gboolean gimp_path_stroke (GimpItem *item,
GimpDrawable *drawable,
GimpStrokeOptions *stroke_options,
gboolean push_undo,
GimpProgress *progress,
GError **error);
static void gimp_path_to_selection (GimpItem *item,
GimpChannelOps op,
gboolean antialias,
gboolean feather,
gdouble feather_radius_x,
gdouble feather_radius_y);
static void gimp_path_real_freeze (GimpPath *path);
static void gimp_path_real_thaw (GimpPath *path);
static void gimp_path_real_stroke_add (GimpPath *path,
GimpStroke *stroke);
static void gimp_path_real_stroke_remove (GimpPath *path,
GimpStroke *stroke);
static GimpStroke * gimp_path_real_stroke_get (GimpPath *path,
const GimpCoords *coord);
static GimpStroke *gimp_path_real_stroke_get_next (GimpPath *path,
GimpStroke *prev);
static gdouble gimp_path_real_stroke_get_length (GimpPath *path,
GimpStroke *prev);
static GimpAnchor * gimp_path_real_anchor_get (GimpPath *path,
const GimpCoords *coord,
GimpStroke **ret_stroke);
static void gimp_path_real_anchor_delete (GimpPath *path,
GimpAnchor *anchor);
static gdouble gimp_path_real_get_length (GimpPath *path,
const GimpAnchor *start);
static gdouble gimp_path_real_get_distance (GimpPath *path,
const GimpCoords *coord);
static gint gimp_path_real_interpolate (GimpPath *path,
GimpStroke *stroke,
gdouble precision,
gint max_points,
GimpCoords *ret_coords);
static GimpBezierDesc * gimp_path_make_bezier (GimpPath *path);
static GimpBezierDesc * gimp_path_real_make_bezier (GimpPath *path);
G_DEFINE_TYPE (GimpPath, gimp_path, GIMP_TYPE_ITEM)
#define parent_class gimp_path_parent_class
static guint gimp_path_signals[LAST_SIGNAL] = { 0 };
static void
gimp_path_class_init (GimpPathClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
GimpObjectClass *gimp_object_class = GIMP_OBJECT_CLASS (klass);
GimpViewableClass *viewable_class = GIMP_VIEWABLE_CLASS (klass);
GimpItemClass *item_class = GIMP_ITEM_CLASS (klass);
gimp_path_signals[FREEZE] =
g_signal_new ("freeze",
G_TYPE_FROM_CLASS (klass),
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (GimpPathClass, freeze),
NULL, NULL, NULL,
G_TYPE_NONE, 0);
gimp_path_signals[THAW] =
g_signal_new ("thaw",
G_TYPE_FROM_CLASS (klass),
G_SIGNAL_RUN_FIRST,
G_STRUCT_OFFSET (GimpPathClass, thaw),
NULL, NULL, NULL,
G_TYPE_NONE, 0);
object_class->finalize = gimp_path_finalize;
gimp_object_class->get_memsize = gimp_path_get_memsize;
viewable_class->get_new_preview = gimp_path_get_new_preview;
viewable_class->default_icon_name = "gimp-path";
item_class->is_attached = gimp_path_is_attached;
item_class->get_tree = gimp_path_get_tree;
item_class->bounds = gimp_path_bounds;
item_class->duplicate = gimp_path_duplicate;
item_class->convert = gimp_path_convert;
item_class->translate = gimp_path_translate;
item_class->scale = gimp_path_scale;
item_class->resize = gimp_path_resize;
item_class->flip = gimp_path_flip;
item_class->rotate = gimp_path_rotate;
item_class->transform = gimp_path_transform;
item_class->get_clip = gimp_path_get_clip;
item_class->fill = gimp_path_fill;
item_class->stroke = gimp_path_stroke;
item_class->to_selection = gimp_path_to_selection;
item_class->default_name = _("Path");
item_class->rename_desc = C_("undo-type", "Rename Path");
item_class->translate_desc = C_("undo-type", "Move Path");
item_class->scale_desc = C_("undo-type", "Scale Path");
item_class->resize_desc = C_("undo-type", "Resize Path");
item_class->flip_desc = C_("undo-type", "Flip Path");
item_class->rotate_desc = C_("undo-type", "Rotate Path");
item_class->transform_desc = C_("undo-type", "Transform Path");
item_class->fill_desc = C_("undo-type", "Fill Path");
item_class->stroke_desc = C_("undo-type", "Stroke Path");
item_class->to_selection_desc = C_("undo-type", "Path to Selection");
item_class->reorder_desc = C_("undo-type", "Reorder Path");
item_class->raise_desc = C_("undo-type", "Raise Path");
item_class->raise_to_top_desc = C_("undo-type", "Raise Path to Top");
item_class->lower_desc = C_("undo-type", "Lower Path");
item_class->lower_to_bottom_desc = C_("undo-type", "Lower Path to Bottom");
item_class->raise_failed = _("Path cannot be raised higher.");
item_class->lower_failed = _("Path cannot be lowered more.");
klass->freeze = gimp_path_real_freeze;
klass->thaw = gimp_path_real_thaw;
klass->stroke_add = gimp_path_real_stroke_add;
klass->stroke_remove = gimp_path_real_stroke_remove;
klass->stroke_get = gimp_path_real_stroke_get;
klass->stroke_get_next = gimp_path_real_stroke_get_next;
klass->stroke_get_length = gimp_path_real_stroke_get_length;
klass->anchor_get = gimp_path_real_anchor_get;
klass->anchor_delete = gimp_path_real_anchor_delete;
klass->get_length = gimp_path_real_get_length;
klass->get_distance = gimp_path_real_get_distance;
klass->interpolate = gimp_path_real_interpolate;
klass->make_bezier = gimp_path_real_make_bezier;
}
static void
gimp_path_init (GimpPath *path)
{
gimp_item_set_visible (GIMP_ITEM (path), FALSE, FALSE);
path->strokes = g_queue_new ();
path->stroke_to_list = g_hash_table_new (g_direct_hash, g_direct_equal);
path->last_stroke_id = 0;
path->freeze_count = 0;
path->precision = 0.2;
path->bezier_desc = NULL;
path->bounds_valid = FALSE;
}
static void
gimp_path_finalize (GObject *object)
{
GimpPath *path = GIMP_PATH (object);
if (path->bezier_desc)
{
gimp_bezier_desc_free (path->bezier_desc);
path->bezier_desc = NULL;
}
if (path->strokes)
{
g_queue_free_full (path->strokes, (GDestroyNotify) g_object_unref);
path->strokes = NULL;
}
if (path->stroke_to_list)
{
g_hash_table_destroy (path->stroke_to_list);
path->stroke_to_list = NULL;
}
G_OBJECT_CLASS (parent_class)->finalize (object);
}
static gint64
gimp_path_get_memsize (GimpObject *object,
gint64 *gui_size)
{
GimpPath *path;
GList *list;
gint64 memsize = 0;
path = GIMP_PATH (object);
for (list = path->strokes->head; list; list = g_list_next (list))
memsize += (gimp_object_get_memsize (GIMP_OBJECT (list->data), gui_size) +
sizeof (GList));
return memsize + GIMP_OBJECT_CLASS (parent_class)->get_memsize (object,
gui_size);
}
static gboolean
gimp_path_is_attached (GimpItem *item)
{
GimpImage *image = gimp_item_get_image (item);
return (GIMP_IS_IMAGE (image) &&
gimp_container_have (gimp_image_get_paths (image),
GIMP_OBJECT (item)));
}
static GimpItemTree *
gimp_path_get_tree (GimpItem *item)
{
if (gimp_item_is_attached (item))
{
GimpImage *image = gimp_item_get_image (item);
return gimp_image_get_path_tree (image);
}
return NULL;
}
static gboolean
gimp_path_bounds (GimpItem *item,
gdouble *x,
gdouble *y,
gdouble *width,
gdouble *height)
{
GimpPath *path = GIMP_PATH (item);
if (! path->bounds_valid)
{
GimpStroke *stroke;
path->bounds_empty = TRUE;
path->bounds_x1 = path->bounds_x2 = 0.0;
path->bounds_y1 = path->bounds_y2 = 0.0;
for (stroke = gimp_path_stroke_get_next (path, NULL);
stroke;
stroke = gimp_path_stroke_get_next (path, stroke))
{
GArray *stroke_coords;
gboolean closed;
stroke_coords = gimp_stroke_interpolate (stroke, 1.0, &closed);
if (stroke_coords)
{
GimpCoords point;
gint i;
if (path->bounds_empty && stroke_coords->len > 0)
{
point = g_array_index (stroke_coords, GimpCoords, 0);
path->bounds_x1 = path->bounds_x2 = point.x;
path->bounds_y1 = path->bounds_y2 = point.y;
path->bounds_empty = FALSE;
}
for (i = 0; i < stroke_coords->len; i++)
{
point = g_array_index (stroke_coords, GimpCoords, i);
path->bounds_x1 = MIN (path->bounds_x1, point.x);
path->bounds_y1 = MIN (path->bounds_y1, point.y);
path->bounds_x2 = MAX (path->bounds_x2, point.x);
path->bounds_y2 = MAX (path->bounds_y2, point.y);
}
g_array_free (stroke_coords, TRUE);
}
}
path->bounds_valid = TRUE;
}
*x = path->bounds_x1;
*y = path->bounds_y1;
*width = path->bounds_x2 - path->bounds_x1;
*height = path->bounds_y2 - path->bounds_y1;
return ! path->bounds_empty;
}
static GimpItem *
gimp_path_duplicate (GimpItem *item,
GType new_type)
{
GimpItem *new_item;
g_return_val_if_fail (g_type_is_a (new_type, GIMP_TYPE_PATH), NULL);
new_item = GIMP_ITEM_CLASS (parent_class)->duplicate (item, new_type);
if (GIMP_IS_PATH (new_item))
{
GimpPath *path = GIMP_PATH (item);
GimpPath *new_path = GIMP_PATH (new_item);
gimp_path_copy_strokes (path, new_path);
}
return new_item;
}
static void
gimp_path_convert (GimpItem *item,
GimpImage *dest_image,
GType old_type)
{
gimp_item_set_size (item,
gimp_image_get_width (dest_image),
gimp_image_get_height (dest_image));
GIMP_ITEM_CLASS (parent_class)->convert (item, dest_image, old_type);
}
static void
gimp_path_translate (GimpItem *item,
gdouble offset_x,
gdouble offset_y,
gboolean push_undo)
{
GimpPath *path = GIMP_PATH (item);
GList *list;
gimp_path_freeze (path);
if (push_undo)
gimp_image_undo_push_path_mod (gimp_item_get_image (item),
_("Move Path"),
path);
for (list = path->strokes->head; list; list = g_list_next (list))
{
GimpStroke *stroke = list->data;
gimp_stroke_translate (stroke, offset_x, offset_y);
}
gimp_path_thaw (path);
}
static void
gimp_path_scale (GimpItem *item,
gint new_width,
gint new_height,
gint new_offset_x,
gint new_offset_y,
GimpInterpolationType interpolation_type,
GimpProgress *progress)
{
GimpPath *path = GIMP_PATH (item);
GimpImage *image = gimp_item_get_image (item);
GList *list;
gimp_path_freeze (path);
if (gimp_item_is_attached (item))
gimp_image_undo_push_path_mod (image, NULL, path);
for (list = path->strokes->head; list; list = g_list_next (list))
{
GimpStroke *stroke = list->data;
gimp_stroke_scale (stroke,
(gdouble) new_width / (gdouble) gimp_item_get_width (item),
(gdouble) new_height / (gdouble) gimp_item_get_height (item));
gimp_stroke_translate (stroke, new_offset_x, new_offset_y);
}
GIMP_ITEM_CLASS (parent_class)->scale (item,
gimp_image_get_width (image),
gimp_image_get_height (image),
0, 0,
interpolation_type, progress);
gimp_path_thaw (path);
}
static void
gimp_path_resize (GimpItem *item,
GimpContext *context,
GimpFillType fill_type,
gint new_width,
gint new_height,
gint offset_x,
gint offset_y)
{
GimpPath *path = GIMP_PATH (item);
GimpImage *image = gimp_item_get_image (item);
GList *list;
gimp_path_freeze (path);
if (gimp_item_is_attached (item))
gimp_image_undo_push_path_mod (image, NULL, path);
for (list = path->strokes->head; list; list = g_list_next (list))
{
GimpStroke *stroke = list->data;
gimp_stroke_translate (stroke, offset_x, offset_y);
}
GIMP_ITEM_CLASS (parent_class)->resize (item, context, fill_type,
gimp_image_get_width (image),
gimp_image_get_height (image),
0, 0);
gimp_path_thaw (path);
}
static void
gimp_path_flip (GimpItem *item,
GimpContext *context,
GimpOrientationType flip_type,
gdouble axis,
gboolean clip_result)
{
GimpPath *path = GIMP_PATH (item);
GList *list;
GimpMatrix3 matrix;
gimp_matrix3_identity (&matrix);
gimp_transform_matrix_flip (&matrix, flip_type, axis);
gimp_path_freeze (path);
gimp_image_undo_push_path_mod (gimp_item_get_image (item),
_("Flip Path"),
path);
for (list = path->strokes->head; list; list = g_list_next (list))
{
GimpStroke *stroke = list->data;
gimp_stroke_transform (stroke, &matrix, NULL);
}
gimp_path_thaw (path);
}
static void
gimp_path_rotate (GimpItem *item,
GimpContext *context,
GimpRotationType rotate_type,
gdouble center_x,
gdouble center_y,
gboolean clip_result)
{
GimpPath *path = GIMP_PATH (item);
GList *list;
GimpMatrix3 matrix;
gimp_matrix3_identity (&matrix);
gimp_transform_matrix_rotate (&matrix, rotate_type, center_x, center_y);
gimp_path_freeze (path);
gimp_image_undo_push_path_mod (gimp_item_get_image (item),
_("Rotate Path"),
path);
for (list = path->strokes->head; list; list = g_list_next (list))
{
GimpStroke *stroke = list->data;
gimp_stroke_transform (stroke, &matrix, NULL);
}
gimp_path_thaw (path);
}
static void
gimp_path_transform (GimpItem *item,
GimpContext *context,
const GimpMatrix3 *matrix,
GimpTransformDirection direction,
GimpInterpolationType interpolation_type,
GimpTransformResize clip_result,
GimpProgress *progress)
{
GimpPath *path = GIMP_PATH (item);
GimpMatrix3 local_matrix;
GQueue strokes;
GList *list;
gimp_path_freeze (path);
gimp_image_undo_push_path_mod (gimp_item_get_image (item),
_("Transform Path"),
path);
local_matrix = *matrix;
if (direction == GIMP_TRANSFORM_BACKWARD)
gimp_matrix3_invert (&local_matrix);
g_queue_init (&strokes);
while (! g_queue_is_empty (path->strokes))
{
GimpStroke *stroke = g_queue_peek_head (path->strokes);
g_object_ref (stroke);
gimp_path_stroke_remove (path, stroke);
gimp_stroke_transform (stroke, &local_matrix, &strokes);
g_object_unref (stroke);
}
path->last_stroke_id = 0;
for (list = strokes.head; list; list = g_list_next (list))
{
GimpStroke *stroke = list->data;
gimp_path_stroke_add (path, stroke);
g_object_unref (stroke);
}
g_queue_clear (&strokes);
gimp_path_thaw (path);
}
static GimpTransformResize
gimp_path_get_clip (GimpItem *item,
GimpTransformResize clip_result)
{
return GIMP_TRANSFORM_RESIZE_ADJUST;
}
static gboolean
gimp_path_fill (GimpItem *item,
GimpDrawable *drawable,
GimpFillOptions *fill_options,
gboolean push_undo,
GimpProgress *progress,
GError **error)
{
GimpPath *path = GIMP_PATH (item);
if (g_queue_is_empty (path->strokes))
{
g_set_error_literal (error, GIMP_ERROR, GIMP_FAILED,
_("Not enough points to fill"));
return FALSE;
}
return gimp_drawable_fill_path (drawable, fill_options,
path, push_undo, error);
}
static gboolean
gimp_path_stroke (GimpItem *item,
GimpDrawable *drawable,
GimpStrokeOptions *stroke_options,
gboolean push_undo,
GimpProgress *progress,
GError **error)
{
GimpPath *path = GIMP_PATH (item);
gboolean retval = FALSE;
if (g_queue_is_empty (path->strokes))
{
g_set_error_literal (error, GIMP_ERROR, GIMP_FAILED,
_("Not enough points to stroke"));
return FALSE;
}
switch (gimp_stroke_options_get_method (stroke_options))
{
case GIMP_STROKE_LINE:
retval = gimp_drawable_stroke_path (drawable, stroke_options,
path, push_undo, error);
break;
case GIMP_STROKE_PAINT_METHOD:
{
GimpPaintInfo *paint_info;
GimpPaintCore *core;
GimpPaintOptions *paint_options;
gboolean emulate_dynamics;
paint_info = gimp_context_get_paint_info (GIMP_CONTEXT (stroke_options));
core = g_object_new (paint_info->paint_type, NULL);
paint_options = gimp_stroke_options_get_paint_options (stroke_options);
emulate_dynamics = gimp_stroke_options_get_emulate_dynamics (stroke_options);
retval = gimp_paint_core_stroke_path (core, drawable,
paint_options,
emulate_dynamics,
path, push_undo, error);
g_object_unref (core);
}
break;
default:
g_return_val_if_reached (FALSE);
}
return retval;
}
static void
gimp_path_to_selection (GimpItem *item,
GimpChannelOps op,
gboolean antialias,
gboolean feather,
gdouble feather_radius_x,
gdouble feather_radius_y)
{
GimpPath *path = GIMP_PATH (item);
GimpImage *image = gimp_item_get_image (item);
gimp_channel_select_path (gimp_image_get_mask (image),
GIMP_ITEM_GET_CLASS (item)->to_selection_desc,
path,
op, antialias,
feather, feather_radius_x, feather_radius_x,
TRUE);
}
static void
gimp_path_real_freeze (GimpPath *path)
{
/* release cached bezier representation */
if (path->bezier_desc)
{
gimp_bezier_desc_free (path->bezier_desc);
path->bezier_desc = NULL;
}
/* invalidate bounds */
path->bounds_valid = FALSE;
}
static void
gimp_path_real_thaw (GimpPath *path)
{
gimp_viewable_invalidate_preview (GIMP_VIEWABLE (path));
}
/* public functions */
GimpPath *
gimp_path_new (GimpImage *image,
const gchar *name)
{
GimpPath *path;
g_return_val_if_fail (GIMP_IS_IMAGE (image), NULL);
path = GIMP_PATH (gimp_item_new (GIMP_TYPE_PATH,
image, name,
0, 0,
gimp_image_get_width (image),
gimp_image_get_height (image)));
return path;
}
GimpPath *
gimp_path_get_parent (GimpPath *path)
{
g_return_val_if_fail (GIMP_IS_PATH (path), NULL);
return GIMP_PATH (gimp_viewable_get_parent (GIMP_VIEWABLE (path)));
}
void
gimp_path_freeze (GimpPath *path)
{
g_return_if_fail (GIMP_IS_PATH (path));
path->freeze_count++;
if (path->freeze_count == 1)
g_signal_emit (path, gimp_path_signals[FREEZE], 0);
}
void
gimp_path_thaw (GimpPath *path)
{
g_return_if_fail (GIMP_IS_PATH (path));
g_return_if_fail (path->freeze_count > 0);
path->freeze_count--;
if (path->freeze_count == 0)
g_signal_emit (path, gimp_path_signals[THAW], 0);
}
void
gimp_path_copy_strokes (GimpPath *src_path,
GimpPath *dest_path)
{
g_return_if_fail (GIMP_IS_PATH (src_path));
g_return_if_fail (GIMP_IS_PATH (dest_path));
gimp_path_freeze (dest_path);
g_queue_free_full (dest_path->strokes, (GDestroyNotify) g_object_unref);
dest_path->strokes = g_queue_new ();
g_hash_table_remove_all (dest_path->stroke_to_list);
dest_path->last_stroke_id = 0;
gimp_path_add_strokes (src_path, dest_path);
gimp_path_thaw (dest_path);
}
void
gimp_path_add_strokes (GimpPath *src_path,
GimpPath *dest_path)
{
GList *stroke;
g_return_if_fail (GIMP_IS_PATH (src_path));
g_return_if_fail (GIMP_IS_PATH (dest_path));
gimp_path_freeze (dest_path);
for (stroke = src_path->strokes->head;
stroke != NULL;
stroke = g_list_next (stroke))
{
GimpStroke *newstroke = gimp_stroke_duplicate (stroke->data);
g_queue_push_tail (dest_path->strokes, newstroke);
/* Also add to {stroke: GList node} map */
g_hash_table_insert (dest_path->stroke_to_list,
newstroke,
g_queue_peek_tail_link (dest_path->strokes));
dest_path->last_stroke_id++;
gimp_stroke_set_id (newstroke,
dest_path->last_stroke_id);
}
gimp_path_thaw (dest_path);
}
void
gimp_path_stroke_add (GimpPath *path,
GimpStroke *stroke)
{
g_return_if_fail (GIMP_IS_PATH (path));
g_return_if_fail (GIMP_IS_STROKE (stroke));
gimp_path_freeze (path);
GIMP_PATH_GET_CLASS (path)->stroke_add (path, stroke);
gimp_path_thaw (path);
}
static void
gimp_path_real_stroke_add (GimpPath *path,
GimpStroke *stroke)
{
/*
* Don't prepend into path->strokes. See ChangeLog 2003-05-21
* --Mitch
*/
g_queue_push_tail (path->strokes, g_object_ref (stroke));
/* Also add to {stroke: GList node} map */
g_hash_table_insert (path->stroke_to_list,
stroke,
g_queue_peek_tail_link (path->strokes));
path->last_stroke_id++;
gimp_stroke_set_id (stroke, path->last_stroke_id);
}
void
gimp_path_stroke_remove (GimpPath *path,
GimpStroke *stroke)
{
g_return_if_fail (GIMP_IS_PATH (path));
g_return_if_fail (GIMP_IS_STROKE (stroke));
gimp_path_freeze (path);
GIMP_PATH_GET_CLASS (path)->stroke_remove (path, stroke);
gimp_path_thaw (path);
}
static void
gimp_path_real_stroke_remove (GimpPath *path,
GimpStroke *stroke)
{
GList *list = g_hash_table_lookup (path->stroke_to_list, stroke);
if (list)
{
g_queue_delete_link (path->strokes, list);
g_hash_table_remove (path->stroke_to_list, stroke);
g_object_unref (stroke);
}
}
gint
gimp_path_get_n_strokes (GimpPath *path)
{
g_return_val_if_fail (GIMP_IS_PATH (path), 0);
return g_queue_get_length (path->strokes);
}
GimpStroke *
gimp_path_stroke_get (GimpPath *path,
const GimpCoords *coord)
{
g_return_val_if_fail (GIMP_IS_PATH (path), NULL);
return GIMP_PATH_GET_CLASS (path)->stroke_get (path, coord);
}
static GimpStroke *
gimp_path_real_stroke_get (GimpPath *path,
const GimpCoords *coord)
{
GimpStroke *minstroke = NULL;
gdouble mindist = G_MAXDOUBLE;
GList *list;
for (list = path->strokes->head; list; list = g_list_next (list))
{
GimpStroke *stroke = list->data;
GimpAnchor *anchor = gimp_stroke_anchor_get (stroke, coord);
if (anchor)
{
gdouble dx = coord->x - anchor->position.x;
gdouble dy = coord->y - anchor->position.y;
if (mindist > dx * dx + dy * dy)
{
mindist = dx * dx + dy * dy;
minstroke = stroke;
}
}
}
return minstroke;
}
GimpStroke *
gimp_path_stroke_get_by_id (GimpPath *path,
gint id)
{
GList *list;
g_return_val_if_fail (GIMP_IS_PATH (path), NULL);
for (list = path->strokes->head; list; list = g_list_next (list))
{
if (gimp_stroke_get_id (list->data) == id)
return list->data;
}
return NULL;
}
GimpStroke *
gimp_path_stroke_get_next (GimpPath *path,
GimpStroke *prev)
{
g_return_val_if_fail (GIMP_IS_PATH (path), NULL);
return GIMP_PATH_GET_CLASS (path)->stroke_get_next (path, prev);
}
static GimpStroke *
gimp_path_real_stroke_get_next (GimpPath *path,
GimpStroke *prev)
{
if (! prev)
{
return g_queue_peek_head (path->strokes);
}
else
{
GList *stroke = g_hash_table_lookup (path->stroke_to_list, prev);
g_return_val_if_fail (stroke != NULL, NULL);
return stroke->next ? stroke->next->data : NULL;
}
}
gdouble
gimp_path_stroke_get_length (GimpPath *path,
GimpStroke *stroke)
{
g_return_val_if_fail (GIMP_IS_PATH (path), 0.0);
g_return_val_if_fail (GIMP_IS_STROKE (stroke), 0.0);
return GIMP_PATH_GET_CLASS (path)->stroke_get_length (path, stroke);
}
static gdouble
gimp_path_real_stroke_get_length (GimpPath *path,
GimpStroke *stroke)
{
g_return_val_if_fail (GIMP_IS_PATH (path), 0.0);
g_return_val_if_fail (GIMP_IS_STROKE (stroke), 0.0);
return gimp_stroke_get_length (stroke, path->precision);
}
GimpAnchor *
gimp_path_anchor_get (GimpPath *path,
const GimpCoords *coord,
GimpStroke **ret_stroke)
{
g_return_val_if_fail (GIMP_IS_PATH (path), NULL);
return GIMP_PATH_GET_CLASS (path)->anchor_get (path, coord,
ret_stroke);
}
static GimpAnchor *
gimp_path_real_anchor_get (GimpPath *path,
const GimpCoords *coord,
GimpStroke **ret_stroke)
{
GimpAnchor *minanchor = NULL;
gdouble mindist = -1;
GList *list;
for (list = path->strokes->head; list; list = g_list_next (list))
{
GimpStroke *stroke = list->data;
GimpAnchor *anchor = gimp_stroke_anchor_get (stroke, coord);
if (anchor)
{
gdouble dx = coord->x - anchor->position.x;
gdouble dy = coord->y - anchor->position.y;
if (mindist > dx * dx + dy * dy || mindist < 0)
{
mindist = dx * dx + dy * dy;
minanchor = anchor;
if (ret_stroke)
*ret_stroke = stroke;
}
}
}
return minanchor;
}
void
gimp_path_anchor_delete (GimpPath *path,
GimpAnchor *anchor)
{
g_return_if_fail (GIMP_IS_PATH (path));
g_return_if_fail (anchor != NULL);
GIMP_PATH_GET_CLASS (path)->anchor_delete (path, anchor);
}
static void
gimp_path_real_anchor_delete (GimpPath *path,
GimpAnchor *anchor)
{
}
void
gimp_path_anchor_select (GimpPath *path,
GimpStroke *target_stroke,
GimpAnchor *anchor,
gboolean selected,
gboolean exclusive)
{
GList *list;
for (list = path->strokes->head; list; list = g_list_next (list))
{
GimpStroke *stroke = list->data;
gimp_stroke_anchor_select (stroke,
stroke == target_stroke ? anchor : NULL,
selected, exclusive);
}
}
gdouble
gimp_path_get_length (GimpPath *path,
const GimpAnchor *start)
{
g_return_val_if_fail (GIMP_IS_PATH (path), 0.0);
return GIMP_PATH_GET_CLASS (path)->get_length (path, start);
}
static gdouble
gimp_path_real_get_length (GimpPath *path,
const GimpAnchor *start)
{
g_printerr ("gimp_path_get_length: default implementation\n");
return 0;
}
gdouble
gimp_path_get_distance (GimpPath *path,
const GimpCoords *coord)
{
g_return_val_if_fail (GIMP_IS_PATH (path), 0.0);
return GIMP_PATH_GET_CLASS (path)->get_distance (path, coord);
}
static gdouble
gimp_path_real_get_distance (GimpPath *path,
const GimpCoords *coord)
{
g_printerr ("gimp_path_get_distance: default implementation\n");
return 0;
}
gint
gimp_path_interpolate (GimpPath *path,
GimpStroke *stroke,
gdouble precision,
gint max_points,
GimpCoords *ret_coords)
{
g_return_val_if_fail (GIMP_IS_PATH (path), 0);
return GIMP_PATH_GET_CLASS (path)->interpolate (path, stroke,
precision, max_points,
ret_coords);
}
static gint
gimp_path_real_interpolate (GimpPath *path,
GimpStroke *stroke,
gdouble precision,
gint max_points,
GimpCoords *ret_coords)
{
g_printerr ("gimp_path_interpolate: default implementation\n");
return 0;
}
const GimpBezierDesc *
gimp_path_get_bezier (GimpPath *path)
{
g_return_val_if_fail (GIMP_IS_PATH (path), NULL);
if (! path->bezier_desc)
{
path->bezier_desc = gimp_path_make_bezier (path);
}
return path->bezier_desc;
}
static GimpBezierDesc *
gimp_path_make_bezier (GimpPath *path)
{
return GIMP_PATH_GET_CLASS (path)->make_bezier (path);
}
static GimpBezierDesc *
gimp_path_real_make_bezier (GimpPath *path)
{
GimpStroke *stroke;
GArray *cmd_array;
GimpBezierDesc *ret_bezdesc = NULL;
cmd_array = g_array_new (FALSE, FALSE, sizeof (cairo_path_data_t));
for (stroke = gimp_path_stroke_get_next (path, NULL);
stroke;
stroke = gimp_path_stroke_get_next (path, stroke))
{
GimpBezierDesc *bezdesc = gimp_stroke_make_bezier (stroke);
if (bezdesc)
{
cmd_array = g_array_append_vals (cmd_array, bezdesc->data,
bezdesc->num_data);
gimp_bezier_desc_free (bezdesc);
}
}
if (cmd_array->len > 0)
ret_bezdesc = gimp_bezier_desc_new ((cairo_path_data_t *) cmd_array->data,
cmd_array->len);
g_array_free (cmd_array, FALSE);
return ret_bezdesc;
}
|