Debian Codegraph @

fontforgeexe/math.c

raw symbols SCIP
1 /* Copyright (C) 2007-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 "gresedit.h"
34 #include "mathconstants.h"
35 #include "splineutil.h"
36 #include "ustring.h"
37 #include "utype.h"
38
39 #include <math.h>
40 #include <stddef.h>
41
42 extern struct math_constants_descriptor math_constants_descriptor[];
43
44 static char *aspectnames[] = {
45 N_("Constants"),
46 N_("Sub/Superscript"),
47 N_("Limits"),
48 N_("Stacks"),
49 N_("Fractions"),
50 N_("Over/Underbars"),
51 N_("Radicals"),
52 N_("Connectors"),
53 NULL
54 };
55
56 GResFont math_font = GRESFONT_INIT("400 12pt " SANS_UI_FAMILIES);
57 GResFont math_boldfont = GRESFONT_INIT("700 12pt " SANS_UI_FAMILIES);
58 extern GBox _ggadget_Default_Box;
59 #define MAIN_FOREGROUND (_ggadget_Default_Box.main_foreground)
60
61 static char *GlyphConstruction_Dlg(GGadget *g, int r, int c);
62 static char *MKChange_Dlg(GGadget *g, int r, int c);
63 static void extpart_finishedit(GGadget *g, int r, int c, int wasnew);
64 static void italic_finishedit(GGadget *g, int r, int c, int wasnew);
65 static void topaccent_finishedit(GGadget *g, int r, int c, int wasnew);
66 static void mathkern_initrow(GGadget *g, int r);
67
68 static GTextInfo truefalse[] = {
69 { (unichar_t *) N_("false"), NULL, 0, 0, (void *) 0, NULL, 0, 0, 0, 0, 0, 0, 1, 0, 0, '\0' },
70 { (unichar_t *) N_("true"), NULL, 0, 0, (void *) 1, NULL, 0, 0, 0, 0, 0, 0, 1, 0, 0, '\0' },
71 GTEXTINFO_EMPTY
72 };
73
74 static struct col_init exten_shape_ci[] = {
75 { me_string, NULL, NULL, NULL, N_("Glyph") },
76 { me_enum, NULL, truefalse, NULL, N_("Is Extended Shape") },
77 COL_INIT_EMPTY
78 };
79
80 static struct col_init italic_cor_ci[] = {
81 { me_string, NULL, NULL, NULL, N_("Glyph") },
82 { me_int, NULL, NULL, NULL, N_("Italic Correction") },
83 { me_funcedit, DevTab_Dlg, NULL, NULL, N_("Adjust") },
84 COL_INIT_EMPTY
85 };
86
87 static struct col_init top_accent_ci[] = {
88 { me_string, NULL, NULL, NULL, N_("Glyph") },
89 { me_int, NULL, NULL, NULL, N_("Top Accent Horiz. Pos") },
90 { me_funcedit, DevTab_Dlg, NULL, NULL, N_("Adjust") },
91 COL_INIT_EMPTY
92 };
93
94 static struct col_init glyph_variants_ci[] = {
95 { me_string, NULL, NULL, NULL, N_("Glyph") },
96 { me_string, NULL, NULL, NULL, N_("Pre-Built Larger Variants") },
97 COL_INIT_EMPTY
98 };
99
100 static struct col_init glyph_construction_ci[] = {
101 { me_string, NULL, NULL, NULL, N_("Glyph") },
102 /* GT: Italic correction */
103 { me_int, NULL, NULL, NULL, N_("I.C.") },
104 { me_funcedit, DevTab_Dlg, NULL, NULL, N_("Adjust") },
105 { me_funcedit, GlyphConstruction_Dlg, NULL, NULL, N_("Parts List") },
106 COL_INIT_EMPTY
107 };
108
109 static struct col_init math_kern_ci[] = {
110 { me_string, NULL, NULL, NULL, N_("Glyph") },
111 { me_button, MKChange_Dlg, NULL, NULL, N_("Height/Kern Data") },
112 COL_INIT_EMPTY
113 };
114
115 struct matrixinit mis[] = {
116 { sizeof(exten_shape_ci)/sizeof(struct col_init)-1, exten_shape_ci, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL },
117 { sizeof(italic_cor_ci)/sizeof(struct col_init)-1, italic_cor_ci, 0, NULL, NULL, NULL, italic_finishedit, NULL, NULL, NULL },
118 { sizeof(top_accent_ci)/sizeof(struct col_init)-1, top_accent_ci, 0, NULL, NULL, NULL, topaccent_finishedit, NULL, NULL, NULL },
119 { sizeof(math_kern_ci)/sizeof(struct col_init)-1, math_kern_ci, 0, NULL, mathkern_initrow, NULL, NULL, NULL, NULL, NULL },
120 { sizeof(glyph_variants_ci)/sizeof(struct col_init)-1, glyph_variants_ci, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL },
121 { sizeof(glyph_construction_ci)/sizeof(struct col_init)-1, glyph_construction_ci, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL },
122 { sizeof(glyph_variants_ci)/sizeof(struct col_init)-1, glyph_variants_ci, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL },
123 { sizeof(glyph_construction_ci)/sizeof(struct col_init)-1, glyph_construction_ci, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL },
124 MATRIXINIT_EMPTY
125 };
126
127 static struct col_init extensionpart[] = {
128 { me_string , NULL, NULL, NULL, N_("Glyph") },
129 { me_enum, NULL, truefalse, NULL, N_("Extender") },
130 /* GT: "Len" is an abbreviation for "Length" */
131 { me_int, NULL, NULL, NULL, N_("StartLen") },
132 { me_int, NULL, NULL, NULL, N_("EndLen") },
133 { me_int, NULL, NULL, NULL, N_("FullLen") },
134 COL_INIT_EMPTY
135 };
136 static struct matrixinit mi_extensionpart =
137 { sizeof(extensionpart)/sizeof(struct col_init)-1, extensionpart, 0, NULL, NULL, NULL, extpart_finishedit, NULL, NULL, NULL };
138
139 static struct col_init mathkern[] = {
140 { me_int , NULL, NULL, NULL, N_("Height") },
141 { me_int, NULL, NULL, NULL, N_("Kern") },
142 { me_funcedit, DevTab_Dlg, NULL, NULL, N_("Height Adjusts") },
143 { me_funcedit, DevTab_Dlg, NULL, NULL, N_("Kern Adjusts") },
144 COL_INIT_EMPTY
145 };
146 static struct matrixinit mi_mathkern =
147 { sizeof(mathkern)/sizeof(struct col_init)-1, mathkern, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL };
148
149
150 #define CID_Exten 1000
151 #define CID_Italic 1001
152 #define CID_TopAccent 1002
153 #define CID_MathKern 1003
154 #define CID_VGlyphVar 1004
155 #define CID_VGlyphConst 1005
156 #define CID_HGlyphVar 1006
157 #define CID_HGlyphConst 1007
158
159 static char *gi_aspectnames[] = {
160 N_("Exten Shapes"),
161 N_("Italic Correction"),
162 N_("Top Accent"),
163 N_("Math Kern"),
164 N_("Vert. Variants"),
165 N_("Vert. Construction"),
166 N_("Hor. Variants"),
167 N_("Hor. Construction"),
168 NULL
169 };
170
171 static char *cornernames[] = {
172 N_("Top Right"),
173 N_("Top Left"),
174 N_("Bottom Right"),
175 N_("Bottom Left"),
176 NULL
177 };
178
179 void MathInit(void) {
180 int i, j;
181 static int inited = false;
182 static struct col_init *ci[] = { exten_shape_ci, italic_cor_ci,
183 top_accent_ci, glyph_variants_ci, glyph_construction_ci,
184 extensionpart, math_kern_ci, mathkern, NULL };
185 static GTextInfo *tis[] = { truefalse, NULL };
186 static char **chars[] = { aspectnames, gi_aspectnames, cornernames, NULL };
187
188 if ( inited )
189 return;
190
191 for ( j=0; chars[j]!=NULL; ++j )
192 for ( i=0; chars[j][i]!=NULL; ++i )
193 chars[j][i] = _(chars[j][i]);
194 for ( i=0; math_constants_descriptor[i].ui_name!=NULL; ++i ) {
195 math_constants_descriptor[i].ui_name=_(math_constants_descriptor[i].ui_name);
196 if ( math_constants_descriptor[i].message != NULL )
197 math_constants_descriptor[i].message=_(math_constants_descriptor[i].message);
198 }
199
200 for ( j=0; tis[j]!=NULL; ++j )
201 for ( i=0; tis[j][i].text!=NULL; ++i )
202 tis[j][i].text = (unichar_t *) _((char *) tis[j][i].text);
203 for ( j=0; ci[j]!=NULL; ++j )
204 for ( i=0; ci[j][i].title!=NULL; ++i )
205 ci[j][i].title = _(ci[j][i].title);
206
207 inited = true;
208 }
209
210 static int GV_StringCheck(SplineFont *sf,char *str) {
211 char *start, *pt;
212 int scnt, pcnt, ch;
213 SplineChar *sc;
214
215 pcnt = 0;
216 for ( start = str ; ; ) {
217 while ( *start==' ' ) ++start;
218 if ( *start=='\0' )
219 return( pcnt );
220 for ( pt=start; *pt!=':' && *pt!=' ' && *pt!='\0' ; ++pt );
221 ch = *pt;
222 if ( ch==' ' || ch=='\0' )
223 return( -1 );
224 if ( sf!=NULL ) {
225 *pt = '\0';
226 sc = SFGetChar(sf,-1,start);
227 *pt = ch;
228 if ( sc==NULL )
229 return( -1 );
230 }
231 scnt = 0;
232 while ( *pt!=' ' && *pt!='\0' ) {
233 if ( *pt==':' ) ++scnt;
234 else if ( !isdigit( *pt ))
235 return( -1 );
236 ++pt;
237 }
238 if ( scnt!=4 )
239 return( -1 );
240 ++pcnt;
241 start = pt;
242 }
243 }
244
245 static struct glyphvariants *GV_FromString(struct glyphvariants *gv,char *str) {
246 int pcnt = GV_StringCheck(NULL,str);
247 char *start, *pt;
248 int ch, temp;
249
250 if ( pcnt<=0 )
251 return( gv );
252 if ( gv==NULL )
253 gv = chunkalloc(sizeof(struct glyphvariants));
254 gv->part_cnt = pcnt;
255 gv->parts = calloc(pcnt,sizeof(struct gv_part));
256 pcnt = 0;
257 for ( start = str ; ; ) {
258 while ( *start==' ' ) ++start;
259 if ( *start=='\0' )
260 return( gv );
261 for ( pt=start; *pt!=':' ; ++pt );
262 ch = *pt; *pt = '\0';
263 gv->parts[pcnt].component = copy(start);
264 *pt = ch;
265 sscanf(pt,":%d:%hd:%hd:%hd", &temp,
266 &gv->parts[pcnt].startConnectorLength,
267 &gv->parts[pcnt].endConnectorLength,
268 &gv->parts[pcnt].fullAdvance);
269 gv->parts[pcnt].is_extender = temp;
270 while ( *pt!=' ' && *pt!='\0' ) ++pt;
271 ++pcnt;
272 start = pt;
273 }
274 }
275
276 static char *GV_ToString(struct glyphvariants *gv) {
277 int i, len;
278 char buffer[80], *str;
279
280 if ( gv==NULL || gv->part_cnt==0 )
281 return( NULL );
282 for ( i=len=0; i<gv->part_cnt; ++i ) {
283 len += strlen(gv->parts[i].component);
284 sprintf( buffer, ":%d:%d:%d:%d ", gv->parts[i].is_extender,
285 gv->parts[i].startConnectorLength,
286 gv->parts[i].endConnectorLength,
287 gv->parts[i].fullAdvance);
288 len += strlen( buffer );
289 }
290 str = malloc(len+1);
291 for ( i=len=0; i<gv->part_cnt; ++i ) {
292 strcpy(str+len,gv->parts[i].component);
293 len += strlen(gv->parts[i].component);
294 sprintf( buffer, ":%d:%d:%d:%d ", gv->parts[i].is_extender,
295 gv->parts[i].startConnectorLength,
296 gv->parts[i].endConnectorLength,
297 gv->parts[i].fullAdvance);
298 strcpy(str+len,buffer);
299 len += strlen( buffer );
300 }
301 if ( len!=0 )
302 str[len-1] = '\0';
303 else
304 *str = '\0';
305 return( str );
306 }
307
308 static int SF_NameListCheck(SplineFont *sf,char *list) {
309 char *start, *pt;
310 int ch;
311 SplineChar *sc;
312
313 if ( list==NULL )
314 return( true );
315
316 for ( start = list ; ; ) {
317 while ( *start== ' ' ) ++start;
318 if ( *start=='\0' )
319 return( true );
320 for ( pt=start ; *pt!=' ' && *pt!='\0' && *pt!='('; ++pt );
321 ch = *pt; *pt = '\0';
322 sc = SFGetChar(sf,-1,start);
323 *pt = ch;
324 if ( ch=='(' ) {
325 while ( *pt!=')' && *pt!='\0' ) ++pt;
326 if ( *pt==')' ) ++pt;
327 }
328 start = pt;
329 if ( sc==NULL )
330 return( false );
331 }
332 }
333
334 typedef struct mathdlg {
335 GWindow gw;
336 SplineFont *sf;
337 int def_layer;
338 struct MATH *math;
339 uint8_t done;
340 uint8_t ok;
341 uint16_t popup_r;
342 GGadget *popup_g;
343 /* Used by glyphconstruction_dlg */
344 SplineChar *sc;
345 int is_horiz;
346 } MathDlg;
347
348 static unichar_t **MATH_GlyphNameCompletion(GGadget *t,int from_tab) {
349 MathDlg *math = GDrawGetUserData(GDrawGetParentWindow(GGadgetGetWindow(t)));
350 SplineFont *sf = math->sf;
351
352 return( SFGlyphNameCompletion(sf,t,from_tab,false));
353 }
354
355 static unichar_t **MATH_GlyphListCompletion(GGadget *t,int from_tab) {
356 MathDlg *math = GDrawGetUserData(GDrawGetParentWindow(GGadgetGetWindow(t)));
357 SplineFont *sf = math->sf;
358
359 return( SFGlyphNameCompletion(sf,t,from_tab,true));
360 }
361
362 static void MATH_Init(MathDlg *math) {
363 int i, cnt, ccnt, ta, h;
364 char buffer[20];
365 GGadget *g;
366 struct matrix_data *mds;
367 SplineFont *sf = math->sf;
368 SplineChar *sc;
369 int cols;
370 char *change = _("Change");
371
372 for ( i=0; math_constants_descriptor[i].ui_name!=NULL; ++i ) {
373 GGadget *tf = GWidgetGetControl(math->gw,2*i+1);
374 int16_t *pos = (int16_t *) (((char *) (math->math)) + math_constants_descriptor[i].offset );
375
376 sprintf( buffer, "%d", *pos );
377 GGadgetSetTitle8(tf,buffer);
378 if ( math_constants_descriptor[i].devtab_offset >= 0 ) {
379 GGadget *tf2 = GWidgetGetControl(math->gw,2*i+2);
380 DeviceTable **devtab = (DeviceTable **) (((char *) (math->math)) + math_constants_descriptor[i].devtab_offset );
381 char *str;
382
383 DevTabToString(&str,*devtab);
384 if ( str!=NULL )
385 GGadgetSetTitle8(tf2,str);
386 free(str);
387 }
388 }
389
390 /* Extension Shapes */
391 for ( i=cnt=0; i<sf->glyphcnt; ++i ) if ( (sc=sf->glyphs[i])!=NULL )
392 if ( sc->is_extended_shape )
393 ++cnt;
394 mds = calloc(cnt*2,sizeof(struct matrix_data));
395 for ( i=cnt=0; i<sf->glyphcnt; ++i ) if ( (sc=sf->glyphs[i])!=NULL )
396 if ( sc->is_extended_shape ) {
397 mds[2*cnt+0].u.md_str = copy(sc->name);
398 mds[2*cnt+1].u.md_ival = true;
399 ++cnt;
400 }
401 GMatrixEditSet(GWidgetGetControl(math->gw,CID_Exten), mds,cnt,false);
402
403 /* Italic Correction && Top Angle Horizontal Position */
404 for ( ta=0; ta<2; ++ta ) {
405 g = GWidgetGetControl(math->gw,ta?CID_TopAccent:CID_Italic );
406 cols = GMatrixEditGetColCnt(g);
407 for ( i=cnt=0; i<sf->glyphcnt; ++i ) if ( (sc=sf->glyphs[i])!=NULL ) {
408 if ( (ta==0 && sc->italic_correction!=TEX_UNDEF ) ||
409 (ta==1 && sc->top_accent_horiz!=TEX_UNDEF))
410 ++cnt;
411 }
412 mds = calloc(cnt*cols,sizeof(struct matrix_data));
413 for ( i=cnt=0; i<sf->glyphcnt; ++i ) if ( (sc=sf->glyphs[i])!=NULL ) {
414 if ( ta==0 && sc->italic_correction!=TEX_UNDEF ) {
415 mds[cols*cnt+0].u.md_str = copy(sc->name);
416 mds[cols*cnt+1].u.md_ival = sc->italic_correction;
417 DevTabToString(&mds[cols*cnt+2].u.md_str,sc->italic_adjusts);
418 ++cnt;
419 } else if ( ta==1 &&sc->top_accent_horiz!=TEX_UNDEF ) {
420 mds[cols*cnt+0].u.md_str = copy(sc->name);
421 mds[cols*cnt+1].u.md_ival = sc->top_accent_horiz;
422 DevTabToString(&mds[cols*cnt+2].u.md_str,sc->top_accent_adjusts);
423 ++cnt;
424 }
425 }
426 GMatrixEditSet(g, mds,cnt,false);
427 }
428
429 /* Math Kern */
430 g = GWidgetGetControl(math->gw,CID_MathKern);
431 cols = GMatrixEditGetColCnt(g);
432 for ( i=cnt=0; i<sf->glyphcnt; ++i ) if ( (sc=sf->glyphs[i])!=NULL )
433 if ( sc->mathkern!=NULL )
434 ++cnt;
435 mds = calloc(cnt*cols,sizeof(struct matrix_data));
436 for ( i=cnt=0; i<sf->glyphcnt; ++i ) if ( (sc=sf->glyphs[i])!=NULL )
437 if ( sc->mathkern!=NULL ) {
438 mds[cols*cnt+0].u.md_str = copy(sc->name);
439 mds[cols*cnt+1].u.md_str = copy(change);
440 ++cnt;
441 }
442 GMatrixEditSet(g, mds,cnt,false);
443
444 /* Horizontal/Vertical Glyph Variants */
445 for ( h=0; h<2; ++h ) {
446 g = GWidgetGetControl(math->gw,CID_VGlyphVar+2*h);
447 cols = GMatrixEditGetColCnt(g);
448 for ( i=cnt=ccnt=0; i<sf->glyphcnt; ++i ) if ( (sc=sf->glyphs[i])!=NULL ) {
449 struct glyphvariants *gv = h ? sc->horiz_variants : sc->vert_variants;
450 if ( gv!=NULL && gv->variants!=NULL )
451 ++cnt;
452 if ( gv!=NULL && gv->part_cnt!=0 )
453 ++ccnt;
454 }
455 mds = calloc(cnt*cols,sizeof(struct matrix_data));
456 for ( i=cnt=0; i<sf->glyphcnt; ++i ) if ( (sc=sf->glyphs[i])!=NULL ) {
457 struct glyphvariants *gv = h ? sc->horiz_variants : sc->vert_variants;
458 if ( gv!=NULL && gv->variants!=NULL ) {
459 mds[cols*cnt+0].u.md_str = SCNameUniStr(sc);
460 mds[cols*cnt+1].u.md_str = SFNameList2NameUni(sf,gv->variants);
461 ++cnt;
462 }
463 }
464 GMatrixEditSet(g, mds,cnt,false);
465
466 /* Glyph Construction */
467 g = GWidgetGetControl(math->gw,CID_VGlyphConst+2*h);
468 cols = GMatrixEditGetColCnt(g);
469 mds = calloc(ccnt*cols,sizeof(struct matrix_data));
470 for ( i=cnt=0; i<sf->glyphcnt; ++i ) if ( (sc=sf->glyphs[i])!=NULL ) {
471 struct glyphvariants *gv = h ? sc->horiz_variants : sc->vert_variants;
472 if ( gv!=NULL && gv->part_cnt!=0 ) {
473 mds[cols*cnt+0].u.md_str = copy(sc->name);
474 mds[cols*cnt+1].u.md_ival = gv->italic_correction;
475 DevTabToString(&mds[cols*cnt+2].u.md_str,gv->italic_adjusts);
476 mds[cols*cnt+cols-1].u.md_str = GV_ToString(gv);
477 ++cnt;
478 }
479 }
480 GMatrixEditSet(g, mds,cnt,false);
481 }
482 }
483
484 static void MATH_FreeImage(const void *_math, GImage *img) {
485 GImageDestroy(img);
486 }
487
488 static GImage *_MATHVar_GetImage(const void *_math) {
489 MathDlg *math = (MathDlg *) _math;
490 GGadget *varlist = math->popup_g;
491 int rows, cols = GMatrixEditGetColCnt(varlist);
492 struct matrix_data *old = GMatrixEditGet(varlist,&rows);
493 SplineChar *sc = SFGetChar(math->sf,-1, old[cols*math->popup_r].u.md_str);
494 static OTLookup dummyl = OTLOOKUP_EMPTY;
495 static struct lookup_subtable dummys = LOOKUP_SUBTABLE_EMPTY;
496
497 dummyl.lookup_type = gsub_multiple;
498 dummys.lookup = &dummyl;
499 return( PST_GetImage(varlist,math->sf,math->def_layer,&dummys,math->popup_r,sc) );
500 }
501
502 static void MATHVar_PopupPrepare(GGadget *g, int r, int c) {
503 MathDlg *math = GDrawGetUserData(GGadgetGetWindow(g));
504 int rows, cols = GMatrixEditGetColCnt(g);
505 struct matrix_data *old = GMatrixEditGet(g,&rows);
506
507 if ( c<0 || c>=cols || r<0 || r>=rows || old[cols*r].u.md_str==NULL ||
508 SFGetChar(math->sf,-1, old[cols*r+0].u.md_str)==NULL )
509 return;
510 math->popup_r = r;
511 math->popup_g = g;
512 GGadgetPreparePopupImage(GGadgetGetWindow(g),NULL,math,_MATHVar_GetImage,MATH_FreeImage);
513 }
514
515 static GImage *_MATHConst_GetImage(const void *_math) {
516 MathDlg *math = (MathDlg *) _math;
517 GGadget *varlist = math->popup_g;
518 int rows, cols = GMatrixEditGetColCnt(varlist);
519 struct matrix_data *old = GMatrixEditGet(varlist,&rows);
520 SplineChar *sc = SFGetChar(math->sf,-1, old[cols*math->popup_r].u.md_str);
521 struct glyphvariants *gv = GV_FromString(NULL,old[cols*math->popup_r+cols-1].u.md_str);
522 GImage *ret;
523
524 ret = GV_GetConstructedImage(sc,math->def_layer,gv,GGadgetGetCid(varlist)==CID_HGlyphConst);
525 GlyphVariantsFree(gv);
526 return( ret );
527 }
528
529 static void MATHConst_PopupPrepare(GGadget *g, int r, int c) {
530 MathDlg *math = GDrawGetUserData(GGadgetGetWindow(g));
531 int rows, cols = GMatrixEditGetColCnt(g);
532 struct matrix_data *old = GMatrixEditGet(g,&rows);
533
534 if ( c<0 || c>=cols || r<0 || r>=rows || old[cols*r].u.md_str==NULL ||
535 SFGetChar(math->sf,-1, old[cols*r+0].u.md_str)==NULL )
536 return;
537 math->popup_r = r;
538 math->popup_g = g;
539 GGadgetPreparePopupImage(GGadgetGetWindow(g),NULL,math,_MATHConst_GetImage,MATH_FreeImage);
540 }
541
542 static GImage *_MATHLine_GetImage(const void *_math) {
543 MathDlg *math = (MathDlg *) _math;
544 GGadget *varlist = math->popup_g;
545 int rows, cols = GMatrixEditGetColCnt(varlist);
546 struct matrix_data *old = GMatrixEditGet(varlist,&rows);
547 SplineChar *sc = SFGetChar(math->sf,-1, old[cols*math->popup_r].u.md_str);
548
549 return( SC_GetLinedImage(sc,math->def_layer,old[cols*math->popup_r+1].u.md_ival,GGadgetGetCid(varlist)==CID_Italic));
550 }
551
552 static void MATHLine_PopupPrepare(GGadget *g, int r, int c) {
553 MathDlg *math = GDrawGetUserData(GGadgetGetWindow(g));
554 int rows, cols = GMatrixEditGetColCnt(g);
555 struct matrix_data *old = GMatrixEditGet(g,&rows);
556
557 if ( c<0 || c>=cols || r<0 || r>=rows || old[cols*r].u.md_str==NULL ||
558 SFGetChar(math->sf,-1, old[cols*r+0].u.md_str)==NULL )
559 return;
560 math->popup_r = r;
561 math->popup_g = g;
562 GGadgetPreparePopupImage(GGadgetGetWindow(g),NULL,math,_MATHLine_GetImage,MATH_FreeImage);
563 }
564
565 static GImage *_GVC_GetImage(const void *_math) {
566 MathDlg *math = (MathDlg *) _math;
567 GGadget *varlist = math->popup_g;
568 int rows, cols = GMatrixEditGetColCnt(varlist);
569 struct matrix_data *old = GMatrixEditGet(varlist,&rows);
570 GImage *ret;
571 struct glyphvariants *gv;
572
573 gv = GV_ParseConstruction(NULL,old,rows,cols);
574 ret = GV_GetConstructedImage(math->sc,math->def_layer,gv,math->is_horiz);
575 GlyphVariantsFree(gv);
576 return( ret );
577 }
578
579 static void italic_finishedit(GGadget *g, int r, int c, int wasnew) {
580 int rows;
581 struct matrix_data *stuff;
582 MathDlg *math;
583 int cols;
584 DBounds b;
585 SplineChar *sc;
586
587 if ( c!=0 )
588 return;
589 if ( !wasnew )
590 return;
591 /* If they added a new glyph to the sequence then set some defaults for it. */
592 /* only the full advance has any likelihood of being correct */
593 math = GDrawGetUserData(GGadgetGetWindow(g));
594 stuff = GMatrixEditGet(g, &rows);
595 cols = GMatrixEditGetColCnt(g);
596 if ( stuff[r*cols+0].u.md_str==NULL )
597 return;
598 sc = SFGetChar(math->sf,-1,stuff[r*cols+0].u.md_str);
599 if ( sc==NULL )
600 return;
601 SplineCharFindBounds(sc,&b);
602 if ( b.maxx>sc->width ) {
603 stuff[r*cols+1].u.md_ival = rint((b.maxx-sc->width) +
604 (math->sf->ascent+math->sf->descent)/16.0);
605 GGadgetRedraw(g);
606 }
607 }
608
609 static void topaccent_finishedit(GGadget *g, int r, int c, int wasnew) {
610 int rows;
611 struct matrix_data *stuff;
612 MathDlg *math;
613 int cols;
614 DBounds b;
615 SplineChar *sc;
616 double italic_off;
617
618 if ( c!=0 )
619 return;
620 if ( !wasnew )
621 return;
622 /* If they added a new glyph to the sequence then set some defaults for it. */
623 /* only the full advance has any likelihood of being correct */
624 math = GDrawGetUserData(GGadgetGetWindow(g));
625 stuff = GMatrixEditGet(g, &rows);
626 cols = GMatrixEditGetColCnt(g);
627 if ( stuff[r*cols+0].u.md_str==NULL )
628 return;
629 sc = SFGetChar(math->sf,-1,stuff[r*cols+0].u.md_str);
630 if ( sc==NULL )
631 return;
632 SplineCharFindBounds(sc,&b);
633 italic_off = (b.maxy-b.miny)*tan(-math->sf->italicangle);
634 if ( b.maxx-b.minx-italic_off < 0 )
635 stuff[r*cols+1].u.md_ival = rint(b.minx + (b.maxx-b.minx)/2);
636 else
637 stuff[r*cols+1].u.md_ival = rint(b.minx + italic_off + (b.maxx - b.minx - italic_off)/2);
638 GGadgetRedraw(g);
639 }
640
641 static void mathkern_initrow(GGadget *g, int r) {
642 int rows;
643 struct matrix_data *stuff;
644 int cols;
645
646 cols = GMatrixEditGetColCnt(g);
647 stuff = GMatrixEditGet(g, &rows);
648 stuff[r*cols+1].u.md_str = copy(_("Change"));
649 };
650
651 static void extpart_finishedit(GGadget *g, int r, int c, int wasnew) {
652 int rows;
653 struct matrix_data *stuff;
654 MathDlg *math;
655 int cols;
656 DBounds b;
657 double full_advance;
658 SplineChar *sc;
659
660 if ( c!=0 )
661 return;
662 if ( !wasnew )
663 return;
664 /* If they added a new glyph to the sequence then set some defaults for it. */
665 /* only the full advance has any likelihood of being correct */
666 math = GDrawGetUserData(GGadgetGetWindow(g));
667 stuff = GMatrixEditGet(g, &rows);
668 cols = GMatrixEditGetColCnt(g);
669 if ( stuff[r*cols+0].u.md_str==NULL )
670 return;
671 sc = SFGetChar(math->sf,-1,stuff[r*cols+0].u.md_str);
672 if ( sc==NULL )
673 return;
674 SplineCharFindBounds(sc,&b);
675 if ( math->is_horiz )
676 full_advance = b.maxx - b.minx;
677 else
678 full_advance = b.maxy - b.miny;
679 stuff[r*cols+2].u.md_ival = stuff[r*cols+3].u.md_ival = rint(full_advance/3);
680 stuff[r*cols+4].u.md_ival = rint(full_advance);
681 GGadgetRedraw(g);
682 }
683
684 static void GVC_PopupPrepare(GGadget *g, int r, int c) {
685 MathDlg *math = GDrawGetUserData(GGadgetGetWindow(g));
686
687 math->popup_g = g;
688 if ( math->sc==NULL )
689 return;
690 GGadgetPreparePopupImage(GGadgetGetWindow(g),NULL,math,_GVC_GetImage,MATH_FreeImage);
691 }
692
693 static int GVC_OK(GGadget *g, GEvent *e) {
694 if ( e->type==et_controlevent && e->u.control.subtype == et_buttonactivate ) {
695 MathDlg *math = GDrawGetUserData(GGadgetGetWindow(g));
696 math->done = true;
697 math->ok = true;
698 }
699 return( true );
700 }
701
702 static int MATH_Cancel(GGadget *g, GEvent *e) {
703 if ( e->type==et_controlevent && e->u.control.subtype == et_buttonactivate ) {
704 MathDlg *math = GDrawGetUserData(GGadgetGetWindow(g));
705 math->done = true;
706 }
707 return( true );
708 }
709
710 static int gc_e_h(GWindow gw, GEvent *event) {
711 MathDlg *math = GDrawGetUserData(gw);
712
713 if ( event->type==et_close ) {
714 math->done = true;
715 } else if ( event->type==et_char ) {
716 if ( event->u.chr.keysym == GK_F1 || event->u.chr.keysym == GK_Help ) {
717 help("ui/dialogs/math.html", "#math-glyphconstruction");
718 return( true );
719 } else if ( GMenuIsCommand(event,H_("Quit|Ctl+Q") )) {
720 MenuExit(NULL,NULL,NULL);
721 } else if ( GMenuIsCommand(event,H_("Close|Ctl+Shft+Q") )) {
722 math->done = true;
723 }
724 return( false );
725 }
726 return( true );
727 }
728
729 static char *GlyphConstruction_Dlg(GGadget *g, int r, int c) {
730 MathDlg *math = GDrawGetUserData(GGadgetGetWindow(g));
731 MathDlg md;
732 GRect pos;
733 GWindow gw;
734 GWindowAttrs wattrs;
735 int rows, cols = GMatrixEditGetColCnt(g);
736 struct matrix_data *old = GMatrixEditGet(g,&rows);
737 GGadgetCreateData *harray[7], mgcd[4], *varray[6], mboxes[3];
738 GTextInfo mlabel[3];
739 struct glyphvariants *gv;
740 char *ret;
741
742 memset(&md,0,sizeof(md));
743 md.sf = math->sf;
744 md.is_horiz = GGadgetGetCid(g)==CID_HGlyphConst;
745 md.sc = SFGetChar(md.sf,-1,old[r*cols+0].u.md_str);
746
747 memset(&wattrs,0,sizeof(wattrs));
748 wattrs.mask = wam_events|wam_cursor|wam_utf8_wtitle|wam_undercursor|wam_restrict|wam_isdlg;
749 wattrs.event_masks = ~(1<<et_charup);
750 wattrs.restrict_input_to_me = 1;
751 wattrs.is_dlg = 1;
752 wattrs.undercursor = 1;
753 wattrs.cursor = ct_pointer;
754 wattrs.utf8_window_title = _("Glyph Construction");
755 pos.x = pos.y = 0;
756 pos.width = 100;
757 pos.height = 100;
758 md.gw = gw = GDrawCreateTopWindow(NULL,&pos,gc_e_h,&md,&wattrs);
759
760 memset(mgcd,0,sizeof(mgcd));
761 memset(mlabel,0,sizeof(mlabel));
762 memset(mboxes,0,sizeof(mboxes));
763
764 mgcd[0].gd.flags = gg_visible | gg_enabled;
765 mgcd[0].gd.u.matrix = &mi_extensionpart;
766 mgcd[0].gd.cid = CID_VGlyphConst;
767 mgcd[0].creator = GMatrixEditCreate;
768
769 mgcd[1].gd.flags = gg_visible | gg_enabled | gg_but_default;
770 mlabel[1].text = (unichar_t *) _("_OK");
771 mlabel[1].text_is_1byte = true;
772 mlabel[1].text_in_resource = true;
773 mgcd[1].gd.label = &mlabel[1];
774 mgcd[1].gd.handle_controlevent = GVC_OK;
775 mgcd[1].creator = GButtonCreate;
776
777 mgcd[2].gd.flags = gg_visible | gg_enabled | gg_but_cancel;
778 mlabel[2].text = (unichar_t *) _("_Cancel");
779 mlabel[2].text_is_1byte = true;
780 mlabel[2].text_in_resource = true;
781 mgcd[2].gd.label = &mlabel[2];
782 mgcd[2].gd.handle_controlevent = MATH_Cancel;
783 mgcd[2].creator = GButtonCreate;
784
785 harray[0] = GCD_Glue; harray[1] = &mgcd[1]; harray[2] = GCD_Glue;
786 harray[3] = GCD_Glue; harray[4] = &mgcd[2]; harray[5] = GCD_Glue;
787 harray[6] = NULL;
788
789 mboxes[2].gd.flags = gg_enabled|gg_visible;
790 mboxes[2].gd.u.boxelements = harray;
791 mboxes[2].creator = GHBoxCreate;
792
793 varray[0] = &mgcd[0]; varray[1] = NULL;
794 varray[2] = &mboxes[2]; varray[3] = NULL;
795 varray[4] = NULL;
796
797 mboxes[0].gd.pos.x = mboxes[0].gd.pos.y = 2;
798 mboxes[0].gd.flags = gg_enabled|gg_visible;
799 mboxes[0].gd.u.boxelements = varray;
800 mboxes[0].creator = GHVGroupCreate;
801
802 GGadgetsCreate(gw,mboxes);
803 GHVBoxSetExpandableRow(mboxes[0].ret,0);
804 GHVBoxSetExpandableCol(mboxes[2].ret,gb_expandgluesame);
805 GMatrixEditSetColumnCompletion(mgcd[0].ret,0,MATH_GlyphNameCompletion);
806 GMatrixEditSetMouseMoveReporter(mgcd[0].ret,GVC_PopupPrepare);
807
808 /* If it's unparseable, this will give 'em nothing */
809 gv = GV_FromString(NULL,old[r*cols+cols-1].u.md_str);
810 GV_ToMD(mgcd[0].ret,gv);
811 GlyphVariantsFree(gv);
812
813 GHVBoxFitWindow(mboxes[0].ret);
814
815 GDrawSetVisible(md.gw,true);
816
817 while ( !md.done )
818 GDrawProcessOneEvent(NULL);
819
820 if ( md.ok ) {
821 int rs, cs = GMatrixEditGetColCnt(mgcd[0].ret);
822 struct matrix_data *stuff = GMatrixEditGet(mgcd[0].ret,&rs);
823 gv = GV_ParseConstruction(NULL,stuff,rs,cs);
824 ret = GV_ToString(gv);
825 GlyphVariantsFree(gv);
826 } else
827 ret = copy( old[r*cols+cols-1].u.md_str );
828 GDrawDestroyWindow(md.gw);
829 return( ret );
830 }
831
832 static char *MKChange_Dlg(GGadget *g, int r, int c) {
833 MathDlg *math = GDrawGetUserData(GGadgetGetWindow(g));
834 int rows, cols = GMatrixEditGetColCnt(g);
835 struct matrix_data *old = GMatrixEditGet(g,&rows);
836 SplineChar *sc;
837
838 if ( old[r*cols+0].u.md_str==NULL )
839 return( NULL );
840 sc = SFGetChar(math->sf,-1,old[r*cols+0].u.md_str);
841 if ( sc==NULL )
842 return( NULL );
843
844 MathKernDialog(sc,math->def_layer);
845 return( NULL );
846 }
847
848 static int MATH_OK(GGadget *g, GEvent *e) {
849 if ( e->type==et_controlevent && e->u.control.subtype == et_buttonactivate ) {
850 MathDlg *math = GDrawGetUserData(GGadgetGetWindow(g));
851 int err=false;
852 int cid,i;
853 int high,low;
854 SplineFont *sf = math->sf;
855 SplineChar *sc;
856
857 /* Two passes. First checks that everything is parsable */
858 for ( i=0; math_constants_descriptor[i].ui_name!=NULL; ++i ) {
859 GetInt8(math->gw,2*i+1,math_constants_descriptor[i].ui_name,&err);
860 if ( err )
861 return( true );
862 if ( math_constants_descriptor[i].devtab_offset >= 0 ) {
863 GGadget *tf2 = GWidgetGetControl(math->gw,2*i+2);
864 char *str = GGadgetGetTitle8(tf2);
865 if ( !DeviceTableOK(str,&low,&high)) {
866 ff_post_error(_("Bad device table"), _("Bad device table for %s"),
867 math_constants_descriptor[i].ui_name);
868 free(str);
869 return( true );
870 }
871 free(str);
872 }
873 }
874 /* Now check that the various glyph lists are parseable */
875 for ( cid=CID_Exten; cid<=CID_HGlyphConst; ++cid ) {
876 GGadget *g = GWidgetGetControl(math->gw,cid);
877 int rows, cols = GMatrixEditGetColCnt(g);
878 struct matrix_data *old = GMatrixEditGet(g,&rows);
879 for ( i=0; i<rows; ++i ) {
880 if ( SFGetChar(sf,-1,old[i*cols+0].u.md_str)==NULL ) {
881 ff_post_error(_("Missing Glyph"), _("There is no glyph named %s (used in %s)"),
882 old[i*cols+0].u.md_str, gi_aspectnames[cid-CID_Exten]);
883 return( true );
884 }
885 if ( cid==CID_Italic || cid==CID_TopAccent ||
886 cid == CID_VGlyphConst || cid == CID_HGlyphConst ) {
887 if ( !DeviceTableOK(old[i*cols+2].u.md_str,&low,&high)) {
888 ff_post_error(_("Bad device table"), _("Bad device table for glyph %s in %s"),
889 old[i*cols+0].u.md_str, gi_aspectnames[cid-CID_Exten]);
890 return( true );
891 }
892 }
893 if ( cid == CID_VGlyphConst || cid == CID_HGlyphConst ) {
894 if ( GV_StringCheck(sf,old[i*cols+cols-1].u.md_str)==-1 ) {
895 ff_post_error(_("Bad Parts List"), _("Bad parts list for glyph %s in %s"),
896 old[i*cols+0].u.md_str, gi_aspectnames[cid-CID_Exten]);
897 return( true );
898 }
899 }
900 if ( cid == CID_VGlyphVar || cid == CID_HGlyphVar ) {
901 if ( !SF_NameListCheck(sf,old[i*cols+1].u.md_str)) {
902 ff_post_error(_("Bad Variants List"), _("Bad Variants list for glyph %s in %s"),
903 old[i*cols+0].u.md_str, gi_aspectnames[cid-CID_Exten]);
904 return( true );
905 }
906 }
907 }
908 }
909
910 /*********************************************/
911 /* Ok, if we got this far it should be legal */
912 /*********************************************/
913 for ( i=0; math_constants_descriptor[i].ui_name!=NULL; ++i ) {
914 int16_t *pos = (int16_t *) (((char *) (math->math)) + math_constants_descriptor[i].offset );
915 *pos = GetInt8(math->gw,2*i+1,math_constants_descriptor[i].ui_name,&err);
916
917 if ( math_constants_descriptor[i].devtab_offset >= 0 ) {
918 GGadget *tf2 = GWidgetGetControl(math->gw,2*i+2);
919 char *str = GGadgetGetTitle8(tf2);
920 DeviceTable **devtab = (DeviceTable **) (((char *) (math->math)) + math_constants_descriptor[i].devtab_offset );
921
922 *devtab = DeviceTableParse(*devtab,str);
923 free(str);
924 }
925 }
926 sf->MATH = math->math;
927
928 /* As for the per-glyph stuff... Well the only way I can insure that */
929 /* things which have been removed in the dlg are removed in the font */
930 /* is to clear everything now, and start from a blank slate when I */
931 /* parse stuff. (Except for math kerning which I don't support here) */
932 for ( i=0; i<sf->glyphcnt; ++i ) if ( (sc=sf->glyphs[i])!=NULL ) {
933 sc->is_extended_shape = false;
934 sc->italic_correction = TEX_UNDEF;
935 sc->top_accent_horiz = TEX_UNDEF;
936 DeviceTableFree(sc->italic_adjusts);
937 DeviceTableFree(sc->top_accent_adjusts);
938 sc->italic_adjusts = sc->top_accent_adjusts = NULL;
939 GlyphVariantsFree(sc->vert_variants);
940 GlyphVariantsFree(sc->horiz_variants);
941 sc->vert_variants = sc->horiz_variants = NULL;
942 /* MathKernFree(sc->mathkern); sc->mathkern = NULL; */
943 }
944 /* Then process each table to set whatever it sets */
945 for ( cid=CID_Exten; cid<=CID_HGlyphConst; ++cid ) {
946 GGadget *g = GWidgetGetControl(math->gw,cid);
947 int rows, cols = GMatrixEditGetColCnt(g);
948 struct matrix_data *old = GMatrixEditGet(g,&rows);
949 for ( i=0; i<rows; ++i ) {
950 sc = SFGetChar(sf,-1,old[i*cols+0].u.md_str);
951 if ( cid==CID_Exten )
952 sc->is_extended_shape = old[i*cols+1].u.md_ival;
953 else if ( cid==CID_Italic ) {
954 sc->italic_correction = old[i*cols+1].u.md_ival;
955 sc->italic_adjusts = DeviceTableParse(NULL,old[i*cols+2].u.md_str);
956 } else if ( cid==CID_TopAccent ) {
957 sc->top_accent_horiz = old[i*cols+1].u.md_ival;
958 sc->top_accent_adjusts = DeviceTableParse(NULL,old[i*cols+2].u.md_str);
959 } else if ( cid==CID_VGlyphVar || cid==CID_HGlyphVar ) {
960 struct glyphvariants **gvp = cid == CID_VGlyphVar ?
961 &sc->vert_variants : &sc->horiz_variants;
962 char *str = old[i*cols+1].u.md_str;
963 if ( str!=NULL ) while ( *str==' ' ) ++str;
964 if ( str!=NULL && *str!='\0' ) {
965 *gvp = chunkalloc(sizeof(struct glyphvariants));
966 (*gvp)->variants = GlyphNameListDeUnicode( str );
967 }
968 } else if ( cid==CID_VGlyphConst || cid==CID_HGlyphConst ) {
969 struct glyphvariants **gvp = cid == CID_VGlyphConst ?
970 &sc->vert_variants : &sc->horiz_variants;
971 *gvp = GV_FromString(*gvp,old[cols*i+cols-1].u.md_str);
972 if ( *gvp!=NULL && (*gvp)->part_cnt!=0 ) {
973 (*gvp)->italic_correction = old[i*cols+1].u.md_ival;
974 (*gvp)->italic_adjusts = DeviceTableParse(NULL,old[i*cols+2].u.md_str);
975 }
976 }
977 }
978 }
979
980 /* Done! */
981
982 math->done = true;
983 math->ok = true;
984 }
985 return( true );
986 }
987
988 static int math_e_h(GWindow gw, GEvent *event) {
989 MathDlg *math = GDrawGetUserData(gw);
990
991 if ( event->type==et_close ) {
992 math->done = true;
993 } else if ( event->type==et_char ) {
994 if ( event->u.chr.keysym == GK_F1 || event->u.chr.keysym == GK_Help ) {
995 help("ui/dialogs/math.html", NULL);
996 return( true );
997 } else if ( GMenuIsCommand(event,H_("Quit|Ctl+Q") )) {
998 MenuExit(NULL,NULL,NULL);
999 } else if ( GMenuIsCommand(event,H_("Close|Ctl+Shft+Q") )) {
1000 math->done = true;
1001 }
1002 return( false );
1003 }
1004 return( true );
1005 }
1006
1007 #define MAX_PAGE 9
1008 #define MAX_ROW 12
1009
1010 void SFMathDlg(SplineFont *sf,int def_layer) {
1011 MathDlg md;
1012 int i, j, page, row, h;
1013 GGadget *g;
1014 GRect pos;
1015 GWindow gw;
1016 GWindowAttrs wattrs;
1017 GGadgetCreateData gcd[MAX_PAGE][MAX_ROW][3], boxes[MAX_PAGE][2],
1018 *hvarray[MAX_PAGE][MAX_ROW+1][4], *harray[7], mgcd[4],
1019 *varray[6], mboxes[3], gi[8][2];
1020 GTextInfo label[MAX_PAGE][MAX_ROW], mlabel[3];
1021 GTabInfo aspects[MAX_PAGE+8+1];
1022
1023 MathInit();
1024
1025 memset(&md,0,sizeof(md));
1026 if ( sf->cidmaster ) sf = sf->cidmaster;
1027 md.sf = sf;
1028 md.def_layer = def_layer;
1029 md.math = sf->MATH;
1030 if ( md.math==NULL )
1031 md.math = MathTableNew(sf);
1032
1033 memset(&wattrs,0,sizeof(wattrs));
1034 wattrs.mask = wam_events|wam_cursor|wam_utf8_wtitle|wam_undercursor|wam_restrict|wam_isdlg;
1035 wattrs.event_masks = ~(1<<et_charup);
1036 wattrs.restrict_input_to_me = 1;
1037 wattrs.is_dlg = 1;
1038 wattrs.undercursor = 1;
1039 wattrs.cursor = ct_pointer;
1040 wattrs.utf8_window_title = _("MATH table");
1041 pos.x = pos.y = 0;
1042 pos.width = 100;
1043 pos.height = 100;
1044 md.gw = gw = GDrawCreateTopWindow(NULL,&pos,math_e_h,&md,&wattrs);
1045
1046 memset(gcd,0,sizeof(gcd));
1047 memset(label,0,sizeof(label));
1048 memset(boxes,0,sizeof(boxes));
1049 memset(aspects,0,sizeof(aspects));
1050 memset(gi,0,sizeof(gi));
1051
1052 page = row = 0;
1053 for ( i=0; math_constants_descriptor[i].ui_name!=NULL; ++i ) {
1054 if ( math_constants_descriptor[i].new_page ) {
1055 hvarray[page][row][0] = hvarray[page][row][1] = hvarray[page][row][2] = GCD_Glue;
1056 hvarray[page][row][3] = NULL;
1057 hvarray[page][row+1][0] = NULL;
1058 ++page;
1059 if ( page>=MAX_PAGE ) {
1060 IError( "Too many pages" );
1061 return;
1062 }
1063 row = 0;
1064 }
1065
1066 label[page][row].text = (unichar_t *) math_constants_descriptor[i].ui_name;
1067 label[page][row].text_is_1byte = true;
1068 label[page][row].text_in_resource = true;
1069 gcd[page][row][0].gd.label = &label[page][row];
1070 gcd[page][row][0].gd.flags = gg_visible | gg_enabled;
1071 gcd[page][row][0].gd.popup_msg = math_constants_descriptor[i].message;
1072 gcd[page][row][0].creator = GLabelCreate;
1073 hvarray[page][row][0] = &gcd[page][row][0];
1074
1075 gcd[page][row][1].gd.flags = gg_visible | gg_enabled;
1076 gcd[page][row][1].gd.pos.width = 50;
1077 gcd[page][row][1].gd.cid = 2*i+1;
1078 gcd[page][row][1].gd.popup_msg = math_constants_descriptor[i].message;
1079 gcd[page][row][1].creator = GTextFieldCreate;
1080 hvarray[page][row][1] = &gcd[page][row][1];
1081
1082 if ( math_constants_descriptor[i].devtab_offset>=0 ) {
1083 gcd[page][row][2].gd.flags = gg_visible | gg_enabled;
1084 gcd[page][row][2].gd.cid = 2*i+2;
1085 gcd[page][row][2].gd.popup_msg = math_constants_descriptor[i].message;
1086 gcd[page][row][2].creator = GTextFieldCreate;
1087 hvarray[page][row][2] = &gcd[page][row][2];
1088 } else
1089 hvarray[page][row][2] = GCD_Glue;
1090 hvarray[page][row][3] = NULL;
1091
1092 if ( ++row>=MAX_ROW ) {
1093 IError( "Too many rows" );
1094 return;
1095 }
1096 }
1097 hvarray[page][row][0] = hvarray[page][row][1] = hvarray[page][row][2] = GCD_Glue;
1098 hvarray[page][row][3] = NULL;
1099 hvarray[page][row+1][0] = NULL;
1100
1101 for ( i=0; aspectnames[i]!=NULL; ++i ) {
1102 boxes[i][0].gd.flags = gg_enabled|gg_visible;
1103 boxes[i][0].gd.u.boxelements = hvarray[i][0];
1104 boxes[i][0].creator = GHVBoxCreate;
1105
1106 aspects[i].text = (unichar_t *) aspectnames[i];
1107 aspects[i].text_is_1byte = true;
1108 aspects[i].nesting = i!=0;
1109 aspects[i].gcd = boxes[i];
1110 }
1111 if ( i!=page+1 ) { /* Page never gets its final increment */
1112 IError( "Page miscount %d in descriptor table, but only %d names.", page+1, i );
1113 return;
1114 }
1115
1116 for ( j=0; mis[j].col_cnt!=0; ++j ) {
1117 gi[j][0].gd.flags = gg_enabled|gg_visible;
1118 gi[j][0].gd.u.matrix = &mis[j];
1119 gi[j][0].gd.cid = CID_Exten+j;
1120 gi[j][0].creator = GMatrixEditCreate;
1121
1122 aspects[i+j].text = (unichar_t *) gi_aspectnames[j];
1123 aspects[i+j].text_is_1byte = true;
1124 aspects[i+j].gcd = gi[j];
1125 }
1126
1127 memset(mgcd,0,sizeof(mgcd));
1128 memset(mlabel,0,sizeof(mlabel));
1129 memset(mboxes,0,sizeof(mboxes));
1130
1131 mgcd[0].gd.u.tabs = aspects;
1132 mgcd[0].gd.flags = gg_visible | gg_enabled | gg_tabset_vert;
1133 /*mgcd[0].gd.cid = CID_Tabs;*/
1134 mgcd[0].creator = GTabSetCreate;
1135
1136 mgcd[1].gd.flags = gg_visible | gg_enabled | gg_but_default;
1137 mlabel[1].text = (unichar_t *) _("_OK");
1138 mlabel[1].text_is_1byte = true;
1139 mlabel[1].text_in_resource = true;
1140 mgcd[1].gd.label = &mlabel[1];
1141 mgcd[1].gd.handle_controlevent = MATH_OK;
1142 mgcd[1].creator = GButtonCreate;
1143
1144 mgcd[2].gd.flags = gg_visible | gg_enabled | gg_but_cancel;
1145 mlabel[2].text = (unichar_t *) _("_Cancel");
1146 mlabel[2].text_is_1byte = true;
1147 mlabel[2].text_in_resource = true;
1148 mgcd[2].gd.label = &mlabel[2];
1149 mgcd[2].gd.handle_controlevent = MATH_Cancel;
1150 mgcd[2].creator = GButtonCreate;
1151
1152 harray[0] = GCD_Glue; harray[1] = &mgcd[1]; harray[2] = GCD_Glue;
1153 harray[3] = GCD_Glue; harray[4] = &mgcd[2]; harray[5] = GCD_Glue;
1154 harray[6] = NULL;
1155
1156 mboxes[2].gd.flags = gg_enabled|gg_visible;
1157 mboxes[2].gd.u.boxelements = harray;
1158 mboxes[2].creator = GHBoxCreate;
1159
1160 varray[0] = &mgcd[0]; varray[1] = NULL;
1161 varray[2] = &mboxes[2]; varray[3] = NULL;
1162 varray[4] = NULL;
1163
1164 mboxes[0].gd.pos.x = mboxes[0].gd.pos.y = 2;
1165 mboxes[0].gd.flags = gg_enabled|gg_visible;
1166 mboxes[0].gd.u.boxelements = varray;
1167 mboxes[0].creator = GHVGroupCreate;
1168
1169 GGadgetsCreate(gw,mboxes);
1170 GHVBoxSetExpandableRow(mboxes[0].ret,0);
1171 GHVBoxSetExpandableCol(mboxes[2].ret,gb_expandgluesame);
1172 for ( i=0; aspectnames[i]!=NULL; ++i ) {
1173 GHVBoxSetExpandableCol(boxes[i][0].ret,2);
1174 GHVBoxSetExpandableRow(boxes[i][0].ret,gb_expandglue);
1175 }
1176 for ( j=0; mis[j].col_cnt!=0; ++j )
1177 GMatrixEditSetColumnCompletion(gi[j][0].ret,0,MATH_GlyphNameCompletion);
1178 for ( h=0; h<2; ++h ) {
1179 g = GWidgetGetControl(md.gw,CID_VGlyphVar+2*h);
1180 GMatrixEditSetColumnCompletion(g,1,MATH_GlyphListCompletion);
1181 GMatrixEditSetMouseMoveReporter(g,MATHVar_PopupPrepare);
1182 g = GWidgetGetControl(md.gw,CID_VGlyphConst+2*h);
1183 GMatrixEditSetMouseMoveReporter(g,MATHConst_PopupPrepare);
1184 }
1185 GMatrixEditSetMouseMoveReporter(GWidgetGetControl(md.gw,CID_Italic),MATHLine_PopupPrepare);
1186 GMatrixEditSetMouseMoveReporter(GWidgetGetControl(md.gw,CID_TopAccent),MATHLine_PopupPrepare);
1187 MATH_Init(&md);
1188 GHVBoxFitWindow(mboxes[0].ret);
1189
1190 GDrawSetVisible(md.gw,true);
1191
1192 while ( !md.done )
1193 GDrawProcessOneEvent(NULL);
1194 if ( sf->MATH==NULL && !md.ok )
1195 MATHFree(md.math);
1196
1197 GDrawDestroyWindow(md.gw);
1198 }
1199
1200 /* ************************************************************************** */
1201 /* ****************************** Math Kern Dlg ***************************** */
1202 /* ************************************************************************** */
1203
1204 #define CID_TopBox 1000
1205 #define CID_Glyph 1001
1206 #define CID_Tabs 1002
1207 #define CID_Corners 1003
1208 #define CID_TopRight 1004
1209 #define CID_TopLeft 1005
1210 #define CID_BottomRight 1006
1211 #define CID_BottomLeft 1007
1212
1213 static void MKD_SetGlyphList(MathKernDlg *mkd, SplineChar *sc) {
1214 SplineFont *sf = sc->parent;
1215 int k,cnt, gid;
1216 GTextInfo **tis = NULL;
1217 SplineChar *test;
1218
1219 for ( k=0; k<2; ++k ) {
1220 cnt = 0;
1221 for ( gid=0; gid<sf->glyphcnt; ++gid ) if ( (test=sf->glyphs[gid])!=NULL ) {
1222 if ( test==sc || test->mathkern!=NULL ) {
1223 if ( k ) {
1224 tis[cnt] = calloc(1,sizeof(GTextInfo));
1225 tis[cnt]->text = utf82u_copy(test->name);
1226 tis[cnt]->userdata = test;
1227 tis[cnt]->selected = test==sc;
1228 tis[cnt]->fg = tis[cnt]->bg = COLOR_DEFAULT;
1229 }
1230 ++cnt;
1231 }
1232 }
1233 if ( !k )
1234 tis = malloc((cnt+1)*sizeof(GTextInfo *));
1235 else
1236 tis[cnt] = calloc(1,sizeof(GTextInfo));
1237 }
1238 GGadgetSetList(GWidgetGetControl(mkd->gw,CID_Glyph),tis,false);
1239 }
1240
1241 static void MKDSubResize(MathKernDlg *mkd, GEvent *event) {
1242 int width, height;
1243 int i;
1244 GRect r;
1245
1246 if ( !event->u.resize.sized )
1247 return;
1248
1249 width = (event->u.resize.size.width-4*mkd->mid_space)/4;
1250 height = (event->u.resize.size.height-mkd->cv_y-8);
1251 if ( width<70 || height<80 ) {
1252 if ( width<70 ) width = 70;
1253 width = 4*(width+mkd->mid_space);
1254 if ( height<80 ) height = 80;
1255 height += mkd->cv_y+mkd->button_height+8;
1256 GDrawGetSize(mkd->gw,&r);
1257 width += r.width-event->u.resize.size.width;
1258 height += r.height-event->u.resize.size.height;
1259 GDrawResize(mkd->gw,width,height);
1260 return;
1261 }
1262 if ( width!=mkd->cv_width || height!=mkd->cv_height ) {
1263 mkd->cv_width = width; mkd->cv_height = height;
1264 for ( i=0; i<4; ++i ) {
1265 CharView *cv = (&mkd->cv_topright)+i;
1266 GDrawResize(cv->gw,width,height);
1267 if ( i!=0 )
1268 GDrawMove(cv->gw,10+i*(mkd->cv_width+mkd->mid_space),mkd->cv_y);
1269 }
1270 }
1271
1272 GDrawSync(NULL);
1273 GDrawProcessPendingEvents(NULL);
1274 GDrawRequestExpose(mkd->cvparent_w,NULL,false);
1275 }
1276
1277 static void MKDTopResize(MathKernDlg *mkd, GEvent *event) {
1278
1279 if ( !event->u.resize.sized )
1280 return;
1281
1282 GGadgetMove(GWidgetGetControl(mkd->gw,CID_TopBox),4,4);
1283 GGadgetResize(GWidgetGetControl(mkd->gw,CID_TopBox),
1284 event->u.resize.size.width-8,
1285 event->u.resize.size.height-12);
1286 }
1287
1288
1289 static void MKDDraw(MathKernDlg *mkd, GWindow pixmap, GEvent *event) {
1290 GRect r;
1291 int i;
1292
1293 GDrawSetLineWidth(pixmap,0);
1294 for ( i=0; i<4; ++i ) {
1295 CharView *cv = (&mkd->cv_topright)+i;
1296
1297 r.x = 10+i*(mkd->cv_width+mkd->mid_space)-1; r.y=mkd->cv_y-1;
1298 r.width = mkd->cv_width+1; r.height = mkd->cv_height+1;
1299 GDrawDrawRect(pixmap,&r,MAIN_FOREGROUND);
1300
1301 GDrawSetFont(pixmap,cv->inactive ? mkd->plain : mkd->bold);
1302 GDrawDrawText8(pixmap,r.x,5+mkd->as,cornernames[i],-1,MAIN_FOREGROUND);
1303 }
1304 }
1305
1306 void MKDMakeActive(MathKernDlg *mkd,CharView *cv) {
1307 GRect r;
1308 int i;
1309
1310 if ( mkd==NULL )
1311 return;
1312 for ( i=0; i<4; ++i )
1313 (&mkd->cv_topright)[i].inactive = true;
1314 cv->inactive = false;
1315 GDrawSetUserData(mkd->gw,cv);
1316 GDrawSetUserData(mkd->cvparent_w,cv);
1317 for ( i=0; i<4; ++i )
1318 GDrawRequestExpose((&mkd->cv_topright)[i].v,NULL,false);
1319 GDrawGetSize(mkd->gw,&r);
1320 r.x = 0;
1321 r.y = 0;
1322 r.height = mkd->fh+10;
1323 GDrawRequestExpose(mkd->cvparent_w,&r,false);
1324 }
1325
1326 static void MKDChar(MathKernDlg *mkd, GEvent *event) {
1327 int i;
1328 for ( i=0; i<4; ++i )
1329 if ( !(&mkd->cv_topright)[i].inactive )
1330 break;
1331
1332 if ( event->u.chr.keysym==GK_Tab || event->u.chr.keysym==GK_BackTab ) {
1333 if ( event->u.chr.keysym==GK_Tab ) ++i; else --i;
1334 if ( i<0 ) i=3; else if ( i>3 ) i = 0;
1335 MKDMakeActive(mkd,(&mkd->cv_topright)+i);
1336 } else
1337 CVChar((&mkd->cv_topright)+i,event);
1338 }
1339
1340 void MKD_DoClose(struct cvcontainer *cvc) {
1341 MathKernDlg *mkd = (MathKernDlg *) cvc;
1342 int i;
1343
1344 for ( i=0; i<4; ++i ) {
1345 SplineChar *msc = &(&mkd->sc_topright)[i];
1346 SplinePointListsFree(msc->layers[0].splines);
1347 SplinePointListsFree(msc->layers[1].splines);
1348 free( msc->layers );
1349 }
1350
1351 mkd->done = true;
1352 }
1353
1354 static int mkd_sub_e_h(GWindow gw, GEvent *event) {
1355 MathKernDlg *mkd = (MathKernDlg *) ((CharViewBase *) GDrawGetUserData(gw))->container;
1356
1357 switch ( event->type ) {
1358 case et_expose:
1359 MKDDraw(mkd,gw,event);
1360 break;
1361 case et_resize:
1362 if ( event->u.resize.sized )
1363 MKDSubResize(mkd,event);
1364 break;
1365 case et_char:
1366 MKDChar(mkd,event);
1367 break;
1368 default: break;
1369 }
1370 return( true );
1371 }
1372
1373 static int mkd_e_h(GWindow gw, GEvent *event) {
1374 MathKernDlg *mkd = (MathKernDlg *) ((CharViewBase *) GDrawGetUserData(gw))->container;
1375 int i;
1376
1377 switch ( event->type ) {
1378 case et_char:
1379 MKDChar(mkd,event);
1380 break;
1381 case et_resize:
1382 if ( event->u.resize.sized )
1383 MKDTopResize(mkd,event);
1384 break;
1385 case et_close:
1386 MKD_DoClose((struct cvcontainer *) mkd);
1387 break;
1388 case et_create:
1389 break;
1390 case et_map:
1391 for ( i=0; i<4; ++i ) {
1392 CharView *cv = (&mkd->cv_topright)+i;
1393 if ( !cv->inactive ) {
1394 if ( event->u.map.is_visible )
1395 CVPaletteActivate(cv);
1396 else
1397 CVPalettesHideIfMine(cv);
1398 break;
1399 }
1400 }
1401 /* mkd->isvisible = event->u.map.is_visible; */
1402 break;
1403 default: break;
1404 }
1405 return( true );
1406 }
1407
1408 static void MKDFillup(MathKernDlg *mkd, SplineChar *sc) {
1409 int i, j, rows;
1410 SplineSet *last, *cur;
1411 RefChar *ref;
1412 GTextInfo **list;
1413
1414 if ( mkd->last_aspect==0 ) {
1415 for ( i=0; i<4; ++i ) {
1416 SplineChar *msc = &(&mkd->sc_topright)[i];
1417 struct mathkernvertex *mkv = sc->mathkern==NULL ? NULL : &(&sc->mathkern->top_right)[i];
1418 msc->width = sc->width;
1419 msc->italic_correction = sc->italic_correction;
1420 msc->top_accent_horiz = sc->top_accent_horiz;
1421 last = NULL;
1422 SplinePointListsFree(msc->layers[0].splines);
1423 SplinePointListsFree(msc->layers[1].splines);
1424 msc->layers[0].splines = msc->layers[1].splines = NULL;
1425
1426 /* copy the character itself into the background */
1427 last = msc->layers[0].splines = SplinePointListCopy(sc->layers[ly_fore].splines);
1428 if ( last!=NULL )
1429 while ( last->next!=NULL ) last = last->next;
1430 for ( ref=sc->layers[ly_fore].refs; ref!=NULL; ref=ref->next ) {
1431 if ( last==NULL )
1432 cur = SplinePointListCopy(ref->layers[0].splines);
1433 if ( last==NULL )
1434 msc->layers[0].splines = cur;
1435 else
1436 last->next = cur;
1437 if ( cur!=NULL )
1438 for ( last=cur; last->next==NULL; last = last->next );
1439 }
1440 /* Now copy the dots from the mathkern vertex structure */
1441 last = NULL;
1442 if ( mkv!=NULL ) {
1443 for ( j=0; j<mkv->cnt; ++j ) {
1444 cur = chunkalloc(sizeof(SplineSet));
1445 cur->first = cur->last = SplinePointCreate(mkv->mkd[j].kern +
1446 ((i&1)?0:sc->width) +
1447 ((i&2)?0:sc->italic_correction==TEX_UNDEF?0:sc->italic_correction),
1448 mkv->mkd[j].height );
1449 cur->first->pointtype = pt_corner;
1450 if ( last==NULL )
1451 msc->layers[ly_fore].splines = cur;
1452 else
1453 last->next = cur;
1454 last = cur;
1455 }
1456 }
1457 }
1458 } else {
1459 for ( i=0; i<4; ++i ) {
1460 struct mathkernvertex *mkv = sc->mathkern==NULL ? NULL : &(&sc->mathkern->top_right)[i];
1461 GGadget *list = GWidgetGetControl(mkd->gw,CID_TopRight+i);
1462 int cols = GMatrixEditGetColCnt(list);
1463 struct matrix_data *md;
1464
1465 if ( mkv!=NULL ) {
1466 md = calloc(mkv->cnt*cols,sizeof(struct matrix_data));
1467 for ( j=0; j<mkv->cnt; ++j ) {
1468 md[j*cols+0].u.md_ival = mkv->mkd[j].height;
1469 md[j*cols+1].u.md_ival = mkv->mkd[j].kern;
1470 DevTabToString(&md[j*cols+2].u.md_str,mkv->mkd[j].height_adjusts);
1471 DevTabToString(&md[j*cols+3].u.md_str,mkv->mkd[j].kern_adjusts);
1472 }
1473 GMatrixEditSet(list, md,mkv->cnt,false);
1474 } else
1475 GMatrixEditSet(list, NULL,0,false);
1476 }
1477 }
1478 mkd->cursc = sc;
1479
1480 list = GGadgetGetList(GWidgetGetControl(mkd->gw,CID_Glyph),&rows);
1481 for ( i=rows-1; i>=0; --i )
1482 if ( list[i]->userdata==sc )
1483 break;
1484 if ( i>=0 )
1485 GGadgetSelectOneListItem(GWidgetGetControl(mkd->gw,CID_Glyph),i);
1486 }
1487
1488 static void MKDFillupRefresh(MathKernDlg *mkd, SplineChar *sc) {
1489 int i;
1490
1491 MKDFillup(mkd, sc);
1492 if ( mkd->last_aspect==0 ) {
1493 for ( i=0; i<4; ++i ) {
1494 CharView *cv = &mkd->cv_topright + i;
1495 GDrawRequestExpose(cv->gw,NULL,false);
1496 GDrawRequestExpose(cv->v,NULL,false);
1497 }
1498 }
1499 }
1500
1501 static int bp_order_height(const void *bpp1, const void *bpp2) {
1502 const BasePoint *bp1 = *(const BasePoint **) bpp1;
1503 const BasePoint *bp2 = *(const BasePoint **) bpp2;
1504 if ( bp1->y > bp2->y )
1505 return( 1 );
1506 else if ( bp1->y < bp2->y )
1507 return( -1 );
1508
1509 return( 0 );
1510 }
1511
1512 static int mkd_order_height(const void *_mkd1, const void *_mkd2) {
1513 const struct mathkerndata *mkd1 = (const struct mathkerndata *) _mkd1;
1514 const struct mathkerndata *mkd2 = (const struct mathkerndata *) _mkd2;
1515 if ( mkd1->height > mkd2->height )
1516 return( 1 );
1517 else if ( mkd1->height < mkd2->height )
1518 return( -1 );
1519
1520 return( 0 );
1521 }
1522
1523 static int MKD_Parse(MathKernDlg *mkd) {
1524 int i, cnt, j, k;
1525 SplineSet *ss;
1526 SplinePoint *sp;
1527 BasePoint **bases;
1528 int allzeroes = true;
1529
1530 if ( mkd->cursc->mathkern==NULL )
1531 mkd->cursc->mathkern = chunkalloc(sizeof(struct mathkern));
1532
1533 if ( mkd->last_aspect==0 ) { /* Graphical view is current */
1534 for ( i=0; i<4; ++i ) {
1535 SplineChar *msc = &(&mkd->sc_topright)[i];
1536 struct mathkernvertex *mkv = &(&mkd->cursc->mathkern->top_right)[i];
1537
1538 for ( k=0; k<2; ++k ) {
1539 cnt = 0;
1540 for ( ss = msc->layers[ly_fore].splines; ss!=NULL; ss=ss->next ) {
1541 for ( sp=ss->first ; ; ) {
1542 if ( k )
1543 bases[cnt] = &sp->me;
1544 ++cnt;
1545 if ( sp->next == NULL )
1546 break;
1547 sp = sp->next->to;
1548 if ( sp == ss->first )
1549 break;
1550 }
1551 }
1552 if ( !k )
1553 bases = malloc(cnt*sizeof(BasePoint *));
1554 }
1555 qsort(bases,cnt,sizeof(BasePoint *),bp_order_height);
1556 if ( cnt>mkv->cnt ) {
1557 mkv->mkd = realloc(mkv->mkd,cnt*sizeof(struct mathkerndata));
1558 memset(mkv->mkd+mkv->cnt,0,(cnt-mkv->cnt)*sizeof(struct mathkerndata));
1559 }
1560 for ( j=0; j<cnt; ++j ) {
1561 bases[j]->x = rint(bases[j]->x);
1562 if ( !(i&1) ) bases[j]->x -= mkd->cursc->width;
1563 if ( !(i&2) ) bases[j]->x -= mkd->cursc->italic_correction==TEX_UNDEF?0:mkd->cursc->italic_correction;
1564 bases[j]->y = rint(bases[j]->y);
1565 /* If we have a previous entry with this height retain the height dv */
1566 /* If we have a previous entry with this height and width retain the width dv too */
1567 for ( k=j; k<mkv->cnt; ++k )
1568 if ( bases[j]->y == mkv->mkd[k].height )
1569 break;
1570 if ( k!=j ) {
1571 DeviceTableFree(mkv->mkd[j].height_adjusts);
1572 DeviceTableFree(mkv->mkd[j].kern_adjusts);
1573 mkv->mkd[j].height_adjusts = mkv->mkd[j].kern_adjusts = NULL;
1574 }
1575 if ( k<mkv->cnt ) {
1576 mkv->mkd[j].height_adjusts = mkv->mkd[k].height_adjusts;
1577 if ( bases[j]->x == mkv->mkd[k].kern )
1578 mkv->mkd[j].kern_adjusts = mkv->mkd[k].kern_adjusts;
1579 else {
1580 DeviceTableFree(mkv->mkd[k].kern_adjusts);
1581 mkv->mkd[k].kern_adjusts = NULL;
1582 }
1583 if ( j!=k )
1584 mkv->mkd[k].height_adjusts = mkv->mkd[k].kern_adjusts = NULL;
1585 }
1586 mkv->mkd[j].height = bases[j]->y;
1587 mkv->mkd[j].kern = bases[j]->x;
1588 }
1589 for ( ; j<mkv->cnt; ++j ) {
1590 DeviceTableFree(mkv->mkd[j].height_adjusts);
1591 DeviceTableFree(mkv->mkd[j].kern_adjusts);
1592 mkv->mkd[j].height_adjusts = mkv->mkd[j].kern_adjusts = NULL;
1593 }
1594 mkv->cnt = cnt;
1595 free(bases);
1596 if ( cnt!=0 )
1597 allzeroes = false;
1598 }
1599 } else {
1600 int low, high;
1601 /* Parse the textual info */
1602 for ( i=0; i<4; ++i ) {
1603 GGadget *list = GWidgetGetControl(mkd->gw,CID_TopRight+i);
1604 int rows, cols = GMatrixEditGetColCnt(list);
1605 struct matrix_data *old = GMatrixEditGet(list,&rows);
1606
1607 for ( j=0; j<rows; ++j ) {
1608 if ( !DeviceTableOK(old[j*cols+2].u.md_str,&low,&high) ||
1609 !DeviceTableOK(old[j*cols+3].u.md_str,&low,&high)) {
1610 ff_post_error(_("Bad device table"), _("Bad device table for in row %d of %s"),
1611 j, cornernames[i]);
1612 return( false );
1613 }
1614 }
1615 }
1616 for ( i=0; i<4; ++i ) {
1617 struct mathkernvertex *mkv = &(&mkd->cursc->mathkern->top_right)[i];
1618 GGadget *list = GWidgetGetControl(mkd->gw,CID_TopRight+i);
1619 int rows, cols = GMatrixEditGetColCnt(list);
1620 struct matrix_data *old = GMatrixEditGet(list,&rows);
1621
1622 for ( j=0; j<mkv->cnt; ++j ) {
1623 DeviceTableFree(mkv->mkd[j].height_adjusts);
1624 DeviceTableFree(mkv->mkd[j].kern_adjusts);
1625 mkv->mkd[j].height_adjusts = mkv->mkd[j].kern_adjusts = NULL;
1626 }
1627 if ( rows>mkv->cnt ) {
1628 mkv->mkd = realloc(mkv->mkd,rows*sizeof(struct mathkerndata));
1629 memset(mkv->mkd+mkv->cnt,0,(rows-mkv->cnt)*sizeof(struct mathkerndata));
1630 }
1631 for ( j=0; j<rows; ++j ) {
1632 mkv->mkd[j].height = old[j*cols+0].u.md_ival;
1633 mkv->mkd[j].kern = old[j*cols+1].u.md_ival;
1634 mkv->mkd[j].height_adjusts = DeviceTableParse(NULL,old[j*cols+2].u.md_str);
1635 mkv->mkd[j].kern_adjusts = DeviceTableParse(NULL,old[j*cols+3].u.md_str);
1636 }
1637 qsort(mkv->mkd,rows,sizeof(struct mathkerndata),mkd_order_height);
1638 mkv->cnt = rows;
1639 if ( rows!=0 )
1640 allzeroes=false;
1641 }
1642 }
1643 if ( allzeroes ) {
1644 MathKernFree(mkd->cursc->mathkern);
1645 mkd->cursc->mathkern = NULL;
1646 }
1647 /* The only potential error is two entries with the same height, and I don't */
1648 /* check for that */
1649 return( true );
1650 }
1651
1652 static int MKD_AspectChange(GGadget *g, GEvent *e) {
1653 if ( e==NULL || (e->type==et_controlevent && e->u.control.subtype == et_radiochanged )) {
1654 MathKernDlg *mkd = (MathKernDlg *) (((CharViewBase *) GDrawGetUserData(GGadgetGetWindow(g)))->container);
1655 int new_aspect = GTabSetGetSel(g);
1656
1657 if ( new_aspect == mkd->last_aspect )
1658 return( true );
1659
1660 GGadgetSetEnabled(mkd->mb,new_aspect==0);
1661
1662 if ( new_aspect==0 ) {
1663 /* We are moving from textual to graphical. Parse text, clear old */
1664 /* points, set new points */
1665 } else {
1666 /* We are moving from graphical to textual. */
1667 if ( !mkd->saved_mathkern ) {
1668 mkd->orig_mathkern = MathKernCopy(mkd->cursc->mathkern);
1669 mkd->saved_mathkern = true;
1670 }
1671 }
1672 MKD_Parse(mkd);
1673 mkd->last_aspect = new_aspect;
1674 MKDFillup(mkd,mkd->cursc);
1675 }
1676 return( true );
1677 }
1678
1679 static int MathKernD_GlyphChanged(GGadget *g, GEvent *e) {
1680 MathKernDlg *mkd = (MathKernDlg *) (((CharViewBase *) GDrawGetUserData(GGadgetGetWindow(g)))->container);
1681 if ( e->type==et_controlevent && e->u.control.subtype == et_listselected ) {
1682 GTextInfo *sel = GGadgetGetListItemSelected(g);
1683
1684 if ( sel!=NULL && MKD_Parse(mkd)) {
1685 SplineChar *sc = sel->userdata;
1686 MKDFillupRefresh(mkd, sc);
1687 }
1688 }
1689 return( true );
1690 }
1691
1692 static int MathKernD_Cancel(GGadget *g, GEvent *e) {
1693 if ( e->type==et_controlevent && e->u.control.subtype == et_buttonactivate ) {
1694 MathKernDlg *mkd = (MathKernDlg *) (((CharViewBase *) GDrawGetUserData(GGadgetGetWindow(g)))->container);
1695 if ( mkd->saved_mathkern ) {
1696 MathKernFree(mkd->cursc->mathkern);
1697 mkd->cursc->mathkern = mkd->orig_mathkern;
1698 }
1699 MKD_DoClose(((CharViewBase *) GDrawGetUserData(GGadgetGetWindow(g)))->container);
1700 }
1701 return( true );
1702 }
1703
1704 static int MathKernD_OK(GGadget *g, GEvent *e) {
1705 if ( e->type==et_controlevent && e->u.control.subtype == et_buttonactivate ) {
1706 MathKernDlg *mkd = (MathKernDlg *) (((CharViewBase *) GDrawGetUserData(GGadgetGetWindow(g)))->container);
1707 if ( MKD_Parse(mkd) ) {
1708 MathKernFree(mkd->orig_mathkern);
1709 mkd->orig_mathkern = NULL;
1710 mkd->saved_mathkern = false;
1711 MKD_DoClose( (struct cvcontainer *) mkd );
1712 }
1713 }
1714 return( true );
1715 }
1716
1717 static int MKD_Can_Navigate(struct cvcontainer *cvc, enum nav_type type) {
1718 return( true );
1719 }
1720
1721 static int MKD_Can_Open(struct cvcontainer *cvc) {
1722 return( false );
1723 }
1724
1725 static void MKD_Do_Navigate(struct cvcontainer *cvc, enum nav_type type) {
1726 MathKernDlg *mkd = ( MathKernDlg * ) cvc;
1727 SplineChar *sc = NULL;
1728 int pos;
1729 GGadget *list = GWidgetGetControl(mkd->gw,CID_Glyph);
1730 int32_t rows;
1731 GTextInfo **tis;
1732
1733 if ( !MKD_Parse(mkd))
1734 return;
1735 MathKernFree(mkd->orig_mathkern);
1736 mkd->orig_mathkern = NULL;
1737 mkd->saved_mathkern = false;
1738
1739 if ( type == nt_goto ) {
1740 SplineFont *sf = mkd->cursc->parent;
1741 int enc = GotoChar(sf,sf->fv->map,NULL);
1742 if ( enc==-1 || sf->fv->map->map[enc]==-1 || (sc = sf->glyphs[ sf->fv->map->map[enc] ])==NULL )
1743 return;
1744 if ( sc->mathkern==NULL )
1745 MKD_SetGlyphList(mkd,sc);
1746 } else if ( type == nt_next || type == nt_nextdef ) {
1747 tis = GGadgetGetList(list,&rows);
1748 for ( pos=rows-1; pos>=0; --pos )
1749 if ( tis[pos]->selected )
1750 break;
1751 ++pos;
1752 if ( pos==rows )
1753 return;
1754 sc = tis[pos]->userdata;
1755 } else {
1756 tis = GGadgetGetList(list,&rows);
1757 for ( pos=rows-1; pos>=0; --pos )
1758 if ( tis[pos]->selected )
1759 break;
1760 if ( pos<=0 )
1761 return;
1762 --pos;
1763 sc = tis[pos]->userdata;
1764 }
1765 MKDFillupRefresh(mkd,sc);
1766 }
1767
1768 static SplineFont *SF_Of_MKD(struct cvcontainer *foo) {
1769 return( NULL );
1770 }
1771
1772 struct cvcontainer_funcs mathkern_funcs = {
1773 cvc_mathkern,
1774 (void (*) (struct cvcontainer *cvc,CharViewBase *cv)) MKDMakeActive,
1775 (void (*) (struct cvcontainer *cvc,void *)) MKDChar,
1776 MKD_Can_Navigate,
1777 MKD_Do_Navigate,
1778 MKD_Can_Open,
1779 MKD_DoClose,
1780 SF_Of_MKD
1781 };
1782
1783 static void MKDInit(MathKernDlg *mkd,SplineChar *sc) {
1784 int i;
1785
1786 memset(mkd,0,sizeof(*mkd));
1787 mkd->base.funcs = &mathkern_funcs;
1788
1789 for ( i=0; i<4; ++i ) {
1790 SplineChar *msc = &(&mkd->sc_topright)[i];
1791 CharView *mcv = &(&mkd->cv_topright)[i];
1792 msc->orig_pos = i;
1793 msc->unicodeenc = -1;
1794 msc->name = i==0 ? _("TopRight") :
1795 i==1 ? _("TopLeft") :
1796 i==2 ? _("BottomRight"):
1797 _("BottomLeft");
1798 msc->parent = &mkd->dummy_sf;
1799 msc->layer_cnt = 2;
1800 msc->layers = calloc(2,sizeof(Layer));
1801 LayerDefault(&msc->layers[0]);
1802 LayerDefault(&msc->layers[1]);
1803 mkd->chars[i] = msc;
1804
1805 mcv->b.sc = msc;
1806 mcv->b.layerheads[dm_fore] = &msc->layers[ly_fore];
1807 mcv->b.layerheads[dm_back] = &msc->layers[ly_back];
1808 mcv->b.layerheads[dm_grid] = &mkd->dummy_sf.grid;
1809 mcv->b.drawmode = dm_fore;
1810 mcv->b.container = (struct cvcontainer *) mkd;
1811 mcv->inactive = i!=0;
1812 }
1813 mkd->dummy_sf.glyphs = mkd->chars;
1814 mkd->dummy_sf.glyphcnt = mkd->dummy_sf.glyphmax = 4;
1815 mkd->dummy_sf.pfminfo.fstype = -1;
1816 mkd->dummy_sf.pfminfo.stylemap = -1;
1817 mkd->dummy_sf.fontname = mkd->dummy_sf.fullname = mkd->dummy_sf.familyname = "dummy";
1818 mkd->dummy_sf.weight = "Medium";
1819 mkd->dummy_sf.origname = "dummy";
1820 mkd->dummy_sf.ascent = sc->parent->ascent;
1821 mkd->dummy_sf.descent = sc->parent->descent;
1822 mkd->dummy_sf.layers = mkd->layerinfo;
1823 mkd->dummy_sf.layer_cnt = 2;
1824 mkd->layerinfo[ly_back].order2 = sc->layers[ly_back].order2;
1825 mkd->layerinfo[ly_back].name = _("Back");
1826 mkd->layerinfo[ly_fore].order2 = sc->layers[ly_fore].order2;
1827 mkd->layerinfo[ly_fore].name = _("Fore");
1828 mkd->dummy_sf.grid.order2 = sc->layers[ly_back].order2;
1829 mkd->dummy_sf.anchor = NULL;
1830
1831 mkd->dummy_sf.fv = (FontViewBase *) &mkd->dummy_fv;
1832 mkd->dummy_fv.b.active_layer = ly_fore;
1833 mkd->dummy_fv.b.sf = &mkd->dummy_sf;
1834 mkd->dummy_fv.b.selected = mkd->sel;
1835 mkd->dummy_fv.cbw = mkd->dummy_fv.cbh = default_fv_font_size+1;
1836 mkd->dummy_fv.magnify = 1;
1837
1838 mkd->dummy_fv.b.map = &mkd->dummy_map;
1839 mkd->dummy_map.map = mkd->map;
1840 mkd->dummy_map.backmap = mkd->backmap;
1841 mkd->dummy_map.enccount = mkd->dummy_map.encmax = mkd->dummy_map.backmax = 4;
1842 mkd->dummy_map.enc = &custom;
1843 }
1844
1845 void MathKernDialog(SplineChar *sc,int def_layer) {
1846 MathKernDlg mkd;
1847 GRect pos;
1848 GWindow gw;
1849 GWindowAttrs wattrs;
1850 GGadgetCreateData gcd[6], boxes[4], *harray[8], *varray[5], *garray[5];
1851 GGadgetCreateData cgcd[4][2], tabsetgcd[2];
1852 GTextInfo label[6];
1853 GTabInfo aspects[3], corners[5];
1854 int as, ds, ld;
1855 int i,k;
1856
1857 MathInit();
1858 MKDInit( &mkd, sc );
1859 mkd.def_layer = def_layer;
1860
1861 memset(&wattrs,0,sizeof(wattrs));
1862 wattrs.mask = wam_events|wam_cursor|wam_isdlg|wam_restrict|wam_undercursor|wam_utf8_wtitle;
1863 wattrs.is_dlg = true;
1864 wattrs.restrict_input_to_me = 1;
1865 wattrs.undercursor = 1;
1866 wattrs.event_masks = -1;
1867 wattrs.cursor = ct_pointer;
1868 wattrs.utf8_window_title = _("Math Kerning");
1869 pos.width = 600;
1870 pos.height = 400;
1871 mkd.gw = gw = GDrawCreateTopWindow(NULL,&pos,mkd_e_h,&mkd.cv_topright,&wattrs);
1872
1873 mkd.plain = math_font.fi;
1874 mkd.bold = math_boldfont.fi;
1875 GDrawWindowFontMetrics(mkd.gw,mkd.plain,&as,&ds,&ld);
1876 mkd.fh = as+ds; mkd.as = as;
1877
1878 memset(&label,0,sizeof(label));
1879 memset(&gcd,0,sizeof(gcd));
1880 memset(&boxes,0,sizeof(boxes));
1881 memset(&aspects,'\0',sizeof(aspects));
1882 memset(&corners,'\0',sizeof(corners));
1883 memset(&cgcd,0,sizeof(cgcd));
1884 memset(&tabsetgcd,0,sizeof(tabsetgcd));
1885
1886 for ( k=0; k<4; ++k ) {
1887 cgcd[k][0].gd.flags = gg_visible | gg_enabled;
1888 cgcd[k][0].gd.u.matrix = &mi_mathkern;
1889 cgcd[k][0].gd.cid = CID_TopRight+k;
1890 cgcd[k][0].creator = GMatrixEditCreate;
1891
1892 corners[k].text = (unichar_t *) cornernames[k];
1893 corners[k].text_is_1byte = true;
1894 corners[k].gcd = cgcd[k];
1895 }
1896
1897 tabsetgcd[0].gd.flags = gg_visible|gg_enabled|gg_tabset_vert ;
1898 tabsetgcd[0].gd.u.tabs = corners;
1899 tabsetgcd[0].gd.cid = CID_Corners;
1900 /*tabsetgcd[0].gd.handle_controlevent = MKD_AspectChange;*/
1901 tabsetgcd[0].creator = GTabSetCreate;
1902
1903 k = 0;
1904 gcd[k].gd.flags = gg_visible|gg_enabled ;
1905 gcd[k].gd.pos.height = 18; gcd[k].gd.pos.width = 20;
1906 gcd[k++].creator = GSpacerCreate;
1907
1908 aspects[0].text = (unichar_t *) _("Graphical");
1909 aspects[0].text_is_1byte = true;
1910 aspects[0].gcd = NULL;
1911
1912 aspects[1].text = (unichar_t *) _("Textual");
1913 aspects[1].text_is_1byte = true;
1914 aspects[1].gcd = tabsetgcd;
1915
1916 gcd[k].gd.flags = gg_visible|gg_enabled ;
1917 gcd[k].gd.u.tabs = aspects;
1918 gcd[k].gd.cid = CID_Tabs;
1919 gcd[k].gd.handle_controlevent = MKD_AspectChange;
1920 gcd[k++].creator = GTabSetCreate;
1921
1922 gcd[k].gd.flags = gg_visible|gg_enabled ;
1923 gcd[k].gd.cid = CID_Glyph;
1924 gcd[k].gd.handle_controlevent = MathKernD_GlyphChanged;
1925 gcd[k++].creator = GListButtonCreate;
1926
1927 label[k].text = (unichar_t *) _("_OK");
1928 label[k].text_is_1byte = true;
1929 label[k].text_in_resource = true;
1930 gcd[k].gd.label = &label[k];
1931 gcd[k].gd.flags = gg_enabled|gg_visible|gg_but_default;
1932 gcd[k].gd.handle_controlevent = MathKernD_OK;
1933 gcd[k++].creator = GButtonCreate;
1934
1935 label[k].text = (unichar_t *) _("_Done");
1936 label[k].text_is_1byte = true;
1937 label[k].text_in_resource = true;
1938 gcd[k].gd.label = &label[k];
1939 gcd[k].gd.flags = gg_enabled|gg_visible|gg_but_cancel;
1940 gcd[k].gd.handle_controlevent = MathKernD_Cancel;
1941 gcd[k++].creator = GButtonCreate;
1942
1943 harray[0] = GCD_Glue; harray[1] = &gcd[k-2]; harray[2] = GCD_Glue;
1944 harray[3] = GCD_Glue; harray[4] = &gcd[k-1]; harray[5] = GCD_Glue;
1945 harray[6] = NULL;
1946
1947 boxes[2].gd.flags = gg_enabled|gg_visible;
1948 boxes[2].gd.u.boxelements = harray;
1949 boxes[2].creator = GHBoxCreate;
1950
1951 garray[0] = &gcd[k-3]; garray[1] = GCD_Glue; garray[2] = NULL;
1952 boxes[3].gd.flags = gg_enabled|gg_visible;
1953 boxes[3].gd.u.boxelements = garray;
1954 boxes[3].creator = GHBoxCreate;
1955
1956 varray[0] = &gcd[0];
1957 varray[1] = &gcd[1];
1958 varray[2] = &boxes[3];
1959 varray[3] = &boxes[2];
1960 varray[4] = NULL;
1961
1962 boxes[0].gd.flags = gg_enabled|gg_visible;
1963 boxes[0].gd.u.boxelements = varray;
1964 boxes[0].gd.cid = CID_TopBox;
1965 boxes[0].creator = GVBoxCreate;
1966
1967 GGadgetsCreate(gw,boxes);
1968
1969 mkd.cvparent_w = GTabSetGetSubwindow(gcd[1].ret,0);
1970 GDrawSetEH(mkd.cvparent_w,mkd_sub_e_h);
1971 MKDCharViewInits(&mkd);
1972
1973 MKD_SetGlyphList(&mkd, sc);
1974 MKDFillup( &mkd, sc );
1975
1976 GHVBoxSetExpandableRow(boxes[0].ret,1);
1977 GHVBoxSetExpandableCol(boxes[2].ret,gb_expandgluesame);
1978 GHVBoxSetExpandableCol(boxes[3].ret,gb_expandglue);
1979 GGadgetResize(boxes[0].ret,pos.width,pos.height);
1980
1981 mkd.button_height = GDrawPointsToPixels(gw,60);
1982 GDrawResize(gw,1000,400); /* Force a resize event */
1983
1984 GDrawSetVisible(mkd.gw,true);
1985
1986 while ( !mkd.done )
1987 GDrawProcessOneEvent(NULL);
1988
1989 for ( i=0; i<4; ++i ) {
1990 CharView *cv = &mkd.cv_topright + i;
1991 CVPalettesHideIfMine(cv);
1992 }
1993 GDrawDestroyWindow(mkd.gw);
1994 }