Debian Codegraph @

fontforgeexe/cvknife.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 "splineutil.h"
34
35 #include <math.h>
36
37 #if defined(KNIFE_CONTINUOUS) /* Use this code to do cuts as we move along. Probably a bad idea, let's wait till the end */
38 static void ProcessKnife(CharView *cv, PressedOn *p) {
39 real dx, dy;
40 SplinePoint *n;
41
42 /* If we've already made a cut, don't make another cut too close to it */
43 /* ie. if the hand shakes over a cut let's not get about six tiny cut */
44 /* segments adjacent to one another */
45 if ( (dx=cv->info.x-cv->lastknife.x)<0 ) dx=-dx;
46 if ( (dy=cv->info.y-cv->lastknife.y)<0 ) dy=-dy;
47 if ( (dx+dy)/cv->scale <= 6 )
48 return;
49 if ( p->sp==NULL && p->spline==NULL )
50 return; /* Nothing to cut */
51
52 if ( p->spline!=NULL )
53 p->sp = SplineBisect(p->spline,p->t);
54 if ( p->spl==NULL ) /* Kanou says this can happen. It doesn't hurt to check for it */
55 return;
56 if ( p->spl->first!=p->spl->last )
57 if ( p->sp==p->spl->first || p->sp==p->spl->last )
58 return; /* Already cut here */
59 n = chunkalloc(sizeof(SplinePoint));
60 p->sp->pointtype = pt_corner;
61 *n = *p->sp;
62 n->hintmask = NULL;
63 p->sp->next = NULL;
64 n->prev = NULL;
65 n->next->from = n;
66 if ( p->spl->first==p->spl->last ) {
67 p->spl->first = n;
68 p->spl->last = p->sp;
69 } else {
70 SplinePointList *nspl = chunkalloc(sizeof(SplinePointList));
71 nspl->next = p->spl->next;
72 p->spl->next = nspl;
73 nspl->first = n;
74 nspl->last = p->spl->last;
75 p->spl->last = p->sp;
76 }
77
78 cv->lastknife.x = cv->info.x;
79 cv->lastknife.y = cv->info.y;
80 CVSetCharChanged(cv,true);
81 SCUpdateAll(cv->b.sc);
82 }
83 #endif
84
85 void CVMouseDownKnife(CharView *cv) {
86 #if defined(KNIFE_CONTINUOUS)
87 CVPreserveState(&cv->b);
88 cv->lastknife.x = cv->lastknife.y = -9999;
89 ProcessKnife(cv,&cv->p);
90 #else
91 cv->p.rubberlining = true;
92 #endif
93 }
94
95 void CVMouseMoveKnife(CharView *cv, PressedOn *p) {
96 #if defined(KNIFE_CONTINUOUS)
97 ProcessKnife(cv,p);
98 #else
99 GDrawRequestExpose(cv->v,NULL,false);
100 #endif
101 }
102
103 #if !defined(KNIFE_CONTINUOUS)
104 static void ReorderSpirosAndAddAndCut(SplineSet *spl,int spiro_index) {
105 /* We just cut a closed contour. It is now open. */
106 /* If spl->spiros[spiro_index] == spl->first->me then they cut on top of */
107 /* a point which already existed */
108 /* If so, move that point to the head of the list */
109 /* copy it so that it's at the end of the list too */
110 /* mark the start as SPIRO_OPEN */
111 /* Otherwise they cut between spiro_cps */
112 /* Insert 2 copies of spl->first->me after spiro_index */
113 /* rotate so that one of these is at the end of the list and the other at the start */
114 /* mark start as SPIRO_OPEN */
115 spiro_cp *newspiros;
116
117 if ( spiro_index!=spl->spiro_cnt-1 &&
118 spl->first->me.x == spl->spiros[spiro_index].x &&
119 spl->first->me.y == spl->spiros[spiro_index].y ) {
120 newspiros = malloc((spl->spiro_cnt+1) * sizeof(spiro_cp));
121 memcpy(newspiros,spl->spiros+spiro_index,(spl->spiro_cnt-1-spiro_index)*sizeof(spiro_cp));
122 memcpy(newspiros+(spl->spiro_cnt-1-spiro_index),spl->spiros,spiro_index*sizeof(spiro_cp));
123 memcpy(newspiros+spl->spiro_cnt-1,newspiros,sizeof(spiro_cp));
124 memcpy(newspiros+spl->spiro_cnt,spl->spiros+spl->spiro_cnt-1,sizeof(spiro_cp));
125 newspiros[0].ty = SPIRO_OPEN_CONTOUR;
126 free(spl->spiros);
127 spl->spiros = newspiros;
128 ++(spl->spiro_cnt);
129 spl->spiro_max = spl->spiro_cnt;
130 } else {
131 newspiros = malloc((spl->spiro_cnt+2) * sizeof(spiro_cp));
132 newspiros[0].x = spl->first->me.x;
133 newspiros[0].y = spl->first->me.y;
134 newspiros[0].ty = SPIRO_OPEN_CONTOUR;
135 memcpy(newspiros+1,spl->spiros+spiro_index+1,(spl->spiro_cnt-1-(spiro_index+1))*sizeof(spiro_cp));
136 memcpy(newspiros+1+(spl->spiro_cnt-1-(spiro_index+1)),spl->spiros,(spiro_index+1)*sizeof(spiro_cp));
137 memcpy(newspiros+spl->spiro_cnt,newspiros,sizeof(spiro_cp));
138 memcpy(newspiros+spl->spiro_cnt+1,spl->spiros+spl->spiro_cnt-1,sizeof(spiro_cp));
139 newspiros[spl->spiro_cnt].ty = SPIRO_G4;
140 free(spl->spiros);
141 spl->spiros = newspiros;
142 spl->spiro_cnt += 2;
143 spl->spiro_max = spl->spiro_cnt;
144 }
145 }
146
147 static void SplitSpirosAndAddAndCut(SplineSet *spl,SplineSet *spl2,int spiro_index) {
148 /* OK, spl was an open contour and we just cut it at spiro_index */
149 /* We don't need to rotate the spiros */
150 /* The first half of spl remains in it. (We'll leave all spiros up to and including spiro_index) */
151 /* If spl->spiros[spiro_index] != spl->first->me we must add spl->first here */
152 /* In the spl2 we either start out with spl->spiros[spiro_index] or with spl->first */
153 /* then add all spiros after spiro_index */
154
155 spl2->spiros = malloc((spl->spiro_cnt-spiro_index+2) * sizeof(spiro_cp));
156 spl2->spiro_max = spl->spiro_cnt-spiro_index+2;
157 if ( spl2->first->me.x == spl->spiros[spiro_index].x &&
158 spl2->first->me.y == spl->spiros[spiro_index].y ) {
159 memcpy(spl2->spiros,spl->spiros+spiro_index,(spl->spiro_cnt-spiro_index)*sizeof(spiro_cp));
160 spl2->spiros[0].ty = SPIRO_OPEN_CONTOUR;
161 memcpy(spl->spiros+spiro_index+1,spl->spiros+spl->spiro_cnt-1,sizeof(spiro_cp));
162 spl2->spiro_cnt = spl->spiro_cnt-spiro_index;
163 spl->spiro_cnt = spiro_index+2;
164 } else {
165 spl2->spiros[0].x = spl2->first->me.x;
166 spl2->spiros[0].y = spl2->first->me.y;
167 spl2->spiros[0].ty = SPIRO_OPEN_CONTOUR;
168 memcpy(spl2->spiros+1,spl->spiros+spiro_index+1,(spl->spiro_cnt-(spiro_index+1))*sizeof(spiro_cp));
169 spl2->spiro_cnt = spl->spiro_cnt-spiro_index;
170 if ( spiro_index+3>spl->spiro_max )
171 spl->spiros = realloc(spl->spiros,(spl->spiro_max=spiro_index+3)*sizeof(spiro_cp));
172 memcpy(spl->spiros+spiro_index+1,spl2->spiros,sizeof(spiro_cp));
173 spl->spiros[spiro_index+1].ty = SPIRO_G4;
174 memcpy(spl->spiros+spiro_index+2,spl->spiros+spl->spiro_cnt-1,sizeof(spiro_cp));
175 spl->spiro_cnt = spiro_index+3;
176 }
177 }
178 #endif
179
180 void CVMouseUpKnife(CharView *cv, GEvent *event)
181 {
182 #if !defined(KNIFE_CONTINUOUS)
183 /* draw a line from (cv->p.cx,cv->p.cy) to (cv->info.x,cv->info.y) */
184 /* and cut anything intersected by it */
185 SplineSet *spl, *spl2;
186 Spline *s, *nexts;
187 Spline dummy;
188 SplinePoint dummyfrom, dummyto, *mid, *mid2;
189 BasePoint inters[9];
190 extended t1s[10], t2s[10];
191 int foundsomething = true, ever = false;
192 int i;
193 int spiro_index = 0;
194
195 memset(&dummy,0,sizeof(dummy));
196 memset(&dummyfrom,0,sizeof(dummyfrom));
197 memset(&dummyto,0,sizeof(dummyto));
198 dummyfrom.me.x = cv->p.cx; dummyfrom.me.y = cv->p.cy;
199 dummyto.me.x = cv->info.x; dummyto.me.y = cv->info.y;
200 dummyfrom.nextcp = dummyfrom.prevcp = dummyfrom.me;
201 dummyto.nextcp = dummyto.prevcp = dummyto.me;
202 dummyfrom.nonextcp = dummyfrom.noprevcp = dummyto.nonextcp = dummyto.noprevcp = true;
203 dummy.splines[0].d = cv->p.cx; dummy.splines[0].c = cv->info.x-cv->p.cx;
204 dummy.splines[1].d = cv->p.cy; dummy.splines[1].c = cv->info.y-cv->p.cy;
205 dummy.from = &dummyfrom; dummy.to = &dummyto;
206 dummy.islinear = dummy.knownlinear = true;
207 dummyfrom.next = dummyto.prev = &dummy;
208
209 for ( spl = cv->b.layerheads[cv->b.drawmode]->splines; spl!=NULL ; spl = spl->next )
210 spl->ticked = false;
211
212 while ( foundsomething ) {
213 foundsomething = false;
214 for ( spl = cv->b.layerheads[cv->b.drawmode]->splines; spl!=NULL && !foundsomething; spl = spl->next ) {
215 for ( s = spl->first->next; s!=NULL ; ) {
216 nexts = NULL;
217 if ( s->to!=spl->first )
218 nexts = s->to->next;
219 if ( SplinesIntersect(s,&dummy,inters,t1s,t2s)>0 ) {
220 if ( event->u.mouse.state&ksm_meta ) {
221 for ( i=0; i<4 && t1s[i]!=-1 && (t1s[i]<.0001 || t1s[i]>1-.0001); ++i );
222 if ( i<4 && t1s[i]!=-1 ) {
223 /* With meta down we just remove the spline rather than */
224 /* cutting it */
225 foundsomething = true;
226 nexts = NULL;
227 if ( !ever )
228 CVPreserveState(&cv->b);
229 ever = true;
230 if ( spl->first==spl->last ) {
231 spl->first = s->to;
232 spl->last = s->from;
233 } else {
234 spl2 = chunkalloc(sizeof(SplineSet));
235 spl2->next = spl->next;
236 spl->next = spl2;
237 spl2->first = s->to;
238 spl2->last = spl->last;
239 spl->last = s->from;
240 }
241 s->to->prev = s->from->next = NULL;
242 SplineFree(s);
243 SplineSetSpirosClear(spl);
244 }
245 } else {
246 for ( i=0; i<4 && t1s[i]!=-1 &&
247 (( t1s[i]<.001 && s->from->prev==NULL ) ||
248 ( t1s[i]>1-.001 && s->to->next == NULL ));
249 ++i );
250 if ( i<4 && t1s[i]!=-1 ) {
251 foundsomething = true;
252 nexts = NULL;
253 if ( !ever )
254 CVPreserveState(&cv->b);
255 ever = true;
256 spiro_index = -1;
257 if ( cv->b.sc->inspiro && hasspiro()) {
258 if ( spl->spiro_cnt!=0 )
259 spiro_index = SplineT2SpiroIndex(s,t1s[i],spl);
260 } else
261 SplineSetSpirosClear(spl);
262 if ( t1s[i]<.001 ) {
263 mid = s->from;
264 } else if ( t1s[i]>1-.001 ) {
265 mid = s->to;
266 } else
267 mid = SplineBisect(s,t1s[i]);
268 /* if the intersection is close to an end point */
269 /* cut at the end point, else break in the middle */
270 /* Cut here, and then */
271 /* start all over again (we may need to alter the */
272 /* splineset structure so drastically that we just */
273 /* can't continue these loops) */
274 mid->pointtype = pt_corner;
275 mid2 = chunkalloc(sizeof(SplinePoint));
276 *mid2 = *mid;
277 mid2->hintmask = NULL;
278 mid->next = NULL;
279 mid2->prev = NULL;
280 mid2->next->from = mid2;
281 spl->ticked = true;
282 if ( spl->first==spl->last ) {
283 spl->first = mid2;
284 spl->last = mid;
285 if ( spiro_index!=-1 )
286 ReorderSpirosAndAddAndCut(spl,spiro_index);
287 } else {
288 spl2 = chunkalloc(sizeof(SplineSet));
289 spl2->next = spl->next;
290 spl->next = spl2;
291 spl2->first = mid2;
292 spl2->last = spl->last;
293 spl->last = mid;
294 if ( spiro_index!=-1 )
295 SplitSpirosAndAddAndCut(spl,spl2,spiro_index);
296 spl2->ticked = true;
297 }
298 }
299 }
300 }
301 s = nexts;
302 }
303 }
304 }
305 if ( ever ) {
306 for ( spl = cv->b.layerheads[cv->b.drawmode]->splines; spl!=NULL ; spl = spl->next ) {
307 if ( spl->ticked && spl->spiros!=NULL && cv->b.sc->inspiro && hasspiro())
308 SSRegenerateFromSpiros(spl);
309 spl->ticked = false;
310 }
311 CVCharChangedUpdate( &cv->b );
312 } else {
313 GDrawRequestExpose(cv->v, NULL, false);
314 }
315 #endif
316 }
317