lock xft, plugin index builder lock, clear clip thumbnails, clear vwin clock, setpoin...
[goodguy/history.git] / cinelerra-5.1 / guicast / bcresources.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2009-2015 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 "bccolors.h"
31 #include "bccmodels.h"
32 #include "cstrdup.h"
33 #include "fonts.h"
34 #include "language.h"
35 #include "vframe.h"
36 #include "workarounds.h"
37
38 #include <string.h>
39 #include <iconv.h>
40 #include <sys/ipc.h>
41 #include <sys/shm.h>
42 #include <X11/extensions/XShm.h>
43 #include <fontconfig/fontconfig.h>
44 #include <fontconfig/fcfreetype.h>
45 #include <unistd.h>
46
47
48
49
50
51 int BC_Resources::error = 0;
52
53 VFrame* BC_Resources::bg_image = 0;
54 VFrame* BC_Resources::menu_bg = 0;
55
56 int BC_Resources::locale_utf8 = 0;
57 int BC_Resources::little_endian = 0;
58 char BC_Resources::language[LEN_LANG] = {0};
59 char BC_Resources::region[LEN_LANG] = {0};
60 char BC_Resources::encoding[LEN_ENCOD] = {0};
61 const char *BC_Resources::wide_encoding = 0;
62 ArrayList<BC_FontEntry*> *BC_Resources::fontlist = 0;
63 int BC_Resources::font_debug = 0;
64 const char *BC_Resources::fc_properties[] = { FC_SLANT, FC_WEIGHT, FC_WIDTH };
65 #define LEN_FCPROP (sizeof(BC_Resources::fc_properties) / sizeof(const char*))
66
67 static const char *def_small_font = "-*-helvetica-medium-r-normal-*-%d-*";  // 10
68 static const char *def_small_font2 = "-*-helvetica-medium-r-normal-*-%d-*"; // 11
69 static const char *def_medium_font = "-*-helvetica-bold-r-normal-*-%d-*";   // 14
70 static const char *def_medium_font2 = "-*-helvetica-bold-r-normal-*-%d-*";  // 14
71 static const char *def_large_font = "-*-helvetica-bold-r-normal-*-%d-*";    // 18
72 static const char *def_large_font2 = "-*-helvetica-bold-r-normal-*-%d-*";   // 20
73 static const char *def_big_font =
74   "-*-bitstream charter-bold-r-normal-*-*-0-%d-%d-p-0-iso8859-1"; // 160
75 static const char *def_big_font2 =
76   "-*-nimbus sans l-bold-r-normal-*-*-0-%d-%d-p-0-iso8859-1";     // 160
77 static const char *def_clock_font = "-*-helvetica-bold-r-normal-*-%d-*";      // 16
78 static const char *def_clock_font2 = "-*-helvetica-bold-r-normal-*-%d-*";     // 18
79 static const char *def_small_fontset =  "-*-helvetica-medium-r-normal-*-%d-*";// 10
80 static const char *def_medium_fontset = "-*-helvetica-bold-r-normal-*-%d-*";  // 14
81 static const char *def_large_fontset =  "-*-helvetica-bold-r-normal-*-%d-*";  // 18
82 static const char *def_big_fontset =    "-*-helvetica-bold-r-normal-*-%d-*";  // 24
83 static const char *def_clock_fontset = "-*-helvetica-bold-r-normal-*-%d-*";   // 16
84
85 static const char *def_small_font_xft = "Sans:pixelsize=%.4f";           // 10.6667
86 static const char *def_small_b_font_xft = "Sans:bold:pixelsize=%.4f";    // 10.6667
87 static const char *def_medium_font_xft = "Sans:pixelsize=%.4f";          // 13.3333
88 static const char *def_medium_b_font_xft = "Sans:bold:pixelsize=%.4f";   // 13.3333
89 static const char *def_large_font_xft = "Sans:pixelsize=%.4f";           // 21.3333
90 static const char *def_large_b_font_xft = "Sans:bold:pixelsize=%.4f";    // 21.3333
91 static const char *def_big_font_xft = "Sans:pixelsize=37.3333";          // 37.3333
92 static const char *def_big_b_font_xft = "Sans:bold:pixelsize=37.33333";  // 37.3333
93 static const char *def_clock_font_xft = "Sans:pixelsize=%.4f";           // 16
94
95 #define default_font_xft2 "-microsoft-verdana-*-*-*-*-*-*-*-*-*-*-*-*"
96
97 const char* BC_Resources::small_font = 0;
98 const char* BC_Resources::small_font2 = 0;
99 const char* BC_Resources::medium_font = 0;
100 const char* BC_Resources::medium_font2 = 0;
101 const char* BC_Resources::large_font = 0;
102 const char* BC_Resources::large_font2 = 0;
103 const char* BC_Resources::big_font = 0;
104 const char* BC_Resources::big_font2 = 0;
105 const char* BC_Resources::clock_font = 0;
106 const char* BC_Resources::clock_font2 = 0;
107 const char* BC_Resources::small_fontset = 0;
108 const char* BC_Resources::medium_fontset = 0;
109 const char* BC_Resources::large_fontset = 0;
110 const char* BC_Resources::big_fontset = 0;
111 const char* BC_Resources::clock_fontset = 0;
112 const char* BC_Resources::small_font_xft = 0;
113 const char* BC_Resources::small_font_xft2 = 0;
114 const char* BC_Resources::small_b_font_xft = 0;
115 const char* BC_Resources::medium_font_xft = 0;
116 const char* BC_Resources::medium_font_xft2 = 0;
117 const char* BC_Resources::medium_b_font_xft = 0;
118 const char* BC_Resources::large_font_xft = 0;
119 const char* BC_Resources::large_font_xft2 = 0;
120 const char* BC_Resources::large_b_font_xft = 0;
121 const char* BC_Resources::big_font_xft = 0;
122 const char* BC_Resources::big_font_xft2 = 0;
123 const char* BC_Resources::big_b_font_xft = 0;
124 const char* BC_Resources::clock_font_xft = 0;
125 const char* BC_Resources::clock_font_xft2 = 0;
126
127 #define def_font(v, s...) do { sprintf(string,def_##v,s); v = cstrdup(string); } while(0)
128 #define set_font(v, s) do { sprintf(string, "%s", s); v = cstrdup(string); } while(0)
129 #define iround(v) ((int)(v+0.5))
130 void BC_Resources::init_font_defs(double scale)
131 {
132         char string[BCTEXTLEN];
133         def_font(small_font,       iround(scale*11));
134         def_font(small_font2,      iround(scale*11));
135         def_font(medium_font,      iround(scale*14));
136         def_font(medium_font2,     iround(scale*14));
137         def_font(large_font,       iround(scale*18));
138         def_font(large_font2,      iround(scale*20));
139         def_font(big_font,         iround(scale*160), iround(scale*160));
140         def_font(big_font2,        iround(scale*16), iround(scale*16));
141         def_font(clock_font,       iround(scale*16));
142         def_font(clock_font2,      iround(scale*18));
143         def_font(small_fontset,    iround(scale*10));
144         def_font(medium_fontset,   iround(scale*14));
145         def_font(large_fontset,    iround(scale*18));
146         def_font(big_fontset,      iround(scale*24));
147         def_font(clock_fontset,    iround(scale*16));
148         def_font(small_font_xft,   (scale*10.6667));
149         def_font(small_b_font_xft, (scale*10.6667));
150         def_font(medium_font_xft,  (scale*13.3333));
151         def_font(medium_b_font_xft,(scale*13.3333));
152         def_font(large_font_xft,   (scale*21.3333));
153         def_font(large_b_font_xft, (scale*21.3333));
154         def_font(big_font_xft,     (scale*37.3333));
155         def_font(big_b_font_xft,   (scale*37.3333));
156         def_font(clock_font_xft,   (scale*16.));
157
158         set_font(small_font_xft2,  default_font_xft2);
159         set_font(medium_font_xft2, default_font_xft2);
160         set_font(large_font_xft2,  default_font_xft2);
161         set_font(big_font_xft2,    default_font_xft2);
162         set_font(clock_font_xft2,  default_font_xft2);
163 }
164 void BC_Resources::finit_font_defs()
165 {
166         delete [] small_font;
167         delete [] small_font2;
168         delete [] medium_font;
169         delete [] medium_font2;
170         delete [] large_font;
171         delete [] large_font2;
172         delete [] big_font;
173         delete [] big_font2;
174         delete [] clock_font;
175         delete [] clock_font2;
176         delete [] small_fontset;
177         delete [] medium_fontset;
178         delete [] large_fontset;
179         delete [] big_fontset;
180         delete [] clock_fontset;
181         delete [] small_font_xft;
182         delete [] small_b_font_xft;
183         delete [] medium_font_xft;
184         delete [] medium_b_font_xft;
185         delete [] large_font_xft;
186         delete [] large_b_font_xft;
187         delete [] big_font_xft;
188         delete [] big_b_font_xft;
189         delete [] clock_font_xft;
190
191         delete [] small_font_xft2;
192         delete [] medium_font_xft2;
193         delete [] large_font_xft2;
194         delete [] big_font_xft2;
195         delete [] clock_font_xft2;
196 }
197
198
199 suffix_to_type_t BC_Resources::suffix_to_type[] =
200 {
201     { "aac", ICON_SOUND },
202     { "ac3", ICON_SOUND },
203     { "dts", ICON_SOUND },
204     { "f4a", ICON_SOUND },
205     { "flac", ICON_SOUND },
206     { "m4a", ICON_SOUND },
207     { "mp2", ICON_SOUND },
208     { "mp3", ICON_SOUND },
209     { "mpc", ICON_SOUND },
210     { "oga", ICON_SOUND },
211     { "ogg", ICON_SOUND },
212     { "opus", ICON_SOUND },
213     { "ra", ICON_SOUND },
214     { "tta", ICON_SOUND },
215     { "vox", ICON_SOUND },
216     { "wav", ICON_SOUND },
217     { "wma", ICON_SOUND },
218     { "3gp", ICON_FILM },
219     { "avi", ICON_FILM },
220     { "bmp", ICON_FILM },
221     { "cr2", ICON_FILM },
222     { "dnxhd", ICON_FILM },
223     { "drc", ICON_FILM },
224     { "dv", ICON_FILM },
225     { "dvd", ICON_FILM },
226     { "exr", ICON_FILM },
227     { "f4v", ICON_FILM },
228     { "flv", ICON_FILM },
229     { "gif", ICON_FILM },
230     { "gxf", ICON_FILM },
231     { "h263", ICON_FILM },
232     { "h264", ICON_FILM },
233     { "h265", ICON_FILM },
234     { "hevc", ICON_FILM },
235     { "ifo", ICON_FILM },
236     { "jpeg", ICON_FILM },
237     { "jpg", ICON_FILM },
238     { "m2t", ICON_FILM },
239     { "m2ts", ICON_FILM },
240     { "m2v", ICON_FILM },
241     { "m4p", ICON_FILM },
242     { "m4v", ICON_FILM },
243     { "mkv", ICON_FILM },
244     { "mov", ICON_FILM },
245     { "mp4", ICON_FILM },
246     { "mpe", ICON_FILM },
247     { "mpeg", ICON_FILM },
248     { "mpg", ICON_FILM },
249     { "mpv", ICON_FILM },
250     { "mts", ICON_FILM },
251     { "mxf", ICON_FILM },
252     { "ogv", ICON_FILM },
253     { "pcm", ICON_FILM },
254     { "pgm", ICON_FILM },
255     { "png", ICON_FILM },
256     { "ppm", ICON_FILM },
257     { "qt", ICON_FILM },
258     { "rm", ICON_FILM },
259     { "rmvb", ICON_FILM },
260     { "rv", ICON_FILM },
261     { "swf", ICON_FILM },
262     { "tif", ICON_FILM },
263     { "tiff", ICON_FILM },
264     { "ts",  ICON_FILM },
265     { "vob", ICON_FILM },
266     { "vts", ICON_FILM },
267     { "webm", ICON_FILM },
268     { "webp", ICON_FILM },
269     { "wmv", ICON_FILM },
270     { "xml", ICON_FILM },
271     { "y4m", ICON_FILM },
272     { 0, 0 },
273 };
274
275 BC_Signals* BC_Resources::signal_handler = 0;
276 Mutex BC_Resources::fontconfig_lock("BC_Resources::fonconfig_lock");
277
278 int BC_Resources::x_error_handler(Display *display, XErrorEvent *event)
279 {
280 #if defined(OUTPUT_X_ERROR)
281         char string[1024];
282         XGetErrorText(event->display, event->error_code, string, 1024);
283         fprintf(stderr, "BC_Resources::x_error_handler: error_code=%d opcode=%d,%d %s\n",
284                 event->error_code,
285                 event->request_code,
286                 event->minor_code,
287                 string);
288 #endif
289
290         BC_Resources::error = 1;
291 // This bug only happens in 32 bit mode.
292 //      if(sizeof(long) == 4)
293 //              BC_WindowBase::get_resources()->use_xft = 0;
294         return 0;
295 }
296
297 int BC_Resources::machine_cpus = 1;
298
299 int BC_Resources::get_machine_cpus()
300 {
301         int cpus = 1;
302         FILE *proc = fopen("/proc/cpuinfo", "r");
303         if( proc ) {
304                 char string[BCTEXTLEN], *cp;
305                 while(!feof(proc) && fgets(string, sizeof(string), proc) ) {
306                         if( !strncasecmp(string, "processor", 9) &&
307                             (cp = strchr(string, ':')) != 0 ) {
308                                 int n = atol(cp+1) + 1;
309                                 if( n > cpus ) cpus = n;
310                         }
311                         else if( !strncasecmp(string, "cpus detected", 13) &&
312                             (cp = strchr(string, ':')) != 0 )
313                                 cpus = atol(cp+1);
314                 }
315                 fclose(proc);
316         }
317         return cpus;
318 }
319
320 void BC_Resources::new_vframes(int n, VFrame *vframes[], ...)
321 {
322         va_list ap;
323         va_start(ap, vframes);
324         for( int i=0; i<n; ++i )
325                 vframes[i] = va_arg(ap, VFrame *);
326         va_end(ap);
327 }
328
329 VFrame *BC_Resources::default_type_to_icon[6] = { 0, };
330 VFrame *BC_Resources::default_bar = 0;
331 VFrame *BC_Resources::default_cancel_images[3] = { 0, };
332 VFrame *BC_Resources::default_ok_images[3] = { 0, };
333 VFrame *BC_Resources::default_usethis_images[3] = { 0, };
334 #if 0
335 VFrame *BC_Resources::default_checkbox_images[5] = { 0, };
336 VFrame *BC_Resources::default_radial_images[5] = { 0, };
337 VFrame *BC_Resources::default_label_images[5] = { 0, };
338 #endif
339 VFrame *BC_Resources::default_menuitem_data[3] = { 0, };
340 VFrame *BC_Resources::default_menubar_data[3] = { 0, };
341 VFrame *BC_Resources::default_menu_popup_bg = 0;
342 VFrame *BC_Resources::default_menu_bar_bg = 0;
343 VFrame *BC_Resources::default_check_image = 0;
344 VFrame *BC_Resources::default_filebox_text_images[3] = { 0, };
345 VFrame *BC_Resources::default_filebox_icons_images[3] = { 0, };
346 VFrame *BC_Resources::default_filebox_updir_images[3] = { 0, };
347 VFrame *BC_Resources::default_filebox_newfolder_images[3] = { 0, };
348 VFrame *BC_Resources::default_filebox_rename_images[3] = { 0, };
349 VFrame *BC_Resources::default_filebox_delete_images[3] = { 0, };
350 VFrame *BC_Resources::default_filebox_reload_images[3] = { 0, };
351 VFrame *BC_Resources::default_filebox_szfmt_images[12] = { 0, };
352 VFrame *BC_Resources::default_listbox_button[4] = { 0, };
353 VFrame *BC_Resources::default_listbox_bg = 0;
354 VFrame *BC_Resources::default_listbox_expand[5] = { 0, };
355 VFrame *BC_Resources::default_listbox_column[3] = { 0, };
356 VFrame *BC_Resources::default_listbox_up = 0;
357 VFrame *BC_Resources::default_listbox_dn = 0;
358 VFrame *BC_Resources::default_pot_images[3] = { 0, };
359 VFrame *BC_Resources::default_progress_images[2] = { 0, };
360 VFrame *BC_Resources::default_medium_7segment[20] = { 0, };
361
362 BC_Resources::BC_Resources()
363 {
364         synchronous = 0;
365         vframe_shm = 0;
366         double default_scale = 1.0; // display_size/1000.;
367         char *env = getenv("BC_FONT_DEBUG");
368         font_debug = env ? atoi(env) : 0;
369         env = getenv("BC_FONT_SCALE");
370         font_scale = env ? atof(env) : default_scale;
371         if( font_scale <= 0 ) font_scale = 1;
372         init_font_defs(font_scale);
373         env = getenv("BC_ICON_SCALE");
374         icon_scale = env ? atof(env) : default_scale;
375         if( icon_scale <= 0 ) icon_scale = 1;
376
377         id_lock = new Mutex("BC_Resources::id_lock");
378         create_window_lock = new Mutex("BC_Resources::create_window_lock", 1);
379         id = 0;
380         machine_cpus = get_machine_cpus();
381
382         for(int i = 0; i < FILEBOX_HISTORY_SIZE; i++)
383                 filebox_history[i].path[0] = 0;
384
385 #ifdef HAVE_XFT
386         xftInitFtLibrary();
387 #endif
388
389         little_endian = (*(const u_int32_t*)"\01\0\0\0") & 1;
390         wide_encoding = little_endian ?  "UTF32LE" : "UTF32BE";
391         use_xvideo = 1;
392
393 #include "images/file_folder_png.h"
394 #include "images/file_unknown_png.h"
395 #include "images/file_film_png.h"
396 #include "images/file_sound_png.h"
397 #include "images/file_label_png.h"
398 #include "images/file_column_png.h"
399 new_vframes(6,default_type_to_icon,
400         new VFramePng(file_folder_png),
401         new VFramePng(file_unknown_png),
402         new VFramePng(file_film_png),
403         new VFramePng(file_sound_png),
404         new VFramePng(file_label_png),
405         new VFramePng(file_column_png));
406
407 #include "images/bar_png.h"
408         default_bar = new VFramePng(bar_png);
409
410 #include "images/cancel_up_png.h"
411 #include "images/cancel_hi_png.h"
412 #include "images/cancel_dn_png.h"
413 new_vframes(3,default_cancel_images,
414         new VFramePng(cancel_up_png),
415         new VFramePng(cancel_hi_png),
416         new VFramePng(cancel_dn_png));
417
418 #include "images/ok_up_png.h"
419 #include "images/ok_hi_png.h"
420 #include "images/ok_dn_png.h"
421 new_vframes(3,default_ok_images,
422         new VFramePng(ok_up_png),
423         new VFramePng(ok_hi_png),
424         new VFramePng(ok_dn_png));
425
426 #include "images/usethis_up_png.h"
427 #include "images/usethis_uphi_png.h"
428 #include "images/usethis_dn_png.h"
429 new_vframes(3,default_usethis_images,
430         new VFramePng(usethis_up_png),
431         new VFramePng(usethis_uphi_png),
432         new VFramePng(usethis_dn_png));
433
434 #if 0
435 #include "images/checkbox_checked_png.h"
436 #include "images/checkbox_dn_png.h"
437 #include "images/checkbox_checkedhi_png.h"
438 #include "images/checkbox_up_png.h"
439 #include "images/checkbox_hi_png.h"
440 new_vframes(5,default_checkbox_images,
441         new VFramePng(checkbox_up_png),
442         new VFramePng(checkbox_hi_png),
443         new VFramePng(checkbox_checked_png),
444         new VFramePng(checkbox_dn_png),
445         new VFramePng(checkbox_checkedhi_png));
446
447 #include "images/radial_checked_png.h"
448 #include "images/radial_dn_png.h"
449 #include "images/radial_checkedhi_png.h"
450 #include "images/radial_up_png.h"
451 #include "images/radial_hi_png.h"
452 new_vframes(5,default_radial_images,
453         new VFramePng(radial_up_png),
454         new VFramePng(radial_hi_png),
455         new VFramePng(radial_checked_png),
456         new VFramePng(radial_dn_png),
457         new VFramePng(radial_checkedhi_png));
458
459 new_vframes(5,default_label_images,
460         new VFramePng(radial_up_png),
461         new VFramePng(radial_hi_png),
462         new VFramePng(radial_checked_png),
463         new VFramePng(radial_dn_png),
464         new VFramePng(radial_checkedhi_png));
465 #endif
466
467 #include "images/menuitem_up_png.h"
468 #include "images/menuitem_hi_png.h"
469 #include "images/menuitem_dn_png.h"
470 new_vframes(3,default_menuitem_data,
471         new VFramePng(menuitem_up_png),
472         new VFramePng(menuitem_hi_png),
473         new VFramePng(menuitem_dn_png));
474
475 #include "images/menubar_up_png.h"
476 #include "images/menubar_hi_png.h"
477 #include "images/menubar_dn_png.h"
478 new_vframes(3,default_menubar_data,
479         new VFramePng(menubar_up_png),
480         new VFramePng(menubar_hi_png),
481         new VFramePng(menubar_dn_png));
482
483 #include "images/menu_popup_bg_png.h"
484         default_menu_popup_bg = new VFramePng(menu_popup_bg_png);
485
486 #include "images/menubar_bg_png.h"
487         default_menu_bar_bg = new VFramePng(menubar_bg_png);
488
489 #include "images/check_png.h"
490         default_check_image = new VFramePng(check_png);
491
492 #include "images/file_text_up_png.h"
493 #include "images/file_text_hi_png.h"
494 #include "images/file_text_dn_png.h"
495 new_vframes(3,default_filebox_text_images,
496         new VFramePng(file_text_up_png),
497         new VFramePng(file_text_hi_png),
498         new VFramePng(file_text_dn_png));
499
500 #include "images/file_icons_up_png.h"
501 #include "images/file_icons_hi_png.h"
502 #include "images/file_icons_dn_png.h"
503 new_vframes(3,default_filebox_icons_images,
504         new VFramePng(file_icons_up_png),
505         new VFramePng(file_icons_hi_png),
506         new VFramePng(file_icons_dn_png));
507
508 #include "images/file_updir_up_png.h"
509 #include "images/file_updir_hi_png.h"
510 #include "images/file_updir_dn_png.h"
511 new_vframes(3,default_filebox_updir_images,
512         new VFramePng(file_updir_up_png),
513         new VFramePng(file_updir_hi_png),
514         new VFramePng(file_updir_dn_png));
515
516 #include "images/file_newfolder_up_png.h"
517 #include "images/file_newfolder_hi_png.h"
518 #include "images/file_newfolder_dn_png.h"
519 new_vframes(3,default_filebox_newfolder_images,
520         new VFramePng(file_newfolder_up_png),
521         new VFramePng(file_newfolder_hi_png),
522         new VFramePng(file_newfolder_dn_png));
523
524 #include "images/file_rename_up_png.h"
525 #include "images/file_rename_hi_png.h"
526 #include "images/file_rename_dn_png.h"
527 new_vframes(3,default_filebox_rename_images,
528         new VFramePng(file_rename_up_png),
529         new VFramePng(file_rename_hi_png),
530         new VFramePng(file_rename_dn_png));
531
532 #include "images/file_delete_up_png.h"
533 #include "images/file_delete_hi_png.h"
534 #include "images/file_delete_dn_png.h"
535 new_vframes(3,default_filebox_delete_images,
536         new VFramePng(file_delete_up_png),
537         new VFramePng(file_delete_hi_png),
538         new VFramePng(file_delete_dn_png));
539
540 #include "images/file_reload_up_png.h"
541 #include "images/file_reload_hi_png.h"
542 #include "images/file_reload_dn_png.h"
543 new_vframes(3,default_filebox_reload_images,
544         new VFramePng(file_reload_up_png),
545         new VFramePng(file_reload_hi_png),
546         new VFramePng(file_reload_dn_png));
547
548 #include "images/file_size_capb_up_png.h"
549 #include "images/file_size_capb_hi_png.h"
550 #include "images/file_size_capb_dn_png.h"
551 #include "images/file_size_lwrb_up_png.h"
552 #include "images/file_size_lwrb_hi_png.h"
553 #include "images/file_size_lwrb_dn_png.h"
554 #include "images/file_size_semi_up_png.h"
555 #include "images/file_size_semi_hi_png.h"
556 #include "images/file_size_semi_dn_png.h"
557 #include "images/file_size_zero_up_png.h"
558 #include "images/file_size_zero_hi_png.h"
559 #include "images/file_size_zero_dn_png.h"
560 new_vframes(12,default_filebox_szfmt_images,
561         new VFramePng(file_size_zero_up_png),
562         new VFramePng(file_size_zero_hi_png),
563         new VFramePng(file_size_zero_dn_png),
564         new VFramePng(file_size_lwrb_up_png),
565         new VFramePng(file_size_lwrb_hi_png),
566         new VFramePng(file_size_lwrb_dn_png),
567         new VFramePng(file_size_capb_up_png),
568         new VFramePng(file_size_capb_hi_png),
569         new VFramePng(file_size_capb_dn_png),
570         new VFramePng(file_size_semi_up_png),
571         new VFramePng(file_size_semi_hi_png),
572         new VFramePng(file_size_semi_dn_png));
573
574 #include "images/listbox_button_dn_png.h"
575 #include "images/listbox_button_hi_png.h"
576 #include "images/listbox_button_up_png.h"
577 #include "images/listbox_button_disabled_png.h"
578 new_vframes(4,default_listbox_button,
579         new VFramePng(listbox_button_up_png),
580         new VFramePng(listbox_button_hi_png),
581         new VFramePng(listbox_button_dn_png),
582         new VFramePng(listbox_button_disabled_png));
583
584 default_listbox_bg = 0;
585
586 #include "images/listbox_expandchecked_png.h"
587 #include "images/listbox_expandcheckedhi_png.h"
588 #include "images/listbox_expanddn_png.h"
589 #include "images/listbox_expandup_png.h"
590 #include "images/listbox_expanduphi_png.h"
591 new_vframes(5,default_listbox_expand,
592         new VFramePng(listbox_expandup_png),
593         new VFramePng(listbox_expanduphi_png),
594         new VFramePng(listbox_expandchecked_png),
595         new VFramePng(listbox_expanddn_png),
596         new VFramePng(listbox_expandcheckedhi_png));
597
598 #include "images/listbox_columnup_png.h"
599 #include "images/listbox_columnhi_png.h"
600 #include "images/listbox_columndn_png.h"
601 new_vframes(3,default_listbox_column,
602         new VFramePng(listbox_columnup_png),
603         new VFramePng(listbox_columnhi_png),
604         new VFramePng(listbox_columndn_png));
605
606 #include "images/listbox_up_png.h"
607         default_listbox_up = new VFramePng(listbox_up_png);
608
609 #include "images/listbox_dn_png.h"
610         default_listbox_dn = new VFramePng(listbox_dn_png);
611
612 #include "images/pot_hi_png.h"
613 #include "images/pot_up_png.h"
614 #include "images/pot_dn_png.h"
615 new_vframes(3,default_pot_images,
616         new VFramePng(pot_up_png),
617         new VFramePng(pot_hi_png),
618         new VFramePng(pot_dn_png));
619
620 #include "images/progress_up_png.h"
621 #include "images/progress_hi_png.h"
622 new_vframes(2,default_progress_images,
623         new VFramePng(progress_up_png),
624         new VFramePng(progress_hi_png));
625
626 #include "images/7seg_small/0_png.h"
627 #include "images/7seg_small/1_png.h"
628 #include "images/7seg_small/2_png.h"
629 #include "images/7seg_small/3_png.h"
630 #include "images/7seg_small/4_png.h"
631 #include "images/7seg_small/5_png.h"
632 #include "images/7seg_small/6_png.h"
633 #include "images/7seg_small/7_png.h"
634 #include "images/7seg_small/8_png.h"
635 #include "images/7seg_small/9_png.h"
636 #include "images/7seg_small/colon_png.h"
637 #include "images/7seg_small/period_png.h"
638 #include "images/7seg_small/a_png.h"
639 #include "images/7seg_small/b_png.h"
640 #include "images/7seg_small/c_png.h"
641 #include "images/7seg_small/d_png.h"
642 #include "images/7seg_small/e_png.h"
643 #include "images/7seg_small/f_png.h"
644 #include "images/7seg_small/space_png.h"
645 #include "images/7seg_small/dash_png.h"
646 new_vframes(20,default_medium_7segment,
647         new VFramePng(_0_png),
648         new VFramePng(_1_png),
649         new VFramePng(_2_png),
650         new VFramePng(_3_png),
651         new VFramePng(_4_png),
652         new VFramePng(_5_png),
653         new VFramePng(_6_png),
654         new VFramePng(_7_png),
655         new VFramePng(_8_png),
656         new VFramePng(_9_png),
657         new VFramePng(colon_png),
658         new VFramePng(period_png),
659         new VFramePng(a_png),
660         new VFramePng(b_png),
661         new VFramePng(c_png),
662         new VFramePng(d_png),
663         new VFramePng(e_png),
664         new VFramePng(f_png),
665         new VFramePng(space_png),
666         new VFramePng(dash_png));
667
668         type_to_icon = default_type_to_icon;
669         bar_data = default_bar;
670         check = default_check_image;
671         listbox_button = default_listbox_button;
672         listbox_bg = default_listbox_bg;
673         listbox_expand = default_listbox_expand;
674         listbox_column = default_listbox_column;
675         listbox_up = default_listbox_up;
676         listbox_dn = default_listbox_dn;
677         listbox_title_overlap = 0;
678         listbox_title_margin = 0;
679         listbox_title_color = BLACK;
680         listbox_title_hotspot = 5;
681
682         listbox_border1 = DKGREY;
683         listbox_border2_hi = RED;
684         listbox_border2 = BLACK;
685         listbox_border3_hi = RED;
686         listbox_border3 = MEGREY;
687         listbox_border4 = WHITE;
688         listbox_selected = BLUE;
689         listbox_highlighted = LTGREY;
690         listbox_inactive = WHITE;
691         listbox_text = BLACK;
692
693         pan_data = 0;
694         pan_text_color = YELLOW;
695
696         generic_button_margin = 15;
697         draw_clock_background=1;
698
699         use_shm = -1;
700         shm_reply = 1;
701
702 // Initialize
703         bg_color = ORANGE;
704         bg_shadow1 = DKGREY;
705         bg_shadow2 = BLACK;
706         bg_light1 = WHITE;
707         bg_light2 = bg_color;
708
709
710         border_light1 = bg_color;
711         border_light2 = MEGREY;
712         border_shadow1 = BLACK;
713         border_shadow2 = bg_color;
714
715         default_text_color = BLACK;
716         disabled_text_color = DMGREY;
717
718         button_light = MEGREY;           // bright corner
719 //      button_highlighted = LTGREY;  // face when highlighted
720         button_highlighted = 0xffe000;  // face when highlighted
721         button_down = MDGREY;         // face when down
722 //      button_up = MEGREY;           // face when up
723         button_up = 0xffc000;           // face when up
724         button_shadow = BLACK;       // dark corner
725         button_uphighlighted = RED;   // upper side when highlighted
726
727         tumble_data = 0;
728         tumble_duration = 150;
729
730         ok_images = default_ok_images;
731         cancel_images = default_cancel_images;
732         usethis_button_images = default_usethis_images;
733         filebox_descend_images = default_ok_images;
734
735         menu_light = LTCYAN;
736         menu_highlighted = LTBLUE;
737         menu_down = MDCYAN;
738         menu_up = MECYAN;
739         menu_shadow = DKCYAN;
740
741         menu_title_bg = default_menubar_data;
742         menu_popup_bg = default_menu_popup_bg;
743         menu_bar_bg = default_menu_bar_bg;
744
745         popupmenu_images = 0;
746
747
748         popupmenu_margin = 10;
749         popupmenu_btnup = 1;
750         popupmenu_triangle_margin = 10;
751
752         min_menu_w = 0;
753         menu_title_text = BLACK;
754         popup_title_text = BLACK;
755         menu_item_text = BLACK;
756         menu_highlighted_fontcolor = BLACK;
757         progress_text = BLACK;
758         grab_input_focus = 1;
759
760         text_default = BLACK;
761         highlight_inverse = WHITE ^ BLUE;
762         text_background = WHITE;
763         text_background_disarmed = 0xc08080;
764         text_background_hi = LTYELLOW;
765         text_background_noborder_hi = LTGREY;
766         text_background_noborder = -1;
767         text_border1 = DKGREY;
768         text_border2 = BLACK;
769         text_border2_hi = RED;
770         text_border3 = MEGREY;
771         text_border3_hi = LTPINK;
772         text_border4 = WHITE;
773         text_highlight = BLUE;
774         text_selected_highlight = SLBLUE;
775         text_inactive_highlight = MEGREY;
776
777         toggle_highlight_bg = 0;
778         toggle_text_margin = 0;
779
780 // Delays must all be different for repeaters
781         double_click = 300;
782         blink_rate = 250;
783         scroll_repeat = 150;
784         tooltip_delay = 1000;
785         tooltip_bg_color = YELLOW;
786         tooltips_enabled = 1;
787         textbox_focus_policy = 0;
788
789         filebox_margin = 110;
790         dirbox_margin = 90;
791         filebox_mode = LISTBOX_TEXT;
792         sprintf(filebox_filter, "*");
793         filebox_w = 640;
794         filebox_h = 480;
795         filebox_columntype[0] = FILEBOX_NAME;
796         filebox_columntype[1] = FILEBOX_SIZE;
797         filebox_columntype[2] = FILEBOX_DATE;
798         filebox_columntype[3] = FILEBOX_EXTENSION;
799         filebox_columnwidth[0] = 200;
800         filebox_columnwidth[1] = 100;
801         filebox_columnwidth[2] = 100;
802         filebox_columnwidth[3] = 100;
803         dirbox_columntype[0] = FILEBOX_NAME;
804         dirbox_columntype[1] = FILEBOX_DATE;
805         dirbox_columnwidth[0] = 200;
806         dirbox_columnwidth[1] = 100;
807
808         filebox_text_images = default_filebox_text_images;
809         filebox_icons_images = default_filebox_icons_images;
810         filebox_updir_images = default_filebox_updir_images;
811         filebox_newfolder_images = default_filebox_newfolder_images;
812         filebox_rename_images = default_filebox_rename_images;
813         filebox_delete_images = default_filebox_delete_images;
814         filebox_reload_images = default_filebox_reload_images;
815         filebox_szfmt_images = default_filebox_szfmt_images;
816         filebox_size_format = FILEBOX_SIZE_RAW;
817         directory_color = BLUE;
818         file_color = BLACK;
819
820         filebox_sortcolumn = 0;
821         filebox_sortorder = BC_ListBox::SORT_ASCENDING;
822         dirbox_sortcolumn = 0;
823         dirbox_sortorder = BC_ListBox::SORT_ASCENDING;
824
825
826         pot_images = default_pot_images;
827         pot_offset = 2;
828         pot_x1 = pot_images[0]->get_w() / 2 - pot_offset;
829         pot_y1 = pot_images[0]->get_h() / 2 - pot_offset;
830         pot_r = pot_x1;
831         pot_needle_color = BLACK;
832
833         progress_images = default_progress_images;
834
835         xmeter_images = 0;
836         ymeter_images = 0;
837         meter_font = SMALLFONT_3D;
838         meter_font_color = RED;
839         meter_title_w = 20;
840         meter_3d = 1;
841         medium_7segment = default_medium_7segment;
842
843         audiovideo_color = RED;
844
845         use_fontset = 0;
846
847 // Xft has priority over font set
848 #ifdef HAVE_XFT
849 // But Xft dies in 32 bit mode after some amount of drawing.
850         use_xft = 1;
851 #else
852         use_xft = 0;
853 #endif
854
855
856         drag_radius = 10;
857         recursive_resizing = 1;
858
859
860 }
861
862 void BC_Resources::del_vframes(VFrame *vframes[], int n)
863 {
864         while( --n >= 0 ) delete vframes[n];
865 }
866
867 BC_Resources::~BC_Resources()
868 {
869         delete id_lock;
870         delete create_window_lock;
871         del_vframes(default_type_to_icon, 6);
872         delete default_bar;
873         del_vframes(default_cancel_images, 3);
874         del_vframes(default_ok_images, 3);
875         del_vframes(default_usethis_images, 3);
876 #if 0
877         del_vframes(default_checkbox_images, 5);
878         del_vframes(default_radial_images, 5);
879         del_vframes(default_label_images, 5);
880 #endif
881         del_vframes(default_menuitem_data, 3);
882         del_vframes(default_menubar_data, 3);
883         delete default_menu_popup_bg;
884         delete default_menu_bar_bg;
885         delete default_check_image;
886         del_vframes(default_filebox_text_images, 3);
887         del_vframes(default_filebox_icons_images, 3);
888         del_vframes(default_filebox_updir_images, 3);
889         del_vframes(default_filebox_newfolder_images, 3);
890         del_vframes(default_filebox_rename_images, 3);
891         del_vframes(default_filebox_delete_images, 3);
892         del_vframes(default_filebox_reload_images, 3);
893         del_vframes(default_filebox_szfmt_images, 12);
894         del_vframes(default_listbox_button, 4);
895         delete default_listbox_bg;
896         del_vframes(default_listbox_expand, 5);
897         del_vframes(default_listbox_column, 3);
898         delete default_listbox_up;
899         delete default_listbox_dn;
900         del_vframes(default_pot_images, 3);
901         del_vframes(default_progress_images, 2);
902         del_vframes(default_medium_7segment, 20);
903         if( fontlist ) {
904                 fontlist->remove_all_objects();
905                 delete fontlist;
906         }
907         finit_font_defs();
908 }
909
910 int BC_Resources::initialize_display(BC_WindowBase *window)
911 {
912 // Set up IPC cleanup handlers
913 //      bc_init_ipc();
914
915 // Test for shm.  Must come before yuv test
916         init_shm(window);
917         return 0;
918 }
919
920
921 void BC_Resources::init_shm(BC_WindowBase *window)
922 {
923         use_shm = 0;
924         int (*old_handler)(Display *, XErrorEvent *) =
925                 XSetErrorHandler(BC_Resources::x_error_handler);
926
927         if(XShmQueryExtension(window->display))
928         {
929                 XShmSegmentInfo test_shm;
930                 memset(&test_shm,0,sizeof(test_shm));
931                 XImage *test_image = XShmCreateImage(window->display, window->vis,
932                         window->default_depth, ZPixmap, (char*)NULL, &test_shm, 5, 5);
933                 BC_Resources::error = 0;
934                 test_shm.shmid = shmget(IPC_PRIVATE, 5 * test_image->bytes_per_line, (IPC_CREAT | 0600));
935                 if(test_shm.shmid != -1) {
936                         char *data = (char *)shmat(test_shm.shmid, NULL, 0);
937                         if(data != (void *)-1) {
938                                 shmctl(test_shm.shmid, IPC_RMID, 0);
939                                 test_shm.shmaddr = data;
940                                 test_shm.readOnly = 0;
941
942                                 if(XShmAttach(window->display, &test_shm)) {
943                                         XSync(window->display, False);
944                                         use_shm = 1;
945                                 }
946                                 shmdt(data);
947                         }
948                 }
949
950                 XDestroyImage(test_image);
951                 if(BC_Resources::error) use_shm = 0;
952         }
953         XSetErrorHandler(old_handler);
954 }
955
956
957
958
959 BC_Synchronous* BC_Resources::get_synchronous()
960 {
961         return synchronous;
962 }
963
964 void BC_Resources::set_synchronous(BC_Synchronous *synchronous)
965 {
966         this->synchronous = synchronous;
967 }
968
969
970
971
972 int BC_Resources::get_bg_color() { return bg_color; }
973
974 int BC_Resources::get_bg_shadow1() { return bg_shadow1; }
975
976 int BC_Resources::get_bg_shadow2() { return bg_shadow2; }
977
978 int BC_Resources::get_bg_light1() { return bg_light1; }
979
980 int BC_Resources::get_bg_light2() { return bg_light2; }
981
982
983 int BC_Resources::get_id()
984 {
985         id_lock->lock("BC_Resources::get_id");
986         int result = id++;
987         id_lock->unlock();
988         return result;
989 }
990
991 int BC_Resources::get_filebox_id()
992 {
993         id_lock->lock("BC_Resources::get_filebox_id");
994         int result = filebox_id++;
995         id_lock->unlock();
996         return result;
997 }
998
999
1000 void BC_Resources::set_signals(BC_Signals *signal_handler)
1001 {
1002         BC_Resources::signal_handler = signal_handler;
1003 }
1004
1005 int BC_Resources::init_fontconfig(const char *search_path)
1006 {
1007         if( fontlist ) return 0;
1008         fontlist = new ArrayList<BC_FontEntry*>;
1009
1010 #define get_str(str,sep,ptr,cond) do { char *out = str; \
1011   while( *ptr && !strchr(sep,*ptr) && (cond) ) *out++ = *ptr++; \
1012   *out = 0; \
1013 } while(0)
1014
1015 #define skip_str(sep, ptr) do { \
1016   while( *ptr && strchr(sep,*ptr) ) ++ptr; \
1017 } while(0)
1018
1019         char find_command[BCTEXTLEN];
1020         char *fp = find_command, *ep = fp+sizeof(find_command)-1;
1021         fp += snprintf(fp, ep-fp, "%s", "find");
1022         const char *bc_font_path = getenv("BC_FONT_PATH");
1023 // if BC_FONT_PATH starts with ':', omit default path
1024         if( !(bc_font_path && bc_font_path[0] == ':') )
1025                 fp += snprintf(fp, ep-fp, " '%s'", search_path);
1026         if( bc_font_path ) {
1027                 const char *path = bc_font_path;
1028                 while( *path ) {
1029                         char font_path[BCTEXTLEN];
1030                         const char *cp = strchr(path,':');
1031                         int len = !cp ? strlen(path) : cp-path;
1032                         if( len > 0 ) {
1033                                 memcpy(font_path, path, len);
1034                                 font_path[len] = 0;  path += len;
1035                                 fp += snprintf(fp, ep-fp, " '%s'", font_path);
1036                         }
1037                         if( cp ) ++path;
1038                 }
1039         }
1040         fp += snprintf(fp, ep-fp, " -name 'fonts.scale' -print -exec cat {} \\;");
1041         FILE *in = popen(find_command, "r");
1042
1043         FT_Library freetype_library = 0;
1044 //      FT_Face freetype_face = 0;
1045 //      ft_Init_FreeType(&freetype_library);
1046
1047         char line[BCTEXTLEN], current_dir[BCTEXTLEN];
1048         current_dir[0] = 0;
1049
1050         while( !feof(in) && fgets(line, BCTEXTLEN, in) ) {
1051                 if(!strlen(line)) break;
1052
1053                 char *in_ptr = line;
1054
1055 // Get current directory
1056                 if(line[0] == '/') {
1057                         get_str(current_dir, "\n", in_ptr,1);
1058                         for( int i=strlen(current_dir); --i>=0 && current_dir[i]!='/'; )
1059                                 current_dir[i] = 0;
1060                         continue;
1061                 }
1062
1063 //printf("TitleMain::build_fonts %s\n", line);
1064                 BC_FontEntry *entry = new BC_FontEntry;
1065                 char string[BCTEXTLEN];
1066 // Path
1067                 get_str(string, "\n", in_ptr, in_ptr[0]!=' ' || in_ptr[1]!='-');
1068                 entry->path = cstrcat(2, current_dir, string);
1069 // Foundary
1070                 skip_str(" -", in_ptr);
1071                 get_str(string, "-\n", in_ptr, 1);
1072                 if( !string[0] ) { delete entry;  continue; }
1073                 entry->foundry = cstrdup(string);
1074                 if(*in_ptr == '-') in_ptr++;
1075 // Family
1076                 get_str(string, "-\n", in_ptr, 1);
1077                 if( !string[0] ) { delete entry;  continue; }
1078                 entry->family = cstrdup(string);
1079                 if(*in_ptr == '-') in_ptr++;
1080 // Weight
1081                 get_str(string, "-\n", in_ptr, 1);
1082                 entry->weight = cstrdup(string);
1083                 if(*in_ptr == '-') in_ptr++;
1084 // Slant
1085                 get_str(string, "-\n", in_ptr, 1);
1086                 entry->slant = cstrdup(string);
1087                 if(*in_ptr == '-') in_ptr++;
1088 // SWidth
1089                 get_str(string, "-\n", in_ptr, 1);
1090                 entry->swidth = cstrdup(string);
1091                 if(*in_ptr == '-') in_ptr++;
1092 // Adstyle
1093                 get_str(string, "-\n", in_ptr, 1);
1094                 entry->adstyle = cstrdup(string);
1095                 if(*in_ptr == '-') in_ptr++;
1096 // pixelsize
1097                 get_str(string, "-\n", in_ptr, 1);
1098                 entry->pixelsize = atol(string);
1099                 if(*in_ptr == '-') in_ptr++;
1100 // pointsize
1101                 get_str(string, "-\n", in_ptr, 1);
1102                 entry->pointsize = atol(string);
1103                 if(*in_ptr == '-') in_ptr++;
1104 // xres
1105                 get_str(string, "-\n", in_ptr, 1);
1106                 entry->xres = atol(string);
1107                 if(*in_ptr == '-') in_ptr++;
1108 // yres
1109                 get_str(string, "-\n", in_ptr, 1);
1110                 entry->yres = atol(string);
1111                 if(*in_ptr == '-') in_ptr++;
1112 // spacing
1113                 get_str(string, "-\n", in_ptr, 1);
1114                 entry->spacing = cstrdup(string);
1115                 if(*in_ptr == '-') in_ptr++;
1116 // avg_width
1117                 get_str(string, "-\n", in_ptr, 1);
1118                 entry->avg_width = atol(string);
1119                 if(*in_ptr == '-') in_ptr++;
1120 // registry
1121                 get_str(string, "-\n", in_ptr, 1);
1122                 entry->registry = cstrdup(string);
1123                 if(*in_ptr == '-') in_ptr++;
1124 // encoding
1125                 get_str(string, "-\n", in_ptr, 1);
1126                 entry->encoding = cstrdup(string);
1127                 if(*in_ptr == '-') in_ptr++;
1128
1129 // Add to list
1130 //printf("TitleMain::build_fonts 1 %s\n", entry->path);
1131 // This takes a real long time to do.  Instead just take all fonts
1132 //              if(!load_freetype_face(freetype_library,
1133 //                      freetype_face, entry->path) )
1134 // Fix parameters
1135                 sprintf(line, "%s (%s)", entry->family, entry->foundry);
1136                 entry->displayname = cstrdup(line);
1137
1138                 if(!strcasecmp(entry->weight, "demibold")) {
1139                         entry->fixed_style |= BC_FONT_BOLD;
1140                         entry->style |= FL_WEIGHT_DEMIBOLD;
1141                 }
1142                 else if(!strcasecmp(entry->weight, "bold")) {
1143                         entry->fixed_style |= BC_FONT_BOLD;
1144                         entry->style |= FL_WEIGHT_BOLD;
1145                 }
1146                 else {
1147                         entry->style |= FL_WEIGHT_NORMAL;
1148                 }
1149
1150                 if(!strcasecmp(entry->slant, "r")) {
1151                         entry->style |= FL_SLANT_ROMAN;
1152                 }
1153                 else if(!strcasecmp(entry->slant, "i")) {
1154                         entry->style |= FL_SLANT_ITALIC;
1155                         entry->fixed_style |= BC_FONT_ITALIC;
1156                 }
1157                 else if(!strcasecmp(entry->slant, "o")) {
1158                         entry->style |= FL_SLANT_OBLIQUE;
1159                         entry->fixed_style |= BC_FONT_ITALIC;
1160                 }
1161
1162                 if(!strcasecmp(entry->swidth, "normal"))
1163                         entry->style |= FL_WIDTH_NORMAL;
1164                 else if(!strcasecmp(entry->swidth, "ultracondensed"))
1165                         entry->style |= FL_WIDTH_ULTRACONDENSED;
1166                 else if(!strcasecmp(entry->swidth, "extracondensed"))
1167                         entry->style |= FL_WIDTH_EXTRACONDENSED;
1168                 else if(!strcasecmp(entry->swidth, "condensed"))
1169                         entry->style |= FL_WIDTH_CONDENSED;
1170                 else if(!strcasecmp(entry->swidth, "semicondensed"))
1171                         entry->style |= FL_WIDTH_SEMICONDENSED;
1172                 else if(!strcasecmp(entry->swidth, "semiexpanded"))
1173                         entry->style |= FL_WIDTH_SEMIEXPANDED;
1174                 else if(!strcasecmp(entry->swidth, "expanded"))
1175                         entry->style |= FL_WIDTH_EXPANDED;
1176                 else if(!strcasecmp(entry->swidth, "extraexpanded"))
1177                         entry->style |= FL_WIDTH_EXTRAEXPANDED;
1178                 else if(!strcasecmp(entry->swidth, "ultraexpanded"))
1179                         entry->style |= FL_WIDTH_ULTRAEXPANDED;
1180                 else
1181                         entry->style |= FL_WIDTH_NORMAL;
1182
1183                 fontlist->append(entry);
1184                 if( font_debug )
1185                         dump_font_entry(stdout, "font 0: ", entry);
1186
1187 //              printf("TitleMain::build_fonts %s: success\n",  entry->path);
1188 //printf("TitleMain::build_fonts 2\n");
1189         }
1190         pclose(in);
1191
1192         if( bc_font_path && bc_font_path[0] == ':' )
1193                 return 0;
1194
1195 // Load all the fonts from fontconfig
1196         FcPattern *pat;
1197         FcFontSet *fs;
1198         FcObjectSet *os;
1199         FcChar8 *family, *file, *foundry, *style, *format;
1200         int slant, spacing, width, weight;
1201         int force_style = 0;
1202 // if you want limit search to TrueType put 1
1203         int limit_to_trutype = 1;
1204         FcConfig *config;
1205         int i;
1206         char tmpstring[BCTEXTLEN];
1207         if(!fcInit())
1208                 return 1;
1209         config = fcConfigGetCurrent();
1210         fcConfigSetRescanInterval(config, 0);
1211
1212         pat = fcPatternCreate();
1213         os = fcObjectSetBuild( FC_FAMILY, FC_FILE, FC_FOUNDRY, FC_WEIGHT,
1214                 FC_WIDTH, FC_SLANT, FC_FONTFORMAT, FC_SPACING, FC_STYLE, (char *) 0);
1215         fcPatternAddBool(pat, FC_SCALABLE, true);
1216
1217         if(language[0]) {
1218                 char langstr[LEN_LANG * 3];
1219                 strcpy(langstr, language);
1220
1221                 if(region[0]) {
1222                         strcat(langstr, "-");
1223                         strcat(langstr, region);
1224                 }
1225
1226                 FcLangSet *ls =  fcLangSetCreate();
1227                 if(fcLangSetAdd(ls, (const FcChar8*)langstr))
1228                 if(fcPatternAddLangSet(pat, FC_LANG, ls))
1229                 fcLangSetDestroy(ls);
1230         }
1231
1232         fs = fcFontList(config, pat, os);
1233         fcPatternDestroy(pat);
1234         fcObjectSetDestroy(os);
1235
1236         for (i = 0; fs && i < fs->nfont; i++) {
1237                 FcPattern *font = fs->fonts[i];
1238                 force_style = 0;
1239                 fcPatternGetString(font, FC_FONTFORMAT, 0, &format);
1240                 //on this point you can limit font search
1241                 if(limit_to_trutype && strcmp((char *)format, "TrueType"))
1242                         continue;
1243
1244                 sprintf(tmpstring, "%s", format);
1245                 BC_FontEntry *entry = new BC_FontEntry;
1246                 if(fcPatternGetString(font, FC_FILE, 0, &file) == FcResultMatch) {
1247                         entry->path = cstrdup((char*)file);
1248                 }
1249
1250                 if(fcPatternGetString(font, FC_FOUNDRY, 0, &foundry) == FcResultMatch) {
1251                         entry->foundry = cstrdup((char*)foundry);
1252                 }
1253
1254                 if(fcPatternGetInteger(font, FC_WEIGHT, 0, &weight) == FcResultMatch) {
1255                         switch(weight) {
1256                         case FC_WEIGHT_THIN:
1257                         case FC_WEIGHT_EXTRALIGHT:
1258                         case FC_WEIGHT_LIGHT:
1259                         case FC_WEIGHT_BOOK:
1260                                 force_style = 1;
1261                                 entry->weight = cstrdup("medium");
1262                                 break;
1263
1264                         case FC_WEIGHT_NORMAL:
1265                         case FC_WEIGHT_MEDIUM:
1266                         default:
1267                                 entry->weight = cstrdup("medium");
1268                                 break;
1269
1270                         case FC_WEIGHT_BLACK:
1271                         case FC_WEIGHT_SEMIBOLD:
1272                         case FC_WEIGHT_BOLD:
1273                                 entry->weight = cstrdup("bold");
1274                                 entry->fixed_style |= BC_FONT_BOLD;
1275                                 break;
1276
1277                         case FC_WEIGHT_EXTRABOLD:
1278                         case FC_WEIGHT_EXTRABLACK:
1279                                 force_style = 1;
1280                                 entry->weight = cstrdup("bold");
1281                                 entry->fixed_style |= BC_FONT_BOLD;
1282                                 break;
1283                         }
1284                 }
1285
1286                 if(fcPatternGetString(font, FC_FAMILY, 0, &family) == FcResultMatch)
1287                         entry->family = cstrdup((char*)family);
1288
1289                 if(fcPatternGetInteger(font, FC_SLANT, 0, &slant) == FcResultMatch) {
1290                         switch(slant) {
1291                         case FC_SLANT_ROMAN:
1292                         default:
1293                                 entry->slant = cstrdup("r");
1294                                 entry->style |= FL_SLANT_ROMAN;
1295                                 break;
1296                         case FC_SLANT_ITALIC:
1297                                 entry->slant = cstrdup("i");
1298                                 entry->style |= FL_SLANT_ITALIC;
1299                                 entry->fixed_style |= BC_FONT_ITALIC;
1300                                 break;
1301                         case FC_SLANT_OBLIQUE:
1302                                 entry->slant = cstrdup("o");
1303                                 entry->style |= FL_SLANT_OBLIQUE;
1304                                 entry->fixed_style |= BC_FONT_ITALIC;
1305                                 break;
1306                         }
1307                 }
1308
1309                 if(fcPatternGetInteger(font, FC_WIDTH, 0, &width) == FcResultMatch) {
1310                         switch(width) {
1311                         case FC_WIDTH_ULTRACONDENSED:
1312                                 entry->swidth = cstrdup("ultracondensed");
1313                                 break;
1314
1315                         case FC_WIDTH_EXTRACONDENSED:
1316                                 entry->swidth = cstrdup("extracondensed");
1317                                 break;
1318
1319                         case FC_WIDTH_CONDENSED:
1320                                 entry->swidth = cstrdup("condensed");
1321                                 break;
1322                         case FC_WIDTH_SEMICONDENSED:
1323                                 entry->swidth = cstrdup("semicondensed");
1324                                 break;
1325
1326                         case FC_WIDTH_NORMAL:
1327                         default:
1328                                 entry->swidth = cstrdup("normal");
1329                                 break;
1330
1331                         case FC_WIDTH_SEMIEXPANDED:
1332                                 entry->swidth = cstrdup("semiexpanded");
1333                                 break;
1334
1335                         case FC_WIDTH_EXPANDED:
1336                                 entry->swidth = cstrdup("expanded");
1337                                 break;
1338
1339                         case FC_WIDTH_EXTRAEXPANDED:
1340                                 entry->swidth = cstrdup("extraexpanded");
1341                                 break;
1342
1343                         case FC_WIDTH_ULTRAEXPANDED:
1344                                 entry->swidth = cstrdup("ultraexpanded");
1345                                 break;
1346                         }
1347                 }
1348
1349                 if(fcPatternGetInteger(font, FC_SPACING, 0, &spacing) == FcResultMatch) {
1350                         switch(spacing) {
1351                         case 0:
1352                         default:
1353                                 entry->spacing = cstrdup("p");
1354                                 break;
1355
1356                         case 90:
1357                                 entry->spacing = cstrdup("d");
1358                                 break;
1359
1360                         case 100:
1361                                 entry->spacing = cstrdup("m");
1362                                 break;
1363
1364                         case 110:
1365                                 entry->spacing = cstrdup("c");
1366                                 break;
1367                         }
1368                 }
1369
1370                 // Add fake stuff for compatibility
1371                 entry->adstyle = cstrdup(" ");
1372                 entry->pixelsize = 0;
1373                 entry->pointsize = 0;
1374                 entry->xres = 0;
1375                 entry->yres = 0;
1376                 entry->avg_width = 0;
1377                 entry->registry = cstrdup("utf");
1378                 entry->encoding = cstrdup("8");
1379
1380                 if( fcPatternGetString(font, FC_STYLE, 0, &style) != FcResultMatch )
1381                         force_style = 0;
1382
1383                 // If font has a style unmanaged by titler plugin, force style to be displayed on name
1384                 // in this way we can shown all available fonts styles.
1385                 if(force_style) {
1386                         sprintf(tmpstring, "%s (%s)", entry->family, style);
1387                         entry->displayname = cstrdup(tmpstring);
1388                 }
1389                 else {
1390                         if(strcmp(entry->foundry, "unknown")) {
1391                                 sprintf(tmpstring, "%s (%s)", entry->family, entry->foundry);
1392                                 entry->displayname = cstrdup(tmpstring);
1393                         }
1394                         else {
1395                                 sprintf(tmpstring, "%s", entry->family);
1396                                 entry->displayname = cstrdup(tmpstring);
1397                         }
1398
1399                 }
1400                 fontlist->append(entry);
1401                 if( font_debug )
1402                         dump_font_entry(stdout, "font 1: ", entry);
1403         }
1404
1405         fcFontSetDestroy(fs);
1406         if(freetype_library)
1407                 ft_Done_FreeType(freetype_library);
1408 // for(int i = 0; i < fonts->total; i++)
1409 //      fonts->values[i]->dump();
1410
1411         fcConfigAppFontAddDir(0, (const FcChar8*)search_path);
1412         fcConfigSetRescanInterval(0, 0);
1413
1414         os = fcObjectSetBuild(FC_FAMILY, FC_FILE, FC_FOUNDRY, FC_WEIGHT,
1415                 FC_WIDTH, FC_SLANT, FC_SPACING, FC_STYLE, (char *)0);
1416         pat = fcPatternCreate();
1417         fcPatternAddBool(pat, FC_SCALABLE, true);
1418
1419         if(language[0])
1420         {
1421                 char langstr[LEN_LANG * 3];
1422                 strcpy(langstr, language);
1423
1424                 if(region[0])
1425                 {
1426                         strcat(langstr, "-");
1427                         strcat(langstr, region);
1428                 }
1429
1430                 FcLangSet *ls =  fcLangSetCreate();
1431                 if(fcLangSetAdd(ls, (const FcChar8*)langstr))
1432                         if(fcPatternAddLangSet(pat, FC_LANG, ls))
1433                 fcLangSetDestroy(ls);
1434         }
1435
1436         fs = fcFontList(0, pat, os);
1437         fcPatternDestroy(pat);
1438         fcObjectSetDestroy(os);
1439
1440         for(int i = 0; i < fs->nfont; i++)
1441         {
1442                 FcPattern *font = fs->fonts[i];
1443                 BC_FontEntry *entry = new BC_FontEntry;
1444
1445                 FcChar8 *strvalue;
1446                 if(fcPatternGetString(font, FC_FILE, 0, &strvalue) == FcResultMatch)
1447                 {
1448                         entry->path = new char[strlen((char*)strvalue) + 1];
1449                         strcpy(entry->path, (char*)strvalue);
1450                 }
1451
1452                 if(fcPatternGetString(font, FC_FOUNDRY, 0, &strvalue) == FcResultMatch)
1453                 {
1454                         entry->foundry = new char[strlen((char*)strvalue) + 1];
1455                         strcpy(entry->foundry, (char *)strvalue);
1456                 }
1457
1458                 if(fcPatternGetString(font, FC_FAMILY, 0, &strvalue) == FcResultMatch)
1459                 {
1460                         entry->family = new char[strlen((char*)strvalue) + 2];
1461                         strcpy(entry->family, (char*)strvalue);
1462                 }
1463
1464                 int intvalue;
1465                 if(fcPatternGetInteger(font, FC_SLANT, 0, &intvalue) == FcResultMatch)
1466                 {
1467                         switch(intvalue)
1468                         {
1469                         case FC_SLANT_ROMAN:
1470                         default:
1471                                 entry->style |= FL_SLANT_ROMAN;
1472                                 break;
1473
1474                         case FC_SLANT_ITALIC:
1475                                 entry->style |= FL_SLANT_ITALIC;
1476                                 break;
1477
1478                         case FC_SLANT_OBLIQUE:
1479                                 entry->style |= FL_SLANT_OBLIQUE;
1480                                 break;
1481                         }
1482                 }
1483
1484                 if(fcPatternGetInteger(font, FC_WEIGHT, 0, &intvalue) == FcResultMatch)
1485                 {
1486                         switch(intvalue)
1487                         {
1488                         case FC_WEIGHT_THIN:
1489                                 entry->style |= FL_WEIGHT_THIN;
1490                                 break;
1491
1492                         case FC_WEIGHT_EXTRALIGHT:
1493                                 entry->style |= FL_WEIGHT_EXTRALIGHT;
1494                                 break;
1495
1496                         case FC_WEIGHT_LIGHT:
1497                                 entry->style |= FL_WEIGHT_LIGHT;
1498                                 break;
1499
1500                         case FC_WEIGHT_BOOK:
1501                                 entry->style |= FL_WEIGHT_BOOK;
1502                                 break;
1503
1504                         case FC_WEIGHT_NORMAL:
1505                         default:
1506                                 entry->style |= FL_WEIGHT_NORMAL;
1507                                 break;
1508
1509                         case FC_WEIGHT_MEDIUM:
1510                                 entry->style |= FL_WEIGHT_MEDIUM;
1511                                 break;
1512
1513                         case FC_WEIGHT_DEMIBOLD:
1514                                 entry->style |= FL_WEIGHT_DEMIBOLD;
1515                                 break;
1516
1517                         case FC_WEIGHT_BOLD:
1518                                 entry->style |= FL_WEIGHT_BOLD;
1519                                 break;
1520
1521                         case FC_WEIGHT_EXTRABOLD:
1522                                 entry->style |= FL_WEIGHT_EXTRABOLD;
1523                                 break;
1524
1525                         case FC_WEIGHT_BLACK:
1526                                 entry->style |= FL_WEIGHT_BLACK;
1527                                 break;
1528
1529                         case FC_WEIGHT_EXTRABLACK:
1530                                 entry->style |= FL_WEIGHT_EXTRABLACK;
1531                                 break;
1532                         }
1533                 }
1534
1535                 if(fcPatternGetInteger(font, FC_WIDTH, 0, &intvalue) == FcResultMatch)
1536                 {
1537                         switch(intvalue)
1538                         {
1539                         case FC_WIDTH_ULTRACONDENSED:
1540                                 entry->style |= FL_WIDTH_ULTRACONDENSED;
1541                                 break;
1542
1543                         case FC_WIDTH_EXTRACONDENSED:
1544                                 entry->style |= FL_WIDTH_EXTRACONDENSED;
1545                                 break;
1546
1547                         case FC_WIDTH_CONDENSED:
1548                                 entry->style |= FL_WIDTH_CONDENSED;
1549                                 break;
1550
1551                         case FC_WIDTH_SEMICONDENSED:
1552                                 entry->style = FL_WIDTH_SEMICONDENSED;
1553                                 break;
1554
1555                         case FC_WIDTH_NORMAL:
1556                         default:
1557                                 entry->style |= FL_WIDTH_NORMAL;
1558                                 break;
1559
1560                         case FC_WIDTH_SEMIEXPANDED:
1561                                 entry->style |= FL_WIDTH_SEMIEXPANDED;
1562                                 break;
1563
1564                         case FC_WIDTH_EXPANDED:
1565                                 entry->style |= FL_WIDTH_EXPANDED;
1566                                 break;
1567
1568                         case FC_WIDTH_EXTRAEXPANDED:
1569                                 entry->style |= FL_WIDTH_EXTRAEXPANDED;
1570                                 break;
1571
1572                         case FC_WIDTH_ULTRAEXPANDED:
1573                                 entry->style |= FL_WIDTH_ULTRAEXPANDED;
1574                                 break;
1575                         }
1576                 }
1577                 if(fcPatternGetInteger(font, FC_SPACING, 0, &intvalue) == FcResultMatch)
1578                 {
1579                         switch(intvalue)
1580                         {
1581                         case FC_PROPORTIONAL:
1582                         default:
1583                                 entry->style |= FL_PROPORTIONAL;
1584                                 break;
1585
1586                         case FC_DUAL:
1587                                 entry->style |= FL_DUAL;
1588                                 break;
1589
1590                         case FC_MONO:
1591                                 entry->style |= FL_MONO;
1592                                 break;
1593
1594                         case FC_CHARCELL:
1595                                 entry->style |= FL_CHARCELL;
1596                                 break;
1597                         }
1598                 }
1599                 if(entry->foundry && strcmp(entry->foundry, "unknown"))
1600                 {
1601                         char tempstr[BCTEXTLEN];
1602                         sprintf(tempstr, "%s (%s)", entry->family, entry->foundry);
1603                         entry->displayname = new char[strlen(tempstr) + 1];
1604                         strcpy(entry->displayname, tempstr);
1605                 }
1606                 else
1607                 {
1608                         entry->displayname = new char[strlen(entry->family) + 1];
1609                         strcpy(entry->displayname, entry->family);
1610                 }
1611                 fontlist->append(entry);
1612                 if( font_debug )
1613                         dump_font_entry(stdout, "font 2: ", entry);
1614         }
1615         fcFontSetDestroy(fs);
1616         return 0;
1617 }
1618
1619 #define STYLE_MATCH(fst, stl, msk) ((fst) & (msk) & (stl)) && \
1620        !((fst) & ~(style) & (msk))
1621
1622 BC_FontEntry *BC_Resources::find_fontentry(const char *displayname, int style,
1623         int mask, int preferred)
1624 {
1625         BC_FontEntry *entry, *style_match, *preferred_match;
1626
1627         if(!fontlist)
1628                 return 0;
1629
1630         style_match = 0;
1631         preferred_match = 0;
1632
1633         if(displayname)
1634         {
1635                 for(int i = 0; i < fontlist->total; i++)
1636                 {
1637                         entry = fontlist->values[i];
1638
1639                         if(strcmp(entry->displayname, displayname) == 0 &&
1640                                         STYLE_MATCH(entry->style, style, mask))
1641                         {
1642                                 if(!style_match)
1643                                         style_match = entry;
1644                                 if(!preferred_match && entry->fixed_style == preferred)
1645                                         preferred_match = entry;
1646                         }
1647                 }
1648                 if(preferred_match)
1649                         return preferred_match;
1650
1651                 if(style_match)
1652                         return style_match;
1653         }
1654
1655 // No exact match - assume normal width font
1656         style |= FL_WIDTH_NORMAL;
1657         mask |= FL_WIDTH_MASK;
1658         style_match = 0;
1659         preferred_match = 0;
1660
1661         for(int i = 0; i < fontlist->total; i++)
1662         {
1663                 entry = fontlist->values[i];
1664
1665                 if(STYLE_MATCH(entry->style, style, mask))
1666                 {
1667                         if(!style_match)
1668                                 style_match = entry;
1669
1670                         if(!preferred_match && (entry->style & preferred))
1671                                 preferred_match = entry;
1672
1673                         if(!strncasecmp(displayname, entry->family,
1674                                         strlen(entry->family)))
1675                         return entry;
1676                 }
1677         }
1678
1679         if(preferred_match)
1680                 return preferred_match;
1681
1682         return style_match;
1683 }
1684
1685
1686 class utf8conv {
1687         uint8_t *obfr, *out, *oend;
1688         uint8_t *ibfr, *inp, *iend;
1689 public:
1690         utf8conv(void *out, int olen, void *inp, int ilen) {
1691                 this->obfr = this->out = (uint8_t*)out;
1692                 this->oend = this->out + olen;
1693                 this->ibfr = this->inp = (uint8_t*)inp;
1694                 this->iend = this->inp + ilen;
1695         }
1696         int cur() { return inp>=iend ? -1 : *inp; }
1697         int next() { return inp>=iend ? -1 : *inp++; }
1698         int next(int ch) { return out>=oend ? -1 : *out++ = ch; }
1699         int ilen() { return inp-ibfr; }
1700         int olen() { return out-obfr; }
1701         int wnext();
1702         int wnext(unsigned int v);
1703 };
1704
1705 int utf8conv::
1706 wnext(unsigned int v)
1707 {
1708   if( v < 0x00000080 ) { next(v);  return 1; }
1709   int n = v < 0x00000800 ? 2 : v < 0x00010000 ? 3 :
1710           v < 0x00200000 ? 4 : v < 0x04000000 ? 5 : 6;
1711   int m = (0xff00 >> n), i = n-1;
1712   next((v>>(6*i)) | m);
1713   while( --i >= 0 ) next(((v>>(6*i)) & 0x3f) | 0x80);
1714   return n;
1715 }
1716
1717 int utf8conv::
1718 wnext()
1719 {
1720   int v = 0, n = 0, ch = next();
1721   if( ch == '\\' ) {
1722     switch( (ch=next()) ) {
1723     case 'n': return '\n';
1724     case 't': return '\t';
1725     case 'r': return '\r';
1726     case 'b': return '\b';
1727     case 'f': return '\f';
1728     case 'v': return '\v';
1729     case 'a': return '\a';
1730     case '0': case '1': case '2': case '3':
1731     case '4': case '5': case '6': case '7':
1732       v = ch - '0';
1733       for( int i=3; --i>0; v=v*8+ch, next() )
1734         if( (ch=cur()-'0') < 0 || ch >= 8 ) break;
1735       return v;
1736     case 'x':  n = 2;  break;
1737     case 'u':  n = 4;  break;
1738     case 'U':  n = 8;  break;
1739     default: return ch;
1740     }
1741     for( int i=n; --i>=0; v=v*16+ch, next() ) {
1742       if( (ch=cur()-'0')>=0 && ch<10 ) continue;
1743       if( (ch-='A'-'0'-10)>=10 && ch<16 ) continue;
1744       if( (ch-='a'-'A')<10 || ch>=16 ) break;
1745     }
1746   }
1747   else if( ch >= 0x80 ) {
1748     static const unsigned char byts[] = {
1749       1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 4, 5,
1750     };
1751     int i = ch - 0xc0;
1752     n = i<0 ? 0 : byts[i/4];
1753     for( v=ch, i=n; --i>=0; v+=next() ) v <<= 6;
1754     static const unsigned int ofs[6] = {
1755       0x00000000U, 0x00003080U, 0x000E2080U,
1756       0x03C82080U, 0xFA082080U, 0x82082080U
1757     };
1758     v -= ofs[n];
1759   }
1760   else
1761     v = ch;
1762   return v;
1763 }
1764
1765
1766 size_t BC_Resources::encode(const char *from_enc, const char *to_enc,
1767         char *input, int input_length, char *output, int output_length)
1768 {
1769         if( !from_enc || *from_enc == 0 ||
1770             !strcmp(from_enc,"UTF8") || !strcmp(from_enc, "US-ASCII") )
1771                 from_enc = "UTF-8";
1772
1773         if( !to_enc || *to_enc == 0 ||
1774             !strcmp(to_enc,"UTF8") || !strcmp(to_enc, "US-ASCII") )
1775                 to_enc = "UTF-8";
1776
1777         iconv_t cd;
1778         char *outbase = output;
1779         size_t inbytes = input_length < 0 ? strlen(input) : input_length;
1780         size_t outbytes = output_length;
1781
1782         if( inbytes && outbytes ) {
1783                 if( !strcmp(from_enc, to_enc) ) {
1784                         if( inbytes > outbytes ) inbytes = outbytes;
1785                         memcpy(output,  input, inbytes);
1786                         output += inbytes;
1787                         outbytes -= inbytes;
1788                 }
1789                 else if( !strcmp(from_enc, "UTF-8") && !strcmp(to_enc,"UTF32LE") ) {
1790                         utf8conv uc(0,0, input,inbytes);
1791                         uint32_t *op = (uint32_t *)output;
1792                         uint32_t *ep = (uint32_t *)(output+output_length);
1793                         for( int wch; op<ep && (wch=uc.wnext())>=0; *op++=wch );
1794                         output = (char *)op;
1795                         outbytes = (char*)ep - output;
1796                 }
1797                 else if( !strcmp(from_enc, "UTF32LE") && !strcmp(to_enc,"UTF-8") ) {
1798                         utf8conv uc(output,output_length, 0,0);
1799                         uint32_t *ip = (uint32_t *)input;
1800                         uint32_t *ep = (uint32_t *)(input+inbytes);
1801                         for( ; ip<ep && uc.wnext(*ip)>=0; ++ip );
1802                         output += uc.olen();
1803                         outbytes -= uc.olen();
1804                 }
1805                 else if( (cd = iconv_open(to_enc, from_enc)) != (iconv_t)-1 ) {
1806                         iconv(cd, &input, &inbytes, &output, &outbytes);
1807                         iconv_close(cd);
1808                 }
1809                 else {
1810                         printf(_("Conversion from %s to %s is not available\n"),
1811                                 from_enc, to_enc);
1812                 }
1813         }
1814         if( outbytes > sizeof(uint32_t) )
1815                 outbytes = sizeof(uint32_t);
1816         for( uint32_t i = 0; i < outbytes; i++)
1817                 output[i] = 0;
1818         return output - outbase;
1819 }
1820
1821 void BC_Resources::encode_to_utf8(char *buffer, int buflen)
1822 {
1823         if(BC_Resources::locale_utf8) return;
1824         char lbuf[buflen];
1825         encode(encoding, 0, buffer, buflen, lbuf, buflen);
1826         strcpy(buffer, lbuf);
1827 }
1828
1829 int BC_Resources::find_font_by_char(FT_ULong char_code, char *path_new, const FT_Face oldface)
1830 {
1831         FcPattern *font, *ofont;
1832         FcChar8 *file;
1833         int result = 0;
1834
1835         *path_new = 0;
1836
1837         // Do not search control codes
1838         if(char_code < ' ')
1839                 return 0;
1840
1841         if( (ofont = fcFreeTypeQueryFace(oldface, (const FcChar8*)"", 4097, 0)) != 0 ) {
1842                 if( (font = find_similar_font(char_code, ofont)) != 0 ) {
1843                         if(fcPatternGetString(font, FC_FILE, 0, &file) == FcResultMatch) {
1844                                 strcpy(path_new, (char*)file);
1845                                 result = 1;
1846                         }
1847                         fcPatternDestroy(font);
1848                 }
1849                 fcPatternDestroy(ofont);
1850         }
1851         return result;
1852 }
1853
1854 FcPattern* BC_Resources::find_similar_font(FT_ULong char_code, FcPattern *oldfont)
1855 {
1856         FcPattern *pat, *font;
1857         FcFontSet *fs;
1858         FcObjectSet *os;
1859         FcCharSet *fcs;
1860         int ival;
1861
1862         // Do not search control codes
1863         if(char_code < ' ')
1864                 return 0;
1865
1866         fontconfig_lock.lock("BC_Resources::find_similar_font");
1867         pat = fcPatternCreate();
1868         os = fcObjectSetBuild(FC_FILE, FC_CHARSET, FC_SCALABLE, FC_FAMILY,
1869                 FC_SLANT, FC_WEIGHT, FC_WIDTH, (char *)0);
1870
1871         fcPatternAddBool(pat, FC_SCALABLE, true);
1872         fcs = fcCharSetCreate();
1873         if(fcCharSetAddChar(fcs, char_code))
1874                 fcPatternAddCharSet(pat, FC_CHARSET, fcs);
1875         fcCharSetDestroy(fcs);
1876         for( int i=0; i<(int)LEN_FCPROP; ++i ) {
1877                 if(fcPatternGetInteger(oldfont, fc_properties[i], 0, &ival) == FcResultMatch)
1878                         fcPatternAddInteger(pat, fc_properties[i], ival);
1879         }
1880         fs = fcFontList(0, pat, os);
1881         for( int i=LEN_FCPROP; --i>=0 && !fs->nfont; ) {
1882                 fcFontSetDestroy(fs);
1883                 fcPatternDel(pat, fc_properties[i]);
1884                 fs = fcFontList(0, pat, os);
1885         }
1886         fcPatternDestroy(pat);
1887         fcObjectSetDestroy(os);
1888
1889         pat = 0;
1890
1891         for (int i = 0; i < fs->nfont; i++)
1892         {
1893                 font = fs->fonts[i];
1894                 if(fcPatternGetCharSet(font, FC_CHARSET, 0, &fcs) == FcResultMatch)
1895                 {
1896                         if(fcCharSetHasChar(fcs, char_code))
1897                         {
1898                                 pat =  fcPatternDuplicate(font);
1899                                 break;
1900                         }
1901                 }
1902         }
1903         fcFontSetDestroy(fs);
1904         fontconfig_lock.unlock();
1905
1906         return pat;
1907 }
1908
1909 void BC_Resources::dump_fonts(FILE *fp)
1910 {
1911         for( int i=0; i<fontlist->total; ++i )
1912                 dump_font_entry(fp, "", fontlist->values[i]);
1913 }
1914
1915 void BC_Resources::dump_font_entry(FILE *fp, const char *cp,  BC_FontEntry *ep)
1916 {
1917         fprintf(fp,"%s%s = %s\n",cp,ep->displayname,ep->path);
1918         fprintf(fp,"  %s:%s:%s:%s:%s:%s:%d:%d:%d:%d:%d:%s:%d:%s:%s:%d\n",
1919                 ep->foundry, ep->family, ep->weight, ep->slant, ep->swidth, ep->adstyle,
1920                 ep->pixelsize, ep->pointsize, ep->xres, ep->yres, ep->style, ep->spacing,
1921                 ep->avg_width, ep->registry, ep->encoding, ep->fixed_style);
1922 }
1923