| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 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) |
| 38 |
static void ProcessKnife(CharView *cv, PressedOn *p) { |
| 39 |
real dx, dy; |
| 40 |
SplinePoint *n; |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 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; |
| 51 |
|
| 52 |
if ( p->spline!=NULL ) |
| 53 |
p->sp = SplineBisect(p->spline,p->t); |
| 54 |
if ( p->spl==NULL ) |
| 55 |
return; |
| 56 |
if ( p->spl->first!=p->spl->last ) |
| 57 |
if ( p->sp==p->spl->first || p->sp==p->spl->last ) |
| 58 |
return; |
| 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 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 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 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 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 |
|
| 184 |
|
| 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 |
|
| 224 |
|
| 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 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 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 |
|