Debian Codegraph @

fontforgeexe/cvshapes.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 "splineorder2.h"
34 #include "splineutil.h"
35 #include "splineutil2.h"
36
37 #include <math.h>
38
39 static struct shapedescrip {
40 BasePoint me, prevcp, nextcp; int nocp;
41 } ellipse3[] = {
42 { { -1, 0 }, { -1, -0.552 }, { -1, 0.552 }, false },
43 { { 0, 1 }, { -0.552, 1 }, { 0.552, 1 }, false },
44 { { 1, 0 }, { 1, 0.552 }, { 1, -0.552 }, false },
45 { { 0, -1 }, { 0.552, -1 }, { -0.552, -1 }, false },
46 { { 0, 0 }, { 0, 0 }, { 0, 0 }, 0 }
47 };
48
49 static SplinePoint *SPMake(BasePoint *base,int pt) {
50 SplinePoint *new;
51
52 new = SplinePointCreate(base->x,base->y);
53 new->pointtype = pt;
54 new->selected = true;
55 return( new );
56 }
57
58 static SplinePoint *SPMakeTo(BasePoint *base,int pt, SplinePoint *from) {
59 SplinePoint *to = SPMake(base,pt);
60 SplineMake(from,to,false); /* Always make a cubic, then convert to quadratic when done */
61 return( to );
62 }
63
64 void CVMouseDownShape(CharView *cv,GEvent *event) {
65 real radius = CVRoundRectRadius(); int points = CVPolyStarPoints();
66 SplinePoint *last;
67 int i;
68 struct shapedescrip *ellipse;
69
70 if ( event->u.mouse.clicks==2 &&
71 (cv->active_tool == cvt_rect || cv->active_tool == cvt_elipse)) {
72 CVRectEllipsePosDlg(cv);
73 return;
74 }
75
76 CVClearSel(cv);
77 CVPreserveState(&cv->b);
78 CVSetCharChanged(cv,true);
79 cv->active_shape = chunkalloc(sizeof(SplineSet));
80 cv->active_shape->next = cv->b.layerheads[cv->b.drawmode]->splines;
81 cv->b.layerheads[cv->b.drawmode]->splines = cv->active_shape;
82 cv->active_shape->first = last = SPMake(&cv->info,pt_corner);
83
84 /* always make a cubic shape. Then convert to order2 when done */
85 /* the conversion routine already knows about rounding to int */
86 switch ( cv->active_tool ) {
87 case cvt_rect:
88 if ( radius==0 ) {
89 last = SPMakeTo(&cv->info,pt_corner,last);
90 last = SPMakeTo(&cv->info,pt_corner,last);
91 last = SPMakeTo(&cv->info,pt_corner,last);
92 } else {
93 last->pointtype = pt_tangent;
94 last = SPMakeTo(&cv->info,pt_tangent,last);
95 last = SPMakeTo(&cv->info,pt_tangent,last);
96 last = SPMakeTo(&cv->info,pt_tangent,last);
97 last = SPMakeTo(&cv->info,pt_tangent,last);
98 last = SPMakeTo(&cv->info,pt_tangent,last);
99 last = SPMakeTo(&cv->info,pt_tangent,last);
100 last = SPMakeTo(&cv->info,pt_tangent,last);
101 }
102 break;
103 case cvt_elipse:
104 ellipse = /*order2 ? ellipse2 :*/ ellipse3;
105 last->pointtype = pt_curve;
106 for ( i=1; ellipse[i].me.x!=0 || ellipse[i].me.y!=0 ; ++i ) {
107 last = SPMakeTo(&cv->info,pt_curve,last);
108 }
109 break;
110 case cvt_poly:
111 for ( i=1; i<points; ++i )
112 last = SPMakeTo(&cv->info,pt_corner,last);
113 break;
114 case cvt_star:
115 for ( i=1; i<2*points; ++i )
116 last = SPMakeTo(&cv->info,pt_corner,last);
117 break;
118 }
119 SplineMake(last,cv->active_shape->first,false);
120 cv->active_shape->last = cv->active_shape->first;
121 cv->b.sc->suspendMetricsViewEventPropagation = 1;
122 SCUpdateAll(cv->b.sc);
123 }
124
125 static void SetCorner(SplinePoint *sp,real x, real y) {
126 sp->me.x = x; sp->me.y = y;
127 sp->nextcp = sp->me;
128 sp->prevcp = sp->me;
129 }
130
131 static void SetCurve(SplinePoint *sp,BasePoint *center,real xrad, real yrad,
132 struct shapedescrip *pt_d) {
133 sp->me.x = center->x+xrad*pt_d->me.x; sp->me.y = center->y+yrad*pt_d->me.y;
134 sp->nextcp.x = center->x+xrad*pt_d->nextcp.x; sp->nextcp.y = center->y+yrad*pt_d->nextcp.y;
135 sp->prevcp.x = center->x+xrad*pt_d->prevcp.x; sp->prevcp.y = center->y+yrad*pt_d->prevcp.y;
136 }
137
138 static void SetPTangent(SplinePoint *sp,real x, real y,real xrad, real yrad) {
139 sp->me.x = x; sp->me.y = y;
140 sp->nextcp = sp->me;
141 sp->prevcp = sp->me;
142 if ( !sp->next->order2 ) {
143 xrad *= .552;
144 yrad *= .552;
145 }
146 sp->prevcp.x += xrad;
147 sp->prevcp.y += yrad;
148 }
149
150 static void SetNTangent(SplinePoint *sp,real x, real y,real xrad, real yrad) {
151 sp->me.x = x; sp->me.y = y;
152 sp->nextcp = sp->me;
153 if ( !sp->next->order2 ) {
154 xrad *= .552;
155 yrad *= .552;
156 }
157 sp->nextcp.x += xrad;
158 sp->nextcp.y += yrad;
159 sp->prevcp = sp->me;
160 }
161
162 static void RedoActiveSplineSet(SplineSet *ss) {
163 Spline *spline, *first;
164
165 first = NULL;
166 for ( spline = ss->first->next; spline!=NULL && spline!=first; spline = spline->to->next ) {
167 SplineRefigure(spline);
168 if ( first == NULL ) first = spline;
169 }
170 }
171
172 void CVMouseMoveShape(CharView *cv) {
173 real radius = CVRoundRectRadius(); int points = CVPolyStarPoints();
174 int center_out = CVRectElipseCenter();
175 real xrad,yrad, xrr, yrr;
176 real r2;
177 SplinePoint *sp;
178 real base, off;
179 int i;
180 struct shapedescrip *ellipse;
181 BasePoint center;
182
183 if ( cv->active_shape==NULL )
184 return;
185 switch ( cv->active_tool ) {
186 case cvt_rect:
187 if ( radius==0 ) {
188 sp = cv->active_shape->first->next->to;
189 if ( !center_out ) {
190 SetCorner(sp,cv->p.cx,cv->info.y);
191 SetCorner(sp=sp->next->to, cv->info.x,cv->info.y);
192 SetCorner(sp=sp->next->to, cv->info.x,cv->p.cy);
193 } else {
194 if (( xrad = (cv->p.cx-cv->info.x) )<0 ) xrad = -xrad;
195 if (( yrad = (cv->p.cy-cv->info.y) )<0 ) yrad = -yrad;
196 SetCorner(sp,cv->p.cx-xrad,cv->p.cy+yrad);
197 SetCorner(sp=sp->next->to, cv->p.cx+xrad,cv->p.cy+yrad);
198 SetCorner(sp=sp->next->to, cv->p.cx+xrad,cv->p.cy-yrad);
199 SetCorner(sp=sp->next->to, cv->p.cx-xrad,cv->p.cy-yrad);
200 }
201 } else {
202 if ( !center_out ) {
203 if (( xrad = (cv->p.cx-cv->info.x)/2 )<0 ) xrad = -xrad;
204 if (( yrad = (cv->p.cy-cv->info.y)/2 )<0 ) yrad = -yrad;
205 if ( xrad>radius ) xrr = radius; else xrr = xrad;
206 if ( yrad>radius ) yrr = radius; else yrr = yrad;
207 if ( cv->info.x<cv->p.cx ) xrr = -xrr;
208 if ( cv->info.y<cv->p.cy ) yrr = -yrr;
209 sp = cv->active_shape->first;
210 SetPTangent(sp,
211 cv->p.cx+xrr,cv->p.cy, -xrr,0);
212 SetNTangent(sp=sp->next->to,
213 cv->info.x-xrr,cv->p.cy, xrr,0);
214 SetPTangent(sp=sp->next->to,
215 cv->info.x,cv->p.cy+yrr, 0,-yrr);
216 SetNTangent(sp=sp->next->to,
217 cv->info.x,cv->info.y-yrr, 0,yrr);
218 SetPTangent(sp=sp->next->to,
219 cv->info.x-xrr,cv->info.y, xrr,0);
220 SetNTangent(sp=sp->next->to,
221 cv->p.cx+xrr,cv->info.y, -xrr,0);
222 SetPTangent(sp=sp->next->to,
223 cv->p.cx,cv->info.y-yrr, 0,yrr);
224 SetNTangent(sp=sp->next->to,
225 cv->p.cx,cv->p.cy+yrr, 0,-yrr);
226 } else {
227 if (( xrad = (cv->p.cx-cv->info.x) )<0 ) xrad = -xrad;
228 if (( yrad = (cv->p.cy-cv->info.y) )<0 ) yrad = -yrad;
229 if ( xrad>radius ) xrr = radius; else xrr = xrad;
230 if ( yrad>radius ) yrr = radius; else yrr = yrad;
231 sp = cv->active_shape->first;
232 SetPTangent(sp,
233 cv->p.cx-xrad+xrr,cv->p.cy-yrad, -xrr,0);
234 SetNTangent(sp=sp->next->to,
235 cv->p.cx+xrad-xrr,cv->p.cy-yrad, xrr,0);
236 SetPTangent(sp=sp->next->to,
237 cv->p.cx+xrad,cv->p.cy-yrad+yrr, 0,-yrr);
238 SetNTangent(sp=sp->next->to,
239 cv->p.cx+xrad,cv->p.cy+yrad-yrr, 0,yrr);
240 SetPTangent(sp=sp->next->to,
241 cv->p.cx+xrad-xrr,cv->p.cy+yrad, xrr,0);
242 SetNTangent(sp=sp->next->to,
243 cv->p.cx-xrad+xrr,cv->p.cy+yrad, -xrr,0);
244 SetPTangent(sp=sp->next->to,
245 cv->p.cx-xrad,cv->p.cy+yrad-yrr, 0,yrr);
246 SetNTangent(sp=sp->next->to,
247 cv->p.cx-xrad,cv->p.cy-yrad+yrr, 0,-yrr);
248 }
249 }
250 break;
251 case cvt_elipse:
252 if ( !center_out ) {
253 center.x = (cv->p.cx+cv->info.x)/2; center.y = (cv->p.cy+cv->info.y)/2;
254 if (( xrad = (cv->p.cx-cv->info.x)/2 )<0 ) xrad = -xrad;
255 if (( yrad = (cv->p.cy-cv->info.y)/2 )<0 ) yrad = -yrad;
256 } else {
257 center.x = cv->p.cx; center.y = cv->p.cy;
258 if (( xrad = (cv->p.cx-cv->info.x) )<0 ) xrad = -xrad;
259 if (( yrad = (cv->p.cy-cv->info.y) )<0 ) yrad = -yrad;
260 }
261 if ( cv->b.layerheads[cv->b.drawmode]->order2 ) {
262 xrad = rint(xrad);
263 yrad = rint(yrad);
264 }
265 ellipse = /*cv->b.sc->parent->order2 ? ellipse2 :*/ ellipse3;
266 for ( i=0, sp=cv->active_shape->first; ellipse[i].me.x!=0 || ellipse[i].me.y!=0 ; ++i, sp=sp->next->to )
267 SetCurve(sp,&center,xrad,yrad,&ellipse[i]);
268 break;
269 case cvt_poly:
270 base = atan2(cv->p.cy-cv->info.y,cv->p.cx-cv->info.x);
271 radius = sqrt((cv->p.cy-cv->info.y)*(cv->p.cy-cv->info.y) +
272 (cv->p.cx-cv->info.x)*(cv->p.cx-cv->info.x));
273 off = -2*FF_PI/points;
274 sp = cv->active_shape->last->prev->from;
275 for ( i=0; i<points; ++i )
276 SetCorner(sp=sp->next->to, cv->p.cx+radius*cos(base+i*off),
277 cv->p.cy+radius*sin(base+i*off));
278 break;
279 case cvt_star:
280 base = atan2(cv->p.cy-cv->info.y,cv->p.cx-cv->info.x)-FF_PI;
281 if ( base<(-FF_PI-0.00001) )
282 base += 2*FF_PI;
283 radius = sqrt((cv->p.cy-cv->info.y)*(cv->p.cy-cv->info.y) +
284 (cv->p.cx-cv->info.x)*(cv->p.cx-cv->info.x));
285 off = -2*FF_PI/(2*points);
286 sp = cv->active_shape->last->prev->from;
287 r2 = radius/CVStarRatio();
288 for ( i=0; i<2*points; ++i ) {
289 if ( i&1 ) {
290 SetCorner(sp=sp->next->to, cv->p.cx+r2*cos(base+i*off),
291 cv->p.cy+r2*sin(base+i*off));
292 } else {
293 SetCorner(sp=sp->next->to, cv->p.cx+radius*cos(base+i*off),
294 cv->p.cy+radius*sin(base+i*off));
295 }
296 }
297 break;
298 }
299 RedoActiveSplineSet(cv->active_shape);
300 cv->b.sc->suspendMetricsViewEventPropagation = 1;
301 SCUpdateAll(cv->b.sc);
302 }
303
304 void CVMouseUpShape(CharView *cv) {
305 SplinePoint *first, *second, *sp;
306 extern int snaptoint;
307
308 if ( cv->active_shape==NULL )
309 return;
310
311 if ( cv->b.layerheads[cv->b.drawmode]->order2 ) {
312 SplineSet *prev, *new, *ss;
313 new = SplineSetsTTFApprox(cv->active_shape);
314 for ( ss=cv->b.layerheads[cv->b.drawmode]->splines, prev=NULL;
315 ss!=NULL && ss!=cv->active_shape;
316 prev = ss, ss=ss->next );
317 if ( ss==NULL )
318 IError("Couldn't find shape");
319 else {
320 if ( prev==NULL )
321 cv->b.layerheads[cv->b.drawmode]->splines = new;
322 else
323 prev->next = new;
324 SplinePointListsFree(cv->active_shape);
325 cv->active_shape = new;
326 }
327 }
328 first = cv->active_shape->first; second = first->next->to;
329 if ( first->me.x == second->me.x && first->me.y == second->me.y ) {
330 /* Remove this shape, it will be selected */
331 cv->b.layerheads[cv->b.drawmode]->splines = SplinePointListRemoveSelected(cv->b.sc,
332 cv->b.layerheads[cv->b.drawmode]->splines);
333 } else if ( cv->active_tool==cvt_rect || cv->active_tool==cvt_elipse ) {
334 if ( SplinePointListIsClockwise(cv->active_shape)==0 )
335 SplineSetReverse(cv->active_shape);
336 if ( snaptoint ) {
337 for ( sp= cv->active_shape->first; ; ) {
338 SplinePointRound(sp,1.0);
339 sp = sp->next->to;
340 if ( sp==cv->active_shape->first )
341 break;
342 }
343 for ( sp= cv->active_shape->first; ; ) {
344 SplineRefigure(sp->next);
345 sp = sp->next->to;
346 if ( sp==cv->active_shape->first )
347 break;
348 }
349 }
350 }
351 if ( cv->b.sc->inspiro && hasspiro()) {
352 free(cv->active_shape->spiros);
353 cv->active_shape->spiros = SplineSet2SpiroCP(cv->active_shape,&cv->active_shape->spiro_cnt);
354 cv->active_shape->spiro_max = cv->active_shape->spiro_cnt;
355 }
356 cv->active_shape = NULL;
357
358 cv->b.sc->suspendMetricsViewEventPropagation = 0;
359 SCUpdateAll(cv->b.sc);
360 }