nix startup on theme fail, fixes for font init and titler color model
[goodguy/history.git] / cinelerra-5.0 / guicast / bcresources.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2009 Adam Williams <broadcast at earthling dot net>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  */
21
22 #include "bcdisplayinfo.h"
23 #include "bcipc.h"
24 #include "bclistbox.inc"
25 #include "bcfontentry.h"
26 #include "bcresources.h"
27 #include "bcsignals.h"
28 #include "bcsynchronous.h"
29 #include "bcwindowbase.h"
30 #include "colors.h"
31 #include "bccmodels.h"
32 #include "cstrdup.h"
33 #include "fonts.h"
34 #include "language.h"
35 #include "vframe.h"
36
37 #include <string.h>
38 #include <iconv.h>
39 #include <sys/ipc.h>
40 #include <sys/shm.h>
41 #include <X11/extensions/XShm.h>
42 #include <fontconfig/fontconfig.h>
43 #include <fontconfig/fcfreetype.h>
44 #include <unistd.h>
45
46
47
48
49
50 int BC_Resources::error = 0;
51
52 VFrame* BC_Resources::bg_image = 0;
53 VFrame* BC_Resources::menu_bg = 0;
54
55 int BC_Resources::locale_utf8 = 0;
56 int BC_Resources::little_endian = 0;
57 char BC_Resources::language[LEN_LANG] = {0};
58 char BC_Resources::region[LEN_LANG] = {0};
59 char BC_Resources::encoding[LEN_ENCOD] = {0};
60 const char *BC_Resources::wide_encoding = 0;
61 ArrayList<BC_FontEntry*> *BC_Resources::fontlist = 0;
62 const char *BC_Resources::fc_properties[] = { FC_SLANT, FC_WEIGHT, FC_WIDTH };
63 #define LEN_FCPROP (sizeof(BC_Resources::fc_properties) / sizeof(const char*))
64
65 static const char *def_small_font = "-*-helvetica-medium-r-normal-*-%d-*";  // 10
66 static const char *def_small_font2 = "-*-helvetica-medium-r-normal-*-%d-*"; // 11
67 static const char *def_medium_font = "-*-helvetica-bold-r-normal-*-%d-*";   // 14
68 static const char *def_medium_font2 = "-*-helvetica-bold-r-normal-*-%d-*";  // 14
69 static const char *def_large_font = "-*-helvetica-bold-r-normal-*-%d-*";    // 18
70 static const char *def_large_font2 = "-*-helvetica-bold-r-normal-*-%d-*";   // 20
71 static const char *def_big_font =
72   "-*-bitstream charter-bold-r-normal-*-*-0-%d-%d-p-0-iso8859-1"; // 160
73 static const char *def_big_font2 =
74   "-*-nimbus sans l-bold-r-normal-*-*-0-%d-%d-p-0-iso8859-1";     // 160
75 static const char *def_small_fontset =  "-*-helvetica-medium-r-normal-*-%d-*";// 10
76 static const char *def_medium_fontset = "-*-helvetica-bold-r-normal-*-%d-*";  // 14
77 static const char *def_large_fontset =  "-*-helvetica-bold-r-normal-*-%d-*";  // 18
78 static const char *def_big_fontset =    "-*-helvetica-bold-r-normal-*-%d-*";  // 24
79 static const char *def_small_font_xft = "Sans:pixelsize=%.4f";           // 10.6667
80 static const char *def_small_b_font_xft = "Sans:bold:pixelsize=%.4f";    // 10.6667
81 static const char *def_medium_font_xft = "Sans:pixelsize=%.4f";          // 13.3333
82 static const char *def_medium_b_font_xft = "Sans:bold:pixelsize=%.4f";   // 13.3333
83 static const char *def_large_font_xft = "Sans:pixelsize=%.4f";           // 21.3333
84 static const char *def_large_b_font_xft = "Sans:bold:pixelsize=%.4f";    // 21.3333
85 static const char *def_big_font_xft = "Sans:pixelsize=37.3333";          // 37.3333
86 static const char *def_big_b_font_xft = "Sans:bold:pixelsize=37.33333";  // 37.3333
87
88 #define default_font_xft2 "-microsoft-verdana-*-*-*-*-*-*-*-*-*-*-*-*"
89
90 const char* BC_Resources::small_font = 0;
91 const char* BC_Resources::small_font2 = 0;
92 const char* BC_Resources::medium_font = 0;
93 const char* BC_Resources::medium_font2 = 0;
94 const char* BC_Resources::large_font = 0;
95 const char* BC_Resources::large_font2 = 0;
96 const char* BC_Resources::big_font = 0;
97 const char* BC_Resources::big_font2 = 0;
98 const char* BC_Resources::small_fontset = 0;
99 const char* BC_Resources::medium_fontset = 0;
100 const char* BC_Resources::large_fontset = 0;
101 const char* BC_Resources::big_fontset = 0;
102 const char* BC_Resources::small_font_xft = 0;
103 const char* BC_Resources::small_font_xft2 = 0;
104 const char* BC_Resources::small_b_font_xft = 0;
105 const char* BC_Resources::medium_font_xft = 0;
106 const char* BC_Resources::medium_font_xft2 = 0;
107 const char* BC_Resources::medium_b_font_xft = 0;
108 const char* BC_Resources::large_font_xft = 0;
109 const char* BC_Resources::large_font_xft2 = 0;
110 const char* BC_Resources::large_b_font_xft = 0;
111 const char* BC_Resources::big_font_xft = 0;
112 const char* BC_Resources::big_font_xft2 = 0;
113 const char* BC_Resources::big_b_font_xft = 0;
114
115 #define def_font(v, s...) do { sprintf(string,def_##v,s); v = cstrdup(string); } while(0)
116 #define set_font(v, s) do { sprintf(string, "%s", s); v = cstrdup(string); } while(0)
117 #define iround(v) ((int)(v+0.5))
118 void BC_Resources::init_font_defs(double scale)
119 {
120         char string[BCTEXTLEN];
121         def_font(small_font,       iround(scale*11));
122         def_font(small_font2,      iround(scale*11));
123         def_font(medium_font,      iround(scale*14));
124         def_font(medium_font2,     iround(scale*14));
125         def_font(large_font,       iround(scale*18));
126         def_font(large_font2,      iround(scale*20));
127         def_font(big_font,         iround(scale*160), iround(scale*160));
128         def_font(big_font2,        iround(scale*160), iround(scale*160));
129         def_font(small_fontset,    iround(scale*10));
130         def_font(medium_fontset,   iround(scale*14));
131         def_font(large_fontset,    iround(scale*18));
132         def_font(big_fontset,      iround(scale*24));
133         def_font(small_font_xft,   (scale*10.6667));
134         def_font(small_b_font_xft, (scale*10.6667));
135         def_font(medium_font_xft,  (scale*13.3333));
136         def_font(medium_b_font_xft,(scale*13.3333));
137         def_font(large_font_xft,   (scale*21.3333));
138         def_font(large_b_font_xft, (scale*21.3333));
139         def_font(big_font_xft,     (scale*37.3333));
140         def_font(big_b_font_xft,   (scale*37.3333));
141
142         set_font(small_font_xft2,  default_font_xft2);
143         set_font(medium_font_xft2, default_font_xft2);
144         set_font(large_font_xft2,  default_font_xft2);
145         set_font(big_font_xft2,    default_font_xft2);
146 }
147
148 suffix_to_type_t BC_Resources::suffix_to_type[] =
149 {
150         { "m2v", ICON_FILM },
151         { "mov", ICON_FILM },
152         { "mp2", ICON_SOUND },
153         { "mp3", ICON_SOUND },
154         { "ac3", ICON_SOUND },
155         { "mpg", ICON_FILM },
156         { "vob", ICON_FILM },
157         { "ifo", ICON_FILM },
158         { "ts",  ICON_FILM },
159         { "vts", ICON_FILM },
160         { "wav", ICON_SOUND }
161 };
162
163 BC_Signals* BC_Resources::signal_handler = 0;
164 Mutex BC_Resources::fontconfig_lock("BC_Resources::fonconfig_lock");
165
166 int BC_Resources::x_error_handler(Display *display, XErrorEvent *event)
167 {
168 #if defined(OUTPUT_X_ERROR)
169         char string[1024];
170         XGetErrorText(event->display, event->error_code, string, 1024);
171         fprintf(stderr, "BC_Resources::x_error_handler: error_code=%d opcode=%d,%d %s\n",
172                 event->error_code,
173                 event->request_code,
174                 event->minor_code,
175                 string);
176 #endif
177
178         BC_Resources::error = 1;
179 // This bug only happens in 32 bit mode.
180 //      if(sizeof(long) == 4)
181 //              BC_WindowBase::get_resources()->use_xft = 0;
182         return 0;
183 }
184
185 int BC_Resources::machine_cpus = 1;
186
187 int BC_Resources::get_machine_cpus()
188 {
189         int cpus = 1;
190         FILE *proc = fopen("/proc/cpuinfo", "r");
191         if( proc ) {
192                 char string[BCTEXTLEN], *cp;
193                 while(!feof(proc) && fgets(string, sizeof(string), proc) ) {
194                         if( !strncasecmp(string, "processor", 9) &&
195                             (cp = strchr(string, ':')) != 0 ) {
196                                 int n = atol(cp+1) + 1;
197                                 if( n > cpus ) cpus = n;
198                         }
199                         else if( !strncasecmp(string, "cpus detected", 13) &&
200                             (cp = strchr(string, ':')) != 0 )
201                                 cpus = atol(cp+1);
202                 }
203                 fclose(proc);
204         }
205         return cpus;
206 }
207
208 BC_Resources::BC_Resources()
209 {
210         synchronous = 0;
211         vframe_shm = 0;
212         display_info = new BC_DisplayInfo("", 0);
213         double scale = 1;
214         char *font_scale = getenv("BC_FONT_SCALE");
215         if( !font_scale ) {
216                 int display_w = display_info->get_root_w();
217                 int display_h = display_info->get_root_h();
218                 int display_size = display_h < display_w ? display_h : display_w;
219                 scale = display_size / 1000.;
220         }
221         else {
222                 double env_scale = atof(font_scale);
223                 if( env_scale > 0 ) scale = env_scale;
224         }
225         init_font_defs(scale);
226         id_lock = new Mutex("BC_Resources::id_lock");
227         create_window_lock = new Mutex("BC_Resources::create_window_lock", 1);
228         id = 0;
229         machine_cpus = get_machine_cpus();
230
231         for(int i = 0; i < FILEBOX_HISTORY_SIZE; i++)
232                 filebox_history[i].path[0] = 0;
233
234 #ifdef HAVE_XFT
235         XftInitFtLibrary();
236 #endif
237
238         little_endian = (*(const u_int32_t*)"\01\0\0\0") & 1;
239         wide_encoding = little_endian ?  "UTF32LE" : "UTF32BE";
240         use_xvideo = 1;
241
242 #include "images/file_film_png.h"
243 #include "images/file_folder_png.h"
244 #include "images/file_sound_png.h"
245 #include "images/file_unknown_png.h"
246 #include "images/file_column_png.h"
247         static VFrame* default_type_to_icon[] =
248         {
249                 new VFrame(file_folder_png),
250                 new VFrame(file_unknown_png),
251                 new VFrame(file_film_png),
252                 new VFrame(file_sound_png),
253                 new VFrame(file_column_png)
254         };
255         type_to_icon = default_type_to_icon;
256
257
258 #include "images/bar_png.h"
259         static VFrame* default_bar = new VFrame(bar_png);
260         bar_data = default_bar;
261
262
263 #include "images/cancel_up_png.h"
264 #include "images/cancel_hi_png.h"
265 #include "images/cancel_dn_png.h"
266         static VFrame* default_cancel_images[] =
267         {
268                 new VFrame(cancel_up_png),
269                 new VFrame(cancel_hi_png),
270                 new VFrame(cancel_dn_png)
271         };
272
273 #include "images/ok_up_png.h"
274 #include "images/ok_hi_png.h"
275 #include "images/ok_dn_png.h"
276         static VFrame* default_ok_images[] =
277         {
278                 new VFrame(ok_up_png),
279                 new VFrame(ok_hi_png),
280                 new VFrame(ok_dn_png)
281         };
282
283 #include "images/usethis_up_png.h"
284 #include "images/usethis_uphi_png.h"
285 #include "images/usethis_dn_png.h"
286         static VFrame* default_usethis_images[] =
287         {
288                 new VFrame(usethis_up_png),
289                 new VFrame(usethis_uphi_png),
290                 new VFrame(usethis_dn_png)
291         };
292
293 #if 0
294 #include "images/checkbox_checked_png.h"
295 #include "images/checkbox_dn_png.h"
296 #include "images/checkbox_checkedhi_png.h"
297 #include "images/checkbox_up_png.h"
298 #include "images/checkbox_hi_png.h"
299         static VFrame* default_checkbox_images[] =
300         {
301                 new VFrame(checkbox_up_png),
302                 new VFrame(checkbox_hi_png),
303                 new VFrame(checkbox_checked_png),
304                 new VFrame(checkbox_dn_png),
305                 new VFrame(checkbox_checkedhi_png)
306         };
307
308 #include "images/radial_checked_png.h"
309 #include "images/radial_dn_png.h"
310 #include "images/radial_checkedhi_png.h"
311 #include "images/radial_up_png.h"
312 #include "images/radial_hi_png.h"
313         static VFrame* default_radial_images[] =
314         {
315                 new VFrame(radial_up_png),
316                 new VFrame(radial_hi_png),
317                 new VFrame(radial_checked_png),
318                 new VFrame(radial_dn_png),
319                 new VFrame(radial_checkedhi_png)
320         };
321
322         static VFrame* default_label_images[] =
323         {
324                 new VFrame(radial_up_png),
325                 new VFrame(radial_hi_png),
326                 new VFrame(radial_checked_png),
327                 new VFrame(radial_dn_png),
328                 new VFrame(radial_checkedhi_png)
329         };
330 #endif
331
332 #include "images/file_text_up_png.h"
333 #include "images/file_text_hi_png.h"
334 #include "images/file_text_dn_png.h"
335 #include "images/file_icons_up_png.h"
336 #include "images/file_icons_hi_png.h"
337 #include "images/file_icons_dn_png.h"
338 #include "images/file_newfolder_up_png.h"
339 #include "images/file_newfolder_hi_png.h"
340 #include "images/file_newfolder_dn_png.h"
341 #include "images/file_rename_up_png.h"
342 #include "images/file_rename_hi_png.h"
343 #include "images/file_rename_dn_png.h"
344 #include "images/file_updir_up_png.h"
345 #include "images/file_updir_hi_png.h"
346 #include "images/file_updir_dn_png.h"
347 #include "images/file_delete_up_png.h"
348 #include "images/file_delete_hi_png.h"
349 #include "images/file_delete_dn_png.h"
350 #include "images/file_reload_up_png.h"
351 #include "images/file_reload_hi_png.h"
352 #include "images/file_reload_dn_png.h"
353         static VFrame* default_filebox_text_images[] =
354         {
355                 new VFrame(file_text_up_png),
356                 new VFrame(file_text_hi_png),
357                 new VFrame(file_text_dn_png)
358         };
359
360         static VFrame* default_filebox_icons_images[] =
361         {
362                 new VFrame(file_icons_up_png),
363                 new VFrame(file_icons_hi_png),
364                 new VFrame(file_icons_dn_png)
365         };
366
367         static VFrame* default_filebox_updir_images[] =
368         {
369                 new VFrame(file_updir_up_png),
370                 new VFrame(file_updir_hi_png),
371                 new VFrame(file_updir_dn_png)
372         };
373
374         static VFrame* default_filebox_newfolder_images[] =
375         {
376                 new VFrame(file_newfolder_up_png),
377                 new VFrame(file_newfolder_hi_png),
378                 new VFrame(file_newfolder_dn_png)
379         };
380
381
382         static VFrame* default_filebox_rename_images[] =
383         {
384                 new VFrame(file_rename_up_png),
385                 new VFrame(file_rename_hi_png),
386                 new VFrame(file_rename_dn_png)
387         };
388
389         static VFrame* default_filebox_delete_images[] =
390         {
391                 new VFrame(file_delete_up_png),
392                 new VFrame(file_delete_hi_png),
393                 new VFrame(file_delete_dn_png)
394         };
395
396         static VFrame* default_filebox_reload_images[] =
397         {
398                 new VFrame(file_reload_up_png),
399                 new VFrame(file_reload_hi_png),
400                 new VFrame(file_reload_dn_png)
401         };
402
403 #include "images/listbox_button_dn_png.h"
404 #include "images/listbox_button_hi_png.h"
405 #include "images/listbox_button_up_png.h"
406 #include "images/listbox_button_disabled_png.h"
407         static VFrame* default_listbox_button[] =
408         {
409                 new VFrame(listbox_button_up_png),
410                 new VFrame(listbox_button_hi_png),
411                 new VFrame(listbox_button_dn_png),
412                 new VFrame(listbox_button_disabled_png)
413         };
414         listbox_button = default_listbox_button;
415
416 #include "images/menu_popup_bg_png.h"
417         static VFrame* default_listbox_bg = 0;
418         listbox_bg = default_listbox_bg;
419
420 #include "images/listbox_expandchecked_png.h"
421 #include "images/listbox_expandcheckedhi_png.h"
422 #include "images/listbox_expanddn_png.h"
423 #include "images/listbox_expandup_png.h"
424 #include "images/listbox_expanduphi_png.h"
425         static VFrame* default_listbox_expand[] =
426         {
427                 new VFrame(listbox_expandup_png),
428                 new VFrame(listbox_expanduphi_png),
429                 new VFrame(listbox_expandchecked_png),
430                 new VFrame(listbox_expanddn_png),
431                 new VFrame(listbox_expandcheckedhi_png),
432         };
433         listbox_expand = default_listbox_expand;
434
435 #include "images/listbox_columnup_png.h"
436 #include "images/listbox_columnhi_png.h"
437 #include "images/listbox_columndn_png.h"
438         static VFrame* default_listbox_column[] =
439         {
440                 new VFrame(listbox_columnup_png),
441                 new VFrame(listbox_columnhi_png),
442                 new VFrame(listbox_columndn_png)
443         };
444         listbox_column = default_listbox_column;
445
446
447 #include "images/listbox_up_png.h"
448 #include "images/listbox_dn_png.h"
449         listbox_up = new VFrame(listbox_up_png);
450         listbox_dn = new VFrame(listbox_dn_png);
451         listbox_title_overlap = 0;
452         listbox_title_margin = 0;
453         listbox_title_color = BLACK;
454         listbox_title_hotspot = 5;
455
456         listbox_border1 = DKGREY;
457         listbox_border2_hi = RED;
458         listbox_border2 = BLACK;
459         listbox_border3_hi = RED;
460         listbox_border3 = MEGREY;
461         listbox_border4 = WHITE;
462         listbox_selected = BLUE;
463         listbox_highlighted = LTGREY;
464         listbox_inactive = WHITE;
465         listbox_text = BLACK;
466
467 #include "images/pot_hi_png.h"
468 #include "images/pot_up_png.h"
469 #include "images/pot_dn_png.h"
470         static VFrame *default_pot_images[] =
471         {
472                 new VFrame(pot_up_png),
473                 new VFrame(pot_hi_png),
474                 new VFrame(pot_dn_png)
475         };
476
477 #include "images/progress_up_png.h"
478 #include "images/progress_hi_png.h"
479         static VFrame* default_progress_images[] =
480         {
481                 new VFrame(progress_up_png),
482                 new VFrame(progress_hi_png)
483         };
484
485         pan_data = 0;
486         pan_text_color = YELLOW;
487
488 #include "images/7seg_small/0_png.h"
489 #include "images/7seg_small/1_png.h"
490 #include "images/7seg_small/2_png.h"
491 #include "images/7seg_small/3_png.h"
492 #include "images/7seg_small/4_png.h"
493 #include "images/7seg_small/5_png.h"
494 #include "images/7seg_small/6_png.h"
495 #include "images/7seg_small/7_png.h"
496 #include "images/7seg_small/8_png.h"
497 #include "images/7seg_small/9_png.h"
498 #include "images/7seg_small/colon_png.h"
499 #include "images/7seg_small/period_png.h"
500 #include "images/7seg_small/a_png.h"
501 #include "images/7seg_small/b_png.h"
502 #include "images/7seg_small/c_png.h"
503 #include "images/7seg_small/d_png.h"
504 #include "images/7seg_small/e_png.h"
505 #include "images/7seg_small/f_png.h"
506 #include "images/7seg_small/space_png.h"
507 #include "images/7seg_small/dash_png.h"
508         static VFrame* default_medium_7segment[] =
509         {
510                 new VFrame(_0_png),
511                 new VFrame(_1_png),
512                 new VFrame(_2_png),
513                 new VFrame(_3_png),
514                 new VFrame(_4_png),
515                 new VFrame(_5_png),
516                 new VFrame(_6_png),
517                 new VFrame(_7_png),
518                 new VFrame(_8_png),
519                 new VFrame(_9_png),
520                 new VFrame(colon_png),
521                 new VFrame(period_png),
522                 new VFrame(a_png),
523                 new VFrame(b_png),
524                 new VFrame(c_png),
525                 new VFrame(d_png),
526                 new VFrame(e_png),
527                 new VFrame(f_png),
528                 new VFrame(space_png),
529                 new VFrame(dash_png)
530         };
531
532         generic_button_margin = 15;
533         draw_clock_background=1;
534
535         use_shm = -1;
536         shm_reply = 1;
537
538 // Initialize
539         bg_color = ORANGE;
540         bg_shadow1 = DKGREY;
541         bg_shadow2 = BLACK;
542         bg_light1 = WHITE;
543         bg_light2 = bg_color;
544
545
546         border_light1 = bg_color;
547         border_light2 = MEGREY;
548         border_shadow1 = BLACK;
549         border_shadow2 = bg_color;
550
551         default_text_color = BLACK;
552         disabled_text_color = DMGREY;
553
554         button_light = MEGREY;           // bright corner
555 //      button_highlighted = LTGREY;  // face when highlighted
556         button_highlighted = 0xffe000;  // face when highlighted
557         button_down = MDGREY;         // face when down
558 //      button_up = MEGREY;           // face when up
559         button_up = 0xffc000;           // face when up
560         button_shadow = BLACK;       // dark corner
561         button_uphighlighted = RED;   // upper side when highlighted
562
563         tumble_data = 0;
564         tumble_duration = 150;
565
566         ok_images = default_ok_images;
567         cancel_images = default_cancel_images;
568         usethis_button_images = default_usethis_images;
569         filebox_descend_images = default_ok_images;
570
571         menu_light = LTCYAN;
572         menu_highlighted = LTBLUE;
573         menu_down = MDCYAN;
574         menu_up = MECYAN;
575         menu_shadow = DKCYAN;
576
577
578 #include "images/menuitem_up_png.h"
579 #include "images/menuitem_hi_png.h"
580 #include "images/menuitem_dn_png.h"
581 #include "images/menubar_up_png.h"
582 #include "images/menubar_hi_png.h"
583 #include "images/menubar_dn_png.h"
584 #include "images/menubar_bg_png.h"
585
586         static VFrame *default_menuitem_data[] =
587         {
588                 new VFrame(menuitem_up_png),
589                 new VFrame(menuitem_hi_png),
590                 new VFrame(menuitem_dn_png),
591         };
592         menu_item_bg = default_menuitem_data;
593
594
595         static VFrame *default_menubar_data[] =
596         {
597                 new VFrame(menubar_up_png),
598                 new VFrame(menubar_hi_png),
599                 new VFrame(menubar_dn_png),
600         };
601         menu_title_bg = default_menubar_data;
602
603         menu_popup_bg = new VFrame(menu_popup_bg_png);
604
605         menu_bar_bg = new VFrame(menubar_bg_png);
606
607         popupmenu_images = 0;
608
609
610         popupmenu_margin = 10;
611         popupmenu_triangle_margin = 10;
612
613         min_menu_w = 0;
614         menu_title_text = BLACK;
615         popup_title_text = BLACK;
616         menu_item_text = BLACK;
617         menu_highlighted_fontcolor = BLACK;
618         progress_text = BLACK;
619
620
621
622         text_default = BLACK;
623         highlight_inverse = WHITE ^ BLUE;
624         text_background = WHITE;
625         text_background_hi = LTYELLOW;
626         text_background_noborder_hi = LTGREY;
627         text_background_noborder = -1;
628         text_border1 = DKGREY;
629         text_border2 = BLACK;
630         text_border2_hi = RED;
631         text_border3 = MEGREY;
632         text_border3_hi = LTPINK;
633         text_border4 = WHITE;
634         text_highlight = BLUE;
635         text_inactive_highlight = MEGREY;
636
637         toggle_highlight_bg = 0;
638         toggle_text_margin = 0;
639
640 // Delays must all be different for repeaters
641         double_click = 300;
642         blink_rate = 250;
643         scroll_repeat = 150;
644         tooltip_delay = 1000;
645         tooltip_bg_color = YELLOW;
646         tooltips_enabled = 1;
647
648         filebox_margin = 110;
649         dirbox_margin = 90;
650         filebox_mode = LISTBOX_TEXT;
651         sprintf(filebox_filter, "*");
652         filebox_w = 640;
653         filebox_h = 480;
654         filebox_columntype[0] = FILEBOX_NAME;
655         filebox_columntype[1] = FILEBOX_SIZE;
656         filebox_columntype[2] = FILEBOX_DATE;
657         filebox_columntype[3] = FILEBOX_EXTENSION;
658         filebox_columnwidth[0] = 200;
659         filebox_columnwidth[1] = 100;
660         filebox_columnwidth[2] = 100;
661         filebox_columnwidth[3] = 100;
662         dirbox_columntype[0] = FILEBOX_NAME;
663         dirbox_columntype[1] = FILEBOX_DATE;
664         dirbox_columnwidth[0] = 200;
665         dirbox_columnwidth[1] = 100;
666
667         filebox_text_images = default_filebox_text_images;
668         filebox_icons_images = default_filebox_icons_images;
669         filebox_updir_images = default_filebox_updir_images;
670         filebox_newfolder_images = default_filebox_newfolder_images;
671         filebox_rename_images = default_filebox_rename_images;
672         filebox_delete_images = default_filebox_delete_images;
673         filebox_reload_images = default_filebox_reload_images;
674         directory_color = BLUE;
675         file_color = BLACK;
676
677         filebox_sortcolumn = 0;
678         filebox_sortorder = BC_ListBox::SORT_ASCENDING;
679         dirbox_sortcolumn = 0;
680         dirbox_sortorder = BC_ListBox::SORT_ASCENDING;
681
682
683         pot_images = default_pot_images;
684         pot_offset = 2;
685         pot_x1 = pot_images[0]->get_w() / 2 - pot_offset;
686         pot_y1 = pot_images[0]->get_h() / 2 - pot_offset;
687         pot_r = pot_x1;
688         pot_needle_color = BLACK;
689
690         progress_images = default_progress_images;
691
692         xmeter_images = 0;
693         ymeter_images = 0;
694         meter_font = SMALLFONT_3D;
695         meter_font_color = RED;
696         meter_title_w = 20;
697         meter_3d = 1;
698         medium_7segment = default_medium_7segment;
699
700         audiovideo_color = RED;
701
702         use_fontset = 0;
703
704 // Xft has priority over font set
705 #ifdef HAVE_XFT
706 // But Xft dies in 32 bit mode after some amount of drawing.
707         use_xft = 1;
708 #else
709         use_xft = 0;
710 #endif
711
712
713         drag_radius = 10;
714         recursive_resizing = 1;
715
716
717 }
718
719 BC_Resources::~BC_Resources()
720 {
721 }
722
723 int BC_Resources::initialize_display(BC_WindowBase *window)
724 {
725 // Set up IPC cleanup handlers
726 //      bc_init_ipc();
727
728 // Test for shm.  Must come before yuv test
729         init_shm(window);
730         return 0;
731 }
732
733
734 void BC_Resources::init_shm(BC_WindowBase *window)
735 {
736         use_shm = 0;
737         int (*old_handler)(Display *, XErrorEvent *) =
738                 XSetErrorHandler(BC_Resources::x_error_handler);
739
740         if(XShmQueryExtension(window->display))
741         {
742                 XShmSegmentInfo test_shm;
743                 memset(&test_shm,0,sizeof(test_shm));
744                 XImage *test_image = XShmCreateImage(window->display, window->vis,
745                         window->default_depth, ZPixmap, (char*)NULL, &test_shm, 5, 5);
746                 BC_Resources::error = 0;
747                 test_shm.shmid = shmget(IPC_PRIVATE, 5 * test_image->bytes_per_line, (IPC_CREAT | 0600));
748                 if(test_shm.shmid != -1) {
749                         char *data = (char *)shmat(test_shm.shmid, NULL, 0);
750                         if(data != (void *)-1) {
751                                 shmctl(test_shm.shmid, IPC_RMID, 0);
752                                 test_shm.shmaddr = data;
753                                 test_shm.readOnly = 0;
754
755                                 if(XShmAttach(window->display, &test_shm)) {
756                                         XSync(window->display, False);
757                                         use_shm = 1;
758                                 }
759                                 shmdt(data);
760                         }
761                 }
762
763                 XDestroyImage(test_image);
764                 if(BC_Resources::error) use_shm = 0;
765         }
766         XSetErrorHandler(old_handler);
767 }
768
769
770
771
772 BC_Synchronous* BC_Resources::get_synchronous()
773 {
774         return synchronous;
775 }
776
777 void BC_Resources::set_synchronous(BC_Synchronous *synchronous)
778 {
779         this->synchronous = synchronous;
780 }
781
782
783
784
785
786
787
788 int BC_Resources::get_top_border()
789 {
790         return display_info->get_top_border();
791 }
792
793 int BC_Resources::get_left_border()
794 {
795         return display_info->get_left_border();
796 }
797
798 int BC_Resources::get_right_border()
799 {
800         return display_info->get_right_border();
801 }
802
803 int BC_Resources::get_bottom_border()
804 {
805         return display_info->get_bottom_border();
806 }
807
808
809 int BC_Resources::get_bg_color() { return bg_color; }
810
811 int BC_Resources::get_bg_shadow1() { return bg_shadow1; }
812
813 int BC_Resources::get_bg_shadow2() { return bg_shadow2; }
814
815 int BC_Resources::get_bg_light1() { return bg_light1; }
816
817 int BC_Resources::get_bg_light2() { return bg_light2; }
818
819
820 int BC_Resources::get_id()
821 {
822         id_lock->lock("BC_Resources::get_id");
823         int result = id++;
824         id_lock->unlock();
825         return result;
826 }
827
828 int BC_Resources::get_filebox_id()
829 {
830         id_lock->lock("BC_Resources::get_filebox_id");
831         int result = filebox_id++;
832         id_lock->unlock();
833         return result;
834 }
835
836
837 void BC_Resources::set_signals(BC_Signals *signal_handler)
838 {
839         BC_Resources::signal_handler = signal_handler;
840 }
841
842 int BC_Resources::init_fontconfig(const char *search_path)
843 {
844         if( fontlist ) return 0;
845         fontlist = new ArrayList<BC_FontEntry*>;
846
847 #define get_str(str,sep,ptr,cond) do { char *out = str; \
848   while( *ptr && !strchr(sep,*ptr) && (cond) ) *out++ = *ptr++; \
849   *out = 0; \
850 } while(0)
851
852 #define skip_str(sep, ptr) do { \
853   while( *ptr && strchr(sep,*ptr) ) ++ptr; \
854 } while(0)
855
856         char find_command[BCTEXTLEN];
857         sprintf(find_command,
858                 "find %s -name 'fonts.dir' -print -exec cat {} \\;",
859                 search_path);
860         FILE *in = popen(find_command, "r");
861
862         FT_Library freetype_library = 0;
863 //      FT_Face freetype_face = 0;
864 //      FT_Init_FreeType(&freetype_library);
865
866         char line[BCTEXTLEN], current_dir[BCTEXTLEN];
867         current_dir[0] = 0;
868
869         while( !feof(in) && fgets(line, BCTEXTLEN, in) ) {
870                 if(!strlen(line)) break;
871
872                 char *in_ptr = line;
873
874 // Get current directory
875                 if(line[0] == '/') {
876                         get_str(current_dir, "\n", in_ptr,1);
877                         for( int i=strlen(current_dir); --i>=0 && current_dir[i]!='/'; )
878                                 current_dir[i] = 0;
879                         continue;
880                 }
881
882 //printf("TitleMain::build_fonts %s\n", line);
883                 BC_FontEntry *entry = new BC_FontEntry;
884                 char string[BCTEXTLEN];
885 // Path
886                 get_str(string, "\n", in_ptr, in_ptr[0]!=' ' || in_ptr[1]!='-');
887                 entry->path = cstrcat(2, current_dir, string);
888 // Foundary
889                 skip_str(" -", in_ptr);
890                 get_str(string, " -\n", in_ptr, 1);
891                 if( !string[0] ) { delete entry;  continue; }
892                 entry->foundry = cstrdup(string);
893                 if(*in_ptr == '-') in_ptr++;
894 // Family
895                 get_str(string, "-\n", in_ptr, 1);
896                 if( !string[0] ) { delete entry;  continue; }
897                 entry->family = cstrdup(string);
898                 if(*in_ptr == '-') in_ptr++;
899 // Weight
900                 get_str(string, "-\n", in_ptr, 1);
901                 entry->weight = cstrdup(string);
902                 if(*in_ptr == '-') in_ptr++;
903 // Slant
904                 get_str(string, "-\n", in_ptr, 1);
905                 entry->slant = cstrdup(string);
906                 if(*in_ptr == '-') in_ptr++;
907 // SWidth
908                 get_str(string, "-\n", in_ptr, 1);
909                 entry->swidth = cstrdup(string);
910                 if(*in_ptr == '-') in_ptr++;
911 // Adstyle
912                 get_str(string, "-\n", in_ptr, 1);
913                 entry->adstyle = cstrdup(string);
914                 if(*in_ptr == '-') in_ptr++;
915 // pixelsize
916                 get_str(string, "-\n", in_ptr, 1);
917                 entry->pixelsize = atol(string);
918                 if(*in_ptr == '-') in_ptr++;
919 // pointsize
920                 get_str(string, "-\n", in_ptr, 1);
921                 entry->pointsize = atol(string);
922                 if(*in_ptr == '-') in_ptr++;
923 // xres
924                 get_str(string, "-\n", in_ptr, 1);
925                 entry->xres = atol(string);
926                 if(*in_ptr == '-') in_ptr++;
927 // yres
928                 get_str(string, "-\n", in_ptr, 1);
929                 entry->yres = atol(string);
930                 if(*in_ptr == '-') in_ptr++;
931 // spacing
932                 get_str(string, "-\n", in_ptr, 1);
933                 entry->spacing = cstrdup(string);
934                 if(*in_ptr == '-') in_ptr++;
935 // avg_width
936                 get_str(string, "-\n", in_ptr, 1);
937                 entry->avg_width = atol(string);
938                 if(*in_ptr == '-') in_ptr++;
939 // registry
940                 get_str(string, "-\n", in_ptr, 1);
941                 entry->registry = cstrdup(string);
942                 if(*in_ptr == '-') in_ptr++;
943 // encoding
944                 get_str(string, "-\n", in_ptr, 1);
945                 entry->encoding = cstrdup(string);
946                 if(*in_ptr == '-') in_ptr++;
947
948 // Add to list
949 //printf("TitleMain::build_fonts 1 %s\n", entry->path);
950 // This takes a real long time to do.  Instead just take all fonts
951 //              if(!load_freetype_face(freetype_library,
952 //                      freetype_face, entry->path) )
953 // Fix parameters
954                 sprintf(line, "%s (%s)", entry->family, entry->foundry);
955                 entry->displayname = cstrdup(line);
956
957                 if(!strcasecmp(entry->weight, "demibold")) {
958                         entry->fixed_style |= BC_FONT_BOLD;
959                         entry->style |= FL_WEIGHT_DEMIBOLD;
960                 }
961                 else if(!strcasecmp(entry->weight, "bold")) {
962                         entry->fixed_style |= BC_FONT_BOLD;
963                         entry->style |= FL_WEIGHT_BOLD;
964                 }
965                 else {
966                         entry->style |= FL_WEIGHT_NORMAL;
967                 }
968
969                 if(!strcasecmp(entry->slant, "r")) {
970                         entry->style |= FL_SLANT_ROMAN;
971                 }
972                 else if(!strcasecmp(entry->slant, "i")) {
973                         entry->style |= FL_SLANT_ITALIC;
974                         entry->fixed_style |= BC_FONT_ITALIC;
975                 }
976                 else if(!strcasecmp(entry->slant, "o")) {
977                         entry->style |= FL_SLANT_OBLIQUE;
978                         entry->fixed_style |= BC_FONT_ITALIC;
979                 }
980
981                 if(!strcasecmp(entry->swidth, "normal"))
982                         entry->style = FL_WIDTH_NORMAL;
983                 else if(!strcasecmp(entry->swidth, "ultracondensed"))
984                         entry->style = FL_WIDTH_ULTRACONDENSED;
985                 else if(!strcasecmp(entry->swidth, "extracondensed"))
986                         entry->style = FL_WIDTH_EXTRACONDENSED;
987                 else if(!strcasecmp(entry->swidth, "condensed"))
988                         entry->style = FL_WIDTH_CONDENSED;
989                 else if(!strcasecmp(entry->swidth, "semicondensed"))
990                         entry->style = FL_WIDTH_SEMICONDENSED;
991                 else if(!strcasecmp(entry->swidth, "semiexpanded"))
992                         entry->style = FL_WIDTH_SEMIEXPANDED;
993                 else if(!strcasecmp(entry->swidth, "expanded"))
994                         entry->style = FL_WIDTH_EXPANDED;
995                 else if(!strcasecmp(entry->swidth, "extraexpanded"))
996                         entry->style = FL_WIDTH_EXTRAEXPANDED;
997                 else if(!strcasecmp(entry->swidth, "ultraexpanded"))
998                         entry->style = FL_WIDTH_ULTRAEXPANDED;
999                 else
1000                         entry->style = FL_WIDTH_NORMAL;
1001
1002                 fontlist->append(entry);
1003 //              printf("TitleMain::build_fonts %s: success\n",  entry->path);
1004 //printf("TitleMain::build_fonts 2\n");
1005         }
1006         pclose(in);
1007
1008
1009 // Load all the fonts from fontconfig
1010         FcPattern *pat;
1011         FcFontSet *fs;
1012         FcObjectSet *os;
1013         FcChar8 *family, *file, *foundry, *style, *format;
1014         int slant, spacing, width, weight;
1015         int force_style = 0;
1016 // if you want limit search to TrueType put 1
1017         int limit_to_trutype = 1;
1018         FcConfig *config;
1019         int i;
1020         char tmpstring[BCTEXTLEN];
1021         if(!FcInit())
1022                 return 1;
1023         config = FcConfigGetCurrent();
1024         FcConfigSetRescanInterval(config, 0);
1025
1026         pat = FcPatternCreate();
1027         os = FcObjectSetBuild ( FC_FAMILY, FC_FILE, FC_FOUNDRY, FC_WEIGHT,
1028                 FC_WIDTH, FC_SLANT, FC_FONTFORMAT, FC_SPACING, FC_STYLE, (char *) 0);
1029         FcPatternAddBool(pat, FC_SCALABLE, true);
1030
1031         if(language[0]) {
1032                 char langstr[LEN_LANG * 3];
1033                 strcpy(langstr, language);
1034
1035                 if(region[0]) {
1036                         strcat(langstr, "-");
1037                         strcat(langstr, region);
1038                 }
1039
1040                 FcLangSet *ls =  FcLangSetCreate();
1041                 if(FcLangSetAdd(ls, (const FcChar8*)langstr))
1042                 if(FcPatternAddLangSet(pat, FC_LANG, ls))
1043                 FcLangSetDestroy(ls);
1044         }
1045
1046         fs = FcFontList(config, pat, os);
1047         FcPatternDestroy(pat);
1048         FcObjectSetDestroy(os);
1049
1050         for (i = 0; fs && i < fs->nfont; i++) {
1051                 FcPattern *font = fs->fonts[i];
1052                 force_style = 0;
1053                 FcPatternGetString(font, FC_FONTFORMAT, 0, &format);
1054                 //on this point you can limit font search
1055                 if(limit_to_trutype && strcmp((char *)format, "TrueType"))
1056                         continue;
1057
1058                 sprintf(tmpstring, "%s", format);
1059                 BC_FontEntry *entry = new BC_FontEntry;
1060                 if(FcPatternGetString(font, FC_FILE, 0, &file) == FcResultMatch) {
1061                         entry->path = cstrdup((char*)file);
1062                 }
1063
1064                 if(FcPatternGetString(font, FC_FOUNDRY, 0, &foundry) == FcResultMatch) {
1065                         entry->foundry = cstrdup((char*)foundry);
1066                 }
1067
1068                 if(FcPatternGetInteger(font, FC_WEIGHT, 0, &weight) == FcResultMatch) {
1069                         switch(weight) {
1070                         case FC_WEIGHT_THIN:
1071                         case FC_WEIGHT_EXTRALIGHT:
1072                         case FC_WEIGHT_LIGHT:
1073                         case FC_WEIGHT_BOOK:
1074                                 force_style = 1;
1075                                 entry->weight = cstrdup("medium");
1076                                 break;
1077
1078                         case FC_WEIGHT_NORMAL:
1079                         case FC_WEIGHT_MEDIUM:
1080                         default:
1081                                 entry->weight = cstrdup("medium");
1082                                 break;
1083
1084                         case FC_WEIGHT_BLACK:
1085                         case FC_WEIGHT_SEMIBOLD:
1086                         case FC_WEIGHT_BOLD:
1087                                 entry->weight = cstrdup("bold");
1088                                 entry->fixed_style |= BC_FONT_BOLD;
1089                                 break;
1090
1091                         case FC_WEIGHT_EXTRABOLD:
1092                         case FC_WEIGHT_EXTRABLACK:
1093                                 force_style = 1;
1094                                 entry->weight = cstrdup("bold");
1095                                 entry->fixed_style |= BC_FONT_BOLD;
1096                                 break;
1097                         }
1098                 }
1099
1100                 if(FcPatternGetString(font, FC_FAMILY, 0, &family) == FcResultMatch)
1101                         entry->family = cstrdup((char*)family);
1102
1103                 if(FcPatternGetInteger(font, FC_SLANT, 0, &slant) == FcResultMatch) {
1104                         switch(slant) {
1105                         case FC_SLANT_ROMAN:
1106                         default:
1107                                 entry->slant = cstrdup("r");
1108                                 entry->style |= FL_SLANT_ROMAN;
1109                                 break;
1110                         case FC_SLANT_ITALIC:
1111                                 entry->slant = cstrdup("i");
1112                                 entry->style |= FL_SLANT_ITALIC;
1113                                 entry->fixed_style |= BC_FONT_ITALIC;
1114                                 break;
1115                         case FC_SLANT_OBLIQUE:
1116                                 entry->slant = cstrdup("o");
1117                                 entry->style |= FL_SLANT_OBLIQUE;
1118                                 entry->fixed_style |= BC_FONT_ITALIC;
1119                                 break;
1120                         }
1121                 }
1122
1123                 if(FcPatternGetInteger(font, FC_WIDTH, 0, &width) == FcResultMatch) {
1124                         switch(width) {
1125                         case FC_WIDTH_ULTRACONDENSED:
1126                                 entry->swidth = cstrdup("ultracondensed");
1127                                 break;
1128
1129                         case FC_WIDTH_EXTRACONDENSED:
1130                                 entry->swidth = cstrdup("extracondensed");
1131                                 break;
1132
1133                         case FC_WIDTH_CONDENSED:
1134                                 entry->swidth = cstrdup("condensed");
1135                                 break;
1136                         case FC_WIDTH_SEMICONDENSED:
1137                                 entry->swidth = cstrdup("semicondensed");
1138                                 break;
1139
1140                         case FC_WIDTH_NORMAL:
1141                         default:
1142                                 entry->swidth = cstrdup("normal");
1143                                 break;
1144
1145                         case FC_WIDTH_SEMIEXPANDED:
1146                                 entry->swidth = cstrdup("semiexpanded");
1147                                 break;
1148
1149                         case FC_WIDTH_EXPANDED:
1150                                 entry->swidth = cstrdup("expanded");
1151                                 break;
1152
1153                         case FC_WIDTH_EXTRAEXPANDED:
1154                                 entry->swidth = cstrdup("extraexpanded");
1155                                 break;
1156
1157                         case FC_WIDTH_ULTRAEXPANDED:
1158                                 entry->swidth = cstrdup("ultraexpanded");
1159                                 break;
1160                         }
1161                 }
1162
1163                 if(FcPatternGetInteger(font, FC_SPACING, 0, &spacing) == FcResultMatch) {
1164                         switch(spacing) {
1165                         case 0:
1166                         default:
1167                                 entry->spacing = cstrdup("p");
1168                                 break;
1169
1170                         case 90:
1171                                 entry->spacing = cstrdup("d");
1172                                 break;
1173
1174                         case 100:
1175                                 entry->spacing = cstrdup("m");
1176                                 break;
1177
1178                         case 110:
1179                                 entry->spacing = cstrdup("c");
1180                                 break;
1181                         }
1182                 }
1183
1184                 // Add fake stuff for compatibility
1185                 entry->adstyle = cstrdup(" ");
1186                 entry->pixelsize = 0;
1187                 entry->pointsize = 0;
1188                 entry->xres = 0;
1189                 entry->yres = 0;
1190                 entry->avg_width = 0;
1191                 entry->registry = cstrdup("utf");
1192                 entry->encoding = cstrdup("8");
1193
1194                 if(!FcPatternGetString(font, FC_STYLE, 0, &style) == FcResultMatch)
1195                         force_style = 0;
1196
1197                 // If font has a style unmanaged by titler plugin, force style to be displayed on name
1198                 // in this way we can shown all available fonts styles.
1199                 if(force_style) {
1200                         sprintf(tmpstring, "%s (%s)", entry->family, style);
1201                         entry->displayname = cstrdup(tmpstring);
1202                 }
1203                 else {
1204                         if(strcmp(entry->foundry, "unknown")) {
1205                                 sprintf(tmpstring, "%s (%s)", entry->family, entry->foundry);
1206                                 entry->displayname = cstrdup(tmpstring);
1207                         }
1208                         else {
1209                                 sprintf(tmpstring, "%s", entry->family);
1210                                 entry->displayname = cstrdup(tmpstring);
1211                         }
1212
1213                 }
1214                 fontlist->append(entry);
1215         }
1216
1217         FcFontSetDestroy(fs);
1218         if(freetype_library)
1219                 FT_Done_FreeType(freetype_library);
1220 // for(int i = 0; i < fonts->total; i++)
1221 //      fonts->values[i]->dump();
1222
1223         FcConfigAppFontAddDir(0, (const FcChar8*)search_path);
1224         FcConfigSetRescanInterval(0, 0);
1225
1226         os = FcObjectSetBuild(FC_FAMILY, FC_FILE, FC_FOUNDRY, FC_WEIGHT,
1227                 FC_WIDTH, FC_SLANT, FC_SPACING, FC_STYLE, (char *)0);
1228         pat = FcPatternCreate();
1229         FcPatternAddBool(pat, FC_SCALABLE, true);
1230
1231         if(language[0])
1232         {
1233                 char langstr[LEN_LANG * 3];
1234                 strcpy(langstr, language);
1235
1236                 if(region[0])
1237                 {
1238                         strcat(langstr, "-");
1239                         strcat(langstr, region);
1240                 }
1241
1242                 FcLangSet *ls =  FcLangSetCreate();
1243                 if(FcLangSetAdd(ls, (const FcChar8*)langstr))
1244                         if(FcPatternAddLangSet(pat, FC_LANG, ls))
1245                 FcLangSetDestroy(ls);
1246         }
1247
1248         fs = FcFontList(0, pat, os);
1249         FcPatternDestroy(pat);
1250         FcObjectSetDestroy(os);
1251
1252         for(int i = 0; i < fs->nfont; i++)
1253         {
1254                 FcPattern *font = fs->fonts[i];
1255                 BC_FontEntry *entry = new BC_FontEntry;
1256
1257                 FcChar8 *strvalue;
1258                 if(FcPatternGetString(font, FC_FILE, 0, &strvalue) == FcResultMatch)
1259                 {
1260                         entry->path = new char[strlen((char*)strvalue) + 1];
1261                         strcpy(entry->path, (char*)strvalue);
1262                 }
1263
1264                 if(FcPatternGetString(font, FC_FOUNDRY, 0, &strvalue) == FcResultMatch)
1265                 {
1266                         entry->foundry = new char[strlen((char*)strvalue) + 1];
1267                         strcpy(entry->foundry, (char *)strvalue);
1268                 }
1269
1270                 if(FcPatternGetString(font, FC_FAMILY, 0, &strvalue) == FcResultMatch)
1271                 {
1272                         entry->family = new char[strlen((char*)strvalue) + 2];
1273                         strcpy(entry->family, (char*)strvalue);
1274                 }
1275
1276                 int intvalue;
1277                 if(FcPatternGetInteger(font, FC_SLANT, 0, &intvalue) == FcResultMatch)
1278                 {
1279                         switch(intvalue)
1280                         {
1281                         case FC_SLANT_ROMAN:
1282                         default:
1283                                 entry->style |= FL_SLANT_ROMAN;
1284                                 break;
1285
1286                         case FC_SLANT_ITALIC:
1287                                 entry->style |= FL_SLANT_ITALIC;
1288                                 break;
1289
1290                         case FC_SLANT_OBLIQUE:
1291                                 entry->style |= FL_SLANT_OBLIQUE;
1292                                 break;
1293                         }
1294                 }
1295
1296                 if(FcPatternGetInteger(font, FC_WEIGHT, 0, &intvalue) == FcResultMatch)
1297                 {
1298                         switch(intvalue)
1299                         {
1300                         case FC_WEIGHT_THIN:
1301                                 entry->style |= FL_WEIGHT_THIN;
1302                                 break;
1303
1304                         case FC_WEIGHT_EXTRALIGHT:
1305                                 entry->style |= FL_WEIGHT_EXTRALIGHT;
1306                                 break;
1307
1308                         case FC_WEIGHT_LIGHT:
1309                                 entry->style |= FL_WEIGHT_LIGHT;
1310                                 break;
1311
1312                         case FC_WEIGHT_BOOK:
1313                                 entry->style |= FL_WEIGHT_BOOK;
1314                                 break;
1315
1316                         case FC_WEIGHT_NORMAL:
1317                         default:
1318                                 entry->style |= FL_WEIGHT_NORMAL;
1319                                 break;
1320
1321                         case FC_WEIGHT_MEDIUM:
1322                                 entry->style |= FL_WEIGHT_MEDIUM;
1323                                 break;
1324
1325                         case FC_WEIGHT_DEMIBOLD:
1326                                 entry->style |= FL_WEIGHT_DEMIBOLD;
1327                                 break;
1328
1329                         case FC_WEIGHT_BOLD:
1330                                 entry->style |= FL_WEIGHT_BOLD;
1331                                 break;
1332
1333                         case FC_WEIGHT_EXTRABOLD:
1334                                 entry->style |= FL_WEIGHT_EXTRABOLD;
1335                                 break;
1336
1337                         case FC_WEIGHT_BLACK:
1338                                 entry->style |= FL_WEIGHT_BLACK;
1339                                 break;
1340
1341                         case FC_WEIGHT_EXTRABLACK:
1342                                 entry->style |= FL_WEIGHT_EXTRABLACK;
1343                                 break;
1344                         }
1345                 }
1346
1347                 if(FcPatternGetInteger(font, FC_WIDTH, 0, &intvalue) == FcResultMatch)
1348                 {
1349                         switch(intvalue)
1350                         {
1351                         case FC_WIDTH_ULTRACONDENSED:
1352                                 entry->style |= FL_WIDTH_ULTRACONDENSED;
1353                                 break;
1354
1355                         case FC_WIDTH_EXTRACONDENSED:
1356                                 entry->style |= FL_WIDTH_EXTRACONDENSED;
1357                                 break;
1358
1359                         case FC_WIDTH_CONDENSED:
1360                                 entry->style |= FL_WIDTH_CONDENSED;
1361                                 break;
1362
1363                         case FC_WIDTH_SEMICONDENSED:
1364                                 entry->style = FL_WIDTH_SEMICONDENSED;
1365                                 break;
1366
1367                         case FC_WIDTH_NORMAL:
1368                         default:
1369                                 entry->style |= FL_WIDTH_NORMAL;
1370                                 break;
1371
1372                         case FC_WIDTH_SEMIEXPANDED:
1373                                 entry->style |= FL_WIDTH_SEMIEXPANDED;
1374                                 break;
1375
1376                         case FC_WIDTH_EXPANDED:
1377                                 entry->style |= FL_WIDTH_EXPANDED;
1378                                 break;
1379
1380                         case FC_WIDTH_EXTRAEXPANDED:
1381                                 entry->style |= FL_WIDTH_EXTRAEXPANDED;
1382                                 break;
1383
1384                         case FC_WIDTH_ULTRAEXPANDED:
1385                                 entry->style |= FL_WIDTH_ULTRAEXPANDED;
1386                                 break;
1387                         }
1388                 }
1389                 if(FcPatternGetInteger(font, FC_SPACING, 0, &intvalue) == FcResultMatch)
1390                 {
1391                         switch(intvalue)
1392                         {
1393                         case FC_PROPORTIONAL:
1394                         default:
1395                                 entry->style |= FL_PROPORTIONAL;
1396                                 break;
1397
1398                         case FC_DUAL:
1399                                 entry->style |= FL_DUAL;
1400                                 break;
1401
1402                         case FC_MONO:
1403                                 entry->style |= FL_MONO;
1404                                 break;
1405
1406                         case FC_CHARCELL:
1407                                 entry->style |= FL_CHARCELL;
1408                                 break;
1409                         }
1410                 }
1411                 if(entry->foundry && strcmp(entry->foundry, "unknown"))
1412                 {
1413                         char tempstr[BCTEXTLEN];
1414                         sprintf(tempstr, "%s (%s)", entry->family, entry->foundry);
1415                         entry->displayname = new char[strlen(tempstr) + 1];
1416                         strcpy(entry->displayname, tempstr);
1417                 }
1418                 else
1419                 {
1420                         entry->displayname = new char[strlen(entry->family) + 1];
1421                         strcpy(entry->displayname, entry->family);
1422                 }
1423                 fontlist->append(entry);
1424         }
1425         FcFontSetDestroy(fs);
1426         return 0;
1427 }
1428
1429 BC_FontEntry *BC_Resources::find_fontentry(const char *displayname, int style, int mask)
1430 {
1431         BC_FontEntry *entry, *style_match, *displayname_match;
1432
1433         if(!fontlist)
1434                 return 0;
1435
1436         if(displayname)
1437         {
1438                 for(int i = 0; i < fontlist->total; i++)
1439                 {
1440                         entry = fontlist->values[i];
1441
1442                         if(strcmp(entry->displayname, displayname) == 0 &&
1443                                         (entry->style & mask) == style)
1444                                 return entry;
1445                 }
1446         }
1447 // No exact match - assume normal width font
1448         style |= FL_WIDTH_NORMAL;
1449         mask |= FL_WIDTH_MASK;
1450         style_match = 0;  displayname_match = 0;
1451         for(int i = 0; i < fontlist->total; i++)
1452         {
1453                 entry = fontlist->values[i];
1454
1455                 if(!strncasecmp(displayname, entry->family,
1456                                 strlen(entry->family)))
1457                 {
1458                         if((entry->style & mask) == style)
1459                                 return entry;
1460                         if(!displayname_match)
1461                                 displayname_match = entry;
1462                 }
1463
1464                 if((entry->style & mask) == style)
1465                 {
1466                         if(!style_match)
1467                                 style_match = entry;
1468                 }
1469         }
1470         return displayname_match ? displayname_match : style_match;
1471 }
1472
1473 size_t BC_Resources::encode(const char *from_enc, const char *to_enc,
1474         char *input, int input_length, char *output, int output_length)
1475 {
1476         size_t inbytes, outbytes = 0;
1477         iconv_t cd;
1478         char *outbase = output;
1479
1480         if(!from_enc || *from_enc == 0)
1481                 from_enc = "UTF-8";
1482
1483         if(!to_enc || *to_enc == 0)
1484                 to_enc = "UTF-8";
1485
1486         if(input_length < 0)
1487                 inbytes = strlen(input);
1488         else
1489                 inbytes = input_length;
1490
1491         if(strcmp(from_enc, to_enc) && inbytes)
1492         {
1493                 if((cd = iconv_open(to_enc, from_enc)) == (iconv_t)-1)
1494                 {
1495                         printf(_("Conversion from %s to %s is not available"),
1496                                 from_enc, to_enc);
1497                         return 0;
1498                 }
1499
1500                 outbytes = output_length - 1;
1501
1502                 iconv(cd, &input, &inbytes, &output, &outbytes);
1503
1504                 iconv_close(cd);
1505                 inbytes = output - outbase;
1506         }
1507         else if(inbytes)
1508         {
1509                 memcpy(output,  input, inbytes);
1510                 outbytes -= inbytes;
1511         }
1512         for(int i = 0; i < 4; i++)
1513         {
1514                 output[i] = 0;
1515                 if(outbytes-- == 0)
1516                         break;
1517         }
1518         return inbytes;
1519 }
1520
1521 int BC_Resources::find_font_by_char(FT_ULong char_code, char *path_new, const FT_Face oldface)
1522 {
1523         FcPattern *font, *ofont;
1524         FcChar8 *file;
1525         int result = 0;
1526
1527         *path_new = 0;
1528
1529         // Do not search control codes
1530         if(char_code < ' ')
1531                 return 0;
1532
1533         if( (ofont = FcFreeTypeQueryFace(oldface, (const FcChar8*)"", 4097, 0)) != 0 ) {
1534                 if( (font = find_similar_font(char_code, ofont)) != 0 ) {
1535                         if(FcPatternGetString(font, FC_FILE, 0, &file) == FcResultMatch) {
1536                                 strcpy(path_new, (char*)file);
1537                                 result = 1;
1538                         }
1539                         FcPatternDestroy(font);
1540                 }
1541                 FcPatternDestroy(ofont);
1542         }
1543         return result;
1544 }
1545
1546 FcPattern* BC_Resources::find_similar_font(FT_ULong char_code, FcPattern *oldfont)
1547 {
1548         FcPattern *pat, *font;
1549         FcFontSet *fs;
1550         FcObjectSet *os;
1551         FcCharSet *fcs;
1552         int ival;
1553
1554         // Do not search control codes
1555         if(char_code < ' ')
1556                 return 0;
1557
1558         fontconfig_lock.lock("BC_Resources::find_similar_font");
1559         pat = FcPatternCreate();
1560         os = FcObjectSetBuild(FC_FILE, FC_CHARSET, FC_SCALABLE, FC_FAMILY,
1561                 FC_SLANT, FC_WEIGHT, FC_WIDTH, (char *)0);
1562
1563         FcPatternAddBool(pat, FC_SCALABLE, true);
1564         fcs = FcCharSetCreate();
1565         if(FcCharSetAddChar(fcs, char_code))
1566                 FcPatternAddCharSet(pat, FC_CHARSET, fcs);
1567         FcCharSetDestroy(fcs);
1568         for( int i=0; i<(int)LEN_FCPROP; ++i ) {
1569                 if(FcPatternGetInteger(oldfont, fc_properties[i], 0, &ival) == FcResultMatch)
1570                         FcPatternAddInteger(pat, fc_properties[i], ival);
1571         }
1572         fs = FcFontList(0, pat, os);
1573         for( int i=LEN_FCPROP; --i>=0 && !fs->nfont; ) {
1574                 FcFontSetDestroy(fs);
1575                 FcPatternDel(pat, fc_properties[i]);
1576                 fs = FcFontList(0, pat, os);
1577         }
1578         FcPatternDestroy(pat);
1579         FcObjectSetDestroy(os);
1580
1581         pat = 0;
1582
1583         for (int i = 0; i < fs->nfont; i++)
1584         {
1585                 font = fs->fonts[i];
1586                 if(FcPatternGetCharSet(font, FC_CHARSET, 0, &fcs) == FcResultMatch)
1587                 {
1588                         if(FcCharSetHasChar(fcs, char_code))
1589                         {
1590                                 pat =  FcPatternDuplicate(font);
1591                                 break;
1592                         }
1593                 }
1594         }
1595         FcFontSetDestroy(fs);
1596         fontconfig_lock.unlock();
1597
1598         return pat;
1599 }
1600
1601 void BC_Resources::dump_fonts(FILE *fp)
1602 {
1603         for( int i=0; i<fontlist->total; ++i ) {
1604                 BC_FontEntry *ep = fontlist->values[i];
1605                 fprintf(fp,"%s = %s\n",ep->displayname,ep->path);
1606                 fprintf(fp,"  %s:%s:%s:%s:%s:%s:%d:%d:%d:%d:%d:%s:%d:%s:%s:%d\n",
1607                         ep->foundry, ep->family, ep->weight, ep->slant, ep->swidth, ep->adstyle,
1608                         ep->pixelsize, ep->pointsize, ep->xres, ep->yres, ep->style, ep->spacing,
1609                         ep->avg_width, ep->registry, ep->encoding, ep->fixed_style);
1610         }
1611 }
1612