/
contextchain.c
3213 lines (2949 loc) · 112 KB
/
contextchain.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright (C) 2003-2012 by George Williams */
/*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
* EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "fontforgeui.h"
#include <chardata.h>
#include <utype.h>
#include <ustring.h>
#include <gkeysym.h>
/* Currently uses class numbers rather than names!!!!!! */
/* ************************************************************************** */
/* ************************ Context/Chaining Dialog ************************* */
/* ************************************************************************** */
struct contextchaindlg {
struct gfi_data *gfi;
SplineFont *sf;
FPST *fpst;
unichar_t *newname;
int isnew;
GWindow gw;
GWindow formats, coverage, grules, glyphs, classrules, classnumber;
GWindow coverage_simple, glyphs_simple, classes_simple;
enum activewindow { aw_formats, aw_coverage, aw_grules, aw_glyphs,
aw_classrules, aw_classnumber,
aw_coverage_simple, aw_glyphs_simple, aw_classes_simple
} aw;
/* Wizard panes:
formats -- gives user a choice between by glyph/class/coverage table
Simple version:
glyphs_simple -- user picked glyph format, shows a matrix edit of glyph lists
and lookups.
classes_simple -- user picked class format. shows a matrix edit of class lists
with lookups. Also the three sets of classes_simple.
coverage_simple -- user picked coverage table format. shows a matrix edit with
a bunch of coverage tables and lookups/replacements.
[coverage_simple format only accepts 1 rule]
Complex version:
grules -- user picked glyph format, shows a list of glyph lists and with
lookups. Editing a glyph list takes us to the glyphs pane
classrules -- user picked class format. shows a list of class lists with
lookups. Also a tabbed widget showing the three sets of
classes.
coverage -- user picked coverage table format. shows a tabbed widget with
three lists of coverage tables. (for match also shows a
set of sequence #/lookup pairs.
[There is no coveragerules pane because coverage format
only accepts 1 rule]
glyphs -- from grules, user asked for a new rule. Shows tabbed widget
with three lists of glyphs (for match, also shows a set of
sequence #/lookups
classnumber - from class rules, tabbed pane describing one class based rule
*/
int row_being_edited;
int subheightdiff, canceldrop;
int done;
int layer;
};
#define CID_OK 100
#define CID_Cancel 101
#define CID_Next 102
#define CID_Prev 103
#define CID_SubSize 104
#define CID_ByGlyph 200
#define CID_ByClass 201
#define CID_ByCoverage 202
#define CID_Simple 205
#define CID_Complex 206
/* There are more CIDs used in this file than those listed here */
/* The CIDs given here are for glyph lists (aw_glyphs & aw_grules) */
/* similar controls refering to coverage tables have 100 added to them */
/* The CIDs here are also for the "match" aspect of the tabsets */
/* those in backtrack get 20 added, those in lookahead get 40 */
#define CID_GList 305
#define CID_MatchType 1003
#define CID_Set 1004
#define CID_Select 1005
#define CID_GlyphList 1006
#define CID_Set2 1007
#define CID_Select2 1008
#define CID_RplList 1009
#define CID_LookupList 1010
#define CID_LNew 1011
#define CID_LEdit 1012
#define CID_LDelete 1013
#define CID_LUp 1014
#define CID_LDown 1015
#define CID_ClassNumbers 2000
#define CID_ClassList 2001
#define CID_ClassType 2002
#define CID_SameAsClasses 2003
/* And for the simple dlg... */
#define CID_GList_Simple 4000
#define CID_GNewSection 4001
#define CID_GAddLookup 4002
#define CID_CList_Simple (CID_GList_Simple+100)
#define CID_CNewSection (CID_GNewSection+100)
#define CID_CAddLookup (CID_GAddLookup+100)
#define CID_MatchClasses (CID_GList_Simple+300+0*20)
#define CID_BackClasses (CID_MatchClasses+1*20)
#define CID_ForeClasses (CID_MatchClasses+2*20)
#define CID_SameAsClasses_S (CID_MatchClasses+1) /* Not actually used itself */
#define CID_BackClassesSameAs_S (CID_SameAsClasses_S+1*20)
#define CID_ForeClassesSameAs_S (CID_SameAsClasses_S+2*20)
#define CID_ClassMatchType 4500
#define CID_Covers 4600
char *cu_copybetween(const unichar_t *start, const unichar_t *end) {
char *ret = galloc(end-start+1);
cu_strncpy(ret,start,end-start);
ret[end-start] = '\0';
return( ret );
}
static char *reversenames(char *str) {
char *ret;
char *rpt, *pt, *start, *spt;
if ( str==NULL )
return( NULL );
rpt = ret = galloc(strlen(str)+1);
*ret = '\0';
for ( pt=str+strlen(str); pt>str; pt=start ) {
for ( start = pt-1; start>=str && *start!=' '; --start );
for ( spt=start+1; spt<pt; )
*rpt++ = *spt++;
*rpt++ = ' ';
}
if ( rpt>ret )
rpt[-1] = '\0';
return( ret );
}
static char *rpl(const char *src, const char *find, const char *rpl) {
const char *pt, *start;
char *ret, *rpt;
int found_cnt=0;
int flen = strlen(find);
for ( pt=src; *pt; ) {
while ( isspace(*pt)) ++pt;
if ( *pt=='\0' )
break;
for ( start=pt; !isspace(*pt) && *pt!='\0'; ++pt );
if ( pt-start==flen && strncmp(find,start,flen)==0 )
++found_cnt;
}
if ( found_cnt==0 )
return( copy(src));
rpt = ret = galloc(strlen(src)+found_cnt*(strlen(rpl)-flen)+1);
for ( pt=src; *pt; ) {
while ( isspace(*pt))
*rpt++ = *pt++;
if ( *pt=='\0' )
break;
for ( start=pt; !isspace(*pt) && *pt!='\0'; ++pt );
if ( pt-start==flen && strncmp(find,start,flen)==0 ) {
strcpy(rpt,rpl);
rpt += strlen(rpt);
} else {
strncpy(rpt,start,pt-start);
rpt += (pt-start);
}
}
*rpt = '\0';
return( ret );
}
static char *classnumbers(int cnt,uint16 *classes, struct matrix_data *classnames, int rows, int cols) {
char buf[20];
int i, len;
char *pt, *ret;
len = 0;
for ( i=0; i<cnt; ++i ) {
if ( classnames[cols*classes[i]+0].u.md_str==NULL ) {
sprintf( buf, "%d ", classes[i]);
len += strlen(buf);
} else {
len += strlen(classnames[cols*classes[i]+0].u.md_str)+1;
}
}
ret = pt = galloc(len+3);
*pt = '\0'; /* In case it is empty */
for ( i=0; i<cnt; ++i ) {
if ( classnames[cols*classes[i]+0].u.md_str==NULL ) {
sprintf( pt, "%d ", classes[i]);
pt += strlen(pt);
} else {
strcpy(pt, classnames[cols*classes[i]+0].u.md_str);
pt += strlen(pt);
*pt++ = ' ';
}
}
if ( pt>ret && pt[-1]==' ' )
pt[-1] = '\0';
return( ret );
}
static char *rclassnumbers(int cnt,uint16 *classes, struct matrix_data *classnames, int rows, int cols) {
char buf[20];
int i, len;
char *pt, *ret;
len = 0;
for ( i=0; i<cnt; ++i ) {
if ( classnames[cols*classes[i]+0].u.md_str==NULL ) {
sprintf( buf, "%d ", classes[i]);
len += strlen(buf);
} else {
len += strlen(classnames[cols*classes[i]+0].u.md_str)+1;
}
}
ret = pt = galloc(len+3);
*pt = '\0';
for ( i=cnt-1; i>=0; --i ) {
if ( classnames[cols*classes[i]+0].u.md_str==NULL ) {
sprintf( pt, "%d ", classes[i]);
pt += strlen(pt);
} else {
strcpy(pt, classnames[cols*classes[i]+0].u.md_str);
pt += strlen(pt);
*pt++ = ' ';
}
}
if ( pt>ret && pt[-1]==' ' )
pt[-1] = '\0';
return( ret );
}
static int CCD_GlyphNameCnt(const char *pt) {
int cnt = 0;
while ( *pt ) {
while ( isspace( *pt )) ++pt;
if ( *pt=='\0' )
return( cnt );
++cnt;
while ( !isspace(*pt) && *pt!='\0' ) ++pt;
}
return( cnt );
}
static int seqlookuplen(struct fpst_rule *r) {
int i, len=0;
char buf[20];
len += 4; /* for the arrow, takes 3 bytes in utf8 */
for ( i=0; i<r->lookup_cnt; ++i ) {
sprintf( buf," %d \"\",", r->lookups[i].seq );
len += strlen(buf) + strlen( r->lookups[i].lookup->lookup_name );
}
return( len );
}
static char *addseqlookups(char *pt, struct fpst_rule *r) {
int i;
pt = utf8_idpb(pt, 0x21d2);
for ( i=0; i<r->lookup_cnt; ++i ) {
sprintf( pt," %d <%s>,", r->lookups[i].seq, r->lookups[i].lookup->lookup_name);
pt += strlen(pt);
}
if ( pt[-1]==',' ) --pt;
*pt = '\0';
return( pt );
}
static void parseseqlookups(SplineFont *sf, const char *solooks, struct fpst_rule *r) {
int cnt;
const char *pt;
for ( pt = solooks, cnt=0; *pt!='\0'; ) {
++cnt;
while ( *pt!='<' && *pt!='\0' ) ++pt;
if ( *pt=='<' ) {
++pt;
while ( *pt!='>' && *pt!='\0' ) ++pt;
if ( *pt=='>' ) ++pt;
}
if ( *pt==',' ) ++pt;
}
r->lookup_cnt = cnt;
r->lookups = gcalloc(cnt,sizeof(struct seqlookup));
cnt = 0;
pt = solooks;
forever {
char *end;
r->lookups[cnt].seq = strtol(pt,&end,10);
for ( pt = end+1; isspace( *pt ); ++pt );
if ( *pt=='<' ) {
const char *eoname; char *temp;
++pt;
for ( eoname = pt; *eoname!='\0' && *eoname!='>'; ++eoname );
temp = copyn(pt,eoname-pt);
r->lookups[cnt].lookup = SFFindLookup(sf,temp);
if ( r->lookups[cnt].lookup==NULL )
IError("No lookup in parseseqlookups");
free(temp);
pt = eoname;
if ( *pt=='>' ) ++pt;
} else
IError("No lookup in parseseqlookups");
++cnt;
if ( *pt!=',' )
break;
++pt;
}
}
static char *gruleitem(struct fpst_rule *r) {
char *ret, *pt;
int len;
len = (r->u.glyph.back==NULL ? 0 : strlen(r->u.glyph.back)) +
strlen(r->u.glyph.names) +
(r->u.glyph.fore==0 ? 0 : strlen(r->u.glyph.fore)) +
seqlookuplen(r);
ret = pt = galloc(len+8);
if ( r->u.glyph.back!=NULL && *r->u.glyph.back!='\0' ) {
char *temp = reversenames(r->u.glyph.back);
strcpy(pt,temp);
pt += strlen(temp);
free(temp);
*pt++ = ' ';
}
*pt++ = '|';
*pt++ = ' ';
strcpy(pt,r->u.glyph.names);
pt += strlen(r->u.glyph.names);
*pt++ = ' ';
*pt++ = '|';
*pt++ = ' ';
if ( r->u.glyph.fore!=NULL && *r->u.glyph.fore!='\0' ) {
strcpy(pt,r->u.glyph.fore);
pt += strlen(r->u.glyph.fore);
*pt++ = ' ';
}
pt = addseqlookups(pt, r);
return( ret );
}
static void gruleitem2rule(SplineFont *sf, const char *ruletext,struct fpst_rule *r) {
const char *pt, *pt2;
char *temp, *freeme;
int ch;
if ( ruletext==NULL )
return;
for ( pt=ruletext; *pt!='\0' && *pt!='|'; ++pt );
if ( *pt=='\0' )
return;
if ( pt>ruletext ) {
temp = GlyphNameListDeUnicode(freeme = copyn(ruletext,pt-1-ruletext));
r->u.glyph.back = reversenames(temp);
free(temp); free(freeme);
}
ruletext = pt+2;
for ( pt=ruletext; *pt!='\0' && *pt!='|'; ++pt );
if ( *pt=='\0' )
return;
r->u.glyph.names = GlyphNameListDeUnicode(freeme = copyn(ruletext,pt-1-ruletext));
free(freeme);
ruletext = pt+2;
for ( pt2=ruletext; (ch=utf8_ildb((const char **) &pt2))!='\0' && ch!=0x21d2; );
if ( ch=='\0' )
return;
if ( pt2!=ruletext ) {
r->u.glyph.fore = GlyphNameListDeUnicode(freeme = copyn(ruletext,pt2-3-ruletext));
free(freeme);
}
parseseqlookups(sf,pt2+2,r);
}
static char *classruleitem(struct fpst_rule *r,struct matrix_data **classes, int clen[3], int cols) {
char *ret, *pt;
int len, i, k;
char buf[20];
len = 0;
for ( i=0; i<3; ++i ) {
for ( k=0; k<(&r->u.class.ncnt)[i]; ++k ) {
int c = (&r->u.class.nclasses)[i][k];
if ( classes[i][cols*c+0].u.md_str!=NULL && *classes[i][cols*c+0].u.md_str!='\0' )
len += strlen(classes[i][cols*c+0].u.md_str)+1;
else {
sprintf( buf, "%d ", c);
len += strlen(buf);
}
}
}
ret = pt = galloc((len+8+seqlookuplen(r)) * sizeof(unichar_t));
for ( k=r->u.class.bcnt-1; k>=0; --k ) {
int c = r->u.class.bclasses[k];
if ( classes[1][cols*c+0].u.md_str!=NULL && *classes[1][cols*c+0].u.md_str!='\0' ) {
strcpy(pt,classes[1][cols*c+0].u.md_str);
pt += strlen( pt );
*pt++ = ' ';
} else {
sprintf( pt, "%d ", c);
pt += strlen(pt);
}
}
*pt++ = '|';
for ( k=0; k<r->u.class.ncnt; ++k ) {
int c = r->u.class.nclasses[k];
if ( classes[0][cols*c+0].u.md_str!=NULL && *classes[0][cols*c+0].u.md_str!='\0' ) {
strcpy(pt,classes[0][cols*c+0].u.md_str);
pt += strlen( pt );
*pt++ = ' ';
} else {
sprintf( pt, "%d ", c);
pt += strlen(pt);
}
}
if ( pt[-1]==' ' ) --pt;
*pt++ = '|';
for ( k=0; k<r->u.class.fcnt; ++k ) {
int c = r->u.class.fclasses[k];
if ( classes[2][cols*c+0].u.md_str!=NULL && *classes[2][cols*c+0].u.md_str!='\0' ) {
strcpy(pt,classes[2][cols*c+0].u.md_str);
pt += strlen( pt );
*pt++ = ' ';
} else {
sprintf( pt, "%d ", c);
pt += strlen(pt);
}
}
*pt++ = ' ';
pt = addseqlookups(pt, r);
return( ret );
}
static void classruleitem2rule(SplineFont *sf,const char *ruletext,struct fpst_rule *r,
struct matrix_data **classes, int clen[3], int cols) {
const char *pt, *start, *nstart; char *end;
int len, i, ch, k;
memset(r,0,sizeof(*r));
len = 0; i=1;
for ( pt=ruletext; *pt; ) {
ch = utf8_ildb((const char **) &pt);
while ( ch!='|' && ch!=0x21d2 && ch!='\0' ) {
while ( isspace(ch))
ch = utf8_ildb((const char **) &pt);
if ( ch=='|' || ch== 0x21d2 || ch=='\0' )
break;
++len;
while ( !isspace(ch) && ch!='|' && ch!=0x21d2 && ch!='\0' )
ch = utf8_ildb((const char **) &pt);
}
(&r->u.class.ncnt)[i] = len;
(&r->u.class.nclasses)[i] = galloc(len*sizeof(uint16));
len = 0;
if ( ch=='\0' || ch==0x21d2 )
break;
if ( ch=='|' ) {
if ( i==1 ) i=0;
else if ( i==0 ) i=2;
}
}
len = 0; i=1;
for ( pt=ruletext; *pt; ) {
start = pt;
ch = utf8_ildb((const char **) &pt);
while ( ch!='|' && ch!=0x21d2 && ch!='\0' ) {
while ( isspace(ch)) {
start = pt;
ch = utf8_ildb((const char **) &pt);
}
if ( ch=='|' || ch== 0x21d2 || ch=='\0' )
break;
nstart = start;
while ( !isspace(ch) && ch!='|' && ch!=0x21d2 && ch!='\0' ) {
nstart = pt;
ch = utf8_ildb((const char **) &pt);
}
(&r->u.class.nclasses)[i][len] = strtol(start,&end,10);
if ( end<nstart ) { /* Not a number, must be a class name */
for ( k=0; k<clen[i]; ++k ) {
if ( classes[i][cols*k+0].u.md_str==NULL )
continue;
if ( strlen(classes[i][cols*k+0].u.md_str)==nstart-start &&
strncmp(classes[i][cols*k+0].u.md_str,start,nstart-start)==0 ) {
(&r->u.class.nclasses)[i][len] = k;
break;
}
}
}
start = nstart;
++len;
}
len = 0;
if ( ch=='\0' || ch==0x21d2 )
break;
if ( ch=='|' ) {
if ( i==1 ) i=0;
else if ( i==0 ) i=2;
}
}
/* reverse the backtrack */
for ( i=0; i<r->u.class.bcnt/2; ++i ) {
int temp = r->u.class.bclasses[i];
r->u.class.bclasses[i] = r->u.class.bclasses[r->u.class.bcnt-1-i];
r->u.class.bclasses[r->u.class.bcnt-1-i] = temp;
}
if ( ch=='\0' || *pt=='\0' )
return;
parseseqlookups(sf,pt,r);
}
static void CCD_ParseLookupList(SplineFont *sf, struct fpst_rule *r,GGadget *list) {
int len, i;
struct matrix_data *classes = GMatrixEditGet(list,&len);
r->lookup_cnt = len;
r->lookups = galloc(len*sizeof(struct seqlookup));
for ( i=0; i<len; ++i ) {
r->lookups[i].seq = classes[2*i+1].u.md_ival;
r->lookups[i].lookup = (OTLookup *) classes[2*i+0].u.md_ival;
}
}
static void CCD_EnableNextPrev(struct contextchaindlg *ccd) {
/* Cancel is always enabled, Not so for OK or Next or Prev */
switch ( ccd->aw ) {
case aw_formats:
GGadgetSetEnabled(GWidgetGetControl(ccd->gw,CID_Prev),false);
GGadgetSetEnabled(GWidgetGetControl(ccd->gw,CID_Next),true);
GGadgetSetEnabled(GWidgetGetControl(ccd->gw,CID_OK),false);
break;
case aw_glyphs:
case aw_classnumber:
GGadgetSetEnabled(GWidgetGetControl(ccd->gw,CID_Prev),true);
GGadgetSetEnabled(GWidgetGetControl(ccd->gw,CID_Next),true);
GGadgetSetEnabled(GWidgetGetControl(ccd->gw,CID_OK),false);
break;
case aw_coverage:
case aw_grules:
case aw_classrules:
GGadgetSetEnabled(GWidgetGetControl(ccd->gw,CID_Prev),ccd->fpst->format!=pst_reversecoverage);
GGadgetSetEnabled(GWidgetGetControl(ccd->gw,CID_Next),false);
GGadgetSetEnabled(GWidgetGetControl(ccd->gw,CID_OK),true);
break;
case aw_glyphs_simple:
case aw_classes_simple:
case aw_coverage_simple:
GGadgetSetEnabled(GWidgetGetControl(ccd->gw,CID_Prev),ccd->fpst->type!=pst_reversesub);
GGadgetSetEnabled(GWidgetGetControl(ccd->gw,CID_Next),false);
GGadgetSetEnabled(GWidgetGetControl(ccd->gw,CID_OK),true);
break;
default:
IError("Can't get here");
break;
}
}
static int CCD_ReasonableClassNum(const unichar_t *umatch,GGadget *list,
struct fpst_rule *r, int which ) {
int cols, len;
struct matrix_data *classes;
char *match = u2utf8_copy(umatch);
char *pt, *start, *end;
int doit;
int any,val;
cols = GMatrixEditGetColCnt(list);
classes = GMatrixEditGet(list,&len);
for ( doit = 0; doit<2; ++doit ) {
any = 0;
for ( pt=match;; ) {
while ( *pt==' ' ) ++pt;
if ( *pt=='\0' )
break;
for ( start=pt; *pt!=' ' && *pt!='\0'; ++pt );
val = strtol(start,&end,10);
if ( *end!='\0' ) {
for ( val=len-1; val>=0; --val )
if ( classes[cols*val+0].u.md_str!=NULL &&
strlen(classes[cols*val+0].u.md_str)==pt-start &&
strncasecmp(start,classes[cols*val+0].u.md_str,pt-start)==0 )
break;
}
if ( val<0 || val>=len ) {
ff_post_error(_("Bad Class"),_("%.*s is not a valid class name (or number)"), pt-start, start);
return( false );
}
if ( doit )
(&r->u.class.nclasses)[which][any] = val;
++any;
}
if ( !doit ) {
(&r->u.class.ncnt)[which] = any;
(&r->u.class.nclasses)[which] = galloc(any*sizeof(uint16));
}
}
return( true );
}
static int ClassNamePrep(struct contextchaindlg *ccd,struct matrix_data **classes,int clen[3]) {
int i;
for ( i=0; i<3; ++i ) {
if ( i==0 || !GGadgetIsChecked(GWidgetGetControl(ccd->gw,CID_SameAsClasses+i*20)) )
classes[i] = GMatrixEditGet(GWidgetGetControl(ccd->gw,CID_GList+300+i*20),&clen[i]);
else
classes[i] = GMatrixEditGet(GWidgetGetControl(ccd->gw,CID_GList+300),&clen[i]);
}
return( GMatrixEditGetColCnt(GWidgetGetControl(ccd->gw,CID_GList+300+0*20)) );
}
static void CCD_FinishRule(struct contextchaindlg *ccd) {
struct fpst_rule dummy;
GGadget *list;
int i,tot;
char *buts[3];
buts[0] = _("_Yes"); buts[1] = _("_No"); buts[2] = NULL;
struct matrix_data *md;
int len;
int clen[3], ccols;
struct matrix_data *classes[3];
if ( ccd->aw==aw_classnumber ) {
char *ret;
memset(&dummy,0,sizeof(dummy));
CCD_ParseLookupList(ccd->sf,&dummy,GWidgetGetControl(ccd->gw,CID_LookupList+500));
if ( dummy.lookup_cnt==0 ) {
int ans = gwwv_ask(_("No Sequence/Lookups"),
(const char **) buts,0,1,
_("There are no entries in the Sequence/Lookup List, was this intentional?"));
if ( ans==1 )
return;
}
ccols = ClassNamePrep(ccd,classes,clen);
if ( !CCD_ReasonableClassNum(
_GGadgetGetTitle(GWidgetGetControl(ccd->gw,CID_ClassNumbers)),
GWidgetGetControl(ccd->gw,CID_ClassList),
&dummy,0) ||
!CCD_ReasonableClassNum(
_GGadgetGetTitle(GWidgetGetControl(ccd->gw,CID_ClassNumbers+20)),
GWidgetGetControl(ccd->gw,CID_ClassList+20),
&dummy,1) ||
!CCD_ReasonableClassNum(
_GGadgetGetTitle(GWidgetGetControl(ccd->gw,CID_ClassNumbers+40)),
GWidgetGetControl(ccd->gw,CID_ClassList+40),
&dummy,2)) {
FPSTRuleContentsFree(&dummy,pst_class);
return;
}
/* reverse the backtrack */
for ( i=0; i<dummy.u.class.bcnt/2; ++i ) {
int temp = dummy.u.class.bclasses[i];
dummy.u.class.bclasses[i] = dummy.u.class.bclasses[dummy.u.class.bcnt-1-i];
dummy.u.class.bclasses[dummy.u.class.bcnt-1-i] = temp;
}
for ( i=0; i<dummy.lookup_cnt; ++i ) {
if ( dummy.lookups[i].seq >= dummy.u.class.ncnt ) {
ff_post_error(_("Bad Sequence/Lookup List"),_("Sequence number out of bounds, must be less than %d (number of classes in list above)"), dummy.u.class.ncnt );
return;
}
}
ret = classruleitem(&dummy,classes,clen,ccols);
FPSTRuleContentsFree(&dummy,pst_class);
list = GWidgetGetControl(ccd->gw,CID_GList+200);
md = GMatrixEditGet(list,&len);
free(md[ccd->row_being_edited].u.md_str);
md[ccd->row_being_edited].u.md_str = ret;
GMatrixEditSet(list,md,len,false);
ccd->aw = aw_classrules;
GDrawSetVisible(ccd->classnumber,false);
GDrawSetVisible(ccd->classrules,true);
} else if ( ccd->aw==aw_glyphs ) { /* It's from glyph list */
char *ret, *temp, *freeme;
memset(&dummy,0,sizeof(dummy));
CCD_ParseLookupList(ccd->sf,&dummy,GWidgetGetControl(ccd->gw,CID_LookupList));
if ( dummy.lookup_cnt==0 ) {
int ans = gwwv_ask(_("No Sequence/Lookups"),
(const char **) buts,0,1,
_("There are no entries in the Sequence/Lookup List, was this intentional?"));
if ( ans==1 )
return;
}
temp = GGadgetGetTitle8(GWidgetGetControl(ccd->gw,CID_GlyphList));
dummy.u.glyph.names = GlyphNameListDeUnicode(temp);
free(temp);
tot = CCD_GlyphNameCnt( temp = GGadgetGetTitle8(GWidgetGetControl(ccd->gw,CID_GlyphList)));
free(temp);
for ( i=0; i<dummy.lookup_cnt; ++i ) {
if ( dummy.lookups[i].seq >= tot ) {
ff_post_error(_("Bad Sequence/Lookup List"),_("Sequence number out of bounds, must be less than %d (number of glyphs, classes or coverage tables)"), tot );
return;
}
}
temp = GGadgetGetTitle8(GWidgetGetControl(ccd->gw,CID_GlyphList+20));
freeme = GlyphNameListDeUnicode(temp);
dummy.u.glyph.back = reversenames(freeme);
free(temp); free(freeme);
temp = GGadgetGetTitle8(GWidgetGetControl(ccd->gw,CID_GlyphList+40));
dummy.u.glyph.fore = GlyphNameListDeUnicode(temp);
free(temp);
ret = gruleitem(&dummy);
FPSTRuleContentsFree(&dummy,pst_glyphs);
list = GWidgetGetControl(ccd->gw,CID_GList+0);
md = GMatrixEditGet(list,&len);
free(md[ccd->row_being_edited].u.md_str);
md[ccd->row_being_edited].u.md_str = ret;
GMatrixEditSet(list,md,len,false);
ccd->aw = aw_grules;
GDrawSetVisible(ccd->glyphs,false);
GDrawSetVisible(ccd->grules,true);
} else {
IError("Bad format in CCD_FinishRule");
}
}
static int isEverythingElse(char *text) {
/* GT: The string "{Everything Else}" is used in the context of a list */
/* GT: of classes (a set of kerning classes) where class 0 designates the */
/* GT: default class containing all glyphs not specified in the other classes */
int ret = strcmp(text,_("{Everything Else}"));
return( ret==0 );
}
static char *CCD_PickGlyphsForClass(GGadget *g,int r, int c) {
struct contextchaindlg *ccd = GDrawGetUserData(GGadgetGetWindow(g));
int rows, cols = GMatrixEditGetColCnt(g);
struct matrix_data *classes = _GMatrixEditGet(g,&rows);
char *new = GlyphSetFromSelection(ccd->sf,ccd->layer,classes[r*cols+c].u.md_str);
return( new );
}
static void _CCD_FromSelection(struct contextchaindlg *ccd,int cid ) {
char *curval = GGadgetGetTitle8(GWidgetGetControl(ccd->gw,cid));
char *new = GlyphSetFromSelection(ccd->sf,ccd->layer,curval);
free(curval);
if ( new==NULL )
return;
GGadgetSetTitle8(GWidgetGetControl(ccd->gw,cid),new);
free(new);
}
static int CCD_FromSelection(GGadget *g, GEvent *e) {
if ( e->type==et_controlevent && e->u.control.subtype == et_buttonactivate ) {
struct contextchaindlg *ccd = GDrawGetUserData(GGadgetGetWindow(g));
int cid = (intpt) GGadgetGetUserData(g);
_CCD_FromSelection(ccd,cid);
}
return( true );
}
static struct matrix_data *MD2MD(struct matrix_data *md,int len) {
struct matrix_data *newmd = gcalloc(2*len,sizeof(struct matrix_data));
int i;
for ( i=0; i<len; ++i ) {
newmd[2*i+0].u.md_str = copy(md[3*i+0].u.md_str);
newmd[2*i+1].u.md_str = copy(md[3*i+1].u.md_str);
}
return( newmd );
}
static void CCD_NewGlyphRule(GGadget *glyphrules,int r,int c) {
struct contextchaindlg *ccd = GDrawGetUserData(GGadgetGetWindow(glyphrules));
int rows;
struct matrix_data *rulelist = GMatrixEditGet(glyphrules,&rows);
struct fpst_rule dummy;
GGadget *lookuplist = GWidgetGetControl(ccd->glyphs,CID_LookupList+0);
struct matrix_data *md;
char *temp2;
int j;
memset(&dummy,0,sizeof(dummy));
gruleitem2rule(ccd->sf,rulelist[r].u.md_str,&dummy);
GGadgetSetTitle8(GWidgetGetControl(ccd->gw,CID_GlyphList),dummy.u.glyph.names==NULL?"":dummy.u.glyph.names);
GGadgetSetTitle8(GWidgetGetControl(ccd->gw,CID_GlyphList+20),(temp2 = reversenames(dummy.u.glyph.back))==NULL ? "" : temp2);
free(temp2);
GGadgetSetTitle8(GWidgetGetControl(ccd->gw,CID_GlyphList+40),dummy.u.glyph.fore!=NULL?dummy.u.glyph.fore:"");
md = gcalloc(2*dummy.lookup_cnt,sizeof(struct matrix_data));
for ( j=0; j<dummy.lookup_cnt; ++j ) {
md[2*j+0].u.md_ival = (intpt) (void *) dummy.lookups[j].lookup;
md[2*j+1].u.md_ival = (intpt) dummy.lookups[j].seq;
}
GMatrixEditSet(lookuplist,md,dummy.lookup_cnt,false);
ccd->aw = aw_glyphs;
ccd->row_being_edited = r;
GDrawSetVisible(ccd->grules,false);
GDrawSetVisible(ccd->glyphs,true);
CCD_EnableNextPrev(ccd);
}
static char *_CCD_NewGlyphRule(GGadget *glyphrules,int r,int c) {
int rows;
struct matrix_data *rulelist = GMatrixEditGet(glyphrules,&rows);
CCD_NewGlyphRule(glyphrules,r,c);
return( copy( rulelist[r].u.md_str ) ); /* We change the display to get the new value, but we must return something... */
}
static void CCD_NewClassRule(GGadget *classrules,int r,int c) {
struct contextchaindlg *ccd = GDrawGetUserData(GGadgetGetWindow(classrules));
int rows;
struct matrix_data *rulelist = GMatrixEditGet(classrules,&rows);
struct fpst_rule dummy;
GGadget *lookuplist = GWidgetGetControl(ccd->classnumber,CID_LookupList+500);
struct matrix_data *md, *classes[3];
int len, clen[3], ccols;
int i,j;
char *temp;
ccols = ClassNamePrep(ccd,classes,clen);
memset(&dummy,0,sizeof(dummy));
classruleitem2rule(ccd->sf,rulelist[r].u.md_str,&dummy,classes,clen,ccols);
GGadgetSetTitle8(GWidgetGetControl(ccd->gw,CID_ClassNumbers),
(temp=classnumbers(dummy.u.class.ncnt,dummy.u.class.nclasses,classes[0],clen[0],ccols)));
free(temp);
GGadgetSetTitle8(GWidgetGetControl(ccd->gw,CID_ClassNumbers+20),
(temp=rclassnumbers(dummy.u.class.bcnt,dummy.u.class.bclasses,classes[1],clen[1],ccols)));
free(temp);
GGadgetSetTitle8(GWidgetGetControl(ccd->gw,CID_ClassNumbers+40),
(temp=classnumbers(dummy.u.class.fcnt,dummy.u.class.fclasses,classes[2],clen[2],ccols)));
free(temp);
md = gcalloc(2*dummy.lookup_cnt,sizeof(struct matrix_data));
for ( j=0; j<dummy.lookup_cnt; ++j ) {
md[2*j+0].u.md_ival = (intpt) (void *) dummy.lookups[j].lookup;
md[2*j+1].u.md_ival = (intpt) dummy.lookups[j].seq;
}
GMatrixEditSet(lookuplist,md,dummy.lookup_cnt,false);
FPSTRuleContentsFree(&dummy,pst_class);
GWidgetIndicateFocusGadget(GWidgetGetControl(ccd->gw,CID_ClassNumbers));
for ( i=0; i<3; ++i ) {
if ( i!=0 && GGadgetIsChecked(GWidgetGetControl(ccd->gw,CID_SameAsClasses+i*20)) )
md = GMatrixEditGet(GWidgetGetControl(ccd->gw,CID_GList+300),&len);
else
md = GMatrixEditGet(GWidgetGetControl(ccd->gw,CID_GList+300+i*20),&len);
GMatrixEditSet(GWidgetGetControl(ccd->gw,CID_ClassList+i*20),MD2MD(md,len),len,false);
}
ccd->aw = aw_classnumber;
ccd->row_being_edited = r;
GDrawSetVisible(ccd->classrules,false);
GDrawSetVisible(ccd->classnumber,true);
CCD_EnableNextPrev(ccd);
}
static char *_CCD_NewClassRule(GGadget *classrules,int r,int c) {
int rows;
struct matrix_data *rulelist = GMatrixEditGet(classrules,&rows);
CCD_NewClassRule(classrules,r,c);
return( copy( rulelist[r].u.md_str ) ); /* We change the display to get the new value, but we must return something... */
}
static int CCD_SameAsClasses(GGadget *g, GEvent *e) {
int ison = GGadgetIsChecked(g);
int cid = GGadgetGetCid(g);
struct contextchaindlg *ccd = GDrawGetUserData(GGadgetGetWindow(g));
if ( cid < CID_SameAsClasses_S ) {
int off = cid-CID_SameAsClasses;
GGadgetSetEnabled(GWidgetGetControl(ccd->gw,CID_GList+300+off),!ison);
return( true );
} else {
int off = cid-CID_SameAsClasses_S;
GGadgetSetEnabled(GWidgetGetControl(ccd->gw,CID_CList_Simple+off),!ison);
return( true );
}
}
static void CCD_ClassSelected(GGadget *g, int r, int c) {
struct contextchaindlg *ccd = GDrawGetUserData(GGadgetGetWindow(g));
int off = GGadgetGetCid(g)-CID_ClassList;
int rows, cols = GMatrixEditGetColCnt(g);
struct matrix_data *classes = GMatrixEditGet(g,&rows);
GGadget *tf = GWidgetGetControl(ccd->gw,CID_ClassNumbers+off);
char buf[20];
unichar_t ubuf[80];
if ( r<0 || r>=rows )
return;
if ( classes[cols*r+0].u.md_str == NULL || classes[cols*r+0].u.md_str[0]=='\0' ) {
sprintf( buf, " %d ", r );
uc_strcpy(ubuf,buf);
} else {
ubuf[0]=' ';
utf82u_strncpy(ubuf-1,classes[cols*r+0].u.md_str,sizeof(ubuf)/sizeof(ubuf[0])-2 );
ubuf[sizeof(ubuf)/sizeof(ubuf[0])-2] = '\0';
uc_strcat(ubuf," ");
}
GTextFieldReplace(tf,ubuf);
return;
}
static void CCD_Close(struct contextchaindlg *ccd) {
if ( ccd->isnew )
GFI_FinishContextNew(ccd->gfi,ccd->fpst,false);
ccd->done = true;
}
static char **CCD_ParseCoverageList(struct contextchaindlg *ccd,int cid,int *cnt) {
int i;
int _cnt;
struct matrix_data *covers = GMatrixEditGet(GWidgetGetControl(ccd->gw,cid),&_cnt);
char **ret;
*cnt = _cnt;
if ( _cnt==0 )
return( NULL );
ret = galloc(*cnt*sizeof(char *));
for ( i=0; i<_cnt; ++i )
ret[i] = GlyphNameListDeUnicode(covers[i].u.md_str);
return( ret );
}
static void CoverageReverse(char **bcovers,int bcnt) {
int i;
char *temp;
for ( i=0; i<bcnt/2; ++i ) {
temp = bcovers[i];
bcovers[i] = bcovers[bcnt-i-1];
bcovers[bcnt-i-1] = temp;
}
}
static void _CCD_Ok(struct contextchaindlg *ccd) {
FPST *fpst = ccd->fpst, *dummyfpst;
int len, i, k, had_class0, clen[3], ccols;
struct matrix_data *old, *classes[3], *classes_simple;
struct fpst_rule dummy;
char *msg;
int ans, cnt, cols, first, last, forward_start;
int iswarning;
char *temp;
int has[3];
char *buts[3];
buts[0] = _("_Yes"); buts[1] = _("_No"); buts[2] = NULL;
switch ( ccd->aw ) {
case aw_grules: {
old = GMatrixEditGet(GWidgetGetControl(ccd->gw,CID_GList),&len);
if ( len==0 ) {
ff_post_error(_("Missing rules"),_(" There must be at least one contextual rule"));
return;
}
FPSTRulesFree(fpst->rules,fpst->format,fpst->rule_cnt);
fpst->format = pst_glyphs;
fpst->rule_cnt = len;
fpst->rules = gcalloc(len,sizeof(struct fpst_rule));
for ( i=0; i<len; ++i )
gruleitem2rule(ccd->sf,old[i].u.md_str,&fpst->rules[i]);
} break;
case aw_classrules: {
old = GMatrixEditGet(GWidgetGetControl(ccd->gw,CID_GList+200),&len);