Debian Codegraph @

fontforgeexe/cvfreehand.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 "splinefit.h"
33 #include "splineorder2.h"
34 #include "splinestroke.h"
35 #include "splineutil.h"
36 #include "splineutil2.h"
37
38 #include <math.h>
39
40 #undef DEBUG_FREEHAND
41
42 const int min_line_cnt = 10; /* line segments must be at least this many datapoints to be distinguished */
43 const int min_line_len = 20; /* line segments must be at least this many pixels to be distinguished */
44 const double line_wobble = 1.0; /* data must not stray more than this many pixels from the true line */
45 const int extremum_locale = 7; /* for something to be an extremum in x it must be an extremum for at least this many y pixels. Or vice versa */
46 const int extremum_cnt = 4; /* for something to be an extremum in x it must be an extremum for at least this data samples. Or for y */
47
48 enum extreme { e_none=0, e_xmin, e_xmax, e_xflat, e_ymin, e_ymax, e_yflat };
49
50 typedef struct tracedata {
51 /* First the data */
52 int x,y;
53 BasePoint here;
54 uint32_t time;
55 int32_t pressure, xtilt, ytilt, separation;
56
57 /* Then overhead */
58 struct tracedata *next, *prev;
59
60 unsigned int extremum: 3;
61 unsigned int use_as_pt: 1;
62 unsigned int online: 1;
63 unsigned int wasconstrained: 1;
64 unsigned int constrained_corner: 1;
65 uint16_t num;
66 } TraceData;
67
68 static void TraceDataFree(TraceData *td) {
69 TraceData *next, *first=td;
70
71 while ( td!=NULL ) {
72 next = td->next;
73 chunkfree(td,sizeof(TraceData));
74 td = next;
75 if ( td==first )
76 break;
77 }
78 }
79
80 static void TraceDataFromEvent(CharView *cv, GEvent *event) {
81 CharViewTab* tab = CVGetActiveTab(cv);
82 TraceData *new;
83 int skiplast;
84
85 if ( cv->freehand.head!=NULL &&
86 cv->freehand.last->here.x==(event->u.mouse.x-tab->xoff)/tab->scale &&
87 cv->freehand.last->here.y==(cv->height-event->u.mouse.y-tab->yoff)/tab->scale ) {
88 /* Has not moved */
89 int constrained = (event->u.mouse.state&ksm_shift)?1:0;
90 if ( constrained != cv->freehand.last->wasconstrained )
91 cv->freehand.last->constrained_corner = true;
92 return;
93 }
94
95 /* I sometimes seem to get events out of order on the wacom */
96 skiplast = false;
97 if ( cv->freehand.last!=NULL && cv->freehand.last->prev!=NULL ) {
98 int x = (event->u.mouse.x-tab->xoff)/tab->scale;
99 int y = (cv->height-event->u.mouse.y-tab->yoff)/tab->scale;
100 TraceData *bad = cv->freehand.last, *base = bad->prev;
101
102 if ( ((bad->here.x < x-15 && bad->here.x < base->here.x-15) ||
103 (bad->here.x > x+15 && bad->here.x > base->here.x+15)) &&
104 ((bad->here.y < y-15 && bad->here.y < base->here.y-15) ||
105 (bad->here.y > y+15 && bad->here.y > base->here.y+15)) )
106 skiplast = true;
107 }
108
109 if ( skiplast )
110 new = cv->freehand.last;
111 else {
112 new = chunkalloc(sizeof(TraceData));
113
114 if ( cv->freehand.head==NULL )
115 cv->freehand.head = cv->freehand.last = new;
116 else {
117 cv->freehand.last->next = new;
118 new->prev = cv->freehand.last;
119 cv->freehand.last = new;
120 }
121 }
122
123 new->here.x = (event->u.mouse.x-tab->xoff)/tab->scale;
124 new->here.y = (cv->height-event->u.mouse.y-tab->yoff)/tab->scale;
125 new->time = event->u.mouse.time;
126 new->pressure = event->u.mouse.pressure;
127 new->xtilt = event->u.mouse.xtilt;
128 new->ytilt = event->u.mouse.ytilt;
129 new->wasconstrained = (event->u.mouse.state&ksm_shift)?1:0;
130 #ifdef DEBUG_FREEHAND
131 printf( "(%d,%d) (%g,%g) %d %d\n", event->u.mouse.x, event->u.mouse.y,
132 new->here.x, new->here.y, new->pressure, new->time );
133 if ( new->prev!=NULL && new->time<new->prev->time )
134 printf( "\tAh ha!\n" );
135 #endif
136 if ( new->wasconstrained &&
137 ( new->prev==NULL || !new->prev->wasconstrained))
138 new->constrained_corner = true;
139 else if ( new->prev!=NULL && !new->wasconstrained && new->prev->wasconstrained )
140 new->prev->constrained_corner = true;
141 }
142
143 static int TraceGoodLine(TraceData *base,TraceData *end) {
144 /* Make sure that we don't stray more than line_wobble pixels */
145 int dx, dy;
146 double diff;
147 TraceData *pt;
148
149 if (( dx = end->x-base->x )<0 ) dx = -dx;
150 if (( dy = end->y-base->y )<0 ) dy = -dy;
151
152 if ( dy>dx ) {
153 dx = end->x-base->x; dy = end->y-base->y;
154 for ( pt=base->next; pt!=end; pt=pt->next ) {
155 diff = (pt->y-base->y)*dx/(double) dy + base->x - pt->x;
156 if ( diff>line_wobble || diff<-line_wobble )
157 return( false );
158 }
159 } else {
160 dx = end->x-base->x; dy = end->y-base->y;
161 for ( pt=base->next; pt!=end; pt=pt->next ) {
162 diff = (pt->x-base->x)*dy/(double) dx + base->y - pt->y;
163 if ( diff>line_wobble || diff<-line_wobble )
164 return( false );
165 }
166 }
167 return( true );
168 }
169
170 static TraceData *TraceLineCheck(TraceData *base) {
171 /* Look for a line. To be a line it must be at least min_line_len pixels */
172 /* long (or reach the end of data with no real bends) */
173 /* and we can't have strayed more than line_wobble pixels from the center */
174 /* of the line */
175 TraceData *pt, *end, *last_good;
176 int cnt;
177
178 if ( base->next==NULL || base->next->next==NULL || base->next->next->next==NULL ||
179 base->wasconstrained )
180 return( base );
181
182 for ( end=base->next, cnt=0; end->next!=NULL; end=end->next, ++cnt ) {
183 if ( (end->x-base->x)*(end->x-base->x) + (end->y-base->y)*(end->y-base->y)>=
184 min_line_len*min_line_len &&
185 cnt>=min_line_cnt )
186 break;
187 if ( end->wasconstrained )
188 return( base );
189 }
190
191 last_good = NULL;
192 while ( TraceGoodLine(base,end) ) {
193 last_good = end;
194 if ( end->wasconstrained )
195 break;
196 end = end->next;
197 if ( end==NULL )
198 break;
199 }
200 if ( last_good==NULL )
201 return( base ); /* No good line */
202 base->use_as_pt = last_good->use_as_pt = true;
203 for ( pt = base; pt!=last_good; pt=pt->next )
204 pt->online = true;
205 last_good->online = true;
206 return( last_good );
207 }
208
209 static enum extreme TraceIsExtremum(TraceData *base) {
210 TraceData *pt;
211 enum extreme type;
212 int i;
213
214 if ( base->online || base->use_as_pt )
215 return( e_none );
216
217 type = e_xflat;
218 for ( pt = base->next, i=0; pt!=NULL && type!=e_none; pt=pt->next, ++i ) {
219 if ( i>=extremum_cnt && ( pt->y-base->y > extremum_locale || pt->y-base->y < -extremum_locale ))
220 break;
221 if ( pt->x>base->x ) {
222 if ( type==e_xmax )
223 type = e_none;
224 else
225 type = e_xmin;
226 } else if ( pt->x<base->x ) {
227 if ( type==e_xmin )
228 type = e_none;
229 else
230 type = e_xmax;
231 }
232 }
233 for ( pt = base->prev, i=0; pt!=NULL && type!=e_none; pt=pt->prev, ++i ) {
234 if ( i>=extremum_cnt && ( pt->y-base->y > extremum_locale || pt->y-base->y < -extremum_locale ))
235 break;
236 if ( pt->x>base->x ) {
237 if ( type==e_xmax )
238 type = e_none;
239 else
240 type = e_xmin;
241 } else if ( pt->x<base->x ) {
242 if ( type==e_xmin )
243 type = e_none;
244 else
245 type = e_xmax;
246 }
247 }
248 if ( type!=e_none )
249 return( type );
250
251 type = e_yflat;
252 for ( pt = base->next, i=0; pt!=NULL && type!=e_none; pt=pt->next, ++i ) {
253 if ( i>=extremum_cnt && ( pt->x-base->x > extremum_locale || pt->x-base->x < -extremum_locale ))
254 break;
255 if ( pt->y>base->y ) {
256 if ( type==e_ymax )
257 type = e_none;
258 else
259 type = e_ymin;
260 } else if ( pt->y<base->y ) {
261 if ( type==e_ymin )
262 type = e_none;
263 else
264 type = e_ymax;
265 }
266 }
267 for ( pt = base->prev, i=0; pt!=NULL && type!=e_none; pt=pt->prev, ++i ) {
268 if ( i>=extremum_cnt && ( pt->x-base->x > extremum_locale || pt->x-base->x < -extremum_locale ))
269 break;
270 if ( pt->y>base->y ) {
271 if ( type==e_ymax )
272 type = e_none;
273 else
274 type = e_ymin;
275 } else if ( pt->y<base->y ) {
276 if ( type==e_ymin )
277 type = e_none;
278 else
279 type = e_ymax;
280 }
281 }
282
283 return( type );
284 }
285
286 static TraceData *TraceNextPoint(TraceData *pt) {
287 if ( pt==NULL )
288 return( NULL );
289 for ( pt=pt->next; pt!=NULL && !pt->use_as_pt ; pt=pt->next );
290 return( pt );
291 }
292
293 static void TraceMassage(TraceData *head, TraceData *end) {
294 TraceData *pt, *npt, *last, *nnpt;
295 double nangle, pangle;
296 /* Look for corners that are too close together */
297 /* Paired tangents that might better be a single curve */
298 /* tangent corner tangent that might better be a single curve */
299
300 for ( pt=head ; pt!=NULL ; pt=npt ) {
301 npt = TraceNextPoint(pt);
302 for (;;) {
303 nnpt = TraceNextPoint(npt);
304 if ( nnpt!=NULL &&
305 ((pt->x==npt->x && npt->x==nnpt->x) ||
306 (pt->y==npt->y && npt->y==nnpt->y)) &&
307 (pt->next == npt || pt->next->online) ) {
308 /*npt->online = true;*/
309 npt->use_as_pt = false;
310 } else if ( nnpt==NULL && npt!=NULL && (pt->x==npt->x || pt->y==npt->y) &&
311 (pt->next == npt || pt->next->online) ) {
312 pt->use_as_pt = false;
313 } else
314 break;
315 npt = nnpt;
316 }
317 }
318
319 /* Remove very short segments */
320 if ( head && head->next ) {
321 for ( pt=head->next; pt!=NULL && pt->use_as_pt && !pt->constrained_corner; pt = pt->next )
322 pt->use_as_pt = false;
323 for ( pt=head->next ; pt->next!=NULL ; pt=pt->next ) {
324 if ( pt->use_as_pt && pt->next->use_as_pt && !pt->constrained_corner )
325 pt->use_as_pt = false;
326 }
327 }
328 for ( pt=head ; pt!=NULL ; pt=npt ) {
329 npt = pt;
330 for (;;) {
331 npt = TraceNextPoint(pt);
332 if ( npt == NULL || npt->constrained_corner )
333 break;
334 if ( (npt->x-pt->x)*(npt->x-pt->x) + (npt->y-pt->y)*(npt->y-pt->y)<4 )
335 npt->use_as_pt = false;
336 else
337 break;
338 }
339 }
340
341 last = NULL;
342 for ( pt=head ; pt!=NULL ; pt=pt->next ) {
343 if ( pt->use_as_pt && pt->next!=NULL && pt->next->online && !pt->next->wasconstrained &&
344 pt->prev!=NULL && !pt->prev->online ) {
345 npt = TraceNextPoint(pt);
346 if ( npt==NULL )
347 break;
348 if ( npt->wasconstrained )
349 /* Leave it */;
350 else if ( npt->next!=NULL && !npt->next->online ) {
351 /* We've got two adjacent tangents */
352 nnpt = TraceNextPoint(npt);
353 if ( last!=NULL && nnpt!=NULL ) {
354 int lencur = (npt->x-pt->x)*(npt->x-pt->x) + (npt->y-pt->y)*(npt->y-pt->y);
355 int lennext = (nnpt->x-npt->x)*(nnpt->x-npt->x) + (nnpt->y-npt->y)*(nnpt->y-npt->y);
356 int lenprev = (npt->x-pt->x)*(npt->x-pt->x) + (npt->y-pt->y)*(npt->y-pt->y);
357 if ( lencur<16*lennext || lencur<16*lenprev ) {
358 /* Probably better done as one point */
359 int which = (pt->num+npt->num)/2;
360 for (;;) {
361 pt->online = pt->use_as_pt = false;
362 if ( pt->num==which )
363 pt->use_as_pt = true;
364 if ( pt==npt )
365 break;
366 pt = pt->next;
367 }
368 }
369 }
370 } else if ( (nnpt = TraceNextPoint(npt))!=NULL &&
371 nnpt->next && !nnpt->next->online ) {
372 /* We've got tangent corner tangent */
373 pangle = atan2(npt->y-pt->y,npt->x-pt->x);
374 nangle = atan2(nnpt->y-npt->y,nnpt->x-npt->x);
375 if ( pangle<0 && nangle>0 && nangle-pangle>=FF_PI )
376 pangle += 2*FF_PI;
377 else if ( pangle>0 && nangle<0 && pangle-nangle>=FF_PI )
378 nangle += 2*FF_PI;
379 if ( nangle-pangle<1 && nangle-pangle>-1 ) {
380 for (;;) {
381 pt->online = pt->use_as_pt = false;
382 if ( pt==nnpt )
383 break;
384 pt = pt->next;
385 }
386 pt = npt;
387 pt->use_as_pt = true;
388 }
389 }
390 }
391 if ( pt->use_as_pt )
392 last = pt;
393 }
394 head->use_as_pt = end->use_as_pt = true;
395 }
396
397 static bigreal Trace_Factor(void *_cv,Spline *spline, real t) {
398 CharView *cv = (CharView *) _cv;
399 TraceData *head = cv->freehand.head, *pt, *from=NULL, *to=NULL;
400 int fromnum = spline->from->ptindex, tonum = spline->to->ptindex;
401 StrokeInfo *si = CVFreeHandInfo();
402 int p;
403
404 if ( si->width<=0 || si->pressure1==si->pressure2 )
405 return( 1.0 );
406
407 for ( pt = head; pt!=NULL; pt=pt->next ) {
408 if ( fromnum == pt->num ) {
409 from = pt;
410 if ( to!=NULL )
411 break;
412 }
413 if ( tonum == pt->num ) {
414 to = pt;
415 if ( from!=NULL )
416 break;
417 }
418 }
419 if ( from==NULL || to==NULL ) {
420 fprintf( stderr, "Not found\n" );
421 return( 1.0 );
422 }
423 p = from->pressure*t + to->pressure*(1-t);
424
425 if ( p<=si->pressure1 && p<=si->pressure2 ) {
426 if ( si->pressure1<si->pressure2 )
427 return( 1.0 );
428 else
429 return( si->radius2/(si->width/2) );
430 } else if ( p>=si->pressure1 && p>=si->pressure2 ) {
431 if ( si->pressure1<si->pressure2 )
432 return( si->radius2/(si->width/2) );
433 else
434 return( 1.0 );
435 } else
436 return( ((p-si->pressure1)*si->radius2 + (si->pressure2-p)*(si->width/2))/
437 ((si->width/2)*(si->pressure2-si->pressure1)) );
438 }
439
440 static void TraceFigureCPs(SplinePoint *last,SplinePoint *cur,TraceData *tlast,TraceData *tcur) {
441 TraceData *pt;
442
443 for ( pt=tlast; !pt->use_as_pt ; pt=pt->prev );
444 if ( pt->online ) {
445 last->nextcp.x = last->me.x + ( tlast->x-pt->x );
446 last->nextcp.y = last->me.y + ( tlast->y-pt->y );
447 } else if ( tlast->extremum==e_xmin || tlast->extremum==e_xmax || tlast->extremum==e_xflat ) {
448 last->nextcp.x = last->me.x;
449 last->nextcp.y = cur->me.y>last->me.y ? last->me.y+1 : last->me.y-1;
450 } else if ( tlast->extremum==e_ymin || tlast->extremum==e_ymax || tlast->extremum==e_yflat ) {
451 last->nextcp.y = last->me.y;
452 last->nextcp.x = cur->me.x>last->me.x ? last->me.x+1 : last->me.x-1;
453 } else if ( !last->noprevcp && !tlast->constrained_corner ) {
454 last->nextcp.x = last->me.x + (last->me.x - last->prevcp.x);
455 last->nextcp.y = last->me.y + (last->me.y - last->prevcp.y);
456 } else
457 last->nextcp = last->me;
458
459 if ( tcur->online ) {
460 for ( pt=tcur; !pt->use_as_pt ; pt=pt->next );
461 cur->prevcp.x = cur->me.x + ( tcur->x-pt->x );
462 cur->prevcp.y = cur->me.y + ( tcur->y-pt->y );
463 } else if ( tcur->extremum==e_xmin || tcur->extremum==e_xmax || tcur->extremum==e_xflat ) {
464 cur->prevcp.x = cur->me.x;
465 cur->prevcp.y = last->me.y>cur->me.y ? cur->me.y+1 : cur->me.y-1;
466 } else if ( tcur->extremum==e_ymin || tcur->extremum==e_ymax || tcur->extremum==e_yflat ) {
467 cur->prevcp.y = cur->me.y;
468 cur->prevcp.x = last->me.x>cur->me.x ? cur->me.x+1 : cur->me.x-1;
469 } else if ( !cur->nonextcp && !tcur->constrained_corner ) { /* Only happens at end of a closed contour */
470 cur->prevcp.x = cur->me.x + (cur->me.x - cur->nextcp.x);
471 cur->prevcp.y = cur->me.y + (cur->me.y - cur->nextcp.y);
472 } else
473 cur->prevcp = cur->me;
474 }
475
476 static SplineSet *TraceCurve(CharView *cv) {
477 CharViewTab* tab = CVGetActiveTab(cv);
478 TraceData *head = cv->freehand.head, *pt, *base, *e;
479 SplineSet *spl;
480 SplinePoint *last, *cur;
481 int cnt, i, tot;
482 FitPoint *mids;
483 double len,sofar;
484 StrokeInfo *si;
485
486 /* First we look for straight lines in the data. We will put SplinePoints */
487 /* at their endpoints */
488 /* Then we find places that are local extrema, or just flat in x,y */
489 /* SplinePoints here too. */
490 /* Then approximate splines between */
491
492 cnt = 0;
493 for ( pt=head; pt!=NULL; pt = pt->next ) {
494 pt->extremum = e_none;
495 pt->online = pt->wasconstrained;
496 pt->use_as_pt = pt->constrained_corner;
497 /* We recalculate x,y because we might have autoscrolled the window */
498 pt->x = tab->xoff + rint(pt->here.x*tab->scale);
499 pt->y = -tab->yoff + cv->height - rint(pt->here.y*tab->scale);
500 pt->num = cnt++;
501 }
502 head->use_as_pt = cv->freehand.last->use_as_pt = true;
503
504 /* Look for lines */
505 for ( pt=head->next ; pt!=NULL; pt=pt->next )
506 pt = TraceLineCheck(pt);
507
508 /* Look for extremum */
509 for ( pt=head->next ; pt!=NULL; pt=pt->next )
510 pt->extremum = TraceIsExtremum(pt);
511 /* Find the middle of a range of extremum points, that'll be the one we want */
512 for ( base=head->next; base!=NULL; base = base->next ) {
513 if ( base->extremum>=e_xmin ) {
514 tot = 1;
515 if ( base->extremum>=e_ymin ) {
516 for ( pt=base->next ; pt->extremum>=e_ymin ; pt = pt->next ) ++tot;
517 } else {
518 for ( pt=base->next ; pt->extremum>=e_xmin && pt->extremum<e_ymin ; pt = pt->next ) ++tot;
519 }
520 tot /= 2;
521 e = pt;
522 for ( pt=base, i=0 ; i<tot ; pt = pt->next, ++i );
523 pt->use_as_pt = true;
524 base = e;
525 }
526 }
527
528 TraceMassage(head,cv->freehand.last);
529
530 /* Calculate the mids array */
531 mids = malloc(cnt*sizeof(FitPoint));
532 for ( base=head; base!=NULL && base->next!=NULL; base = pt ) {
533 mids[base->num].p.x = base->here.x;
534 mids[base->num].p.y = base->here.y;
535 mids[base->num].t = 0;
536 len = 0;
537 if ( base->next->online ) {
538 pt = base->next; /* Don't bother to calculate the length, we won't use the data */
539 continue;
540 }
541 for ( pt=base->next; ; pt=pt->next ) {
542 len += sqrt((double) (
543 (pt->x-pt->prev->x)*(pt->x-pt->prev->x) +
544 (pt->y-pt->prev->y)*(pt->y-pt->prev->y) ));
545 if ( pt->use_as_pt )
546 break;
547 }
548 sofar = 0;
549 for ( pt=base->next; ; pt=pt->next ) {
550 sofar += sqrt((double) (
551 (pt->x-pt->prev->x)*(pt->x-pt->prev->x) +
552 (pt->y-pt->prev->y)*(pt->y-pt->prev->y) ));
553 mids[pt->num].p.x = pt->here.x;
554 mids[pt->num].p.y = pt->here.y;
555 mids[pt->num].t = sofar/len;
556 if ( pt->use_as_pt )
557 break;
558 }
559 }
560
561 /* Splice things together */
562 spl = chunkalloc(sizeof(SplineSet));
563 spl->first = last = SplinePointCreate(rint(head->here.x),rint(head->here.y));
564 last->ptindex = 0;
565
566 for ( base=head; base!=NULL && base->next!=NULL; base = pt ) {
567 for ( pt=base->next; !pt->use_as_pt ; pt=pt->next );
568 cur = SplinePointCreate(rint(pt->here.x),rint(pt->here.y));
569 cur->ptindex = pt->num;
570 /* even if we are order2, do everything in order3, and then convert */
571 /* when they raise the pen */
572 if ( base->next->online || base->next==pt )
573 SplineMake(last,cur,false);
574 else {
575 TraceFigureCPs(last,cur,base,pt);
576 if ( last->nextcp.x == last->me.x && last->nextcp.y == last->me.y &&
577 cur->prevcp.x == cur->me.x && cur->prevcp.y == cur->me.y )
578 ApproximateSplineFromPointsSlopes(last,cur,mids+base->num+1,pt->num-base->num-1,false,mt_matrix);
579 else {
580 last->nextcp = last->me; cur->prevcp=cur->me;
581 ApproximateSplineFromPoints(last,cur,mids+base->num+1,pt->num-base->num-1,false);
582 }
583 }
584 last = cur;
585 }
586 spl->last = last;
587
588 /* Now we've got a rough approximation to the contour, but the joins are */
589 /* probably not smooth. Clean things up a bit... */
590 if ( spl->first->nonextcp )
591 spl->first->pointtype = pt_corner;
592 else
593 spl->first->pointtype = pt_curve;
594 if ( spl->last->noprevcp )
595 spl->last->pointtype = pt_corner;
596 else
597 spl->last->pointtype = pt_curve;
598
599 free(mids);
600
601 si = CVFreeHandInfo();
602 if ( si->stroke_type!=si_centerline ) {
603 si->factor = ( si->pressure1==si->pressure2 ) ? NULL : Trace_Factor;
604 si->data = cv;
605 spl->next = SplineSetStroke(spl,si,false);
606 }
607 return( spl );
608 }
609
610 static void TraceDataClose(CharView *cv,GEvent *event) {
611 TraceData *new;
612 SplineSet *trace;
613 double langle, hangle, llen, hlen;
614 double dx,dy;
615 BasePoint oldp, oldn;
616
617 if ( cv->freehand.head==NULL )
618 return; /* Eh? No points? How did that happen? */
619 if ( cv->freehand.head->here.x!=cv->freehand.last->here.x ||
620 cv->freehand.head->here.y!=cv->freehand.last->here.y ) {
621 new = chunkalloc(sizeof(TraceData));
622 *new = *cv->freehand.head;
623 new->time = event->u.mouse.time;
624 new->wasconstrained = (event->u.mouse.state&ksm_shift)?1:0;
625
626 new->prev = cv->freehand.last;
627 new->next = NULL;
628 cv->freehand.last->next = new;
629 cv->freehand.last = new;
630 SplinePointListsFree(cv->freehand.current_trace);
631 cv->freehand.current_trace = TraceCurve(cv);
632 } else if ( cv->freehand.head == cv->freehand.last )
633 return; /* Only one point, no good way to close it */
634
635 /* Now the first and last points are at the same location. We need to: */
636 /* average their control points (so that they form a smooth curve) */
637 /* merge the two SplinePoints into one */
638
639 trace = cv->freehand.current_trace;
640 trace->first->prevcp = trace->last->prevcp;
641 trace->first->noprevcp = trace->last->noprevcp;
642 trace->first->prevcpdef = trace->last->prevcpdef;
643 trace->first->prev = trace->last->prev;
644 trace->first->prev->to = trace->first;
645 SplinePointFree(trace->last);
646 trace->last = trace->first;
647
648 if ( cv->freehand.head->wasconstrained || cv->freehand.last->wasconstrained )
649 return;
650
651 trace->first->pointtype = pt_curve;
652
653 if ( trace->first->nonextcp && trace->first->noprevcp ) {
654 SplineCharDefaultPrevCP(trace->first);
655 SplineCharDefaultNextCP(trace->first);
656 return;
657 }
658
659 dx = trace->first->nextcp.x-trace->first->me.x;
660 dy = trace->first->nextcp.y-trace->first->me.y;
661 hlen = sqrt(dx*dx+dy*dy);
662 hangle = atan2(dy,dx);
663 oldn = trace->first->nextcp;
664 dx = trace->first->me.x-trace->first->prevcp.x;
665 dy = trace->first->me.y-trace->first->prevcp.y;
666 llen = sqrt(dx*dx+dy*dy);
667 langle = atan2(dy,dx);
668 oldp = trace->first->prevcp;
669
670 if ( trace->first->nonextcp ) {
671 SplineCharDefaultPrevCP(trace->first);
672 dx = trace->first->me.x-trace->first->prevcp.x;
673 dy = trace->first->me.y-trace->first->prevcp.y;
674 llen = sqrt(dx*dx+dy*dy);
675 trace->first->prevcp.x = trace->first->me.x -
676 cos(hangle)*llen;
677 trace->first->prevcp.y = trace->first->me.y -
678 sin(hangle)*llen;
679 trace->first->nextcp = oldn;
680 } else if ( trace->first->noprevcp ) {
681 SplineCharDefaultPrevCP(trace->first);
682 dx = trace->first->me.x-trace->first->nextcp.x;
683 dy = trace->first->me.y-trace->first->nextcp.y;
684 hlen = sqrt(dx*dx+dy*dy);
685 trace->first->nextcp.x = trace->first->me.x +
686 cos(langle)*hlen;
687 trace->first->nextcp.y = trace->first->me.y +
688 sin(langle)*hlen;
689 trace->first->prevcp = oldp;
690 } else {
691 if ( hangle>FF_PI/2 && langle<-FF_PI/2 )
692 langle += 2*FF_PI;
693 if ( hangle<-FF_PI/2 && langle>FF_PI/2 )
694 hangle += 2*FF_PI;
695 hangle = (hangle+langle)/2;
696 dx = cos(hangle);
697 dy = sin(hangle);
698 trace->first->prevcp.x = trace->first->me.x - dx*llen;
699 trace->first->prevcp.y = trace->first->me.y - dy*llen;
700 trace->first->nextcp.x = trace->first->me.x + dx*hlen;
701 trace->first->nextcp.y = trace->first->me.y + dy*hlen;
702 }
703 SplineRefigure(trace->first->next);
704 SplineRefigure(trace->first->prev);
705 }
706
707 static int TraceDataCleanup(CharView *cv) {
708 TraceData *mid, *next;
709 int cnt=0;
710
711 for ( mid = cv->freehand.head; mid!=NULL; mid=next ) {
712 ++cnt;
713 next = mid->next;
714 }
715 return( cnt );
716 }
717
718 void CVMouseDownFreeHand(CharView *cv, GEvent *event) {
719
720 TraceDataFree(cv->freehand.head);
721 cv->freehand.head = cv->freehand.last = NULL;
722 cv->freehand.current_trace = NULL;
723 TraceDataFromEvent(cv,event);
724
725 cv->freehand.current_trace = chunkalloc(sizeof(SplinePointList));
726 cv->freehand.current_trace->first = cv->freehand.current_trace->last =
727 SplinePointCreate(rint(cv->freehand.head->here.x),rint(cv->freehand.head->here.y));
728 }
729
730 void CVMouseMoveFreeHand(CharView *cv, GEvent *event) {
731 CharViewTab* tab = CVGetActiveTab(cv);
732 double dx, dy;
733 SplinePoint *last;
734 BasePoint *here;
735
736 TraceDataFromEvent(cv,event);
737 /* I used to do the full processing here to get a path. But that took */
738 /* too long to process them and we appeared to get events out of order */
739 /* from the wacom tablet */
740 last = cv->freehand.current_trace->last;
741 here = &cv->freehand.last->here;
742 if ( (dx=here->x-last->me.x)<0 ) dx = -dx;
743 if ( (dy=here->y-last->me.y)<0 ) dy = -dy;
744 if ( (dx+dy)*tab->scale > 4 ) {
745 SplineMake3(last,SplinePointCreate(rint(here->x),rint(here->y)));
746 cv->freehand.current_trace->last = last->next->to;
747 GDrawRequestExpose(cv->v,NULL,false);
748 }
749 }
750
751 #ifdef DEBUG_FREEHAND
752 static void RepeatFromFile(CharView *cv) {
753 FILE *foo = fopen("mousemove","r");
754 /*char buffer[100];*/
755 GEvent e;
756 int x,y,p,t;
757
758 if ( foo==NULL )
759 return;
760
761 memset(&e,0,sizeof(e));
762
763 e.w = cv->v;
764 e.type = et_mousedown;
765 fscanf(foo,"(%d,%d) (%*g,%*g) %d %d\n", &x, &y, &p, &t);
766 e.u.mouse.x = x; e.u.mouse.y = y; e.u.mouse.pressure = p; e.u.mouse.time = t;
767 CVMouseDownFreeHand(cv,&e);
768
769 e.type = et_mousemove;
770 while ( fscanf(foo,"(%d,%d) (%*g,%*g) %d %d\n", &x, &y, &p, &t)==4 ) {
771 e.u.mouse.x = x; e.u.mouse.y = y; e.u.mouse.pressure = p; e.u.mouse.time = t;
772 CVMouseMoveFreeHand(cv,&e);
773 }
774
775 e.type = et_mouseup;
776 CVMouseUpFreeHand(cv,&e);
777 fclose(foo);
778 }
779 #endif
780
781 void CVMouseUpFreeHand(CharView *cv, GEvent *event) {
782 CharViewTab* tab = CVGetActiveTab(cv);
783 TraceData *head = cv->freehand.head;
784 TraceData *last;
785 double dx, dy;
786
787 #ifdef DEBUG_FREEHAND
788 if ( event->u.mouse.clicks>1 ) {
789 TraceDataFree(cv->freehand.head);
790 cv->freehand.head = cv->freehand.last = NULL;
791 RepeatFromFile(cv);
792 return;
793 }
794 #endif
795 if ( head==NULL )
796 return;
797
798 if ( TraceDataCleanup(cv)>=4 ) {
799 /* If there are fewer than 4 points then assume they made a mistake */
800 /* (or intended a double click) and just ignore the path */
801 last = cv->freehand.last;
802 if ( (dx=head->x-last->x)<0 ) dx = -dx;
803 if ( (dy=head->y-last->y)<0 ) dy = -dy;
804
805 if (( event->u.chr.state&ksm_meta ) || (dx+dy)*tab->scale > 4 )
806 TraceDataClose(cv,event);
807 else {
808 SplinePointListsFree(cv->freehand.current_trace);
809 cv->freehand.current_trace = TraceCurve(cv);
810 }
811 if ( cv->freehand.current_trace!=NULL ) {
812 CVPreserveState((CharViewBase *) cv);
813 if ( cv->b.layerheads[cv->b.drawmode]->order2 )
814 cv->freehand.current_trace = SplineSetsTTFApprox(cv->freehand.current_trace);
815 if ( CVFreeHandInfo()->stroke_type==si_centerline ) {
816 cv->freehand.current_trace->next = cv->b.layerheads[cv->b.drawmode]->splines;
817 cv->b.layerheads[cv->b.drawmode]->splines = cv->freehand.current_trace;
818 } else {
819 SplineSet *ss = cv->freehand.current_trace;
820 while ( ss->next!=NULL )
821 ss = ss->next;
822 ss->next = cv->b.layerheads[cv->b.drawmode]->splines;
823 cv->b.layerheads[cv->b.drawmode]->splines = cv->freehand.current_trace->next;
824 cv->freehand.current_trace->next = NULL;
825 /*SplinePointListsFree(cv->freehand.current_trace);*/
826 }
827 cv->freehand.current_trace = NULL;
828 }
829 } else {
830 SplinePointListsFree(cv->freehand.current_trace);
831 cv->freehand.current_trace = NULL;
832 }
833 TraceDataFree(cv->freehand.head);
834 cv->freehand.head = cv->freehand.last = NULL;
835 CVCharChangedUpdate(&cv->b);
836 #ifdef DEBUG_FREEHAND
837 fflush( stdout );
838 #endif
839 }