Debian Codegraph @

fontforgeexe/alignment.c

raw symbols SCIP
1 /* -*- coding: utf-8 -*- */
2 /* Copyright (C) 2000-2012 by George Williams */
3 /*
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6
7 * Redistributions of source code must retain the above copyright notice, this
8 * list of conditions and the following disclaimer.
9
10 * Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13
14 * The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
20 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
23 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
25 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
26 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include <fontforge-config.h>
30
31 #include "cvundoes.h"
32 #include "fontforgeui.h"
33 #include "splinefont.h"
34 #include "ustring.h"
35
36 #include <math.h>
37
38 static void SpAdjustTo(SplinePoint *sp,real newx, real newy) {
39 sp->prevcp.x += newx-sp->me.x;
40 sp->nextcp.x += newx-sp->me.x;
41 sp->prevcp.y += newy-sp->me.y;
42 sp->nextcp.y += newy-sp->me.y;
43 sp->me.x = newx;
44 sp->me.y = newy;
45 }
46
47 static void SpaceOne(CharView *cv,SplinePoint *sp) {
48 SplinePoint *prev, *next;
49 BasePoint v, new;
50 real len, off;
51 /* Rotate the coordinate system so that one axis is parallel to the */
52 /* line between sp->next->to and sp->prev->from. Position sp so that */
53 /* it is mid-way between the two on that axis while its distance from */
54 /* the axis (ie. the other coord) remains unchanged */
55 /* Of course we do this with dot products rather than actual rotations */
56
57 if ( sp->next==NULL || sp->prev==NULL )
58 return;
59
60 prev = sp->prev->from; next = sp->next->to;
61 if ( prev==next )
62 return;
63
64 v.x = next->me.x - prev->me.x;
65 v.y = next->me.y - prev->me.y;
66 len = sqrt(v.x*v.x + v.y*v.y);
67 if ( len==0 )
68 return;
69 v.x /= len; v.y /= len;
70 off = (sp->me.x-prev->me.x)*v.y - (sp->me.y-prev->me.y)*v.x;
71 new.x = (next->me.x + prev->me.x)/2 + off*v.y;
72 new.y = (next->me.y + prev->me.y)/2 - off*v.x;
73
74 CVPreserveState((CharViewBase *) cv);
75 SpAdjustTo(sp,new.x,new.y);
76 SplineRefigure(sp->prev); SplineRefigure(sp->next);
77 CVCharChangedUpdate(&cv->b);
78 }
79
80 static void SpaceMany(CharView *cv,DBounds *b, int dir, int region_size, int cnt) {
81 SplinePoint *sp;
82 SplineSet *spl;
83 struct region { real begin, end, offset; } *regions;
84 int rcnt,i,j;
85 real range, rtot, space, rpos;
86
87 if ( dir==-1 ) {
88 if ( b->maxx - b->minx > b->maxy - b->miny )
89 dir = 0; /* Space out along x axis */
90 else
91 dir = 1; /* Space out along y axis */
92 }
93
94 regions = malloc(cnt*sizeof(struct region));
95 rcnt = 0;
96 for ( spl= cv->b.layerheads[cv->b.drawmode]->splines; spl!=NULL; spl=spl->next ) {
97 sp=spl->first;
98 while ( 1 ) {
99 if ( sp->selected ) {
100 real coord = dir?sp->me.y:sp->me.x;
101 for ( i=0; i<rcnt && coord>regions[i].end+region_size; ++i );
102 if ( i==rcnt ) {
103 regions[i].begin = regions[i].end = coord;
104 ++rcnt;
105 } else if ( coord<regions[i].begin-region_size ) {
106 for ( j=++rcnt; j>i; --j )
107 regions[j] = regions[j-1];
108 regions[i].begin = regions[i].end = coord;
109 } else {
110 if ( regions[i].begin>coord )
111 regions[i].begin = coord;
112 else if ( regions[i].end < coord ) {
113 regions[i].end = coord;
114 if ( i<rcnt-1 && regions[i].end+region_size>=regions[i+1].begin ) {
115 /* Merge two regions */
116 regions[i].end = regions[i+1].end;
117 --rcnt;
118 for ( j=i+1; j<rcnt; ++j )
119 regions[j] = regions[j+1];
120 }
121 }
122 }
123 }
124 if ( sp->next==NULL )
125 break;
126 sp = sp->next->to;
127 if ( sp==spl->first )
128 break;
129 }
130 }
131
132 /* we need at least three regions to space things out */
133 if ( rcnt<3 )
134 return;
135
136 /* Now should I allow equal spaces between regions, or spaces between */
137 /* region mid-points? I think spaces between regions */
138 range = regions[rcnt-1].end-regions[0].begin;
139 rtot = 0;
140 for ( j=0; j<rcnt; ++j )
141 rtot += regions[j].end-regions[j].begin;
142 space = (range-rtot)/(rcnt-1);
143 rpos = regions[0].begin;
144 for ( j=0; j<rcnt-1; ++j ) {
145 regions[j].offset = rpos-regions[j].begin;
146 rpos += regions[j].end-regions[j].begin+space;
147 }
148 regions[rcnt-1].offset = 0;
149
150 CVPreserveState((CharViewBase *) cv);
151 for ( spl= cv->b.layerheads[cv->b.drawmode]->splines; spl!=NULL; spl=spl->next ) {
152 sp=spl->first;
153 while ( 1 ) {
154 if ( sp->selected ) {
155 real coord = dir?sp->me.y:sp->me.x;
156 for ( i=0; i<rcnt && coord>regions[i].end; ++i );
157 if ( i==rcnt )
158 IError( "Region list is screwed up");
159 else {
160 if ( dir==0 ) {
161 sp->me.x += regions[i].offset;
162 sp->prevcp.x += regions[i].offset;
163 sp->nextcp.x += regions[i].offset;
164 } else {
165 sp->me.y += regions[i].offset;
166 sp->prevcp.y += regions[i].offset;
167 sp->nextcp.y += regions[i].offset;
168 }
169 if ( sp->prev!=NULL )
170 SplineRefigure(sp->prev);
171 if ( sp->next!=NULL )
172 SplineRefigure(sp->next);
173 }
174 }
175 if ( sp->next==NULL )
176 break;
177 sp = sp->next->to;
178 if ( sp==spl->first )
179 break;
180 }
181 }
182 CVCharChangedUpdate(&cv->b);
183 free(regions);
184 }
185
186
187 static void AlignTwoMaybeAsk(CharView *cv,SplinePoint *sp1, SplinePoint *sp2)
188 {
189 real xoff, yoff, xpos, ypos;
190 int HorizontalAlignment = 0;
191
192 xoff = sp1->me.x - sp2->me.x;
193 yoff = sp1->me.y - sp2->me.y;
194 // printf("AlignTwo() %f %f, %f %f, xoff:%f yoff:%f\n",
195 // sp1->me.x, sp2->me.x,
196 // sp1->me.y, sp2->me.y,
197 // xoff, yoff );
198
199 if ( fabs(yoff)<fabs(xoff)/2 ) {
200 // printf("average y\n");
201 /* average y */
202 HorizontalAlignment = 0;
203 } else if ( fabs(xoff)<fabs(yoff)/2 ) {
204 /* average x */
205 // printf("average x\n");
206 HorizontalAlignment = 1;
207 }
208 else {
209 char *buts[5];
210
211 buts[0] = _("_Horizontal");
212 buts[1] = _("_Vertical");
213 buts[2] = _("_Cancel");
214 buts[3] = NULL;
215
216 int asked = gwwv_ask(_("Align Points"),(const char **) buts,0,3,_("How to align these points?"));
217 HorizontalAlignment = asked;
218 if( asked == 2 )
219 return;
220 }
221
222 CVPreserveState((CharViewBase *) cv);
223 if ( HorizontalAlignment == 0 )
224 {
225 // printf("average y\n");
226 /* average y */
227 ypos = rint( (sp1->me.y+sp2->me.y)/2 );
228 sp1->prevcp.y += ypos-sp1->me.y;
229 sp1->nextcp.y += ypos-sp1->me.y;
230 sp2->prevcp.y += ypos-sp2->me.y;
231 sp2->nextcp.y += ypos-sp2->me.y;
232 sp1->me.y = sp2->me.y = ypos;
233 }
234 else
235 {
236 /* average x */
237 // printf("average x\n");
238 xpos = rint( (sp1->me.x+sp2->me.x)/2 );
239 sp1->prevcp.x += xpos-sp1->me.x;
240 sp1->nextcp.x += xpos-sp1->me.x;
241 sp2->prevcp.x += xpos-sp2->me.x;
242 sp2->nextcp.x += xpos-sp2->me.x;
243 sp1->me.x = sp2->me.x = xpos;
244 }
245
246 if ( sp1->prev ) SplineRefigure(sp1->prev);
247 if ( sp1->next ) SplineRefigure(sp1->next);
248 if ( sp2->prev ) SplineRefigure(sp2->prev);
249 if ( sp2->next ) SplineRefigure(sp2->next);
250 CVCharChangedUpdate(&cv->b);
251 }
252
253 static void AlignManyMaybeAsk(CharView *cv,DBounds *b) {
254 real xoff, yoff, xpos, ypos;
255 SplinePoint *sp;
256 SplineSet *spl;
257 int HorizontalAlignment = 0;
258
259 xoff = b->maxx - b->minx;
260 yoff = b->maxy - b->miny;
261
262 if ( yoff<xoff )
263 {
264 /* average y */
265 HorizontalAlignment = 0;
266 }
267 else if ( xoff<yoff/2 )
268 {
269 /* constrain x */
270 HorizontalAlignment = 1;
271 }
272 else
273 {
274 char *buts[5];
275
276 buts[0] = _("_Horizontal");
277 buts[1] = _("_Vertical");
278 buts[2] = _("_Cancel");
279 buts[3] = NULL;
280
281 int asked = gwwv_ask(_("Align Points"),(const char **) buts,0,3,_("How to align these points?"));
282 HorizontalAlignment = asked;
283 if( asked == 2 )
284 return;
285 }
286
287 CVPreserveState((CharViewBase *) cv);
288 if ( HorizontalAlignment == 0 )
289 {
290 /* average y */
291 ypos = rint( (b->maxy+b->miny)/2 );
292 for ( spl= cv->b.layerheads[cv->b.drawmode]->splines; spl!=NULL; spl=spl->next )
293 {
294 sp=spl->first;
295 while ( 1 ) {
296 if ( sp->selected ) {
297 sp->prevcp.y += ypos-sp->me.y;
298 sp->nextcp.y += ypos-sp->me.y;
299 sp->me.y = ypos;
300 if ( sp->prev ) SplineRefigure(sp->prev);
301 if ( sp->next ) SplineRefigure(sp->next);
302 }
303 if ( sp->next==NULL )
304 break;
305 sp = sp->next->to;
306 if ( sp==spl->first )
307 break;
308 }
309 }
310 }
311 else
312 {
313 /* constrain x */
314 xpos = rint( (b->maxx+b->minx)/2 );
315 for ( spl= cv->b.layerheads[cv->b.drawmode]->splines; spl!=NULL; spl=spl->next )
316 {
317 sp=spl->first;
318 while ( 1 ) {
319 if ( sp->selected ) {
320 sp->prevcp.x += xpos-sp->me.x;
321 sp->nextcp.x += xpos-sp->me.x;
322 sp->me.x = xpos;
323 if ( sp->prev ) SplineRefigure(sp->prev);
324 if ( sp->next ) SplineRefigure(sp->next);
325 }
326 if ( sp->next==NULL )
327 break;
328 sp = sp->next->to;
329 if ( sp==spl->first )
330 break;
331 }
332 }
333 }
334 CVCharChangedUpdate(&cv->b);
335 }
336
337
338 struct rcd {
339 CharView *cv;
340 int done;
341 DBounds *b;
342 int cnt;
343 };
344 static double lastsize = 100;
345
346 #define CID_Y 1001
347 #define CID_X 1002
348 #define CID_Size 1003
349
350
351 static int RC_OK(GGadget *g, GEvent *e) {
352 if ( e->type==et_controlevent && e->u.control.subtype == et_buttonactivate ) {
353 GWindow gw = GGadgetGetWindow(g);
354 struct rcd *rcd = GDrawGetUserData(gw);
355 int err=false;
356 real size;
357 int dir = GGadgetIsChecked(GWidgetGetControl(gw,CID_Y));
358 size = GetReal8(gw,CID_Size,_("_Size:"),&err);
359 if ( err )
360 return(true);
361 SpaceMany(rcd->cv,rcd->b, dir, size, rcd->cnt);
362
363 rcd->done = true;
364 }
365 return( true );
366 }
367
368 static int RC_Cancel(GGadget *g, GEvent *e) {
369 if ( e->type==et_controlevent && e->u.control.subtype == et_buttonactivate ) {
370 struct rcd *rcd = GDrawGetUserData(GGadgetGetWindow(g));
371 rcd->done = true;
372 }
373 return( true );
374 }
375
376 static int rcd_e_h(GWindow gw, GEvent *event) {
377 if ( event->type==et_close ) {
378 struct rcd *rcd = GDrawGetUserData(gw);
379 rcd->done = true;
380 }
381 return( event->type!=et_char );
382 }
383
384 static void RegionControl(CharView *cv,DBounds *b,int cnt) {
385 GRect pos;
386 GWindow gw;
387 GWindowAttrs wattrs;
388 GGadgetCreateData gcd[9], boxes[5], *rarray[5], *narray[5], *barray[10], *hvarray[7][2];
389 GTextInfo label[9];
390 struct rcd rcd;
391 char buffer[20];
392
393 rcd.cv = cv;
394 rcd.b = b;
395 rcd.cnt = cnt;
396 rcd.done = false;
397
398 memset(&wattrs,0,sizeof(wattrs));
399 wattrs.mask = wam_events|wam_cursor|wam_utf8_wtitle|wam_undercursor|wam_isdlg|wam_restrict;
400 wattrs.event_masks = ~(1<<et_charup);
401 wattrs.restrict_input_to_me = 1;
402 wattrs.undercursor = 1;
403 wattrs.cursor = ct_pointer;
404 wattrs.utf8_window_title = _("Space Regions");
405 wattrs.is_dlg = true;
406 pos.x = pos.y = 0;
407 pos.width = GGadgetScale(GDrawPointsToPixels(NULL,225));
408 pos.height = GDrawPointsToPixels(NULL,115);
409 gw = GDrawCreateTopWindow(NULL,&pos,rcd_e_h,&rcd,&wattrs);
410
411 memset(&label,0,sizeof(label));
412 memset(&gcd,0,sizeof(gcd));
413 memset(&boxes,0,sizeof(boxes));
414
415 label[0].text = (unichar_t *) _("Coordinate along which to space");
416 label[0].text_is_1byte = true;
417 gcd[0].gd.label = &label[0];
418 gcd[0].gd.pos.x = 5; gcd[0].gd.pos.y = 6;
419 gcd[0].gd.flags = gg_enabled|gg_visible;
420 gcd[0].creator = GLabelCreate;
421 hvarray[0][0] = &gcd[0]; hvarray[0][1] = NULL;
422
423 label[1].text = (unichar_t *) _("_X");
424 label[1].text_is_1byte = true;
425 label[1].text_in_resource = true;
426 gcd[1].gd.label = &label[1];
427 gcd[1].gd.pos.x = 25; gcd[1].gd.pos.y = gcd[0].gd.pos.y+13;
428 gcd[1].gd.flags = gg_enabled|gg_visible;
429 gcd[1].creator = GRadioCreate;
430 gcd[1].gd.cid = CID_X;
431 rarray[0] = GCD_HPad10; rarray[1] = &gcd[1];
432
433 label[2].text = (unichar_t *) _("_Y");
434 label[2].text_is_1byte = true;
435 label[2].text_in_resource = true;
436 gcd[2].gd.label = &label[2];
437 gcd[2].gd.pos.x = 60; //FIXME: gcd[2].gd.pos.y = gcd[2].gd.pos.y;
438 gcd[2].gd.flags = gg_enabled|gg_visible;
439 gcd[2].gd.cid = CID_Y;
440 gcd[2].creator = GRadioCreate;
441 rarray[2] = &gcd[2];
442 rarray[3] = GCD_Glue; rarray[4] = NULL;
443
444 boxes[2].gd.flags = gg_enabled | gg_visible;
445 boxes[2].gd.u.boxelements = rarray;
446 boxes[2].creator = GHBoxCreate;
447 hvarray[1][0] = &boxes[2]; hvarray[1][1] = NULL;
448
449 if ( b->maxx-b->minx > b->maxy-b->miny )
450 gcd[1].gd.flags |= gg_cb_on;
451 else
452 gcd[2].gd.flags |= gg_cb_on;
453
454 label[3].text = (unichar_t *) _("_Maximum distance between points in a region");
455 label[3].text_is_1byte = true;
456 label[3].text_in_resource = true;
457 gcd[3].gd.label = &label[3];
458 gcd[3].gd.pos.x = 5; gcd[3].gd.pos.y = gcd[1].gd.pos.y+16;
459 gcd[3].gd.flags = gg_enabled|gg_visible;
460 gcd[3].creator = GLabelCreate;
461 hvarray[2][0] = &gcd[3]; hvarray[2][1] = NULL;
462
463 sprintf( buffer, "%g", lastsize );
464 label[4].text = (unichar_t *) buffer;
465 label[4].text_is_1byte = true;
466 gcd[4].gd.label = &label[4];
467 gcd[4].gd.pos.x = gcd[1].gd.pos.x; gcd[4].gd.pos.y = gcd[3].gd.pos.y+14; gcd[4].gd.pos.width = 40;
468 gcd[4].gd.flags = gg_enabled|gg_visible;
469 gcd[4].gd.cid = CID_Size;
470 gcd[4].creator = GNumericFieldCreate;
471 narray[0] = GCD_HPad10; narray[1] = &gcd[4]; narray[2] = GCD_Glue; narray[3] = NULL;
472
473 boxes[3].gd.flags = gg_enabled | gg_visible;
474 boxes[3].gd.u.boxelements = narray;
475 boxes[3].creator = GHBoxCreate;
476 hvarray[3][0] = &boxes[3]; hvarray[3][1] = NULL;
477
478 gcd[5].gd.pos.x = 20-3; gcd[5].gd.pos.y = gcd[4].gd.pos.y+35-3;
479 gcd[5].gd.flags = gg_visible | gg_enabled | gg_but_default;
480 label[5].text = (unichar_t *) _("_OK");
481 label[5].text_is_1byte = true;
482 label[5].text_in_resource = true;
483 gcd[5].gd.mnemonic = 'O';
484 gcd[5].gd.label = &label[5];
485 gcd[5].gd.handle_controlevent = RC_OK;
486 gcd[5].creator = GButtonCreate;
487 barray[0] = GCD_Glue; barray[1] = &gcd[5]; barray[2] = barray[3] = barray[4] = barray[5] = GCD_Glue;
488
489 gcd[6].gd.pos.x = -20; gcd[6].gd.pos.y = gcd[5].gd.pos.y+3;
490 gcd[6].gd.flags = gg_visible | gg_enabled | gg_but_cancel;
491 label[6].text = (unichar_t *) _("_Cancel");
492 label[6].text_is_1byte = true;
493 label[6].text_in_resource = true;
494 gcd[6].gd.label = &label[6];
495 gcd[6].gd.mnemonic = 'C';
496 gcd[6].gd.handle_controlevent = RC_Cancel;
497 gcd[6].creator = GButtonCreate;
498 barray[5] = &gcd[6]; barray[6] = GCD_Glue; barray[7] = NULL;
499
500 boxes[4].gd.flags = gg_enabled | gg_visible;
501 boxes[4].gd.u.boxelements = barray;
502 boxes[4].creator = GHBoxCreate;
503 hvarray[4][0] = GCD_Glue; hvarray[4][1] = NULL;
504 hvarray[5][0] = &boxes[4]; hvarray[5][1] = NULL;
505 hvarray[6][0] = NULL;
506
507 boxes[0].gd.pos.x = boxes[0].gd.pos.y = 2;
508 boxes[0].gd.flags = gg_enabled | gg_visible;
509 boxes[0].gd.u.boxelements = hvarray[0];
510 boxes[0].creator = GHVGroupCreate;
511
512 GGadgetsCreate(gw,boxes);
513 GHVBoxSetExpandableRow(boxes[0].ret,gb_expandglue);
514 GHVBoxSetExpandableCol(boxes[2].ret,gb_expandglue);
515 GHVBoxSetExpandableCol(boxes[3].ret,gb_expandglue);
516 GHVBoxSetExpandableCol(boxes[4].ret,gb_expandgluesame);
517 GHVBoxFitWindow(boxes[0].ret);
518
519 GTextFieldSelect(GWidgetGetControl(gw,CID_Size),0,-1);
520
521 GDrawSetVisible(gw,true);
522 while ( !rcd.done )
523 GDrawProcessOneEvent(NULL);
524 GDrawDestroyWindow(gw);
525 }
526
527 void CVConstrainSelection(CharView *cv,constrainSelection_t type) {
528 DBounds b;
529 SplinePoint *first=NULL, *second=NULL, *other=NULL, *sp;
530 SplineSet *spl;
531 int cnt=0;
532
533 for ( spl= cv->b.layerheads[cv->b.drawmode]->splines; spl!=NULL; spl=spl->next ) {
534 sp=spl->first;
535 while ( 1 ) {
536 if ( sp->selected ) {
537 ++cnt;
538 if ( first == NULL ) {
539 first = sp;
540 b.minx = b.maxx = sp->me.x;
541 b.miny = b.maxy = sp->me.y;
542 } else {
543 if ( second==NULL ) second = sp;
544 else other = sp;
545 if ( b.minx>sp->me.x ) b.minx = sp->me.x;
546 if ( b.maxx<sp->me.x ) b.maxx = sp->me.x;
547 if ( b.miny>sp->me.y ) b.miny = sp->me.y;
548 if ( b.maxy<sp->me.y ) b.maxy = sp->me.y;
549 }
550 }
551 if ( sp->next==NULL )
552 break;
553 sp = sp->next->to;
554 if ( sp==spl->first )
555 break;
556 }
557 }
558
559 if ( type == constrainSelection_AveragePoints ) {
560 /* Average points */
561 if ( second==NULL )
562 /* Do Nothing */;
563 else if ( !other )
564 AlignTwoMaybeAsk(cv,first,second);
565 else
566 AlignManyMaybeAsk(cv,&b);
567 } else if ( type == constrainSelection_SpacePoints ) {
568 /* Space points */
569 if ( other!=NULL )
570 SpaceMany(cv,&b,-1,0,cnt);
571 else if ( second!=NULL )
572 /* Can't deal with it */;
573 else if ( first!=NULL )
574 SpaceOne(cv,first);
575 else {
576 /* Nothing selected */;
577 }
578 } else if ( type == constrainSelection_SpaceSelectedRegions ) {
579 /* Space selected regions */
580 if ( other==NULL )
581 return; /* Do nothing, need at least three regions */
582 RegionControl(cv,&b,cnt);
583 }
584 }
585
586 static void MakeParallel(Spline *e1, Spline *e2, SplinePoint *mobile) {
587 /* Splines e1&e2 are to be made parallel */
588 /* The spline containing mobile is the one which is to be moved */
589 Spline *temp;
590 double xdiff, ydiff, axdiff, aydiff;
591 SplinePoint *other;
592
593 if ( e1->to==mobile || e1->from==mobile ) {
594 temp = e1;
595 e1 = e2;
596 e2 = temp;
597 }
598 /* Now the spline to be moved is e2 */
599 other = e2->from==mobile ? e2->to : e2->from;
600
601 if ( (axdiff = xdiff = e1->to->me.x-e1->from->me.x)<0 ) axdiff = -axdiff;
602 if ( (aydiff = ydiff = e1->to->me.y-e1->from->me.y)<0 ) aydiff = -aydiff;
603 if ( aydiff > axdiff ) {
604 /* Hold the y coordinate fixed in e2 and move the x coord appropriately */
605 int oldx = mobile->me.x;
606 mobile->me.x = (mobile->me.y-other->me.y)*xdiff/ydiff + other->me.x;
607 mobile->nextcp.x += mobile->me.x-oldx;
608 mobile->prevcp.x += mobile->me.x-oldx;
609 } else {
610 int oldy = mobile->me.y;
611 mobile->me.y = (mobile->me.x-other->me.x)*ydiff/xdiff + other->me.y;
612 mobile->nextcp.y += mobile->me.y-oldy;
613 mobile->prevcp.y += mobile->me.y-oldy;
614 }
615 if ( mobile->next!=NULL )
616 SplineRefigure(mobile->prev);
617 if ( mobile->prev!=NULL )
618 SplineRefigure(mobile->next);
619 }
620
621 static void MakeParallelogram(Spline *e11, Spline *e12, Spline *e21, Spline *e22,
622 SplinePoint *mobile) {
623 /* Splines e11&e12 are to be made parallel, as are e21&e22 */
624 /* The spline containing mobile is the one which is to be moved */
625 Spline *temp;
626 SplinePoint *other1, *other2, *unconnected;
627 double denom;
628
629 if ( e11->to==mobile || e11->from==mobile ) {
630 temp = e11;
631 e11 = e12;
632 e12 = temp;
633 }
634 if ( e21->to==mobile || e21->from==mobile ) {
635 temp = e21;
636 e21 = e22;
637 e22 = temp;
638 }
639 /* Now the splines to be moved are e?2, while e?1 is held fixed */
640 other1 = e12->from==mobile ? e12->to : e12->from;
641 other2 = e22->from==mobile ? e22->to : e22->from;
642 unconnected = e11->from==other2 ? e11->to : e11->from;
643
644 denom = (unconnected->me.y-other1->me.y)*(unconnected->me.x-other2->me.x) -
645 (unconnected->me.y-other2->me.y)*(unconnected->me.x-other1->me.x);
646 if ( denom>-.0001 && denom<.0001 ) {
647 /* The two splines e11 and e21 are essentially parallel, so we can't */
648 /* move mobile to the place where they intersect */
649 mobile->me = unconnected->me;
650 } else {
651 mobile->me.y =
652 ((other2->me.x-other1->me.x)*(unconnected->me.y-other1->me.y)*
653 (unconnected->me.y-other2->me.y) -
654 other2->me.y*(unconnected->me.y-other2->me.y)*(unconnected->me.x-other1->me.x) +
655 other1->me.y*(unconnected->me.y-other1->me.y)*(unconnected->me.x-other2->me.x))/
656 denom;
657 if ( unconnected->me.y-other1->me.y==0 )
658 mobile->me.x = other1->me.x + (unconnected->me.x-other2->me.x)/(unconnected->me.y-other2->me.y)*
659 (mobile->me.y-other1->me.y);
660 else
661 mobile->me.x = other2->me.x + (unconnected->me.x-other1->me.x)/(unconnected->me.y-other1->me.y)*
662 (mobile->me.y-other2->me.y);
663 }
664 mobile->nextcp = mobile->prevcp = mobile->me;
665 SplineRefigure(mobile->prev);
666 SplineRefigure(mobile->next);
667 }
668
669 static int CommonEndPoint(Spline *s1, Spline *s2) {
670 return( s1->to==s2->to || s1->to==s2->from || s1->from==s2->to || s1->from==s2->from );
671 }
672
673 void CVMakeParallel(CharView *cv) {
674 /* takes exactly four selected points and tries to find two lines between */
675 /* them which it then makes parallel */
676 /* If the points are a quadralateral then we make it a parallelogram */
677 /* If the points define 3 lines then throw out the middle one */
678 /* If the points define 2 lines then good */
679 /* Else complain */
680 /* If possible, fix things by moving the lastselpt */
681 SplinePoint *pts[4];
682 Spline *edges[4];
683 int cnt=0, mobilis;
684 SplineSet *ss;
685 SplinePoint *sp;
686
687 for ( ss = cv->b.layerheads[cv->b.drawmode]->splines; ss!=NULL; ss=ss->next ) {
688 for ( sp=ss->first; ; ) {
689 if ( sp->selected ) {
690 if ( cnt>=4 )
691 return;
692 pts[cnt++] = sp;
693 }
694 if ( sp->next==NULL )
695 break;
696 sp = sp->next->to;
697 if ( sp == ss->first )
698 break;
699 }
700 }
701 if ( cnt!=4 )
702 return;
703
704 for ( mobilis=0; mobilis<4; ++mobilis )
705 if ( pts[mobilis]==cv->lastselpt )
706 break;
707 if ( mobilis==4 ) mobilis=3; /* lastselpt didn't match, pick one */
708
709 cnt=0;
710 if ( pts[0]->next!=NULL && pts[0]->next->islinear &&
711 (pts[0]->next->to==pts[1] ||
712 pts[0]->next->to==pts[2] ||
713 pts[0]->next->to==pts[3]) &&
714 (pts[0]->me.x!=pts[0]->next->to->me.x || pts[0]->me.y!=pts[0]->next->to->me.y))
715 edges[cnt++] = pts[0]->next;
716 if ( pts[1]->next!=NULL && pts[1]->next->islinear &&
717 (pts[1]->next->to==pts[0] ||
718 pts[1]->next->to==pts[2] ||
719 pts[1]->next->to==pts[3]) &&
720 (pts[1]->me.x!=pts[1]->next->to->me.x || pts[1]->me.y!=pts[1]->next->to->me.y))
721 edges[cnt++] = pts[1]->next;
722 if ( pts[2]->next!=NULL && pts[2]->next->islinear &&
723 (pts[2]->next->to==pts[0] ||
724 pts[2]->next->to==pts[1] ||
725 pts[2]->next->to==pts[3]) &&
726 (pts[2]->me.x!=pts[2]->next->to->me.x || pts[2]->me.y!=pts[2]->next->to->me.y))
727 edges[cnt++] = pts[2]->next;
728 if ( pts[3]->next!=NULL && pts[3]->next->islinear &&
729 (pts[3]->next->to==pts[0] ||
730 pts[3]->next->to==pts[1] ||
731 pts[3]->next->to==pts[2]) &&
732 (pts[3]->me.x!=pts[3]->next->to->me.x || pts[3]->me.y!=pts[3]->next->to->me.y))
733 edges[cnt++] = pts[3]->next;
734
735 if ( cnt<2 ) {
736 ff_post_error(_("Not enough lines"),_("Not enough lines"));
737 return;
738 } else if ( cnt==2 && CommonEndPoint(edges[0],edges[1]) ) {
739 ff_post_error(_("Can't Parallel"),_("These two lines share a common endpoint, I can't make them parallel"));
740 return;
741 }
742
743 CVPreserveState((CharViewBase *) cv);
744 if ( cnt==4 ) {
745 int second=3, third=1, fourth=2;
746 if ( !CommonEndPoint(edges[0],edges[1])) {
747 second = 1; third = 3;
748 } else if ( !CommonEndPoint(edges[0],edges[2])) {
749 second = 2;
750 fourth = 3;
751 }
752 MakeParallelogram(edges[0],edges[second], edges[third],edges[fourth],
753 pts[mobilis]);
754 } else if ( cnt==3 ) {
755 if ( !CommonEndPoint(edges[0],edges[1]) )
756 MakeParallel(edges[0],edges[1],pts[mobilis]);
757 else if ( !CommonEndPoint(edges[0],edges[2]) )
758 MakeParallel(edges[0],edges[2],pts[mobilis]);
759 else
760 MakeParallel(edges[1],edges[2],pts[mobilis]);
761 } else {
762 MakeParallel(edges[0],edges[1],pts[mobilis]);
763 }
764 CVCharChangedUpdate(&cv->b);
765 }