Debian Codegraph @

fontforgeexe/basedlg.c

raw symbols SCIP
1 /* Copyright (C) 2008-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 "gkeysym.h"
32 #include "splineutil.h"
33 #include "tottfgpos.h"
34 #include "ustring.h"
35 #include "utype.h"
36
37 #include <math.h>
38
39 static uint32_t TagFromString(char *pt) {
40 char script[4];
41
42 memset(script,' ',4);
43 script[0] = *pt++;
44 if ( *pt!='\0' ) {
45 script[1] = *pt++;
46 if ( *pt!='\0' ) {
47 script[2] = *pt++;
48 if ( *pt!='\0' )
49 script[3] = *pt;
50 }
51 }
52 return( CHR(script[0],script[1],script[2],script[3]));
53 }
54
55 typedef struct basedlg {
56 GWindow gw;
57 SplineFont *sf;
58 struct Base *old;
59 int vertical;
60 int done;
61 } BaseDlg;
62
63 typedef struct baselangdlg {
64 GWindow gw;
65 SplineFont *sf;
66 struct baselangextent *old;
67 int vertical;
68 int islang;
69 int done;
70 uint32_t script;
71 } BaseLangDlg;
72
73 extern GTextInfo scripts[], languages[];
74
75 static struct col_init baselangci[4] = {
76 { me_stringchoicetag , NULL, languages, NULL, N_("Language") },
77 { me_int , NULL, NULL, NULL, N_("Min") },
78 { me_int , NULL, NULL, NULL, N_("Max") },
79 { me_addr, NULL, NULL, NULL, "hidden" } /* Used to hold per-language data */
80 };
81 static struct col_init basefeatci[4] = {
82 { me_string , NULL, NULL, NULL, N_("Feature") },
83 { me_int , NULL, NULL, NULL, N_("Min") },
84 { me_int , NULL, NULL, NULL, N_("Max") },
85 { me_addr, NULL, NULL, NULL, "hidden" } /* Not used */
86 };
87
88 #define CID_Languages 1008
89 #define CID_Extents 1009
90
91 static void Base_EnableOtherButtons(GGadget *g, int r, int c) {
92 GGadgetSetEnabled(GWidgetGetControl(GGadgetGetWindow(g),CID_Extents),r!=-1);
93 }
94
95 static void Base_DelClean(GGadget *g, int r) {
96 int rows, cols = GMatrixEditGetColCnt(g);
97 struct matrix_data *md = _GMatrixEditGet(g,&rows);
98
99 BaseLangFree((struct baselangextent *) md[r*cols+cols-1].u.md_str);
100 md[r*cols+cols-1].u.md_str = NULL;
101 }
102
103 static void BaseLang_FinishEdit(GGadget *g, int r, int c, int wasnew) {
104
105 if ( wasnew && c==0 ) {
106 BaseLangDlg *b = GDrawGetUserData(GGadgetGetWindow(g));
107 int rows, cols = GMatrixEditGetColCnt(g);
108 struct matrix_data *md = GMatrixEditGet(g,&rows);
109 uint32_t lang = TagFromString(md[r*cols+0].u.md_str);
110 int gid;
111 SplineChar *sc;
112 DBounds bnd, scb;
113
114 memset(&bnd,0,sizeof(bnd));
115 for ( gid=0; gid<b->sf->glyphcnt; ++gid ) if ( (sc=b->sf->glyphs[gid])!=NULL ) {
116 if ( lang==CHR('E','N','G',' ') && (sc->unicodeenc<0 || sc->unicodeenc>127))
117 /* English just uses ASCII */;
118 else if ( b->script==CHR('l','a','t','n') &&
119 (sc->unicodeenc<0 || sc->unicodeenc>255) &&
120 lang!=CHR('V','I','T',' ') )
121 /* Most languages in latin script only use one accent per */
122 /* letter. So latin1 should provide a reasonable bounding */
123 /* box. Vietnamese is an exception. */;
124 else if ( SCScriptFromUnicode(sc)!=b->script )
125 /* Not interesting */;
126 else {
127 SplineCharFindBounds(sc,&scb);
128 if ( bnd.minx==bnd.maxx )
129 bnd = scb;
130 else {
131 if ( scb.minx<bnd.minx ) bnd.minx = scb.minx;
132 if ( scb.miny<bnd.miny ) bnd.miny = scb.miny;
133 if ( scb.maxx>bnd.maxx ) bnd.maxx = scb.maxx;
134 if ( scb.maxy>bnd.maxy ) bnd.maxy = scb.maxy;
135 }
136 }
137 }
138 if ( b->vertical ) {
139 md[r*cols+1].u.md_ival = floor(bnd.minx);
140 md[r*cols+2].u.md_ival = ceil(bnd.maxx);
141 } else {
142 md[r*cols+1].u.md_ival = floor(bnd.miny);
143 md[r*cols+2].u.md_ival = ceil(bnd.maxy);
144 }
145 }
146 }
147
148 static void BaseLangMatrixInit(struct matrixinit *mi,struct baselangextent *old,
149 int islang, int isvert) {
150 struct matrix_data *md;
151 int cnt;
152 char lang[8];
153 struct baselangextent *bl;
154
155 memset(mi,0,sizeof(*mi));
156 mi->col_cnt = 4;
157 mi->col_init = islang ? baselangci : basefeatci;
158
159 mi->col_init[1].title = isvert ? _("Min") : _("Min (descent)");
160 mi->col_init[2].title = isvert ? _("Max") : _("Max (ascent)");
161
162 for ( cnt = 0, bl=old; bl!=NULL; bl=bl->next, ++cnt);
163 mi->initial_row_cnt = cnt;
164 mi->matrix_data = md = calloc(mi->col_cnt*cnt,sizeof(struct matrix_data));
165
166 for ( cnt = 0, bl=old; bl!=NULL; bl=bl->next, ++cnt) {
167 lang[0] = bl->lang>>24;
168 lang[1] = bl->lang>>16;
169 lang[2] = bl->lang>>8;
170 lang[3] = bl->lang;
171 lang[4] = '\0';
172 md[mi->col_cnt*cnt+0].u.md_str = copy(lang);
173 md[mi->col_cnt*cnt+1].u.md_ival = bl->descent;
174 md[mi->col_cnt*cnt+2].u.md_ival = bl->ascent;
175 md[mi->col_cnt*cnt+mi->col_cnt-1].u.md_str = (char *) BaseLangCopy(bl->features);
176 }
177
178 mi->finishedit = BaseLang_FinishEdit;
179 mi->candelete = NULL;
180 mi->popupmenu = NULL;
181 mi->handle_key = NULL;
182 mi->bigedittitle = NULL;
183 }
184
185 static struct baselangextent *SFBaselang(SplineFont *sf,struct baselangextent *old,
186 int is_vertical, int islang, uint32_t script );
187
188 static int BaseLang_Extents(GGadget *g, GEvent *e) {
189 if ( e->type==et_controlevent && e->u.control.subtype == et_buttonactivate ) {
190 BaseLangDlg *b = GDrawGetUserData(GGadgetGetWindow(g));
191 GGadget *gme = GWidgetGetControl(b->gw,CID_Languages);
192 int rows, cols = GMatrixEditGetColCnt(gme);
193 struct matrix_data *md = _GMatrixEditGet(gme,&rows);
194 int r = GMatrixEditGetActiveRow(gme);
195
196 if ( r<0 || r>=rows )
197 return( true );
198 md[r*cols+cols-1].u.md_str = (char *)
199 SFBaselang(b->sf,(struct baselangextent *)md[r*cols+cols-1].u.md_str,
200 b->vertical,false, b->script);
201 }
202 return( true );
203 }
204
205 static void BaseLang_DoCancel(BaseLangDlg *b) {
206 GGadget *g = GWidgetGetControl(b->gw,CID_Languages);
207 int r, rows, cols = GMatrixEditGetColCnt(g);
208 struct matrix_data *md = _GMatrixEditGet(g,&rows);
209
210 for ( r=0; r<rows; ++r ) {
211 BaseLangFree( (struct baselangextent *) md[r*cols+cols-1].u.md_str);
212 md[r*cols+cols-1].u.md_str = NULL;
213 }
214 b->done = true;
215 }
216
217 static int BaseLang_Cancel(GGadget *g, GEvent *e) {
218 BaseLangDlg *b;
219
220 if ( e->type==et_controlevent && e->u.control.subtype == et_buttonactivate ) {
221 b = GDrawGetUserData(GGadgetGetWindow(g));
222 BaseLang_DoCancel(b);
223 }
224 return( true );
225 }
226
227 static int BaseLang_OK(GGadget *g, GEvent *e) {
228
229 if ( e->type==et_controlevent && e->u.control.subtype == et_buttonactivate ) {
230 BaseLangDlg *b = GDrawGetUserData(GGadgetGetWindow(g));
231 GGadget *g = GWidgetGetControl(b->gw,CID_Languages);
232 int r, rows, cols = GMatrixEditGetColCnt(g);
233 struct matrix_data *md = GMatrixEditGet(g,&rows);
234 struct baselangextent *cur, *last;
235
236 if ( md==NULL )
237 return( true );
238
239 BaseLangFree(b->old);
240 b->old = last = NULL;
241 for ( r=0; r<rows; ++r ) {
242 cur = chunkalloc(sizeof(struct baselangextent));
243 cur->lang = TagFromString(md[r*cols+0].u.md_str);
244 cur->descent = md[r*cols+1].u.md_ival;
245 cur->ascent = md[r*cols+2].u.md_ival;
246 cur->features = (struct baselangextent *) md[r*cols+3].u.md_str;
247 md[r*cols+3].u.md_str = NULL;
248 if ( last==NULL )
249 b->old = cur;
250 else
251 last->next = cur;
252 last = cur;
253 }
254 b->done = true;
255 }
256 return( true );
257 }
258
259 static int baselang_e_h(GWindow gw, GEvent *event) {
260 BaseLangDlg *b = GDrawGetUserData(gw);
261
262 switch ( event->type ) {
263 case et_close:
264 BaseLang_DoCancel(b);
265 break;
266 case et_char:
267 if ( event->u.chr.keysym == GK_F1 || event->u.chr.keysym == GK_Help ) {
268 help("ui/dialogs/baseline.html", NULL);
269 return( true );
270 }
271 return( false );
272 break;
273 default: break;
274 }
275 return( true );
276 }
277
278 static struct baselangextent *SFBaselang(SplineFont *sf,struct baselangextent *old,
279 int is_vertical, int islang, uint32_t script ) {
280 int i,j,k;
281 GRect pos;
282 GWindowAttrs wattrs;
283 BaseLangDlg b;
284 GWindow gw;
285 char buffer[200];
286 GGadgetCreateData gcd[13], box[4], buttongcd[2];
287 GGadgetCreateData *buttonarray[8], *varray[12];
288 GTextInfo label[13], buttonlabel[1];
289 struct matrixinit mi;
290 GGadget *g;
291
292 memset(&b,0,sizeof(b));
293 b.sf = sf;
294 b.old = old;
295 b.vertical = is_vertical;
296 b.islang = islang;
297 b.script = script;
298
299 memset(&wattrs,0,sizeof(wattrs));
300
301 wattrs.mask = wam_events|wam_cursor|wam_utf8_wtitle|wam_undercursor|wam_isdlg|wam_restrict;
302 wattrs.event_masks = ~(1<<et_charup);
303 wattrs.restrict_input_to_me = true;
304 wattrs.undercursor = 1;
305 wattrs.cursor = ct_pointer;
306 snprintf(buffer,sizeof(buffer),is_vertical ? _("Vertical Extents for %c%c%c%c") : _("Horizontal Extents for %c%c%c%c"),
307 script>>24, script>>16, script>>8, script);
308 wattrs.utf8_window_title = buffer;
309 wattrs.is_dlg = true;
310 pos.x = pos.y = 0;
311 pos.width = GGadgetScale(GDrawPointsToPixels(NULL,300));
312 pos.height = GDrawPointsToPixels(NULL,200);
313 b.gw = gw = GDrawCreateTopWindow(NULL,&pos,baselang_e_h,&b,&wattrs);
314
315 memset(&gcd,0,sizeof(gcd));
316 memset(&buttongcd,0,sizeof(buttongcd));
317 memset(&box,0,sizeof(box));
318 memset(&label,0,sizeof(label));
319 memset(&buttonlabel,0,sizeof(buttonlabel));
320
321 i = j = k = 0;
322 if ( islang )
323 label[i].text = (unichar_t *) _(
324 "Set the minimum and maximum values by which\n"
325 "the glyphs in this script extend below and\n"
326 "above the baseline. This may vary by language" );
327 else
328 label[i].text = (unichar_t *) _(
329 "Set the minimum and maximum values by which\n"
330 "the glyphs in this script extend below and\n"
331 "above the baseline when modified by a feature." );
332 label[i].text_is_1byte = true;
333 gcd[i].gd.label = &label[i];
334 gcd[i].gd.pos.x = 5; gcd[i].gd.pos.y = 5+4;
335 gcd[i].gd.flags = gg_enabled|gg_visible;
336 gcd[i].creator = GLabelCreate;
337 varray[j++] = &gcd[i++]; varray[j++] = NULL;
338
339 BaseLangMatrixInit(&mi,old,islang,is_vertical);
340 gcd[i].gd.pos.width = 300; gcd[i].gd.pos.height = 200;
341 gcd[i].gd.flags = gg_enabled | gg_visible;
342 gcd[i].gd.cid = CID_Languages;
343 gcd[i].gd.u.matrix = &mi;
344 gcd[i++].creator = GMatrixEditCreate;
345 varray[j++] = &gcd[i-1]; varray[j++] = NULL;
346
347 gcd[i].gd.flags = gg_visible | gg_enabled | gg_but_default;
348 label[i].text = (unichar_t *) _("_OK");
349 label[i].text_is_1byte = true;
350 label[i].text_in_resource = true;
351 gcd[i].gd.label = &label[i];
352 gcd[i].gd.handle_controlevent = BaseLang_OK;
353 gcd[i++].creator = GButtonCreate;
354
355 gcd[i].gd.flags = gg_visible | gg_enabled | gg_but_cancel;
356 label[i].text = (unichar_t *) _("_Cancel");
357 label[i].text_is_1byte = true;
358 label[i].text_in_resource = true;
359 gcd[i].gd.label = &label[i];
360 gcd[i].gd.handle_controlevent = BaseLang_Cancel;
361 gcd[i++].creator = GButtonCreate;
362
363 buttonarray[0] = GCD_Glue; buttonarray[1] = &gcd[i-2]; buttonarray[2] = GCD_Glue;
364 buttonarray[3] = GCD_Glue; buttonarray[4] = &gcd[i-1]; buttonarray[5] = GCD_Glue;
365 buttonarray[6] = NULL;
366 box[3].gd.flags = gg_enabled|gg_visible;
367 box[3].gd.u.boxelements = buttonarray;
368 box[3].creator = GHBoxCreate;
369 varray[j++] = &box[3]; varray[j++] = NULL; varray[j++] = NULL;
370
371 box[0].gd.pos.x = box[0].gd.pos.y = 2;
372 box[0].gd.flags = gg_enabled|gg_visible;
373 box[0].gd.u.boxelements = varray;
374 box[0].creator = GHVGroupCreate;
375
376 GGadgetsCreate(gw,box);
377 GHVBoxSetExpandableRow(box[0].ret,1);
378 GHVBoxSetExpandableCol(box[3].ret,gb_expandgluesame);
379
380 buttongcd[0].gd.flags = gg_visible;
381 buttonlabel[0].text = (unichar_t *) S_("Set Feature Extents");
382 buttonlabel[0].text_is_1byte = true;
383 buttonlabel[0].text_in_resource = true;
384 buttongcd[0].gd.label = &buttonlabel[0];
385 buttongcd[0].gd.cid = CID_Extents;
386 buttongcd[0].gd.handle_controlevent = BaseLang_Extents;
387 buttongcd[0].creator = GButtonCreate;
388
389 g = GWidgetGetControl(gw,CID_Languages);
390 if ( islang ) {
391 GMatrixEditAddButtons(g,buttongcd);
392 GMatrixEditSetOtherButtonEnable(g,Base_EnableOtherButtons);
393 }
394 GMatrixEditShowColumn(g,3,false);
395
396 GMatrixEditSetBeforeDelete(GWidgetGetControl(gw,CID_Languages),Base_DelClean);
397 GHVBoxFitWindow(box[0].ret);
398
399 GDrawSetVisible(gw,true);
400 while ( !b.done )
401 GDrawProcessOneEvent(NULL);
402 GDrawDestroyWindow(gw);
403 return( b.old );
404 }
405
406 GTextInfo baselinetags[] = {
407 { (unichar_t *) ("hang"), NULL, 0, 0, (void *) CHR('h','a','n','g'), NULL, 0, 0, 0, 0, 0, 0, 1, 0, 0, '\0' },
408 { (unichar_t *) ("icfb"), NULL, 0, 0, (void *) CHR('i','c','f','b'), NULL, 0, 0, 0, 0, 0, 0, 1, 0, 0, '\0' },
409 { (unichar_t *) ("icft"), NULL, 0, 0, (void *) CHR('i','c','f','t'), NULL, 0, 0, 0, 0, 0, 0, 1, 0, 0, '\0' },
410 { (unichar_t *) ("ideo"), NULL, 0, 0, (void *) CHR('i','d','e','o'), NULL, 0, 0, 0, 0, 0, 0, 1, 0, 0, '\0' },
411 { (unichar_t *) ("idtp"), NULL, 0, 0, (void *) CHR('i','d','t','p'), NULL, 0, 0, 0, 0, 0, 0, 1, 0, 0, '\0' },
412 { (unichar_t *) ("math"), NULL, 0, 0, (void *) CHR('m','a','t','h'), NULL, 0, 0, 0, 0, 0, 0, 1, 0, 0, '\0' },
413 { (unichar_t *) ("romn"), NULL, 0, 0, (void *) CHR('r','o','m','n'), NULL, 0, 0, 0, 0, 0, 0, 1, 0, 0, '\0' },
414 GTEXTINFO_EMPTY
415 };
416
417 static struct col_init baselinesci[10] = {
418 { me_stringchoicetag , NULL, scripts, NULL, N_("writing system|Script") },
419 { me_enum , NULL, baselinetags, NULL, N_("Default Baseline") },
420 { me_int, NULL, NULL, NULL, "hang" },
421 { me_int, NULL, NULL, NULL, "icfb" },
422 { me_int, NULL, NULL, NULL, "icft" },
423 { me_int, NULL, NULL, NULL, "ideo" },
424 { me_int, NULL, NULL, NULL, "idtp" },
425 { me_int, NULL, NULL, NULL, "math" },
426 { me_int, NULL, NULL, NULL, "romn" },
427 { me_int, NULL, NULL, NULL, "hidden" } /* Used to hold per-script data */
428 };
429 static uint32_t stdtags[] = {
430 CHR('h','a','n','g'), CHR('i','c','f','b'), CHR('i','c','f','t'), CHR('i','d','e','o'),
431 CHR('i','d','t','p'), CHR('m','a','t','h'), CHR('r','o','m','n'), 0
432 };
433
434 static void Base_FinishEdit(GGadget *g, int r, int c, int wasnew) {
435
436 if ( wasnew && c==0 ) {
437 int rows, cols = GMatrixEditGetColCnt(g);
438 struct matrix_data *md = GMatrixEditGet(g,&rows);
439 uint32_t script = TagFromString(md[r*cols+0].u.md_str);
440 uint32_t bsln;
441 int i=0,j,k;
442
443 /* This if is duplicated (almost) in tottfaat.c: PerGlyphDefBaseline */
444 if ( script==CHR('k','a','n','a') || script==CHR('h','a','n','g') ||
445 script==CHR('h','a','n','i') || script==CHR('b','o','p','o') ||
446 script==CHR('j','a','m','o') || script==CHR('y','i',' ',' '))
447 bsln = CHR('i','d','e','o');
448 else if ( script==CHR('t','i','b','t' ) ||
449 script == CHR('b','e','n','g' ) || script == CHR('b','n','g','2') ||
450 script == CHR('d','e','v','a' ) || script == CHR('d','e','v','2') ||
451 script == CHR('g','u','j','r' ) || script == CHR('g','j','r','2') ||
452 script == CHR('g','u','r','u' ) || script == CHR('g','u','r','2') ||
453 script == CHR('k','n','d','a' ) || script == CHR('k','n','d','2') ||
454 script == CHR('m','l','y','m' ) || script == CHR('m','l','m','2') ||
455 script == CHR('o','r','y','a' ) || script == CHR('o','r','y','2') ||
456 script == CHR('t','a','m','l' ) || script == CHR('t','m','l','2') ||
457 script == CHR('t','e','l','u' ) || script == CHR('t','e','l','2'))
458 bsln = CHR('h','a','n','g');
459 else if ( script==CHR('m','a','t','h') )
460 bsln = CHR('m','a','t','h');
461 else
462 bsln = CHR('r','o','m','n');
463 md[r*cols+1].u.md_ival = bsln;
464 for ( j=0; j<rows; ++j ) if ( j!=r ) {
465 if ( md[j*cols+1].u.md_ival == i ) {
466 for ( k=1; k<cols-1; ++k )
467 md[r*cols+k].u.md_ival = md[j*cols+k].u.md_ival;
468 break;
469 }
470 }
471 }
472 }
473
474 static void BaselineMatrixInit(struct matrixinit *mi,struct Base *old) {
475 struct matrix_data *md;
476 int k, i, cnt, mustfreemem;
477 int _maps[20], *mapping;
478 char script[8];
479 struct basescript *bs;
480
481 memset(mi,0,sizeof(*mi));
482 mi->col_cnt = 10;
483 mi->col_init = baselinesci;
484
485 cnt = 0;
486 mustfreemem = 0;
487 if ( old!=NULL )
488 for ( cnt=0, bs=old->scripts; bs!=NULL; bs=bs->next, ++cnt );
489 mi->initial_row_cnt = cnt;
490 mi->matrix_data = md = calloc(mi->col_cnt*cnt,sizeof(struct matrix_data));
491
492 if ( old!=NULL ) {
493 if ( old->baseline_cnt<sizeof(_maps)/sizeof(_maps[0]) )
494 mapping = _maps;
495 else {
496 mustfreemem = 1;
497 mapping = malloc(old->baseline_cnt*sizeof(int));
498 }
499 for ( k=0; k<old->baseline_cnt; ++k ) {
500 mapping[k] = -1;
501 for ( i=0; stdtags[i]!=0; ++i )
502 if ( old->baseline_tags[k]==stdtags[i]) mapping[k] = i+2;
503 }
504 for ( cnt=0, bs=old->scripts; bs!=NULL; bs=bs->next, ++cnt ) {
505 script[0] = bs->script>>24;
506 script[1] = bs->script>>16;
507 script[2] = bs->script>>8;
508 script[3] = bs->script;
509 script[4] = '\0';
510 md[mi->col_cnt*cnt+0].u.md_str = copy(script);
511 if ( old->baseline_cnt!=0 ) {
512 md[mi->col_cnt*cnt+1].u.md_ival = old->baseline_tags[bs->def_baseline];
513 for ( k=0; k<old->baseline_cnt; ++k ) if ( mapping[k]!=-1 )
514 md[mi->col_cnt*cnt+mapping[k]].u.md_ival = bs->baseline_pos[k];
515 }
516 md[mi->col_cnt*cnt+mi->col_cnt-1].u.md_str = (char *) BaseLangCopy(bs->langs);
517 }
518 if (mustfreemem != 0)
519 free(mapping);
520 }
521
522 mi->finishedit = Base_FinishEdit;
523 mi->candelete = NULL;
524 mi->popupmenu = NULL;
525 mi->handle_key = NULL;
526 mi->bigedittitle = NULL;
527 }
528
529
530 #define CID_BaseHang 1001
531 #define CID_BaseIcfb 1002
532 #define CID_BaseIcft 1003
533 #define CID_BaseIdeo 1004
534 #define CID_BaseIdtp 1005
535 #define CID_BaseMath 1006
536 #define CID_BaseRomn 1007
537 #define CID_Baselines 1008
538
539 static int Base_Extents(GGadget *g, GEvent *e) {
540 if ( e->type==et_controlevent && e->u.control.subtype == et_buttonactivate ) {
541 BaseDlg *b = GDrawGetUserData(GGadgetGetWindow(g));
542 GGadget *gme = GWidgetGetControl(b->gw,CID_Baselines);
543 int rows, cols = GMatrixEditGetColCnt(gme);
544 struct matrix_data *md = _GMatrixEditGet(gme,&rows);
545 int r = GMatrixEditGetActiveRow(gme);
546
547 if ( r<0 || r>=rows )
548 return( true );
549 md[r*cols+cols-1].u.md_str = (char *)
550 SFBaselang(b->sf,(struct baselangextent *)md[r*cols+cols-1].u.md_str,
551 b->vertical,true, TagFromString(md[r*cols+0].u.md_str));
552 }
553 return( true );
554 }
555
556 static int Base_ChangeBase(GGadget *g, GEvent *e) {
557 if ( e==NULL || (e->type==et_controlevent && e->u.control.subtype == et_radiochanged )) {
558 int k, any = false;
559 GWindow gw = GGadgetGetWindow(g);
560 GGadget *bases = GWidgetGetControl(gw,CID_Baselines);
561 for ( k=CID_BaseHang; k<=CID_BaseRomn; ++k ) {
562 int checked = GGadgetIsChecked(GWidgetGetControl(gw,k));
563 GMatrixEditShowColumn(bases,k-CID_BaseHang+2, checked);
564 any |= checked;
565 }
566
567 GMatrixEditShowColumn(bases,1,any);
568 GMatrixEditShowColumn(bases,9,false);
569 }
570 return( true );
571 }
572
573 static void Base_DoCancel(BaseDlg *b) {
574 GGadget *g = GWidgetGetControl(b->gw,CID_Baselines);
575 int r, rows, cols = GMatrixEditGetColCnt(g);
576 struct matrix_data *md = _GMatrixEditGet(g,&rows);
577
578 for ( r=0; r<rows; ++r ) {
579 BaseLangFree( (struct baselangextent *) md[r*cols+cols-1].u.md_str);
580 md[r*cols+cols-1].u.md_str = NULL;
581 }
582 b->done = true;
583 }
584
585 static int Base_Cancel(GGadget *g, GEvent *e) {
586 BaseDlg *b;
587
588 if ( e->type==et_controlevent && e->u.control.subtype == et_buttonactivate ) {
589 b = GDrawGetUserData(GGadgetGetWindow(g));
590 Base_DoCancel(b);
591 }
592 return( true );
593 }
594
595 static int Base_OK(GGadget *g, GEvent *e) {
596
597 if ( e->type==et_controlevent && e->u.control.subtype == et_buttonactivate ) {
598 BaseDlg *b = GDrawGetUserData(GGadgetGetWindow(g));
599 GGadget *g = GWidgetGetControl(b->gw,CID_Baselines);
600 int r, rows, cols = GMatrixEditGetColCnt(g);
601 struct matrix_data *md = GMatrixEditGet(g,&rows);
602 int mapping[20], backmap[20];
603 int i, j, cnt;
604 struct basescript *bs, *last;
605
606 if ( md==NULL )
607 return( true );
608
609 i=0;
610 mapping[0] = GGadgetIsChecked(GWidgetGetControl(b->gw,CID_BaseHang))? i++ : -1;
611 mapping[1] = GGadgetIsChecked(GWidgetGetControl(b->gw,CID_BaseIcfb))? i++ : -1;
612 mapping[2] = GGadgetIsChecked(GWidgetGetControl(b->gw,CID_BaseIcft))? i++ : -1;
613 mapping[3] = GGadgetIsChecked(GWidgetGetControl(b->gw,CID_BaseIdeo))? i++ : -1;
614 mapping[4] = GGadgetIsChecked(GWidgetGetControl(b->gw,CID_BaseIdtp))? i++ : -1;
615 mapping[5] = GGadgetIsChecked(GWidgetGetControl(b->gw,CID_BaseMath))? i++ : -1;
616 mapping[6] = GGadgetIsChecked(GWidgetGetControl(b->gw,CID_BaseRomn))? i++ : -1;
617 cnt = i;
618 memset(backmap,-1,sizeof(backmap));
619 for ( j=0; j<7; ++j ) if ( mapping[j]!=-1 )
620 backmap[mapping[j]] = j;
621
622 if ( cnt!=0 ) {
623 for ( r=0; r<rows; ++r ) {
624 int tag = md[cols*r+1].u.md_ival;
625 for ( j=0; stdtags[j]!=0 && stdtags[j]!=tag; ++j );
626 if ( stdtags[j]==0 || mapping[ j ]==-1 ) {
627 ff_post_error(_("Bad default baseline"),_("Script '%s' claims baseline '%c%c%c%c' as its default, but that baseline is not currently active."),
628 md[cols*r+0].u.md_str,
629 tag>>24, tag>>16, tag>>8, tag );
630 return( true );
631 }
632 }
633 }
634
635 BaseFree(b->old);
636 b->old = chunkalloc(sizeof(struct Base));
637
638 b->old->baseline_cnt = cnt;
639 if ( i!=0 ) {
640 b->old->baseline_tags = malloc(cnt*sizeof(uint32_t));
641 for ( i=0; stdtags[i]!=0 ; ++i ) if ( mapping[i]!=-1 )
642 b->old->baseline_tags[mapping[i]] = stdtags[i];
643 }
644 last = NULL;
645 for ( r=0; r<rows; ++r ) {
646 if ( cnt==0 && md[r*cols+cols-1].u.md_str==NULL )
647 continue;
648 bs = chunkalloc(sizeof(struct basescript));
649 bs->script = TagFromString(md[r*cols+0].u.md_str);
650 if ( cnt!=0 ) {
651 int tag = md[cols*r+1].u.md_ival;
652 for ( j=0; stdtags[j]!=0 && stdtags[j]!=tag; ++j );
653 bs->def_baseline = mapping[j];
654 bs->baseline_pos = malloc(cnt*sizeof(int16_t));
655 for ( i=0; stdtags[i]!=0 ; ++i ) if ( mapping[i]!=-1 )
656 bs->baseline_pos[mapping[i]] = md[r*cols+i+2].u.md_ival;
657 }
658 bs->langs = (struct baselangextent *) md[r*cols+cols-1].u.md_str;
659 md[r*cols+cols-1].u.md_str = NULL;
660 if ( last==NULL )
661 b->old->scripts = bs;
662 else
663 last->next = bs;
664 last = bs;
665 }
666 b->done = true;
667 }
668 return( true );
669 }
670
671 static int base_e_h(GWindow gw, GEvent *event) {
672 BaseDlg *b = GDrawGetUserData(gw);
673
674 switch ( event->type ) {
675 case et_close:
676 Base_DoCancel(b);
677 break;
678 case et_char:
679 if ( event->u.chr.keysym == GK_F1 || event->u.chr.keysym == GK_Help ) {
680 help("ui/dialogs/baseline.html", NULL);
681 return( true );
682 }
683 return( false );
684 break;
685 default: break;
686 }
687 return( true );
688 }
689
690 static void BaseUIInit(void) {
691 static int inited = false;
692
693 if ( inited )
694 return;
695
696 baselinesci[0].title = S_(baselinesci[0].title);
697 baselinesci[1].title = S_(baselinesci[1].title);
698 baselangci[0].title = S_(baselangci[0].title);
699 baselangci[1].title = S_(baselangci[1].title);
700 baselangci[2].title = S_(baselangci[2].title);
701 basefeatci[0].title = S_(basefeatci[0].title);
702 basefeatci[1].title = S_(basefeatci[1].title);
703 basefeatci[2].title = S_(basefeatci[2].title);
704 inited = true;
705 }
706
707 struct Base *SFBaselines(SplineFont *sf,struct Base *old,int is_vertical) {
708 int i,j,k;
709 GRect pos;
710 GWindowAttrs wattrs;
711 BaseDlg b;
712 GWindow gw;
713 char buffer[200];
714 GGadgetCreateData gcd[13], box[4], buttongcd[2];
715 GGadgetCreateData *buttonarray[8], *varray[12], *hvarray[12];
716 GTextInfo label[13], buttonlabel[1];
717 struct matrixinit mi;
718 GGadget *g;
719
720 memset(&b,0,sizeof(b));
721 b.sf = sf;
722 b.sf = sf;
723 b.old = old;
724 b.vertical = is_vertical;
725
726 memset(&wattrs,0,sizeof(wattrs));
727
728 wattrs.mask = wam_events|wam_cursor|wam_utf8_wtitle|wam_undercursor|wam_isdlg|wam_restrict;
729 wattrs.event_masks = ~(1<<et_charup);
730 wattrs.restrict_input_to_me = true;
731 wattrs.undercursor = 1;
732 wattrs.cursor = ct_pointer;
733 snprintf(buffer,sizeof(buffer),"%s", is_vertical ? _("Vertical Baselines") : _("Horizontal Baselines") );
734 wattrs.utf8_window_title = buffer;
735 wattrs.is_dlg = true;
736 pos.x = pos.y = 0;
737 pos.width = GGadgetScale(GDrawPointsToPixels(NULL,300));
738 pos.height = GDrawPointsToPixels(NULL,200);
739 b.gw = gw = GDrawCreateTopWindow(NULL,&pos,base_e_h,&b,&wattrs);
740
741 LookupUIInit();
742 BaseUIInit();
743
744 memset(&gcd,0,sizeof(gcd));
745 memset(&buttongcd,0,sizeof(buttongcd));
746 memset(&box,0,sizeof(box));
747 memset(&label,0,sizeof(label));
748 memset(&buttonlabel,0,sizeof(buttonlabel));
749
750 i = j = k = 0;
751 label[i].text = (unichar_t *) _("From the list below, select the baselines for which you\nwill provide data.");
752 label[i].text_is_1byte = true;
753 gcd[i].gd.label = &label[i];
754 gcd[i].gd.pos.x = 5; gcd[i].gd.pos.y = 5+4;
755 gcd[i].gd.flags = gg_enabled|gg_visible;
756 gcd[i].creator = GLabelCreate;
757 varray[j++] = &gcd[i++]; varray[j++] = NULL;
758
759 label[i].text = (unichar_t *) _("hang");
760 label[i].text_is_1byte = true;
761 label[i].text_in_resource = true;
762 gcd[i].gd.label = &label[i];
763 gcd[i].gd.flags = gg_enabled|gg_visible;
764 gcd[i].gd.popup_msg = _("Indic (& Tibetan) hanging baseline");
765 gcd[i].gd.cid = CID_BaseHang;
766 gcd[i].gd.handle_controlevent = Base_ChangeBase;
767 gcd[i].creator = GCheckBoxCreate;
768 hvarray[k++] = &gcd[i++];
769
770 label[i].text = (unichar_t *) _("icfb");
771 label[i].text_is_1byte = true;
772 label[i].text_in_resource = true;
773 gcd[i].gd.label = &label[i];
774 gcd[i].gd.flags = gg_enabled|gg_visible;
775 gcd[i].gd.popup_msg = _("Ideographic character face bottom edge baseline");
776 gcd[i].gd.cid = CID_BaseIcfb;
777 gcd[i].gd.handle_controlevent = Base_ChangeBase;
778 gcd[i].creator = GCheckBoxCreate;
779 hvarray[k++] = &gcd[i++];
780
781 label[i].text = (unichar_t *) _("icft");
782 label[i].text_is_1byte = true;
783 label[i].text_in_resource = true;
784 gcd[i].gd.label = &label[i];
785 gcd[i].gd.flags = gg_enabled|gg_visible;
786 gcd[i].gd.popup_msg = _("Ideographic character face top edge baseline");
787 gcd[i].gd.cid = CID_BaseIcft;
788 gcd[i].gd.handle_controlevent = Base_ChangeBase;
789 gcd[i].creator = GCheckBoxCreate;
790 hvarray[k++] = &gcd[i++];
791
792 label[i].text = (unichar_t *) _("ideo");
793 label[i].text_is_1byte = true;
794 label[i].text_in_resource = true;
795 gcd[i].gd.label = &label[i];
796 gcd[i].gd.flags = gg_enabled|gg_visible;
797 gcd[i].gd.popup_msg = _("Ideographic em-box bottom edge baseline");
798 gcd[i].gd.cid = CID_BaseIdeo;
799 gcd[i].gd.handle_controlevent = Base_ChangeBase;
800 gcd[i].creator = GCheckBoxCreate;
801 hvarray[k++] = &gcd[i++]; hvarray[k++] = NULL;
802
803 label[i].text = (unichar_t *) _("idtp");
804 label[i].text_is_1byte = true;
805 label[i].text_in_resource = true;
806 gcd[i].gd.label = &label[i];
807 gcd[i].gd.flags = gg_enabled|gg_visible;
808 gcd[i].gd.popup_msg = _("Ideographic em-box top edge baseline");
809 gcd[i].gd.cid = CID_BaseIdtp;
810 gcd[i].gd.handle_controlevent = Base_ChangeBase;
811 gcd[i].creator = GCheckBoxCreate;
812 hvarray[k++] = &gcd[i++];
813
814 label[i].text = (unichar_t *) _("math");
815 label[i].text_is_1byte = true;
816 label[i].text_in_resource = true;
817 gcd[i].gd.label = &label[i];
818 gcd[i].gd.flags = gg_enabled|gg_visible;
819 gcd[i].gd.popup_msg = _("Mathematical centerline");
820 gcd[i].gd.cid = CID_BaseMath;
821 gcd[i].gd.handle_controlevent = Base_ChangeBase;
822 gcd[i].creator = GCheckBoxCreate;
823 hvarray[k++] = &gcd[i++];
824
825 label[i].text = (unichar_t *) _("romn");
826 label[i].text_is_1byte = true;
827 label[i].text_in_resource = true;
828 gcd[i].gd.label = &label[i];
829 gcd[i].gd.flags = gg_enabled|gg_visible;
830 gcd[i].gd.popup_msg = _("Baseline used for Latin, Greek, Cyrillic text.");
831 gcd[i].gd.cid = CID_BaseRomn;
832 gcd[i].gd.handle_controlevent = Base_ChangeBase;
833 gcd[i].creator = GCheckBoxCreate;
834 hvarray[k++] = &gcd[i++];
835 hvarray[k++] = GCD_Glue; hvarray[k++] = NULL; hvarray[k++] = NULL;
836
837 box[2].gd.flags = gg_enabled|gg_visible;
838 box[2].gd.u.boxelements = hvarray;
839 box[2].creator = GHVBoxCreate;
840 varray[j++] = &box[2]; varray[j++] = NULL;
841
842 label[i].text = (unichar_t *) _(
843 "If any of the above baselines are active then you should\n"
844 "specify which one is the default baseline for each script\n"
845 "in the font, and specify how to position glyphs in this\n"
846 "script relative to all active baselines");
847 label[i].text_is_1byte = true;
848 gcd[i].gd.label = &label[i];
849 gcd[i].gd.pos.x = 5; gcd[i].gd.pos.y = 5+4;
850 gcd[i].gd.flags = gg_enabled|gg_visible;
851 gcd[i].creator = GLabelCreate;
852 varray[j++] = &gcd[i++]; varray[j++] = NULL;
853
854 BaselineMatrixInit(&mi,old);
855 gcd[i].gd.pos.width = 300; gcd[i].gd.pos.height = 200;
856 gcd[i].gd.flags = gg_enabled | gg_visible;
857 gcd[i].gd.cid = CID_Baselines;
858 gcd[i].gd.u.matrix = &mi;
859 gcd[i++].creator = GMatrixEditCreate;
860 varray[j++] = &gcd[i-1]; varray[j++] = NULL;
861
862 gcd[i].gd.flags = gg_visible | gg_enabled | gg_but_default;
863 label[i].text = (unichar_t *) _("_OK");
864 label[i].text_is_1byte = true;
865 label[i].text_in_resource = true;
866 gcd[i].gd.label = &label[i];
867 gcd[i].gd.handle_controlevent = Base_OK;
868 gcd[i++].creator = GButtonCreate;
869
870 gcd[i].gd.flags = gg_visible | gg_enabled | gg_but_cancel;
871 label[i].text = (unichar_t *) _("_Cancel");
872 label[i].text_is_1byte = true;
873 label[i].text_in_resource = true;
874 gcd[i].gd.label = &label[i];
875 gcd[i].gd.handle_controlevent = Base_Cancel;
876 gcd[i++].creator = GButtonCreate;
877
878 buttonarray[0] = GCD_Glue; buttonarray[1] = &gcd[i-2]; buttonarray[2] = GCD_Glue;
879 buttonarray[3] = GCD_Glue; buttonarray[4] = &gcd[i-1]; buttonarray[5] = GCD_Glue;
880 buttonarray[6] = NULL;
881 box[3].gd.flags = gg_enabled|gg_visible;
882 box[3].gd.u.boxelements = buttonarray;
883 box[3].creator = GHBoxCreate;
884 varray[j++] = &box[3]; varray[j++] = NULL; varray[j++] = NULL;
885
886 box[0].gd.pos.x = box[0].gd.pos.y = 2;
887 box[0].gd.flags = gg_enabled|gg_visible;
888 box[0].gd.u.boxelements = varray;
889 box[0].creator = GHVGroupCreate;
890
891 GGadgetsCreate(gw,box);
892 GHVBoxSetExpandableRow(box[0].ret,3);
893 GHVBoxSetExpandableCol(box[3].ret,gb_expandgluesame);
894
895 buttongcd[0].gd.flags = gg_visible;
896 buttonlabel[0].text = (unichar_t *) S_("Set Extents");
897 buttonlabel[0].text_is_1byte = true;
898 buttonlabel[0].text_in_resource = true;
899 buttongcd[0].gd.label = &buttonlabel[0];
900 buttongcd[0].gd.cid = CID_Extents;
901 buttongcd[0].gd.handle_controlevent = Base_Extents;
902 buttongcd[0].creator = GButtonCreate;
903
904 g = GWidgetGetControl(gw,CID_Baselines);
905 GMatrixEditAddButtons(g,buttongcd);
906 GMatrixEditSetOtherButtonEnable(g,Base_EnableOtherButtons);
907
908 GMatrixEditSetBeforeDelete(GWidgetGetControl(gw,CID_Baselines),Base_DelClean);
909 if ( old!=NULL ) {
910 for ( k=0; k<old->baseline_cnt; ++k ) {
911 for ( i=0; stdtags[i]!=0; ++i )
912 if ( old->baseline_tags[k] == stdtags[i] ) {
913 GGadgetSetChecked(GWidgetGetControl(gw,CID_BaseHang+i),true);
914 break;
915 }
916 }
917 }
918 Base_ChangeBase(GWidgetGetControl(gw,CID_BaseHang), NULL);
919 GHVBoxFitWindow(box[0].ret);
920
921 GDrawSetVisible(gw,true);
922 while ( !b.done )
923 GDrawProcessOneEvent(NULL);
924 GDrawDestroyWindow(gw);
925 return( b.old );
926 }