/
oflib.c
2143 lines (1928 loc) · 61.8 KB
/
oflib.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) 2008-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 <ustring.h>
#include <utype.h>
#include <gkeysym.h>
#include <unistd.h>
#include <sys/stat.h>
#include "gfile.h"
#include <pthread.h>
#ifndef __VMS
# include <sched.h>
#endif
#include <setjmp.h>
#include <signal.h>
extern GBox _ggadget_Default_Box;
#define ACTIVE_BORDER (_ggadget_Default_Box.active_border)
#define MAIN_FOREGROUND (_ggadget_Default_Box.main_foreground)
#if defined(__MINGW32__)
# define SIGUSR1 18
#endif
/* A dialog for browsing through fonts on the Open Font Library website */
/* The OFL returns fonts 10 at a time. The biggest page so far is 52949 bytes */
/* (16 June 2008). We might as well just plan to read each page entirely into*/
/* memory. Not that big, and makes life easier */
#define OFL_FONT_URL "http://openfontlibrary.org/media/view/media/fonts"
/* That gets the first 10. Subsequent request have "?offset=10" (or 20, 30, etc) appended */
/* Fonts come out "most recent first" */
static int fakemons[] = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
static char *monnames[] = { N_("January"), N_("February"), N_("March"), N_("April"), N_("May"), N_("June"), N_("July"), N_("August"), N_("September"), N_("October"), N_("November"), N_("December") };
enum ofl_license { ofll_ofl, ofll_pd };
struct ofl_download_urls {
char *url;
char *comment;
uint8 selected;
struct ofl_download_urls *next;
};
struct ofl_font_info {
char *name;
char *author;
int date; /* Not real date. Minutes since 2005, assuming Feb has 29 days*/
char *taglist;
enum ofl_license license;
struct ofl_download_urls *urls;
/* If they've looked at the preview image in the past, cache that locally */
char *preview_filename;
GImage *preview;
/* If they interrupted the download after this font info (so there might */
/* be a gap after it) set the following */
uint8 potential_gap_after_me;
uint8 open; /* Display download URLS in UI */
uint8 selected;
uint8 downloading_in_background;
};
struct ofl_state {
struct ofl_font_info *fonts;
int fcnt, fmax;
int complete; /* True if there is no font on the website */
/* older than those we've got. Since we download fonts in */
/* reverse chronological order, and the user can interrupt */
/* the process then we might have several gaps */
/* Fonts info will be retained in reverse chronological order */
/* it may be displayed quite differently */
};
static char *strconcat_free(char *str1, char *str2) {
char *ret;
int len;
if ( str1==NULL )
return( str2 );
if ( str2==NULL )
return( str1 );
len = strlen(str1);
ret = galloc(len+strlen(str2)+1);
strcpy(ret,str1);
strcpy(ret+len,str2);
free(str1); free(str2);
return( ret );
}
static char *despace(char *str) {
char *to = str, *orig = str;
while ( *str ) {
*to++ = *str;
if ( isspace(*str)) {
while ( isspace(*str)) ++str;
} else
++str;
}
*to='\0';
return( orig );
}
static int parseOFLibDate(char *str) {
char *end;
int days, hours, min, i;
/* OFLib dates look like "October 23, 2006 02:16 am" */
while ( isspace(*str)) ++str;
days = 0;
for ( i=0; i<12; ++i ) {
if ( strncmp(str,monnames[i],strlen(monnames[i]))==0 )
break;
days += fakemons[i];
}
if ( i!=12 )
str += strlen(monnames[i]);
while ( isspace(*str)) ++str;
days += strtol(str,&end,10)-1;
str = end;
while ( *str==',' || isspace(*str)) ++str;
days += 366 * (strtol(str,&end,10)-2005);
str = end;
while ( isspace(*str)) ++str;
hours = strtol(str,&end,10);
if ( hours==12 ) hours=0;
str = end;
if ( *str==':' ) ++str;
min = strtol(str,&end,10);
str = end;
while ( isspace(*str)) ++str;
if ( strncmp(str,"pm",2)==0 )
hours += 12;
return( days*24*60 + hours*60 + min );
}
static char *formatOFLibDate(char *buffer, int blen, int date) {
int year, mon, day, hours, min, am;
min = date%60;
date /= 60;
hours = date%24;
date /= 24;
day = date % 366;
date /= 366;
year = 2005 + date;
if ( hours==0 && min==0 )
am = -1; /* midnight */
else if ( hours == 12 && min==0 )
am = -2; /* noon */
else if ( hours<12 )
am = true;
else
am = false;
for ( mon=0 ; mon<12; ++mon ) {
if ( day<fakemons[mon] )
break;
day -= fakemons[mon];
}
if ( am<0 )
snprintf( buffer, blen, "%d %s %d %s", day+1, _(monnames[mon]), year,
am==-1 ? _("Midnight") : _("Noon") );
else
snprintf( buffer, blen, "%d %s %d %d:%02d %s", day+1, _(monnames[mon]), year,
hours, min, am ? _("AM") : _("PM") );
return( buffer );
}
static char *skip_over_plain_text(char *start) {
char *end = strchr(start,'<');
if ( end == NULL )
return( start+strlen(start));
return( end );
}
static char *skip_to_plain_text(char *start) {
while ( *start=='<' || isspace(*start)) {
if ( *start=='<' ) {
while( *start!='\0' && *start!='>' ) {
if ( *start=='"' || *start=='\'' ) {
char ch = *start;
++start;
while ( *start!='\0' && *start!=ch )
++start;
if ( *start==ch ) ++start;
} else
++start;
}
if ( *start=='>' ) ++start;
} else
++start;
}
return( start );
}
static struct ofl_font_info *ParseOFLFontPage(char *page, int *more) {
char *pt, *start, *test, *end, *temp, *ptend, *url;
struct ofl_font_info *block, *cur;
struct ofl_download_urls *duhead, *dulast, *ducur;
char *name, *author, *taglist;
int date, license, index;
*more = false;
pt = strstr(page,"<!-- CONTENT STARTS -->");
if ( pt==NULL )
return( NULL );
index = 0;
block = gcalloc(21,sizeof(struct ofl_font_info));
while ( (start=strstr(pt,"<div id=\"cc_record_listing\">"))!=NULL ) {
if ( (start = strstr(start,"<h2>"))==NULL )
break;
if ( (start = skip_to_plain_text(start))==NULL )
break;
end = skip_over_plain_text(start);
name = copyn(start,end-start);
start = end;
if ( (start = strstr(start,"<th>by:</th>"))==NULL )
break;
start += strlen("<th>by:</th>");
if ( (start = skip_to_plain_text(start))==NULL )
break;
end = skip_over_plain_text(start);
author = copyn(start,end-start);
start = end;
if ( (start = strstr(start,"<th>date:</th>"))==NULL )
break;
start += strlen("<th>date:</th>");
if ( (start = skip_to_plain_text(start))==NULL )
break;
end = skip_over_plain_text(start);
temp = copyn(start,end-start);
start = end;
date = parseOFLibDate(temp);
free(temp);
if ( (start = strstr(start,"<th>tags:</th>"))==NULL )
break;
start += strlen("<th>tags:</th>");
if ( (start = strstr(start,"<td>"))==NULL )
break;
if ( (end = strstr(start,"</td>"))==NULL )
break;
taglist = NULL;
forever {
test = skip_to_plain_text(start);
if ( test==NULL || test>end )
break;
ptend = skip_over_plain_text(test);
temp = despace(copyn(test,ptend-test));
test = ptend;
taglist = strconcat_free(taglist,temp);
/* Plain text alternates between tag and comma, we want both */
test = skip_over_plain_text(test);
if ( test==NULL )
break;
start = test;
}
if ( (start = strstr(start,"<th>license:</th>"))==NULL )
break;
license = ofll_pd;
test = strstr(start,"<img src=\"");
if ( test==NULL )
break;
test = strstr(test,"lics/");
if ( test==NULL )
break;
test += strlen("lics/");
if ( strncmp(test,"small-", strlen("small-"))==0 )
test += strlen("small-");
if ( test[0]=='p' && test[1]=='d' )
license = ofll_pd;
else if ( test[0]=='o' && test[1]=='f' && test[2]=='l' )
license = ofll_ofl;
if ( (start = strstr(start,"<div class=\"cc_file_download_popup\""))==NULL )
break;
if ( (end = strstr(start,"</div>"))==NULL )
break;
duhead = dulast = NULL;
forever {
if ( (test = strstr(start,"<a href=\""))==NULL )
break;
if ( test>end )
break;
test += strlen("<a href=\"");
temp = strchr(test,'"');
if ( temp==NULL || temp>end )
break;
url = copyn(test,temp-test);
test = strchr(temp,'>');
if ( test==NULL )
break;
test = skip_to_plain_text(test+1);
if ( test==NULL )
break;
ducur = chunkalloc(sizeof( struct ofl_download_urls ));
ducur->url = url;
ptend = skip_over_plain_text(test);
ducur->comment = copyn(test,ptend-test);
test = ptend;
if ( duhead==NULL )
duhead = ducur;
else
dulast->next = ducur;
dulast = ducur;
start = test;
}
pt = end;
if ( duhead==NULL )
continue;
if ( index>20 )
break;
cur = &block[index];
memset(cur,0,sizeof(*cur));
cur->name = name;
cur->author = author;
cur->date = date;
cur->taglist = taglist;
cur->license = license;
cur->urls = duhead;
++index;
}
*more = false;
if ( pt!=NULL ) {
*more = strstr(pt,"More >>>")!=NULL;
if ( index>0 )
block[index-1].potential_gap_after_me = *more;
}
return( block );
}
static void oflfiFreeContents(struct ofl_font_info *oflfi) {
struct ofl_download_urls *du, *next;
free(oflfi->name);
free(oflfi->author);
free(oflfi->taglist);
for ( du=oflfi->urls; du!=NULL; du=next ) {
next = du->next;
free(du->comment);
free(du->url);
chunkfree(du,sizeof(*du));
}
free(oflfi->preview_filename);
if ( oflfi->preview!=NULL )
GImageDestroy(oflfi->preview);
}
static int OflInfoMerge(struct ofl_state *all,struct ofl_font_info *block) {
int i,j,k,l,tot,lastj,anymatches;
/* We return whether any of the new font_infos were the same as */
/* any of the old */
if ( block[0].name==NULL ) {
free(block);
return( false );
}
for ( i=0; block[i].name!=NULL; ++i );
tot = i;
if ( all->fonts==NULL ) {
all->fonts = block;
all->fcnt = all->fmax = tot;
return( false );
} else {
anymatches = false;
lastj = 0;
for ( i=0; block[i].name!=NULL; ) {
for ( j=lastj; j<all->fcnt; ++j )
if ( block[i].date >= all->fonts[j].date )
break;
lastj = j;
if ( j>0 )
all->fonts[j-1].potential_gap_after_me = false;
if ( j>=all->fcnt ) {
if ( all->fcnt+tot-i > all->fmax )
all->fonts = grealloc(all->fonts,(all->fmax += (tot-i+70))*sizeof(struct ofl_font_info));
memcpy(all->fonts+all->fcnt,block+i,(tot-i)*sizeof(struct ofl_font_info));
all->fcnt += (tot-i);
return( anymatches );
} else if ( block[i].date == all->fonts[j].date ) {
oflfiFreeContents(&block[i]);
anymatches = true;
++i;
} else {
for ( k=i; block[k].name!=NULL; ++k )
if ( block[k].date <= all->fonts[j].date )
break;
if ( all->fcnt+k-i > all->fmax )
all->fonts = grealloc(all->fonts,(all->fmax += (k-i+70))*sizeof(struct ofl_font_info));
for ( l=all->fcnt-1; l>=j; --l )
all->fonts[l+(k-i)] = all->fonts[l];
memcpy(all->fonts+j,block+i,(k-i)*sizeof(struct ofl_font_info));
all->fcnt += (k-i);
i=k;
}
}
return( anymatches );
}
}
enum tok_type { tok_int, tok_str, tok_name, tok_EOF };
struct tokbuf {
char *buf;
int buf_max;
enum tok_type type;
int ival;
};
static enum tok_type ofl_gettoken(FILE *ofl, struct tokbuf *tok) {
int ch;
while ( isspace( ch=getc(ofl)) );
if ( isdigit(ch) ) {
char numbuf[40], *pt;
tok->type = tok_int;
pt = numbuf;
while ( isdigit(ch) && pt<numbuf+sizeof(numbuf)-2) {
*pt++ = ch;
ch = getc(ofl);
}
*pt ='\0';
ungetc(ch,ofl);
tok->ival = strtol(numbuf,NULL,10);
} else if ( ch==EOF ) {
tok->type = tok_EOF;
} else if ( ch=='"' ) {
char *pt, *end;
tok->type = tok_str;
pt = tok->buf; end = pt+tok->buf_max;
while ( (ch=getc(ofl))!=EOF && ch!='"' ) {
if ( pt>=end ) {
int off = pt-tok->buf;
tok->buf = grealloc(tok->buf,tok->buf_max = (end-tok->buf)+200);
pt = tok->buf+off;
end = tok->buf+tok->buf_max;
}
*pt++ = ch;
}
if ( pt>=end ) {
int off = pt-tok->buf;
tok->buf = grealloc(tok->buf,tok->buf_max = (pt-end)+200);
pt = tok->buf+off;
}
*pt++ = '\0';
} else {
char *pt, *end;
tok->type = tok_name;
pt = tok->buf; end = pt+tok->buf_max;
while ( ch!=EOF && ch!='"' && !isspace(ch) ) {
if ( pt>=end ) {
int off = pt-tok->buf;
tok->buf = grealloc(tok->buf,tok->buf_max = (end-tok->buf)+200);
pt = tok->buf+off;
end = tok->buf+tok->buf_max;
}
*pt++ = ch;
ch = getc(ofl);
}
if ( pt>=end ) {
int off = pt-tok->buf;
tok->buf = grealloc(tok->buf,tok->buf_max = (pt-end)+200);
pt = tok->buf+off;
}
*pt++ = '\0';
}
return( tok->type );
}
static char *getOFLibDir(void) {
static char *oflibdir=NULL;
char buffer[1025];
if ( oflibdir!=NULL )
return( oflibdir );
if ( getPfaEditDir(buffer)==NULL )
return( NULL );
sprintf(buffer,"%s/OFLib", getPfaEditDir(buffer));
if ( access(buffer,F_OK)==-1 )
if ( GFileMkDir(buffer)==-1 )
return( NULL );
oflibdir = copy(buffer);
return( oflibdir );
}
static void LoadOFLibState(struct ofl_state *all) {
FILE *file;
int ch;
struct ofl_download_urls *du, *last;
struct ofl_font_info *cur;
struct tokbuf tok;
char buffer[1025];
if ( getOFLibDir()==NULL )
return;
sprintf( buffer,"%s/State", getOFLibDir());
file = fopen(buffer,"r");
if ( file==NULL )
return;
memset(&tok,0,sizeof(tok));
if ( ofl_gettoken(file,&tok)!= tok_name || strcmp(tok.buf,"OFLibState")!=0 ) {
/* Not an OFLibState file */
fclose(file);
free(tok.buf);
return;
}
cur = NULL;
forever {
if ( ofl_gettoken(file,&tok)!= tok_name )
break;
if ( strcmp(tok.buf,"Count:")==0 ) {
if ( ofl_gettoken(file,&tok)!= tok_int )
break;
all->fmax = tok.ival;
all->fcnt = 0;
all->fonts = gcalloc(all->fmax,sizeof(struct ofl_font_info));
} else if ( strcmp(tok.buf,"Complete:")==0 ) {
if ( ofl_gettoken(file,&tok)!= tok_int )
break;
all->complete = tok.ival;
} else if ( strcmp(tok.buf,"Font:")==0 ) {
cur = &all->fonts[all->fcnt];
if ( ofl_gettoken(file,&tok)!= tok_str )
break;
cur->name = copy(tok.buf);
if ( ofl_gettoken(file,&tok)!= tok_str )
break;
cur->author = copy(tok.buf);
if ( ofl_gettoken(file,&tok)!= tok_int )
break;
cur->date = tok.ival;
if ( ofl_gettoken(file,&tok)!= tok_str )
break;
cur->taglist = copy(tok.buf);
if ( ofl_gettoken(file,&tok)!= tok_str )
break;
cur->license = strcmp(tok.buf,"OFL")==0 ? ofll_ofl : ofll_pd;
if ( ofl_gettoken(file,&tok)!= tok_str )
break;
cur->preview_filename = tok.buf[0]=='\0' ? NULL : copy(tok.buf);
if ( ofl_gettoken(file,&tok)!= tok_int )
break;
cur->potential_gap_after_me = tok.ival;
++(all->fcnt);
last = NULL;
} else if ( strcmp(tok.buf,"URL:")==0 ) {
du = chunkalloc(sizeof( struct ofl_download_urls ));
if ( ofl_gettoken(file,&tok)!= tok_str )
break;
du->comment = copy(tok.buf);
if ( ofl_gettoken(file,&tok)!= tok_str )
break;
du->url = copy(tok.buf);
if ( cur==NULL )
break;
if ( last==NULL )
cur->urls = du;
else
last->next = du;
last = du;
}
while ( (ch=getc(file))!=EOF && ch!='\n' );
}
free(tok.buf);
fclose(file);
}
static void DumpOFLibState(struct ofl_state *all) {
FILE *file;
int i;
struct ofl_download_urls *du;
struct ofl_font_info *cur;
char buffer[1025];
if ( getOFLibDir()==NULL )
return;
sprintf( buffer,"%s/State", getOFLibDir());
file = fopen(buffer,"w");
if ( file==NULL )
return;
fprintf( file, "OFLibState\n" );
fprintf( file, "Count: %d\n", all->fcnt );
fprintf( file, "Complete: %d\n", all->complete ); /* Well, it was complete when saved, might be new things since, but there are no holes */
for ( i=0; i<all->fcnt; ++i ) {
cur = &all->fonts[i];
fprintf( file, "Font: \"%s\" \"%s\" %d \"%s\" \"%s\" \"%s\" %d\n",
cur->name, cur->author, cur->date, cur->taglist,
cur->license==ofll_ofl ? "OFL" : "PD",
cur->preview_filename == NULL ? "" : cur->preview_filename,
cur->potential_gap_after_me );
for ( du = cur->urls; du!=NULL; du=du->next ) {
fprintf( file, " URL: \"%s\" \"%s\"\n", du->comment, du->url );
}
}
fclose(file);
}
/* ************************************************************************** */
/* ********************************* Dialog ********************************* */
/* ************************************************************************** */
int oflib_automagic_preview = false;
typedef struct previewthread {
struct previewthread *next;
pthread_t preview_thread;
pthread_mutex_t *http_thread_can_do_stuff;
int done;
struct ofl_font_info *fi;
struct ofl_download_urls *active;
int is_image;
FILE *result;
} PreviewThread;
typedef struct oflibdlg {
GWindow gw;
struct ofl_state all;
int scnt, smax;
struct ofl_font_info **show;
int ltot, lsize, l_off_top;
int margin;
int fh,as;
GFont *font;
int lheight, lwidth;
int pheight, pwidth;
int p_off_top, p_off_left;
GImage *preview_image;
/* Spawn separate thread for downloading fonts information */
/* (Not for the downloads the user asks for, but the background download */
/* to check that our database is up-to-date, with luck, user won't notice) */
pthread_t http_thread;
pthread_mutex_t http_thread_can_do_stuff; /* Like allocate memory, call non-thread-safe routines */
pthread_mutex_t http_thread_done; /* child releases this when it has finished its current task */
GTimer *http_timer;
char *databuf;
int datalen;
int nextfont;
int done;
int amount_read;
int any_new_stuff;
int die;
GWindow alert;
int background_preview_requests;
PreviewThread *active;
} OFLibDlg;
static OFLibDlg *active;
static pthread_key_t jump_key;
enum searchtype { st_showall, st_author, st_name, st_tag, st_license };
static int initted = false;
static GTextInfo searchtypes[] = {
{ (unichar_t *) N_("Show All"), NULL, 0, 0, (void *) st_showall, NULL, 0, 0, 0, 0, 1, 0, 1, 0, 0, '\0'}, /* Selected, One byte */
{ (unichar_t *) N_("Designer"), NULL, 0, 0, (void *) st_author, NULL, 0, 0, 0, 0, 0, 0, 1, 0, 0, '\0'},
{ (unichar_t *) N_("Name"), NULL, 0, 0, (void *) st_name, NULL, 0, 0, 0, 0, 0, 0, 1, 0, 0, '\0'},
{ (unichar_t *) N_("Tag(s)"), NULL, 0, 0, (void *) st_tag, NULL, 0, 0, 0, 0, 0, 0, 1, 0, 0, '\0'},
{ (unichar_t *) N_("License"), NULL, 0, 0, (void *) st_license, NULL, 0, 0, 0, 0, 0, 0, 1, 0, 0, '\0'},
GTEXTINFO_EMPTY
};
#define CID_SortDate 1001
#define CID_SortAuthor 1002
#define CID_SortName 1003
#define CID_SortReverse 1004
#define CID_SearchType 1011
#define CID_SearchString 1012
#define CID_Fonts 1021
#define CID_FontSB 1022
#define CID_PreviewW 1031
#define CID_PreviewVSB 1032
#define CID_PreviewHSB 1033
#define CID_BackgroundPreview 1034
#define CID_Open 1040
#define CID_Preview 1041
#define CID_Done 1042
#define CID_TopBox 1051
static void OFLibInit(void) {
int i;
initted = true;
for ( i=0; searchtypes[i].text!=NULL; ++i )
searchtypes[i].text = (unichar_t *) _((char *) searchtypes[i].text);
}
static struct ofl_download_urls *OFLibHasImage(OFLibDlg *d,int sel_font) {
struct ofl_download_urls *du;
if ( d->show[sel_font]->open ) {
/* If an image is selected use it */
for ( du=d->show[sel_font]->urls; du!=NULL; du=du->next ) {
char *ext = strrchr(du->url,'.');
if ( ext==NULL || !du->selected)
continue;
if (
#ifndef _NO_LIBPNG
strcasecmp(ext,".png")==0 ||
#endif
#ifndef _NO_LIBJPEG
strcasecmp(ext,".jpeg")==0 || strcasecmp(ext,".jpg")==0 ||
#endif
#ifndef _NO_LIBTIFF
strcasecmp(ext,".tiff")==0 || strcasecmp(ext,".tif")==0 ||
#endif
#ifndef _NO_LIBUNGIF
strcasecmp(ext,".gif")==0 ||
#endif
strcasecmp(ext,".bmp")==0 )
return( du );
}
}
/* Otherwise use an unselected image (if present) */
for ( du=d->show[sel_font]->urls; du!=NULL; du=du->next ) {
char *ext = strrchr(du->url,'.');
if ( ext==NULL )
continue;
if (
#ifndef _NO_LIBPNG
strcasecmp(ext,".png")==0 ||
#endif
#ifndef _NO_LIBJPEG
strcasecmp(ext,".jpeg")==0 || strcasecmp(ext,".jpg")==0 ||
#endif
#ifndef _NO_LIBTIFF
strcasecmp(ext,".tiff")==0 || strcasecmp(ext,".tif")==0 ||
#endif
#ifndef _NO_LIBUNGIF
strcasecmp(ext,".gif")==0 ||
#endif
strcasecmp(ext,".bmp")==0 )
return( du );
}
return( NULL );
}
static struct ofl_download_urls *OFLibHasFont(OFLibDlg *d,int sel_font) {
struct ofl_download_urls *du;
struct ofl_download_urls *otf=NULL, *pf=NULL, *zip=NULL, *sfd=NULL;
for ( du=d->show[sel_font]->urls; du!=NULL; du=du->next ) {
char *ext = strrchr(du->url,'.');
if ( ext==NULL )
continue;
if ( strcasecmp(ext,".zip")==0 )
zip = du;
else if ( strcasecmp(ext,".pfa")==0 || strcasecmp(ext,".pfb")==0 ||
strcasecmp(ext,".cff")==0 )
pf = du;
else if ( strcasecmp(ext,".ttf")==0 || strcasecmp(ext,".otf")==0 ||
strcasecmp(ext,".ttc")==0 )
otf = du;
else if ( strcasecmp(ext,".sfd")==0 )
sfd = du;
}
if ( sfd!=NULL )
return( sfd );
else if ( otf!=NULL )
return( otf );
else if ( pf!=NULL )
return( pf );
else
return( zip );
}
static void OFLibSetSb(OFLibDlg *d) {
GGadget *sb = GWidgetGetControl(d->gw,CID_FontSB);
struct ofl_download_urls *du;
int i, tot;
/* Number of lines visible */
for ( i=tot=0; i<d->scnt; ++i ) {
++tot;
if ( d->show[i]->open ) {
for ( du=d->show[i]->urls; du!=NULL; du=du->next )
++tot;
}
}
d->ltot = tot;
d->lsize = (d->lheight-2*d->margin)/d->fh;
GScrollBarSetBounds(sb,0,d->ltot,d->lsize);
if ( d->l_off_top+d->lsize > d->ltot )
d->l_off_top = d->ltot-d->lsize;
if ( d->l_off_top < 0 )
d->l_off_top = 0;
GScrollBarSetPos(sb,d->l_off_top);
}
static int sortrevdate(const void *_op1, const void *_op2) {
const struct ofl_font_info *op1 = *(const struct ofl_font_info **) _op1;
const struct ofl_font_info *op2 = *(const struct ofl_font_info **) _op2;
return( op2->date - op1->date );
}
static int sortdate(const void *_op1, const void *_op2) {
const struct ofl_font_info *op1 = *(const struct ofl_font_info **) _op1;
const struct ofl_font_info *op2 = *(const struct ofl_font_info **) _op2;
return( op1->date - op2->date );
}
static int sortrevauthor(const void *_op1, const void *_op2) {
const struct ofl_font_info *op1 = *(const struct ofl_font_info **) _op1;
const struct ofl_font_info *op2 = *(const struct ofl_font_info **) _op2;
return( strcasecmp(op2->author, op1->author) );
}
static int sortauthor(const void *_op1, const void *_op2) {
const struct ofl_font_info *op1 = *(const struct ofl_font_info **) _op1;
const struct ofl_font_info *op2 = *(const struct ofl_font_info **) _op2;
return( strcasecmp(op1->author, op2->author) );
}
static int sortrevname(const void *_op1, const void *_op2) {
const struct ofl_font_info *op1 = *(const struct ofl_font_info **) _op1;
const struct ofl_font_info *op2 = *(const struct ofl_font_info **) _op2;
return( strcasecmp(op2->name, op1->name) );
}
static int sortname(const void *_op1, const void *_op2) {
const struct ofl_font_info *op1 = *(const struct ofl_font_info **) _op1;
const struct ofl_font_info *op2 = *(const struct ofl_font_info **) _op2;
return( strcasecmp(op1->name, op2->name) );
}
static void OFLibSortSearch(OFLibDlg *d) {
int reverse = GGadgetIsChecked( GWidgetGetControl(d->gw,CID_SortReverse));
enum searchtype st = (intpt) GGadgetGetListItemSelected(GWidgetGetControl(d->gw,CID_SearchType))->userdata;
char *search = GGadgetGetTitle8( GWidgetGetControl(d->gw,CID_SearchString));
int i, s;
int isofl = false, ispd = false;
char *end, *start, *found, ch;
if ( d->smax<d->all.fcnt )
d->show = grealloc(d->show,(d->smax = d->all.fcnt+20)*sizeof(struct ofl_font_info *));
if ( st==st_license ) {
isofl = strcasecmp(search,"ofl")==0;
ispd = strcasecmp(search,"pd")==0;
}
for ( i=s=0; i<d->all.fcnt ; ++i ) {
int include = false;
switch ( st ) {
case st_showall:
include = true;
break;
case st_author:
include = strstrmatch(d->all.fonts[i].author,search)!=NULL;
break;
case st_name:
include = strstrmatch(d->all.fonts[i].name,search)!=NULL;
break;
case st_license:
include = (isofl && d->all.fonts[i].license==ofll_ofl) ||
(ispd && d->all.fonts[i].license==ofll_pd);
break;
case st_tag: /* check that each tag is found */
start = search;
include = true;
while ( *start ) {
while ( *start==',' || isspace(*start)) ++start;
if ( *start=='\0' )
break;
for ( end=start; *end!=',' && *end!='\0'; ++end );
ch = *end; *end = '\0';
found = strstrmatch(d->all.fonts[i].taglist, start);
*end = ch;
if ( found==NULL ) {
include = false;
break;
} else if ( found==d->all.fonts[i].taglist ||
found[-1] == ',' ||
isspace(found[-1]) ) {
/* Looks good */
} else {
include = false;
break;
}
ch = found[ end-start ];
if ( ch=='\0' || ch==',' || isspace(ch)) {
/* Is good */
} else {
include = false;
break;
}
start = end;
}
break;
default:
include = false;
break;
}
if ( include )
d->show[s++] = &d->all.fonts[i];
}
d->scnt = s;
if ( s!=0 ) {
qsort(d->show,s,sizeof(d->show[0]),
GGadgetIsChecked( GWidgetGetControl(d->gw,CID_SortDate)) ?
(reverse ? sortrevdate : sortdate) :
GGadgetIsChecked( GWidgetGetControl(d->gw,CID_SortAuthor)) ?
(reverse ? sortrevauthor : sortauthor) :
(reverse ? sortrevname : sortname) );
}
OFLibSetSb(d);
GGadgetRedraw( GWidgetGetControl(d->gw,CID_Fonts));
}
static int OFL_SortBy(GGadget *g, GEvent *e) {
if ( e->type==et_controlevent && e->u.control.subtype == et_radiochanged ) {
OFLibDlg *d = GDrawGetUserData(GGadgetGetWindow(g));
OFLibSortSearch(d);
}
return( true );
}
static int OFL_SearchFor(GGadget *g, GEvent *e) {
if ( e->type==et_controlevent && e->u.control.subtype == et_listselected ) {
OFLibDlg *d = GDrawGetUserData(GGadgetGetWindow(g));
OFLibSortSearch(d);
}
return( true );
}
static int OFL_SearchStringChanged(GGadget *g, GEvent *e) {
if ( e->type==et_controlevent && e->u.control.subtype == et_textchanged ) {
OFLibDlg *d = GDrawGetUserData(GGadgetGetWindow(g));
OFLibSortSearch(d);
}
return( true );
}
static void PreviewThreadsKill(OFLibDlg *d) {
PreviewThread *next, *cur;
void *status;