Debian Codegraph @

fontforgeexe/tilepath.c

raw symbols SCIP
1 /* Copyright (C) 2002-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 "fvfonts.h"
33 #include "gkeysym.h"
34 #include "gresedit.h"
35 #include "splinefit.h"
36 #include "splinefont.h"
37 #include "splineutil.h"
38 #include "splineutil2.h"
39
40 #include <math.h>
41
42 #ifdef FONTFORGE_CONFIG_TILEPATH
43 /* Given a path and a splineset */
44 /* Treat the splineset as a tile and lay it down on the path until we reach the*/
45 /* end of the path */
46 /* More precisely, find the length of the path */
47 /* Find the height of the tile */
48 /* We'll need length/height tiles */
49 /* For each tile */
50 /* For a point on the central (in x) axis of the tile */
51 /* Use its y-position to figure out how far along the path we are ( y-pos/length ) */
52 /* Then this point should be moved to exactly that point */
53 /* For a point off the central axis */
54 /* Perform the above calculation, and */
55 /* Find the normal vector to the path */
56 /* Our new location should be: */
57 /* the location found above + our xoffset * (normal vector) */
58 /* Do that for a lot of points on each spline of the tile and then */
59 /* use approximate spline from points to find the new splines */
60 /* Complications: */
61 /* There may not be an integral number of tiles, so we must be prepared to truncate some splines */
62
63 GResFont tilepath_font = GRESFONT_INIT("400 12pt " SANS_UI_FAMILIES);
64 GResFont tilepath_boldfont = GRESFONT_INIT("700 12pt " SANS_UI_FAMILIES);
65 typedef struct tiledata {
66 SplineSet *basetile; /* Moved so that ymin==0, and x is adjusted */
67 /* about the x-axis as implied by tilepos */
68 SplineSet *firsttile;
69 SplineSet *lasttile;
70 SplineSet *isolatedtile;
71 SplineSet *tileset; /* As many copies of the basetile as we are */
72 /* going to need. Each successive one bb.maxy */
73 /* higher than the last */
74 SplineSet *result; /* Final result after transformation */
75 DBounds bb, fbb, lbb, ibb; /* Of the basetile, first & last tiles */
76 uint8_t include_white, finclude_white, linclude_white, iinclude_white;
77 bigreal xscale[3];
78
79 SplineSet *path;
80 bigreal plength; /* Length of path */
81 int pcnt; /* Number of splines in path */
82 int nsamples;
83 struct tdsample {
84 real dx, dy; /* offset from path->first->me */
85 real c,s; /* cos/sin of normal vector pointing right of path */
86 } *samples; /* an array of [nsamples+1] actually */
87 int njoins;
88 struct jsample {
89 real dx, dy;
90 real c1,s1;
91 real c2,s2;
92 real sofar;
93 } *joins; /* an array of [pcnt or pcnt-1], one of each join */
94
95 enum tilepos { tp_left, tp_center, tp_right } tilepos;
96 enum tilescale { ts_tile, ts_tilescale, ts_scale } tilescale;
97 /* ts_scale means that we scale the one tile until it height is the same */
98 /* as plength */
99 /* ts_tile means that we lay down as many tiles as we need so that */
100 /* n*tile-height == plength. Note: n need not be an integer, so we */
101 /* may be an incomplete tile => incomplete splines (a spline may even */
102 /* get cut so that it becomes two splines) */
103 /* ts_tilescale means that we find n = floor(plength/tile-height) and */
104 /* scale = plength/(n*tile-height). We scale the tile by "scale", and */
105 /* then lay down n of them */
106
107 int doallpaths;
108
109 BasePoint patternSize; /* Last 3 fields used only for patterns */
110 IPoint repeatCnt; /* And not for tilepath */
111 Layer *pattern;
112 } TD;
113 enum whitespace_type { ws_include=0x1, ws_but_not_first=0x2 };
114
115 static int TDMakeSamples(TD *td) {
116 Spline *spline, *first;
117 bigreal len, slen, sofar, t, toff, dt_per_sample;
118 int i,end, base, pcnt;
119 bigreal sx, sy, angle;
120
121 first = NULL; len = 0; pcnt = 0;
122 for ( spline=td->path->first->next; spline!=NULL && spline!=first; spline=spline->to->next ) {
123 if ( first==NULL ) first = spline;
124 len += SplineLength(spline);
125 ++pcnt;
126 }
127 if ( len==0 )
128 return( false );
129 td->plength = len;
130 td->pcnt = pcnt;
131
132 td->nsamples = ceil(len)+10;
133 td->samples = malloc((td->nsamples+1)*sizeof(struct tdsample));
134 td->joins = malloc(td->pcnt*sizeof(struct jsample));
135
136 i = 0; pcnt = 0;
137 first = NULL; sofar = 0;
138 for ( spline=td->path->first->next; spline!=NULL && spline!=first; spline=spline->to->next ) {
139 if ( first==NULL ) first = spline;
140 slen = SplineLength(spline);
141 /* I'm assuming that length is approximately linear in t */
142 toff = (i - td->nsamples*sofar/len)/slen;
143 base = i;
144 end = floor(td->nsamples*(sofar+slen)/len);
145 dt_per_sample = end==i?1:(1.0-toff)/(end-i);
146 if ( spline->to->next==NULL || spline->to->next==first )
147 end = td->nsamples;
148 while ( i<=end ) {
149 t = toff + (i-base)*dt_per_sample;
150 if ( i==td->nsamples || t>1 ) t = 1;
151 td->samples[i].dx = ((spline->splines[0].a*t+spline->splines[0].b)*t+spline->splines[0].c)*t + spline->splines[0].d /*-
152 td->path->first->me.x*/;
153 td->samples[i].dy = ((spline->splines[1].a*t+spline->splines[1].b)*t+spline->splines[1].c)*t + spline->splines[1].d /*-
154 td->path->first->me.y*/;
155 sx = (3*spline->splines[0].a*t+2*spline->splines[0].b)*t+spline->splines[0].c;
156 sy = (3*spline->splines[1].a*t+2*spline->splines[1].b)*t+spline->splines[1].c;
157 if ( sx==0 && sy==0 ) {
158 sx = spline->to->me.x - spline->from->me.x;
159 sy = spline->to->me.y - spline->from->me.y;
160 }
161 angle = atan2(sy,sx) - FF_PI/2;
162 td->samples[i].c = cos(angle);
163 td->samples[i].s = sin(angle);
164 if ( td->samples[i].s>-.00001 && td->samples[i].s<.00001 ) { td->samples[i].s=0; td->samples[i].c = ( td->samples[i].c>0 )? 1 : -1; }
165 if ( td->samples[i].c>-.00001 && td->samples[i].c<.00001 ) { td->samples[i].c=0; td->samples[i].s = ( td->samples[i].s>0 )? 1 : -1; }
166 ++i;
167 }
168 sofar += slen;
169 if (( pcnt<td->pcnt-1 || td->path->first==td->path->last ) &&
170 spline->to->next!=NULL &&
171 !((spline->to->pointtype==pt_curve && !spline->to->nonextcp && !spline->to->noprevcp) ||
172 (spline->to->pointtype==pt_hvcurve && !spline->to->nonextcp && !spline->to->noprevcp) ||
173 (spline->to->pointtype==pt_tangent && spline->to->nonextcp+spline->to->noprevcp==1 )) ) {
174 /* We aren't interested in joins where the two splines are tangent */
175 Spline *next = spline->to->next;
176 td->joins[pcnt].sofar = sofar;
177 td->joins[pcnt].dx = spline->to->me.x;
178 td->joins[pcnt].dy = spline->to->me.y;
179 /* there are two normals at a join, one for each spline */
180 /* it should bisect the normal vectors of the two splines */
181 sx = next->splines[0].c; sy = next->splines[1].c;
182 angle = atan2(sy,sx) - FF_PI/2;
183 td->joins[pcnt].c1 = cos(angle);
184 td->joins[pcnt].s1 = sin(angle);
185 if ( td->joins[pcnt].s1>-.00001 && td->joins[pcnt].s1<.00001 ) { td->joins[pcnt].s1=0; td->joins[pcnt].c1 = ( td->joins[pcnt].c1>0 )? 1 : -1; }
186 if ( td->joins[pcnt].c1>-.00001 && td->joins[pcnt].c1<.00001 ) { td->joins[pcnt].c1=0; td->joins[pcnt].s1 = ( td->joins[pcnt].s1>0 )? 1 : -1; }
187
188 sx = (3*spline->splines[0].a+2*spline->splines[0].b)+spline->splines[0].c;
189 sy = (3*spline->splines[1].a+2*spline->splines[1].b)+spline->splines[1].c;
190 angle = atan2(sy,sx) - FF_PI/2;
191 td->joins[pcnt].c2 = cos(angle);
192 td->joins[pcnt].s2 = sin(angle);
193 if ( td->joins[pcnt].s2>-.00001 && td->joins[pcnt].s2<.00001 ) { td->joins[pcnt].s2=0; td->joins[pcnt].c2 = ( td->joins[pcnt].c2>0 )? 1 : -1; }
194 if ( td->joins[pcnt].c2>-.00001 && td->joins[pcnt].c2<.00001 ) { td->joins[pcnt].c2=0; td->joins[pcnt].s2 = ( td->joins[pcnt].s2>0 )? 1 : -1; }
195 ++pcnt;
196 }
197 }
198 td->njoins = pcnt;
199 if ( i!=td->nsamples+1 )
200 IError("Sample failure %d is not %d", i, td->samples+1 );
201 return( true );
202 }
203
204 static void TDAddPoints(TD *td) {
205 /* Insert additional points in the tileset roughly at the locations */
206 /* corresponding to the ends of the splines in the path */
207 SplineSet *spl;
208 Spline *spline, *first, *tsp;
209 bigreal len;
210 bigreal ts[3];
211
212 first = NULL; len = 0;
213 for ( spline=td->path->first->next; spline!=NULL && spline!=first; spline=spline->to->next ) {
214 if ( first==NULL ) first = spline;
215 if ( spline->to->next==NULL || spline->to->next==first )
216 break;
217 len += SplineLength(spline);
218
219 for ( spl=td->tileset; spl!=NULL; spl=spl->next ) {
220 for ( tsp=spl->first->next; tsp!=NULL ; tsp = tsp->to->next ) {
221 if ( RealApprox(tsp->to->me.y,len) || RealApprox(tsp->from->me.y,len))
222 /* Do Nothing, already broken here */;
223 else if ( (tsp->to->me.y>len || tsp->to->prevcp.y>len || tsp->from->me.y>len || tsp->from->nextcp.y>len) &&
224 (tsp->to->me.y<len || tsp->to->prevcp.y<len || tsp->from->me.y<len || tsp->from->nextcp.y<len) &&
225 CubicSolve(&tsp->splines[1],len,ts) ) {
226 SplinePoint *mid = SplineBisect(tsp,ts[0]);
227 tsp = mid->next;
228 }
229 if ( tsp->to == spl->first )
230 break;
231 }
232 }
233 }
234 }
235
236 static void SplineSplitAtY(Spline *spline,real y) {
237 bigreal ts[3];
238 SplinePoint *last;
239
240 if ( spline->from->me.y<=y && spline->from->nextcp.y<=y &&
241 spline->to->me.y<=y && spline->to->prevcp.y<=y )
242 return;
243 if ( spline->from->me.y>=y && spline->from->nextcp.y>=y &&
244 spline->to->me.y>=y && spline->to->prevcp.y>=y )
245 return;
246
247
248 if ( !CubicSolve(&spline->splines[1],y,ts) )
249 return;
250
251 last = spline->to;
252 spline = SplineSplit(spline,ts);
253 while ( spline->to!=last ) {
254 if ( spline->to->me.y!=y ) {
255 real diff = y-spline->to->me.y;
256 spline->to->me.y = y;
257 spline->to->prevcp.y += diff;
258 spline->to->nextcp.y += diff;
259 SplineRefigure(spline); SplineRefigure(spline->to->next);
260 }
261 spline = spline->to->next;
262 }
263 }
264
265 static void _SplinesRemoveBetween( Spline *spline, Spline *beyond, SplineSet *spl ) {
266 Spline *next;
267
268 while ( spline!=NULL && spline!=beyond ) {
269 next = spline->to->next;
270 if ( spline->from!=spl->last && spline->from!=spl->first )
271 SplinePointFree(spline->from);
272 SplineFree(spline);
273 spline = next;
274 }
275 }
276
277 static SplineSet *SplinePointListTruncateAtY(SplineSet *spl,real y) {
278 SplineSet *prev=NULL, *ss=spl, *nprev, *snext, *ns;
279 Spline *spline, *next;
280
281 for ( ; spl!=NULL; spl = spl->next ) {
282 for ( spline=spl->first->next ; spline!=NULL; spline = next ) {
283 next = spline->to->next;
284 SplineSplitAtY(spline,y);
285 if ( next==NULL || next->from == spl->last )
286 break;
287 }
288 }
289
290 prev = NULL;
291 y += 1/128.0; /* Small fudge factor for rounding errors */
292 for ( spl=ss; spl!=NULL; spl = snext ) {
293 snext = spl->next;
294 nprev = spl;
295 for ( spline=spl->first->next ; spline!=NULL ; spline = next ) {
296 next = spline->to->next;
297 if ( spline->from->me.y<=y && spline->from->nextcp.y<=y &&
298 spline->to->me.y<=y && spline->to->prevcp.y<=y ) {
299 if ( spline->to==spl->first )
300 break;
301 else
302 continue;
303 }
304 /* Remove this spline */
305 while ( next!=NULL && next->from!=spl->first &&
306 (next->from->me.y>y || next->from->nextcp.y>y ||
307 next->to->me.y>y || next->to->prevcp.y>y) )
308 next = next->to->next;
309 if ( next==NULL || next->from==spl->first ) {
310 /* The area to be removed continues to the end of splineset */
311 if ( spline==spl->first->next ) {
312 /* Remove entire splineset */
313 if ( prev==NULL )
314 ss = snext;
315 else
316 prev->next = snext;
317 SplinePointListFree(spl);
318 nprev = prev;
319 break;
320 }
321 spl->last = spline->from;
322 spline->from->next = NULL;
323 spl->first->prev = NULL;
324 _SplinesRemoveBetween(spline,next,spl);
325 break;
326 } else {
327 if ( spline==spl->first->next ) {
328 /* Remove everything before next */
329 next->from->prev = NULL;
330 spl->first->next = NULL;
331 spl->first = next->from;
332 } else if ( spl->first==spl->last ) {
333 /* rotate splineset so break is at start and end. */
334 spl->last = spline->from;
335 spl->first = next->from;
336 next->from->prev = NULL;
337 spline->from->next = NULL;
338 } else {
339 /* Split into two splinesets and remove all between */
340 ns = chunkalloc(sizeof(SplineSet));
341 ns->first = next->from;
342 ns->last = spl->last;
343 spl->last = spline->from;
344 spline->from->next = NULL;
345 spl->first->prev = NULL;
346 next->from->prev = NULL;
347 ns->last->next = NULL;
348 ns->next = spl->next;
349 spl->next = ns->next;
350 nprev = ns;
351 }
352 _SplinesRemoveBetween(spline,next,spl);
353 spl = nprev;
354 }
355 }
356 prev = nprev;
357 }
358 return( ss );
359 }
360
361 static SplineSet *SplinePointListMerge(SplineSet *old,SplineSet *new) {
362 /* Merge the new splineset into the old looking for any endpoints */
363 /* common to both, and if any are found, merging them */
364 SplineSet *test1, *next;
365 SplineSet *oldold = old;
366
367 while ( new!=NULL ) {
368 next = new->next;
369 if ( new->first!=new->last ) {
370 for ( test1=oldold; test1!=NULL; test1=test1->next ) {
371 if ( test1->first!=test1->last &&
372 ((test1->first->me.x==new->first->me.x && test1->first->me.y==new->first->me.y) ||
373 (test1->last->me.x==new->first->me.x && test1->last->me.y==new->first->me.y) ||
374 (test1->first->me.x==new->last->me.x && test1->first->me.y==new->last->me.y) ||
375 (test1->last->me.x==new->last->me.x && test1->last->me.y==new->last->me.y)) )
376 break;
377 }
378 if ( test1!=NULL ) {
379 if ((test1->first->me.x==new->first->me.x && test1->first->me.y==new->first->me.y) ||
380 (test1->last->me.x==new->last->me.x && test1->last->me.y==new->last->me.y))
381 SplineSetReverse(new);
382 if ( test1->last->me.x==new->first->me.x && test1->last->me.y==new->first->me.y ) {
383 test1->last->nextcp = new->first->nextcp;
384 test1->last->nonextcp = new->first->nonextcp;
385 test1->last->nextcpdef = new->first->nextcpdef;
386 test1->last->next = new->first->next;
387 new->first->next->from = test1->last;
388 test1->last = new->last;
389 SplinePointFree(new->first);
390 new->first = new->last = NULL;
391 SplinePointListFree(new);
392 if ( test1->last->me.x == test1->first->me.x &&
393 test1->last->me.y == test1->first->me.y ) {
394 test1->first->prevcp = test1->last->prevcp;
395 test1->first->noprevcp = test1->last->noprevcp;
396 test1->first->prevcpdef = test1->last->prevcpdef;
397 test1->last->prev->to = test1->first;
398 SplinePointFree(test1->last);
399 test1->last = test1->first;
400 }
401 } else {
402 test1->first->prevcp = new->last->prevcp;
403 test1->first->noprevcp = new->last->noprevcp;
404 test1->first->prevcpdef = new->last->prevcpdef;
405 test1->first->prev = new->last->prev;
406 new->last->prev->to = test1->first;
407 test1->first = new->first;
408 SplinePointFree(new->last);
409 new->first = new->last = NULL;
410 SplinePointListFree(new);
411 }
412 new = next;
413 continue;
414 }
415 }
416 new->next = old;
417 old = new;
418 new = next;
419 }
420 return( old );
421 }
422
423 #define Round_Up_At .5
424 static void TileLine(TD *td) {
425 int tilecnt=1, i;
426 bigreal scale=1, y;
427 real trans[6];
428 SplineSet *new;
429 int use_first=false, use_last=false, use_isolated=false;
430
431 switch ( td->tilescale ) {
432 case ts_tile:
433 if ( td->path->first->prev!=NULL ) /* Closed contours have no ends => all tiles intermediate */
434 tilecnt = ceil( td->plength/td->bb.maxy );
435 else if ( td->plength<=td->ibb.maxy ) {
436 tilecnt = 1;
437 use_isolated = true;
438 } else if ( td->plength<=td->fbb.maxy ) {
439 tilecnt = 1;
440 use_first = true;
441 } else if ( td->plength<=td->fbb.maxy+td->lbb.maxy && td->firsttile!=NULL && td->lasttile!=NULL ) {
442 tilecnt = 2;
443 use_first = use_last = true;
444 } else {
445 use_first = (td->firsttile!=NULL);
446 use_last = (td->lasttile!=NULL);
447 tilecnt = use_first+use_last+ceil( (td->plength-td->fbb.maxy-td->lbb.maxy)/td->bb.maxy );
448 }
449 break;
450 case ts_scale:
451 if ( td->isolatedtile!=NULL ) {
452 use_isolated = true;
453 scale = td->plength/td->ibb.maxy;
454 } else
455 scale = td->plength/td->bb.maxy;
456 break;
457 case ts_tilescale:
458 tilecnt = -1;
459 if ( td->path->first->prev!=NULL ) /* Closed contours have no ends => all tiles intermediate */
460 scale = td->plength/td->bb.maxy;
461 else if ( td->isolatedtile!=NULL &&
462 (( td->firsttile!=NULL && td->lasttile!=NULL && td->plength<td->fbb.maxy+Round_Up_At*td->lbb.maxy) ||
463 ( td->firsttile!=NULL && td->lasttile==NULL && td->plength<td->fbb.maxy+Round_Up_At*td->bb.maxy) ||
464 ( td->firsttile==NULL && td->lasttile==NULL && td->plength<(1+Round_Up_At)*td->bb.maxy)) ) {
465 use_isolated = true;
466 scale = td->plength/td->ibb.maxy;
467 tilecnt = 1;
468 } else if ( td->firsttile!=NULL && td->lasttile!=NULL ) {
469 if ( td->plength<td->fbb.maxy+Round_Up_At*td->lbb.maxy ) {
470 use_first = true;
471 tilecnt = 1;
472 scale = td->plength/td->fbb.maxy;
473 } else if ( td->plength<td->fbb.maxy+td->lbb.maxy+Round_Up_At*td->bb.maxy ) {
474 use_first = use_last = true;
475 tilecnt = 2;
476 scale = 2*td->plength/(td->fbb.maxy+td->lbb.maxy);
477 } else {
478 use_first = use_last = true;
479 scale = 2 + (td->plength-td->fbb.maxy-td->lbb.maxy)/td->bb.maxy;
480 }
481 } else if ( td->firsttile!=NULL ) {
482 if ( td->plength<td->fbb.maxy+Round_Up_At*td->bb.maxy ) {
483 use_first = true;
484 tilecnt = 1;
485 scale = td->plength/td->fbb.maxy;
486 } else {
487 use_first = true;
488 scale = 1 + (td->plength-td->fbb.maxy)/td->bb.maxy;
489 }
490 } else if ( td->lasttile!=NULL ) {
491 if ( td->plength<td->lbb.maxy+Round_Up_At*td->bb.maxy ) {
492 use_last = true;
493 tilecnt = 1;
494 scale = td->plength/td->lbb.maxy;
495 } else {
496 use_last = true;
497 scale = 1 + (td->plength-td->lbb.maxy)/td->bb.maxy;
498 }
499 } else
500 scale = td->plength/td->bb.maxy;
501 if ( tilecnt == -1 ) {
502 tilecnt = floor( scale );
503 if ( tilecnt==0 )
504 tilecnt = 1;
505 else if ( scale-tilecnt>Round_Up_At )
506 ++tilecnt;
507 scale = td->plength/(use_first*td->fbb.maxy + use_last*td->lbb.maxy +
508 (tilecnt-use_first-use_last)*td->bb.maxy);
509 }
510 break;
511 }
512
513 trans[0] = 1; trans[3] = scale; /* Only scale y */
514 trans[1] = trans[2] = trans[4] = trans[5] = 0;
515 y = 0;
516 for ( i=0; i<tilecnt; ++i ) {
517 int which = (i==0 && use_first) ? 1 :
518 (i==0 && use_isolated ) ? 3 :
519 (i==tilecnt-1 && use_last ) ? 2 :
520 0;
521 new = SplinePointListCopy((&td->basetile)[which]);
522 trans[5] = y;
523 new = SplinePointListTransform(new,trans,tpt_AllPoints);
524 if ( i==tilecnt-1 && td->tilescale==ts_tile )
525 new = SplinePointListTruncateAtY(new,td->plength);
526 td->tileset = SplinePointListMerge(td->tileset,new);
527 y += (&td->bb)[which].maxy*scale;
528 }
529 if ( td->pcnt>1 ) {
530 /* If there are fewer tiles than there are spline elements, then we */
531 /* may not be able to do a good job approximating (suppose the path */
532 /* draws a circle, but there is just one tile, a straight line. */
533 /* without some extra points in the middle of that line there is no */
534 /* way to make a circle). So here we add some extra breaks */
535 /* Actually, it's worse than that. If the transition isn't smooth */
536 /* then we'll always want those extra points... */
537 TDAddPoints(td);
538 }
539 }
540
541 static void AdjustPoint(TD *td,Spline *spline,bigreal t, FitPoint *to) {
542 bigreal x, y;
543 bigreal pos;
544 int low;
545 bigreal dx, dy, c, s;
546 int i;
547
548 to->t = t;
549
550 x = ((spline->splines[0].a*t+spline->splines[0].b)*t+spline->splines[0].c)*t + spline->splines[0].d;
551 y = ((spline->splines[1].a*t+spline->splines[1].b)*t+spline->splines[1].c)*t + spline->splines[1].d;
552
553 for ( i=td->pcnt-2; i>=0; --i )
554 if ( RealNearish(y,td->joins[i].sofar) )
555 break;
556 if ( i>=0 ) {
557 bigreal x1,y1, x2, y2, dx1, dx2, dy1, dy2;
558 x1 = td->joins[i].dx + td->joins[i].c1*x;
559 y1 = td->joins[i].dy + td->joins[i].s1*x;
560 dx1 = -td->joins[i].s1;
561 dy1 = td->joins[i].c1;
562
563 x2 = td->joins[i].dx + td->joins[i].c2*x;
564 y2 = td->joins[i].dy + td->joins[i].s2*x;
565 dx2 = -td->joins[i].s2;
566 dy2 = td->joins[i].c2;
567 /* there are two lines at a join and I need to find the intersection */
568 if ( dy2>-.00001 && dy2<.00001 ) {
569 to->p.y = y2;
570 if ( dy1>-.00001 && dy1<.00001 ) /* essentially parallel */
571 to->p.x = x2;
572 else
573 to->p.x = x1 + dx1*(y2-y1)/dy1;
574 } else {
575 bigreal s=(dy1*dx2/dy2-dx1);
576 if ( s>-.00001 && s<.00001 ) { /* essentially parallel */
577 to->p.x = x1; to->p.y = y1;
578 } else {
579 bigreal t1 = (x1-x2- dx2/dy2*(y1-y2))/s;
580 to->p.x = x1 + dx1*t1;
581 to->p.y = y1 + dy1*t1;
582 }
583 }
584 } else {
585 pos = y/td->plength;
586 if ( pos<0 ) pos=0; /* should not happen */
587 if ( pos>1 ) pos = 1;
588
589 pos *= td->nsamples;
590 low = floor(pos);
591 pos -= low;
592
593 if ( pos==0 || low==td->nsamples ) {
594 dx = td->samples[low].dx;
595 dy = td->samples[low].dy;
596 c = td->samples[low].c;
597 s = td->samples[low].s;
598 } else {
599 dx = (td->samples[low].dx*(1-pos) + td->samples[low+1].dx*pos);
600 dy = (td->samples[low].dy*(1-pos) + td->samples[low+1].dy*pos);
601 c = (td->samples[low].c*(1-pos) + td->samples[low+1].c*pos);
602 s = (td->samples[low].s*(1-pos) + td->samples[low+1].s*pos);
603 }
604
605 to->p.x = dx + c*x;
606 to->p.y = dy + s*x;
607 }
608 }
609
610 static SplinePoint *TDMakePoint(TD *td,Spline *old,real t) {
611 FitPoint fp;
612 SplinePoint *new;
613
614 AdjustPoint(td,old,t,&fp);
615 return SplinePointCreate(fp.p.x, fp.p.y);
616 }
617
618 static Spline *AdjustSpline(TD *td,Spline *old,SplinePoint *newfrom,SplinePoint *newto,
619 int order2) {
620 FitPoint fps[15];
621 int i;
622 bigreal t;
623
624 if ( newfrom==NULL )
625 newfrom = TDMakePoint(td,old,0);
626 if ( newto==NULL )
627 newto = TDMakePoint(td,old,1);
628 for ( i=1, t=1/16.0; i<16; ++i, t+= 1/16.0 )
629 AdjustPoint(td,old,t,&fps[i-1]);
630 return( ApproximateSplineFromPoints(newfrom,newto,fps,15, order2) );
631 }
632
633 static void AdjustSplineSet(TD *td,int order2) {
634 SplineSet *spl, *last=NULL, *new;
635 Spline *spline, *s;
636 SplinePoint *lastsp, *nextsp, *sp;
637
638 if ( td->result!=NULL )
639 for ( last=td->result ; last->next!=NULL; last = last->next );
640
641 for ( spl=td->tileset; spl!=NULL; spl=spl->next ) {
642 new = chunkalloc(sizeof(SplineSet));
643 if ( last==NULL )
644 td->result = new;
645 else
646 last->next = new;
647 last = new;
648 new->first = lastsp = TDMakePoint(td,spl->first->next,0);
649 nextsp = NULL;
650 for ( spline=spl->first->next; spline!=NULL; spline=spline->to->next ) {
651 if ( spline->to==spl->first )
652 nextsp = new->first;
653 s = AdjustSpline(td,spline,lastsp,nextsp,order2);
654 lastsp = s->to;
655 if ( nextsp!=NULL )
656 break;
657 }
658 if ( lastsp!=new->first &&
659 RealNearish(lastsp->me.x,new->first->me.x) &&
660 RealNearish(lastsp->me.y,new->first->me.y) ) {
661 new->first->prev = lastsp->prev;
662 new->first->prevcp = lastsp->prevcp;
663 new->first->prevcpdef = lastsp->prevcpdef;
664 lastsp->prev->to = new->first;
665 new->last = new->first;
666 SplineRefigure(new->first->prev);
667 SplinePointFree(lastsp);
668 } else
669 new->last = lastsp;
670
671 for ( sp = new->first; sp!=NULL; ) {
672 SplinePointCategorize(sp);
673 if ( sp->next==NULL )
674 break;
675 sp = sp->next->to;
676 if ( sp==new->first )
677 break;
678 }
679 }
680 }
681
682 static void TileSplineSets(TD *td,SplineSet **head,int order2) {
683 SplineSet *prev=NULL, *spl, *next;
684
685 for ( spl = *head; spl!=NULL; spl = next ) {
686 next = spl->next;
687 if ( td->doallpaths || PointListIsSelected(spl)) {
688 if ( prev==NULL )
689 *head = next;
690 else
691 prev->next = next;
692 td->path = spl;
693 if ( TDMakeSamples(td)) {
694 TileLine(td);
695 AdjustSplineSet(td,order2);
696 free( td->samples );
697 free( td->joins );
698 SplinePointListsFree(td->tileset);
699 }
700 SplinePointListFree(td->path);
701 td->path = td->tileset = NULL;
702 } else
703 prev = spl;
704 }
705 SPLCategorizePoints(td->result);
706 if ( *head==NULL )
707 *head = td->result;
708 else {
709 for ( spl= *head; spl->next!=NULL; spl = spl->next );
710 spl->next = td->result;
711 }
712 }
713
714 static void TileIt(SplineSet **head,struct tiledata *td,
715 int doall,int order2) {
716 real trans[6];
717 int i;
718 SplineSet *thistile;
719
720 td->doallpaths = doall;
721
722 trans[0] = trans[3] = 1;
723 trans[1] = trans[2] = 0;
724 for ( i=0; i<4; ++i ) if ( (thistile = (&td->basetile)[i])!=NULL ) {
725 DBounds *bb = &(&td->bb)[i];
726 SplineSetFindBounds(thistile,bb);
727 trans[5] = (&td->include_white)[i]&ws_include ? 0 : -bb->miny;
728 trans[4] = -bb->minx;
729 if ( td->tilepos==tp_center )
730 trans[4] -= (bb->maxx-bb->minx)/2;
731 else if ( td->tilepos==tp_left )
732 trans[4] = -bb->maxx;
733 if ( trans[4]!=0 || trans[5]!=0 )
734 SplinePointListTransform(thistile,trans,tpt_AllPoints);
735 SplineSetFindBounds(thistile,bb);
736 }
737 td->tileset = td->result = NULL;
738
739 TileSplineSets(td,head,order2);
740 }
741
742 static enum tilepos tilepos=tp_center;
743 static enum tilescale tilescale=ts_tilescale;
744 static int include_whitespace[4] = {0,0,0,0};
745 static SplineSet *last_tiles[4];
746
747 #define CID_Center 1001
748 #define CID_Left 1002
749 #define CID_Right 1003
750 #define CID_Tile 1011
751 #define CID_TileScale 1012
752 #define CID_Scale 1013
753 #define CID_IncludeWhiteSpaceBelowTile 1021 /* +[0...3] */
754 #define CID_FirstTile 1025 /* +[0...3] for the other tiles */
755
756 static void TPDSubResize(TilePathDlg *tpd, GEvent *event) {
757 int width, height;
758 int i;
759
760 if ( !event->u.resize.sized )
761 return;
762
763 width = event->u.resize.size.width;
764 height = event->u.resize.size.height;
765 if ( width!=tpd->cv_width || height!=tpd->cv_height ) {
766 tpd->cv_width = width; tpd->cv_height = height;
767 for ( i=0; i<4; ++i ) {
768 CharView *cv = (&tpd->cv_first)+i;
769 GDrawResize(cv->gw,width,height);
770 }
771 }
772
773 GDrawSync(NULL);
774 GDrawProcessPendingEvents(NULL);
775 }
776
777 static char *tilenames[] = { N_("First"), N_("Medial"), N_("Final"), N_("Isolated") };
778 static void TPDDraw(TilePathDlg *tpd, GWindow pixmap, GEvent *event) {
779 GRect r,pos;
780 int i;
781
782 Color fg = GDrawGetDefaultForeground(NULL);
783 GDrawSetLineWidth(pixmap,0);
784 for ( i=0; i<4; ++i ) {
785 CharView *cv = (&tpd->cv_first)+i;
786
787 GGadgetGetSize(GWidgetGetControl(tpd->gw,CID_FirstTile+i),&pos);
788 r.x = pos.x; r.y = pos.y-1;
789 r.width = pos.width+1; r.height = pos.height+1;
790 GDrawDrawRect(pixmap,&r,fg);
791
792 GDrawSetFont(pixmap,cv->inactive ? tpd->plain : tpd->bold);
793 GDrawDrawText8(pixmap,r.x,pos.y-2-tpd->fh+tpd->as,_(tilenames[i]),-1,fg);
794 }
795 }
796
797 static void TPDMakeActive(TilePathDlg *tpd,CharView *cv) {
798 int i;
799
800 if ( tpd==NULL )
801 return;
802 for ( i=0; i<4; ++i )
803 (&tpd->cv_first)[i].inactive = true;
804 cv->inactive = false;
805 GDrawSetUserData(tpd->gw,cv);
806 for ( i=0; i<4; ++i )
807 GDrawRequestExpose((&tpd->cv_first)[i].v,NULL,false);
808 GDrawRequestExpose(tpd->gw,NULL,false);
809 }
810
811 static void TPDChar(TilePathDlg *tpd, GEvent *event) {
812 int i;
813 for ( i=0; i<4; ++i )
814 if ( !(&tpd->cv_first)[i].inactive )
815 break;
816
817 if ( event->u.chr.keysym==GK_Tab || event->u.chr.keysym==GK_BackTab ) {
818 if ( event->u.chr.keysym==GK_Tab ) ++i; else --i;
819 if ( i<0 ) i=3; else if ( i>3 ) i = 0;
820 TPDMakeActive(tpd,(&tpd->cv_first)+i);
821 } else
822 CVChar((&tpd->cv_first)+i,event);
823 }
824
825 static void TPD_DoClose(struct cvcontainer *cvc) {
826 TilePathDlg *tpd = (TilePathDlg *) cvc;
827 int i;
828
829 for ( i=0; i<4; ++i ) {
830 SplineChar *msc = &(&tpd->sc_first)[i];
831 SplinePointListsFree(msc->layers[0].splines);
832 SplinePointListsFree(msc->layers[1].splines);
833 free( msc->layers );
834 }
835
836 tpd->done = true;
837 }
838
839 static int tpd_sub_e_h(GWindow gw, GEvent *event) {
840 TilePathDlg *tpd;
841
842 if ( event->type==et_destroy )
843 return( true );
844
845 tpd = (TilePathDlg *) ((CharViewBase *) GDrawGetUserData(gw))->container;
846
847 switch ( event->type ) {
848 case et_resize:
849 if ( event->u.resize.sized )
850 TPDSubResize(tpd,event);
851 break;
852 case et_char:
853 TPDChar(tpd,event);
854 break;
855 }
856 return( true );
857 }
858
859 static int tpd_e_h(GWindow gw, GEvent *event) {
860 TilePathDlg *tpd;
861 int i;
862
863 if ( event->type == et_destroy )
864 return( true );
865
866 tpd = (TilePathDlg *) ((CharViewBase *) GDrawGetUserData(gw))->container;
867 switch ( event->type ) {
868 case et_expose:
869 TPDDraw(tpd, gw, event);
870 break;
871 case et_char:
872 TPDChar(tpd,event);
873 break;
874 case et_close:
875 TPD_DoClose((struct cvcontainer *) tpd);
876 break;
877 case et_create:
878 break;
879 case et_map:
880 for ( i=0; i<4; ++i ) {
881 CharView *cv = (&tpd->cv_first)+i;
882 if ( !cv->inactive ) {
883 if ( event->u.map.is_visible )
884 CVPaletteActivate(cv);
885 else
886 CVPalettesHideIfMine(cv);
887 break;
888 }
889 }
890 /* tpd->isvisible = event->u.map.is_visible; */
891 break;
892 }
893 return( true );
894 }
895
896 static int TilePathD_Cancel(GGadget *g, GEvent *e) {
897 if ( e->type==et_controlevent && e->u.control.subtype == et_buttonactivate ) {
898 TilePathDlg *tpd = (TilePathDlg *) (((CharViewBase *) GDrawGetUserData(GGadgetGetWindow(g)))->container);
899 TPD_DoClose(&tpd->base);
900 }
901 return( true );
902 }
903
904 static int TPD_Useless(SplineSet *ss) {
905 DBounds bb;
906
907 if ( ss==NULL )
908 return( true );
909 SplineSetFindBounds(ss,&bb);
910 return( bb.maxy==bb.miny );
911 }
912
913 static int TilePathD_OK(GGadget *g, GEvent *e) {
914 if ( e->type==et_controlevent && e->u.control.subtype == et_buttonactivate ) {
915 TilePathDlg *tpd = (TilePathDlg *) (((CharViewBase *) GDrawGetUserData(GGadgetGetWindow(g)))->container);
916 struct tiledata *td = tpd->td;
917
918 if ( GGadgetIsChecked(GWidgetGetControl(tpd->gw,CID_Center)) )
919 td->tilepos = tp_center;
920 else if ( GGadgetIsChecked(GWidgetGetControl(tpd->gw,CID_Left)) )
921 td->tilepos = tp_left;
922 else
923 td->tilepos = tp_right;
924 if ( GGadgetIsChecked(GWidgetGetControl(tpd->gw,CID_Tile)) )
925 td->tilescale = ts_tile;
926 else if ( GGadgetIsChecked(GWidgetGetControl(tpd->gw,CID_TileScale)) )
927 td->tilescale = ts_tilescale;
928 else
929 td->tilescale = ts_scale;
930 if ( TPD_Useless(tpd->sc_medial.layers[ly_fore].splines) &&
931 (td->tilescale!=ts_scale ||
932 TPD_Useless(tpd->sc_isolated.layers[ly_fore].splines)) ) {
933 if ( td->tilescale == ts_scale )
934 ff_post_error(_("Bad Tile"),_("You must specify an isolated (or medial) tile"));
935 else
936 ff_post_error(_("Bad Tile"),_("You must specify a medial tile"));
937 return( true );
938 }
939
940 tilepos = td->tilepos;
941 tilescale = td->tilescale;
942
943 td->firsttile = tpd->sc_first.layers[ly_fore].splines;
944 tpd->sc_first.layers[ly_fore].splines = NULL;
945 include_whitespace[0] = td->finclude_white = GGadgetIsChecked(GWidgetGetControl(tpd->gw,CID_IncludeWhiteSpaceBelowTile+0))?ws_include:0;
946 td->basetile = tpd->sc_medial.layers[ly_fore].splines;
947 tpd->sc_medial.layers[ly_fore].splines = NULL;
948 include_whitespace[1] = td->include_white = GGadgetIsChecked(GWidgetGetControl(tpd->gw,CID_IncludeWhiteSpaceBelowTile+1))?ws_include:0;
949 td->lasttile = tpd->sc_final.layers[ly_fore].splines;
950 tpd->sc_final.layers[ly_fore].splines = NULL;
951 include_whitespace[2] = td->linclude_white = GGadgetIsChecked(GWidgetGetControl(tpd->gw,CID_IncludeWhiteSpaceBelowTile+2))?ws_include:0;
952 td->isolatedtile = tpd->sc_isolated.layers[ly_fore].splines;
953 tpd->sc_isolated.layers[ly_fore].splines = NULL;
954 include_whitespace[3] = td->iinclude_white = GGadgetIsChecked(GWidgetGetControl(tpd->gw,CID_IncludeWhiteSpaceBelowTile+3))?ws_include:0;
955
956 TPD_DoClose(&tpd->base);
957 tpd->oked = true;
958 }
959 return( true );
960 }
961
962 static int TPD_Can_Navigate(struct cvcontainer *cvc, enum nav_type type) {
963 return( false );
964 }
965
966 static int TPD_Can_Open(struct cvcontainer *cvc) {
967 return( false );
968 }
969
970 static SplineFont *SF_Of_TPD(struct cvcontainer *foo) {
971 return( NULL );
972 }
973
974 struct cvcontainer_funcs tilepath_funcs = {
975 cvc_tilepath,
976 (void (*) (struct cvcontainer *cvc,CharViewBase *cv)) TPDMakeActive,
977 (void (*) (struct cvcontainer *cvc,void *)) TPDChar,
978 TPD_Can_Navigate,
979 NULL,
980 TPD_Can_Open,
981 TPD_DoClose,
982 SF_Of_TPD
983 };
984
985
986 static void TPDInit(TilePathDlg *tpd,SplineFont *sf) {
987 int i;
988
989 memset(tpd,0,sizeof(*tpd));
990 tpd->base.funcs = &tilepath_funcs;
991 tpd->base_sf = sf;
992
993 for ( i=0; i<4; ++i ) {
994 SplineChar *msc = &(&tpd->sc_first)[i];
995 CharView *mcv = &(&tpd->cv_first)[i];
996 msc->orig_pos = i;
997 msc->unicodeenc = -1;
998 msc->name = i==0 ? "First" :
999 i==1 ? "Medial" :
1000 i==2 ? "Last":
1001 "Isolated";
1002 msc->parent = &tpd->dummy_sf;
1003 msc->layer_cnt = 2;
1004 msc->layers = calloc(2,sizeof(Layer));
1005 LayerDefault(&msc->layers[0]);
1006 LayerDefault(&msc->layers[1]);
1007 tpd->chars[i] = msc;
1008
1009 mcv->b.sc = msc;
1010 mcv->b.layerheads[dm_fore] = &msc->layers[ly_fore];
1011 mcv->b.layerheads[dm_back] = &msc->layers[ly_back];
1012 mcv->b.layerheads[dm_grid] = &tpd->dummy_sf.grid;
1013 msc->layers[ly_fore].splines = SplinePointListCopy(last_tiles[i]);
1014 mcv->b.drawmode = dm_fore;
1015 mcv->b.container = (struct cvcontainer *) tpd;
1016 mcv->inactive = i!=0;
1017 }
1018 tpd->dummy_sf.glyphs = tpd->chars;
1019 tpd->dummy_sf.glyphcnt = tpd->dummy_sf.glyphmax = 4;
1020 tpd->dummy_sf.pfminfo.fstype = -1;
1021 tpd->dummy_sf.pfminfo.stylemap = -1;
1022 tpd->dummy_sf.fontname = tpd->dummy_sf.fullname = tpd->dummy_sf.familyname = "dummy";
1023 tpd->dummy_sf.weight = "Medium";
1024 tpd->dummy_sf.origname = "dummy";
1025 tpd->dummy_sf.ascent = sf->ascent;
1026 tpd->dummy_sf.descent = sf->descent;
1027 tpd->dummy_sf.layers = tpd->layerinfo;
1028 tpd->dummy_sf.layer_cnt = 2;
1029 tpd->layerinfo[ly_back].order2 = sf->layers[ly_back].order2;
1030 tpd->layerinfo[ly_back].name = _("Back");
1031 tpd->layerinfo[ly_fore].order2 = sf->layers[ly_fore].order2;
1032 tpd->layerinfo[ly_fore].name = _("Fore");
1033 tpd->dummy_sf.grid.order2 = sf->grid.order2;
1034 tpd->dummy_sf.anchor = NULL;
1035
1036 tpd->dummy_sf.fv = (FontViewBase *) &tpd->dummy_fv;
1037 tpd->dummy_fv.b.active_layer = ly_fore;
1038 tpd->dummy_fv.b.sf = &tpd->dummy_sf;
1039 tpd->dummy_fv.b.selected = tpd->sel;
1040 tpd->dummy_fv.cbw = tpd->dummy_fv.cbh = default_fv_font_size+1;
1041 tpd->dummy_fv.magnify = 1;
1042
1043 tpd->dummy_fv.b.map = &tpd->dummy_map;
1044 tpd->dummy_map.map = tpd->map;
1045 tpd->dummy_map.backmap = tpd->backmap;
1046 tpd->dummy_map.enccount = tpd->dummy_map.encmax = tpd->dummy_map.backmax = 4;
1047 tpd->dummy_map.enc = &custom;
1048 }
1049
1050 static int TileAsk(struct tiledata *td,SplineFont *sf) {
1051 TilePathDlg tpd;
1052 GRect pos;
1053 GWindow gw;
1054 GWindowAttrs wattrs;
1055 GGadgetCreateData gcd[24], boxes[5], *harray[8], *varray[5],
1056 *rhvarray[4][5], *chvarray[4][5];
1057 GTextInfo label[24];
1058 int as, ds, ld;
1059 int i,k;
1060
1061 TPDInit( &tpd,sf );
1062 tpd.td = td;
1063 memset(td,0,sizeof(*td));
1064
1065 memset(&wattrs,0,sizeof(wattrs));
1066 wattrs.mask = wam_events|wam_cursor|wam_isdlg|wam_restrict|wam_undercursor|wam_utf8_wtitle;
1067 wattrs.is_dlg = true;
1068 wattrs.restrict_input_to_me = 1;
1069 wattrs.undercursor = 1;
1070 wattrs.event_masks = -1;
1071 wattrs.cursor = ct_pointer;
1072 wattrs.utf8_window_title = _("Tile Path");
1073 pos.width = 600;
1074 pos.height = 300;
1075 tpd.gw = gw = GDrawCreateTopWindow(NULL,&pos,tpd_e_h,&tpd.cv_first,&wattrs);
1076
1077 tpd.plain = tilepath_font.fi;
1078 tpd.bold = tilepath_boldfont.fi;
1079 GDrawWindowFontMetrics(tpd.gw,tpd.plain,&as,&ds,&ld);
1080 tpd.fh = as+ds; tpd.as = as;
1081
1082 memset(&label,0,sizeof(label));
1083 memset(&gcd,0,sizeof(gcd));
1084 memset(&boxes,0,sizeof(boxes));
1085
1086 k = 0;
1087 gcd[k].gd.flags = gg_visible|gg_enabled ; /* This space is for the menubar */
1088 gcd[k].gd.pos.height = 18; gcd[k].gd.pos.width = 20;
1089 gcd[k++].creator = GSpacerCreate;
1090
1091 for ( i=0; i<4; ++i ) {
1092 gcd[k].gd.pos.height = tpd.fh;
1093 gcd[k].gd.flags = gg_visible | gg_enabled;
1094 gcd[k++].creator = GSpacerCreate;
1095 chvarray[0][i] = &gcd[k-1];
1096
1097 gcd[k].gd.pos.width = gcd[k].gd.pos.height = 200;
1098 gcd[k].gd.flags = gg_visible | gg_enabled;
1099 gcd[k].gd.cid = CID_FirstTile+i;
1100 gcd[k].gd.u.drawable_e_h = tpd_sub_e_h;
1101 gcd[k++].creator = GDrawableCreate;
1102 chvarray[1][i] = &gcd[k-1];
1103
1104 gcd[k].gd.pos.x = gcd[0].gd.pos.x; gcd[k].gd.pos.y = gcd[6].gd.pos.y+24;
1105 gcd[k].gd.flags = gg_visible | gg_enabled;
1106 if ( include_whitespace[i] ) gcd[k].gd.flags |= gg_cb_on;
1107 label[k].text = (unichar_t *) _("Include Whitespace below Tile");
1108 label[k].text_is_1byte = true;
1109 label[k].text_in_resource = true;
1110 gcd[k].gd.label = &label[k];
1111 gcd[k].gd.popup_msg = _("Normally the Tile will consist of everything\nwithin the minimum bounding box of the tile --\nso adjacent tiles will abut directly on one\nanother. If you wish whitespace between tiles\nset this flag");
1112 gcd[k].gd.cid = CID_IncludeWhiteSpaceBelowTile+i;
1113 gcd[k++].creator = GCheckBoxCreate;
1114 chvarray[2][i] = &gcd[k-1];
1115 }
1116 chvarray[0][4] = chvarray[1][4] = chvarray[2][4] = chvarray[3][0] = NULL;
1117
1118 gcd[k].gd.pos.x = 6; gcd[k].gd.pos.y = 6;
1119 gcd[k].gd.flags = gg_visible | gg_enabled;
1120 gcd[k].gd.mnemonic = 'L';
1121 label[k].text = (unichar_t *) _("_Left");
1122 label[k].text_is_1byte = true;
1123 label[k].text_in_resource = true;
1124 gcd[k].gd.label = &label[k];
1125 gcd[k].gd.popup_msg = _("The tiles should be placed to the left of the path\nas the path is traced from its start point to its end");
1126 gcd[k].gd.cid = CID_Left;
1127 gcd[k++].creator = GRadioCreate;
1128 rhvarray[0][0] = &gcd[k-1];
1129
1130 gcd[k].gd.pos.x = 60; gcd[k].gd.pos.y = gcd[0].gd.pos.y;
1131 gcd[k].gd.flags = gg_visible | gg_enabled;
1132 gcd[k].gd.mnemonic = 'C';
1133 label[k].text = (unichar_t *) _("C_enter");
1134 label[k].text_is_1byte = true;
1135 label[k].text_in_resource = true;
1136 gcd[k].gd.label = &label[k];
1137 gcd[k].gd.popup_msg = _("The tiles should be centered on the path");
1138 gcd[k].gd.cid = CID_Center;
1139 gcd[k++].creator = GRadioCreate;
1140 rhvarray[0][1] = &gcd[k-1];
1141
1142 gcd[k].gd.pos.x = 140; gcd[k].gd.pos.y = gcd[1].gd.pos.y;
1143 gcd[k].gd.flags = gg_visible | gg_enabled;
1144 gcd[k].gd.mnemonic = 'R';
1145 label[k].text = (unichar_t *) _("_Right");
1146 label[k].text_is_1byte = true;
1147 label[k].text_in_resource = true;
1148 gcd[k].gd.label = &label[k];
1149 gcd[k].gd.popup_msg = _("The tiles should be placed to the right of the path\nas the path is traced from its start point to its end");
1150 gcd[k].gd.cid = CID_Right;
1151 gcd[k++].creator = GRadioCreate;
1152 rhvarray[0][2] = &gcd[k-1];
1153 rhvarray[0][3] = GCD_Glue; rhvarray[0][4] = NULL;
1154
1155 gcd[k].gd.pos.x = 5; gcd[k].gd.pos.y = GDrawPointsToPixels(NULL,gcd[2].gd.pos.y+20);
1156 gcd[k].gd.pos.width = pos.width-10;
1157 gcd[k].gd.flags = gg_visible | gg_enabled | gg_pos_in_pixels ;
1158 gcd[k++].creator = GLineCreate;
1159 rhvarray[1][0] = &gcd[k-1];
1160 rhvarray[1][1] = rhvarray[1][2] = GCD_ColSpan;
1161 rhvarray[1][3] = GCD_Glue; rhvarray[1][4] = NULL;
1162
1163 gcd[k].gd.pos.x = gcd[0].gd.pos.x; gcd[k].gd.pos.y = gcd[2].gd.pos.y+24;
1164 gcd[k].gd.flags = gg_visible | gg_enabled;
1165 gcd[k].gd.mnemonic = 'T';
1166 label[k].text = (unichar_t *) _("_Tile");
1167 label[k].text_is_1byte = true;
1168 label[k].text_in_resource = true;
1169 gcd[k].gd.label = &label[k];
1170 gcd[k].gd.popup_msg = _("Multiple copies of the selection should be tiled onto the path");
1171 gcd[k].gd.cid = CID_Tile;
1172 gcd[k++].creator = GRadioCreate;
1173 rhvarray[2][0] = &gcd[k-1];
1174
1175 gcd[k].gd.pos.x = gcd[1].gd.pos.x; gcd[k].gd.pos.y = gcd[4].gd.pos.y;
1176 gcd[k].gd.flags = gg_visible | gg_enabled;
1177 gcd[k].gd.mnemonic = 'a';
1178 label[k].text = (unichar_t *) _("Sc_ale & Tile");
1179 label[k].text_is_1byte = true;
1180 label[k].text_in_resource = true;
1181 gcd[k].gd.label = &label[k];
1182 gcd[k].gd.popup_msg = _("An integral number of the selection will be used to cover the path.\nIf the path length is not evenly divisible by the selection's\nheight, then the selection should be scaled slightly.");
1183 gcd[k].gd.cid = CID_TileScale;
1184 gcd[k++].creator = GRadioCreate;
1185 rhvarray[2][1] = &gcd[k-1];
1186
1187 gcd[k].gd.pos.x = gcd[2].gd.pos.x; gcd[k].gd.pos.y = gcd[5].gd.pos.y;
1188 gcd[k].gd.flags = gg_visible | gg_enabled;
1189 gcd[k].gd.mnemonic = 'S';
1190 label[k].text = (unichar_t *) _("_Scale");
1191 label[k].text_is_1byte = true;
1192 label[k].text_in_resource = true;
1193 gcd[k].gd.label = &label[k];
1194 gcd[k].gd.popup_msg = _("The selection should be scaled so that it will cover the path's length");
1195 gcd[k].gd.cid = CID_Scale;
1196 gcd[k++].creator = GRadioCreate;
1197 rhvarray[2][2] = &gcd[k-1];
1198 rhvarray[2][3] = GCD_Glue; rhvarray[2][4] = NULL;
1199 rhvarray[3][0] = NULL;
1200
1201 gcd[k-7+tilepos].gd.flags |= gg_cb_on;
1202 gcd[k-3+tilescale].gd.flags |= gg_cb_on;
1203
1204 label[k].text = (unichar_t *) _("_OK");
1205 label[k].text_is_1byte = true;
1206 label[k].text_in_resource = true;
1207 gcd[k].gd.label = &label[k];
1208 gcd[k].gd.flags = gg_enabled|gg_visible|gg_but_default;
1209 gcd[k].gd.handle_controlevent = TilePathD_OK;
1210 gcd[k++].creator = GButtonCreate;
1211
1212 label[k].text = (unichar_t *) _("_Cancel");
1213 label[k].text_is_1byte = true;
1214 label[k].text_in_resource = true;
1215 gcd[k].gd.label = &label[k];
1216 gcd[k].gd.flags = gg_enabled|gg_visible|gg_but_cancel;
1217 gcd[k].gd.handle_controlevent = TilePathD_Cancel;
1218 gcd[k++].creator = GButtonCreate;
1219
1220 harray[0] = GCD_Glue; harray[1] = &gcd[k-2]; harray[2] = GCD_Glue;
1221 harray[3] = GCD_Glue; harray[4] = &gcd[k-1]; harray[5] = GCD_Glue;
1222 harray[6] = NULL;
1223
1224 boxes[2].gd.flags = gg_enabled|gg_visible;
1225 boxes[2].gd.u.boxelements = harray;
1226 boxes[2].creator = GHBoxCreate;
1227
1228 boxes[3].gd.flags = gg_enabled|gg_visible;
1229 boxes[3].gd.u.boxelements = chvarray[0];
1230 boxes[3].creator = GHVBoxCreate;
1231
1232 boxes[4].gd.flags = gg_enabled|gg_visible;
1233 boxes[4].gd.u.boxelements = rhvarray[0];
1234 boxes[4].creator = GHVBoxCreate;
1235
1236 varray[0] = &gcd[0];
1237 varray[1] = &boxes[3];
1238 varray[2] = &boxes[4];
1239 varray[3] = &boxes[2];
1240 varray[4] = NULL;
1241
1242 boxes[0].gd.flags = gg_enabled|gg_visible;
1243 boxes[0].gd.u.boxelements = varray;
1244 boxes[0].creator = GVBoxCreate;
1245
1246 GGadgetsCreate(gw,boxes);
1247
1248 TPDCharViewInits(&tpd,CID_FirstTile);
1249
1250 GHVBoxSetExpandableRow(boxes[0].ret,1);
1251 GHVBoxSetExpandableCol(boxes[2].ret,gb_expandgluesame);
1252 GHVBoxSetExpandableRow(boxes[3].ret,1);
1253 GHVBoxSetPadding(boxes[3].ret, 2, 2);
1254 GHVBoxSetExpandableCol(boxes[4].ret,gb_expandglue);
1255 GGadgetResize(boxes[0].ret,pos.width,pos.height);
1256
1257 TPDMakeActive(&tpd,&tpd.cv_medial);
1258
1259 GDrawResize(gw,1000,400); /* Force a resize event */
1260
1261 GDrawSetVisible(tpd.gw,true);
1262
1263 while ( !tpd.done )
1264 GDrawProcessOneEvent(NULL);
1265
1266 for ( i=0; i<4; ++i ) {
1267 CharView *cv = &tpd.cv_first + i;
1268 CVPalettesHideIfMine(cv);
1269 }
1270 GDrawDestroyWindow(tpd.gw);
1271 return( tpd.oked );
1272 }
1273
1274 static void TDFree(struct tiledata *td) {
1275 int i;
1276
1277 for ( i=0 ; i<4; ++i )
1278 SplinePointListFree( last_tiles[i]);
1279
1280 last_tiles[0] = td->firsttile;
1281 last_tiles[1] = td->basetile;
1282 last_tiles[2] = td->lasttile;
1283 last_tiles[3] = td->isolatedtile;
1284 }
1285
1286 void CVTile(CharView *cv) {
1287 struct tiledata td;
1288 int anypoints, anyrefs, anyimages, anyattach;
1289
1290 CVAnySel(cv,&anypoints,&anyrefs,&anyimages,&anyattach);
1291 if ( anyrefs || anyimages || anyattach )
1292 return;
1293
1294 if ( !TileAsk(&td,cv->b.sc->parent))
1295 return;
1296
1297 CVPreserveState((CharViewBase *) cv);
1298 TileIt(&cv->b.layerheads[cv->b.drawmode]->splines,&td, !anypoints,cv->b.layerheads[cv->b.drawmode]->order2);
1299 CVCharChangedUpdate(&cv->b);
1300 TDFree(&td);
1301 cv->lastselpt = NULL;
1302 }
1303
1304 void SCTile(SplineChar *sc,int layer) {
1305 struct tiledata td;
1306
1307 if ( sc->layers[layer].splines==NULL )
1308 return;
1309
1310 if ( !TileAsk(&td,sc->parent))
1311 return;
1312
1313 SCPreserveLayer(sc,layer,false);
1314 TileIt(&sc->layers[layer].splines,&td, true, sc->layers[layer].order2);
1315 SCCharChangedUpdate(sc,layer);
1316 TDFree(&td);
1317 }
1318
1319 void FVTile(FontView *fv) {
1320 struct tiledata td;
1321 SplineChar *sc;
1322 int i, gid;
1323 int layer = fv->b.active_layer;
1324
1325 for ( i=0; i<fv->b.map->enccount; ++i )
1326 if ( fv->b.selected[i] && (gid=fv->b.map->map[i])!=-1 &&
1327 (sc=fv->b.sf->glyphs[gid])!=NULL && sc->layers[ly_fore].splines!=NULL )
1328 break;
1329 if ( i==fv->b.map->enccount )
1330 return;
1331
1332 if ( !TileAsk(&td,fv->b.sf))
1333 return;
1334
1335 SFUntickAll(fv->b.sf);
1336 for ( i=0; i<fv->b.map->enccount; ++i )
1337 if ( fv->b.selected[i] && (gid=fv->b.map->map[i])!=-1 &&
1338 (sc=fv->b.sf->glyphs[gid])!=NULL && !sc->ticked &&
1339 sc->layers[layer].splines!=NULL ) {
1340 sc->ticked = true;
1341 SCPreserveLayer(sc,layer,false);
1342 TileIt(&sc->layers[layer].splines,&td, true, fv->b.sf->layers[layer].order2);
1343 SCCharChangedUpdate(sc,layer);
1344 }
1345 TDFree(&td);
1346 }
1347
1348 /* ************************************************************************** */
1349 /* ****************************** Tile Pattern ****************************** */
1350 /* ************************************************************************** */
1351
1352 static void SCTilePattern(SplineChar *sc, int layer, Layer *pattern,
1353 BasePoint *size, IPoint *cnt) {
1354 Layer *ly;
1355 SplineSet *ss, *lastss;
1356 RefChar *ref, *lastref, *r;
1357 real transform[6];
1358 int i,j;
1359
1360 if ( pattern==NULL || cnt->x<=0 || cnt->y<=0 || size->x<=0 || size->y<=0 ||
1361 (pattern->splines==NULL && (pattern->refs==NULL || layer==ly_grid )))
1362 return;
1363
1364 SCPreserveLayer(sc,layer,false);
1365
1366 if ( layer==ly_grid )
1367 ly = &sc->parent->grid;
1368 else if ( layer<0 )
1369 ly = &sc->layers[ly_fore];
1370 else
1371 ly = &sc->layers[layer];
1372
1373 memset(transform,0,sizeof(transform));
1374 transform[0] = transform[3] = 1.0;
1375
1376 if ( ly->splines==NULL )
1377 lastss = NULL;
1378 else
1379 for ( lastss=ly->splines; lastss->next!=NULL; lastss=lastss->next );
1380 if ( ly->refs==NULL )
1381 lastref = NULL;
1382 else
1383 for ( lastref=ly->refs; lastref->next!=NULL; lastref=lastref->next );
1384
1385 for ( i=0; i<cnt->x; ++i ) for ( j=0; j<cnt->y; ++j ) {
1386 transform[4] = i*size->x; transform[5] = j*size->y;
1387 if ( pattern->splines!=NULL ) {
1388 ss = SplinePointListTransform(SplinePointListCopy(pattern->splines),transform,tpt_AllPoints);
1389 if ( lastss!=NULL )
1390 lastss->next = ss;
1391 else
1392 ly->splines = ss;
1393 for ( ; ss->next!=NULL; ss=ss->next );
1394 lastss = ss;
1395 }
1396 if ( pattern->refs!=NULL ) {
1397 ref = RefCharsCopy(pattern->refs);
1398 ref->transform[4] += transform[4];
1399 ref->transform[5] += transform[5];
1400 if ( lastref!=NULL )
1401 lastref->next = ref;
1402 else
1403 ly->refs = ref;
1404 for ( r=ref; r!=NULL; r=r->next ) {
1405 SCMakeDependent(sc,r->sc);
1406 SCReinstanciateRefChar(sc,r,layer);
1407 lastref = r;
1408 }
1409 }
1410 }
1411 SCCharChangedUpdate(sc,layer);
1412 }
1413
1414 static SplineSet *last_pattern=NULL;
1415 static BasePoint patternSize;
1416 static IPoint patternRepeat = { 10, 10 };
1417
1418 #define CID_Tile 1011 /* Same as above */
1419 #define CID_PatternWidth 1012
1420 #define CID_PatternHeight 1013
1421 #define CID_XRepeat 1014
1422 #define CID_YRepeat 1015
1423
1424 static void PTDSubResize(TilePathDlg *tpd, GEvent *event) {
1425 int width, height;
1426
1427 if ( !event->u.resize.sized )
1428 return;
1429
1430 width = event->u.resize.size.width;
1431 height = event->u.resize.size.height;
1432 if ( width!=tpd->cv_width || height!=tpd->cv_height ) {
1433 tpd->cv_width = width; tpd->cv_height = height;
1434 {
1435 CharView *cv = &tpd->cv_first;
1436 GDrawResize(cv->gw,width,height);
1437 }
1438 }
1439
1440 GDrawSync(NULL);
1441 GDrawProcessPendingEvents(NULL);
1442 }
1443
1444 static void PTDDraw(TilePathDlg *tpd, GWindow pixmap, GEvent *event) {
1445 GRect r,pos;
1446
1447 GDrawSetLineWidth(pixmap,0);
1448
1449 GGadgetGetSize(GWidgetGetControl(tpd->gw,CID_Tile),&pos);
1450 r.x = pos.x; r.y = pos.y-1;
1451 r.width = pos.width+1; r.height = pos.height+1;
1452 GDrawDrawRect(pixmap,&r,GDrawGetDefaultForeground(NULL));
1453 }
1454
1455 static void PTDMakeActive(TilePathDlg *ptd,CharView *cv) {
1456
1457 if ( ptd==NULL )
1458 return;
1459 cv->inactive = false;
1460 GDrawSetUserData(ptd->gw,cv);
1461 }
1462
1463 static void PTDChar(TilePathDlg *ptd, GEvent *event) {
1464
1465 CVChar(&ptd->cv_first,event);
1466 }
1467
1468 static void PTD_DoClose(struct cvcontainer *cvc) {
1469 TilePathDlg *ptd = (TilePathDlg *) cvc;
1470 SplineChar *msc = &ptd->sc_first;
1471
1472 SplinePointListsFree(msc->layers[0].splines);
1473 RefCharsFree(msc->layers[0].refs);
1474 SplinePointListsFree(msc->layers[1].splines);
1475 RefCharsFree(msc->layers[1].refs);
1476 free( msc->layers );
1477
1478 ptd->done = true;
1479 }
1480
1481 static int ptd_sub_e_h(GWindow gw, GEvent *event) {
1482 TilePathDlg *ptd;
1483
1484 if ( event->type==et_destroy )
1485 return( true );
1486
1487 ptd = (TilePathDlg *) ((CharViewBase *) GDrawGetUserData(gw))->container;
1488
1489 switch ( event->type ) {
1490 case et_resize:
1491 if ( event->u.resize.sized )
1492 PTDSubResize(ptd,event);
1493 break;
1494 case et_char:
1495 PTDChar(ptd,event);
1496 break;
1497 }
1498 return( true );
1499 }
1500
1501 static int ptd_e_h(GWindow gw, GEvent *event) {
1502 TilePathDlg *ptd;
1503
1504 if ( event->type == et_destroy )
1505 return( true );
1506
1507 ptd = (TilePathDlg *) ((CharViewBase *) GDrawGetUserData(gw))->container;
1508 switch ( event->type ) {
1509 case et_expose:
1510 PTDDraw(ptd, gw, event);
1511 break;
1512 case et_char:
1513 PTDChar(ptd,event);
1514 break;
1515 case et_close:
1516 PTD_DoClose((struct cvcontainer *) ptd);
1517 break;
1518 case et_timer: {
1519 SplineSet *ss;
1520 char buffer[32];
1521 int err;
1522
1523 for ( ss = ptd->sc_first.layers[ly_back].splines; ss!=NULL; ss=ss->next ) {
1524 if ( ss->first->prev!=NULL || ss->first->next==NULL || ss->first->next->to!=ss->last )
1525 continue;
1526 err = 0;
1527 if ( ss->first->me.x==ss->last->me.x ) {
1528 /* Don't make the change if they are the same (or there's an */
1529 /* error) as the user might be busy editing and that would */
1530 /* lose his/her work */
1531 if ( !RealNear(GetCalmReal8(ptd->gw,CID_PatternWidth,_("Width"),&err),
1532 ss->first->me.x) && !err ) {
1533 sprintf( buffer,"%g",ss->first->me.x);
1534 GGadgetSetTitle8(GWidgetGetControl(ptd->gw,CID_PatternWidth),buffer);
1535 }
1536 } else if ( ss->first->me.y==ss->last->me.y ) {
1537 if ( !RealNear(GetCalmReal8(ptd->gw,CID_PatternHeight,_("Height"),&err),
1538 ss->first->me.y) && !err ) {
1539 sprintf( buffer,"%g",ss->first->me.y);
1540 GGadgetSetTitle8(GWidgetGetControl(ptd->gw,CID_PatternHeight),buffer);
1541 }
1542 }
1543 }
1544 } break;
1545 case et_map:
1546 {
1547 CharView *cv = &ptd->cv_first;
1548 if ( !cv->inactive ) {
1549 if ( event->u.map.is_visible )
1550 CVPaletteActivate(cv);
1551 else
1552 CVPalettesHideIfMine(cv);
1553 break;
1554 }
1555 }
1556 /* ptd->isvisible = event->u.map.is_visible; */
1557 break;
1558 }
1559 return( true );
1560 }
1561
1562 static int PTD_RefigureBackground(GGadget *g, GEvent *e) {
1563 if ( e==NULL || (e->type==et_controlevent && e->u.control.subtype == et_textchanged )) {
1564 TilePathDlg *ptd = (TilePathDlg *) (((CharViewBase *) GDrawGetUserData(GGadgetGetWindow(g)))->container);
1565 int hsize, vsize;
1566 int err = 0;
1567 SplinePoint *sp1, *sp2;
1568 SplineSet *ss;
1569
1570 hsize = GetCalmReal8(ptd->gw,CID_PatternWidth,_("Width"),&err);
1571 vsize = GetCalmReal8(ptd->gw,CID_PatternHeight,_("Height"),&err);
1572 if ( err )
1573 return( true );
1574 ptd->sc_first.width = hsize;
1575 SplinePointListFree(ptd->sc_first.layers[ly_back].splines);
1576 sp1 = SplinePointCreate(-1000,vsize);
1577 sp2 = SplinePointCreate(2000,vsize);
1578 SplineMake(sp1,sp2,ptd->sc_first.layers[ly_back].order2);
1579 ss = chunkalloc(sizeof(SplineSet));
1580 ss->first = sp1; ss->last = sp2;
1581 ptd->sc_first.layers[ly_back].splines = ss;
1582 sp1 = SplinePointCreate(hsize,-1000);
1583 sp2 = SplinePointCreate(hsize,2000);
1584 SplineMake(sp1,sp2,ptd->sc_first.layers[ly_back].order2);
1585 ss = chunkalloc(sizeof(SplineSet));
1586 ss->first = sp1; ss->last = sp2;
1587 ptd->sc_first.layers[ly_back].splines->next = ss;
1588 GDrawRequestExpose(ptd->cv_first.v,NULL,false);
1589 }
1590 return( true );
1591 }
1592
1593 static int PTD_Cancel(GGadget *g, GEvent *e) {
1594 if ( e->type==et_controlevent && e->u.control.subtype == et_buttonactivate ) {
1595 TilePathDlg *ptd = (TilePathDlg *) (((CharViewBase *) GDrawGetUserData(GGadgetGetWindow(g)))->container);
1596 PTD_DoClose(&ptd->base);
1597 }
1598 return( true );
1599 }
1600
1601 static int PTD_OK(GGadget *g, GEvent *e) {
1602 if ( e->type==et_controlevent && e->u.control.subtype == et_buttonactivate ) {
1603 TilePathDlg *ptd = (TilePathDlg *) (((CharViewBase *) GDrawGetUserData(GGadgetGetWindow(g)))->container);
1604 struct tiledata *td = ptd->td;
1605 int err = 0;
1606
1607 td->patternSize.x = GetReal8(ptd->gw,CID_PatternWidth,_("Width"),&err);
1608 td->patternSize.y = GetReal8(ptd->gw,CID_PatternHeight,_("Height"),&err);
1609
1610 td->repeatCnt.x = GetInt8(ptd->gw,CID_XRepeat,_("X Repeat Count"),&err);
1611 td->repeatCnt.y = GetInt8(ptd->gw,CID_YRepeat,_("Y Repeat Count"),&err);
1612 if ( err )
1613 return( true );
1614 if ( td->patternSize.x<=0 || td->patternSize.y<=0 ) {
1615 ff_post_error(_("Bad Pattern Size"),_("The pattern size (width & height) must be a positive number"));
1616 return( true );
1617 }
1618 if ( td->repeatCnt.x<=0 || td->repeatCnt.y<=0 || td->repeatCnt.x>2000 || td->repeatCnt.y>2000 ) {
1619 ff_post_error(_("Bad Pattern Size"),_("The repeat counts must be positive numbers"));
1620 return( true );
1621 }
1622
1623 td->pattern = calloc(1,sizeof(Layer));
1624 td->pattern->splines = SplinePointListCopy(ptd->sc_first.layers[ly_fore].splines);
1625 td->pattern->refs = RefCharsCopy(ptd->sc_first.layers[ly_fore].refs);
1626
1627 if ( td->pattern->splines==NULL && td->pattern->refs==NULL ) {
1628 ff_post_error(_("Bad Pattern"),_("You must specify a pattern"));
1629 return( true );
1630 }
1631
1632 PTD_DoClose(&ptd->base);
1633 ptd->oked = true;
1634 }
1635 return( true );
1636 }
1637
1638 static int PTD_Can_Navigate(struct cvcontainer *cvc, enum nav_type type) {
1639 return( false );
1640 }
1641
1642 static int PTD_Can_Open(struct cvcontainer *cvc) {
1643 return( false );
1644 }
1645
1646 static SplineFont *SF_Of_PTD(struct cvcontainer *cvc) {
1647 TilePathDlg *ptd = (TilePathDlg *) cvc;
1648 return( ptd->base_sf );
1649 }
1650
1651 struct cvcontainer_funcs tilepattern_funcs = {
1652 cvc_multiplepattern,
1653 (void (*) (struct cvcontainer *cvc,CharViewBase *cv)) PTDMakeActive,
1654 (void (*) (struct cvcontainer *cvc,void *)) PTDChar,
1655 PTD_Can_Navigate,
1656 NULL,
1657 PTD_Can_Open,
1658 PTD_DoClose,
1659 SF_Of_PTD
1660 };
1661
1662
1663 static void PTDInit(TilePathDlg *ptd,SplineFont *sf) {
1664
1665 memset(ptd,0,sizeof(*ptd));
1666 ptd->base.funcs = &tilepattern_funcs;
1667 ptd->base_sf = sf;
1668
1669 {
1670 SplineChar *msc = &ptd->sc_first;
1671 CharView *mcv = &ptd->cv_first;
1672 msc->orig_pos = 0;
1673 msc->unicodeenc = -1;
1674 msc->name = "Pattern";
1675 msc->parent = &ptd->dummy_sf;
1676 msc->layer_cnt = 2;
1677 msc->layers = calloc(2,sizeof(Layer));
1678 LayerDefault(&msc->layers[0]);
1679 LayerDefault(&msc->layers[1]);
1680 ptd->chars[0] = msc;
1681
1682 mcv->b.sc = msc;
1683 mcv->b.layerheads[dm_fore] = &msc->layers[ly_fore];
1684 mcv->b.layerheads[dm_back] = &msc->layers[ly_back];
1685 mcv->b.layerheads[dm_grid] = &ptd->dummy_sf.grid;
1686 msc->layers[ly_fore].splines = SplinePointListCopy(last_pattern);
1687 mcv->b.drawmode = dm_fore;
1688 mcv->b.container = (struct cvcontainer *) ptd;
1689 mcv->inactive = false;
1690 }
1691 ptd->dummy_sf.glyphs = ptd->chars;
1692 ptd->dummy_sf.glyphcnt = ptd->dummy_sf.glyphmax = 1;
1693 ptd->dummy_sf.pfminfo.fstype = -1;
1694 ptd->dummy_sf.pfminfo.stylemap = -1;
1695 ptd->dummy_sf.fontname = ptd->dummy_sf.fullname = ptd->dummy_sf.familyname = "dummy";
1696 ptd->dummy_sf.weight = "Medium";
1697 ptd->dummy_sf.origname = "dummy";
1698 ptd->dummy_sf.ascent = sf->ascent;
1699 ptd->dummy_sf.descent = sf->descent;
1700 ptd->dummy_sf.layers = ptd->layerinfo;
1701 ptd->dummy_sf.layer_cnt = 2;
1702 ptd->layerinfo[ly_back].order2 = sf->layers[ly_back].order2;
1703 ptd->layerinfo[ly_back].name = _("Size");
1704 ptd->layerinfo[ly_fore].order2 = sf->layers[ly_fore].order2;
1705 ptd->layerinfo[ly_fore].name = _("Pattern");
1706 ptd->dummy_sf.grid.order2 = sf->grid.order2;
1707 ptd->dummy_sf.anchor = NULL;
1708
1709 ptd->dummy_sf.fv = (FontViewBase *) &ptd->dummy_fv;
1710 ptd->dummy_fv.b.active_layer = ly_fore;
1711 ptd->dummy_fv.b.sf = &ptd->dummy_sf;
1712 ptd->dummy_fv.b.selected = ptd->sel;
1713 ptd->dummy_fv.cbw = ptd->dummy_fv.cbh = default_fv_font_size+1;
1714 ptd->dummy_fv.magnify = 1;
1715
1716 ptd->dummy_fv.b.map = &ptd->dummy_map;
1717 ptd->dummy_map.map = ptd->map;
1718 ptd->dummy_map.backmap = ptd->backmap;
1719 ptd->dummy_map.enccount = ptd->dummy_map.encmax = ptd->dummy_map.backmax = 1;
1720 ptd->dummy_map.enc = &custom;
1721 }
1722
1723 static int TilePatternAsk(struct tiledata *td,SplineFont *sf) {
1724 TilePathDlg ptd;
1725 GRect pos;
1726 GWindow gw;
1727 GWindowAttrs wattrs;
1728 GGadgetCreateData gcd[24], boxes[5], *harray1[8], *harray2[8], *varray[10],
1729 *harray[8];
1730 GTextInfo label[24];
1731 int k;
1732 char width[30], height[30], xr[30], yr[30];
1733
1734 PTDInit( &ptd,sf );
1735 ptd.td = td;
1736 memset(td,0,sizeof(*td));
1737
1738 memset(&wattrs,0,sizeof(wattrs));
1739 wattrs.mask = wam_events|wam_cursor|wam_isdlg|wam_restrict|wam_undercursor|wam_utf8_wtitle;
1740 wattrs.is_dlg = true;
1741 wattrs.restrict_input_to_me = 1;
1742 wattrs.undercursor = 1;
1743 wattrs.event_masks = -1;
1744 wattrs.cursor = ct_pointer;
1745 wattrs.utf8_window_title = _("Tile Pattern");
1746 pos.width = 600;
1747 pos.height = 300;
1748 ptd.gw = gw = GDrawCreateTopWindow(NULL,&pos,ptd_e_h,&ptd.cv_first,&wattrs);
1749
1750 memset(&label,0,sizeof(label));
1751 memset(&gcd,0,sizeof(gcd));
1752 memset(&boxes,0,sizeof(boxes));
1753
1754 k = 0;
1755 gcd[k].gd.flags = gg_visible|gg_enabled ; /* This space is for the menubar */
1756 gcd[k].gd.pos.height = 18; gcd[k].gd.pos.width = 20;
1757 gcd[k++].creator = GSpacerCreate;
1758 varray[0] = &gcd[k-1];
1759
1760 gcd[k].gd.pos.width = gcd[k].gd.pos.height = 200;
1761 gcd[k].gd.flags = gg_visible | gg_enabled;
1762 gcd[k].gd.cid = CID_Tile;
1763 gcd[k].gd.u.drawable_e_h = ptd_sub_e_h;
1764 gcd[k++].creator = GDrawableCreate;
1765 varray[1] = &gcd[k-1];
1766
1767 gcd[k].gd.flags = gg_visible | gg_enabled;
1768 label[k].text = (unichar_t *) _("Pattern Size:");
1769 label[k].text_is_1byte = true;
1770 gcd[k].gd.label = &label[k];
1771 gcd[k++].creator = GLabelCreate;
1772 varray[2] = &gcd[k-1];
1773
1774 gcd[k].gd.flags = gg_visible | gg_enabled;
1775 label[k].text = (unichar_t *) _("Width:");
1776 label[k].text_is_1byte = true;
1777 gcd[k].gd.label = &label[k];
1778 gcd[k++].creator = GLabelCreate;
1779 harray1[0] = &gcd[k-1];
1780
1781 if ( last_pattern==NULL )
1782 sprintf( width, "%d", sf->ascent+sf->descent );
1783 else
1784 sprintf( width, "%g", patternSize.x );
1785 gcd[k].gd.flags = gg_visible | gg_enabled;
1786 label[k].text = (unichar_t *) width;
1787 label[k].text_is_1byte = true;
1788 gcd[k].gd.label = &label[k];
1789 gcd[k].gd.cid = CID_PatternWidth;
1790 gcd[k].gd.handle_controlevent = PTD_RefigureBackground;
1791 gcd[k++].creator = GTextFieldCreate;
1792 harray1[1] = &gcd[k-1];
1793
1794 gcd[k].gd.flags = gg_visible | gg_enabled;
1795 label[k].text = (unichar_t *) _("Height:");
1796 label[k].text_is_1byte = true;
1797 gcd[k].gd.label = &label[k];
1798 gcd[k++].creator = GLabelCreate;
1799 harray1[2] = &gcd[k-1];
1800
1801 if ( last_pattern==NULL )
1802 sprintf( height, "%d", sf->ascent+sf->descent );
1803 else
1804 sprintf( height, "%g", patternSize.y );
1805 gcd[k].gd.flags = gg_visible | gg_enabled;
1806 label[k].text = (unichar_t *) height;
1807 label[k].text_is_1byte = true;
1808 gcd[k].gd.label = &label[k];
1809 gcd[k].gd.cid = CID_PatternHeight;
1810 gcd[k].gd.handle_controlevent = PTD_RefigureBackground;
1811 gcd[k++].creator = GTextFieldCreate;
1812 harray1[3] = &gcd[k-1]; harray1[4] = NULL;
1813
1814 boxes[2].gd.flags = gg_enabled|gg_visible;
1815 boxes[2].gd.u.boxelements = harray1;
1816 boxes[2].creator = GHBoxCreate;
1817 varray[3] = &boxes[2];
1818
1819 gcd[k].gd.flags = gg_visible | gg_enabled;
1820 label[k].text = (unichar_t *) _("Repeat Counts:");
1821 label[k].text_is_1byte = true;
1822 gcd[k].gd.label = &label[k];
1823 gcd[k++].creator = GLabelCreate;
1824 varray[4] = &gcd[k-1];
1825
1826 gcd[k].gd.flags = gg_visible | gg_enabled;
1827 label[k].text = (unichar_t *) _("X:");
1828 label[k].text_is_1byte = true;
1829 gcd[k].gd.label = &label[k];
1830 gcd[k++].creator = GLabelCreate;
1831 harray2[0] = &gcd[k-1];
1832
1833 sprintf( xr, "%d", patternRepeat.x );
1834 gcd[k].gd.flags = gg_visible | gg_enabled;
1835 label[k].text = (unichar_t *) xr;
1836 label[k].text_is_1byte = true;
1837 gcd[k].gd.label = &label[k];
1838 gcd[k].gd.cid = CID_XRepeat;
1839 gcd[k++].creator = GTextFieldCreate;
1840 harray2[1] = &gcd[k-1];
1841
1842 gcd[k].gd.flags = gg_visible | gg_enabled;
1843 label[k].text = (unichar_t *) _("Y:");
1844 label[k].text_is_1byte = true;
1845 gcd[k].gd.label = &label[k];
1846 gcd[k++].creator = GLabelCreate;
1847 harray2[2] = &gcd[k-1];
1848
1849 sprintf( yr, "%d", patternRepeat.y );
1850 gcd[k].gd.flags = gg_visible | gg_enabled;
1851 label[k].text = (unichar_t *) yr;
1852 label[k].text_is_1byte = true;
1853 gcd[k].gd.label = &label[k];
1854 gcd[k].gd.cid = CID_YRepeat;
1855 gcd[k++].creator = GTextFieldCreate;
1856 harray2[3] = &gcd[k-1]; harray2[4] = NULL;
1857
1858 boxes[3].gd.flags = gg_enabled|gg_visible;
1859 boxes[3].gd.u.boxelements = harray2;
1860 boxes[3].creator = GHBoxCreate;
1861 varray[5] = &boxes[3];
1862
1863
1864 label[k].text = (unichar_t *) _("_OK");
1865 label[k].text_is_1byte = true;
1866 label[k].text_in_resource = true;
1867 gcd[k].gd.label = &label[k];
1868 gcd[k].gd.flags = gg_enabled|gg_visible|gg_but_default;
1869 gcd[k].gd.handle_controlevent = PTD_OK;
1870 gcd[k++].creator = GButtonCreate;
1871
1872 label[k].text = (unichar_t *) _("_Cancel");
1873 label[k].text_is_1byte = true;
1874 label[k].text_in_resource = true;
1875 gcd[k].gd.label = &label[k];
1876 gcd[k].gd.flags = gg_enabled|gg_visible|gg_but_cancel;
1877 gcd[k].gd.handle_controlevent = PTD_Cancel;
1878 gcd[k++].creator = GButtonCreate;
1879
1880 harray[0] = GCD_Glue; harray[1] = &gcd[k-2]; harray[2] = GCD_Glue;
1881 harray[3] = GCD_Glue; harray[4] = &gcd[k-1]; harray[5] = GCD_Glue;
1882 harray[6] = NULL;
1883
1884 boxes[4].gd.flags = gg_enabled|gg_visible;
1885 boxes[4].gd.u.boxelements = harray;
1886 boxes[4].creator = GHBoxCreate;
1887 varray[6] = &boxes[4]; varray[7] = NULL;
1888
1889 boxes[0].gd.flags = gg_enabled|gg_visible;
1890 boxes[0].gd.u.boxelements = varray;
1891 boxes[0].creator = GVBoxCreate;
1892
1893 GGadgetsCreate(gw,boxes);
1894
1895 PTDCharViewInits(&ptd,CID_Tile);
1896
1897 GHVBoxSetExpandableRow(boxes[0].ret,1);
1898 GHVBoxSetExpandableCol(boxes[4].ret,gb_expandgluesame);
1899 GGadgetResize(boxes[0].ret,pos.width,pos.height);
1900
1901 PTD_RefigureBackground(GWidgetGetControl(ptd.gw,CID_PatternWidth),NULL);
1902
1903 PTDMakeActive(&ptd,&ptd.cv_first);
1904
1905 GDrawResize(gw,400,400); /* Force a resize event */
1906
1907 GDrawSetVisible(ptd.gw,true);
1908 GDrawRequestTimer(ptd.gw,1000,1000,NULL);
1909
1910 while ( !ptd.done )
1911 GDrawProcessOneEvent(NULL);
1912
1913 {
1914 CharView *cv = &ptd.cv_first;
1915 CVPalettesHideIfMine(cv);
1916 }
1917 GDrawDestroyWindow(ptd.gw);
1918 return( ptd.oked );
1919 }
1920
1921 static void TPFree(struct tiledata *td) {
1922
1923 SplinePointListFree(last_pattern);
1924 last_pattern = td->pattern->splines;
1925 RefCharsFree(td->pattern->refs);
1926 free(td->pattern);
1927 patternSize = td->patternSize;
1928 patternRepeat = td->repeatCnt;
1929 }
1930
1931 void CVPatternTile(CharView *cv) {
1932 struct tiledata td;
1933
1934 if ( !TilePatternAsk(&td,cv->b.sc->parent))
1935 return;
1936
1937 SCTilePattern(cv->b.sc,CVLayer((CharViewBase *) cv),td.pattern, &td.patternSize, &td.repeatCnt);
1938 TPFree(&td);
1939 cv->lastselpt = NULL;
1940 }
1941
1942 void FVPatternTile(FontView *fv) {
1943 struct tiledata td;
1944 SplineChar *sc;
1945 int i, gid;
1946 int layer = fv->b.active_layer;
1947
1948 for ( i=0; i<fv->b.map->enccount; ++i )
1949 if ( fv->b.selected[i] && (gid=fv->b.map->map[i])!=-1 &&
1950 (sc=fv->b.sf->glyphs[gid])!=NULL )
1951 break;
1952 if ( i==fv->b.map->enccount )
1953 return;
1954
1955 if ( !TilePatternAsk(&td,fv->b.sf))
1956 return;
1957
1958 SFUntickAll(fv->b.sf);
1959 for ( i=0; i<fv->b.map->enccount; ++i )
1960 if ( fv->b.selected[i] && (gid=fv->b.map->map[i])!=-1 &&
1961 (sc=fv->b.sf->glyphs[gid])!=NULL && !sc->ticked ) {
1962 sc->ticked = true;
1963 SCTilePattern(sc,layer,td.pattern, &td.patternSize, &td.repeatCnt);
1964 }
1965 TPFree(&td);
1966 }
1967 #endif /* FONTFORGE_CONFIG_TILEPATH */