Debian Codegraph @

fontforgeexe/anchorsaway.c

raw symbols SCIP
1 /* Copyright (C) 2005-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 "fontforgeui.h"
31 #include "fvfonts.h"
32 #include "gkeysym.h"
33 #include "splinefill.h"
34 #include "splineutil.h"
35 #include "ustring.h"
36 #include "utype.h"
37
38 #include <math.h>
39 #include <string.h>
40
41 int aa_pixelsize = 150;
42
43 /* The dialog should contain a lig index !!!! */
44
45 struct apmatch {
46 SplineChar *sc;
47 AnchorPoint *ap;
48 BDFChar *bdfc;
49 int off; /* a mark-to-mark display might have the same */
50 int size; /* odd positioning problems as above */
51 int xstart; /* This is scaled by factor, others are not */
52 };
53
54 struct state {
55 SplineChar *sc;
56 int changed;
57 AnchorPoint *ap_pt;
58 AnchorPoint ap_vals;
59 struct state *next;
60 };
61
62 typedef struct anchord {
63 GWindow gw;
64 int ctl_len;
65 GGadget *hsb;
66 int sb_base;
67 int sb_width;
68 int sb_height;
69 int full_width, full_height;
70
71 SplineChar *sc; /* This is the character whose anchor we are */
72 AnchorPoint *ap; /* interested in, and this is that anchor */
73 BasePoint apos; /* Modified anchor position */
74 DeviceTable xadjust; /* Modified adjustments */
75 DeviceTable yadjust;
76 BDFChar *bdfc;
77 int char_off; /* Mark characters are often positioned oddly */
78 int char_size; /* and have zero advance widths, this corrects*/
79 int ymin, ymax; /* For all combos */
80
81 int pixelsize;
82 double scale; /* Does not include mag factor */
83 int magfactor;
84 int baseline;
85
86 int xoff; /* for scroll bar */
87 int xlen; /* sum of all the following lengths */
88 int cnt; /* Number of anchors we join with */
89 /* joining can happen in four ways: */
90 /* we might be a base char, we see all the accents */
91 /* (all apmatches will be the same length (bdfc->width above)*/
92 /* we might be a mark, we see all the bases (ligs may happen twice) */
93 /* (each base provides the length) */
94 /* we might be cursive entry, we see all the exits that attach */
95 /* (length is bdfc->width + apmatch->width */
96 /* left to right/right to left is important here! */
97 /* we might be cursive exit, we see all the entries that attach */
98 struct apmatch *apmatch;
99 void *freetypecontext;
100 int combo, on_ap;
101 BasePoint orig_pos;
102 int done;
103 /* If they change more than one anchor, retain the original values so that */
104 /* we can revert to them */
105 struct state *orig_vals;
106 int layer;
107 } AnchorDlg;
108
109 extern int DeviceTableFind(DeviceTable *adjust,int pixelsize);
110
111 #define CID_X 1001
112 #define CID_Y 1002
113 #define CID_XCor 1003
114 #define CID_YCor 1004
115 #define CID_DisplaySize 1005
116 #define CID_Mag 1006
117 #define CID_Glyph 1007
118
119 #define Add_Mark (void *) (-1)
120 #define Add_Base (void *) (-3)
121
122 static void AnchorD_FreeChar(AnchorDlg *a) {
123 int i;
124
125 BDFCharFree(a->bdfc); a->bdfc = NULL;
126 for ( i=0; i<a->cnt; ++i )
127 BDFCharFree(a->apmatch[i].bdfc);
128 free(a->apmatch); a->apmatch = NULL;
129 if ( a->freetypecontext!=NULL ) {
130 FreeTypeFreeContext(a->freetypecontext);
131 a->freetypecontext = NULL;
132 }
133 }
134
135 static void AnchorD_FreeAll(AnchorDlg *a) {
136 struct state *old, *oldnext;
137
138 free(a->xadjust.corrections);
139 free(a->yadjust.corrections);
140 for ( old = a->orig_vals; old!=NULL; old=oldnext ) {
141 oldnext = old->next;
142 chunkfree(old,sizeof(struct state));
143 }
144 AnchorD_FreeChar(a);
145 }
146
147 static GTextInfo **AnchorD_GlyphsInClass(AnchorDlg *a) {
148 SplineFont *_sf = a->sc->parent, *sf;
149 AnchorClass *ac = a->ap->anchor;
150 int bcnt, mcnt, btot=0, j, k, gid;
151 GTextInfo **ti;
152 AnchorPoint *ap;
153
154 if ( _sf->cidmaster!=NULL )
155 _sf = _sf->cidmaster;
156 for ( j=0; j<2; ++j ) {
157 k = 0;
158 bcnt = mcnt = 1; /* Reserve two spots for labels (marks vs base glyphs) */
159 do {
160 sf = _sf->subfontcnt==0 ? _sf : _sf->subfonts[k];
161 for ( gid=0; gid<sf->glyphcnt; ++gid ) if ( sf->glyphs[gid]!=NULL ) {
162 for ( ap= sf->glyphs[gid]->anchor; ap!=NULL; ap=ap->next ) {
163 if ( ap->anchor == ac ) {
164 if ( ap->type==at_mark || ap->type == at_centry ) {
165 if ( j ) {
166 ti[btot+1+mcnt] = calloc(1,sizeof(GTextInfo));
167 ti[btot+1+mcnt]->text = utf82u_copy(sf->glyphs[gid]->name);
168 ti[btot+1+mcnt]->fg = ti[btot+1+mcnt]->bg = COLOR_DEFAULT;
169 ti[btot+1+mcnt]->userdata = ap;
170 ti[btot+1+mcnt]->selected = a->ap == ap;
171 }
172 ++mcnt;
173 } else {
174 if ( j ) {
175 ti[bcnt] = calloc(1,sizeof(GTextInfo));
176 ti[bcnt]->text = utf82u_copy(sf->glyphs[gid]->name);
177 ti[bcnt]->fg = ti[bcnt]->bg = COLOR_DEFAULT;
178 ti[bcnt]->userdata = ap;
179 ti[bcnt]->selected = a->ap == ap;
180 }
181 ++bcnt;
182 }
183 }
184 }
185 }
186 ++k;
187 } while ( k<_sf->subfontcnt );
188 if ( !j ) {
189 btot = bcnt;
190 ti = calloc(bcnt+mcnt+5,sizeof(GTextInfo*));
191 ti[0] = calloc(1,sizeof(GTextInfo));
192 ti[0]->text = utf82u_copy(ac->type==act_curs ? _("Exits") : _("Bases"));
193 ti[0]->fg = ti[0]->bg = COLOR_DEFAULT;
194 ti[0]->disabled = true;
195 ti[btot] = calloc(1,sizeof(GTextInfo));
196 ti[btot]->line = true;
197 ti[btot]->fg = ti[btot]->bg = COLOR_DEFAULT;
198 ti[btot+1] = calloc(1,sizeof(GTextInfo));
199 ti[btot+1]->text = utf82u_copy(ac->type==act_curs ? _("Entries") : _("Marks"));
200 ti[btot+1]->fg = ti[btot+1]->bg = COLOR_DEFAULT;
201 ti[btot+1]->disabled = true;
202 ti[btot+mcnt+1] = calloc(1,sizeof(GTextInfo));
203 ti[btot+mcnt+1]->line = true;
204 ti[btot+mcnt+1]->fg = ti[btot+mcnt+1]->bg = COLOR_DEFAULT;
205 ti[btot+mcnt+2] = calloc(1,sizeof(GTextInfo));
206 ti[btot+mcnt+2]->text = utf82u_copy(ac->type==act_curs ? _("Add Exit Anchor...") : _("Add Base Anchor..."));
207 ti[btot+mcnt+2]->fg = ti[btot+mcnt+2]->bg = COLOR_DEFAULT;
208 ti[btot+mcnt+2]->userdata = Add_Base;
209 ti[btot+mcnt+3] = calloc(1,sizeof(GTextInfo));
210 ti[btot+mcnt+3]->text = utf82u_copy(ac->type==act_curs ? _("Add Entry Anchor...") : _("Add Mark Anchor..."));
211 ti[btot+mcnt+3]->fg = ti[btot+mcnt+3]->bg = COLOR_DEFAULT;
212 ti[btot+mcnt+3]->userdata = Add_Mark;
213 ti[btot+mcnt+4] = calloc(1,sizeof(GTextInfo));
214 }
215 }
216 return( ti );
217 }
218
219 static void AnchorD_SetTitle(AnchorDlg *a) {
220 char buffer[300];
221 snprintf(buffer,sizeof(buffer),_("Anchor Control for class %.100s in glyph %.100s as %.20s"),
222 a->ap->anchor->name, a->sc->name,
223 a->ap->type == at_mark ? _("mark") :
224 a->ap->type == at_centry ? _("cursive entry") :
225 a->ap->type == at_cexit ? _("cursive exit") :
226 _("base") );
227 GDrawSetWindowTitles8(a->gw,buffer,_("Anchor Control"));
228 }
229
230 static void AnchorD_FindComplements(AnchorDlg *a) {
231 AnchorClass *ac = a->ap->anchor;
232 enum anchor_type match;
233 AnchorPoint *ap;
234 int i, k, j, cnt;
235 SplineFont *_sf = a->sc->parent, *sf;
236 uint8_t *sel, *oldsel;
237 FontView *fv = (FontView *) _sf->fv;
238 EncMap *map = fv->b.map;
239
240 switch ( a->ap->type ) {
241 case at_mark:
242 switch ( a->ap->anchor->type ) {
243 case act_mark:
244 match = at_basechar;
245 break;
246 case act_mklg:
247 match = at_baselig;
248 break;
249 case act_mkmk:
250 match = at_basemark;
251 break;
252 default:
253 IError( "Unexpected anchor class type" );
254 match = at_basechar;
255 }
256 break;
257 case at_basechar: case at_baselig: case at_basemark:
258 match = at_mark;
259 break;
260 case at_centry:
261 match = at_cexit;
262 break;
263 case at_cexit:
264 match = at_centry;
265 break;
266 default:
267 match = at_max;
268 break;
269 }
270
271 if ( _sf->cidmaster!=NULL )
272 _sf = _sf->cidmaster;
273 for ( j=0; j<2; ++j ) {
274 k = cnt = 0;
275 do {
276 sf = _sf->subfontcnt==0 ? _sf : _sf->subfonts[k];
277 for ( i=0; i<sf->glyphcnt; ++i ) if ( sf->glyphs[i]!=NULL ) {
278 for ( ap= sf->glyphs[i]->anchor; ap!=NULL; ap=ap->next ) {
279 if ( ap->anchor == ac && ap->type==match ) {
280 if ( j ) {
281 a->apmatch[cnt].ap = ap;
282 a->apmatch[cnt++].sc = sf->glyphs[i];
283 } else
284 ++cnt;
285 /* Don't break out of the loop as ligatures can have multiple locations */
286 }
287 }
288 }
289 ++k;
290 } while ( k<_sf->subfontcnt );
291 a->cnt = cnt;
292 if ( cnt==0 )
293 break;
294 if ( j==0 )
295 a->apmatch = calloc(cnt,sizeof(struct apmatch));
296 }
297
298 if ( hasFreeType() && _sf->subfontcnt==0 ) {
299 int enc = map->backmap[a->sc->orig_pos];
300 if ( enc!=-1 ) {
301 sel = calloc(map->enccount,1);
302 sel[enc] = true;
303 for ( i=0; i<sf->glyphcnt; ++i ) if ( sf->glyphs[i]!=NULL ) {
304 enc = map->backmap[i];
305 if ( enc!=-1 ) {
306 for ( ap= sf->glyphs[i]->anchor; ap!=NULL; ap=ap->next ) {
307 if ( ap->anchor == ac && ap->type==match ) {
308 sel[enc] = true;
309 break;
310 }
311 }
312 }
313 }
314 oldsel = fv->b.selected;
315 fv->b.selected = sel;
316 a->freetypecontext = FreeTypeFontContext(_sf,NULL,(FontViewBase *) fv,a->layer);
317 fv->b.selected = oldsel;
318 free(sel);
319 }
320 }
321 }
322
323 static BDFChar *APRasterize(void *freetypecontext, SplineChar *sc,int layer, int *off,int *size,int pixelsize) {
324 BDFChar *bdfc;
325
326 if ( freetypecontext ) {
327 bdfc = SplineCharFreeTypeRasterize(freetypecontext,sc->orig_pos,pixelsize,72,8);
328 } else
329 bdfc = SplineCharAntiAlias(sc,layer,pixelsize,4);
330
331 if ( bdfc->xmin<=0 ) {
332 *off = 1-bdfc->xmin;
333 if ( bdfc->width>bdfc->xmax-bdfc->xmin )
334 *size = bdfc->width;
335 else
336 *size = bdfc->xmax + *off;
337 } else {
338 *off = 0;
339 if ( bdfc->width>bdfc->xmax )
340 *size = bdfc->width;
341 else
342 *size = bdfc->xmax + 1;
343 }
344 ++*size;
345 return( bdfc );
346 }
347
348 static void AnchorD_SetSB(AnchorDlg *a) {
349 GScrollBarSetBounds(a->hsb,0,a->xlen,a->sb_width);
350 if ( a->xoff+a->sb_width > a->xlen )
351 a->xoff = a->xlen - a->sb_width;
352 if ( a->xoff<0 ) a->xoff = 0;
353 GScrollBarSetPos(a->hsb,a->xoff);
354 }
355
356 static void AnchorD_Resize(AnchorDlg *a) {
357 GRect size;
358 int i, extra;
359 int factor = a->magfactor;
360
361 GDrawGetSize(a->gw,&size);
362 a->full_width = size.width;
363 a->full_height = size.height;
364 a->sb_base = a->full_height - a->sb_height;
365 if ( a->ctl_len+a->magfactor*a->char_size + 20 < a->full_width ) {
366 a->sb_width = a->full_width - (a->ctl_len+a->magfactor*a->char_size);
367 GGadgetResize(a->hsb,a->sb_width, a->sb_height);
368 GGadgetMove(a->hsb,a->ctl_len+a->magfactor*a->char_size,a->sb_base);
369 }
370 AnchorD_SetSB(a);
371
372 for ( i=0; i<a->cnt; ++i ) {
373 if ( i==0 )
374 a->apmatch[i].xstart = a->ctl_len+a->char_size*factor;
375 else
376 a->apmatch[i].xstart = a->apmatch[i-1].xstart +
377 a->apmatch[i-1].size*factor;
378 }
379 if ( i==0 )
380 a->xlen = 0;
381 else
382 a->xlen = a->apmatch[i-1].xstart - a->apmatch[0].xstart +
383 a->apmatch[i-1].size*factor;
384
385 if ( a->ymin>0 && a->sb_base- a->ymax*factor >=0 )
386 extra = (a->sb_base - a->ymax*factor)/2;
387 else
388 extra = (a->sb_base - (a->ymax-a->ymin)*factor)/2;
389 a->baseline = a->ymax*factor + extra;
390
391 GDrawRequestExpose(a->gw,NULL,false);
392 }
393
394 static void AnchorD_ChangeMag(AnchorDlg *a) {
395
396 AnchorD_Resize(a);
397 }
398
399 static void AnchorD_ChangeSize(AnchorDlg *a) {
400 int i, off;
401
402 GDrawSetCursor(a->gw,ct_watch);
403 GDrawSync(NULL);
404 /* GDrawProcessPendingEvents(NULL); */ /* Any expose events on the current dlg will cause a crash as there are no bdfc's now */
405
406 a->scale = a->pixelsize / (double) (a->sc->parent->ascent + a->sc->parent->descent);
407
408 BDFCharFree(a->bdfc);
409 a->bdfc = APRasterize(a->freetypecontext,a->sc,a->layer,&a->char_off,&a->char_size,a->pixelsize);
410 a->ymin = a->bdfc->ymin; a->ymax = a->bdfc->ymax;
411 for ( i=0; i<a->cnt; ++i ) {
412 BDFCharFree(a->apmatch[i].bdfc);
413 a->apmatch[i].bdfc = APRasterize(a->freetypecontext,a->apmatch[i].sc,a->layer,&a->apmatch[i].off,&a->apmatch[i].size,a->pixelsize);
414 if ( a->ap->type==at_centry || a->ap->type==at_cexit )
415 a->apmatch[i].size += a->char_size;
416 else if ( a->ap->type!=at_mark )
417 a->apmatch[i].size = a->char_size;
418 switch ( a->ap->type ) {
419 case at_cexit:
420 case at_basechar: case at_baselig: case at_basemark:
421 off = rint((-a->apos.y + a->apmatch[i].ap->me.y)*a->scale);
422 if ( a->apmatch[i].bdfc->ymax+off > a->ymax )
423 a->ymax = a->apmatch[i].bdfc->ymax+off;
424 if ( a->apmatch[i].bdfc->ymin+off < a->ymin )
425 a->ymin = a->apmatch[i].bdfc->ymin+off;
426 break;
427 case at_centry:
428 case at_mark:
429 if ( a->apmatch[i].bdfc->ymax > a->ymax )
430 a->ymax = a->apmatch[i].bdfc->ymax;
431 if ( a->apmatch[i].bdfc->ymin < a->ymin )
432 a->ymin = a->apmatch[i].bdfc->ymin;
433 off = rint((-a->apmatch[i].ap->me.y + a->apos.y)*a->scale);
434 if ( a->bdfc->ymax+off > a->ymax )
435 a->ymax = a->bdfc->ymax+off;
436 if ( a->bdfc->ymin+off < a->ymin )
437 a->ymin = a->bdfc->ymin+off;
438 break;
439 }
440 }
441 AnchorD_ChangeMag(a);
442 GDrawSetCursor(a->gw,ct_pointer);
443 }
444
445 static void AnchorD_Expose(AnchorDlg *a,GWindow pixmap,GEvent *event) {
446 GRect *area = &event->u.expose.rect;
447 GRect clip, old1, old2;
448 int expose_end = area->x+area->width;
449 int factor = a->magfactor;
450 int i,x,y;
451
452 if ( expose_end<a->ctl_len )
453 return;
454
455 GDrawPushClip(pixmap,area,&old1);
456
457 if ( area->x<a->ctl_len+a->char_size*factor ) {
458 GDrawDrawLine(pixmap,a->ctl_len,0,a->ctl_len,a->full_height,0x000000);
459 KCD_DrawGlyph(pixmap,a->char_off*factor+a->ctl_len,a->baseline,a->bdfc,factor);
460 DrawAnchorPoint(pixmap,
461 a->char_off*factor+a->ctl_len + ((int) rint(a->apos.x*a->scale))*factor,
462 a->baseline - ((int) rint(a->apos.y*a->scale))*factor,
463 false);
464 GDrawDrawLine(pixmap,a->ctl_len+a->char_size*factor,0,a->ctl_len+a->char_size*factor,a->full_height,0x000000);
465 }
466
467 if ( expose_end>a->ctl_len+a->char_size && area->y<a->sb_base ) {
468 int xcor=0, ycor=0;
469 unichar_t *end;
470 const unichar_t *ret;
471
472 ret = _GGadgetGetTitle(GWidgetGetControl(a->gw,CID_XCor));
473 xcor = u_strtol(ret,&end,10);
474 while ( *end==' ' ) ++end;
475 if ( *end!='\0' || xcor<-128 || xcor>127 )
476 xcor = 0;
477 ret = _GGadgetGetTitle(GWidgetGetControl(a->gw,CID_YCor));
478 ycor = u_strtol(ret,&end,10);
479 while ( *end==' ' ) ++end;
480 if ( *end!='\0' || ycor<-128 || ycor>127 )
481 ycor = 0;
482
483 clip = *area;
484 if ( area->x<=a->ctl_len+a->char_size ) {
485 clip.width -= a->ctl_len+a->char_size+1 - area->x;
486 clip.x = a->ctl_len+a->char_size+1;
487 }
488 if ( area->y+area->height > a->sb_base )
489 clip.height = a->sb_base-area->y;
490 GDrawPushClip(pixmap,&clip,&old2);
491 for ( i=0; i<a->cnt; ++i ) {
492 if ( a->apmatch[i].xstart - a->xoff > clip.x+clip.width )
493 break;
494 if ( a->apmatch[i].xstart+a->apmatch[i].size*factor - a->xoff < clip.x )
495 continue;
496
497 y = a->baseline;
498 if ( i!=0 )
499 GDrawDrawLine(pixmap,a->apmatch[i].xstart-a->xoff,0,
500 a->apmatch[i].xstart-a->xoff,a->sb_base,0x808080);
501 if ( a->ap->type==at_cexit ||
502 a->ap->type==at_basechar || a->ap->type==at_baselig ||
503 a->ap->type==at_basemark ) {
504 x = a->apmatch[i].xstart+a->char_off*factor - a->xoff;
505 KCD_DrawGlyph(pixmap,x,y,a->bdfc,factor);
506 x += ((int) rint((-a->apmatch[i].ap->me.x + a->apos.x)*a->scale) +
507 xcor - DeviceTableFind(&a->apmatch[i].ap->xadjust,a->pixelsize))*factor;
508 y += ((int) rint((a->apmatch[i].ap->me.y - a->apos.y)*a->scale) +
509 -ycor + DeviceTableFind(&a->apmatch[i].ap->yadjust,a->pixelsize))*factor;
510 KCD_DrawGlyph(pixmap,x,y,a->apmatch[i].bdfc,factor);
511 } else {
512 x = a->apmatch[i].xstart+a->apmatch[i].off*factor - a->xoff;
513 KCD_DrawGlyph(pixmap,x,y,a->apmatch[i].bdfc,factor);
514 x += ((int) rint((a->apmatch[i].ap->me.x - a->apos.x)*a->scale) +
515 DeviceTableFind(&a->apmatch[i].ap->xadjust,a->pixelsize)-xcor)*factor;
516 y += ((int) rint((-a->apmatch[i].ap->me.y + a->apos.y)*a->scale) +
517 -DeviceTableFind(&a->apmatch[i].ap->yadjust,a->pixelsize)+ycor)*factor;
518 KCD_DrawGlyph(pixmap,x,y,a->bdfc,factor);
519 }
520 }
521 if ( i>0 )
522 GDrawDrawLine(pixmap,a->apmatch[i-1].xstart+a->apmatch[i-1].size*factor-a->xoff,0,
523 a->apmatch[i-1].xstart+a->apmatch[i-1].size*factor-a->xoff,a->sb_base,0x808080);
524 GDrawPopClip(pixmap,&old2);
525 }
526 GDrawPopClip(pixmap,&old1);
527 }
528
529 static void AnchorD_FigurePos(AnchorDlg *a,GEvent *event) {
530 int x = (event->u.mouse.x - a->ctl_len)/a->magfactor;
531 int y = (a->baseline - event->u.mouse.y)/a->magfactor;
532
533 a->apos.x = rint((x-a->char_off)/a->scale);
534 a->apos.y = rint(y/a->scale);
535 }
536
537 static void AnchorD_ClearCorrections(AnchorDlg *a) {
538 unichar_t ubuf[2];
539
540 ubuf[0] = '0'; ubuf[1] = '\0';
541 free(a->xadjust.corrections); memset(&a->xadjust,0,sizeof(DeviceTable));
542 free(a->yadjust.corrections); memset(&a->yadjust,0,sizeof(DeviceTable));
543 GGadgetSetTitle(GWidgetGetControl(a->gw,CID_XCor),ubuf);
544 GGadgetSetTitle(GWidgetGetControl(a->gw,CID_YCor),ubuf);
545 }
546
547 static void AnchorD_DrawPos(AnchorDlg *a) {
548 GRect r;
549 char buffer[20];
550 unichar_t ubuf[20];
551
552 sprintf( buffer, "%g", (double) a->apos.x );
553 uc_strcpy(ubuf,buffer);
554 GGadgetSetTitle(GWidgetGetControl(a->gw,CID_X),ubuf);
555 sprintf( buffer, "%g", (double) a->apos.y );
556 uc_strcpy(ubuf,buffer);
557 GGadgetSetTitle(GWidgetGetControl(a->gw,CID_Y),ubuf);
558
559 r.x = a->ctl_len;
560 r.y = 0;
561 r.width = a->char_size*a->magfactor;
562 r.height = a->full_height;
563 GDrawRequestExpose(a->gw,&r,false);
564 }
565
566 static void AnchorD_SelectGlyph(AnchorDlg *a, AnchorPoint *ap) {
567 int i;
568 int32_t len;
569 GTextInfo **ti = GGadgetGetList(GWidgetGetControl(a->gw,CID_Glyph),&len);
570
571 for ( i=0; i<len; ++i )
572 if ( ti[i]->userdata == ap )
573 break;
574 if ( i!=len )
575 GGadgetSelectOneListItem(GWidgetGetControl(a->gw,CID_Glyph),i);
576 }
577
578 static int AnchorD_ChangeGlyph(AnchorDlg *a, SplineChar *sc, AnchorPoint *ap);
579
580 static int AnchorD_Mouse(AnchorDlg *a,GEvent *event) {
581 int on_combo = -1;
582 int on_ap = 0;
583 int i;
584
585 if ( event->u.mouse.x<a->ctl_len || event->u.mouse.y>=a->sb_base )
586 return( false );
587 if ( a->xlen>0 && event->u.mouse.x>=a->apmatch[0].xstart ) {
588 int x = event->u.mouse.x + a->xoff;
589 for ( i=0; i<a->cnt; ++i ) {
590 if ( x>=a->apmatch[i].xstart &&
591 x<a->apmatch[i].xstart + a->apmatch[i].size*a->magfactor ) {
592 on_combo = i;
593 break;
594 }
595 }
596 } else if ( event->u.mouse.x>=a->ctl_len && event->u.mouse.x<a->ctl_len+ a->magfactor*a->char_size ) {
597 int x = event->u.mouse.x - a->ctl_len;
598 int y = a->baseline - event->u.mouse.y;
599 int ax = (a->char_off + (int) rint(a->apos.x*a->scale))*a->magfactor;
600 int ay = (int) rint(a->apos.y*a->scale)*a->magfactor;
601 if ( x>ax-4 && x<ax+4 && y>ay-4 && y<ay+4 )
602 on_ap = 2;
603 else
604 on_ap = 1;
605 }
606
607 if ( event->type == et_mousedown ) {
608 if ( on_combo!=-1 )
609 a->combo = on_combo+1;
610 else if ( on_ap==2 ) {
611 a->on_ap = true;
612 a->orig_pos = a->apos;
613 }
614 } else if ( event->type == et_mouseup ) {
615 if ( on_combo!=-1 && on_combo+1==a->combo ) {
616 AnchorPoint *ap = a->apmatch[on_combo].ap;
617 a->combo = 0;
618 AnchorD_ChangeGlyph(a,a->apmatch[on_combo].sc,a->apmatch[on_combo].ap);
619 AnchorD_SelectGlyph(a,ap);
620 } else if ( on_ap && a->on_ap ) {
621 AnchorD_FigurePos(a,event);
622 AnchorD_ClearCorrections(a);
623 AnchorD_DrawPos(a);
624 GDrawRequestExpose(a->gw,NULL,false);
625 } else if ( a->combo!=0 ) {
626 } else if ( a->on_ap ) {
627 a->apos = a->orig_pos;
628 AnchorD_DrawPos(a);
629 }
630 a->on_ap = 0;
631 a->combo = 0;
632 } else if ( a->on_ap ) {
633 AnchorD_FigurePos(a,event);
634 AnchorD_DrawPos(a);
635 }
636 return( true );
637 }
638
639 static void AnchorD_HScroll(AnchorDlg *a,struct sbevent *sb) {
640 int newpos = a->xoff;
641 GRect rect;
642
643 switch( sb->type ) {
644 case et_sb_top:
645 newpos = 0;
646 break;
647 case et_sb_uppage:
648 newpos -= a->sb_width;
649 break;
650 case et_sb_up:
651 newpos -= a->pixelsize*a->magfactor/2;
652 break;
653 case et_sb_down:
654 newpos = a->pixelsize*a->magfactor/2;
655 break;
656 case et_sb_downpage:
657 newpos += a->sb_width;
658 break;
659 case et_sb_bottom:
660 newpos = a->xlen - a->sb_width;
661 break;
662 case et_sb_thumb:
663 case et_sb_thumbrelease:
664 newpos = sb->pos;
665 break;
666 case et_sb_halfup: case et_sb_halfdown: break;
667 }
668 if ( newpos + a->sb_width >= a->xlen )
669 newpos = a->xlen - a->sb_width;
670 if ( newpos < 0 ) newpos = 0;
671 if ( newpos!=a->xoff ) {
672 int diff = newpos-a->xoff;
673 a->xoff = newpos;
674 GScrollBarSetPos(a->hsb,newpos);
675 if ( a->cnt!=0 ) {
676 rect.x = a->apmatch[0].xstart+1; rect.y = 0;
677 rect.width = a->sb_width;
678 rect.height = a->sb_base;
679 GDrawScroll(a->gw,&rect,-diff,0);
680 }
681 }
682 }
683
684 static SplinePoint *SCFindPoint(SplineChar *sc,int layer,int ptnum) {
685 /* We're going to want to move this point, so a point in a reference */
686 /* is totally useless to us (we'd have to move the entire reference */
687 /* and it's probably better just to detach the anchor in that case. */
688 SplineSet *ss;
689 SplinePoint *sp;
690
691 for ( ss = sc->layers[layer].splines; ss!=NULL; ss=ss->next ) {
692 for ( sp=ss->first; ; ) {
693 if ( sp->ttfindex==ptnum )
694 return( sp );
695 if ( sp->next==NULL )
696 break;
697 sp = sp->next->to;
698 if ( sp==ss->first )
699 break;
700 }
701 }
702 return( NULL );
703 }
704
705 static void SetAnchor(SplineChar *sc,int layer, AnchorPoint *ap,DeviceTable *xadjust, DeviceTable *yadjust, BasePoint *pos) {
706 int ly;
707
708 free(ap->xadjust.corrections);
709 if ( xadjust->corrections==NULL ) {
710 memset(&ap->xadjust,0,sizeof(DeviceTable));
711 } else {
712 ap->xadjust = *xadjust;
713 xadjust->corrections = NULL;
714 }
715 free(ap->yadjust.corrections);
716 if ( yadjust->corrections==NULL ) {
717 memset(&ap->yadjust,0,sizeof(DeviceTable));
718 } else {
719 ap->yadjust = *yadjust;
720 yadjust->corrections = NULL;
721 }
722 ap->me = *pos;
723 /* If the anchor is bound to a truetype point we must move the point too */
724 /* or the anchor will just snap back to the point */
725 ly = ly_none;
726 if ( ap->has_ttf_pt && ap->ttf_pt_index!=0xffff ) {
727 int any = false;
728 for ( ly=ly_fore; ly<sc->layer_cnt; ++ly ) if ( sc->layers[ly].order2 ) {
729 SplinePoint *sp = SCFindPoint(sc,layer,ap->ttf_pt_index);
730 if ( sp!=NULL ) {
731 sp->nextcp.x += pos->x - sp->me.x;
732 sp->prevcp.x += pos->x - sp->me.x;
733 sp->nextcp.y += pos->y - sp->me.y;
734 sp->prevcp.y += pos->y - sp->me.y;
735 sp->me = *pos;
736 any = true;
737 }
738 }
739 if ( !any ) {
740 ff_post_notice(_("Detaching Anchor Point"),_("This anchor was attached to point %d, but that's not a point I can move. I'm detaching the anchor from the point.") );
741 ap->has_ttf_pt = false;
742 ly = ly_none;
743 }
744 }
745 SCCharChangedUpdate(sc,ly);
746 }
747
748 static void AnchorD_DoCancel(AnchorDlg *a) {
749 struct state *old;
750
751 for ( old = a->orig_vals; old!=NULL; old=old->next ) {
752 SetAnchor(old->sc,a->layer,old->ap_pt,&old->ap_vals.xadjust,&old->ap_vals.yadjust,&old->ap_vals.me);
753 old->ap_pt->has_ttf_pt = old->ap_vals.has_ttf_pt;
754 old->sc->changed = old->changed; /* Must come after the charchangedupdate */
755 }
756 if ( a->orig_vals!=NULL ) {
757 FVRefreshAll(a->sc->parent);
758 }
759
760 a->done = true;
761 }
762
763 static int AnchorD_MagnificationChanged(GGadget *g, GEvent *e) {
764 AnchorDlg *a = GDrawGetUserData(GGadgetGetWindow(g));
765 if ( e->type==et_controlevent && e->u.control.subtype == et_listselected ) {
766 int mag = GGadgetGetFirstListSelectedItem(GWidgetGetControl(a->gw,CID_Mag));
767
768 if ( mag!=-1 && mag!=a->magfactor-1 ) {
769 a->xoff = (mag+1)*a->xoff/a->magfactor;
770 a->magfactor = mag+1;
771 AnchorD_ChangeMag(a);
772 GDrawRequestExpose(a->gw,NULL,false);
773 }
774 }
775 return( true );
776 }
777
778 static int AnchorD_DisplaySizeChanged(GGadget *g, GEvent *e) {
779 AnchorDlg *a = GDrawGetUserData(GGadgetGetWindow(g));
780 if ( e->type==et_controlevent && e->u.control.subtype == et_textchanged ) {
781 const unichar_t *ret = _GGadgetGetTitle(GWidgetGetControl(a->gw,CID_DisplaySize));
782 unichar_t *end;
783 int pixelsize = u_strtol(ret,&end,10);
784
785 while ( *end==' ' ) ++end;
786 if ( pixelsize>4 && pixelsize<400 && *end=='\0' ) {
787 unichar_t ubuf[20]; char buffer[20];
788 ubuf[0] = '0'; ubuf[1] = '\0';
789 if ( a->xadjust.corrections!=NULL &&
790 pixelsize>=a->xadjust.first_pixel_size &&
791 pixelsize<=a->xadjust.last_pixel_size ) {
792 sprintf( buffer, "%d", a->xadjust.corrections[
793 pixelsize-a->xadjust.first_pixel_size]);
794 uc_strcpy(ubuf,buffer);
795 }
796 GGadgetSetTitle(GWidgetGetControl(a->gw,CID_XCor),ubuf);
797 ubuf[0] = '0'; ubuf[1] = '\0';
798 if ( a->yadjust.corrections!=NULL &&
799 pixelsize>=a->yadjust.first_pixel_size &&
800 pixelsize<=a->yadjust.last_pixel_size ) {
801 sprintf( buffer, "%d", a->yadjust.corrections[
802 pixelsize-a->yadjust.first_pixel_size]);
803 uc_strcpy(ubuf,buffer);
804 }
805 GGadgetSetTitle(GWidgetGetControl(a->gw,CID_YCor),ubuf);
806 a->xoff = pixelsize*a->xoff/a->pixelsize;
807 a->pixelsize = aa_pixelsize = pixelsize;
808 AnchorD_ChangeSize(a);
809 GDrawRequestExpose(a->gw,NULL,false);
810 }
811 }
812 return( true );
813 }
814
815 static int AnchorD_CorrectionChanged(GGadget *g, GEvent *e) {
816 AnchorDlg *a = GDrawGetUserData(GGadgetGetWindow(g));
817 if ( e->type==et_controlevent && e->u.control.subtype == et_textchanged ) {
818 const unichar_t *ret = _GGadgetGetTitle(g);
819 int is_y = GGadgetGetCid(g)==CID_YCor;
820 unichar_t *end;
821 int correction = u_strtol(ret,&end,10);
822
823 while ( *end==' ' ) ++end;
824 if ( *end!='\0' )
825 return( true );
826 if ( correction<-128 || correction>127 ) {
827 ff_post_error( _("Out of Range"), _("Corrections must be between -128 and 127 (and should be smaller)") );
828 return( true );
829 }
830
831 DeviceTableSet(is_y?&a->yadjust:&a->xadjust,a->pixelsize,correction);
832 GDrawRequestExpose(a->gw,NULL,false);
833 }
834 return( true );
835 }
836
837 static int AnchorD_PositionChanged(GGadget *g, GEvent *e) {
838 AnchorDlg *a = GDrawGetUserData(GGadgetGetWindow(g));
839 if ( e->type==et_controlevent && e->u.control.subtype == et_textchanged ) {
840 const unichar_t *ret = _GGadgetGetTitle(g);
841 int is_y = GGadgetGetCid(g)==CID_Y;
842 unichar_t *end;
843 int val = u_strtol(ret,&end,10);
844
845 while ( *end==' ' ) ++end;
846 if ( *end!='\0' )
847 return( true );
848
849 if ( (is_y && a->apos.y==val) || (!is_y && a->apos.x==val))
850 return( true );
851 if ( is_y )
852 a->apos.y = val;
853 else
854 a->apos.x = val;
855 AnchorD_ClearCorrections(a);
856 GDrawRequestExpose(a->gw,NULL,false);
857 }
858 return( true );
859 }
860
861 static int AnchorD_Cancel(GGadget *g, GEvent *e) {
862 if ( e->type==et_controlevent && e->u.control.subtype == et_buttonactivate ) {
863 AnchorDlg *a = GDrawGetUserData(GGadgetGetWindow(g));
864 AnchorD_DoCancel(a);
865 }
866 return( true );
867 }
868
869 static int AnchorD_Apply(AnchorDlg *a) {
870 SetAnchor(a->sc,a->layer,a->ap,&a->xadjust,&a->yadjust,&a->apos);
871 return( true );
872 }
873
874 static int AnchorD_OK(GGadget *g, GEvent *e) {
875 if ( e->type==et_controlevent && e->u.control.subtype == et_buttonactivate ) {
876 AnchorDlg *a = GDrawGetUserData(GGadgetGetWindow(g));
877 AnchorD_Apply(a);
878 a->done = true;
879 }
880 return( true );
881 }
882
883 static void AnchorD_SetDevTabs(AnchorDlg *a) {
884 char buffer[20];
885 unichar_t ubuf[20];
886 int min, max;
887
888 GGadgetClearList(GWidgetGetControl(a->gw,CID_DisplaySize));
889 min = 1; max = 0;
890 if ( a->xadjust.corrections!=NULL ) {
891 min = a->xadjust.first_pixel_size;
892 max = a->xadjust.last_pixel_size;
893 }
894 if ( a->yadjust.corrections!=NULL ) {
895 if ( a->yadjust.first_pixel_size<min ) min = a->yadjust.first_pixel_size;
896 if ( a->yadjust.last_pixel_size>max ) max = a->yadjust.last_pixel_size;
897 }
898 if ( max>min ) {
899 int i;
900 int len = max-min+1;
901 char buffer[20];
902 GTextInfo **ti = malloc((len+1)*sizeof(GTextInfo *));
903 for ( i=0; i<len; ++i ) {
904 ti[i] = calloc(1,sizeof(GTextInfo));
905 sprintf( buffer, "%d", i+min );
906 ti[i]->text = uc_copy(buffer);
907 ti[i]->fg = ti[i]->bg = COLOR_DEFAULT;
908 }
909 ti[i] = calloc(1,sizeof(GTextInfo));
910 GGadgetSetList(GWidgetGetControl(a->gw,CID_DisplaySize),ti,false);
911 }
912 ubuf[0] = '0'; ubuf[1] = '\0';
913 if ( a->xadjust.corrections!=NULL &&
914 a->pixelsize>=a->xadjust.first_pixel_size &&
915 a->pixelsize<=a->xadjust.last_pixel_size ) {
916 sprintf( buffer, "%d", a->xadjust.corrections[
917 a->pixelsize-a->xadjust.first_pixel_size]);
918 uc_strcpy(ubuf,buffer);
919 }
920 GGadgetSetTitle(GWidgetGetControl(a->gw,CID_XCor),ubuf);
921 ubuf[0] = '0'; ubuf[1] = '\0';
922 if ( a->yadjust.corrections!=NULL &&
923 a->pixelsize>=a->yadjust.first_pixel_size &&
924 a->pixelsize<=a->yadjust.last_pixel_size ) {
925 sprintf( buffer, "%d", a->yadjust.corrections[
926 a->pixelsize-a->yadjust.first_pixel_size]);
927 uc_strcpy(ubuf,buffer);
928 }
929 GGadgetSetTitle(GWidgetGetControl(a->gw,CID_YCor),ubuf);
930 }
931
932 static int AnchorD_ChangeGlyph(AnchorDlg *a, SplineChar *sc, AnchorPoint *ap) {
933 char buf[32];
934 struct state *old;
935
936 if ( ap==NULL || sc==NULL )
937 return( true );
938 if ( a->ap==ap )
939 return( true );
940 /* Do we already have an entry for the current anchor? */
941 for ( old = a->orig_vals; old!=NULL && old->ap_pt!=a->ap; old=old->next );
942 /* If so we've already noted its original state and need do nothing more */
943 /* but otherwise we must store the current state */
944 if ( old==NULL ) {
945 old = chunkalloc(sizeof(struct state));
946 old->sc = a->sc;
947 old->changed = a->sc->changed;
948 old->ap_pt = a->ap;
949 old->ap_vals = *a->ap;
950 memset(&a->ap->xadjust,0,sizeof(DeviceTable));
951 memset(&a->ap->yadjust,0,sizeof(DeviceTable));
952 old->next = a->orig_vals;
953 a->orig_vals = old;
954 }
955 AnchorD_Apply(a);
956 AnchorD_FreeChar(a);
957
958 a->ap = ap;
959 a->sc = sc;
960 a->apos = ap->me;
961 sprintf( buf, "%d", (int) rint(ap->me.x) );
962 GGadgetSetTitle8(GWidgetGetControl(a->gw,CID_X),buf);
963 sprintf( buf, "%d", (int) rint(ap->me.y) );
964 GGadgetSetTitle8(GWidgetGetControl(a->gw,CID_Y),buf);
965
966 AnchorD_FindComplements(a);
967 AnchorD_SetDevTabs(a);
968 AnchorD_ChangeSize(a);
969 AnchorD_SetSB(a);
970 AnchorD_SetTitle(a);
971 return( true );
972 }
973
974 static SplineChar *AddAnchor(AnchorDlg *a, SplineFont *sf, AnchorClass *ac,
975 int ismarklike) {
976 char *ret, *def;
977 SplineChar *sc;
978 int isliga = false, ismrk=false, maxlig=-1;
979 AnchorPoint *ap;
980 PST *pst;
981 int i;
982
983 def = copy(".notdef");
984 for (;;) {
985 ret = gwwv_ask_string(_("Provide a glyph name"),def,_("Please identify a glyph by name, and FontForge will add an anchor to that glyph."));
986 free(def);
987 if ( ret==NULL )
988 return( NULL );
989 sc = SFGetChar(sf,-1,ret);
990 def = ret;
991 if ( sc==NULL )
992 ff_post_error(_("Non-existant glyph"), _("The glyph, %.80s, is not in the font"), ret );
993 else {
994 isliga = ismrk = false;
995 for ( ap=sc->anchor ; ap!=NULL; ap=ap->next ) {
996 if ( ap->type == at_baselig )
997 isliga = true;
998 else if ( ap->type == at_basemark || ap->type == at_mark )
999 ismrk = true;
1000 if ( ap->anchor == ac ) {
1001 if ( (ap->type == at_centry ||
1002 (ap->type == at_mark && ac->type==act_mkmk) ||
1003 ap->type == at_baselig ) && ismarklike==-1 )
1004 ismarklike = false;
1005 else if ( (ap->type == at_cexit || (ap->type == at_basemark && ac->type==act_mkmk)) && ismarklike==-1 )
1006 ismarklike = true;
1007 else if ( ap->type != at_baselig ||
1008 ( ap->type == at_baselig && ismarklike>0 ))
1009 ff_post_error(_("Duplicate Anchor Class"), _("The glyph, %.80s, already contains an anchor in this class, %.80s."), ret, ac->name );
1010 else if ( maxlig<ap->lig_index )
1011 maxlig = ap->lig_index;
1012 break;
1013 }
1014 }
1015 if ( ap==NULL )
1016 break;
1017 }
1018 }
1019
1020 ap = chunkalloc(sizeof(AnchorPoint));
1021 ap->anchor = ac;
1022 ap->me.x = ap->me.y = 0;
1023 ap->next = sc->anchor;
1024 sc->anchor = ap;
1025 SCCharChangedUpdate(sc,ly_none);
1026
1027 if ( sc->width==0 ) ismrk = true;
1028 for ( pst=sc->possub; pst!=NULL; pst=pst->next ) {
1029 if ( pst->type == pst_ligature || pst->type == pst_lcaret ) {
1030 isliga = true;
1031 break;
1032 }
1033 }
1034
1035 if ( isliga || (ac->type==act_mklg && ismarklike==0 ) ) {
1036 ap->type = at_baselig;
1037 ap->lig_index = maxlig+1;
1038 } else if ( ismrk && ismarklike!=0 )
1039 ap->type = at_mark;
1040 else if ( ismarklike==0 && (ismrk || ac->type==act_mkmk) )
1041 ap->type = at_basemark;
1042 else if ( ac->type == act_curs ) {
1043 if ( ismarklike==true )
1044 ap->type = at_centry;
1045 else
1046 ap->type = at_cexit;
1047 } else if ( ismarklike==true )
1048 ap->type = at_mark;
1049 else
1050 ap->type = at_basechar;
1051
1052 if ( a!=NULL ) {
1053 GTextInfo **ti = AnchorD_GlyphsInClass(a);
1054 GGadgetSetList(GWidgetGetControl(a->gw,CID_Glyph),ti,false);
1055 for ( i=0; ti[i]->text!=NULL || ti[i]->line; ++i ) {
1056 if ( ti[i]->userdata == ap ) {
1057 GGadgetSelectOneListItem(GWidgetGetControl(a->gw,CID_Glyph),i);
1058 break;
1059 }
1060 }
1061 AnchorD_ChangeGlyph(a,sc,ap);
1062 }
1063 return( sc );
1064 }
1065
1066 static int AnchorD_GlyphChanged(GGadget *g, GEvent *e) {
1067 AnchorDlg *a = GDrawGetUserData(GGadgetGetWindow(g));
1068 if ( e->type==et_controlevent && e->u.control.subtype == et_listselected ) {
1069 GTextInfo *sel = GGadgetGetListItemSelected(g);
1070
1071 if ( sel!=NULL ) {
1072 AnchorPoint *ap = sel->userdata;
1073 if ( ap==Add_Mark )
1074 AddAnchor(a,a->sc->parent,a->ap->anchor,true);
1075 else if ( ap==Add_Base )
1076 AddAnchor(a,a->sc->parent,a->ap->anchor,false);
1077 else {
1078 char *name = u2utf8_copy(sel->text);
1079 SplineChar *sc = SFGetChar(a->sc->parent,-1,name);
1080
1081 free(name);
1082 AnchorD_ChangeGlyph(a,sc,ap);
1083 }
1084 }
1085 }
1086 return( true );
1087 }
1088
1089 static void AnchorD_NextPrev(AnchorDlg *a,int incr) {
1090 GGadget *g = GWidgetGetControl(a->gw,CID_Glyph);
1091 int len;
1092 GTextInfo **ti = GGadgetGetList(g,&len);
1093 int sel = GGadgetGetFirstListSelectedItem(g);
1094
1095 for ( sel += incr; sel>0 && sel<len; sel+=incr ) {
1096 if ( !( ti[sel]->userdata == Add_Mark ||
1097 ti[sel]->userdata == Add_Base ||
1098 ti[sel]->line ||
1099 ti[sel]->disabled ))
1100 break;
1101 }
1102 if ( sel==0 || sel>=len )
1103 GDrawBeep(NULL);
1104 else {
1105 char *name = u2utf8_copy(ti[sel]->text);
1106 SplineChar *sc = SFGetChar(a->sc->parent,-1,name);
1107
1108 free(name);
1109 GGadgetSelectOneListItem(g,sel);
1110 AnchorD_ChangeGlyph(a,sc,ti[sel]->userdata);
1111 }
1112 }
1113
1114 static int anchord_e_h(GWindow gw, GEvent *event) {
1115 AnchorDlg *a = GDrawGetUserData(gw);
1116
1117 switch ( event->type ) {
1118 case et_close:
1119 AnchorD_DoCancel(a);
1120 break;
1121 case et_char:
1122 if ( event->u.chr.keysym == GK_F1 || event->u.chr.keysym == GK_Help ) {
1123 help("ui/dialogs/anchorcontrol.html", NULL);
1124 return( true );
1125 } else if ( event->u.chr.keysym == GK_Page_Down || event->u.chr.keysym == GK_KP_Page_Down ) {
1126 AnchorD_NextPrev(a,1);
1127 return( true );
1128 } else if ( event->u.chr.keysym == GK_Page_Up || event->u.chr.keysym == GK_KP_Page_Up ) {
1129 AnchorD_NextPrev(a,-1);
1130 return( true );
1131 }
1132 return( false );
1133 break;
1134 case et_mouseup: case et_mousemove: case et_mousedown:
1135 return( AnchorD_Mouse(a,event));
1136 break;
1137 case et_expose:
1138 AnchorD_Expose(a,gw,event);
1139 break;
1140 case et_resize:
1141 AnchorD_Resize(a);
1142 GDrawRequestExpose(a->gw,NULL,false);
1143 break;
1144 case et_controlevent:
1145 switch( event->u.control.subtype ) {
1146 case et_scrollbarchange:
1147 AnchorD_HScroll(a,&event->u.control.u.sb);
1148 break;
1149 default: break;
1150 }
1151 break;
1152 default: break;
1153 }
1154 return( true );
1155 }
1156
1157 static GTextInfo magnifications[] = {
1158 { (unichar_t *) "100%", NULL, 0, 0, NULL, NULL, 0, 0, 0, 0, 1, 0, 1, 0, 0, '\0'},
1159 { (unichar_t *) "200%", NULL, 0, 0, NULL, NULL, 0, 0, 0, 0, 0, 0, 1, 0, 0, '\0'},
1160 { (unichar_t *) "300%", NULL, 0, 0, NULL, NULL, 0, 0, 0, 0, 0, 0, 1, 0, 0, '\0'},
1161 { (unichar_t *) "400%", NULL, 0, 0, NULL, NULL, 0, 0, 0, 0, 0, 0, 1, 0, 0, '\0'},
1162 GTEXTINFO_EMPTY
1163 };
1164
1165 void AnchorControl(SplineChar *sc,AnchorPoint *ap,int layer) {
1166 GRect pos;
1167 GWindowAttrs wattrs;
1168 AnchorDlg a;
1169 GWindow gw;
1170 GGadgetCreateData gcd[15], buttonbox, maingcd[3], *hvarray[60], *buttonarray[8];
1171 GGadgetCreateData glyphbox, *glypharray[5];
1172 GTextInfo label[16];
1173 int k, hv;
1174 extern int _GScrollBar_Width;
1175 char buffer[20], xbuf[20], ybuf[20];
1176 GRect boxsize;
1177
1178 memset(&a,0,sizeof(a));
1179 a.sc = sc;
1180 a.ap = ap;
1181 a.apos = ap->me;
1182 a.pixelsize = aa_pixelsize;
1183 a.magfactor = 1;
1184 a.layer = layer;
1185 if ( ap->xadjust.corrections!=NULL ) {
1186 int len = ap->xadjust.last_pixel_size-ap->xadjust.first_pixel_size+1;
1187 a.xadjust = ap->xadjust;
1188 a.xadjust.corrections = malloc(len);
1189 memcpy(a.xadjust.corrections,ap->xadjust.corrections,len);
1190 }
1191 if ( ap->yadjust.corrections!=NULL ) {
1192 int len = ap->yadjust.last_pixel_size-ap->yadjust.first_pixel_size+1;
1193 a.yadjust = ap->yadjust;
1194 a.yadjust.corrections = malloc(len);
1195 memcpy(a.yadjust.corrections,ap->yadjust.corrections,len);
1196 }
1197
1198 memset(&wattrs,0,sizeof(wattrs));
1199
1200 wattrs.mask = wam_events|wam_cursor|wam_utf8_wtitle|wam_undercursor|wam_isdlg|wam_restrict;
1201 wattrs.event_masks = ~(1<<et_charup);
1202 wattrs.restrict_input_to_me = true;
1203 wattrs.undercursor = 1;
1204 wattrs.cursor = ct_pointer;
1205 wattrs.utf8_window_title = _("Anchor Control...");
1206 wattrs.is_dlg = true;
1207 GDrawGetSize(GDrawGetRoot(NULL),&pos);
1208 pos.x = pos.y = 0;
1209 pos.height = GDrawPointsToPixels(NULL,210);
1210 if ( pos.height<aa_pixelsize+40+25 )
1211 pos.height = aa_pixelsize+40+25;
1212 pos.width -= 50;
1213 a.gw = gw = GDrawCreateTopWindow(NULL,&pos,anchord_e_h,&a,&wattrs);
1214
1215 a.ctl_len = GDrawPointsToPixels(gw,140);
1216 a.sb_height = GDrawPointsToPixels(gw,_GScrollBar_Width);
1217 a.sb_base = pos.height - a.sb_height;
1218
1219 memset(maingcd,0,sizeof(maingcd));
1220 memset(&buttonbox,0,sizeof(buttonbox));
1221 memset(&glyphbox,0,sizeof(glyphbox));
1222 memset(gcd,0,sizeof(gcd));
1223 memset(label,0,sizeof(label));
1224 k = hv = 0;
1225
1226 gcd[k].gd.flags = gg_visible|gg_enabled ;
1227 gcd[k].gd.cid = CID_Glyph;
1228 gcd[k].gd.handle_controlevent = AnchorD_GlyphChanged;
1229 gcd[k++].creator = GListButtonCreate;
1230
1231 glypharray[0] = GCD_Glue; glypharray[1] = &gcd[k-1]; glypharray[2] = GCD_Glue; glypharray[3] = NULL;
1232
1233 glyphbox.gd.flags = gg_enabled|gg_visible;
1234 glyphbox.gd.u.boxelements = glypharray;
1235 glyphbox.creator = GHBoxCreate;
1236
1237 hvarray[hv++] = &glyphbox; hvarray[hv++] = GCD_ColSpan; hvarray[hv++] = GCD_ColSpan;
1238 hvarray[hv++] = GCD_ColSpan; hvarray[hv++] = GCD_Glue; hvarray[hv++] = NULL;
1239
1240 label[k].text = (unichar_t *) _("_Size:");
1241 label[k].text_is_1byte = true;
1242 label[k].text_in_resource = true;
1243 gcd[k].gd.label = &label[k];
1244 gcd[k].gd.pos.x = 5; gcd[k].gd.pos.y = 9;
1245 gcd[k].gd.flags = gg_visible|gg_enabled;
1246 gcd[k++].creator = GLabelCreate;
1247 hvarray[hv++] = &gcd[k-1]; hvarray[hv++] = GCD_ColSpan;
1248
1249 sprintf( buffer, "%d", a.pixelsize );
1250 label[k].text = (unichar_t *) buffer;
1251 label[k].text_is_1byte = true;
1252 gcd[k].gd.label = &label[k];
1253 gcd[k].gd.pos.x = 40; gcd[k].gd.pos.y = gcd[k-1].gd.pos.y-4;
1254 gcd[k].gd.pos.width = 60;
1255 gcd[k].gd.flags = gg_visible|gg_enabled;
1256 gcd[k].gd.cid = CID_DisplaySize;
1257 gcd[k].gd.handle_controlevent = AnchorD_DisplaySizeChanged;
1258 gcd[k].gd.popup_msg = gcd[k-1].gd.popup_msg =
1259 _("The size at which the current glyph is rasterized.\nFor small pixelsize you may want to use the magnification\nfactor below to get a clearer view.\n\nThe pulldown list contains the pixelsizes at which there\nare device table corrections.");
1260 gcd[k++].creator = GListFieldCreate;
1261 hvarray[hv++] = &gcd[k-1]; hvarray[hv++] = GCD_ColSpan; hvarray[hv++] = GCD_Glue; hvarray[hv++] = NULL;
1262
1263 /* GT: Short for: Magnification */
1264 label[k].text = (unichar_t *) _("Mag:");
1265 label[k].text_is_1byte = true;
1266 gcd[k].gd.label = &label[k];
1267 gcd[k].gd.pos.x = 10; gcd[k].gd.pos.y = gcd[k-2].gd.pos.y+26;
1268 gcd[k].gd.flags = gg_visible|gg_enabled;
1269 gcd[k++].creator = GLabelCreate;
1270 hvarray[hv++] = GCD_HPad10; hvarray[hv++] = &gcd[k-1]; hvarray[hv++] = GCD_ColSpan;
1271
1272 gcd[k].gd.pos.x = 45; gcd[k].gd.pos.y = gcd[k-1].gd.pos.y-4;
1273 gcd[k].gd.flags = gg_visible|gg_enabled;
1274 gcd[k].gd.cid = CID_Mag;
1275 gcd[k].gd.u.list = magnifications;
1276 gcd[k].gd.handle_controlevent = AnchorD_MagnificationChanged;
1277 gcd[k].gd.popup_msg = gcd[k-1].gd.popup_msg =
1278 _("The glyph is rasterized at the size above, but it\nmay be difficult to see the alignment errors\nthat can happen at small pixelsizes. This allows\nyou to expand each pixel to show potential problems\nbetter.");
1279 gcd[k++].creator = GListButtonCreate;
1280 hvarray[hv++] = &gcd[k-1]; hvarray[hv++] = GCD_Glue; hvarray[hv++] = NULL;
1281
1282 label[k].text = (unichar_t *) _("_X");
1283 label[k].text_is_1byte = true;
1284 label[k].text_in_resource = true;
1285 gcd[k].gd.label = &label[k];
1286 gcd[k].gd.pos.x = 5; gcd[k].gd.pos.y = gcd[k-1].gd.pos.y+30;
1287 gcd[k].gd.flags = gg_visible|gg_enabled;
1288 gcd[k++].creator = GLabelCreate;
1289 hvarray[hv++] = &gcd[k-1]; hvarray[hv++] = GCD_ColSpan;
1290
1291 sprintf( xbuf, "%d", (int) rint(ap->me.x) );
1292 label[k].text = (unichar_t *) xbuf;
1293 label[k].text_is_1byte = true;
1294 gcd[k].gd.label = &label[k];
1295 gcd[k].gd.pos.x = 40; gcd[k].gd.pos.y = gcd[k-1].gd.pos.y-4;
1296 gcd[k].gd.pos.width = 60;
1297 gcd[k].gd.flags = gg_visible|gg_enabled;
1298 gcd[k].gd.cid = CID_X;
1299 gcd[k].gd.handle_controlevent = AnchorD_PositionChanged;
1300 gcd[k].gd.popup_msg = gcd[k-1].gd.popup_msg =
1301 _("The X coordinate of the anchor point in this glyph");
1302 gcd[k++].creator = GNumericFieldCreate;
1303 hvarray[hv++] = &gcd[k-1]; hvarray[hv++] = GCD_ColSpan; hvarray[hv++] = GCD_Glue; hvarray[hv++] = NULL;
1304
1305 /* GT: Short for Correction */
1306 label[k].text = (unichar_t *) _("Cor:");
1307 label[k].text_is_1byte = true;
1308 gcd[k].gd.label = &label[k];
1309 gcd[k].gd.pos.x = 10; gcd[k].gd.pos.y = gcd[k-1].gd.pos.y+30;
1310 gcd[k].gd.flags = gg_visible|gg_enabled;
1311 gcd[k].gd.popup_msg = _("Correction in pixels to the horizontal positioning of this anchor point\nwhen rasterizing at the given pixelsize.\n(Lives in a Device Table)");
1312 gcd[k++].creator = GLabelCreate;
1313 hvarray[hv++] = GCD_HPad10; hvarray[hv++] = &gcd[k-1]; hvarray[hv++] = GCD_ColSpan;
1314
1315 label[k].text = (unichar_t *) "0";
1316 label[k].text_is_1byte = true;
1317 gcd[k].gd.label = &label[k];
1318 gcd[k].gd.pos.x = 45; gcd[k].gd.pos.y = gcd[k-1].gd.pos.y-4;
1319 gcd[k].gd.pos.width = 60;
1320 gcd[k].gd.flags = gg_visible|gg_enabled;
1321 gcd[k].gd.cid = CID_XCor;
1322 gcd[k].gd.handle_controlevent = AnchorD_CorrectionChanged;
1323 gcd[k].gd.popup_msg = gcd[k-1].gd.popup_msg =
1324 _("This is the number of pixels by which the anchor\nshould be moved horizontally when the glyph is\nrasterized at the above size. This information\nis part of the device table for this anchor.\nDevice tables are particularly important at small\npixelsizes where rounding errors will have a\nproportionally greater effect.");
1325 gcd[k++].creator = GNumericFieldCreate;
1326 hvarray[hv++] = &gcd[k-1]; hvarray[hv++] = GCD_Glue; hvarray[hv++] = NULL;
1327
1328 label[k].text = (unichar_t *) _("_Y");
1329 label[k].text_is_1byte = true;
1330 label[k].text_in_resource = true;
1331 gcd[k].gd.label = &label[k];
1332 gcd[k].gd.pos.x = 5; gcd[k].gd.pos.y = gcd[k-1].gd.pos.y+30;
1333 gcd[k].gd.flags = gg_visible|gg_enabled;
1334 gcd[k++].creator = GLabelCreate;
1335 hvarray[hv++] = &gcd[k-1]; hvarray[hv++] = GCD_ColSpan;
1336
1337 sprintf( ybuf, "%d", (int) rint(ap->me.y) );
1338 label[k].text = (unichar_t *) ybuf;
1339 label[k].text_is_1byte = true;
1340 gcd[k].gd.label = &label[k];
1341 gcd[k].gd.pos.x = 40; gcd[k].gd.pos.y = gcd[k-1].gd.pos.y-4;
1342 gcd[k].gd.pos.width = 60;
1343 gcd[k].gd.flags = gg_visible|gg_enabled;
1344 gcd[k].gd.cid = CID_Y;
1345 gcd[k].gd.handle_controlevent = AnchorD_PositionChanged;
1346 gcd[k].gd.popup_msg = gcd[k-1].gd.popup_msg =
1347 _("The Y coordinate of the anchor point in this glyph");
1348 gcd[k++].creator = GNumericFieldCreate;
1349 hvarray[hv++] = &gcd[k-1]; hvarray[hv++] = GCD_ColSpan; hvarray[hv++] = GCD_Glue; hvarray[hv++] = NULL;
1350
1351 /* GT: Short for Correction */
1352 label[k].text = (unichar_t *) _("Cor:");
1353 label[k].text_is_1byte = true;
1354 gcd[k].gd.label = &label[k];
1355 gcd[k].gd.pos.x = 10; gcd[k].gd.pos.y = gcd[k-1].gd.pos.y+30;
1356 gcd[k].gd.flags = gg_visible|gg_enabled;
1357 gcd[k].gd.popup_msg = _("Correction in pixels to the horizontal positioning of this anchor point\nwhen rasterizing at the given pixelsize.\n(Lives in a Device Table)");
1358 gcd[k++].creator = GLabelCreate;
1359 hvarray[hv++] = GCD_HPad10; hvarray[hv++] = &gcd[k-1]; hvarray[hv++] = GCD_ColSpan;
1360
1361 label[k].text = (unichar_t *) "0";
1362 label[k].text_is_1byte = true;
1363 gcd[k].gd.label = &label[k];
1364 gcd[k].gd.pos.x = 45; gcd[k].gd.pos.y = gcd[k-1].gd.pos.y-4;
1365 gcd[k].gd.pos.width = 60;
1366 gcd[k].gd.flags = gg_visible|gg_enabled;
1367 gcd[k].gd.cid = CID_YCor;
1368 gcd[k].gd.handle_controlevent = AnchorD_CorrectionChanged;
1369 gcd[k].gd.popup_msg = gcd[k-1].gd.popup_msg =
1370 _("This is the number of pixels by which the anchor\nshould be moved vertically when the glyph is\nrasterized at the above size. This information\nis part of the device table for this anchor.\nDevice tables are particularly important at small\npixelsizes where rounding errors will have a\nproportionally greater effect.");
1371 gcd[k++].creator = GNumericFieldCreate;
1372 hvarray[hv++] = &gcd[k-1]; hvarray[hv++] = GCD_Glue; hvarray[hv++] = NULL;
1373
1374 hvarray[hv++] = GCD_Glue; hvarray[hv++] = GCD_Glue; hvarray[hv++] = GCD_Glue;
1375 hvarray[hv++] = GCD_Glue; hvarray[hv++] = GCD_Glue; hvarray[hv++] = NULL;
1376
1377 label[k].text = (unichar_t *) _("_OK");
1378 label[k].text_is_1byte = true;
1379 label[k].text_in_resource = true;
1380 gcd[k].gd.label = &label[k];
1381 gcd[k].gd.pos.x = 5; gcd[k].gd.pos.y = gcd[k-1].gd.pos.y+40;
1382 gcd[k].gd.pos.width = -1;
1383 gcd[k].gd.flags = gg_visible|gg_enabled|gg_but_default;
1384 gcd[k].gd.handle_controlevent = AnchorD_OK;
1385 gcd[k++].creator = GButtonCreate;
1386 buttonarray[0] = GCD_Glue; buttonarray[1] = &gcd[k-1]; buttonarray[2] = GCD_Glue;
1387
1388 label[k].text = (unichar_t *) _("_Cancel");
1389 label[k].text_is_1byte = true;
1390 label[k].text_in_resource = true;
1391 gcd[k].gd.label = &label[k];
1392 gcd[k].gd.pos.x = 80; gcd[k].gd.pos.y = gcd[k-1].gd.pos.y+3;
1393 gcd[k].gd.pos.width = -1;
1394 gcd[k].gd.flags = gg_visible|gg_enabled|gg_but_cancel;
1395 gcd[k].gd.handle_controlevent = AnchorD_Cancel;
1396 gcd[k++].creator = GButtonCreate;
1397 buttonarray[3] = GCD_Glue; buttonarray[4] = &gcd[k-1]; buttonarray[5] = GCD_Glue;
1398 buttonarray[6] = NULL;
1399
1400 buttonbox.gd.flags = gg_enabled|gg_visible;
1401 buttonbox.gd.u.boxelements = buttonarray;
1402 buttonbox.creator = GHBoxCreate;
1403
1404 hvarray[hv++] = &buttonbox; hvarray[hv++] = GCD_ColSpan; hvarray[hv++] = GCD_ColSpan;
1405 hvarray[hv++] = GCD_ColSpan; hvarray[hv++] = GCD_Glue; hvarray[hv++] = NULL;
1406 hvarray[hv++] = NULL;
1407
1408 maingcd[0].gd.pos.x = maingcd[0].gd.pos.y = 5;
1409 maingcd[0].gd.pos.height = pos.height - 10;
1410 /* maingcd[0].gd.pos.width = a.ctl_len - 10; */
1411 maingcd[0].gd.flags = gg_enabled|gg_visible|gg_pos_in_pixels;
1412 maingcd[0].gd.u.boxelements = hvarray;
1413 maingcd[0].creator = GHVBoxCreate;
1414
1415 maingcd[1].gd.pos.x = 300;
1416 maingcd[1].gd.pos.y = pos.height-a.sb_height;
1417 maingcd[1].gd.pos.height = a.sb_height;
1418 maingcd[1].gd.pos.width = pos.width-300;
1419 maingcd[1].gd.flags = gg_visible|gg_enabled|gg_pos_in_pixels;
1420 maingcd[1].creator = GScrollBarCreate;
1421
1422 GGadgetsCreate(a.gw,maingcd);
1423 GGadgetSetList(gcd[0].ret,AnchorD_GlyphsInClass(&a),false);
1424 GTextInfoListFree(gcd[0].gd.u.list);
1425
1426 GHVBoxSetExpandableRow(maingcd[0].ret,gb_expandglue);
1427 GHVBoxSetExpandableCol(maingcd[0].ret,4);
1428 GHVBoxSetExpandableCol(buttonbox.ret,gb_expandgluesame);
1429 GHVBoxSetExpandableCol(glyphbox.ret,gb_expandglue);
1430 GGadgetGetDesiredSize(maingcd[0].ret,&boxsize,NULL);
1431 a.ctl_len = boxsize.width + 10;
1432
1433 a.hsb = maingcd[1].ret;
1434
1435 AnchorD_FindComplements(&a);
1436 AnchorD_SetDevTabs(&a);
1437 AnchorD_ChangeSize(&a);
1438 AnchorD_SetTitle(&a);
1439
1440 GDrawSetVisible(a.gw,true);
1441 while ( !a.done )
1442 GDrawProcessOneEvent(NULL);
1443 GDrawDestroyWindow(a.gw);
1444 AnchorD_FreeAll(&a);
1445 }
1446
1447 void AnchorControlClass(SplineFont *_sf,AnchorClass *ac,int layer) {
1448 /* Pick a random glyph with an anchor point in the class. If no glyph, */
1449 /* give user the chance to create one */
1450 SplineChar *sc, *scmark = NULL;
1451 AnchorPoint *ap, *apmark = NULL;
1452 SplineFont *sf;
1453 int k, gid;
1454
1455 if ( _sf->cidmaster!=NULL ) _sf = _sf->cidmaster;
1456 k=0;
1457 ap = NULL;
1458 do {
1459 sf = _sf->subfontcnt==0 ? _sf : _sf->subfonts[k];
1460 for ( gid=0; gid<sf->glyphcnt; ++gid ) if ( (sc = sf->glyphs[gid])!=NULL ) {
1461 for ( ap=sc->anchor; ap!=NULL; ap=ap->next ) {
1462 if ( ap->anchor==ac ) {
1463 if ( ap->type!=at_mark && ap->type!=at_centry )
1464 break;
1465 else if ( scmark==NULL ) {
1466 scmark = sc;
1467 apmark = ap;
1468 }
1469 }
1470 }
1471 if ( ap!=NULL )
1472 break;
1473 }
1474 if ( ap!=NULL )
1475 break;
1476 ++k;
1477 } while ( k<_sf->subfontcnt );
1478
1479 if ( ap==NULL ) {
1480 sc = scmark;
1481 ap = apmark;
1482 }
1483 if ( ap==NULL ) {
1484 sc = AddAnchor(NULL,_sf,ac,-1);
1485 if ( sc==NULL )
1486 return;
1487 for ( ap=sc->anchor; ap!=NULL; ap=ap->next ) {
1488 if ( ap->anchor==ac )
1489 break;
1490 }
1491 if ( ap==NULL ) /* Can't happen */
1492 return;
1493 }
1494
1495 AnchorControl(sc,ap,layer);
1496 }