Debian Codegraph @

fontforgeexe/cvruler.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 "cvruler.h"
31 #include "fontforgeui.h"
32 #include "gresedit.h"
33 #include "splineutil.h"
34 #include "splineutil2.h"
35 #include "ustring.h"
36
37 #include <math.h>
38
39 int measuretoolshowhorizontolvertical = true;
40 Color measuretoollinecol = 0x000000;
41 Color measuretoolpointcol = 0xFF0000;
42 Color measuretoolpointsnappedcol = 0x00FF00;
43 Color measuretoolcanvasnumberscol = 0xFF0000;
44 Color measuretoolcanvasnumberssnappedcol = 0x00FF00;
45 Color measuretoolwindowforegroundcol = 0x000000;
46 Color measuretoolwindowbackgroundcol = 0xe0e0c0;
47
48 BasePoint last_ruler_offset[2] = { {0,0}, {0,0} };
49 int infowindowdistance = 30;
50
51 static void SlopeToBuf(char *buf,char *label,double dx, double dy) {
52 if ( dx==0 && dy==0 )
53 sprintf( buf, _("%s No Slope"), label );
54 else if ( dx==0 )
55 sprintf( buf, "%s dy/dx= ∞, %4g°", label, atan2(dy,dx)*180/FF_PI);
56 else
57 sprintf( buf, "%s dy/dx= %4g, %4g°", label, dy/dx, atan2(dy,dx)*180/FF_PI);
58 }
59
60 static void CurveToBuf(char *buf,CharView *cv,Spline *s, double t) {
61 double kappa, emsize;
62
63 kappa = SplineCurvature(s,t);
64 if ( kappa==CURVATURE_ERROR )
65 strcpy(buf,_("No Curvature"));
66 else {
67 emsize = cv->b.sc->parent->ascent + cv->b.sc->parent->descent;
68 if ( kappa==0 )
69 sprintf(buf,_(" Curvature: %g"), kappa*emsize);
70 else
71 sprintf(buf,_(" Curvature: %g Radius: %g"), kappa*emsize, 1.0/kappa );
72 }
73 }
74
75 static int RulerText(CharView *cv, unichar_t *ubuf, int line) {
76 char buf[80];
77 double len;
78 double dx, dy;
79 Spline *s;
80 double t;
81 BasePoint slope;
82 real xoff = cv->info.x-cv->p.cx, yoff = cv->info.y-cv->p.cy;
83
84 buf[0] = '\0';
85 switch ( line ) {
86 case 0: {
87 real len = sqrt(xoff*xoff+yoff*yoff);
88
89 if ( cv->autonomous_ruler_w ) {
90 xoff = last_ruler_offset[0].x;
91 yoff = last_ruler_offset[0].y;
92 }
93
94 if ( !cv->autonomous_ruler_w && !cv->p.pressed )
95 /* Give current location accurately */
96 sprintf( buf, "%f,%f", (double) cv->info.x, (double) cv->info.y);
97 else
98 sprintf( buf, "%f %.0f° (%f,%f)", (double) len,
99 atan2(yoff,xoff)*180/FF_PI,
100 (double) xoff,(double) yoff);
101 break; }
102 case 1:
103 if ( cv->p.pressed ) {
104 if ( cv->p.spline!=NULL ||
105 (cv->p.sp!=NULL &&
106 ((cv->p.sp->next==NULL && cv->p.sp->prev!=NULL) ||
107 (cv->p.sp->prev==NULL && cv->p.sp->next!=NULL) ||
108 (cv->p.sp->next!=NULL && cv->p.sp->prev!=NULL &&
109 BpColinear(!cv->p.sp->noprevcp ? &cv->p.sp->prevcp : &cv->p.sp->prev->from->me,
110 &cv->p.sp->me,
111 !cv->p.sp->nonextcp ? &cv->p.sp->nextcp : &cv->p.sp->next->to->me))) ) ) {
112 Spline *spline;
113 double t;
114
115 if ( cv->p.spline!=NULL ) {
116 spline = cv->p.spline;
117 t = cv->p.t;
118 } else if ( cv->p.sp->next == NULL ) {
119 spline = cv->p.sp->prev;
120 t = 1;
121 } else {
122 spline = cv->p.sp->next;
123 t = 0;
124 }
125 slope.x = (3*spline->splines[0].a*t + 2*spline->splines[0].b)*t + spline->splines[0].c;
126 slope.y = (3*spline->splines[1].a*t + 2*spline->splines[1].b)*t + spline->splines[1].c;
127 len = sqrt(slope.x*slope.x + slope.y*slope.y);
128 if ( len!=0 ) {
129 slope.x /= len; slope.y /= len;
130 sprintf( buf, _("Normal Distance: %.2f Along Spline: %.2f"),
131 fabs(slope.y*xoff - slope.x*yoff),
132 slope.x*xoff + slope.y*yoff );
133 }
134 }
135 } else if ( cv->dv!=NULL || cv->b.gridfit!=NULL ) {
136 double scalex = (cv->b.sc->parent->ascent+cv->b.sc->parent->descent)/(rint(cv->ft_pointsizex*cv->ft_dpi/72.0));
137 double scaley = (cv->b.sc->parent->ascent+cv->b.sc->parent->descent)/(rint(cv->ft_pointsizey*cv->ft_dpi/72.0));
138 sprintf( buf, "%.2f,%.2f", (double) (cv->info.x/scalex), (double) (cv->info.y/scaley));
139 } else if ( cv->p.spline!=NULL ) {
140 s = cv->p.spline;
141 t = cv->p.t;
142 sprintf( buf, _("Near (%f,%f)"),
143 (double) (((s->splines[0].a*t+s->splines[0].b)*t+s->splines[0].c)*t+s->splines[0].d),
144 (double) (((s->splines[1].a*t+s->splines[1].b)*t+s->splines[1].c)*t+s->splines[1].d) );
145 } else if ( cv->p.sp!=NULL ) {
146 sprintf( buf, _("Near (%f,%f)"),(double) cv->p.sp->me.x,(double) cv->p.sp->me.y );
147 } else
148 return( false );
149 break;
150 case 2:
151 if ( cv->p.pressed ) {
152 if ( cv->p.sp!=NULL && cv->info_sp!=NULL &&
153 ((cv->p.sp->next!=NULL && cv->p.sp->next->to==cv->info_sp) ||
154 (cv->p.sp->prev!=NULL && cv->p.sp->prev->from==cv->info_sp)) ) {
155 if ( cv->p.sp->next!=NULL && cv->p.sp->next->to==cv->info_sp )
156 len = SplineLength(cv->p.sp->next);
157 else
158 len = SplineLength(cv->p.sp->prev);
159 } else if ( cv->p.spline == cv->info_spline && cv->info_spline!=NULL )
160 len = SplineLengthRange(cv->info_spline,cv->p.t,cv->info_t);
161 else if ( cv->p.sp!=NULL && cv->info_spline!=NULL &&
162 cv->p.sp->next == cv->info_spline )
163 len = SplineLengthRange(cv->info_spline,0,cv->info_t);
164 else if ( cv->p.sp!=NULL && cv->info_spline!=NULL &&
165 cv->p.sp->prev == cv->info_spline )
166 len = SplineLengthRange(cv->info_spline,cv->info_t,1);
167 else if ( cv->info_sp!=NULL && cv->p.spline!=NULL &&
168 cv->info_sp->next == cv->p.spline )
169 len = SplineLengthRange(cv->p.spline,0,cv->p.t);
170 else if ( cv->info_sp!=NULL && cv->p.spline!=NULL &&
171 cv->info_sp->prev == cv->p.spline )
172 len = SplineLengthRange(cv->p.spline,cv->p.t,1);
173 else
174 return( false );
175 if ( len>1 )
176 sprintf( buf, _("Spline Length=%.1f"), len);
177 else
178 sprintf( buf, _("Spline Length=%g"), len);
179 } else if ( cv->p.spline!=NULL ) {
180 s = cv->p.spline;
181 t = cv->p.t;
182 dx = (3*s->splines[0].a*t+2*s->splines[0].b)*t+s->splines[0].c;
183 dy = (3*s->splines[1].a*t+2*s->splines[1].b)*t+s->splines[1].c;
184 SlopeToBuf(buf,"",dx,dy);
185 } else if ( cv->p.sp!=NULL ) {
186 if ( cv->p.sp->nonextcp )
187 strcpy(buf,_("No Next Control Point"));
188 else
189 sprintf(buf,_("Next CP: (%f,%f)"), (double) cv->p.sp->nextcp.x, (double) cv->p.sp->nextcp.y);
190 } else
191 return( false );
192 break;
193 case 3:
194 if ( cv->p.pressed )
195 return( false );
196 else if ( cv->p.spline!=NULL ) {
197 CurveToBuf(buf,cv,cv->p.spline,cv->p.t);
198 } else if ( cv->p.sp!=NULL && cv->p.sp->next!=NULL ) {
199 s = cv->p.sp->next;
200 dx = s->splines[0].c;
201 dy = s->splines[1].c;
202 SlopeToBuf(buf,_(" Next"),dx,dy);
203 } else if ( cv->p.sp!=NULL ) {
204 if ( cv->p.sp->noprevcp )
205 strcpy(buf,_("No Previous Control Point"));
206 else
207 sprintf(buf,_("Prev CP: (%f,%f)"), (double) cv->p.sp->prevcp.x, (double) cv->p.sp->prevcp.y);
208 } else
209 return( false );
210 break;
211 case 4:
212 if ( cv->p.spline!=NULL )
213 return( false );
214 else if ( cv->p.sp->next!=NULL ) {
215 CurveToBuf(buf,cv,cv->p.sp->next,0);
216 } else if ( cv->p.sp->prev!=NULL ) {
217 s = cv->p.sp->prev;
218 dx = (3*s->splines[0].a*1+2*s->splines[0].b)*1+s->splines[0].c;
219 dy = (3*s->splines[1].a*1+2*s->splines[1].b)*1+s->splines[1].c;
220 SlopeToBuf(buf,_(" Prev"),dx,dy);
221 } else
222 return( false );
223 break;
224 case 5:
225 if ( cv->p.sp->next!=NULL ) {
226 if ( cv->p.sp->noprevcp )
227 strcpy(buf,_("No Previous Control Point"));
228 else
229 sprintf(buf,_("Prev CP: (%f,%f)"), (double) cv->p.sp->prevcp.x, (double) cv->p.sp->prevcp.y);
230 } else {
231 CurveToBuf(buf,cv,cv->p.sp->prev,1);
232 }
233 break;
234 case 6:
235 if ( cv->p.sp->next!=NULL && cv->p.sp->prev!=NULL ) {
236 s = cv->p.sp->prev;
237 dx = (3*s->splines[0].a*1+2*s->splines[0].b)*1+s->splines[0].c;
238 dy = (3*s->splines[1].a*1+2*s->splines[1].b)*1+s->splines[1].c;
239 SlopeToBuf(buf,_(" Prev"),dx,dy);
240 } else
241 return( false );
242 break;
243 case 7:
244 if ( cv->p.sp->next!=NULL && cv->p.sp->prev!=NULL ) {
245 CurveToBuf(buf,cv,cv->p.sp->prev,1);
246 } else
247 return( false );
248 break;
249 default:
250 return( false );
251 }
252 utf82u_strcpy(ubuf,buf);
253 return( true );
254 }
255
256 static int RulerTextIntersection(CharView *cv, unichar_t *ubuf, int i) {
257 char buf[80];
258
259 if ( i==0 && cv->num_ruler_intersections>4 ) {
260 real xoff = cv->ruler_intersections[cv->num_ruler_intersections-2].x - cv->ruler_intersections[1].x;
261 real yoff = cv->ruler_intersections[cv->num_ruler_intersections-2].y - cv->ruler_intersections[1].y;
262 real len = sqrt(xoff*xoff+yoff*yoff);
263 snprintf(buf,sizeof buf, _("First Edge to Last Edge: %g x %g length %f"),fabs(xoff),fabs(yoff),len);
264 utf82u_strcpy(ubuf,buf);
265 return( 1 );
266 } else if ( cv->num_ruler_intersections>4 )
267 i -= 1;
268
269 if ( i>=cv->num_ruler_intersections )
270 return( 0 );
271
272 if ( i==0 ) {
273 snprintf(buf,sizeof buf,"[%d] (%g,%g)",i,cv->ruler_intersections[i].x,cv->ruler_intersections[i].y);
274 if ( cv->p.sp ) {
275 strcat(buf, _(" snapped"));
276 cv->start_intersection_snapped = 1;
277 } else {
278 cv->start_intersection_snapped = 0;
279 }
280 } else {
281 real xoff = cv->ruler_intersections[i].x - cv->ruler_intersections[i-1].x;
282 real yoff = cv->ruler_intersections[i].y - cv->ruler_intersections[i-1].y;
283 real len = sqrt(xoff*xoff+yoff*yoff);
284 snprintf(buf,sizeof buf, _("[%d] (%g,%g) %g x %g length %g"),i,cv->ruler_intersections[i].x,cv->ruler_intersections[i].y,fabs(xoff),fabs(yoff),len);
285 if ( i==(cv->num_ruler_intersections-1) ) {
286 if ( cv->info_sp ) {
287 strcat(buf, _(" snapped"));
288 cv->end_intersection_snapped = 1;
289 } else {
290 cv->end_intersection_snapped = 0;
291 }
292 }
293 }
294
295 utf82u_strcpy(ubuf,buf);
296 return( 1 );
297 }
298
299 static int ruler_e_h(GWindow gw, GEvent *event) {
300 CharView *cv = (CharView *) GDrawGetUserData(gw);
301 unichar_t ubuf[80];
302 int line;
303 int i;
304
305 switch ( event->type ) {
306 case et_expose:
307 GDrawSetFont(gw,cv->rfont);
308 for ( line=0; RulerText(cv,ubuf,line); ++line )
309 GDrawDrawText(gw,2,line*cv->rfh+cv->ras+1,ubuf,-1,measuretoolwindowforegroundcol);
310 if ( cv->p.pressed ) for ( i=0; RulerTextIntersection(cv,ubuf,i); ++i )
311 GDrawDrawText(gw,2,(line+i)*cv->rfh+cv->ras+1,ubuf,-1,measuretoolwindowforegroundcol);
312 break;
313 case et_mousedown:
314 cv->autonomous_ruler_w = false;
315 GDrawDestroyWindow(gw);
316 cv->ruler_w = NULL;
317 break;
318 default: break;
319 }
320 return( true );
321 }
322
323 static int ruler_linger_e_h(GWindow gw, GEvent *event) {
324 CharView *cv = (CharView *) GDrawGetUserData(gw);
325 int i;
326
327 switch ( event->type ) {
328 case et_expose:
329 GDrawSetFont(gw,cv->rfont);
330 for ( i=0; i < cv->ruler_linger_num_lines ; ++i )
331 GDrawDrawText(gw,2,i*cv->rfh+cv->ras+1,cv->ruler_linger_lines[i],-1,measuretoolwindowforegroundcol);
332 break;
333 case et_mousedown:
334 // TBD
335 // cv->autonomous_ruler_w = false;
336 // GDrawDestroyWindow(gw);
337 // cv->ruler_linger_w = NULL;
338 break;
339 default: break;
340 }
341 return( true );
342 }
343
344 GResFont cv_measuretoolfont = GRESFONT_INIT("400 12px " MONO_UI_FAMILIES);
345
346 /*
347 * Comparison function for use with qsort.
348 */
349 static int BasePointCompare(const void *_l, const void *_r) {
350 const BasePoint *l = _l, *r = _r;
351 if ( l->x>r->x)
352 return( 1 );
353 if ( l->x<r->x)
354 return( -1 );
355 if ( l->y>r->y)
356 return( 1 );
357 if ( l->y<r->y)
358 return( -1 );
359 return( 0 );
360 }
361
362 static int ReverseBasePointCompare(const void *l, const void *r) {
363 return( -BasePointCompare(l,r) );
364 }
365
366 /*
367 * Fill buffer with intersects on a line (from,to).
368 * return number found, buf fill the buffer only up to a max_intersections.
369 *
370 * The points from and to are also put in the buffer.
371 *
372 * Copied somewhat from CVMouseUpKnife(), perhaps they should be consolidated
373 */
374 static int GetIntersections(CharView *cv,BasePoint from,BasePoint to,BasePoint *all_intersections,int max_intersections) {
375 SplineSet *spl;
376 Spline *s, *nexts;
377 Spline dummy;
378 SplinePoint dummyfrom, dummyto;
379 BasePoint inters[9]; /* These bounds are hard coded in the SplinesIntersect function */
380 extended t1s[10], t2s[10];
381 int i;
382 int total_intersections = 0;
383
384 memset(&dummy,0,sizeof(dummy));
385 memset(&dummyfrom,0,sizeof(dummyfrom));
386 memset(&dummyto,0,sizeof(dummyto));
387 dummyfrom.me.x = from.x; dummyfrom.me.y = from.y;
388 dummyto.me.x = to.x; dummyto.me.y = to.y;
389 dummyfrom.nextcp = dummyfrom.prevcp = dummyfrom.me;
390 dummyto.nextcp = dummyto.prevcp = dummyto.me;
391 dummyfrom.nonextcp = dummyfrom.noprevcp = dummyto.nonextcp = dummyto.noprevcp = true;
392 dummy.splines[0].d = from.x; dummy.splines[0].c = to.x-from.x;
393 dummy.splines[1].d = from.y; dummy.splines[1].c = to.y-from.y;
394 dummy.from = &dummyfrom; dummy.to = &dummyto;
395 dummy.islinear = dummy.knownlinear = true;
396 dummyfrom.next = dummyto.prev = &dummy;
397
398 all_intersections[total_intersections++] = from;
399
400 for ( spl = cv->b.layerheads[cv->b.drawmode]->splines; spl!=NULL; spl = spl->next ) {
401 for ( s = spl->first->next; s!=NULL ; ) {
402 nexts = NULL;
403 if ( s->to!=spl->first )
404 nexts = s->to->next;
405
406 if ( SplinesIntersect(s,&dummy,inters,t1s,t2s)>0 ) {
407 /* TBD, why is 4 used here (copied from CVMouseUpKnife()) */
408 for ( i=0; i<4 && t1s[i]!=-1; ++i ) {
409 if ( (total_intersections+1)<max_intersections )
410 all_intersections[total_intersections] = inters[i];
411 total_intersections++;
412 }
413 }
414 s = nexts;
415 }
416 }
417
418 if ( total_intersections<max_intersections )
419 all_intersections[total_intersections++] = to;
420
421 qsort(all_intersections,
422 total_intersections > max_intersections ? max_intersections : total_intersections,
423 sizeof(all_intersections[0]),
424 BasePointCompare(&from,&to)<=0 ? BasePointCompare : ReverseBasePointCompare );
425
426 /*
427 * Filter out intersectsions that are too close.
428 * This is for snapped points, but we get more than one extra per snap,
429 * so do them all for now.
430 */
431 for ( i = 1 ; i<total_intersections && i<max_intersections ; ) {
432 if ( (0.00001 > fabs(all_intersections[i].x-all_intersections[i-1].x)) &&
433 (0.00001 > fabs(all_intersections[i].y-all_intersections[i-1].y)) ) {
434 int j;
435
436 for( j = i+1 ; j<total_intersections && j<max_intersections ; j++ )
437 all_intersections[j-1] = all_intersections[j];
438 if ( total_intersections < max_intersections )
439 total_intersections--;
440 } else {
441 i++;
442 }
443 }
444
445 return( total_intersections ); /* note that it could be greater than max */
446 }
447
448 static void RulerPlace(CharView *cv, GEvent *event) {
449 unichar_t ubuf[80];
450 int width, x, y;
451 GRect size;
452 GPoint pt;
453 int i,h,w;
454 GWindowAttrs wattrs;
455 GRect pos;
456 int as, ds, ld;
457
458 if ( cv->ruler_w==NULL ) {
459 memset(&wattrs,0,sizeof(wattrs));
460 wattrs.mask = wam_events|wam_cursor|wam_positioned|wam_nodecor|wam_backcol|wam_bordwidth;
461 wattrs.event_masks = (1<<et_expose)|(1<<et_resize)|(1<<et_mousedown);
462 wattrs.cursor = ct_mypointer;
463 wattrs.background_color = measuretoolwindowbackgroundcol;
464 wattrs.nodecoration = 1;
465 wattrs.border_width = 1;
466 pos.x = pos.y = 0; pos.width=pos.height = 20;
467 cv->ruler_w = GWidgetCreateTopWindow(NULL,&pos,ruler_e_h,cv,&wattrs);
468
469 cv->rfont = cv_measuretoolfont.fi;
470 GDrawWindowFontMetrics(cv->ruler_w,cv->rfont,&as,&ds,&ld);
471 cv->rfh = as+ds; cv->ras = as;
472 } else
473 GDrawRaise(cv->ruler_w);
474
475 if ( cv->p.pressed ) {
476 BasePoint from;
477
478 from.x = cv->p.cx;
479 from.y = cv->p.cy;
480
481 if ( !cv->ruler_intersections ) {
482 cv->allocated_ruler_intersections = 32;
483 cv->ruler_intersections = malloc(cv->allocated_ruler_intersections * sizeof(cv->ruler_intersections[0]));
484 }
485 for(;;) {
486 cv->num_ruler_intersections = GetIntersections(cv,from,cv->info,cv->ruler_intersections,cv->allocated_ruler_intersections);
487 if ( cv->num_ruler_intersections>cv->allocated_ruler_intersections ) {
488 cv->allocated_ruler_intersections = cv->num_ruler_intersections * 2;
489 cv->ruler_intersections = realloc(cv->ruler_intersections,cv->allocated_ruler_intersections * sizeof(cv->ruler_intersections[0]));
490 } else
491 break;
492 }
493 }
494
495 GDrawSetFont(cv->ruler_w,cv->rfont);
496 width = h = 0;
497 for ( i=0; RulerText(cv,ubuf,i); ++i ) {
498 w = GDrawGetTextWidth(cv->ruler_w,ubuf,-1);
499 if ( w>width ) width = w;
500 h += cv->rfh;
501 }
502 if ( cv->p.pressed ) for ( i=0; RulerTextIntersection(cv,ubuf,i); ++i ) {
503 w = GDrawGetTextWidth(cv->ruler_w,ubuf,-1);
504 if ( w>width ) width = w;
505 h += cv->rfh;
506 }
507
508 GDrawGetSize(GDrawGetRoot(NULL),&size);
509 pt.x = event->u.mouse.x; pt.y = event->u.mouse.y;
510 GDrawTranslateCoordinates(cv->v,GDrawGetRoot(NULL),&pt);
511 x = pt.x + infowindowdistance;
512 if ( x+width > size.width )
513 x = pt.x - width-infowindowdistance;
514 y = pt.y -cv->ras-2;
515 if ( y+h > size.height )
516 y = pt.y - h - cv->ras -10;
517 GDrawMoveResize(cv->ruler_w,x,y,width+4,h+4);
518 }
519
520 static void RulerLingerPlace(CharView *cv, GEvent *event) {
521 int width, x, y;
522 GRect size;
523 GPoint pt;
524 int i,h,w;
525 GWindowAttrs wattrs;
526 GRect pos;
527 int as, ds, ld;
528 int line;
529 int old_pressed;
530
531 if ( cv->ruler_linger_w==NULL ) {
532 memset(&wattrs,0,sizeof(wattrs));
533 wattrs.mask = wam_events|wam_cursor|wam_positioned|wam_nodecor|wam_backcol|wam_bordwidth;
534 wattrs.event_masks = (1<<et_expose)|(1<<et_resize)|(1<<et_mousedown);
535 wattrs.cursor = ct_mypointer;
536 wattrs.background_color = measuretoolwindowbackgroundcol;
537 wattrs.nodecoration = 1;
538 wattrs.border_width = 1;
539 pos.x = pos.y = 0; pos.width=pos.height = 20;
540 cv->ruler_linger_w = GWidgetCreateTopWindow(NULL,&pos,ruler_linger_e_h,cv,&wattrs);
541
542 cv->rfont = cv_measuretoolfont.fi;
543 GDrawWindowFontMetrics(cv->ruler_linger_w,cv->rfont,&as,&ds,&ld);
544 cv->rfh = as+ds; cv->ras = as;
545 } else
546 GDrawRaise(cv->ruler_linger_w);
547
548 GDrawSetFont(cv->ruler_linger_w,cv->rfont);
549 width = h = 0;
550 line = 0;
551 old_pressed = cv->p.pressed;
552 cv->p.pressed = true;
553
554 for ( i=0; line<sizeof(cv->ruler_linger_lines)/sizeof(cv->ruler_linger_lines[0]) && RulerText(cv,cv->ruler_linger_lines[line],i) ; ++i,++line ) {
555 w = GDrawGetTextWidth(cv->ruler_linger_w,cv->ruler_linger_lines[line],-1);
556 if ( w>width ) width = w;
557 h += cv->rfh;
558 }
559 cv->p.pressed = old_pressed;
560 for ( i=0; line<sizeof(cv->ruler_linger_lines)/sizeof(cv->ruler_linger_lines[0]) && RulerTextIntersection(cv,cv->ruler_linger_lines[line],i); ++i,++line ) {
561 w = GDrawGetTextWidth(cv->ruler_linger_w,cv->ruler_linger_lines[line],-1);
562 if ( w>width ) width = w;
563 h += cv->rfh;
564 }
565 cv->ruler_linger_num_lines = line;
566
567 GDrawGetSize(GDrawGetRoot(NULL),&size);
568 pt.x = event->u.mouse.x; pt.y = event->u.mouse.y;
569 GDrawTranslateCoordinates(cv->v,GDrawGetRoot(NULL),&pt);
570 x = pt.x + infowindowdistance;
571 if ( x+width > size.width )
572 x = pt.x - width-infowindowdistance;
573 y = pt.y -cv->ras-2;
574 if ( y+h > size.height )
575 y = pt.y - h - cv->ras -10;
576 GDrawMoveResize(cv->ruler_linger_w,x,y,width+4,h+4);
577 GDrawSetVisible(cv->ruler_linger_w,true);
578 }
579
580 void CVRulerLingerMove(CharView *cv) {
581 CharViewTab* tab = CVGetActiveTab(cv);
582 if ( cv->ruler_linger_w ) {
583 int x, y;
584 GRect size;
585 GRect rsize;
586 GRect csize;
587 GPoint pt;
588
589 GDrawGetSize(GDrawGetRoot(NULL),&size);
590 GDrawGetSize(cv->ruler_linger_w,&rsize);
591 GDrawGetSize(cv->gw,&csize);
592
593 pt.x = tab->xoff + rint(cv->ruler_intersections[cv->num_ruler_intersections-1].x*tab->scale);
594 pt.y = -tab->yoff + cv->height - rint(cv->ruler_intersections[cv->num_ruler_intersections-1].y*tab->scale);
595 GDrawTranslateCoordinates(cv->v,GDrawGetRoot(NULL),&pt);
596 x = pt.x + infowindowdistance;
597 if ( x+rsize.width>size.width )
598 x = pt.x - rsize.width-infowindowdistance;
599 y = pt.y -cv->ras-2;
600 if ( y+rsize.height>size.height )
601 y = pt.y - rsize.height - cv->ras -10;
602
603 if ( x>=csize.x && x<=(csize.x+csize.width) && y>=csize.y && y<=(csize.y+csize.height) ) {
604 GDrawMove(cv->ruler_linger_w,x,y);
605 GDrawSetVisible(cv->ruler_linger_w,true);
606 } else {
607 GDrawSetVisible(cv->ruler_linger_w,false);
608 }
609 }
610 }
611
612 void CVMouseDownRuler(CharView *cv, GEvent *event) {
613
614 cv->autonomous_ruler_w = false;
615
616 RulerPlace(cv,event);
617 cv->p.rubberlining = true;
618 GDrawSetVisible(cv->ruler_w,true);
619 if ( cv->ruler_linger_w ) {
620 GDrawDestroyWindow(cv->ruler_linger_w);
621 cv->ruler_linger_w = NULL;
622 }
623 }
624
625 void CVMouseMoveRuler(CharView *cv, GEvent *event) {
626 if ( cv->autonomous_ruler_w )
627 return;
628
629 if ( !cv->p.pressed && (event->u.mouse.state&ksm_meta) ) {
630 if ( cv->ruler_w!=NULL && GDrawIsVisible(cv->ruler_w)) {
631 GDrawDestroyWindow(cv->ruler_w);
632 cv->ruler_w = NULL;
633 }
634 return;
635 }
636 if ( !cv->p.pressed )
637 CVMouseAtSpline(cv,event);
638 RulerPlace(cv,event);
639 if ( !cv->p.pressed )
640 GDrawSetVisible(cv->ruler_w,true);
641 GDrawSync(NULL);
642 // The following code may be unnecessary, and it can cause an infinite stack loop.
643 // One would hope that the event queue can take care of these things when we return to it.
644 // We'll find out.
645 //#if 0
646 // GDrawProcessPendingEvents(NULL); /* The resize needs to happen before the expose */
647 // if ( !cv->p.pressed && (event->u.mouse.state&ksm_meta) ) /* but a mouse up might sneak in... */
648 //return;
649 GDrawRequestExpose(cv->ruler_w,NULL,false);
650 if (cv->p.pressed)
651 GDrawRequestExpose(cv->v,NULL,false);
652 //#endif // 0
653 }
654
655 void CVMouseUpRuler(CharView *cv, GEvent *event) {
656 if ( cv->ruler_w!=NULL ) {
657 GRect size;
658
659 last_ruler_offset[1] = last_ruler_offset[0];
660 last_ruler_offset[0].x = cv->info.x-cv->p.cx;
661 last_ruler_offset[0].y = cv->info.y-cv->p.cy;
662
663 /* if we have gone out of bounds abandon the window */
664 GDrawGetSize(cv->v,&size);
665 if ( event->u.mouse.x<0 || event->u.mouse.y<0 || event->u.mouse.x>=size.width || event->u.mouse.y>=size.height ) {
666 GDrawDestroyWindow(cv->ruler_w);
667 cv->ruler_w = NULL;
668 return;
669 }
670
671 if ( !(event->u.mouse.state & ksm_meta) ) {
672 /*cv->autonomous_ruler_w = true;*/
673
674 if ( cv->ruler_linger_w ) {
675 GDrawDestroyWindow(cv->ruler_linger_w);
676 cv->ruler_linger_w = NULL;
677 }
678 if ( cv->num_ruler_intersections>1 ) {
679 RulerLingerPlace(cv,event);
680 }
681 return;
682 }
683
684 GDrawDestroyWindow(cv->ruler_w);
685 cv->ruler_w = NULL;
686 }
687 }
688
689 /* ************************************************************************** */
690
691 static char *PtInfoText(CharView *cv, int lineno, int active, char *buffer, int blen) {
692 BasePoint *cp;
693 double t;
694 Spline *s;
695 SplinePoint *sp = cv->p.sp;
696 extern char *coord_sep;
697 double dx, dy, kappa, kappa2;
698 int emsize;
699
700 if ( !cv->p.prevcp && !cv->p.nextcp ) {
701 sp = cv->active_sp;
702 if ( sp==NULL )
703 return( NULL );
704 }
705
706 if ( active==-1 ) {
707 if ( lineno>0 )
708 return( NULL );
709 if ( sp->next==NULL || sp->prev==NULL )
710 return( NULL );
711 kappa = SplineCurvature(sp->next,0);
712 kappa2 = SplineCurvature(sp->prev,1);
713 emsize = cv->b.sc->parent->ascent + cv->b.sc->parent->descent;
714 if ( kappa == CURVATURE_ERROR || kappa2 == CURVATURE_ERROR )
715 strncpy(buffer,_("No curvature info"), blen);
716 else
717 snprintf( buffer, blen, U_("∆Curvature: %g"), (kappa-kappa2)*emsize );
718 return( buffer );
719 }
720
721 if ( (!cv->p.prevcp && active ) || (!cv->p.nextcp && !active)) {
722 cp = &sp->nextcp;
723 t = 0;
724 s = sp->next;
725 } else {
726 cp = &sp->prevcp;
727 t = 1;
728 s = sp->prev;
729 }
730
731 switch( lineno ) {
732 case 0:
733 if ( t==0 )
734 strncpy( buffer, _(" Next CP"), blen);
735 else
736 strncpy( buffer, _(" Prev CP"), blen);
737 break;
738 case 1:
739 snprintf( buffer, blen, "(%g%s%g)", (double) cp->x, coord_sep, (double) cp->y);
740 break;
741 case 2:
742 snprintf( buffer, blen, "∆ (%g%s%g)", (double) (cp->x-sp->me.x), coord_sep, (double) (cp->y-sp->me.y));
743 break;
744 case 3:
745 dx = cp->x - sp->me.x; dy = cp->y - sp->me.y;
746 if ( dx==0 && dy==0 )
747 snprintf( buffer, blen, "%s", _("No Slope") );
748 else if ( dx==0 )
749 snprintf( buffer, blen, "∆y/∆x= ∞" );
750 else
751 snprintf( buffer, blen, "∆y/∆x= %g", dy/dx );
752 break;
753 case 4:
754 dx = cp->x - sp->me.x; dy = cp->y - sp->me.y;
755 snprintf( buffer, blen, "∠ %g°", atan2(dy,dx)*180/FF_PI );
756 break;
757 case 5:
758 if ( s==NULL )
759 return( NULL );
760 kappa = SplineCurvature(s,t);
761 if ( kappa==CURVATURE_ERROR )
762 return( NULL );
763 emsize = cv->b.sc->parent->ascent + cv->b.sc->parent->descent;
764 /* If we normalize by the em-size, the curvature is often more */
765 /* readable */
766 snprintf( buffer, blen, _("Curvature: %g"), kappa*emsize);
767 break;
768 default:
769 return( NULL );
770 }
771 return( buffer );
772 }
773
774 static int cpinfo_e_h(GWindow gw, GEvent *event) {
775 CharView *cv = (CharView *) GDrawGetUserData(gw);
776 char buf[100];
777 int line, which, y;
778
779 switch ( event->type ) {
780 case et_expose:
781 y = cv->ras+1;
782 GDrawSetFont(gw,cv->rfont);
783 for ( which = 1; which>=0; --which ) {
784 for ( line=0; PtInfoText(cv,line,which,buf,sizeof(buf))!=NULL; ++line ) {
785 GDrawDrawText8(gw,2,y,buf,-1,measuretoolwindowforegroundcol);
786 y += cv->rfh+1;
787 }
788 GDrawDrawLine(gw,0,y+2-cv->ras,2000,y+2-cv->ras,measuretoolwindowforegroundcol);
789 y += 4;
790 }
791 if ( PtInfoText(cv,0,-1,buf,sizeof(buf))!=NULL )
792 GDrawDrawText8(gw,2,y,buf,-1,measuretoolwindowforegroundcol);
793 break;
794 default: break;
795 }
796 return( true );
797 }
798
799 static void CpInfoPlace(CharView *cv, GEvent *event) {
800 CharViewTab* tab = CVGetActiveTab(cv);
801 char buf[100];
802 int line, which;
803 int width, x, y;
804 GRect size;
805 GPoint pt, pt2;
806 int h,w;
807 GWindowAttrs wattrs;
808 GRect pos;
809 int as, ds, ld;
810 SplinePoint *sp;
811
812 if ( cv->ruler_w==NULL ) {
813 memset(&wattrs,0,sizeof(wattrs));
814 wattrs.mask = wam_events|wam_cursor|wam_positioned|wam_nodecor|wam_backcol|wam_bordwidth;
815 wattrs.event_masks = (1<<et_expose)|(1<<et_resize)|(1<<et_mousedown);
816 wattrs.cursor = ct_mypointer;
817 wattrs.background_color = measuretoolwindowbackgroundcol;
818 wattrs.nodecoration = 1;
819 wattrs.border_width = 1;
820 pos.x = pos.y = 0; pos.width=pos.height = 20;
821 cv->ruler_w = GWidgetCreateTopWindow(NULL,&pos,cpinfo_e_h,cv,&wattrs);
822
823 cv->rfont = cv_measuretoolfont.fi;
824 GDrawWindowFontMetrics(cv->ruler_w,cv->rfont,&as,&ds,&ld);
825 cv->rfh = as+ds; cv->ras = as;
826 } else
827 GDrawRaise(cv->ruler_w);
828
829 GDrawSetFont(cv->ruler_w,cv->rfont);
830 h = 0; width = 0;
831 for ( which = 0; which<2; ++which ) {
832 for ( line=0; PtInfoText(cv,line,which,buf,sizeof(buf))!=NULL; ++line ) {
833 w = GDrawGetText8Width(cv->ruler_w,buf,-1);
834 if ( w>width ) width = w;
835 h += cv->rfh+1;
836 }
837 h += 4;
838 }
839 if ( PtInfoText(cv,0,-1,buf,sizeof(buf))!=NULL ) {
840 w = GDrawGetText8Width(cv->ruler_w,buf,-1);
841 if ( w>width ) width = w;
842 h += cv->rfh+1;
843 }
844
845 GDrawGetSize(GDrawGetRoot(NULL),&size);
846 pt.x = event->u.mouse.x; pt.y = event->u.mouse.y; /* Address of cp */
847 GDrawTranslateCoordinates(cv->v,GDrawGetRoot(NULL),&pt);
848
849 sp = cv->p.sp;
850 if ( !cv->p.prevcp && !cv->p.nextcp )
851 sp = cv->active_sp;
852 if ( sp!=NULL ) {
853 x = tab->xoff + rint(sp->me.x*tab->scale);
854 y = -tab->yoff + cv->height - rint(sp->me.y*tab->scale);
855 if ( x>=0 && y>=0 && x<cv->width && y<cv->height ) {
856 pt2.x = x; pt2.y = y;
857 GDrawTranslateCoordinates(cv->v,GDrawGetRoot(NULL),&pt2);
858 } else
859 sp = NULL;
860 }
861
862 x = pt.x + infowindowdistance;
863 y = pt.y - cv->ras-2;
864 if ( sp!=NULL && x<=pt2.x-4 && x+width>=pt2.x+4 && y<=pt2.y-4 && y+h>=pt2.y+4 )
865 x = pt2.x + 4;
866 if ( x+width > size.width ) {
867 x = pt.x - width-30;
868 if ( sp!=NULL && x<=pt2.x-4 && x+width>=pt2.x+4 && y<=pt2.y-4 && y+h>=pt2.y+4 )
869 x = pt2.x - width - 4;
870 if ( x<0 ) {
871 x = pt.x + 10;
872 y = pt.y - h - infowindowdistance;
873 if ( sp!=NULL && x<=pt2.x-4 && x+width>=pt2.x+4 && y<=pt2.y-4 && y+h>=pt2.y+4 )
874 y = pt2.y - h - 4;
875 if ( y<0 )
876 y = pt.y+infowindowdistance; /* If this doesn't work we have nowhere else to */
877 /* try so don't check */
878 }
879 }
880 if ( y+h > size.height )
881 y = pt.y - h - cv->ras - 10;
882 GDrawMoveResize(cv->ruler_w,x,y,width+4,h+4);
883 }
884
885 void CPStartInfo(CharView *cv, GEvent *event) {
886 // printf("CPStartInfo() showcp:%d pressed:%d rw:%p\n", cv->showcpinfo, cv->p.pressed, cv->ruler_w );
887
888 if ( !cv->showcpinfo )
889 return;
890 cv->autonomous_ruler_w = false;
891
892 CpInfoPlace(cv,event);
893 GDrawSetVisible(cv->ruler_w,true);
894 }
895
896 void CPUpdateInfo(CharView *cv, GEvent *event) {
897
898 // printf("CPUpdateInfo() showcp:%d pressed:%d rw:%p\n", cv->showcpinfo, cv->p.pressed, cv->ruler_w );
899
900 if ( !cv->showcpinfo )
901 return;
902 if ( !cv->p.pressed ) {
903 if ( cv->ruler_w!=NULL && GDrawIsVisible(cv->ruler_w)) {
904 GDrawDestroyWindow(cv->ruler_w);
905 cv->ruler_w = NULL;
906 }
907 return;
908 }
909 if ( cv->ruler_w==NULL )
910 CPStartInfo(cv,event);
911 else {
912 CpInfoPlace(cv,event);
913 GDrawSync(NULL);
914 GDrawProcessPendingEvents(NULL); /* The resize needs to happen before the expose */
915 if ( !cv->p.pressed ) /* but a mouse up might sneak in... */
916 return;
917 GDrawRequestExpose(cv->ruler_w,NULL,false);
918 }
919 }
920
921 void CPEndInfo(CharView *cv) {
922 if ( cv->ruler_w!=NULL ) {
923 if ( !cv->p.pressed ) {
924 GDrawDestroyWindow(cv->ruler_w);
925 cv->ruler_w = NULL;
926 }
927 }
928 /* TBD, wrong time to kill? */
929 if ( cv->ruler_linger_w!=NULL && cv->b1_tool!=cvt_ruler && cv->b1_tool_old!=cvt_ruler ) {
930 GDrawDestroyWindow(cv->ruler_linger_w);
931 cv->ruler_linger_w = NULL;
932 }
933 }
934
935 void CVRulerExpose(GWindow pixmap,CharView *cv) {
936 CharViewTab* tab = CVGetActiveTab(cv);
937
938 if ( cv->b1_tool!=cvt_ruler && cv->b1_tool_old!=cvt_ruler ) {
939 cv->num_ruler_intersections = 0;
940 return;
941 }
942
943 if ( cv->num_ruler_intersections >= 2 ) {
944 int x = tab->xoff + rint(cv->ruler_intersections[0].x*tab->scale);
945 int y = -tab->yoff + cv->height - rint(cv->ruler_intersections[0].y*tab->scale);
946 int xend = tab->xoff + rint(cv->ruler_intersections[cv->num_ruler_intersections-1].x*tab->scale);
947 int yend = -tab->yoff + cv->height - rint(cv->ruler_intersections[cv->num_ruler_intersections-1].y*tab->scale);
948 real xdist = fabs(cv->ruler_intersections[0].x - cv->ruler_intersections[cv->num_ruler_intersections-1].x);
949 real ydist = fabs(cv->ruler_intersections[0].y - cv->ruler_intersections[cv->num_ruler_intersections-1].y);
950 int i;
951 int len;
952 int charwidth = 6; /* TBD */
953 Color textcolor = (cv->start_intersection_snapped && cv->end_intersection_snapped) ? measuretoolcanvasnumberssnappedcol : measuretoolcanvasnumberscol;
954 GRect prev_rect;
955
956 if ( measuretoolshowhorizontolvertical ) {
957 char buf[40];
958 unichar_t ubuf[40];
959
960 if ( xdist*tab->scale>10.0 && ydist*tab->scale>10.0 ) {
961
962 GDrawSetFont(pixmap,cv->rfont);
963 len = snprintf(buf,sizeof buf,"%g",xdist);
964 utf82u_strcpy(ubuf,buf);
965 GDrawDrawText(pixmap,(x+xend)/2 - len*charwidth/2,y + (y > yend ? 12 : -5),ubuf,-1,textcolor);
966 GDrawDrawLine(pixmap,x,y,xend,y,measuretoollinecol);
967
968 len = snprintf(buf,sizeof buf,"%g",ydist);
969 utf82u_strcpy(ubuf,buf);
970 GDrawDrawText(pixmap,xend + (x < xend ? charwidth/2 : -(len * charwidth + charwidth/2)),(y+yend)/2,ubuf,-1,textcolor);
971 GDrawDrawLine(pixmap,xend,y,xend,yend,measuretoollinecol);
972 }
973 }
974
975 if ( !cv->p.rubberlining ) {
976 GDrawDrawLine(pixmap,x,y,xend,yend,measuretoollinecol);
977 }
978
979 GDrawSetFont(pixmap,cv->rfont);
980 for ( i=0 ; i<cv->num_ruler_intersections; ++i ) {
981 GRect rect;
982
983 rect.x = tab->xoff + rint(cv->ruler_intersections[i].x*tab->scale) - 1;
984 rect.y = -tab->yoff + cv->height - rint(cv->ruler_intersections[i].y*tab->scale) - 1;
985 rect.width = 3;
986 rect.height = 3;
987
988 GDrawFillElipse(pixmap,&rect,((i==(cv->num_ruler_intersections-1) && cv->info_sp) || (i==0 && cv->p.sp)) ? measuretoolpointsnappedcol : measuretoolpointcol);
989 if ( i>0 && (cv->num_ruler_intersections<6 || (prev_rect.x + 10)<rect.x || (prev_rect.y + 10)<rect.y || (prev_rect.y - 10)>rect.y) ) {
990 real xoff = cv->ruler_intersections[i].x - cv->ruler_intersections[i-1].x;
991 real yoff = cv->ruler_intersections[i].y - cv->ruler_intersections[i-1].y;
992 real len = sqrt(xoff*xoff+yoff*yoff);
993 char buf[40];
994 unichar_t ubuf[40];
995 int x,y;
996
997 x = (prev_rect.x + rect.x)/2;
998 y = (prev_rect.y + rect.y)/2;
999
1000 len = snprintf(buf,sizeof buf,"%g",len);
1001 utf82u_strcpy(ubuf,buf);
1002 GDrawDrawText(pixmap,x + (x < xend ? -(len*charwidth) : charwidth/2 ),y + (y < yend ? 12 : -5),ubuf,-1,textcolor);
1003 }
1004 prev_rect = rect;
1005 }
1006 }
1007 }