bezier_select.c 83 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492
  1. /* The GIMP -- an image manipulation program
  2. * Copyright (C) 1995 Spencer Kimball and Peter Mattis
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. */
  18. #include "config.h"
  19. #include <string.h>
  20. #include <gtk/gtk.h>
  21. #include "apptypes.h"
  22. #include "appenv.h"
  23. #include "cursorutil.h"
  24. #include "draw_core.h"
  25. #include "edit_selection.h"
  26. #include "errors.h"
  27. #include "gdisplay.h"
  28. #include "gimage_mask.h"
  29. #include "rect_select.h"
  30. #include "bezier_select.h"
  31. #include "bezier_selectP.h"
  32. #include "paths_dialogP.h"
  33. #include "selection_options.h"
  34. #include "undo.h"
  35. #include "libgimp/gimpmath.h"
  36. #include "libgimp/gimpintl.h"
  37. /* Bezier extensions made by Raphael FRANCOIS (fraph@ibm.net)
  38. BEZIER_EXTENDS VER 1.0
  39. - work as the cut/copy/paste named functions.
  40. - allow to add/remove/replace bezier curves
  41. - allow to modify the control/anchor points even if the selection is made
  42. - allow to add/remove control/anchor points on a curve
  43. - allow to update a previous saved curve
  44. - cannot operate on open or multiple curves simultaneously
  45. */
  46. #define BEZIER_DRAW_CURVE 1
  47. #define BEZIER_DRAW_CURRENT 2
  48. #define BEZIER_DRAW_HANDLES 4
  49. #define BEZIER_DRAW_ALL (BEZIER_DRAW_CURVE | BEZIER_DRAW_HANDLES)
  50. #define BEZIER_WIDTH 8
  51. #define BEZIER_HALFWIDTH 4
  52. #define SUPERSAMPLE 3
  53. #define SUPERSAMPLE2 9
  54. #define NO 0
  55. #define YES 1
  56. /* the bezier select structures */
  57. typedef double BezierMatrix[4][4];
  58. /* The named paste dialog */
  59. typedef struct _PasteNamedDlg PasteNamedDlg;
  60. struct _PasteNamedDlg
  61. {
  62. GtkWidget *shell;
  63. GtkWidget *list;
  64. GDisplay *gdisp;
  65. };
  66. /* The named buffer structure... */
  67. typedef struct _named_buffer BezierNamedBuffer;
  68. struct _named_buffer
  69. {
  70. BezierSelect *sel;
  71. char * name;
  72. };
  73. typedef struct {
  74. CountCurves curve_count; /* Must be the first element */
  75. gdouble *stroke_points;
  76. gint num_stroke_points; /* num of valid points */
  77. gint len_stroke_points; /* allocated length */
  78. void *next_curve; /* Next curve in list -- we draw all curves
  79. * separately.
  80. */
  81. } BezierRenderPnts;
  82. typedef struct {
  83. CountCurves curve_count; /* Must be the first element */
  84. gboolean firstpnt;
  85. gdouble curdist;
  86. gdouble dist;
  87. gdouble *gradient;
  88. gint *x;
  89. gint *y;
  90. gdouble lastx;
  91. gdouble lasty;
  92. gboolean found;
  93. } BezierDistance;
  94. typedef struct {
  95. CountCurves curve_count; /* Must be the first element */
  96. gint x;
  97. gint y;
  98. gint halfwidth;
  99. gint found;
  100. } BezierCheckPnts;
  101. /* the bezier selection tool options */
  102. static SelectionOptions *bezier_options = NULL;
  103. /* local variables */
  104. static BezierMatrix basis =
  105. {
  106. { -1, 3, -3, 1 },
  107. { 3, -6, 3, 0 },
  108. { -3, 3, 0, 0 },
  109. { 1, 0, 0, 0 },
  110. };
  111. /*
  112. static BezierMatrix basis =
  113. {
  114. { -1/6.0, 3/6.0, -3/6.0, 1/6.0 },
  115. { 3/6.0, -6/6.0, 3/6.0, 0 },
  116. { -3/6.0, 0, 3/6.0, 0 },
  117. { 1/6.0, 4/6.0, 1, 0 },
  118. };
  119. */
  120. /* The named buffer list */
  121. GSList *bezier_named_buffers = NULL;
  122. /* Global Static Variable to maintain informations about the "contexte" */
  123. static BezierSelect * curSel;
  124. static Tool * curTool;
  125. static GDisplay * curGdisp;
  126. static DrawCore * curCore;
  127. static int ModeEdit = EXTEND_NEW;
  128. /* local function prototypes */
  129. static void bezier_select_button_press (Tool *, GdkEventButton *, gpointer);
  130. static void bezier_select_button_release (Tool *, GdkEventButton *, gpointer);
  131. static void bezier_select_motion (Tool *, GdkEventMotion *, gpointer);
  132. static void bezier_select_control (Tool *, ToolAction, gpointer);
  133. static void bezier_select_cursor_update (Tool *, GdkEventMotion *, gpointer);
  134. static void bezier_select_draw (Tool *);
  135. static void bezier_offset_point (BezierPoint *, int, int);
  136. static int bezier_check_point (BezierPoint *, int, int, int);
  137. static void bezier_draw_handles (BezierSelect *, gint);
  138. static void bezier_draw_current (BezierSelect *);
  139. static void bezier_draw_point (BezierSelect *, BezierPoint *, int);
  140. static void bezier_draw_line (BezierSelect *, BezierPoint *, BezierPoint *);
  141. static void bezier_draw_segment (BezierSelect *, BezierPoint *, int, int, BezierPointsFunc,gpointer);
  142. static void bezier_draw_segment_points (BezierSelect *, GdkPoint *, int, gpointer);
  143. static void bezier_compose (BezierMatrix, BezierMatrix, BezierMatrix);
  144. static void bezier_convert (BezierSelect *, GDisplay *, int, int);
  145. static void bezier_convert_points (BezierSelect *, GdkPoint *, int, gpointer);
  146. static void bezier_convert_line (GSList **, int, int, int, int);
  147. static GSList * bezier_insert_in_list (GSList *, int);
  148. static int test_add_point_on_segment (BezierSelect *, BezierPoint *, int, int, int, int, int);
  149. static void bezier_to_sel_internal (BezierSelect *, Tool *, GDisplay *, gint, gint);
  150. static void bezier_stack_points (BezierSelect *, GdkPoint *, int, gpointer);
  151. static gboolean stroke_interpolatable (int, int, int, int, gdouble);
  152. static void bezier_stack_points_aux (GdkPoint *, int, int, gdouble, BezierRenderPnts *);
  153. static gint count_points_on_curve(BezierPoint *);
  154. /* functions */
  155. static void
  156. bezier_select_options_reset ()
  157. {
  158. selection_options_reset (bezier_options);
  159. }
  160. Tool*
  161. tools_new_bezier_select ()
  162. {
  163. Tool * tool;
  164. BezierSelect * private;
  165. /* The tool options */
  166. if (! bezier_options)
  167. {
  168. bezier_options =
  169. selection_options_new (BEZIER_SELECT, bezier_select_options_reset);
  170. tools_register (BEZIER_SELECT, (ToolOptions *) bezier_options);
  171. }
  172. tool = tools_new_tool (BEZIER_SELECT);
  173. private = g_new0 (BezierSelect, 1);
  174. private->num_points = 0;
  175. private->mask = NULL;
  176. private->core = draw_core_new (bezier_select_draw);
  177. bezier_select_reset (private);
  178. tool->scroll_lock = TRUE; /* Disallow scrolling */
  179. tool->preserve = FALSE; /* Don't preserve on drawable change */
  180. tool->private = (void *) private;
  181. tool->button_press_func = bezier_select_button_press;
  182. tool->button_release_func = bezier_select_button_release;
  183. tool->motion_func = bezier_select_motion;
  184. tool->cursor_update_func = bezier_select_cursor_update;
  185. tool->control_func = bezier_select_control;
  186. curCore = private->core;
  187. curSel = private;
  188. curTool = tool;
  189. paths_new_bezier_select_tool ();
  190. return tool;
  191. }
  192. void
  193. tools_free_bezier_select (Tool *tool)
  194. {
  195. BezierSelect * bezier_sel;
  196. bezier_sel = tool->private;
  197. if (tool->state == ACTIVE)
  198. draw_core_stop (bezier_sel->core, tool);
  199. draw_core_free (bezier_sel->core);
  200. bezier_select_reset (bezier_sel);
  201. g_free (bezier_sel);
  202. }
  203. int
  204. bezier_select_load (void *gdisp_ptr,
  205. BezierPoint *pts,
  206. int num_pts,
  207. int closed)
  208. {
  209. GDisplay * gdisp;
  210. Tool * tool;
  211. BezierSelect * bezier_sel;
  212. gdisp = (GDisplay *) gdisp_ptr;
  213. /* select the bezier tool */
  214. gimp_context_set_tool (gimp_context_get_user (), BEZIER_SELECT);
  215. tool = active_tool;
  216. tool->state = ACTIVE;
  217. tool->gdisp_ptr = gdisp_ptr;
  218. bezier_sel = (BezierSelect *) tool->private;
  219. bezier_sel->points = pts;
  220. bezier_sel->last_point = pts->prev;
  221. bezier_sel->num_points = num_pts;
  222. bezier_sel->closed = closed;
  223. bezier_sel->state = BEZIER_EDIT;
  224. bezier_sel->draw = BEZIER_DRAW_ALL;
  225. bezier_convert (bezier_sel, tool->gdisp_ptr, SUBDIVIDE, NO);
  226. draw_core_start (bezier_sel->core, gdisp->canvas->window, tool);
  227. return 1;
  228. }
  229. static BezierPoint *
  230. valid_curve_segment(BezierPoint *points)
  231. {
  232. /* Valid curve segment is made up of four points */
  233. if(points &&
  234. points->next &&
  235. points->next->next &&
  236. points->next->next->next )
  237. return(points);
  238. return (NULL);
  239. }
  240. static BezierPoint *
  241. next_anchor(BezierPoint *points,BezierPoint **next_curve)
  242. {
  243. int loop;
  244. *next_curve = NULL;
  245. if(!points)
  246. return (NULL);
  247. for(loop = 0; loop < 3; loop++)
  248. {
  249. points = points->next;
  250. if (!points)
  251. return (NULL);
  252. if(points->next_curve)
  253. {
  254. *next_curve = points->next_curve;
  255. }
  256. }
  257. return valid_curve_segment(points);
  258. }
  259. void
  260. bezier_draw_curve (BezierSelect *bezier_sel,
  261. BezierPointsFunc func,
  262. gint coord,
  263. gpointer udata)
  264. {
  265. BezierPoint * points;
  266. BezierPoint * start_pt;
  267. BezierPoint * next_curve;
  268. CountCurves * cnt = (CountCurves *)udata;
  269. gint point_counts = 0;
  270. /* printSel(bezier_sel); */
  271. if(cnt)
  272. cnt->count = 0;
  273. points = bezier_sel->points;
  274. start_pt = bezier_sel->points;
  275. if(bezier_sel->num_points >= 4)
  276. {
  277. do {
  278. point_counts = count_points_on_curve(points);
  279. if (point_counts >= 4)
  280. {
  281. do {
  282. bezier_draw_segment (bezier_sel, points,
  283. SUBDIVIDE, coord,
  284. func,
  285. udata);
  286. points = next_anchor (points, &next_curve);
  287. /* g_print ("next_anchor = %p\n",points); */
  288. } while (points != start_pt && points);
  289. if(cnt)
  290. cnt->count++;
  291. start_pt = next_curve;
  292. points = next_curve;
  293. }
  294. else
  295. break; /* must be last curve since only this one is allowed < 4
  296. * points.
  297. */
  298. } while (next_curve);
  299. }
  300. }
  301. void
  302. bezier_select_reset (BezierSelect *bezier_sel)
  303. {
  304. BezierPoint * points;
  305. BezierPoint * start_pt;
  306. BezierPoint * temp_pt;
  307. BezierPoint * next_curve;
  308. if (bezier_sel->num_points > 0)
  309. {
  310. points = bezier_sel->points;
  311. start_pt = bezier_sel->points;
  312. do {
  313. do {
  314. temp_pt = points;
  315. next_curve = points->next_curve;
  316. points = points->next;
  317. if(points != start_pt && next_curve)
  318. {
  319. g_warning("Curve points out of sync");
  320. }
  321. g_free (temp_pt);
  322. } while (points != start_pt && points);
  323. points = next_curve;
  324. start_pt = next_curve;
  325. } while (next_curve);
  326. }
  327. if (bezier_sel->mask)
  328. channel_delete (bezier_sel->mask);
  329. bezier_sel->state = BEZIER_START; /* we are starting the curve */
  330. bezier_sel->draw = BEZIER_DRAW_ALL; /* draw everything by default */
  331. bezier_sel->closed = 0; /* the curve is initally open */
  332. bezier_sel->points = NULL; /* initially there are no points */
  333. bezier_sel->cur_anchor = NULL;
  334. bezier_sel->cur_control = NULL;
  335. bezier_sel->last_point = NULL;
  336. bezier_sel->num_points = 0; /* intially there are no points */
  337. bezier_sel->mask = NULL; /* empty mask */
  338. bezier_sel->scanlines = NULL;
  339. }
  340. void
  341. bezier_select_free (BezierSelect *bezier_sel)
  342. {
  343. bezier_select_reset (bezier_sel);
  344. g_free (bezier_sel);
  345. }
  346. /* Find the curve that points to this curve. This makes to_check point
  347. * the start of a curve.
  348. */
  349. static BezierPoint *
  350. check_for_next_curve(BezierSelect *bezier_sel,
  351. BezierPoint *to_check)
  352. {
  353. BezierPoint *points = bezier_sel->points;
  354. gint num_points = bezier_sel->num_points;
  355. while (points && num_points)
  356. {
  357. if(points->next_curve == to_check)
  358. return (points);
  359. if(points->next_curve)
  360. points = points->next_curve;
  361. else
  362. points = points->next;
  363. num_points--;
  364. }
  365. return (NULL);
  366. }
  367. static gint
  368. count_points_on_curve(BezierPoint *points)
  369. {
  370. BezierPoint *start = points;
  371. gint count = 0;
  372. while(points && points->next != start)
  373. {
  374. points = points->next;
  375. count++;
  376. }
  377. return (count);
  378. }
  379. /* Find the start of the last open curve, if curve already closed
  380. * this is an error..
  381. */
  382. static BezierPoint *
  383. find_start_open_curve(BezierSelect *bsel)
  384. {
  385. BezierPoint *start_pnt = NULL;
  386. BezierPoint *this_pnt = bsel->last_point;
  387. /* Could be one of the first points */
  388. if(!bsel->last_point)
  389. {
  390. return NULL;
  391. }
  392. if(bsel->closed)
  393. {
  394. g_message(_("Bezier path already closed."));
  395. return NULL;
  396. }
  397. /* Step backwards until the prev point is null.
  398. * in this case this is the start of the open curve.
  399. * The start_pnt stuff is to stop us going around forever.
  400. * If we even have a closed curve then we are in seroius
  401. * trouble.
  402. */
  403. while(this_pnt->prev && start_pnt != this_pnt)
  404. {
  405. if(!start_pnt)
  406. start_pnt = this_pnt;
  407. this_pnt = this_pnt->prev;
  408. }
  409. /* Must be an anchor to be the start */
  410. if(start_pnt == this_pnt || this_pnt->type != BEZIER_ANCHOR)
  411. {
  412. g_message(_("Corrupt curve"));
  413. return NULL;
  414. }
  415. /* g_print ("Returned start pnt of curve %p is %p\n",bsel->last_point,this_pnt); */
  416. return (this_pnt);
  417. }
  418. /* Delete a whole curve. Watch out for special cases.
  419. * start_pnt must always be the start point if the curve to delete.
  420. */
  421. static void
  422. delete_whole_curve(BezierSelect *bezier_sel,
  423. BezierPoint *start_pnt)
  424. {
  425. BezierPoint *tmppnt;
  426. BezierPoint *next_curve = NULL; /* Next curve this one
  427. * points at (if any)
  428. */
  429. BezierPoint *prev_curve; /* Does a curve point to this one? */
  430. gint cnt_pnts = 0; /* Count how many pnts deleted */
  431. gint reset_last = FALSE;
  432. /* shift and del means delete whole curve */
  433. /* Three cases, this is first curve, middle curve
  434. * or end curve.
  435. */
  436. /* Does this curve have another chained on the end?
  437. * or is this curve pointed to another one?
  438. */
  439. /* g_print ("delete_whole_curve::\n"); */
  440. tmppnt = start_pnt;
  441. do {
  442. if(tmppnt->next_curve)
  443. {
  444. next_curve = tmppnt->next_curve;
  445. break;
  446. }
  447. tmppnt = tmppnt->next;
  448. } while (tmppnt != start_pnt && tmppnt);
  449. prev_curve = check_for_next_curve(bezier_sel,start_pnt);
  450. /* First curve ?*/
  451. if(bezier_sel->points == start_pnt)
  452. bezier_sel->points = next_curve;
  453. else
  454. {
  455. /* better have a previous curve else how did we get here? */
  456. prev_curve->next_curve = next_curve;
  457. }
  458. /* start_pnt points to the curve we should free .. ignoring the next_curve */
  459. tmppnt = start_pnt;
  460. do {
  461. BezierPoint *fpnt;
  462. fpnt = tmppnt;
  463. tmppnt = tmppnt->next;
  464. if(fpnt == bezier_sel->last_point)
  465. reset_last = TRUE;
  466. g_free (fpnt);
  467. cnt_pnts++;
  468. } while (tmppnt != start_pnt && tmppnt);
  469. bezier_sel->num_points -= cnt_pnts;
  470. /* if deleted curve was unclosed then must have been the last curve
  471. * and thus this curve becomes closed.
  472. */
  473. if(!tmppnt && bezier_sel->num_points > 0)
  474. {
  475. bezier_sel->closed = 1;
  476. bezier_sel->state = BEZIER_EDIT;
  477. }
  478. if(bezier_sel->num_points <= 0)
  479. {
  480. bezier_select_reset(bezier_sel);
  481. }
  482. bezier_sel->cur_anchor = NULL;
  483. bezier_sel->cur_control = NULL;
  484. /* The last point could have been on this curve as well... */
  485. if(reset_last)
  486. {
  487. BezierPoint *points = bezier_sel->points;
  488. BezierPoint *l_pnt = NULL;
  489. gint num_points = bezier_sel->num_points;
  490. while (points && num_points)
  491. {
  492. l_pnt = points;
  493. if(points->next_curve)
  494. points = points->next_curve;
  495. else
  496. points = points->next;
  497. num_points--;
  498. }
  499. bezier_sel->last_point = l_pnt;
  500. }
  501. }
  502. static gint
  503. bezier_edit_point_on_curve(int x,
  504. int y,
  505. int halfwidth,
  506. GDisplay *gdisp,
  507. BezierSelect *bezier_sel,
  508. Tool *tool,
  509. GdkEventButton *bevent)
  510. {
  511. gint grab_pointer = 0;
  512. BezierPoint *next_curve;
  513. BezierPoint *points = bezier_sel->points;
  514. BezierPoint *start_pt = bezier_sel->points;
  515. BezierPoint *last_curve = NULL;
  516. gint point_counts = 0;
  517. /* find if the button press occurred on a point */
  518. do {
  519. point_counts = count_points_on_curve(points);
  520. do {
  521. if (bezier_check_point (points, x, y, halfwidth))
  522. {
  523. BezierPoint *finded=points;
  524. BezierPoint *start_op;
  525. BezierPoint *end_op;
  526. if (ModeEdit== EXTEND_REMOVE)
  527. {
  528. if((bevent->state & GDK_SHIFT_MASK) || (point_counts <= 7))
  529. {
  530. /* Case 1: GDK_SHIFT_MASK - The user explicitly wishes the present
  531. curve to go away.
  532. Case 2: The current implementation cannot cope with less than
  533. 7 points ie: 2 anchors points and 4 controls: the minimal closed curve.
  534. Since the user wishes less than this implementation minimum,
  535. we take this for an implicit wish that the entire curve go away.
  536. G'bye dear curve.
  537. */
  538. delete_whole_curve(bezier_sel,start_pt);
  539. return (1);
  540. }
  541. else if(!finded->prev || !finded->prev->prev)
  542. {
  543. /* This is the first point on the curve */
  544. /* Clear current anchor and control */
  545. bezier_sel->cur_anchor = NULL;
  546. bezier_sel->cur_control = NULL;
  547. /* Where are we? reset to first point... */
  548. /*if(last_curve == NULL)*/
  549. if(start_pt == bezier_sel->points)
  550. {
  551. finded = bezier_sel->points;
  552. bezier_sel->points = finded->next->next->next;
  553. bezier_sel->points->prev = NULL;
  554. }
  555. else
  556. {
  557. finded = last_curve->next_curve;
  558. last_curve->next_curve = finded->next->next->next;
  559. last_curve->next_curve->prev = NULL;
  560. }
  561. bezier_sel->num_points -= 3;
  562. g_free( finded->next->next );
  563. g_free( finded->next );
  564. g_free( finded );
  565. }
  566. else if(!bezier_sel->closed &&
  567. (finded == bezier_sel->last_point ||
  568. finded == bezier_sel->last_point->prev ||
  569. finded == bezier_sel->last_point->prev->prev))
  570. {
  571. /* This is the last point on the curve */
  572. /* Clear current anchor and control */
  573. /* Where are we? reset to last point... */
  574. finded = bezier_sel->last_point->prev;
  575. bezier_sel->last_point = finded->prev->prev;
  576. bezier_sel->last_point->next = NULL;
  577. bezier_sel->cur_anchor = NULL;
  578. bezier_sel->cur_control = NULL;
  579. bezier_sel->num_points -= 3;
  580. g_free( finded->prev );
  581. g_free( finded->next );
  582. g_free( finded );
  583. }
  584. else
  585. {
  586. if ( finded->type == BEZIER_CONTROL )
  587. {
  588. if (finded->next->type == BEZIER_CONTROL)
  589. finded = finded->prev;
  590. else
  591. finded = finded->next;
  592. }
  593. start_op = finded->prev->prev;
  594. end_op = finded->next->next;
  595. /* we can use next_curve here since we are going to
  596. * drop out the bottom anyways.
  597. */
  598. next_curve =
  599. check_for_next_curve(bezier_sel,finded);
  600. if(next_curve)
  601. {
  602. /* Deleteing first point of next curve*/
  603. next_curve->next_curve = finded->prev->prev->prev;
  604. }
  605. else /* Can't be both first and a next curve!*/
  606. {
  607. if (bezier_sel->points == finded)
  608. {
  609. /* g_print ("Deleting first point %p\n",finded); */
  610. bezier_sel->points = finded->prev->prev->prev;
  611. }
  612. }
  613. /* Make sure the chain of curves is preserved */
  614. if(finded->prev->next_curve)
  615. {
  616. /* g_print ("Moving curve on next_curve %p\n",finded->prev->next_curve); */
  617. /* last point on closed multi-path */
  618. finded->prev->prev->prev->prev->next_curve = finded->prev->next_curve;
  619. }
  620. if(bezier_sel->last_point == finded->prev)
  621. {
  622. /* g_print ("Deleting last point %p\n",finded->prev); */
  623. bezier_sel->last_point = bezier_sel->last_point->prev->prev->prev;
  624. }
  625. start_op->next = end_op;
  626. end_op->prev = start_op;
  627. /* if ( (bezier_sel->last_point == finded) || */
  628. /* (bezier_sel->last_point == finded->next) || */
  629. /* (bezier_sel->last_point == finded->prev)) */
  630. /* { */
  631. /* bezier_sel->last_point = start_op->prev->prev; */
  632. /* bezier_sel->points = start_op->prev; */
  633. /* } */
  634. bezier_sel->num_points -= 3;
  635. g_free( finded->prev );
  636. g_free( finded->next );
  637. g_free( finded );
  638. /* Clear current anchor and control */
  639. bezier_sel->cur_anchor = NULL;
  640. bezier_sel->cur_control = NULL;
  641. }
  642. }
  643. else
  644. {
  645. /* set the current anchor and control points */
  646. switch (points->type)
  647. {
  648. case BEZIER_ANCHOR:
  649. bezier_sel->cur_anchor = points;
  650. bezier_sel->cur_control = bezier_sel->cur_anchor->next;
  651. break;
  652. case BEZIER_CONTROL:
  653. bezier_sel->cur_control = points;
  654. if (bezier_sel->cur_control->next &&
  655. bezier_sel->cur_control->next->type == BEZIER_ANCHOR)
  656. bezier_sel->cur_anchor = bezier_sel->cur_control->next;
  657. else
  658. bezier_sel->cur_anchor = bezier_sel->cur_control->prev;
  659. break;
  660. }
  661. }
  662. return(1);
  663. }
  664. next_curve = points->next_curve;
  665. if(next_curve)
  666. last_curve = points;
  667. points = points->next;
  668. } while (points != start_pt && points);
  669. start_pt = next_curve;
  670. points = next_curve;
  671. } while (next_curve);
  672. return grab_pointer;
  673. }
  674. static gint
  675. bezier_add_point_on_segment(int x,
  676. int y,
  677. int halfwidth,
  678. GDisplay *gdisp,
  679. BezierSelect *bezier_sel,
  680. Tool *tool)
  681. {
  682. BezierPoint *points = bezier_sel->points;
  683. BezierPoint *start_pt = bezier_sel->points;
  684. BezierPoint *next_curve;
  685. do {
  686. do {
  687. if (test_add_point_on_segment (bezier_sel,
  688. points,
  689. SUBDIVIDE,
  690. IMAGE_COORDS,
  691. x, y,
  692. halfwidth))
  693. {
  694. return 1;
  695. }
  696. points = points->next;
  697. if(!points)
  698. return 0;
  699. points = points->next;
  700. if(!points)
  701. return 0;
  702. next_curve = points->next_curve;
  703. points = points->next;
  704. } while (points != start_pt && points);
  705. start_pt = next_curve;
  706. points = next_curve;
  707. } while (next_curve);
  708. return 0;
  709. }
  710. static void
  711. bezier_start_new_segment(BezierSelect *bezier_sel,gint x,gint y)
  712. {
  713. /* Must be closed to do this! */
  714. if(!bezier_sel->closed)
  715. return;
  716. bezier_sel->closed = 0; /* End is no longer closed !*/
  717. bezier_sel->state = BEZIER_ADD;
  718. bezier_add_point (bezier_sel, BEZIER_MOVE, (gdouble)x, (gdouble)y);
  719. bezier_add_point (bezier_sel, BEZIER_CONTROL, (gdouble)x, (gdouble)y);
  720. }
  721. static void
  722. bezier_select_button_press (Tool *tool,
  723. GdkEventButton *bevent,
  724. gpointer gdisp_ptr)
  725. {
  726. GDisplay *gdisp;
  727. BezierSelect *bezier_sel;
  728. BezierPoint *points;
  729. BezierPoint *start_pt;
  730. BezierPoint *curve_start;
  731. int grab_pointer;
  732. int op, replace;
  733. int x, y;
  734. int halfwidth, dummy;
  735. gdisp = (GDisplay *) gdisp_ptr;
  736. tool->drawable = gimage_active_drawable (gdisp->gimage);
  737. bezier_sel = tool->private;
  738. grab_pointer = 0;
  739. if (bezier_options->extend)
  740. {
  741. tool->gdisp_ptr = gdisp_ptr;
  742. }
  743. else
  744. {
  745. /* If the tool was being used in another image...reset it */
  746. if (tool->state == ACTIVE && gdisp_ptr != tool->gdisp_ptr) {
  747. draw_core_stop(bezier_sel->core, tool);
  748. bezier_select_reset (bezier_sel);
  749. }
  750. }
  751. gdisplay_untransform_coords (gdisp, bevent->x, bevent->y, &x, &y, TRUE, 0);
  752. /* get halfwidth in image coord */
  753. gdisplay_untransform_coords (gdisp, bevent->x + BEZIER_HALFWIDTH, 0, &halfwidth, &dummy, TRUE, 0);
  754. halfwidth -= x;
  755. curTool = active_tool;
  756. curSel = curTool->private;
  757. curGdisp = (GDisplay *) gdisp_ptr;
  758. active_tool->gdisp_ptr = gdisp_ptr;
  759. curCore = bezier_sel->core;
  760. switch (bezier_sel->state)
  761. {
  762. case BEZIER_START:
  763. if(ModeEdit != EXTEND_NEW)
  764. break;
  765. grab_pointer = 1;
  766. tool->state = ACTIVE;
  767. tool->gdisp_ptr = gdisp_ptr;
  768. /* if (bevent->state & GDK_MOD1_MASK) */
  769. /* { */
  770. /* init_edit_selection (tool, gdisp_ptr, bevent, EDIT_MASK_TRANSLATE); */
  771. /* break; */
  772. /* } */
  773. /* else if (!(bevent->state & GDK_SHIFT_MASK) && !(bevent->state & GDK_CONTROL_MASK)) */
  774. /* if (! (layer_is_floating_sel (gimage_get_active_layer (gdisp->gimage))) && */
  775. /* gdisplay_mask_value (gdisp, bevent->x, bevent->y) > HALF_WAY) */
  776. /* { */
  777. /* init_edit_selection (tool, gdisp_ptr, bevent, EDIT_MASK_TO_LAYER_TRANSLATE); */
  778. /* break; */
  779. /* } */
  780. bezier_sel->state = BEZIER_ADD;
  781. /*bezier_sel->draw = BEZIER_DRAW_CURVE; | BEZIER_DRAW_HANDLES;*/
  782. bezier_sel->draw = BEZIER_DRAW_CURRENT;
  783. bezier_add_point (bezier_sel, BEZIER_ANCHOR, (gdouble)x, (gdouble)y);
  784. bezier_add_point (bezier_sel, BEZIER_CONTROL, (gdouble)x, (gdouble)y);
  785. draw_core_start (bezier_sel->core, gdisp->canvas->window, tool);
  786. break;
  787. case BEZIER_ADD:
  788. grab_pointer = 1;
  789. if(ModeEdit == EXTEND_EDIT)
  790. {
  791. /* erase the handles */
  792. if(bezier_sel->closed)
  793. bezier_sel->draw = BEZIER_DRAW_ALL;
  794. else
  795. bezier_sel->draw = BEZIER_DRAW_CURVE;
  796. draw_core_pause (bezier_sel->core, tool);
  797. /* unset the current anchor and control */
  798. bezier_sel->cur_anchor = NULL;
  799. bezier_sel->cur_control = NULL;
  800. grab_pointer = bezier_edit_point_on_curve(x,y,halfwidth,gdisp,bezier_sel,tool,bevent);
  801. bezier_sel->draw = BEZIER_DRAW_ALL;
  802. draw_core_resume (bezier_sel->core, tool);
  803. if (grab_pointer)
  804. gdk_pointer_grab (gdisp->canvas->window, FALSE,
  805. GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON1_MOTION_MASK | GDK_BUTTON_RELEASE_MASK,
  806. NULL, NULL, bevent->time);
  807. else
  808. {
  809. paths_dialog_set_default_op();
  810. /* recursive call */
  811. bezier_select_button_press(tool,bevent,gdisp_ptr);
  812. }
  813. return;
  814. }
  815. if(ModeEdit == EXTEND_REMOVE)
  816. {
  817. /* if(bezier_sel->num_points < 6) */
  818. /* return; */
  819. /* erase the handles */
  820. bezier_sel->draw = BEZIER_DRAW_ALL;
  821. draw_core_pause (bezier_sel->core, tool);
  822. /* unset the current anchor and control */
  823. bezier_sel->cur_anchor = NULL;
  824. bezier_sel->cur_control = NULL;
  825. /*kkk*/
  826. bezier_sel->draw = BEZIER_DRAW_ALL;
  827. draw_core_resume (bezier_sel->core, tool);
  828. bezier_sel->draw = BEZIER_DRAW_ALL;
  829. draw_core_pause (bezier_sel->core, tool);
  830. /*kkk*/
  831. grab_pointer = bezier_edit_point_on_curve(x,y,halfwidth,gdisp,bezier_sel,tool,bevent);
  832. bezier_sel->draw = BEZIER_DRAW_ALL;
  833. draw_core_resume (bezier_sel->core, tool);
  834. if(bezier_sel->num_points == 0)
  835. {
  836. draw_core_pause(bezier_sel->core, tool);
  837. paths_dialog_set_default_op();
  838. }
  839. if(!grab_pointer)
  840. {
  841. paths_dialog_set_default_op();
  842. /* recursive call */
  843. bezier_select_button_press(tool,bevent,gdisp_ptr);
  844. }
  845. return;
  846. }
  847. if(ModeEdit == EXTEND_ADD)
  848. {
  849. if(bezier_sel->num_points < 5)
  850. return;
  851. bezier_sel->draw = BEZIER_DRAW_ALL;
  852. draw_core_pause (bezier_sel->core, tool);
  853. grab_pointer = bezier_add_point_on_segment(x,y,halfwidth,gdisp,bezier_sel,tool);
  854. bezier_sel->draw = BEZIER_DRAW_ALL;
  855. draw_core_resume (bezier_sel->core, tool);
  856. if (grab_pointer)
  857. gdk_pointer_grab (gdisp->canvas->window, FALSE,
  858. GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON1_MOTION_MASK | GDK_BUTTON_RELEASE_MASK,
  859. NULL, NULL, bevent->time);
  860. else
  861. {
  862. paths_dialog_set_default_op();
  863. /* recursive call */
  864. bezier_select_button_press(tool,bevent,gdisp_ptr);
  865. }
  866. return;
  867. }
  868. if(bezier_sel->cur_anchor)
  869. {
  870. if (bezier_check_point (bezier_sel->cur_anchor, x, y, halfwidth))
  871. {
  872. break;
  873. }
  874. if (bezier_sel->cur_anchor->next &&
  875. bezier_check_point (bezier_sel->cur_anchor->next, x, y, halfwidth))
  876. {
  877. bezier_sel->cur_control = bezier_sel->cur_anchor->next;
  878. break;
  879. }
  880. if (bezier_sel->cur_anchor->prev &&
  881. bezier_check_point (bezier_sel->cur_anchor->prev, x, y, halfwidth))
  882. {
  883. bezier_sel->cur_control = bezier_sel->cur_anchor->prev;
  884. break;
  885. }
  886. }
  887. curve_start = find_start_open_curve(bezier_sel);
  888. if (curve_start && bezier_check_point (curve_start, x, y, halfwidth))
  889. {
  890. bezier_sel->draw = BEZIER_DRAW_ALL;
  891. draw_core_pause (bezier_sel->core, tool);
  892. bezier_add_point (bezier_sel, BEZIER_CONTROL, (gdouble)x, (gdouble)y);
  893. bezier_sel->last_point->next = curve_start;
  894. curve_start->prev = bezier_sel->last_point;
  895. bezier_sel->cur_anchor = curve_start;
  896. bezier_sel->cur_control = curve_start->next;
  897. bezier_sel->closed = 1;
  898. bezier_sel->state = BEZIER_EDIT;
  899. bezier_sel->draw = BEZIER_DRAW_ALL;
  900. draw_core_resume (bezier_sel->core, tool);
  901. }
  902. else
  903. {
  904. if(bezier_sel->cur_anchor)
  905. bezier_sel->cur_anchor->pointflags = 1;
  906. bezier_sel->draw = BEZIER_DRAW_HANDLES;
  907. draw_core_pause (bezier_sel->core, tool);
  908. bezier_add_point (bezier_sel, BEZIER_CONTROL, (gdouble)x, (gdouble)y);
  909. bezier_add_point (bezier_sel, BEZIER_ANCHOR, (gdouble)x, (gdouble)y);
  910. bezier_add_point (bezier_sel, BEZIER_CONTROL, (gdouble)x, (gdouble)y);
  911. bezier_sel->draw = BEZIER_DRAW_CURRENT | BEZIER_DRAW_HANDLES;
  912. draw_core_resume (bezier_sel->core, tool);
  913. }
  914. break;
  915. case BEZIER_EDIT:
  916. if (!bezier_sel->closed)
  917. gimp_fatal_error ("bezier_select_button_press(): Tried to edit on open bezier curve in edit selection");
  918. /* erase the handles */
  919. bezier_sel->draw = BEZIER_DRAW_ALL;
  920. draw_core_pause (bezier_sel->core, tool);
  921. /* unset the current anchor and control */
  922. bezier_sel->cur_anchor = NULL;
  923. bezier_sel->cur_control = NULL;
  924. points = bezier_sel->points;
  925. start_pt = bezier_sel->points;
  926. if (ModeEdit == EXTEND_ADD)
  927. {
  928. if (bezier_sel->closed)
  929. {
  930. grab_pointer = bezier_add_point_on_segment(x,y,halfwidth,gdisp,bezier_sel,tool);
  931. }
  932. }
  933. else
  934. {
  935. grab_pointer = bezier_edit_point_on_curve(x,y,halfwidth,gdisp,bezier_sel,tool,bevent);
  936. if(grab_pointer && bezier_sel->num_points == 0)
  937. {
  938. draw_core_pause(bezier_sel->core, tool);
  939. paths_dialog_set_default_op();
  940. }
  941. }
  942. if (!grab_pointer && channel_value (bezier_sel->mask, x, y))
  943. {
  944. replace = 0;
  945. if ((bevent->state & GDK_SHIFT_MASK) && !(bevent->state & GDK_CONTROL_MASK))
  946. op = ADD;
  947. else if ((bevent->state & GDK_CONTROL_MASK) && !(bevent->state & GDK_SHIFT_MASK))
  948. op = SUB;
  949. else if ((bevent->state & GDK_CONTROL_MASK) && (bevent->state & GDK_SHIFT_MASK))
  950. op = INTERSECT;
  951. else
  952. {
  953. op = ADD;
  954. replace = 1;
  955. }
  956. bezier_to_sel_internal(bezier_sel,tool,gdisp,op,replace);
  957. draw_core_resume (bezier_sel->core, tool);
  958. }
  959. else
  960. {
  961. /* draw the handles */
  962. if(!grab_pointer)
  963. {
  964. paths_dialog_set_default_op();
  965. bezier_start_new_segment(bezier_sel,x,y);
  966. }
  967. bezier_sel->draw = BEZIER_DRAW_ALL;
  968. draw_core_resume (bezier_sel->core, tool);
  969. }
  970. break;
  971. }
  972. if (grab_pointer)
  973. gdk_pointer_grab (gdisp->canvas->window, FALSE,
  974. GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON1_MOTION_MASK | GDK_BUTTON_RELEASE_MASK,
  975. NULL, NULL, bevent->time);
  976. /* Don't bother doing this if we don't have any points */
  977. if(bezier_sel->num_points > 0)
  978. paths_first_button_press(bezier_sel,gdisp);
  979. }
  980. static void
  981. bezier_select_button_release (Tool *tool,
  982. GdkEventButton *bevent,
  983. gpointer gdisp_ptr)
  984. {
  985. GDisplay * gdisp;
  986. BezierSelect *bezier_sel;
  987. gdisp = tool->gdisp_ptr;
  988. bezier_sel = tool->private;
  989. bezier_sel->state &= ~(BEZIER_DRAG);
  990. gdk_pointer_ungrab (bevent->time);
  991. gdk_flush ();
  992. if (bezier_sel->closed)
  993. bezier_convert (bezier_sel, tool->gdisp_ptr, SUBDIVIDE, NO);
  994. /* Here ?*/
  995. paths_newpoint_current(bezier_sel,gdisp);
  996. }
  997. /* unused
  998. static void
  999. bez_copy_points(BezierSelect *tobez,
  1000. BezierSelect *frombez)
  1001. {
  1002. BezierPoint *pts;
  1003. gint i;
  1004. BezierPoint *bpnt = NULL;
  1005. int need_move = 0;
  1006. pts = (BezierPoint *) frombez->points;
  1007. for (i=0; i< frombez->num_points; i++)
  1008. {
  1009. if(need_move)
  1010. {
  1011. bezier_add_point( tobez, BEZIER_MOVE, pts->x, pts->y);
  1012. need_move = 0;
  1013. }
  1014. else
  1015. bezier_add_point( tobez, pts->type, pts->x, pts->y);
  1016. if(pts == frombez->cur_anchor)
  1017. tobez->cur_anchor = tobez->last_point;
  1018. else if(pts == frombez->cur_control)
  1019. tobez->cur_control = tobez->last_point;
  1020. if(bpnt == NULL)
  1021. bpnt = tobez->last_point;
  1022. if(pts->next_curve)
  1023. {
  1024. tobez->last_point->next = bpnt;
  1025. bpnt->prev = tobez->last_point;
  1026. bpnt = NULL;
  1027. need_move = 1;
  1028. pts = pts->next_curve;
  1029. }
  1030. else
  1031. {
  1032. pts = pts->next;
  1033. }
  1034. }
  1035. if ( frombez->closed )
  1036. {
  1037. tobez->last_point->next = bpnt;
  1038. bpnt->prev = tobez->last_point;
  1039. tobez->closed = 1;
  1040. }
  1041. }
  1042. */
  1043. static void
  1044. bezier_select_motion (Tool *tool,
  1045. GdkEventMotion *mevent,
  1046. gpointer gdisp_ptr)
  1047. {
  1048. static int lastx, lasty;
  1049. GDisplay * gdisp;
  1050. BezierSelect * bezier_sel;
  1051. BezierPoint * anchor;
  1052. BezierPoint * opposite_control;
  1053. int offsetx;
  1054. int offsety;
  1055. int x, y;
  1056. if (tool->state != ACTIVE)
  1057. return;
  1058. gdisp = gdisp_ptr;
  1059. bezier_sel = tool->private;
  1060. if (!bezier_sel->cur_anchor || !bezier_sel->cur_control)
  1061. return;
  1062. if (mevent->state & GDK_MOD1_MASK)
  1063. {
  1064. bezier_sel->draw = BEZIER_DRAW_ALL;
  1065. }
  1066. else
  1067. {
  1068. bezier_sel->draw = BEZIER_DRAW_CURRENT | BEZIER_DRAW_HANDLES;
  1069. }
  1070. draw_core_pause (bezier_sel->core, tool);
  1071. gdisplay_untransform_coords (gdisp, mevent->x, mevent->y, &x, &y, TRUE, 0);
  1072. /* If this is the first point then change the state and "remember" the point.
  1073. */
  1074. if (!(bezier_sel->state & BEZIER_DRAG))
  1075. {
  1076. bezier_sel->state |= BEZIER_DRAG;
  1077. lastx = x;
  1078. lasty = y;
  1079. }
  1080. /* The Alt key is down... Move all the points of the bezier curve */
  1081. if (mevent->state & GDK_MOD1_MASK)
  1082. {
  1083. BezierPoint *tmp = bezier_sel->points;
  1084. gint num_points = bezier_sel->num_points;
  1085. offsetx = x - lastx;
  1086. offsety = y - lasty;
  1087. if(mevent->state & GDK_SHIFT_MASK)
  1088. {
  1089. /* Only move this curve */
  1090. BezierPoint *start_pt = bezier_sel->cur_anchor;
  1091. /* g_print ("moving only one curve\n"); */
  1092. tmp = start_pt;
  1093. do {
  1094. bezier_offset_point (tmp, offsetx, offsety);
  1095. tmp = tmp->next;
  1096. } while (tmp != start_pt && tmp);
  1097. /* Check if need to go backwards because curve is open */
  1098. if(!tmp && bezier_sel->cur_anchor->prev)
  1099. {
  1100. BezierPoint *start_pt = bezier_sel->cur_anchor->prev;
  1101. tmp = start_pt;
  1102. do {
  1103. bezier_offset_point (tmp, offsetx, offsety);
  1104. tmp = tmp->prev;
  1105. } while (tmp != start_pt && tmp);
  1106. }
  1107. }
  1108. else
  1109. {
  1110. while (tmp && num_points)
  1111. {
  1112. bezier_offset_point (tmp, offsetx, offsety);
  1113. if(tmp->next_curve)
  1114. tmp = tmp->next_curve;
  1115. else
  1116. tmp = tmp->next;
  1117. num_points--;
  1118. }
  1119. }
  1120. }
  1121. else
  1122. if (mevent->state & GDK_CONTROL_MASK)
  1123. {
  1124. /* the control key is down ... move the current anchor point */
  1125. /* we must also move the neighboring control points appropriately */
  1126. offsetx = x - lastx;
  1127. offsety = y - lasty;
  1128. bezier_offset_point (bezier_sel->cur_anchor, offsetx, offsety);
  1129. bezier_offset_point (bezier_sel->cur_anchor->next, offsetx, offsety);
  1130. bezier_offset_point (bezier_sel->cur_anchor->prev, offsetx, offsety);
  1131. }
  1132. else
  1133. {
  1134. /* the control key is not down ... we move the current control point */
  1135. offsetx = x - bezier_sel->cur_control->x;
  1136. offsety = y - bezier_sel->cur_control->y;
  1137. bezier_offset_point (bezier_sel->cur_control, offsetx, offsety);
  1138. /* if the shift key is not down then we align the opposite control */
  1139. /* point...ie the opposite control point acts like a mirror of the */
  1140. /* current control point */
  1141. if (!(mevent->state & GDK_SHIFT_MASK))
  1142. {
  1143. anchor = NULL;
  1144. opposite_control = NULL;
  1145. if (bezier_sel->cur_control->next)
  1146. {
  1147. if (bezier_sel->cur_control->next->type == BEZIER_ANCHOR)
  1148. {
  1149. anchor = bezier_sel->cur_control->next;
  1150. opposite_control = anchor->next;
  1151. }
  1152. }
  1153. if (bezier_sel->cur_control->prev)
  1154. {
  1155. if (bezier_sel->cur_control->prev->type == BEZIER_ANCHOR)
  1156. {
  1157. anchor = bezier_sel->cur_control->prev;
  1158. opposite_control = anchor->prev;
  1159. }
  1160. }
  1161. if (!anchor)
  1162. gimp_fatal_error ("bezier_select_motion(): Encountered orphaned bezier control point");
  1163. if (opposite_control)
  1164. {
  1165. offsetx = bezier_sel->cur_control->x - anchor->x;
  1166. offsety = bezier_sel->cur_control->y - anchor->y;
  1167. opposite_control->x = anchor->x - offsetx;
  1168. opposite_control->y = anchor->y - offsety;
  1169. }
  1170. }
  1171. }
  1172. /* As we're moving all the control points of the curve,
  1173. we have to redraw all !!!
  1174. */
  1175. if ( mevent->state & GDK_MOD1_MASK)
  1176. bezier_sel->draw = BEZIER_DRAW_ALL;
  1177. else
  1178. bezier_sel->draw = BEZIER_DRAW_CURRENT | BEZIER_DRAW_HANDLES;
  1179. draw_core_resume (bezier_sel->core, tool);
  1180. lastx = x;
  1181. lasty = y;
  1182. }
  1183. /* returns 0 if not on control point, else BEZIER_ANCHOR or BEZIER_CONTROL */
  1184. static gint
  1185. bezier_on_control_point(GDisplay *gdisp,
  1186. BezierSelect *bezier_sel,
  1187. gint x,
  1188. gint y,
  1189. gint halfwidth)
  1190. {
  1191. BezierPoint * points;
  1192. gint num_points;
  1193. /* transform the points from image space to screen space */
  1194. points = bezier_sel->points;
  1195. num_points = bezier_sel->num_points;
  1196. while (points && num_points)
  1197. {
  1198. if(bezier_check_point(points,x,y,halfwidth))
  1199. return points->type;
  1200. if(points->next_curve)
  1201. points = points->next_curve;
  1202. else
  1203. points = points->next;
  1204. num_points--;
  1205. }
  1206. return FALSE;
  1207. }
  1208. static void
  1209. bezier_check_points (BezierSelect *bezier_sel,
  1210. GdkPoint *points,
  1211. int npoints,
  1212. gpointer udata)
  1213. {
  1214. gint loop;
  1215. int l, r, t, b;
  1216. BezierCheckPnts *chkpnts = udata;
  1217. gint halfwidth = chkpnts->halfwidth;
  1218. /* Quick exit if already found */
  1219. if(chkpnts->found)
  1220. return;
  1221. for(loop = 0 ; loop < npoints; loop++)
  1222. {
  1223. l = points[loop].x - halfwidth;
  1224. r = points[loop].x + halfwidth;
  1225. t = points[loop].y - halfwidth;
  1226. b = points[loop].y + halfwidth;
  1227. /* g_print ("x,y = [%d,%d] halfwidth %d l,r,t,d [%d,%d,%d,%d]\n", */
  1228. /* points[loop].x, */
  1229. /* points[loop].y, */
  1230. /* halfwidth, */
  1231. /* l,r,t,b); */
  1232. if ((chkpnts->x >= l) &&
  1233. (chkpnts->x <= r) &&
  1234. (chkpnts->y >= t) &&
  1235. (chkpnts->y <= b))
  1236. {
  1237. chkpnts->found = TRUE;
  1238. }
  1239. }
  1240. }
  1241. static gboolean
  1242. points_in_box(BezierPoint *points,
  1243. gint x,
  1244. gint y)
  1245. {
  1246. /* below code adapted from Wm. Randolph Franklin <wrf@ecse.rpi.edu>
  1247. */
  1248. int i, j, c = 0;
  1249. double yp[4],xp[4];
  1250. for (i = 0; i < 4; i++)
  1251. {
  1252. xp[i] = points->x;
  1253. yp[i] = points->y;
  1254. points = points->next;
  1255. }
  1256. /* Check if straight line ..below don't work if it is! */
  1257. if((xp[0] == xp[1] && yp[0] == yp[1]) ||
  1258. (xp[2] == xp[3] && yp[0] == yp[1]))
  1259. return TRUE;
  1260. for (i = 0, j = 3; i < 4; j = i++) {
  1261. if ((((yp[i]<=y) && (y<yp[j])) ||
  1262. ((yp[j]<=y) && (y<yp[i]))) &&
  1263. (x < (xp[j] - xp[i]) * (y - yp[i]) / (yp[j] - yp[i]) + xp[i]))
  1264. c = !c;
  1265. }
  1266. return c;
  1267. }
  1268. static gint
  1269. bezier_point_on_curve (GDisplay *gdisp,
  1270. BezierSelect *bezier_sel,
  1271. gint x,
  1272. gint y,
  1273. gint halfwidth)
  1274. {
  1275. BezierCheckPnts chkpnts;
  1276. BezierPoint * points;
  1277. BezierPoint * start_pt;
  1278. BezierPoint * next_curve;
  1279. CountCurves * cnt = (CountCurves *)&chkpnts;
  1280. gint point_counts = 0;
  1281. chkpnts.x = x;
  1282. chkpnts.y = y;
  1283. chkpnts.halfwidth = halfwidth;
  1284. chkpnts.found = FALSE;
  1285. if (cnt)
  1286. cnt->count = 0;
  1287. points = bezier_sel->points;
  1288. start_pt = bezier_sel->points;
  1289. if (bezier_sel->num_points >= 4)
  1290. {
  1291. do
  1292. {
  1293. point_counts = count_points_on_curve(points);
  1294. if (point_counts >= 4)
  1295. {
  1296. do
  1297. {
  1298. if (points_in_box (points, x, y))
  1299. {
  1300. bezier_draw_segment (bezier_sel, points,
  1301. SUBDIVIDE, IMAGE_COORDS,
  1302. bezier_check_points,
  1303. &chkpnts);
  1304. }
  1305. points = next_anchor (points, &next_curve);
  1306. /* g_print ("next_anchor = %p\n",points); */
  1307. }
  1308. while (points != start_pt && points);
  1309. if (cnt)
  1310. cnt->count++;
  1311. start_pt = next_curve;
  1312. points = next_curve;
  1313. }
  1314. else
  1315. break; /* must be last curve since only this one is allowed < 4
  1316. * points.
  1317. */
  1318. }
  1319. while (next_curve);
  1320. }
  1321. return chkpnts.found;
  1322. }
  1323. static void
  1324. bezier_select_cursor_update (Tool *tool,
  1325. GdkEventMotion *mevent,
  1326. gpointer gdisp_ptr)
  1327. {
  1328. GDisplay *gdisp;
  1329. BezierSelect * bezier_sel;
  1330. gboolean on_curve;
  1331. gboolean on_control_pnt;
  1332. gboolean in_selection_area;
  1333. gint halfwidth,dummy;
  1334. gint x,y;
  1335. gdisp = (GDisplay *) gdisp_ptr;
  1336. bezier_sel = tool->private;
  1337. if (gdisp != tool->gdisp_ptr || bezier_sel->core->draw_state == INVISIBLE)
  1338. {
  1339. gdisplay_install_tool_cursor (gdisp, GIMP_MOUSE_CURSOR,
  1340. BEZIER_SELECT,
  1341. CURSOR_MODIFIER_NONE,
  1342. FALSE);
  1343. return;
  1344. }
  1345. gdisplay_untransform_coords (gdisp, mevent->x, mevent->y, &x, &y, TRUE, 0);
  1346. /* get halfwidth in image coord */
  1347. gdisplay_untransform_coords (gdisp, mevent->x + BEZIER_HALFWIDTH, 0,
  1348. &halfwidth, &dummy, TRUE, 0);
  1349. halfwidth -= x;
  1350. on_control_pnt = bezier_on_control_point (gdisp, bezier_sel, x, y, halfwidth);
  1351. on_curve = bezier_point_on_curve (gdisp, bezier_sel, x, y, halfwidth);
  1352. if (bezier_sel->mask && bezier_sel->closed &&
  1353. channel_value(bezier_sel->mask, x, y) &&
  1354. !on_control_pnt &&
  1355. (!on_curve || ModeEdit != EXTEND_ADD))
  1356. {
  1357. in_selection_area = TRUE;
  1358. if ((mevent->state & GDK_SHIFT_MASK) &&
  1359. !(mevent->state & GDK_CONTROL_MASK))
  1360. {
  1361. gdisplay_install_tool_cursor (gdisp, GIMP_MOUSE_CURSOR,
  1362. RECT_SELECT,
  1363. CURSOR_MODIFIER_PLUS,
  1364. FALSE);
  1365. }
  1366. else if ((mevent->state & GDK_CONTROL_MASK) &&
  1367. !(mevent->state & GDK_SHIFT_MASK))
  1368. {
  1369. gdisplay_install_tool_cursor (gdisp, GIMP_MOUSE_CURSOR,
  1370. RECT_SELECT,
  1371. CURSOR_MODIFIER_MINUS,
  1372. FALSE);
  1373. }
  1374. else if ((mevent->state & GDK_CONTROL_MASK) &&
  1375. (mevent->state & GDK_SHIFT_MASK))
  1376. {
  1377. gdisplay_install_tool_cursor (gdisp, GIMP_MOUSE_CURSOR,
  1378. RECT_SELECT,
  1379. CURSOR_MODIFIER_INTERSECT,
  1380. FALSE);
  1381. }
  1382. else
  1383. {
  1384. gdisplay_install_tool_cursor (gdisp, GIMP_MOUSE_CURSOR,
  1385. RECT_SELECT,
  1386. CURSOR_MODIFIER_NONE,
  1387. FALSE);
  1388. }
  1389. return;
  1390. }
  1391. if (mevent->state & GDK_MOD1_MASK)
  1392. {
  1393. /* Moving curve */
  1394. if (mevent->state & GDK_SHIFT_MASK)
  1395. {
  1396. /* moving on 1 curve */
  1397. gdisplay_install_tool_cursor (gdisp, GIMP_MOUSE_CURSOR,
  1398. BEZIER_SELECT,
  1399. CURSOR_MODIFIER_MOVE,
  1400. FALSE);
  1401. }
  1402. else
  1403. {
  1404. gdisplay_install_tool_cursor (gdisp, GIMP_MOUSE_CURSOR,
  1405. BEZIER_SELECT,
  1406. CURSOR_MODIFIER_MOVE,
  1407. FALSE);
  1408. }
  1409. }
  1410. else
  1411. {
  1412. switch (ModeEdit)
  1413. {
  1414. case EXTEND_NEW:
  1415. if (on_control_pnt && bezier_sel->closed)
  1416. {
  1417. gdisplay_install_tool_cursor (gdisp, GIMP_MOUSE_CURSOR,
  1418. BEZIER_SELECT,
  1419. CURSOR_MODIFIER_CONTROL,
  1420. FALSE);
  1421. /* g_print ("add to curve cursor\n"); */
  1422. }
  1423. else if (on_curve)
  1424. {
  1425. gdisplay_install_tool_cursor (gdisp, GIMP_MOUSE_CURSOR,
  1426. BEZIER_SELECT,
  1427. CURSOR_MODIFIER_NONE,
  1428. FALSE);
  1429. /* g_print ("edit control point cursor\n"); */
  1430. }
  1431. else
  1432. {
  1433. gdisplay_install_tool_cursor (gdisp, GIMP_MOUSE_CURSOR,
  1434. BEZIER_SELECT,
  1435. CURSOR_MODIFIER_NONE,
  1436. FALSE);
  1437. }
  1438. break;
  1439. case EXTEND_ADD:
  1440. if (on_curve)
  1441. {
  1442. gdisplay_install_tool_cursor (gdisp, GIMP_MOUSE_CURSOR,
  1443. BEZIER_SELECT,
  1444. CURSOR_MODIFIER_PLUS,
  1445. FALSE);
  1446. /* g_print ("add to curve cursor\n"); */
  1447. }
  1448. else
  1449. {
  1450. gdisplay_install_tool_cursor (gdisp, GIMP_MOUSE_CURSOR,
  1451. BEZIER_SELECT,
  1452. CURSOR_MODIFIER_NONE,
  1453. FALSE);
  1454. /* g_print ("default no action cursor\n"); */
  1455. }
  1456. break;
  1457. case EXTEND_EDIT:
  1458. if (on_control_pnt)
  1459. {
  1460. gdisplay_install_tool_cursor (gdisp, GIMP_MOUSE_CURSOR,
  1461. BEZIER_SELECT,
  1462. CURSOR_MODIFIER_CONTROL,
  1463. FALSE);
  1464. /* g_print ("edit control point cursor\n"); */
  1465. }
  1466. else
  1467. {
  1468. gdisplay_install_tool_cursor (gdisp, GIMP_MOUSE_CURSOR,
  1469. BEZIER_SELECT,
  1470. CURSOR_MODIFIER_NONE,
  1471. FALSE);
  1472. /* g_print ("default no action cursor\n"); */
  1473. }
  1474. break;
  1475. case EXTEND_REMOVE:
  1476. if (on_control_pnt && mevent->state & GDK_SHIFT_MASK)
  1477. {
  1478. gdisplay_install_tool_cursor (gdisp, GIMP_MOUSE_CURSOR,
  1479. BEZIER_SELECT,
  1480. CURSOR_MODIFIER_MINUS,
  1481. FALSE);
  1482. /* g_print ("delete whole curve cursor\n"); */
  1483. }
  1484. else if (on_control_pnt)
  1485. {
  1486. gdisplay_install_tool_cursor (gdisp, GIMP_MOUSE_CURSOR,
  1487. BEZIER_SELECT,
  1488. CURSOR_MODIFIER_MINUS,
  1489. FALSE);
  1490. /* g_print ("remove point cursor\n"); */
  1491. }
  1492. else
  1493. {
  1494. gdisplay_install_tool_cursor (gdisp, GIMP_MOUSE_CURSOR,
  1495. BEZIER_SELECT,
  1496. CURSOR_MODIFIER_NONE,
  1497. FALSE);
  1498. /* g_print ("default no action cursor\n"); */
  1499. }
  1500. break;
  1501. default:
  1502. g_print ("In default\n");
  1503. gdisplay_install_tool_cursor (gdisp, GIMP_MOUSE_CURSOR,
  1504. BEZIER_SELECT,
  1505. CURSOR_MODIFIER_NONE,
  1506. FALSE);
  1507. break;
  1508. }
  1509. }
  1510. }
  1511. static void
  1512. bezier_select_control (Tool *tool,
  1513. ToolAction action,
  1514. gpointer gdisp_ptr)
  1515. {
  1516. BezierSelect * bezier_sel;
  1517. bezier_sel = tool->private;
  1518. switch (action)
  1519. {
  1520. case PAUSE :
  1521. draw_core_pause (bezier_sel->core, tool);
  1522. break;
  1523. case RESUME :
  1524. draw_core_resume (bezier_sel->core, tool);
  1525. break;
  1526. case HALT :
  1527. draw_core_stop (bezier_sel->core, tool);
  1528. bezier_select_reset (bezier_sel);
  1529. break;
  1530. default:
  1531. break;
  1532. }
  1533. }
  1534. void
  1535. bezier_draw(GDisplay * gdisp,BezierSelect * bezier_sel)
  1536. {
  1537. BezierPoint * points;
  1538. int num_points;
  1539. int draw_curve;
  1540. int draw_handles;
  1541. int draw_current;
  1542. if (!bezier_sel->draw)
  1543. return;
  1544. draw_curve = bezier_sel->draw & BEZIER_DRAW_CURVE;
  1545. draw_current = bezier_sel->draw & BEZIER_DRAW_CURRENT;
  1546. draw_handles = bezier_sel->draw & BEZIER_DRAW_HANDLES;
  1547. /* reset to the default drawing state of drawing the curve and handles */
  1548. bezier_sel->draw = BEZIER_DRAW_ALL;
  1549. /* transform the points from image space to screen space */
  1550. points = bezier_sel->points;
  1551. num_points = bezier_sel->num_points;
  1552. while (points && num_points)
  1553. {
  1554. gdisplay_transform_coords (gdisp, points->x, points->y,
  1555. &points->sx, &points->sy, 0);
  1556. if(points->next_curve)
  1557. points = points->next_curve;
  1558. else
  1559. points = points->next;
  1560. num_points--;
  1561. }
  1562. if (draw_curve)
  1563. {
  1564. bezier_draw_curve (bezier_sel,bezier_draw_segment_points,SCREEN_COORDS,NULL);
  1565. bezier_draw_handles (bezier_sel,1);
  1566. }
  1567. else if (draw_current)
  1568. {
  1569. bezier_draw_current (bezier_sel);
  1570. bezier_draw_handles (bezier_sel,0);
  1571. }
  1572. else if (draw_handles)
  1573. {
  1574. bezier_draw_handles (bezier_sel,0);
  1575. }
  1576. }
  1577. static void
  1578. bezier_select_draw (Tool *tool)
  1579. {
  1580. GDisplay * gdisp;
  1581. BezierSelect * bezier_sel;
  1582. gdisp = tool->gdisp_ptr;
  1583. bezier_sel = tool->private;
  1584. bezier_draw(gdisp,bezier_sel);
  1585. }
  1586. void
  1587. bezier_add_point (BezierSelect *bezier_sel,
  1588. int type,
  1589. gdouble x,
  1590. gdouble y)
  1591. {
  1592. BezierPoint *newpt;
  1593. newpt = g_new0 (BezierPoint,1);
  1594. newpt->type = type;
  1595. newpt->x = x;
  1596. newpt->y = y;
  1597. newpt->next = NULL;
  1598. newpt->prev = NULL;
  1599. newpt->next_curve = NULL;
  1600. if(type == BEZIER_MOVE &&
  1601. bezier_sel->last_point)
  1602. {
  1603. /* g_print ("Adding move point\n"); */
  1604. newpt->type = BEZIER_ANCHOR;
  1605. bezier_sel->last_point->next_curve = newpt;
  1606. bezier_sel->last_point = newpt;
  1607. bezier_sel->cur_anchor = newpt;
  1608. }
  1609. else
  1610. {
  1611. if(type == BEZIER_MOVE)
  1612. {
  1613. newpt->type = BEZIER_ANCHOR;
  1614. /* g_print ("Adding MOVE point to null curve\n"); */
  1615. }
  1616. if (bezier_sel->last_point)
  1617. {
  1618. bezier_sel->last_point->next = newpt;
  1619. newpt->prev = bezier_sel->last_point;
  1620. bezier_sel->last_point = newpt;
  1621. }
  1622. else
  1623. {
  1624. bezier_sel->points = newpt;
  1625. bezier_sel->last_point = newpt;
  1626. }
  1627. switch (type)
  1628. {
  1629. case BEZIER_ANCHOR:
  1630. bezier_sel->cur_anchor = newpt;
  1631. break;
  1632. case BEZIER_CONTROL:
  1633. bezier_sel->cur_control = newpt;
  1634. break;
  1635. }
  1636. }
  1637. bezier_sel->num_points += 1;
  1638. }
  1639. static void
  1640. bezier_offset_point (BezierPoint *pt,
  1641. int x,
  1642. int y)
  1643. {
  1644. if (pt)
  1645. {
  1646. pt->x += x;
  1647. pt->y += y;
  1648. }
  1649. }
  1650. static int
  1651. bezier_check_point (BezierPoint *pt,
  1652. int x,
  1653. int y,
  1654. int halfwidth)
  1655. {
  1656. int l, r, t, b;
  1657. if (pt)
  1658. {
  1659. l = pt->x - halfwidth;
  1660. r = pt->x + halfwidth;
  1661. t = pt->y - halfwidth;
  1662. b = pt->y + halfwidth;
  1663. return ((x >= l) && (x <= r) && (y >= t) && (y <= b));
  1664. }
  1665. return 0;
  1666. }
  1667. static void
  1668. bezier_draw_handles (BezierSelect *bezier_sel,gint doAll)
  1669. {
  1670. BezierPoint * points;
  1671. int num_points;
  1672. points = bezier_sel->points;
  1673. num_points = bezier_sel->num_points;
  1674. if (num_points <= 0)
  1675. return;
  1676. while (num_points && num_points > 0)
  1677. {
  1678. if (points == bezier_sel->cur_anchor)
  1679. {
  1680. /* g_print ("bezier_draw_handles:: found cur_anchor %p\n",points); */
  1681. bezier_draw_point (bezier_sel, points, 0);
  1682. bezier_draw_point (bezier_sel, points->next, 0);
  1683. bezier_draw_point (bezier_sel, points->prev, 0);
  1684. bezier_draw_line (bezier_sel, points, points->next);
  1685. bezier_draw_line (bezier_sel, points, points->prev);
  1686. }
  1687. else
  1688. {
  1689. /* g_print ("bezier_draw_handles:: not found cur_anchor %p\n",points); */
  1690. if(doAll || points->pointflags == 1)
  1691. {
  1692. bezier_draw_point (bezier_sel, points, 1);
  1693. points->pointflags = 0;
  1694. }
  1695. }
  1696. if(points)
  1697. {
  1698. if(points->next_curve)
  1699. points = points->next_curve;
  1700. else
  1701. points = points->next;
  1702. }
  1703. if(points)
  1704. {
  1705. if(points->next_curve)
  1706. points = points->next_curve;
  1707. else
  1708. points = points->next;
  1709. }
  1710. if(points)
  1711. {
  1712. if(points->next_curve)
  1713. points = points->next_curve;
  1714. else
  1715. points = points->next;
  1716. }
  1717. num_points -= 3;
  1718. }
  1719. }
  1720. static void
  1721. bezier_draw_current (BezierSelect *bezier_sel)
  1722. {
  1723. BezierPoint * points;
  1724. points = bezier_sel->cur_anchor;
  1725. if (points) points = points->prev;
  1726. if (points) points = points->prev;
  1727. if (points) points = points->prev;
  1728. if (points)
  1729. bezier_draw_segment (bezier_sel, points,
  1730. SUBDIVIDE, SCREEN_COORDS,
  1731. bezier_draw_segment_points,
  1732. NULL);
  1733. if (points != bezier_sel->cur_anchor)
  1734. {
  1735. points = bezier_sel->cur_anchor;
  1736. if (points) points = points->next;
  1737. if (points) points = points->next;
  1738. if (points) points = points->next;
  1739. if (points)
  1740. bezier_draw_segment (bezier_sel, bezier_sel->cur_anchor,
  1741. SUBDIVIDE, SCREEN_COORDS,
  1742. bezier_draw_segment_points,
  1743. NULL);
  1744. }
  1745. }
  1746. static void
  1747. bezier_draw_point (BezierSelect *bezier_sel,
  1748. BezierPoint *pt,
  1749. int fill)
  1750. {
  1751. if (pt)
  1752. {
  1753. switch (pt->type)
  1754. {
  1755. case BEZIER_ANCHOR:
  1756. if (fill)
  1757. {
  1758. gdk_draw_arc (bezier_sel->core->win, bezier_sel->core->gc, 1,
  1759. pt->sx - BEZIER_HALFWIDTH, pt->sy - BEZIER_HALFWIDTH,
  1760. BEZIER_WIDTH, BEZIER_WIDTH, 0, 23040);
  1761. }
  1762. else
  1763. {
  1764. gdk_draw_arc (bezier_sel->core->win, bezier_sel->core->gc, 0,
  1765. pt->sx - BEZIER_HALFWIDTH, pt->sy - BEZIER_HALFWIDTH,
  1766. BEZIER_WIDTH, BEZIER_WIDTH, 0, 23040);
  1767. }
  1768. break;
  1769. case BEZIER_CONTROL:
  1770. if (fill)
  1771. {
  1772. gdk_draw_rectangle (bezier_sel->core->win, bezier_sel->core->gc, 1,
  1773. pt->sx - BEZIER_HALFWIDTH, pt->sy - BEZIER_HALFWIDTH,
  1774. BEZIER_WIDTH, BEZIER_WIDTH);
  1775. }
  1776. else
  1777. {
  1778. gdk_draw_rectangle (bezier_sel->core->win, bezier_sel->core->gc, 0,
  1779. pt->sx - BEZIER_HALFWIDTH, pt->sy - BEZIER_HALFWIDTH,
  1780. BEZIER_WIDTH, BEZIER_WIDTH);
  1781. }
  1782. break;
  1783. }
  1784. }
  1785. }
  1786. static void
  1787. bezier_draw_line (BezierSelect *bezier_sel,
  1788. BezierPoint *pt1,
  1789. BezierPoint *pt2)
  1790. {
  1791. if (pt1 && pt2)
  1792. {
  1793. gdk_draw_line (bezier_sel->core->win,
  1794. bezier_sel->core->gc,
  1795. pt1->sx, pt1->sy, pt2->sx, pt2->sy);
  1796. }
  1797. }
  1798. static void
  1799. bezier_draw_segment (BezierSelect *bezier_sel,
  1800. BezierPoint *points,
  1801. int subdivisions,
  1802. int space,
  1803. BezierPointsFunc points_func,
  1804. gpointer udata)
  1805. {
  1806. static GdkPoint gdk_points[256];
  1807. static int npoints = 256;
  1808. BezierMatrix geometry;
  1809. BezierMatrix tmp1, tmp2;
  1810. BezierMatrix deltas;
  1811. double x, dx, dx2, dx3;
  1812. double y, dy, dy2, dy3;
  1813. double d, d2, d3;
  1814. int lastx, lasty;
  1815. int newx, newy;
  1816. int index;
  1817. int i;
  1818. /* construct the geometry matrix from the segment */
  1819. /* assumes that a valid segment containing 4 points is passed in */
  1820. for (i = 0; i < 4; i++)
  1821. {
  1822. if (!points)
  1823. gimp_fatal_error ("bezier_draw_segment(): Bad bezier segment");
  1824. switch (space)
  1825. {
  1826. case IMAGE_COORDS:
  1827. geometry[i][0] = points->x;
  1828. geometry[i][1] = points->y;
  1829. break;
  1830. case AA_IMAGE_COORDS:
  1831. geometry[i][0] = (points->x * SUPERSAMPLE);
  1832. geometry[i][1] = (points->y * SUPERSAMPLE);
  1833. break;
  1834. case SCREEN_COORDS:
  1835. geometry[i][0] = points->sx;
  1836. geometry[i][1] = points->sy;
  1837. break;
  1838. default:
  1839. gimp_fatal_error ("bezier_draw_segment(): Unknown coordinate space: %d", space);
  1840. break;
  1841. }
  1842. geometry[i][2] = 0;
  1843. geometry[i][3] = 0;
  1844. points = points->next;
  1845. }
  1846. /* subdivide the curve n times */
  1847. /* n can be adjusted to give a finer or coarser curve */
  1848. d = 1.0 / subdivisions;
  1849. d2 = d * d;
  1850. d3 = d * d * d;
  1851. /* construct a temporary matrix for determining the forward diffencing deltas */
  1852. tmp2[0][0] = 0; tmp2[0][1] = 0; tmp2[0][2] = 0; tmp2[0][3] = 1;
  1853. tmp2[1][0] = d3; tmp2[1][1] = d2; tmp2[1][2] = d; tmp2[1][3] = 0;
  1854. tmp2[2][0] = 6*d3; tmp2[2][1] = 2*d2; tmp2[2][2] = 0; tmp2[2][3] = 0;
  1855. tmp2[3][0] = 6*d3; tmp2[3][1] = 0; tmp2[3][2] = 0; tmp2[3][3] = 0;
  1856. /* compose the basis and geometry matrices */
  1857. bezier_compose (basis, geometry, tmp1);
  1858. /* compose the above results to get the deltas matrix */
  1859. bezier_compose (tmp2, tmp1, deltas);
  1860. /* extract the x deltas */
  1861. x = deltas[0][0];
  1862. dx = deltas[1][0];
  1863. dx2 = deltas[2][0];
  1864. dx3 = deltas[3][0];
  1865. /* extract the y deltas */
  1866. y = deltas[0][1];
  1867. dy = deltas[1][1];
  1868. dy2 = deltas[2][1];
  1869. dy3 = deltas[3][1];
  1870. lastx = x;
  1871. lasty = y;
  1872. gdk_points[0].x = (lastx);
  1873. gdk_points[0].y = (lasty);
  1874. index = 1;
  1875. /* loop over the curve */
  1876. for (i = 0; i < subdivisions; i++)
  1877. {
  1878. /* increment the x values */
  1879. x += dx;
  1880. dx += dx2;
  1881. dx2 += dx3;
  1882. /* increment the y values */
  1883. y += dy;
  1884. dy += dy2;
  1885. dy2 += dy3;
  1886. newx = ROUND (x);
  1887. newy = ROUND (y);
  1888. /* if this point is different than the last one...then draw it */
  1889. if ((lastx != newx) || (lasty != newy))
  1890. {
  1891. /* add the point to the point buffer */
  1892. gdk_points[index].x = (newx);
  1893. gdk_points[index].y = (newy);
  1894. index++;
  1895. /* if the point buffer is full put it to the screen and zero it out */
  1896. if (index >= npoints)
  1897. {
  1898. (* points_func) (bezier_sel, gdk_points, index, udata);
  1899. index = 0;
  1900. }
  1901. }
  1902. lastx = newx;
  1903. lasty = newy;
  1904. }
  1905. /* if there are points in the buffer, then put them on the screen */
  1906. if (index)
  1907. (* points_func) (bezier_sel, gdk_points, index, udata);
  1908. }
  1909. static void
  1910. bezier_draw_segment_points (BezierSelect *bezier_sel,
  1911. GdkPoint *points,
  1912. int npoints,
  1913. gpointer udata)
  1914. {
  1915. gdk_draw_points (bezier_sel->core->win,
  1916. bezier_sel->core->gc, points, npoints);
  1917. }
  1918. static void
  1919. bezier_compose (BezierMatrix a,
  1920. BezierMatrix b,
  1921. BezierMatrix ab)
  1922. {
  1923. int i, j;
  1924. for (i = 0; i < 4; i++)
  1925. {
  1926. for (j = 0; j < 4; j++)
  1927. {
  1928. ab[i][j] = (a[i][0] * b[0][j] +
  1929. a[i][1] * b[1][j] +
  1930. a[i][2] * b[2][j] +
  1931. a[i][3] * b[3][j]);
  1932. }
  1933. }
  1934. }
  1935. static int start_convert;
  1936. static int width, height;
  1937. static int lastx;
  1938. static int lasty;
  1939. static void
  1940. bezier_convert (BezierSelect *bezier_sel,
  1941. GDisplay *gdisp,
  1942. int subdivisions,
  1943. int antialias)
  1944. {
  1945. PixelRegion maskPR;
  1946. BezierPoint * points;
  1947. BezierPoint * start_pt;
  1948. BezierPoint * next_curve;
  1949. GSList * list;
  1950. unsigned char *buf, *b;
  1951. int draw_type;
  1952. int * vals, val;
  1953. int start, end;
  1954. int x, x2, w;
  1955. int i, j;
  1956. if (!bezier_sel->closed)
  1957. gimp_fatal_error ("bezier_convert(): tried to convert an open bezier curve");
  1958. /* destroy previous mask */
  1959. if (bezier_sel->mask)
  1960. {
  1961. channel_delete (bezier_sel->mask);
  1962. bezier_sel->mask = NULL;
  1963. }
  1964. /* get the new mask's maximum extents */
  1965. if (antialias)
  1966. {
  1967. buf = (unsigned char *) g_malloc (width);
  1968. width = gdisp->gimage->width * SUPERSAMPLE;
  1969. height = gdisp->gimage->height * SUPERSAMPLE;
  1970. draw_type = AA_IMAGE_COORDS;
  1971. /* allocate value array */
  1972. vals = (int *) g_malloc (sizeof (int) * width);
  1973. }
  1974. else
  1975. {
  1976. buf = NULL;
  1977. width = gdisp->gimage->width;
  1978. height = gdisp->gimage->height;
  1979. draw_type = IMAGE_COORDS;
  1980. vals = NULL;
  1981. }
  1982. /* create a new mask */
  1983. bezier_sel->mask = channel_ref (channel_new_mask (gdisp->gimage,
  1984. gdisp->gimage->width,
  1985. gdisp->gimage->height));
  1986. /* allocate room for the scanlines */
  1987. bezier_sel->scanlines = g_malloc (sizeof (GSList *) * height);
  1988. /* zero out the scanlines */
  1989. for (i = 0; i < height; i++)
  1990. bezier_sel->scanlines[i] = NULL;
  1991. /* scan convert the curve */
  1992. points = bezier_sel->points;
  1993. start_pt = bezier_sel->points;
  1994. do {
  1995. start_convert = 1;
  1996. do {
  1997. bezier_draw_segment (bezier_sel, points,
  1998. subdivisions, draw_type,
  1999. bezier_convert_points,
  2000. NULL);
  2001. /* advance to the next segment */
  2002. points = next_anchor(points,&next_curve);
  2003. } while (points != start_pt && points);
  2004. start_pt = next_curve;
  2005. points = next_curve;
  2006. } while (next_curve);
  2007. pixel_region_init (&maskPR, drawable_data (GIMP_DRAWABLE(bezier_sel->mask)),
  2008. 0, 0,
  2009. drawable_width (GIMP_DRAWABLE(bezier_sel->mask)),
  2010. drawable_height (GIMP_DRAWABLE(bezier_sel->mask)), TRUE);
  2011. for (i = 0; i < height; i++)
  2012. {
  2013. list = bezier_sel->scanlines[i];
  2014. /* zero the vals array */
  2015. if (antialias && !(i % SUPERSAMPLE))
  2016. memset (vals, 0, width * sizeof (int));
  2017. while (list)
  2018. {
  2019. x = (long) list->data;
  2020. list = list->next;
  2021. /*
  2022. if (!list)
  2023. g_message ("cannot properly scanline convert bezier curve: %d", i);
  2024. else
  2025. */
  2026. {
  2027. /* bounds checking */
  2028. x = CLAMP (x, 0, width);
  2029. x2 = CLAMP ((long) list->data, 0, width);
  2030. w = x2 - x;
  2031. if (!antialias)
  2032. channel_add_segment (bezier_sel->mask, x, i, w, 255);
  2033. else
  2034. for (j = 0; j < w; j++)
  2035. vals[j + x] += 255;
  2036. list = g_slist_next (list);
  2037. }
  2038. }
  2039. if (antialias && !((i+1) % SUPERSAMPLE))
  2040. {
  2041. b = buf;
  2042. start = 0;
  2043. end = width;
  2044. for (j = start; j < end; j += SUPERSAMPLE)
  2045. {
  2046. val = 0;
  2047. for (x = 0; x < SUPERSAMPLE; x++)
  2048. val += vals[j + x];
  2049. *b++ = (unsigned char) (val / SUPERSAMPLE2);
  2050. }
  2051. pixel_region_set_row (&maskPR, 0, (i / SUPERSAMPLE),
  2052. drawable_width (GIMP_DRAWABLE(bezier_sel->mask)), buf);
  2053. }
  2054. g_slist_free (bezier_sel->scanlines[i]);
  2055. }
  2056. if (antialias)
  2057. {
  2058. g_free (vals);
  2059. g_free (buf);
  2060. }
  2061. g_free (bezier_sel->scanlines);
  2062. bezier_sel->scanlines = NULL;
  2063. channel_invalidate_bounds (bezier_sel->mask);
  2064. }
  2065. static void
  2066. bezier_convert_points (BezierSelect *bezier_sel,
  2067. GdkPoint *points,
  2068. int npoints,
  2069. gpointer udata)
  2070. {
  2071. int i;
  2072. if (start_convert)
  2073. start_convert = 0;
  2074. else
  2075. bezier_convert_line (bezier_sel->scanlines, lastx, lasty, points[0].x, points[0].y);
  2076. for (i = 0; i < (npoints - 1); i++)
  2077. {
  2078. bezier_convert_line (bezier_sel->scanlines,
  2079. points[i].x, points[i].y,
  2080. points[i+1].x, points[i+1].y);
  2081. }
  2082. lastx = points[npoints-1].x;
  2083. lasty = points[npoints-1].y;
  2084. }
  2085. static void
  2086. bezier_convert_line (GSList ** scanlines,
  2087. int x1,
  2088. int y1,
  2089. int x2,
  2090. int y2)
  2091. {
  2092. int dx, dy;
  2093. int error, inc;
  2094. int tmp;
  2095. double slope;
  2096. if (y1 == y2)
  2097. return;
  2098. if (y1 > y2)
  2099. {
  2100. tmp = y2; y2 = y1; y1 = tmp;
  2101. tmp = x2; x2 = x1; x1 = tmp;
  2102. }
  2103. if (y1 < 0)
  2104. {
  2105. if (y2 < 0)
  2106. return;
  2107. if (x2 == x1)
  2108. {
  2109. y1 = 0;
  2110. }
  2111. else
  2112. {
  2113. slope = (double) (y2 - y1) / (double) (x2 - x1);
  2114. x1 = x2 + (int)(0.5 + (double)(0 - y2) / slope);
  2115. y1 = 0;
  2116. }
  2117. }
  2118. if (y2 >= height)
  2119. {
  2120. if (y1 >= height)
  2121. return;
  2122. if (x2 == x1)
  2123. {
  2124. y2 = height;
  2125. }
  2126. else
  2127. {
  2128. slope = (double) (y2 - y1) / (double) (x2 - x1);
  2129. x2 = x1 + (int)(0.5 + (double)(height - y1) / slope);
  2130. y2 = height;
  2131. }
  2132. }
  2133. if (y1 == y2)
  2134. return;
  2135. dx = x2 - x1;
  2136. dy = y2 - y1;
  2137. scanlines = &scanlines[y1];
  2138. if (((dx < 0) ? -dx : dx) > ((dy < 0) ? -dy : dy))
  2139. {
  2140. if (dx < 0)
  2141. {
  2142. inc = -1;
  2143. dx = -dx;
  2144. }
  2145. else
  2146. {
  2147. inc = 1;
  2148. }
  2149. error = -dx /2;
  2150. while (x1 != x2)
  2151. {
  2152. error += dy;
  2153. if (error > 0)
  2154. {
  2155. error -= dx;
  2156. *scanlines = bezier_insert_in_list (*scanlines, x1);
  2157. scanlines++;
  2158. }
  2159. x1 += inc;
  2160. }
  2161. }
  2162. else
  2163. {
  2164. error = -dy /2;
  2165. if (dx < 0)
  2166. {
  2167. dx = -dx;
  2168. inc = -1;
  2169. }
  2170. else
  2171. {
  2172. inc = 1;
  2173. }
  2174. while (y1++ < y2)
  2175. {
  2176. *scanlines = bezier_insert_in_list (*scanlines, x1);
  2177. scanlines++;
  2178. error += dx;
  2179. if (error > 0)
  2180. {
  2181. error -= dy;
  2182. x1 += inc;
  2183. }
  2184. }
  2185. }
  2186. }
  2187. static GSList *
  2188. bezier_insert_in_list (GSList * list,
  2189. int x)
  2190. {
  2191. GSList * orig = list;
  2192. GSList * rest;
  2193. if (!list)
  2194. return g_slist_prepend (list, (void *) ((long) x));
  2195. while (list)
  2196. {
  2197. rest = g_slist_next (list);
  2198. if (x < (long) list->data)
  2199. {
  2200. rest = g_slist_prepend (rest, list->data);
  2201. list->next = rest;
  2202. list->data = (void *) ((long) x);
  2203. return orig;
  2204. }
  2205. else if (!rest)
  2206. {
  2207. g_slist_append (list, (void *) ((long) x));
  2208. return orig;
  2209. }
  2210. list = g_slist_next (list);
  2211. }
  2212. return orig;
  2213. }
  2214. gboolean
  2215. bezier_tool_selected()
  2216. {
  2217. return(active_tool &&
  2218. active_tool->type == BEZIER_SELECT &&
  2219. active_tool->state == ACTIVE);
  2220. }
  2221. void
  2222. bezier_paste_bezierselect_to_current(GDisplay *gdisp,BezierSelect *bsel)
  2223. {
  2224. BezierPoint *pts;
  2225. gint i;
  2226. Tool * tool;
  2227. BezierPoint *bpnt = NULL;
  2228. int need_move = 0;
  2229. /* g_print ("bezier_paste_bezierselect_to_current::\n"); */
  2230. /* printSel(bsel); */
  2231. /* If the tool was being used before clear it */
  2232. if (active_tool &&
  2233. active_tool->type == BEZIER_SELECT &&
  2234. active_tool->state == ACTIVE)
  2235. {
  2236. BezierSelect *bezier_sel = (BezierSelect*)active_tool->private;
  2237. if(bezier_sel)
  2238. {
  2239. draw_core_stop ( curSel->core, active_tool );
  2240. bezier_select_reset (bezier_sel);
  2241. }
  2242. }
  2243. gimp_context_set_tool (gimp_context_get_user (), BEZIER_SELECT);
  2244. active_tool->paused_count = 0;
  2245. active_tool->gdisp_ptr = gdisp;
  2246. active_tool->drawable = gimage_active_drawable (gdisp->gimage);
  2247. tool = active_tool;
  2248. bezier_select_reset( curSel );
  2249. draw_core_start (curSel->core, gdisp->canvas->window, tool);
  2250. tool->state = ACTIVE;
  2251. pts = (BezierPoint *) bsel->points;
  2252. for (i=0; i< bsel->num_points; i++)
  2253. {
  2254. if(need_move)
  2255. {
  2256. bezier_add_point( curSel, BEZIER_MOVE, pts->x, pts->y);
  2257. need_move = 0;
  2258. }
  2259. else
  2260. bezier_add_point( curSel, pts->type, pts->x, pts->y);
  2261. if(bpnt == NULL)
  2262. bpnt = curSel->last_point;
  2263. if(pts->next_curve)
  2264. {
  2265. /* g_print ("bezier_paste_bezierselect_to_current:: Close last curve off \n"); */
  2266. curSel->last_point->next = bpnt;
  2267. bpnt->prev = curSel->last_point;
  2268. curSel->cur_anchor = NULL;
  2269. curSel->cur_control = NULL;
  2270. bpnt = NULL;
  2271. need_move = 1;
  2272. pts = pts->next_curve;
  2273. }
  2274. else
  2275. {
  2276. pts = pts->next;
  2277. }
  2278. }
  2279. if ( bsel->closed )
  2280. {
  2281. curSel->last_point->next = bpnt;
  2282. bpnt->prev = curSel->last_point;
  2283. curSel->cur_anchor = curSel->points;
  2284. curSel->cur_control = curSel->points->next;
  2285. curSel->closed = 1;
  2286. if (curTool->gdisp_ptr)
  2287. bezier_convert(curSel, curTool->gdisp_ptr, SUBDIVIDE, NO);
  2288. }
  2289. /* g_print ("After pasting...\n"); */
  2290. /* printSel(curSel); */
  2291. if(bsel->num_points == 0)
  2292. {
  2293. curSel->state = BEZIER_START;
  2294. curSel->draw = 0;
  2295. draw_core_stop( curSel->core, curTool);
  2296. }
  2297. else
  2298. {
  2299. curSel->state = bsel->state;
  2300. curSel->draw = BEZIER_DRAW_ALL;
  2301. draw_core_resume( curSel->core, curTool);
  2302. }
  2303. }
  2304. static void
  2305. bezier_to_sel_internal(BezierSelect *bezier_sel,
  2306. Tool *tool,
  2307. GDisplay *gdisp,
  2308. gint op,
  2309. gint replace)
  2310. {
  2311. /* If we're antialiased, then recompute the
  2312. * mask...
  2313. */
  2314. if (bezier_options->antialias)
  2315. bezier_convert (bezier_sel, tool->gdisp_ptr, SUBDIVIDE, YES);
  2316. /* if (!bezier_options->extend) */
  2317. /* { */
  2318. /* tool->state = INACTIVE; */
  2319. /* bezier_sel->draw = BEZIER_DRAW_CURVE; */
  2320. /* draw_core_resume (bezier_sel->core, tool); */
  2321. /* bezier_sel->draw = 0; */
  2322. /* draw_core_stop (bezier_sel->core, tool); */
  2323. /* } */
  2324. if (replace)
  2325. gimage_mask_clear (gdisp->gimage);
  2326. else
  2327. gimage_mask_undo (gdisp->gimage);
  2328. if (bezier_options->feather)
  2329. channel_feather (bezier_sel->mask,
  2330. gimage_get_mask (gdisp->gimage),
  2331. bezier_options->feather_radius,
  2332. bezier_options->feather_radius,
  2333. op, 0, 0);
  2334. else
  2335. channel_combine_mask (gimage_get_mask (gdisp->gimage),
  2336. bezier_sel->mask, op, 0, 0);
  2337. /* show selection on all views */
  2338. gdisplays_flush ();
  2339. }
  2340. static int
  2341. test_add_point_on_segment (BezierSelect *bezier_sel,
  2342. BezierPoint *pt,
  2343. int subdivisions,
  2344. int space,
  2345. int xpos,
  2346. int ypos,
  2347. int halfwidth)
  2348. {
  2349. BezierPoint *points;
  2350. BezierMatrix geometry;
  2351. BezierMatrix tmp1, tmp2;
  2352. BezierMatrix deltas;
  2353. double x, dx, dx2, dx3;
  2354. double y, dy, dy2, dy3;
  2355. double d, d2, d3;
  2356. int lastx, lasty;
  2357. int newx, newy;
  2358. int i;
  2359. double ratio;
  2360. /* construct the geometry matrix from the segment */
  2361. /* assumes that a valid segment containing 4 points is passed in */
  2362. /* ALT ignore invalid segments since we might be working on an open curve */
  2363. points = pt;
  2364. ratio = -1.0;
  2365. for (i = 0; i < 4; i++)
  2366. {
  2367. if (!points)
  2368. return 0;
  2369. switch (space)
  2370. {
  2371. case IMAGE_COORDS:
  2372. geometry[i][0] = points->x;
  2373. geometry[i][1] = points->y;
  2374. break;
  2375. case AA_IMAGE_COORDS:
  2376. geometry[i][0] = RINT(points->x * SUPERSAMPLE);
  2377. geometry[i][1] = RINT(points->y * SUPERSAMPLE);
  2378. break;
  2379. case SCREEN_COORDS:
  2380. geometry[i][0] = points->sx;
  2381. geometry[i][1] = points->sy;
  2382. break;
  2383. default:
  2384. gimp_fatal_error ("test_add_point_on_segment(): Unknown coordinate space: %d", space);
  2385. break;
  2386. }
  2387. geometry[i][2] = 0;
  2388. geometry[i][3] = 0;
  2389. points = points->next;
  2390. }
  2391. /* subdivide the curve n times */
  2392. /* n can be adjusted to give a finer or coarser curve */
  2393. d = 1.0 / subdivisions;
  2394. d2 = d * d;
  2395. d3 = d * d * d;
  2396. /* construct a temporary matrix for determining the forward diffencing deltas */
  2397. tmp2[0][0] = 0; tmp2[0][1] = 0; tmp2[0][2] = 0; tmp2[0][3] = 1;
  2398. tmp2[1][0] = d3; tmp2[1][1] = d2; tmp2[1][2] = d; tmp2[1][3] = 0;
  2399. tmp2[2][0] = 6*d3; tmp2[2][1] = 2*d2; tmp2[2][2] = 0; tmp2[2][3] = 0;
  2400. tmp2[3][0] = 6*d3; tmp2[3][1] = 0; tmp2[3][2] = 0; tmp2[3][3] = 0;
  2401. /* compose the basis and geometry matrices */
  2402. bezier_compose (basis, geometry, tmp1);
  2403. /* compose the above results to get the deltas matrix */
  2404. bezier_compose (tmp2, tmp1, deltas);
  2405. /* extract the x deltas */
  2406. x = deltas[0][0];
  2407. dx = deltas[1][0];
  2408. dx2 = deltas[2][0];
  2409. dx3 = deltas[3][0];
  2410. /* extract the y deltas */
  2411. y = deltas[0][1];
  2412. dy = deltas[1][1];
  2413. dy2 = deltas[2][1];
  2414. dy3 = deltas[3][1];
  2415. lastx = x;
  2416. lasty = y;
  2417. /* loop over the curve */
  2418. for (i = 0; i < subdivisions; i++)
  2419. {
  2420. /* increment the x values */
  2421. x += dx;
  2422. dx += dx2;
  2423. dx2 += dx3;
  2424. /* increment the y values */
  2425. y += dy;
  2426. dy += dy2;
  2427. dy2 += dy3;
  2428. newx = ROUND (x);
  2429. newy = ROUND (y);
  2430. /* if this point is different than the last one...then test it */
  2431. if ((lastx != newx) || (lasty != newy))
  2432. {
  2433. int l, r, b, t;
  2434. l = newx - halfwidth;
  2435. r = newx + halfwidth;
  2436. t = newy - halfwidth;
  2437. b = newy + halfwidth;
  2438. if ((xpos >= l) && (xpos <= r) && (ypos >= t) && (ypos <= b))
  2439. {
  2440. /* so we found one point in the square hit */
  2441. ratio = (double)i/(double)subdivisions;
  2442. /* We found the exact point on the curve, so take it ...*/
  2443. if ((xpos==newx) && (ypos==newy)) break;
  2444. /* to Implement :
  2445. keep each time the nearest point of the curve from where we've clicked
  2446. in the case where we haven't click exactely on the curve.
  2447. */
  2448. }
  2449. }
  2450. lastx = newx;
  2451. lasty = newy;
  2452. }
  2453. /* we found a point on the curve */
  2454. if (ratio >= 0.0)
  2455. {
  2456. BezierPoint *pts, *pt1, *pt2, *pt3;
  2457. BezierPoint *P00, *P01, *P02, *P03;
  2458. BezierPoint P10, P11, P12;
  2459. BezierPoint P20, P21;
  2460. BezierPoint P30;
  2461. pts = pt;
  2462. P00 = pts;
  2463. pts = pts->next;
  2464. P01 = pts;
  2465. pts = pts->next;
  2466. P02 = pts;
  2467. pts = pts->next;
  2468. P03 = pts;
  2469. /* De Casteljau algorithme
  2470. [Advanced Animation & Randering Technics / Alan & Mark WATT]
  2471. [ADDISON WESLEY ref 54412]
  2472. Iteratif way of drawing a Bezier curve by geometrical approch
  2473. P0x represent the four controls points ( anchor / control /control /anchor )
  2474. P30 represent the new anchor point to add on the curve
  2475. P2x represent the new control points of P30
  2476. P1x represent the new values of the control points P01 and P02
  2477. so if we moves ratio from 0 to 1 we draw the all curve between P00 and P03
  2478. */
  2479. P10.x = (int)((1-ratio)*P00->x + ratio * P01->x);
  2480. P10.y = (int)((1-ratio)*P00->y + ratio * P01->y);
  2481. P11.x = (1-ratio)*P01->x + ratio * P02->x;
  2482. P11.y = (1-ratio)*P01->y + ratio * P02->y;
  2483. P12.x = (1-ratio)*P02->x + ratio * P03->x;
  2484. P12.y = (1-ratio)*P02->y + ratio * P03->y;
  2485. P20.x = (1-ratio)*P10.x + ratio * P11.x;
  2486. P20.y = (1-ratio)*P10.y + ratio * P11.y;
  2487. P21.x = (1-ratio)*P11.x + ratio * P12.x;
  2488. P21.y = (1-ratio)*P11.y + ratio * P12.y;
  2489. P30.x = (1-ratio)*P20.x + ratio * P21.x;
  2490. P30.y = (1-ratio)*P20.y + ratio * P21.y;
  2491. P01->x = P10.x;
  2492. P01->y = P10.y;
  2493. P02->x = P12.x;
  2494. P02->y = P12.y;
  2495. /* All the computes are done, let's insert the new point on the curve */
  2496. pt1 = g_new0(BezierPoint,1);
  2497. pt2 = g_new0(BezierPoint,1);
  2498. pt3 = g_new0(BezierPoint,1);
  2499. pt1->type = BEZIER_CONTROL;
  2500. pt2->type = BEZIER_ANCHOR;
  2501. pt3->type = BEZIER_CONTROL;
  2502. pt1->x = P20.x; pt1->y = P20.y;
  2503. pt2->x = P30.x; pt2->y = P30.y;
  2504. pt3->x = P21.x; pt3->y = P21.y;
  2505. pt3->next_curve = P01->next_curve;
  2506. P01->next = pt1;
  2507. pt1->prev = P01;
  2508. pt1->next = pt2;
  2509. pt2->prev = pt1;
  2510. pt2->next = pt3;
  2511. pt3->prev = pt2;
  2512. pt3->next = P02;
  2513. P02->prev = pt3;
  2514. bezier_sel->num_points += 3;
  2515. bezier_sel->cur_anchor = pt2;
  2516. bezier_sel->cur_control = pt1;
  2517. return 1;
  2518. }
  2519. return 0;
  2520. }
  2521. void bezier_select_mode(gint mode)
  2522. {
  2523. ModeEdit = mode;
  2524. }
  2525. /* The curve has to be closed to do a selection. */
  2526. void
  2527. bezier_to_selection(BezierSelect *bezier_sel,
  2528. GDisplay *gdisp)
  2529. {
  2530. /* Call the internal function */
  2531. if(!bezier_sel->closed)
  2532. {
  2533. g_message(_("Curve not closed!"));
  2534. return;
  2535. }
  2536. /* force the passed selection to be the current selection..*/
  2537. /* This loads it into curSel for this image */
  2538. bezier_paste_bezierselect_to_current(gdisp,bezier_sel);
  2539. bezier_to_sel_internal(curSel,curTool,gdisp,ADD,1);
  2540. }
  2541. void printSel( BezierSelect *sel)
  2542. {
  2543. BezierPoint *pt;
  2544. BezierPoint *start_pt;
  2545. int i;
  2546. pt = sel->points;
  2547. start_pt = pt;
  2548. g_print ("\n");
  2549. for(i=0; i<sel->num_points; i++)
  2550. {
  2551. g_print ("%d(%p) x,y=%f,%f type=%d next=%p prev=%p next_curve=%p\n", i,pt, pt->x, pt->y,pt->type,pt->next,pt->prev,pt->next_curve);
  2552. if(pt->next != start_pt && pt->next_curve)
  2553. g_print ("Curve out a sync!!\n");
  2554. if(pt->next_curve)
  2555. {
  2556. pt = pt->next_curve;
  2557. start_pt = pt;
  2558. }
  2559. else
  2560. pt = pt->next;
  2561. }
  2562. g_print ("core : %p\n", sel->core);
  2563. g_print ("closed : %d\n", sel->closed);
  2564. g_print ("draw : %d\n", sel->draw);
  2565. g_print ("state: %d\n", sel->state);
  2566. }
  2567. /* check whether vectors (offx, offy), (l_offx, l_offy) have the same angle. */
  2568. static gboolean
  2569. stroke_interpolatable (int offx, int offy, int l_offx, int l_offy, gdouble error)
  2570. {
  2571. if ((offx == l_offx) & (offy == l_offy))
  2572. return TRUE;
  2573. else if ((offx == 0) | (l_offx == 0))
  2574. if (offx == l_offx)
  2575. return ((0 <= offy) & (0 <= offy)) | ((offy < 0) & (l_offy < 0));
  2576. else
  2577. return FALSE;
  2578. else if ((offy == 0) | (l_offy == 0))
  2579. if (offy == l_offy)
  2580. return ((0 < offx) & (0 < l_offx)) | ((offx < 0) & (l_offx < 0));
  2581. else
  2582. return FALSE;
  2583. /* At this point, we can assert: offx, offy, l_offx, l_offy != 0 */
  2584. else if (((0 < offx) & (0 < l_offx)) | ((offx < 0) & (l_offx < 0)))
  2585. {
  2586. gdouble grad1, grad2;
  2587. if (ABS (offy) < ABS (offx))
  2588. {
  2589. grad1 = (gdouble) offy / (gdouble) offx;
  2590. grad2 = (gdouble) l_offy / (gdouble) l_offx;
  2591. }
  2592. else
  2593. {
  2594. grad1 = (gdouble) offx / (gdouble) offy;
  2595. grad2 = (gdouble) l_offx / (gdouble) l_offy;
  2596. }
  2597. /* printf ("error: %f / %f\n", ABS (grad1 - grad2), error); */
  2598. return (ABS (grad1 - grad2) <= error);
  2599. }
  2600. else
  2601. return FALSE;
  2602. }
  2603. static void
  2604. bezier_stack_points_aux (GdkPoint *points,
  2605. int start,
  2606. int end,
  2607. gdouble error,
  2608. BezierRenderPnts *rpnts)
  2609. {
  2610. const gint expand_size = 32;
  2611. gint med;
  2612. gint offx, offy, l_offx, l_offy;
  2613. if (rpnts->stroke_points == NULL)
  2614. return;
  2615. /* BASE CASE: stack the end point */
  2616. if (end - start <= 1)
  2617. {
  2618. if ((rpnts->stroke_points[rpnts->num_stroke_points * 2 - 2] == points[end].x)
  2619. && (rpnts->stroke_points[rpnts->num_stroke_points * 2 - 1] == points[end].y))
  2620. return;
  2621. rpnts->num_stroke_points++;
  2622. if (rpnts->len_stroke_points <= rpnts->num_stroke_points)
  2623. {
  2624. rpnts->len_stroke_points += expand_size;
  2625. rpnts->stroke_points = g_renew (double, rpnts->stroke_points, 2 * rpnts->len_stroke_points);
  2626. if (rpnts->stroke_points == NULL)
  2627. {
  2628. rpnts->len_stroke_points = rpnts->num_stroke_points = 0;
  2629. return;
  2630. }
  2631. }
  2632. rpnts->stroke_points[rpnts->num_stroke_points * 2 - 2] = points[end].x;
  2633. rpnts->stroke_points[rpnts->num_stroke_points * 2 - 1] = points[end].y;
  2634. return;
  2635. }
  2636. if (end - start <= 32)
  2637. {
  2638. gint i;
  2639. for (i = start+ 1; i <= end; i++)
  2640. bezier_stack_points_aux (points, i, i, 0,rpnts);
  2641. return;
  2642. }
  2643. /* Otherwise, check whether to divide the segment recursively */
  2644. offx = points[end].x - points[start].x;
  2645. offy = points[end].y - points[start].y;
  2646. med = (end + start) / 2;
  2647. l_offx = points[med].x - points[start].x;
  2648. l_offy = points[med].y - points[start].y;
  2649. if (! stroke_interpolatable (offx, offy, l_offx, l_offy, error))
  2650. {
  2651. bezier_stack_points_aux (points, start, med, error, rpnts);
  2652. bezier_stack_points_aux (points, med, end, error, rpnts);
  2653. return;
  2654. }
  2655. l_offx = points[end].x - points[med].x;
  2656. l_offy = points[end].y - points[med].y;
  2657. if (! stroke_interpolatable (offx, offy, l_offx, l_offy, error))
  2658. {
  2659. bezier_stack_points_aux (points, start, med, error, rpnts);
  2660. bezier_stack_points_aux (points, med, end, error, rpnts);
  2661. return;
  2662. }
  2663. /* Now, the curve can be represented by a points pair: (start, end).
  2664. So, add the last point to stroke_points. */
  2665. bezier_stack_points_aux (points, end, end, 0, rpnts);
  2666. }
  2667. static void
  2668. bezier_stack_points (BezierSelect *bezier_sel,
  2669. GdkPoint *points,
  2670. int npoints,
  2671. gpointer udata)
  2672. {
  2673. gint i;
  2674. gint expand_size = 32;
  2675. gint minx, maxx, miny, maxy;
  2676. gdouble error;
  2677. BezierRenderPnts *rpnts = udata;
  2678. if (npoints < 2) /* Does this happen? */
  2679. return;
  2680. if (rpnts->stroke_points == NULL) /* initialize it here */
  2681. {
  2682. rpnts->num_stroke_points = 0;
  2683. rpnts->len_stroke_points = expand_size;
  2684. rpnts->stroke_points = g_new (double, 2 * rpnts->len_stroke_points);
  2685. }
  2686. maxx = minx = points[0].x;
  2687. maxy = miny = points[0].y;
  2688. for (i = 1; i < npoints; i++)
  2689. {
  2690. if (points[i].x < minx)
  2691. minx = points[i].x;
  2692. else if (maxx < points[i].x)
  2693. maxx = points[i].x;
  2694. if (points[i].y < miny)
  2695. miny = points[i].x;
  2696. else if (maxy < points[i].y)
  2697. maxy = points[i].y;
  2698. }
  2699. /* allow one pixel fluctuation */
  2700. error = 2.0 / MAX(maxx - minx, maxy - miny);
  2701. error = 0.0; /* ALT */
  2702. /* add the start point */
  2703. bezier_stack_points_aux (points, 0, 0, 0, rpnts);
  2704. /* divide segments recursively */
  2705. bezier_stack_points_aux (points, 0, npoints - 1, error, rpnts);
  2706. /* printf ("npoints: %d\n", npoints); */
  2707. }
  2708. static gint
  2709. bezier_gen_points(BezierSelect *bezier_sel,
  2710. int open_path,
  2711. BezierRenderPnts *rpnts)
  2712. {
  2713. BezierPoint * points;
  2714. BezierPoint * start_pt;
  2715. BezierPoint * next_curve;
  2716. BezierRenderPnts *next_rpnts = rpnts;
  2717. gint point_counts = 0;
  2718. /* stack points */
  2719. points = bezier_sel->points;
  2720. start_pt = bezier_sel->points;
  2721. if (bezier_sel->num_points >= 4)
  2722. {
  2723. do {
  2724. point_counts = count_points_on_curve (points);
  2725. if (point_counts < 4)
  2726. return(TRUE);
  2727. do {
  2728. bezier_draw_segment (bezier_sel, points,
  2729. SUBDIVIDE, AA_IMAGE_COORDS,
  2730. bezier_stack_points,
  2731. (gpointer) next_rpnts);
  2732. points = next_anchor(points,&next_curve);
  2733. } while (points != start_pt && points);
  2734. start_pt = next_curve;
  2735. points = next_curve;
  2736. if(next_curve)
  2737. {
  2738. next_rpnts->next_curve = g_new0 (BezierRenderPnts, 1);
  2739. next_rpnts = next_rpnts->next_curve;
  2740. }
  2741. } while (next_curve);
  2742. }
  2743. return (TRUE);
  2744. }
  2745. void
  2746. bezier_stroke (BezierSelect *bezier_sel,
  2747. GDisplay *gdisp,
  2748. int subdivisions,
  2749. int open_path)
  2750. {
  2751. Argument *return_vals;
  2752. BezierRenderPnts *next_rpnts;
  2753. BezierRenderPnts *rpnts;
  2754. GimpDrawable *drawable;
  2755. gint offset_x, offset_y;
  2756. gint nreturn_vals;
  2757. drawable = gimage_active_drawable (gdisp->gimage);
  2758. if (!drawable)
  2759. return;
  2760. rpnts = g_new0 (BezierRenderPnts, 1);
  2761. gimp_drawable_offsets (drawable, &offset_x, &offset_y);
  2762. /* Start an undo group */
  2763. undo_push_group_start (gdisp->gimage, PAINT_CORE_UNDO);
  2764. bezier_gen_points (bezier_sel, open_path, rpnts);
  2765. do
  2766. {
  2767. if (rpnts->stroke_points)
  2768. {
  2769. gdouble *ptr;
  2770. ptr = rpnts->stroke_points;
  2771. while (ptr < rpnts->stroke_points + (rpnts->num_stroke_points * 2))
  2772. {
  2773. *ptr /= SUPERSAMPLE;
  2774. *ptr++ -= offset_x;
  2775. *ptr /= SUPERSAMPLE;
  2776. *ptr++ -= offset_y;
  2777. }
  2778. /* Stroke with the correct tool */
  2779. return_vals =
  2780. procedural_db_run_proc (tool_active_PDB_string(),
  2781. &nreturn_vals,
  2782. PDB_DRAWABLE, drawable_ID (drawable),
  2783. PDB_INT32, (gint32) rpnts->num_stroke_points * 2,
  2784. PDB_FLOATARRAY, rpnts->stroke_points,
  2785. PDB_END);
  2786. if (return_vals && return_vals[0].value.pdb_int != PDB_SUCCESS)
  2787. g_message (_("Paintbrush operation failed."));
  2788. procedural_db_destroy_args (return_vals, nreturn_vals);
  2789. g_free (rpnts->stroke_points);
  2790. }
  2791. next_rpnts = rpnts->next_curve;
  2792. rpnts->stroke_points = NULL;
  2793. rpnts->len_stroke_points = rpnts->num_stroke_points = 0;
  2794. g_free (rpnts);
  2795. rpnts = next_rpnts;
  2796. }
  2797. while (rpnts);
  2798. /* End an undo group */
  2799. undo_push_group_end (gdisp->gimage);
  2800. gdisplays_flush ();
  2801. }
  2802. static void
  2803. bezier_draw_segment_for_distance (BezierSelect *bezier_sel,
  2804. BezierPoint *points,
  2805. int subdivisions,
  2806. BezierDistance *bdist)
  2807. {
  2808. BezierMatrix geometry;
  2809. BezierMatrix tmp1, tmp2;
  2810. BezierMatrix deltas;
  2811. double x, dx, dx2, dx3;
  2812. double y, dy, dy2, dy3;
  2813. double d, d2, d3;
  2814. int index;
  2815. int i;
  2816. /* construct the geometry matrix from the segment */
  2817. /* assumes that a valid segment containing 4 points is passed in */
  2818. if(bdist->found)
  2819. return;
  2820. for (i = 0; i < 4; i++)
  2821. {
  2822. if (!points)
  2823. gimp_fatal_error ("bezier_draw_segment_for_distance(): Bad bezier segment");
  2824. geometry[i][0] = points->x;
  2825. geometry[i][1] = points->y;
  2826. geometry[i][2] = 0;
  2827. geometry[i][3] = 0;
  2828. points = points->next;
  2829. }
  2830. /* subdivide the curve n times */
  2831. /* n can be adjusted to give a finer or coarser curve */
  2832. d = 1.0 / subdivisions;
  2833. d2 = d * d;
  2834. d3 = d * d * d;
  2835. /* construct a temporary matrix for determining the forward diffencing deltas */
  2836. tmp2[0][0] = 0; tmp2[0][1] = 0; tmp2[0][2] = 0; tmp2[0][3] = 1;
  2837. tmp2[1][0] = d3; tmp2[1][1] = d2; tmp2[1][2] = d; tmp2[1][3] = 0;
  2838. tmp2[2][0] = 6*d3; tmp2[2][1] = 2*d2; tmp2[2][2] = 0; tmp2[2][3] = 0;
  2839. tmp2[3][0] = 6*d3; tmp2[3][1] = 0; tmp2[3][2] = 0; tmp2[3][3] = 0;
  2840. /* compose the basis and geometry matrices */
  2841. bezier_compose (basis, geometry, tmp1);
  2842. /* compose the above results to get the deltas matrix */
  2843. bezier_compose (tmp2, tmp1, deltas);
  2844. /* extract the x deltas */
  2845. x = deltas[0][0];
  2846. dx = deltas[1][0];
  2847. dx2 = deltas[2][0];
  2848. dx3 = deltas[3][0];
  2849. /* extract the y deltas */
  2850. y = deltas[0][1];
  2851. dy = deltas[1][1];
  2852. dy2 = deltas[2][1];
  2853. dy3 = deltas[3][1];
  2854. index = 1;
  2855. /* loop over the curve */
  2856. for (i = 0; i < subdivisions; i++)
  2857. {
  2858. /* increment the x values */
  2859. x += dx;
  2860. dx += dx2;
  2861. dx2 += dx3;
  2862. /* increment the y values */
  2863. y += dy;
  2864. dy += dy2;
  2865. dy2 += dy3;
  2866. /* g_print ("x = %g, y = %g\n",x,y); */
  2867. /* if this point is different than the last one...then draw it */
  2868. /* Note :
  2869. * It assumes the udata is the place we want the
  2870. * floating version of the coords to be stuffed.
  2871. * These are needed when we calculate the gradient of the
  2872. * curve.
  2873. */
  2874. if(!bdist->firstpnt)
  2875. {
  2876. gdouble rx = x;
  2877. gdouble ry = y;
  2878. gdouble dx = bdist->lastx - rx;
  2879. gdouble dy = bdist->lasty - ry;
  2880. bdist->curdist += sqrt((dx*dx)+(dy*dy));
  2881. if(bdist->curdist >= bdist->dist)
  2882. {
  2883. *(bdist->x) = ROUND((rx + dx/2));
  2884. *(bdist->y) = ROUND((ry + dy/2));
  2885. if(dx == 0.0)
  2886. *(bdist->gradient) = G_MAXDOUBLE;
  2887. else
  2888. *(bdist->gradient) = dy/dx;
  2889. /* g_print ("found x = %d, y = %d\n",*(bdist->x),*(bdist->y)); */
  2890. bdist->found = TRUE;
  2891. break;
  2892. }
  2893. bdist->lastx = rx;
  2894. bdist->lasty = ry;
  2895. }
  2896. else
  2897. {
  2898. bdist->firstpnt = FALSE;
  2899. bdist->lastx = x;
  2900. bdist->lasty = y;
  2901. }
  2902. }
  2903. }
  2904. static void
  2905. bezier_draw_curve_for_distance (BezierSelect *bezier_sel,
  2906. BezierDistance *udata)
  2907. {
  2908. BezierPoint * points;
  2909. BezierPoint * start_pt;
  2910. BezierPoint * next_curve;
  2911. points = bezier_sel->points;
  2912. start_pt = bezier_sel->points;
  2913. if(bezier_sel->num_points >= 4)
  2914. {
  2915. do {
  2916. do {
  2917. bezier_draw_segment_for_distance (bezier_sel, points,
  2918. SUBDIVIDE,
  2919. udata);
  2920. points = next_anchor(points,&next_curve);
  2921. } while (points != start_pt && points);
  2922. start_pt = next_curve;
  2923. points = next_curve;
  2924. } while (next_curve);
  2925. }
  2926. }
  2927. gint
  2928. bezier_distance_along(BezierSelect *bezier_sel,
  2929. int open_path,
  2930. gdouble dist,
  2931. gint *x,
  2932. gint *y,
  2933. gdouble *gradient)
  2934. {
  2935. /* Render the curve as points then walk along it... */
  2936. BezierDistance *bdist = g_new0(BezierDistance,1);
  2937. gint ret;
  2938. bdist->firstpnt = TRUE;
  2939. bdist->curdist = 0.0;
  2940. bdist->lastx = 0.0;
  2941. bdist->lasty = 0.0;
  2942. bdist->dist = dist;
  2943. bdist->x = x;
  2944. bdist->y = y;
  2945. bdist->gradient = gradient;
  2946. bdist->found = FALSE;
  2947. bezier_draw_curve_for_distance (bezier_sel,bdist);
  2948. ret = bdist->found;
  2949. g_free(bdist);
  2950. return (ret);
  2951. }