Debian Codegraph @

fontforgeexe/cvaddpoints.c

raw symbols SCIP
1 /* Copyright (C) 2000-2012 by George Williams */
2 /*
3 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted provided that the following conditions are met:
5
6 * Redistributions of source code must retain the above copyright notice, this
7 * list of conditions and the following disclaimer.
8
9 * Redistributions in binary form must reproduce the above copyright notice,
10 * this list of conditions and the following disclaimer in the documentation
11 * and/or other materials provided with the distribution.
12
13 * The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #include <fontforge-config.h>
29
30 #include "cvundoes.h"
31 #include "fontforgeui.h"
32 #include "spiro.h"
33 #include "splinefont.h"
34 #include "splineorder2.h"
35 #include "splineutil.h"
36 #include "splineutil2.h"
37 #include "ustring.h"
38
39 #include <math.h>
40
41 int CVOneThingSel(CharView *cv, SplinePoint **sp, SplinePointList **_spl,
42 RefChar **ref, ImageList **img, AnchorPoint **ap, spiro_cp **scp) {
43 /* if there is exactly one thing selected return it */
44 SplinePointList *spl, *found=NULL;
45 Spline *spline;
46 SplinePoint *foundsp=NULL;
47 RefChar *refs, *foundref=NULL;
48 ImageList *imgs, *foundimg=NULL;
49 AnchorPoint *aps, *foundap=NULL;
50 spiro_cp *foundcp = NULL;
51 int i;
52
53 *sp = NULL; *_spl=NULL; *ref=NULL; *img = NULL; *scp = NULL;
54 if ( ap ) *ap = NULL;
55 for ( spl= cv->b.layerheads[cv->b.drawmode]->splines; spl!=NULL; spl=spl->next ) {
56 if ( !cv->b.sc->inspiro || !hasspiro()) {
57 if ( spl->first->selected ) {
58 if ( found!=NULL )
59 return( 0 ); /* At least two points */
60 found = spl; foundsp = spl->first;
61 }
62 for ( spline = spl->first->next; spline!=NULL ; spline=spline->to->next ) {
63 if ( spline->to==spl->first )
64 break;
65 if ( spline->to->selected ) {
66 if ( found!=NULL )
67 return( 0 );
68 found = spl; foundsp = spline->to;
69 }
70 }
71 } else {
72 for ( i=0; i<spl->spiro_cnt-1; ++i ) {
73 if ( SPIRO_SELECTED(&spl->spiros[i])) {
74 if ( found )
75 return( 0 );
76 found = spl;
77 foundcp = &spl->spiros[i];
78 }
79 }
80 }
81 }
82 *sp = foundsp; *scp = foundcp; *_spl = found;
83
84 if ( cv->b.drawmode!=dm_grid ) {
85 for ( refs=cv->b.layerheads[cv->b.drawmode]->refs; refs!=NULL; refs = refs->next ) {
86 if ( refs->selected ) {
87 if ( found!=NULL || foundref!=NULL )
88 return( 0 );
89 foundref = refs;
90 }
91 }
92 *ref = foundref;
93 if ( cv->showanchor && ap!=NULL ) {
94 for ( aps=cv->b.sc->anchor; aps!=NULL; aps=aps->next ) {
95 if ( aps->selected ) {
96 if ( found!=NULL || foundref!=NULL || foundap!=NULL )
97 return( 0 );
98 foundap = aps;
99 }
100 }
101 *ap = foundap;
102 }
103 }
104
105 for ( imgs=cv->b.layerheads[cv->b.drawmode]->images; imgs!=NULL; imgs = imgs->next ) {
106 if ( imgs->selected ) {
107 if ( found!=NULL || foundimg!=NULL )
108 return( 0 );
109 foundimg = imgs;
110 }
111 }
112 *img = foundimg;
113
114 if ( found )
115 return( foundimg==NULL && foundref==NULL && foundap==NULL );
116 else if ( foundref || foundimg || foundap )
117 return( true );
118
119 return( false );
120 }
121
122 int CVOneContourSel(CharView *cv, SplinePointList **_spl,
123 RefChar **ref, ImageList **img) {
124 /* if there is exactly one contour/image/reg selected return it */
125 SplinePointList *spl, *found=NULL;
126 Spline *spline;
127 RefChar *refs, *foundref=NULL;
128 ImageList *imgs, *foundimg=NULL;
129 int i;
130
131 *_spl=NULL; *ref=NULL; *img = NULL;
132 for ( spl= cv->b.layerheads[cv->b.drawmode]->splines; spl!=NULL; spl=spl->next ) {
133 if ( !cv->b.sc->inspiro || !hasspiro()) {
134 if ( spl->first->selected ) {
135 if ( found!=NULL && found!=spl )
136 return( 0 ); /* At least two contours */
137 found = spl;
138 }
139 for ( spline = spl->first->next; spline!=NULL ; spline=spline->to->next ) {
140 if ( spline->to==spl->first )
141 break;
142 if ( spline->to->selected ) {
143 if ( found!=NULL && found!=spl )
144 return( 0 );
145 found = spl;
146 }
147 }
148 } else {
149 for ( i=0; i<spl->spiro_cnt-1; ++i ) {
150 if ( SPIRO_SELECTED(&spl->spiros[i])) {
151 if ( found!=NULL && found!=spl )
152 return( 0 );
153 found = spl;
154 }
155 }
156 }
157 }
158 *_spl = found;
159
160 if ( cv->b.drawmode==dm_fore ) {
161 for ( refs=cv->b.layerheads[cv->b.drawmode]->refs; refs!=NULL; refs = refs->next ) {
162 if ( refs->selected ) {
163 if ( found!=NULL || foundref!=NULL )
164 return( 0 );
165 foundref = refs;
166 }
167 }
168 *ref = foundref;
169 }
170
171 for ( imgs=cv->b.layerheads[cv->b.drawmode]->images; imgs!=NULL; imgs = imgs->next ) {
172 if ( imgs->selected ) {
173 if ( found!=NULL || foundimg!=NULL )
174 return( 0 );
175 foundimg = imgs;
176 }
177 }
178 *img = foundimg;
179
180 if ( found )
181 return( foundimg==NULL && foundref==NULL );
182 else if ( foundref || foundimg )
183 return( true );
184
185 return( false );
186 }
187
188 SplinePointList *CVAnySelPointList(CharView *cv) {
189 /* if there is exactly one point selected and it is on an open splineset */
190 /* and it is one of the endpoints of the splineset, then return that */
191 /* splineset */
192 SplinePointList *spl, *found=NULL;
193 Spline *spline, *first;
194 int i;
195
196 for ( spl= cv->b.layerheads[cv->b.drawmode]->splines; spl!=NULL; spl=spl->next ) {
197 if ( cv->b.sc->inspiro && hasspiro()) {
198 for ( i = 0; i<spl->spiro_cnt-1; ++i ) {
199 if ( SPIRO_SELECTED(&spl->spiros[i])) {
200 /* Only interesting if the single selection is at the */
201 /* start/end of an open contour */
202 if (( i!=0 && i!=spl->spiro_cnt-2 ) ||
203 !SPIRO_SPL_OPEN(spl))
204 return( NULL );
205 else if ( found==NULL )
206 found = spl;
207 else
208 return( NULL );
209 }
210 }
211 } else {
212 if ( spl->first->selected ) {
213 if ( found!=NULL )
214 return( NULL ); /* At least two points */
215 if ( spl->first->prev!=NULL )
216 return( NULL ); /* Not an open splineset */
217 found = spl;
218 }
219 first = NULL;
220 for ( spline = spl->first->next; spline!=NULL && spline!=first; spline=spline->to->next ) {
221 if ( spline->to->selected ) {
222 if ( found!=NULL )
223 return( NULL );
224 if ( spline->to->next!=NULL )
225 return( NULL ); /* Selected point is not at end of a splineset */
226 found = spl;
227 }
228 if ( first==NULL ) first = spline;
229 }
230 }
231 }
232 return( found );
233 }
234
235 int CVAnySelPoint(CharView *cv,SplinePoint **sp, spiro_cp **cp) {
236 /* if there is exactly one point selected */
237 SplinePointList *spl;
238 Spline *spline, *first; SplinePoint *found = NULL;
239 spiro_cp *foundcp = NULL;
240 int i;
241
242 *sp = NULL; *cp = NULL;
243 if ( cv->b.sc->inspiro && hasspiro()) {
244 for ( spl= cv->b.layerheads[cv->b.drawmode]->splines; spl!=NULL; spl=spl->next ) {
245 for ( i=0; i<spl->spiro_cnt-1; ++i )
246 if ( SPIRO_SELECTED( &spl->spiros[i]) ) {
247 if ( foundcp )
248 return( false );
249 foundcp = &spl->spiros[i];
250 }
251 }
252 *cp = foundcp;
253 return( foundcp!=NULL );
254 } else {
255 for ( spl= cv->b.layerheads[cv->b.drawmode]->splines; spl!=NULL; spl=spl->next ) {
256 if ( spl->first->selected ) {
257 if ( found!=NULL )
258 return( false ); /* At least two points */
259 found = spl->first;
260 }
261 first = NULL;
262 for ( spline = spl->first->next; spline!=NULL && spline!=first; spline=spline->to->next ) {
263 if ( spline->to->selected ) {
264 if ( found!=NULL )
265 return( false );
266 found = spline->to;
267 }
268 if ( first==NULL ) first = spline;
269 }
270 }
271 *sp = found;
272 return( found!=NULL );
273 }
274 }
275
276 // Return type: index into spiro_cp array, or -1 if not found.
277 static int CVSpiroSelected(CharView *cv, SplineSet *ss) {
278 int i, j = 0;
279 bool found = false;
280 for (i = 0; i < ss->spiro_cnt; i++) {
281 if (SPIRO_SELECTED(&ss->spiros[i])) {
282 j = i;
283 found = true;
284 }
285 }
286 if (!found) {
287 TRACE("CVSpiroSelected: We expected to find a selected spiro, but failed to.\n");
288 return -1;
289 }
290 TRACE("CVSpiroSelected: %d\n", j);
291 return j;
292 }
293
294 // If needed, reset the selected spiro.
295 static void CVResetSelSpiro(CharView* cv, SplineSet *ss) {
296 if (CVInSpiro(cv)) {
297 int sel = CVSpiroSelected(cv, ss);
298 cv->p.spiro = cv->lastselcp = cv->active_cp = sel == -1 ? NULL : &ss->spiros[sel];
299 }
300 }
301
302 static void CVMergeSPLS(CharView *cv,SplineSet *ss, SplinePoint *base,SplinePoint *sp) {
303 int order2 = cv->b.layerheads[cv->b.drawmode]->order2;
304
305 cv->joinvalid = true;
306 cv->joinpos = *sp; cv->joinpos.selected = false;
307 if ( sp->prev!=NULL )
308 SplineSetReverse(cv->p.spl);
309 if ( sp->prev!=NULL )
310 IError("Base point not at start of splineset in CVMouseDownPoint");
311 /* remove the old spl entry from the chain */
312 if ( cv->p.spl==cv->b.layerheads[cv->b.drawmode]->splines )
313 cv->b.layerheads[cv->b.drawmode]->splines = cv->p.spl->next;
314 else { SplineSet *temp;
315 for ( temp = cv->b.layerheads[cv->b.drawmode]->splines; temp->next!=cv->p.spl; temp = temp->next );
316 temp->next = cv->p.spl->next;
317 }
318 if ( order2 && (!RealNear(base->nextcp.x,sp->prevcp.x) ||
319 !RealNear(base->nextcp.y,sp->prevcp.y)) ) {
320 base->nextcp = base->me;
321 sp->prevcp = sp->me;
322 }
323 SplineMake(base,sp,order2);
324 SplineCharDefaultNextCP(base);
325 SplineCharDefaultPrevCP(sp);
326 if ( sp->pointtype==pt_tangent ) {
327 SplineCharTangentNextCP(sp);
328 if ( sp->next ) SplineRefigure(sp->next );
329 }
330 ss->last = cv->p.spl->last;
331 if ( ss->spiros && cv->p.spl->spiros ) {
332 if ( ss->spiro_cnt+cv->p.spl->spiro_cnt > ss->spiro_max )
333 ss->spiros = realloc(ss->spiros,
334 (ss->spiro_max = ss->spiro_cnt+cv->p.spl->spiro_cnt)*sizeof(spiro_cp));
335 memcpy(ss->spiros+ss->spiro_cnt-1,
336 cv->p.spl->spiros+1, (cv->p.spl->spiro_cnt-1)*sizeof(spiro_cp));
337 ss->spiro_cnt += cv->p.spl->spiro_cnt-2;
338 } else
339 SplineSetSpirosClear(ss);
340 cv->p.spl->last = cv->p.spl->first = NULL;
341 cv->p.spl->spiros = 0;
342 SplinePointListFree(cv->p.spl);
343 cv->p.spl = NULL;
344 }
345
346 static void CVMouseDownSpiroPoint(CharView *cv, GEvent *event) {
347 SplineSet *sel, *ss;
348 SplineChar *sc = cv->b.sc;
349 spiro_cp *base, *cp;
350 int base_index, cp_index, i;
351 char ty = (cv->active_tool==cvt_curve?SPIRO_G4:
352 cv->active_tool==cvt_hvcurve?SPIRO_G2:
353 cv->active_tool==cvt_corner?SPIRO_CORNER:
354 cv->active_tool==cvt_tangent?SPIRO_LEFT:
355 /*cv->active_tool==cvt_pen?*/SPIRO_RIGHT);
356
357 cv->active_spl = NULL;
358 cv->active_sp = NULL;
359
360 sel = CVAnySelPointList(cv);
361 if ( sel!=NULL ) {
362 if ( SPIRO_SELECTED(&sel->spiros[0]) ) base_index = 0;
363 else base_index = sel->spiro_cnt-2;
364 base = &sel->spiros[base_index];
365 if ( base==cv->p.spiro )
366 return; /* We clicked on the active point, that's a no-op */
367 }
368 CVPreserveState(&cv->b);
369 CVClearSel(cv);
370 if ( sel!=NULL ) {
371 if ( (cp = cv->p.spiro)!=NULL )
372 cp_index = cp-cv->p.spl->spiros;
373 cv->lastselcp = base;
374 ss = sel;
375 if ( base_index!=sel->spiro_cnt-2 ) {
376 SplineSetReverse(sel);
377 base = &sel->spiros[sel->spiro_cnt-2];
378 if ( cv->p.spl==sel ) {
379 cp_index = sel->spiro_cnt-2-cp_index;
380 cp = &sel->spiros[cp_index];
381 }
382 }
383 if ( cp==NULL || (cp_index!=0 && cp_index!=cv->p.spl->spiro_cnt-2) ||
384 cp==base || !SPIRO_SPL_OPEN(cv->p.spl)) {
385 /* Add a new point */
386 if ( sel->spiro_cnt>=sel->spiro_max )
387 sel->spiros = realloc(sel->spiros,(sel->spiro_max += 10)*sizeof(spiro_cp));
388 cp = &sel->spiros[sel->spiro_cnt-1];
389 cp[1] = cp[0]; /* Move the final 'z' */
390 cp->x = cv->p.cx;
391 cp->y = cv->p.cy;
392 cp->ty = ty;
393 SPIRO_DESELECT(cp-1);
394 ++sel->spiro_cnt;
395 } else if ( cv->p.spl==sel ) {
396 /* Close the current spline set */
397 sel->spiros[0].ty = ty;
398 cv->joinvalid = true;
399 cv->joincp = *cp; SPIRO_DESELECT(&cv->joincp);
400 } else {
401 /* Merge two spline sets */
402 SplinePoint *sp = cp_index==0 ? cv->p.spl->first : cv->p.spl->last;
403 SplinePoint *basesp = base_index==0 ? sel->first : sel->last;
404 cv->joincp = *cp; SPIRO_DESELECT(&cv->joincp);
405 CVMergeSPLS(cv,ss,basesp,sp);
406 }
407 } else if ( cv->p.spline!=NULL ) {
408 /* Add an intermediate point on an already existing spline */
409 ss = cv->p.spl;
410 if ( ss->spiro_cnt>=ss->spiro_max )
411 ss->spiros = realloc(ss->spiros,(ss->spiro_max += 10)*sizeof(spiro_cp));
412 for ( i=ss->spiro_cnt-1; i>cv->p.spiro_index; --i )
413 ss->spiros[i+1] = ss->spiros[i];
414 ++ss->spiro_cnt;
415 cp = &ss->spiros[cv->p.spiro_index+1];
416 cp->x = cv->p.cx;
417 cp->y = cv->p.cy;
418 cp->ty = ty;
419 ss = cv->p.spl;
420 cv->joinvalid = true;
421 cv->joincp = *cp; SPIRO_DESELECT(&cv->joincp);
422 } else {
423 /* A new point on a new (open) contour */
424 ss = chunkalloc(sizeof(SplineSet));
425 ss->next = cv->b.layerheads[cv->b.drawmode]->splines;
426 cv->b.layerheads[cv->b.drawmode]->splines = ss;
427 ss->spiros = malloc((ss->spiro_max=10)*sizeof(spiro_cp));
428 cp = &ss->spiros[0];
429 cp->x = cv->p.cx;
430 cp->y = cv->p.cy;
431 cp->ty = SPIRO_OPEN_CONTOUR;
432 cp[1].x = cp[1].y = 0; cp[1].ty = 'z';
433 ss->spiro_cnt = 2;
434 }
435 SPIRO_SELECT(cp);
436
437 SSRegenerateFromSpiros(ss);
438
439 cv->active_spl = ss;
440 cv->active_cp = cp;
441 // We may have reallocated the spiros (above), so we need to reset the selected spiro.
442 CVResetSelSpiro(cv, ss);
443 CVSetCharChanged(cv,true);
444 CVInfoDraw(cv,cv->gw);
445 SCUpdateAll(sc);
446 }
447
448 /* When the user tries to add a point (by doing a mouse down with a point tool
449 selected) there are several cases to be looked at:
450 If there is a single point selected and it is at the beginning/end of an open spline set
451 if we clicked on another point which is the beginning/end of an open splineset
452 draw a spline connecting the two spline sets and merge them
453 (or if it's the same spline set, then close it)
454 else
455 create a new point where we clicked
456 draw a spline between the selected point and the new one
457 deselect the old point
458 select the new one
459 endif
460 else if they clicked on a spline
461 split the spline into two bits at the point where they clicked
462 else
463 create a new point where they clicked
464 put it on a new splineset
465 select it
466 endif
467
468 and, if the old point is a tangent, we may need to adjust its control pt
469
470 With the introduction of spiro mode (Raph Levien's clothoid splines)
471 we've got to worry about all the above cases for spiro points too.
472 */
473 void CVMouseDownPoint(CharView *cv, GEvent *event) {
474 SplineSet *sel, *ss;
475 SplinePoint *sp, *base = NULL;
476 SplineChar *sc = cv->b.sc;
477 enum pointtype ptype = (cv->active_tool==cvt_curve?pt_curve:
478 cv->active_tool==cvt_hvcurve?pt_hvcurve:
479 cv->active_tool==cvt_corner?pt_corner:
480 cv->active_tool==cvt_tangent?pt_tangent:
481 /*cv->active_tool==cvt_pen?*/pt_corner);
482 int order2 = cv->b.layerheads[cv->b.drawmode]->order2;
483 int order2_style = (order2 && !(event->u.mouse.state&ksm_meta)) ||
484 (!order2 && (event->u.mouse.state&ksm_meta));
485
486 cv->active_spl = NULL;
487 cv->active_sp = NULL;
488
489 if ( cv->b.sc->inspiro && hasspiro()) {
490 CVMouseDownSpiroPoint(cv, event);
491 return;
492 }
493
494 sel = CVAnySelPointList(cv);
495 if ( sel!=NULL ) {
496 if ( sel->first->selected ) base = sel->first;
497 else base = sel->last;
498 if ( base==cv->p.sp )
499 return; /* We clicked on the active point, that's a no-op */
500 }
501 CVPreserveState(&cv->b);
502 CVClearSel(cv);
503 if ( sel!=NULL ) {
504 sp = cv->p.sp;
505 cv->lastselpt = base;
506 ss = sel;
507 if ( base->next!=NULL )
508 SplineSetReverse(sel);
509 if ( base->next!=NULL )
510 IError("Base point not at end of splineset in CVMouseDownPoint");
511 if ( sp==NULL || (sp->next!=NULL && sp->prev!=NULL) || sp==base ) {
512 /* Add a new point */
513 SplineSetSpirosClear(sel);
514 sp = SplinePointCreate( cv->p.cx, cv->p.cy );
515 sp->prevcp = sp->nextcp = sp->me;
516 sp->nextcpdef = sp->prevcpdef = 1;
517 sp->pointtype = ptype;
518 sp->selected = true;
519 if ( !base->nonextcp && order2_style &&
520 cv->active_tool==cvt_pen ) {
521 sp->prevcp = base->nextcp;
522 sp->me.x = ( sp->prevcp.x + sp->nextcp.x )/2;
523 sp->me.y = ( sp->prevcp.y + sp->nextcp.y )/2;
524 sp->pointtype = pt_curve;
525 } else if ( order2 && !base->nonextcp ) {
526 sp->prevcp = base->nextcp;
527 if ( cv->active_tool==cvt_pen ) {
528 sp->nextcp.x = sp->me.x - (sp->prevcp.x-sp->me.x);
529 sp->nextcp.y = sp->me.y - (sp->prevcp.y-sp->me.y);
530 sp->pointtype = pt_curve;
531 }
532 }
533 if ( base->nonextcp )
534 base->nextcpdef = true;
535 SplineMake(base,sp,order2);
536 if ( cv->active_tool!=cvt_pen ) {
537 SplineCharDefaultNextCP(base);
538 SplineCharDefaultPrevCP(sp);
539 }
540 ss->last = sp;
541 } else if ( cv->p.spl==sel ) {
542 /* Close the current spline set */
543 SplineSetSpirosClear(sel);
544 cv->joinvalid = true;
545 cv->joinpos = *sp; cv->joinpos.selected = false;
546 if ( order2 ) {
547 if ( base->nonextcp || sp->noprevcp ) {
548 base->nextcp = base->me;
549 sp->prevcp = sp->me;
550 } else {
551 base->nextcp.x = sp->prevcp.x = (base->nextcp.x+sp->prevcp.x)/2;
552 base->nextcp.y = sp->prevcp.y = (base->nextcp.y+sp->prevcp.y)/2;
553 }
554 base->nextcpdef = sp->prevcpdef = true;
555 }
556 SplineMake(base,sp,order2);
557 if ( cv->active_tool!=cvt_pen )
558 SplineCharDefaultNextCP(base);
559 SplineCharDefaultPrevCP(sp);
560 ss->last = sp;
561 if ( sp->pointtype==pt_tangent ) {
562 SplineCharTangentNextCP(sp);
563 if ( sp->next ) SplineRefigure(sp->next );
564 }
565 } else {
566 /* Merge two spline sets */
567 SplineSetSpirosClear(sel);
568 CVMergeSPLS(cv,sel, base,sp);
569 }
570 sp->selected = true;
571 if ( base->pointtype==pt_tangent ) {
572 SplineCharTangentPrevCP(base);
573 if ( base->prev!=NULL )
574 SplineRefigure(base->prev);
575 }
576 } else if ( cv->p.spline!=NULL ) {
577 sp = SplineBisect(cv->p.spline,cv->p.t);
578 cv->joinvalid = true;
579 cv->joinpos = *sp; cv->joinpos.selected = false;
580 if ( cv->active_tool==cvt_pen )
581 ptype = pt_curve;
582 sp->pointtype = ptype;
583 if ( ptype==pt_hvcurve ) {
584 SPHVCurveForce(sp);
585 }
586 sp->selected = true;
587 ss = cv->p.spl;
588 } else {
589 ss = chunkalloc(sizeof(SplineSet));
590 sp = SplinePointCreate( cv->p.cx, cv->p.cy );
591
592 ss->first = ss->last = sp;
593 ss->next = cv->b.layerheads[cv->b.drawmode]->splines;
594 cv->b.layerheads[cv->b.drawmode]->splines = ss;
595 sp->nextcp = sp->prevcp = sp->me;
596 sp->nextcpdef = sp->prevcpdef = 1;
597 sp->pointtype = ptype;
598 sp->selected = true;
599 }
600
601 cv->active_spl = ss;
602 cv->active_sp = sp;
603 CVSetCharChanged(cv,true);
604 CVInfoDraw(cv,cv->gw);
605 SCUpdateAll(sc);
606
607 if ( cv->active_tool == cvt_pen )
608 cv->p.constrain = sp->me;
609 }
610
611 void AdjustControls(SplinePoint *sp) {
612 if ( sp->next!=NULL ) {
613 SplineCharDefaultNextCP(sp); /* also fixes up tangents */
614 SplineCharDefaultPrevCP(sp->next->to);
615 SplineRefigure(sp->next);
616 if ( sp->next->to->pointtype==pt_tangent && sp->next->to->next!=NULL ) {
617 SplineCharTangentNextCP(sp->next->to);
618 SplineRefigure(sp->next->to->next);
619 }
620 }
621 if ( sp->prev!=NULL ) {
622 SplineCharDefaultPrevCP(sp);
623 SplineCharDefaultNextCP(sp->prev->from);
624 SplineRefigure(sp->prev);
625 if ( sp->prev->from->pointtype==pt_tangent && sp->prev->from->prev!=NULL ) {
626 SplineCharTangentPrevCP(sp->prev->from);
627 SplineRefigure(sp->prev->from->prev);
628 }
629 }
630 }
631
632 void CVAdjustPoint(CharView *cv, SplinePoint *sp) {
633
634 if ( cv->info.x==sp->me.x && cv->info.y==sp->me.y )
635 return;
636 sp->nextcp.x += (cv->info.x-sp->me.x);
637 sp->nextcp.y += (cv->info.y-sp->me.y);
638 sp->prevcp.x += (cv->info.x-sp->me.x);
639 sp->prevcp.y += (cv->info.y-sp->me.y);
640 sp->me.x = cv->info.x;
641 sp->me.y = cv->info.y;
642 AdjustControls(sp);
643 CVSetCharChanged(cv,true);
644 }
645
646 void CVMergeSplineSets(CharView *cv, SplinePoint *active, SplineSet *activess,
647 SplinePoint *merge, SplineSet *mergess) {
648 SplinePointList *spl;
649
650 cv->joinvalid = true;
651 cv->joinpos = *merge; cv->joinpos.selected = false;
652
653 if ( active->prev==NULL )
654 SplineSetReverse(activess);
655 if ( merge->next==NULL )
656 SplineSetReverse(mergess);
657 active->nextcp = merge->nextcp;
658 active->nextcpdef = merge->nextcpdef;
659 active->next = merge->next;
660 if ( merge->next!= NULL ) {
661 active->next->from = active;
662 activess->last = mergess->last;
663 }
664 merge->next = NULL;
665 if ( mergess==activess ) {
666 activess->first = activess->last = active;
667 SplinePointMDFree(cv->b.sc,merge);
668 if ( activess->spiro_cnt!=0 ) {
669 activess->spiros[0].ty = activess->spiros[activess->spiro_cnt-2].ty;
670 activess->spiros[activess->spiro_cnt-2] = activess->spiros[activess->spiro_cnt-1];
671 --activess->spiro_cnt;
672 }
673 } else {
674 mergess->last = merge;
675 if ( mergess==cv->b.layerheads[cv->b.drawmode]->splines )
676 cv->b.layerheads[cv->b.drawmode]->splines = mergess->next;
677 else {
678 for ( spl = cv->b.layerheads[cv->b.drawmode]->splines; spl->next!=mergess; spl=spl->next );
679 spl->next = mergess->next;
680 }
681 if ( activess->spiros && mergess->spiros ) {
682 if ( activess->spiro_cnt+mergess->spiro_cnt > activess->spiro_max ) {
683 activess->spiros = realloc(activess->spiros, (activess->spiro_max = activess->spiro_cnt+mergess->spiro_cnt)*sizeof(spiro_cp));
684 }
685 memcpy(activess->spiros+activess->spiro_cnt-1,
686 mergess->spiros+1, (mergess->spiro_cnt-1)*sizeof(spiro_cp));
687 activess->spiro_cnt += mergess->spiro_cnt-2;
688 } else {
689 SplineSetSpirosClear(activess);
690 }
691 SplinePointListMDFree(cv->b.sc,mergess); // frees mergess
692 }
693 if (( active->pointtype==pt_curve || active->pointtype==pt_hvcurve ) &&
694 !active->nonextcp && !active->noprevcp &&
695 !active->nextcpdef && !active->prevcpdef &&
696 !BpColinear(&active->prevcp,&active->me,&active->nextcp))
697 active->nextcpdef = active->prevcpdef = true;
698 SplineSetJoinCpFixup(active);
699 // We may have reallocated the spiros, so we need to reset the selected spiro.
700 CVResetSelSpiro(cv, activess);
701 }
702
703 static void CVMouseMoveSpiroPoint(CharView *cv, PressedOn *p) {
704 spiro_cp *active = cv->active_cp, *merge = p->spiro;
705 SplineSet *activess = cv->active_spl;
706 int active_index;
707
708 if ( active==NULL )
709 return;
710 if ( cv->info.x==active->x && cv->info.y==active->y )
711 return;
712
713 if ( !cv->recentchange ) CVPreserveState(&cv->b);
714
715 active->x = cv->info.x;
716 active->y = cv->info.y;
717 CVSetCharChanged(cv,true);
718
719 active_index = active-activess->spiros;
720
721 if ( active!=merge && merge!=NULL && p->spl!=NULL &&
722 SPIRO_SPL_OPEN(activess) &&
723 SPIRO_SPL_OPEN(p->spl) &&
724 (active_index==0 || active_index==activess->spiro_cnt-2) &&
725 ((merge-p->spl->spiros)==0 || (merge-p->spl->spiros)==p->spl->spiro_cnt-2) ) {
726 SplinePoint *activesp = active_index==0 ? activess->first : activess->last;
727 SplinePoint *mergesp = (merge-p->spl->spiros)==0 ? p->spl->first : p->spl->last;
728 CVMergeSplineSets(cv,activesp,activess,mergesp,p->spl);
729 }
730 SSRegenerateFromSpiros(activess);
731 SCUpdateAll(cv->b.sc);
732 }
733
734 /* We move the active point around following the mouse. */
735 /* There's one special case. If the active point is an end point on its splineset */
736 /* and we've just moved on top of another splineset end-point, then join the */
737 /* two splinesets at the active point. Of course we might close up our own */
738 /* spline set... */
739 void CVMouseMovePoint(CharView *cv, PressedOn *p) {
740 SplinePoint *active = cv->active_sp, *merge = p->sp;
741 SplineSet *activess = cv->active_spl;
742
743 if ( cv->b.sc->inspiro && hasspiro()) {
744 CVMouseMoveSpiroPoint(cv,p);
745 return;
746 }
747
748 if ( active==NULL )
749 return;
750 if ( cv->info.x==active->me.x && cv->info.y==active->me.y )
751 return;
752
753 if ( !cv->recentchange ) CVPreserveState(&cv->b);
754
755 CVAdjustPoint(cv,active);
756 SplineSetSpirosClear(activess);
757 if (( active->next==NULL || active->prev==NULL ) && merge!=NULL && p->spl!=NULL &&
758 merge!=active &&
759 (merge->next==NULL || merge->prev==NULL )) {
760 CVMergeSplineSets(cv,active,activess,merge,p->spl);
761 }
762 SCUpdateAll(cv->b.sc);
763 }
764
765 void CVMouseMovePen(CharView *cv, PressedOn *p, GEvent *event) {
766 SplinePoint *active = cv->active_sp;
767 int order2 = cv->b.layerheads[cv->b.drawmode]->order2;
768 int order2_style = (order2 && !(event->u.mouse.state&ksm_meta)) ||
769 (!order2 && (event->u.mouse.state&ksm_meta));
770
771 if ( cv->b.sc->inspiro && hasspiro()) {
772 CVMouseMoveSpiroPoint(cv,p);
773 return;
774 }
775
776 if ( active==NULL )
777 return;
778 if ( cv->info.x==active->nextcp.x && cv->info.y==active->nextcp.y )
779 return;
780 /* In order2 fonts when the user clicks with the pen tool we'd like to */
781 /* leave it with the default cp (ie. the cp which makes the current point*/
782 /* implicit) rather than moving the cp to the base point and losing the */
783 /* curve */
784 if ( cv->info.x==active->me.x && cv->info.y==active->me.y &&
785 event->type==et_mouseup && cv->b.layerheads[cv->b.drawmode]->order2 )
786 return;
787 SplineSetSpirosClear(cv->active_spl);
788 cv->lastselpt = cv->active_sp;
789
790 active->nextcp.x = cv->info.x;
791 active->nextcp.y = cv->info.y;
792 if ( order2_style && active->next==NULL ) {
793 active->me.x = (active->nextcp.x + active->prevcp.x)/2;
794 active->me.y = (active->nextcp.y + active->prevcp.y)/2;
795 if ( active->me.x != active->nextcp.x || active->me.y != active->nextcp.y ) {
796 active->pointtype = pt_curve;
797 }
798 if ( active->prev!=NULL )
799 SplineRefigure(active->prev);
800 SCUpdateAll(cv->b.sc);
801 return;
802 } else if ( active->nextcp.x==active->me.x && active->nextcp.y==active->me.y ) {
803 active->prevcp = active->nextcp = active->me;
804 active->pointtype = pt_corner;
805 } else {
806 active->prevcp.x = active->me.x - (active->nextcp.x-active->me.x);
807 active->prevcp.y = active->me.y - (active->nextcp.y-active->me.y);
808 active->nextcpdef = active->prevcpdef = false;
809 active->pointtype = pt_curve;
810 }
811 if ( cv->b.layerheads[cv->b.drawmode]->order2 ) {
812 if ( active->prev!=NULL ) {
813 if ( active->prevcp.x==active->me.x && active->prevcp.y==active->me.y )
814 active->prev->from->nextcp = active->prev->from->me;
815 else {
816 active->prev->from->nextcp = active->prevcp;
817 }
818 SplinePointNextCPChanged2(active->prev->from);
819 SplineRefigureFixup(active->prev);
820 }
821 if ( active->next!=NULL ) {
822 if ( active->nextcp.x==active->me.x && active->nextcp.y==active->me.y )
823 active->next->to->prevcp = active->next->to->me;
824 else {
825 active->next->to->prevcp = active->nextcp;
826 }
827 SplineRefigureFixup(active->next);
828 }
829 } else {
830 if ( active->prev!=NULL )
831 SplineRefigure(active->prev);
832 if ( active->next!=NULL )
833 SplineRefigure(active->next);
834 }
835 CPUpdateInfo(cv,event);
836 SCUpdateAll(cv->b.sc);
837 }
838
839 void CVMouseUpPoint(CharView *cv,GEvent *event) {
840 SplinePoint *active = cv->active_sp;
841 cv->lastselpt = active;
842 cv->active_spl = NULL;
843 cv->active_sp = NULL;
844 cv->active_cp = NULL;
845 cv->joinvalid = false;
846 CVInfoDraw(cv,cv->gw);
847 CPEndInfo(cv);
848 if ( event->u.mouse.clicks>1 )
849 CVGetInfo(cv);
850 }