Debian Codegraph @

gdraw/gresource.c

raw symbols SCIP
1 /* Copyright (C) 2000-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 "fontP.h"
31 #include "gdraw.h"
32 #include "gfile.h"
33 #include "gresedit.h"
34 #include "gresource.h"
35 #include "gresourceP.h"
36 #include "ustring.h"
37 #include "utype.h"
38
39 #include "assert.h"
40 #include "math.h"
41
42 char *GResourceProgramName;
43 char *usercharset_names;
44
45 static int rcur, rmax=0;
46 static int rbase = 0, rsummit=0, rskiplen=0; /* when restricting a search */
47 struct _GResource_Res *_GResource_Res;
48 extern GBox _ggadget_Default_Box;
49 extern GResFont _ggadget_default_font;
50
51 static int rcompar(const void *_r1,const void *_r2) {
52 const struct _GResource_Res *r1 = _r1, *r2 = _r2;
53 return( strcmp(r1->res,r2->res));
54 }
55
56 int _GResource_FindResName(const char *name, int do_restrict) {
57 int top=do_restrict?rsummit:rcur, bottom = do_restrict?rbase:0;
58 int test, cmp;
59
60 if ( rcur==0 )
61 return( -1 );
62
63 for (;;) {
64 if ( top==bottom )
65 return( -1 );
66 test = (top+bottom)/2;
67 cmp = strcmp(name,_GResource_Res[test].res+(do_restrict?rskiplen:0));
68 if ( cmp==0 )
69 return( test );
70 if ( test==bottom )
71 return( -1 );
72 if ( cmp>0 )
73 bottom=test+1;
74 else
75 top = test;
76 }
77 }
78
79 static int GResourceRestrict(const char *prefix) {
80 int top=rcur, bottom = 0;
81 int test, cmp;
82 int plen;
83 int oldtest, oldtop;
84 char *prestr;
85
86 if ( prefix==NULL || *prefix=='\0' ) {
87 rbase = rskiplen = 0; rsummit = rcur;
88 return( rcur==0?-1:0 );
89 }
90 if ( rcur==0 )
91 return( -1 );
92
93 prestr = smprintf("%s.", prefix);
94 plen = strlen(prestr);
95
96 for (;;) {
97 test = (top+bottom)/2;
98 cmp = strncmp(prestr,_GResource_Res[test].res,plen);
99 if ( cmp==0 )
100 break;
101 if ( test==bottom ) {
102 free(prestr);
103 return -1;
104 }
105 if ( cmp>0 ) {
106 bottom=test+1;
107 if ( bottom==top ) {
108 free(prestr);
109 return -1;
110 }
111 } else
112 top = test;
113 }
114 /* at this point the resource at test begins with the prestr */
115 /* we want to find the first and last resources that do */
116 oldtop = top; oldtest = top = test; /* find the first resource */
117 for (;;) {
118 test = (top+bottom)/2;
119 cmp = strncmp(prestr,_GResource_Res[test].res,plen);
120 if ( cmp<0 ) {
121 GDrawIError("Resource list out of order");
122 free(prestr);
123 return -1;
124 }
125 if ( test==bottom ) {
126 if ( cmp!=0 ) ++test;
127 break;
128 }
129 if ( cmp>0 ) {
130 bottom=++test;
131 if ( bottom==top )
132 break;
133 } else
134 top = test;
135 }
136 rbase = test;
137
138 top = oldtop; bottom = oldtest+1; /* find the last resource */
139 if ( bottom == top )
140 test = top;
141 else for (;;) {
142 test = (top+bottom)/2;
143 cmp = strncmp(prestr,_GResource_Res[test].res,plen);
144 if ( cmp>0 ) {
145 GDrawIError("Resource list out of order");
146 free(prestr);
147 return -1;
148 }
149 if ( test==bottom ) {
150 if ( cmp==0 ) ++test;
151 break;
152 }
153 if ( cmp==0 ) {
154 bottom=++test;
155 if ( bottom==top )
156 break;
157 } else
158 top = test;
159 }
160 rsummit = test;
161 rskiplen = plen;
162 free(prestr);
163 return( 0 );
164 }
165
166 void GResourceSetProg(const char *prog) {
167 const char *pt;
168
169 if ( prog!=NULL ) {
170 if ( GResourceProgramName!=NULL && strcmp(prog,GResourceProgramName)==0 )
171 return;
172 free(GResourceProgramName);
173 if (( pt=strrchr(prog,'/'))!=NULL )
174 ++pt;
175 else
176 pt = prog;
177 GResourceProgramName = copy(pt);
178 } else if ( GResourceProgramName==NULL )
179 GResourceProgramName = copy("gdraw");
180 else
181 return;
182 }
183
184 #if defined(__MINGW32__)
185 static char* replace_CJK_font_alias(const char* resource) {
186 /* On Windows systems the default UI font doesn't support CJK. We prepend it
187 with locale-specific font by replacing the predefined
188 "windows-cjk-workaround" alias. */
189 static const char alias[] = "windows-cjk-workaround";
190 char locale[100];
191 const char *ui_font = NULL;
192
193 gettext_locale(locale, sizeof(locale));
194 if (strcmp(locale, "zh_CN") == 0) ui_font = "Microsoft YaHei UI";
195 else if (strcmp(locale, "zh_SG") == 0) ui_font = "Microsoft YaHei UI";
196 else if (strcmp(locale, "zh_TW") == 0) ui_font = "Microsoft JhengHei UI";
197 else if (strcmp(locale, "zh_HK") == 0) ui_font = "Microsoft JhengHei UI";
198 else if (strcmp(locale, "ja_JP") == 0) ui_font = "Meiryo UI,Yu Gothic UI";
199 else if (strcmp(locale, "ja") == 0) ui_font = "Meiryo UI,Yu Gothic UI";
200 else if (strcmp(locale, "ko_KR") == 0) ui_font = "Malgun Gothic";
201 else if (strcmp(locale, "ko") == 0) ui_font = "Malgun Gothic";
202
203 if (ui_font == NULL) {
204 return NULL;
205 }
206
207 const char* pos = strstr(resource, alias);
208 if (pos == NULL) {
209 return NULL;
210 }
211
212 int prefix_len = pos - resource;
213 const char *suffix = pos + sizeof(alias) - 1;
214 char *result = smprintf("%.*s%s%s",
215 prefix_len, resource,
216 ui_font,
217 suffix);
218
219 return result;
220 }
221 #endif
222
223 void GResourceAddResourceString(const char *string,const char *prog) {
224 char *ept, *value=NULL;
225 const char *pt, *next;
226 int cnt, plen;
227 struct _GResource_Res temp;
228 int i,j,k, off;
229
230 GResourceSetProg(prog);
231 plen = strlen(GResourceProgramName);
232
233 if ( string==NULL )
234 return;
235
236 cnt = 0;
237 pt = string;
238 while ( *pt!='\0' ) {
239 next = strchr(pt,'\n');
240 if ( next==NULL ) next = pt+strlen(pt);
241 else ++next;
242 if ( strncmp(pt,"Gdraw.",6)==0 ) ++cnt;
243 else if ( strncmp(pt,GResourceProgramName,plen)==0 && pt[plen]=='.' ) ++cnt;
244 else if ( strncmp(pt,"*",1)==0 ) ++cnt;
245 pt = next;
246 }
247 if ( cnt==0 )
248 return;
249
250 if ( rcur+cnt>=rmax ) {
251 if ( cnt<10 ) cnt = 10;
252 if ( rmax==0 )
253 _GResource_Res = malloc(cnt*sizeof(struct _GResource_Res));
254 else
255 _GResource_Res = realloc(_GResource_Res,(rcur+cnt)*sizeof(struct _GResource_Res));
256 rmax += cnt;
257 }
258
259 pt = string;
260 while ( *pt!='\0' ) {
261 next = strchr(pt,'\n');
262 if ( next==NULL ) next = pt+strlen(pt);
263 if ( strncmp(pt,"Gdraw.",6)==0 || strncmp(pt,"*",1)==0 ||
264 (strncmp(pt,GResourceProgramName,plen)==0 && pt[plen]=='.' )) {
265 temp.generic = false;
266 if ( strncmp(pt,"Gdraw.",6)==0 ) {
267 temp.generic = true;
268 off = 6;
269 } else if ( strncmp(pt,"*",1)==0 ) {
270 temp.generic = true;
271 off = 1;
272 } else
273 off = plen+1;
274 ept = strchr(pt+off,':');
275 if ( ept==NULL )
276 goto bad;
277 temp.res = copyn(pt+off,ept-(pt+off));
278 pt = ept+1;
279 while ( isspace( *pt ) && pt<next ) ++pt;
280 value = copyn(pt,next-pt);
281 #if defined(__MINGW32__)
282 {
283 char* fixed_CJK = replace_CJK_font_alias(value);
284 if (fixed_CJK != NULL) {
285 free(value);
286 value = fixed_CJK;
287 }
288 }
289 #endif
290 temp.val = value;
291 temp.new = true;
292 _GResource_Res[rcur++] = temp;
293 }
294 bad:
295 if ( *next ) ++next;
296 pt = next;
297 }
298
299 if ( rcur!=0 )
300 qsort(_GResource_Res,rcur,sizeof(struct _GResource_Res),rcompar);
301
302 for ( i=j=0; i<rcur; ) {
303 if ( i!=j )
304 _GResource_Res[j] = _GResource_Res[i];
305 for ( k=i+1; k<rcur && strcmp(_GResource_Res[j].res,_GResource_Res[k].res)==0; ++k ) {
306 if (( !_GResource_Res[k].generic && (_GResource_Res[i].generic || _GResource_Res[i+1].new)) ||
307 (_GResource_Res[k].generic && _GResource_Res[i].generic && _GResource_Res[i+1].new)) {
308 free(_GResource_Res[j].res); free(_GResource_Res[j].val);
309 _GResource_Res[i].res=NULL;
310 _GResource_Res[j] = _GResource_Res[k];
311 } else {
312 free(_GResource_Res[k].res); free(_GResource_Res[k].val);
313 _GResource_Res[k].res=NULL;
314 }
315 }
316 i = k; ++j;
317 }
318 rcur = rsummit = j;
319 for ( i=0; i<j; ++i )
320 _GResource_Res[i].new = false;
321 }
322
323 void GResourceAddResourceFile(const char *filename,const char *prog,int warn) {
324 FILE *file;
325 char buffer[1000];
326
327 file = fopen(filename,"r");
328 if ( file==NULL ) {
329 if ( warn )
330 fprintf( stderr, "Failed to open resource file: %s\n", filename );
331 return;
332 }
333 while ( fgets(buffer,sizeof(buffer),file)!=NULL )
334 GResourceAddResourceString(buffer,prog);
335 fclose(file);
336 }
337
338 static void _GResourceFindFont(const char *resourcename, GResFont *font, int is_value);
339 static void _GResourceFindImage(const char *fname, GResImage *img);
340
341 void GResourceFind( GResStruct *info,const char *prefix) {
342 int pos;
343
344 if ( GResourceRestrict(prefix)== -1 ) {
345 rbase = rskiplen = 0; rsummit = rcur;
346 // return; // Even if no resources are loaded we still need to init
347 // the default images and fonts
348 }
349 while ( info->resname!=NULL ) {
350 pos = _GResource_FindResName(info->resname, true);
351 info->found = (pos!=-1);
352 if ( pos==-1 && info->type!=rt_font && info->type!=rt_image ) // rt_font and rt_image may need default initialization
353 /* Do Nothing */;
354 else if ( info->type == rt_string ) {
355 if ( info->cvt!=NULL )
356 *(void **) (info->val) = (info->cvt)( _GResource_Res[pos].val, *(void **) (info->val) );
357 else
358 *(char **) (info->val) = copy( _GResource_Res[pos].val );
359 } else if ( info->type == rt_font ) {
360 _GResourceFindFont(info->found ? _GResource_Res[pos].val : NULL, (GResFont *) info->val, true);
361 } else if ( info->type == rt_image ) {
362 _GResourceFindImage(info->found ? _GResource_Res[pos].val : NULL, (GResImage *) info->val);
363 } else if ( info->type == rt_color ) {
364 Color temp = _GImage_ColourFName(_GResource_Res[pos].val );
365 if ( temp==-1 ) {
366 fprintf( stderr, "Can't convert %s to a Color for resource: %s\n",
367 _GResource_Res[pos].val, info->resname );
368 info->found = false;
369 } else
370 *(Color *) (info->val) = temp;
371 } else if ( info->type == rt_int ) {
372 char *end;
373 int val = strtol(_GResource_Res[pos].val,&end,0);
374 if ( *end!='\0' ) {
375 fprintf( stderr, "Can't convert %s to an int for resource: %s\n",
376 _GResource_Res[pos].val, info->resname );
377 info->found = false;
378 } else
379 *(int *) (info->val) = val;
380 } else if ( info->type == rt_bool ) {
381 int val = -1;
382 if ( strmatch(_GResource_Res[pos].val,"true")==0 ||
383 strmatch(_GResource_Res[pos].val,"on")==0 || strcmp(_GResource_Res[pos].val,"1")==0 )
384 val = 1;
385 else if ( strmatch(_GResource_Res[pos].val,"false")==0 ||
386 strmatch(_GResource_Res[pos].val,"off")==0 || strcmp(_GResource_Res[pos].val,"0")==0 )
387 val = 0;
388 if ( val==-1 ) {
389 fprintf( stderr, "Can't convert %s to a boolean for resource: %s\n",
390 _GResource_Res[pos].val, info->resname );
391 info->found = false;
392 } else
393 *(int *) (info->val) = val;
394 } else if ( info->type == rt_double ) {
395 char *end;
396 double val = strtod(_GResource_Res[pos].val,&end);
397 if ( *end=='.' || *end==',' ) {
398 *end = (*end==',')?'.':',';
399 val = strtod(_GResource_Res[pos].val,&end);
400 }
401 if ( *end!='\0' ) {
402 fprintf( stderr, "Can't convert %s to a double for resource: %s\n",
403 _GResource_Res[pos].val, info->resname );
404 info->found = false;
405 } else
406 *(double *) (info->val) = val;
407 } else {
408 fprintf( stderr, "Invalid resource type for: %s\n", info->resname );
409 info->found = false;
410 }
411 ++info;
412 }
413 rbase = rskiplen = 0; rsummit = rcur;
414 }
415
416 char *GResourceFindString(const char *name) {
417 int pos;
418
419 pos = _GResource_FindResName(name, false);
420 if ( pos==-1 )
421 return( NULL );
422 else
423 return( copy(_GResource_Res[pos].val));
424 }
425
426 int GResourceFindBool(const char *name, int def) {
427 int pos;
428 int val = -1;
429
430 pos = _GResource_FindResName(name, false);
431 if ( pos==-1 )
432 return( def );
433
434 if ( strmatch(_GResource_Res[pos].val,"true")==0 ||
435 strmatch(_GResource_Res[pos].val,"on")==0 || strcmp(_GResource_Res[pos].val,"1")==0 )
436 val = 1;
437 else if ( strmatch(_GResource_Res[pos].val,"false")==0 ||
438 strmatch(_GResource_Res[pos].val,"off")==0 || strcmp(_GResource_Res[pos].val,"0")==0 )
439 val = 0;
440
441 if ( val==-1 )
442 return( def );
443
444 return( val );
445 }
446
447 long GResourceFindInt(const char *name, long def) {
448 int pos;
449 char *end;
450 long ret;
451
452 pos = _GResource_FindResName(name, false);
453 if ( pos==-1 )
454 return( def );
455
456 ret = strtol(_GResource_Res[pos].val,&end,10);
457 if ( *end!='\0' )
458 return( def );
459
460 return( ret );
461 }
462
463 Color GResourceFindColor(const char *name, Color def) {
464 int pos;
465 Color ret;
466
467 pos = _GResource_FindResName(name, false);
468 if ( pos==-1 )
469 return( def );
470
471 ret = _GImage_ColourFName(_GResource_Res[pos].val );
472 if ( ret==-1 )
473 return( def );
474
475 return( ret );
476 }
477
478 static int match(char **list, char *val) {
479 int i;
480
481 for ( i=0; list[i]!=NULL; ++i )
482 if ( strmatch(val,list[i])==0 )
483 return( i );
484
485 return( -1 );
486 }
487
488 static void *border_type_cvt(char *val, void *def) {
489 static char *types[] = { "none", "box", "raised", "lowered", "engraved",
490 "embossed", "double", NULL };
491 int ret = match(types,val);
492 if ( ret== -1 )
493 return( def );
494 return( (void *) (intptr_t) ret );
495 }
496
497 static void *border_shape_cvt(char *val, void *def) {
498 static char *shapes[] = { "rect", "roundrect", "elipse", "diamond", NULL };
499 int ret = match(shapes,val);
500 if ( ret== -1 )
501 return( def );
502 return( (void *) (intptr_t) ret );
503 }
504
505 void _GGadgetCopyDefaultBox(GBox *box) {
506 *box = _ggadget_Default_Box;
507 }
508
509 void _GGadgetInitDefaultBox(const char *class, GBox *box) {
510 GResStruct boxtypes[] = {
511 { "Box.BorderType", rt_string, NULL, border_type_cvt, 0 },
512 { "Box.BorderShape", rt_string, NULL, border_shape_cvt, 0 },
513 { "Box.BorderWidth", rt_int, NULL, NULL, 0 },
514 { "Box.Padding", rt_int, NULL, NULL, 0 },
515 { "Box.Radius", rt_int, NULL, NULL, 0 },
516 { "Box.BorderInner", rt_bool, NULL, NULL, 0 },
517 { "Box.BorderOuter", rt_bool, NULL, NULL, 0 },
518 { "Box.ActiveInner", rt_bool, NULL, NULL, 0 },
519 { "Box.DoDepressedBackground", rt_bool, NULL, NULL, 0 },
520 { "Box.DrawDefault", rt_bool, NULL, NULL, 0 },
521 { "Box.BorderBrightest", rt_color, NULL, NULL, 0 },
522 { "Box.BorderBrighter", rt_color, NULL, NULL, 0 },
523 { "Box.BorderDarkest", rt_color, NULL, NULL, 0 },
524 { "Box.BorderDarker", rt_color, NULL, NULL, 0 },
525 { "Box.NormalBackground", rt_color, NULL, NULL, 0 },
526 { "Box.NormalForeground", rt_color, NULL, NULL, 0 },
527 { "Box.DisabledBackground", rt_color, NULL, NULL, 0 },
528 { "Box.DisabledForeground", rt_color, NULL, NULL, 0 },
529 { "Box.ActiveBorder", rt_color, NULL, NULL, 0 },
530 { "Box.PressedBackground", rt_color, NULL, NULL, 0 },
531 { "Box.BorderLeft", rt_color, NULL, NULL, 0 },
532 { "Box.BorderTop", rt_color, NULL, NULL, 0 },
533 { "Box.BorderRight", rt_color, NULL, NULL, 0 },
534 { "Box.BorderBottom", rt_color, NULL, NULL, 0 },
535 { "Box.GradientBG", rt_bool, NULL, NULL, 0 },
536 { "Box.GradientStartCol", rt_color, NULL, NULL, 0 },
537 { "Box.ShadowOuter", rt_bool, NULL, NULL, 0 },
538 { "Box.BorderInnerCol", rt_color, NULL, NULL, 0 },
539 { "Box.BorderOuterCol", rt_color, NULL, NULL, 0 },
540 GRESSTRUCT_EMPTY
541 };
542 intptr_t bt, bs;
543 int bw, pad, rr, inner, outer, active, depressed, def, grad, shadow;
544 char *classstr = NULL;
545
546 bt = box->border_type;
547 bs = box->border_shape;
548 bw = box->border_width;
549 pad = box->padding;
550 rr = box->rr_radius;
551 inner = box->flags & box_foreground_border_inner;
552 outer = box->flags & box_foreground_border_outer;
553 active = box->flags & box_active_border_inner;
554 depressed = box->flags & box_do_depressed_background;
555 def = box->flags & box_draw_default;
556 grad = box->flags & box_gradient_bg;
557 shadow = box->flags & box_foreground_shadow_outer;
558
559 boxtypes[0].val = &bt;
560 boxtypes[1].val = &bs;
561 boxtypes[2].val = &bw;
562 boxtypes[3].val = &pad;
563 boxtypes[4].val = &rr;
564 boxtypes[5].val = &inner;
565 boxtypes[6].val = &outer;
566 boxtypes[7].val = &active;
567 boxtypes[8].val = &depressed;
568 boxtypes[9].val = &def;
569 boxtypes[10].val = &box->border_brightest;
570 boxtypes[11].val = &box->border_brighter;
571 boxtypes[12].val = &box->border_darkest;
572 boxtypes[13].val = &box->border_darker;
573 boxtypes[14].val = &box->main_background;
574 boxtypes[15].val = &box->main_foreground;
575 boxtypes[16].val = &box->disabled_background;
576 boxtypes[17].val = &box->disabled_foreground;
577 boxtypes[18].val = &box->active_border;
578 boxtypes[19].val = &box->depressed_background;
579 boxtypes[20].val = &box->border_brightest;
580 boxtypes[21].val = &box->border_brighter;
581 boxtypes[22].val = &box->border_darkest;
582 boxtypes[23].val = &box->border_darker;
583 boxtypes[24].val = &grad;
584 boxtypes[25].val = &box->gradient_bg_end;
585 boxtypes[26].val = &shadow;
586 boxtypes[27].val = &box->border_inner;
587 boxtypes[28].val = &box->border_outer;
588
589 if ( class!=NULL )
590 classstr = smprintf("%s.", class);
591
592 /* for a plain box, default to all borders being the same. they must change*/
593 /* explicitly */
594 if ( bt==bt_box || bt==bt_double )
595 box->border_brightest = box->border_brighter = box->border_darker = box->border_darkest;
596 GResourceFind( boxtypes, class);
597 free(classstr);
598
599 box->border_type = bt;
600 box->border_shape = bs;
601 box->border_width = bw;
602 box->padding = pad;
603 box->rr_radius = rr;
604 box->flags=0;
605 if ( inner )
606 box->flags |= box_foreground_border_inner;
607 if ( outer )
608 box->flags |= box_foreground_border_outer;
609 if ( active )
610 box->flags |= box_active_border_inner;
611 if ( depressed )
612 box->flags |= box_do_depressed_background;
613 if ( def )
614 box->flags |= box_draw_default;
615 if ( grad )
616 box->flags |= box_gradient_bg;
617 if ( shadow )
618 box->flags |= box_foreground_shadow_outer;
619 }
620
621 /* font name may be something like:
622 bold italic extended 12pt courier
623 400 10pt small-caps
624 family name comes at the end, size must have "pt" or "px" after it
625 */
626 static int _GResToFontRequest(const char *resname, FontRequest *rq, GHashTable *ht,
627 int name_is_value, int strict) {
628 static char *styles[] = { "normal", "italic", "oblique", "small-caps",
629 "bold", "light", "extended", "condensed", NULL };
630 char *val, *pt, *end, ch, *relname=NULL, *e;
631 int ret, top_level = false, percent = 100, adjust = 0;
632 int found_rel = false, found_style = false, found_weight = false, err = false;
633 FontRequest rel_rq;
634
635 if ( ht==NULL ) {
636 ht = g_hash_table_new_full(g_str_hash, g_str_equal, free, NULL);
637 top_level = true;
638 }
639
640 memset(rq,0,sizeof(*rq));
641 memset(&rel_rq,0,sizeof(rel_rq));
642 rq->weight = 400;
643
644 if ( name_is_value )
645 val = copy(resname);
646 else {
647 val = GResourceFindString(resname);
648 }
649
650 if ( val==NULL ) {
651 if ( top_level )
652 GDrawError("Missing font resource reference to '%s': cannot resolve", resname);
653 err = true;
654 }
655
656 for ( pt=val; !err && *pt && *pt!='"'; ) {
657 for ( end=pt; *end!=' ' && *end!='\0'; ++end );
658 ch = *end;
659 *end = '\0';
660 ret = match(styles,pt);
661 if ( ret==-1 && isdigit(*pt)) {
662 ret = strtol(pt,&e,10);
663 if ( strmatch(e,"pt")==0 )
664 rq->point_size = ret;
665 else if ( strmatch(e,"px")==0 )
666 rq->point_size = -ret;
667 else if ( strmatch(e,"%")==0 ) {
668 percent = ret;
669 if ( percent<0 )
670 percent = -percent;
671 } else if ( *e=='\0' ) {
672 found_weight = true;
673 rq->weight = ret;
674 } else {
675 *end = ch;
676 break;
677 }
678 } else if ( ret==-1 && (*pt=='+' || *pt=='-') ) {
679 adjust = strtol(pt,&e,10);
680 } else if ( ret==-1 && *pt=='^' ) {
681 relname = copy(pt+1);
682 if ( g_hash_table_contains(ht, relname) ) {
683 GDrawError("Circular font resource reference to '%s': cannot resolve", relname);
684 if ( strict ) {
685 err = true;
686 break;
687 } else
688 continue;
689 }
690 g_hash_table_add(ht, copy(relname));
691 if ( !_GResToFontRequest(relname, &rel_rq, ht, false, strict) ) {
692 err = true;
693 break;
694 }
695 found_rel = true;
696 } else if ( ret==-1 ) {
697 *end = ch;
698 break;
699 } else if ( ret==0 ) {
700 found_weight = true;
701 rq->weight = 400;
702 } else if ( ret==1 || ret==2 ) {
703 found_style = true;
704 rq->style |= fs_italic;
705 } else if ( ret==3 ) {
706 found_style = true;
707 rq->style |= fs_smallcaps;
708 } else if ( ret==4 ) {
709 found_weight = true;
710 rq->weight = 700;
711 } else if ( ret==5 ) {
712 found_weight = true;
713 rq->weight = 300;
714 } else if ( ret==6 ) {
715 found_style = true;
716 rq->style |= fs_extended;
717 } else {
718 found_style = true;
719 rq->style |= fs_condensed;
720 }
721 *end = ch;
722 pt = end;
723 while ( *pt==' ' ) ++pt;
724 }
725
726 if ( !err ) {
727 if ( *pt!='\0' )
728 rq->utf8_family_name = copy(pt);
729 if ( found_rel && rq->utf8_family_name==NULL && rel_rq.utf8_family_name!=NULL )
730 rq->utf8_family_name = copy(rel_rq.utf8_family_name);
731 if ( found_rel && !found_weight )
732 rq->weight = rel_rq.weight;
733 if ( found_rel && !found_style )
734 rq->style = rel_rq.style;
735 if ( rq->point_size==0 ) {
736 if ( found_rel ) {
737 if ( rel_rq.point_size==0 ) {
738 GDrawError("Point size not set for '%s': cannot calculate relative point size", relname);
739 if ( strict )
740 err = true;
741 } else {
742 float tmp = (float)percent * (float)rel_rq.point_size / 100.0;
743 rq->point_size = (int) ((tmp - floor(tmp) > 0.5) ? ceil(tmp) : floor(tmp));
744 if ( rq->point_size < 0 )
745 rq->point_size -= adjust;
746 else
747 rq->point_size += adjust;
748 }
749 } else if ( strict ) {
750 err = true;
751 }
752 }
753 }
754
755 if ( top_level )
756 g_hash_table_destroy(ht);
757
758 free((char *)rel_rq.utf8_family_name);
759 free(val);
760 free(relname);
761
762 if ( err ) {
763 free((char *)rq->utf8_family_name);
764 rq->utf8_family_name = NULL;
765 return false;
766 }
767
768 return true;
769 }
770
771 int ResStrToFontRequest(const char *resstr, FontRequest *rq) {
772 return _GResToFontRequest(resstr, rq, NULL, true, true);
773 }
774
775 static void _GResourceFindFont(const char *resourcename, GResFont *font, int is_value) {
776 FontRequest rq;
777 FontInstance *fi;
778 char *rstr;
779 memset(&rq, 0, sizeof(rq));
780 if ( is_value )
781 rstr = copy(resourcename);
782 else
783 rstr = GResourceFindString(resourcename);
784
785 if ( rstr!=NULL && ( font->rstr==NULL || strcmp(rstr, font->rstr)!=0 ) ) {
786 if ( ResStrToFontRequest(rstr, &rq) ) {
787 fi = GDrawInstanciateFont(NULL, &rq);
788 if ( fi!=NULL ) {
789 // The original resource string is likely a constant so just accept
790 // leaking in the rare double-set cases.
791 font->rstr = rstr;
792 font->fi = fi;
793 } else {
794 TRACE("Could not find font corresponding to resource %s '%s', using default\n", resourcename, rstr);
795 }
796 } else {
797 //TRACE("Could not convert font resource %s '%s', using default\n", resourcename, rstr);
798 }
799 }
800
801 if ( rstr!=font->rstr )
802 free(rstr);
803
804 if ( font->fi==NULL && font->rstr!=NULL ) {
805 #ifndef NDEBUG
806 int r =
807 #endif
808 ResStrToFontRequest(font->rstr, &rq);
809 assert( r );
810 font->fi = GDrawInstanciateFont(NULL, &rq);
811 free((char *) rq.utf8_family_name);
812 if ( font->fi==NULL ) {
813 TRACE("Could not find font corresponding to default '%s', using system default\n", font->rstr);
814 font->fi = _ggadget_default_font.fi;
815 }
816 }
817 }
818
819 void GResourceFindFont(const char *resourcename, const char *elemname, GResFont *font) {
820 char *rstr;
821 if ( resourcename!=NULL )
822 rstr = smprintf("%s.%s", resourcename, elemname);
823 else
824 rstr = (char *) elemname;
825 _GResourceFindFont(rstr, font, false);
826 if ( resourcename!=NULL )
827 free(rstr);
828 }
829
830 static void _GResourceFindImage(const char *fname, GResImage *img) {
831 GImageCacheBucket *t = NULL;
832
833 if ( fname!=NULL ) {
834 t = _GGadgetImageCache(fname, false);
835 if ( t==NULL || t->image==NULL ) {
836 TRACE("Could not find image corresponding to '%s', using default\n", fname);
837 } else {
838 img->bucket = t;
839 }
840 }
841
842 if ( GResImageGetImage(img)==NULL && img->ini_name!=NULL ) {
843 img->bucket = _GGadgetImageCache(img->ini_name, true);
844 if ( GResImageGetImage(img)==NULL ) {
845 TRACE("Could not find image corresponding to default name '%s'\n", img->ini_name);
846 }
847 }
848 }
849
850 GImage *GResImageGetImage(GResImage *ri) {
851 if ( ri==NULL || ri->bucket==NULL )
852 return NULL;
853
854 return ri->bucket->image;
855 }
856
857 void fix_CJK_UI_font(GResFont* font) {
858 #if defined(__MINGW32__)
859 char* fixed_res = replace_CJK_font_alias(font->rstr);
860 if (fixed_res == NULL)
861 return;
862
863 if (font->can_free_name)
864 free(font->rstr);
865
866 font->rstr = fixed_res;
867 font->can_free_name = true;
868 #endif
869 }