fix valgrind memory leaks/reports, add cache to dump, fix hw probe frame leak, startu...
[goodguy/cinelerra.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 VFrame *BC_Resources::default_vscroll_data[10] = { 0, };
362 VFrame *BC_Resources::default_hscroll_data[10] = { 0, };
363 VFrame *BC_Resources::default_icon_img = 0;
364
365 BC_Resources::BC_Resources(float x_scale, float y_scale)
366 {
367         BC_WindowBase::resources = this;
368         synchronous = 0;
369         vframe_shm = 0;
370         use_shm = -1;
371         shm_reply = 1;
372
373         if( x_scale <= 0 ) x_scale = 1;
374         if( y_scale <= 0 ) y_scale = x_scale;
375         this->x_scale = x_scale;
376         this->y_scale = y_scale;
377         float default_scale = bmin(x_scale, y_scale);
378         const char *env = getenv("BC_FONT_DEBUG");
379         font_debug = env ? atoi(env) : 0;
380         env = getenv("BC_FONT_SCALE");
381         font_scale = env ? atof(env) : default_scale;
382         if( font_scale <= 0 ) font_scale = 1;
383         init_font_defs(font_scale);
384         env = getenv("BC_ICON_SCALE");
385         icon_scale = env ? atof(env) : default_scale;
386         if( icon_scale <= 0 ) icon_scale = 1;
387
388         id_lock = new Mutex("BC_Resources::id_lock");
389         create_window_lock = new Mutex("BC_Resources::create_window_lock", 1);
390         id = 0;
391         machine_cpus = get_machine_cpus();
392
393         for(int i = 0; i < FILEBOX_HISTORY_SIZE; i++)
394                 filebox_history[i].path[0] = 0;
395
396 #ifdef HAVE_XFT
397         xftInit(0);
398         xftInitFtLibrary();
399 #endif
400
401         little_endian = (*(const u_int32_t*)"\01\0\0\0") & 1;
402         wide_encoding = little_endian ?  "UTF32LE" : "UTF32BE";
403         use_xvideo = 1;
404
405 #include "images/file_folder_png.h"
406 #include "images/file_unknown_png.h"
407 #include "images/file_film_png.h"
408 #include "images/file_sound_png.h"
409 #include "images/file_label_png.h"
410 #include "images/file_column_png.h"
411 new_vframes(6,default_type_to_icon,
412         new VFramePng(file_folder_png),
413         new VFramePng(file_unknown_png),
414         new VFramePng(file_film_png),
415         new VFramePng(file_sound_png),
416         new VFramePng(file_label_png),
417         new VFramePng(file_column_png));
418
419 #include "images/bar_png.h"
420         default_bar = new VFramePng(bar_png);
421
422 #include "images/cancel_up_png.h"
423 #include "images/cancel_hi_png.h"
424 #include "images/cancel_dn_png.h"
425 new_vframes(3,default_cancel_images,
426         new VFramePng(cancel_up_png),
427         new VFramePng(cancel_hi_png),
428         new VFramePng(cancel_dn_png));
429
430 #include "images/ok_up_png.h"
431 #include "images/ok_hi_png.h"
432 #include "images/ok_dn_png.h"
433 new_vframes(3,default_ok_images,
434         new VFramePng(ok_up_png),
435         new VFramePng(ok_hi_png),
436         new VFramePng(ok_dn_png));
437
438 #include "images/usethis_up_png.h"
439 #include "images/usethis_uphi_png.h"
440 #include "images/usethis_dn_png.h"
441 new_vframes(3,default_usethis_images,
442         new VFramePng(usethis_up_png),
443         new VFramePng(usethis_uphi_png),
444         new VFramePng(usethis_dn_png));
445
446 #if 0
447 #include "images/checkbox_checked_png.h"
448 #include "images/checkbox_dn_png.h"
449 #include "images/checkbox_checkedhi_png.h"
450 #include "images/checkbox_up_png.h"
451 #include "images/checkbox_hi_png.h"
452 new_vframes(5,default_checkbox_images,
453         new VFramePng(checkbox_up_png),
454         new VFramePng(checkbox_hi_png),
455         new VFramePng(checkbox_checked_png),
456         new VFramePng(checkbox_dn_png),
457         new VFramePng(checkbox_checkedhi_png));
458
459 #include "images/radial_checked_png.h"
460 #include "images/radial_dn_png.h"
461 #include "images/radial_checkedhi_png.h"
462 #include "images/radial_up_png.h"
463 #include "images/radial_hi_png.h"
464 new_vframes(5,default_radial_images,
465         new VFramePng(radial_up_png),
466         new VFramePng(radial_hi_png),
467         new VFramePng(radial_checked_png),
468         new VFramePng(radial_dn_png),
469         new VFramePng(radial_checkedhi_png));
470
471 new_vframes(5,default_label_images,
472         new VFramePng(radial_up_png),
473         new VFramePng(radial_hi_png),
474         new VFramePng(radial_checked_png),
475         new VFramePng(radial_dn_png),
476         new VFramePng(radial_checkedhi_png));
477 #endif
478
479 #include "images/menuitem_up_png.h"
480 #include "images/menuitem_hi_png.h"
481 #include "images/menuitem_dn_png.h"
482 new_vframes(3,default_menuitem_data,
483         new VFramePng(menuitem_up_png),
484         new VFramePng(menuitem_hi_png),
485         new VFramePng(menuitem_dn_png));
486
487 #include "images/menubar_up_png.h"
488 #include "images/menubar_hi_png.h"
489 #include "images/menubar_dn_png.h"
490 new_vframes(3,default_menubar_data,
491         new VFramePng(menubar_up_png),
492         new VFramePng(menubar_hi_png),
493         new VFramePng(menubar_dn_png));
494
495 #include "images/menu_popup_bg_png.h"
496         default_menu_popup_bg = new VFramePng(menu_popup_bg_png);
497
498 #include "images/menubar_bg_png.h"
499         default_menu_bar_bg = new VFramePng(menubar_bg_png);
500
501 #include "images/check_png.h"
502         default_check_image = new VFramePng(check_png);
503
504 #include "images/file_text_up_png.h"
505 #include "images/file_text_hi_png.h"
506 #include "images/file_text_dn_png.h"
507 new_vframes(3,default_filebox_text_images,
508         new VFramePng(file_text_up_png),
509         new VFramePng(file_text_hi_png),
510         new VFramePng(file_text_dn_png));
511
512 #include "images/file_icons_up_png.h"
513 #include "images/file_icons_hi_png.h"
514 #include "images/file_icons_dn_png.h"
515 new_vframes(3,default_filebox_icons_images,
516         new VFramePng(file_icons_up_png),
517         new VFramePng(file_icons_hi_png),
518         new VFramePng(file_icons_dn_png));
519
520 #include "images/file_updir_up_png.h"
521 #include "images/file_updir_hi_png.h"
522 #include "images/file_updir_dn_png.h"
523 new_vframes(3,default_filebox_updir_images,
524         new VFramePng(file_updir_up_png),
525         new VFramePng(file_updir_hi_png),
526         new VFramePng(file_updir_dn_png));
527
528 #include "images/file_newfolder_up_png.h"
529 #include "images/file_newfolder_hi_png.h"
530 #include "images/file_newfolder_dn_png.h"
531 new_vframes(3,default_filebox_newfolder_images,
532         new VFramePng(file_newfolder_up_png),
533         new VFramePng(file_newfolder_hi_png),
534         new VFramePng(file_newfolder_dn_png));
535
536 #include "images/file_rename_up_png.h"
537 #include "images/file_rename_hi_png.h"
538 #include "images/file_rename_dn_png.h"
539 new_vframes(3,default_filebox_rename_images,
540         new VFramePng(file_rename_up_png),
541         new VFramePng(file_rename_hi_png),
542         new VFramePng(file_rename_dn_png));
543
544 #include "images/file_delete_up_png.h"
545 #include "images/file_delete_hi_png.h"
546 #include "images/file_delete_dn_png.h"
547 new_vframes(3,default_filebox_delete_images,
548         new VFramePng(file_delete_up_png),
549         new VFramePng(file_delete_hi_png),
550         new VFramePng(file_delete_dn_png));
551
552 #include "images/file_reload_up_png.h"
553 #include "images/file_reload_hi_png.h"
554 #include "images/file_reload_dn_png.h"
555 new_vframes(3,default_filebox_reload_images,
556         new VFramePng(file_reload_up_png),
557         new VFramePng(file_reload_hi_png),
558         new VFramePng(file_reload_dn_png));
559
560 #include "images/file_size_capb_up_png.h"
561 #include "images/file_size_capb_hi_png.h"
562 #include "images/file_size_capb_dn_png.h"
563 #include "images/file_size_lwrb_up_png.h"
564 #include "images/file_size_lwrb_hi_png.h"
565 #include "images/file_size_lwrb_dn_png.h"
566 #include "images/file_size_semi_up_png.h"
567 #include "images/file_size_semi_hi_png.h"
568 #include "images/file_size_semi_dn_png.h"
569 #include "images/file_size_zero_up_png.h"
570 #include "images/file_size_zero_hi_png.h"
571 #include "images/file_size_zero_dn_png.h"
572 new_vframes(12,default_filebox_szfmt_images,
573         new VFramePng(file_size_zero_up_png),
574         new VFramePng(file_size_zero_hi_png),
575         new VFramePng(file_size_zero_dn_png),
576         new VFramePng(file_size_lwrb_up_png),
577         new VFramePng(file_size_lwrb_hi_png),
578         new VFramePng(file_size_lwrb_dn_png),
579         new VFramePng(file_size_capb_up_png),
580         new VFramePng(file_size_capb_hi_png),
581         new VFramePng(file_size_capb_dn_png),
582         new VFramePng(file_size_semi_up_png),
583         new VFramePng(file_size_semi_hi_png),
584         new VFramePng(file_size_semi_dn_png));
585
586 #include "images/listbox_button_dn_png.h"
587 #include "images/listbox_button_hi_png.h"
588 #include "images/listbox_button_up_png.h"
589 #include "images/listbox_button_disabled_png.h"
590 new_vframes(4,default_listbox_button,
591         new VFramePng(listbox_button_up_png),
592         new VFramePng(listbox_button_hi_png),
593         new VFramePng(listbox_button_dn_png),
594         new VFramePng(listbox_button_disabled_png));
595
596 default_listbox_bg = 0;
597
598 #include "images/listbox_expandchecked_png.h"
599 #include "images/listbox_expandcheckedhi_png.h"
600 #include "images/listbox_expanddn_png.h"
601 #include "images/listbox_expandup_png.h"
602 #include "images/listbox_expanduphi_png.h"
603 new_vframes(5,default_listbox_expand,
604         new VFramePng(listbox_expandup_png),
605         new VFramePng(listbox_expanduphi_png),
606         new VFramePng(listbox_expandchecked_png),
607         new VFramePng(listbox_expanddn_png),
608         new VFramePng(listbox_expandcheckedhi_png));
609
610 #include "images/listbox_columnup_png.h"
611 #include "images/listbox_columnhi_png.h"
612 #include "images/listbox_columndn_png.h"
613 new_vframes(3,default_listbox_column,
614         new VFramePng(listbox_columnup_png),
615         new VFramePng(listbox_columnhi_png),
616         new VFramePng(listbox_columndn_png));
617
618 #include "images/listbox_up_png.h"
619         default_listbox_up = new VFramePng(listbox_up_png);
620
621 #include "images/listbox_dn_png.h"
622         default_listbox_dn = new VFramePng(listbox_dn_png);
623
624 #include "images/pot_hi_png.h"
625 #include "images/pot_up_png.h"
626 #include "images/pot_dn_png.h"
627 new_vframes(3,default_pot_images,
628         new VFramePng(pot_up_png),
629         new VFramePng(pot_hi_png),
630         new VFramePng(pot_dn_png));
631
632 #include "images/progress_up_png.h"
633 #include "images/progress_hi_png.h"
634 new_vframes(2,default_progress_images,
635         new VFramePng(progress_up_png),
636         new VFramePng(progress_hi_png));
637
638 #include "images/7seg_small/0_png.h"
639 #include "images/7seg_small/1_png.h"
640 #include "images/7seg_small/2_png.h"
641 #include "images/7seg_small/3_png.h"
642 #include "images/7seg_small/4_png.h"
643 #include "images/7seg_small/5_png.h"
644 #include "images/7seg_small/6_png.h"
645 #include "images/7seg_small/7_png.h"
646 #include "images/7seg_small/8_png.h"
647 #include "images/7seg_small/9_png.h"
648 #include "images/7seg_small/colon_png.h"
649 #include "images/7seg_small/period_png.h"
650 #include "images/7seg_small/a_png.h"
651 #include "images/7seg_small/b_png.h"
652 #include "images/7seg_small/c_png.h"
653 #include "images/7seg_small/d_png.h"
654 #include "images/7seg_small/e_png.h"
655 #include "images/7seg_small/f_png.h"
656 #include "images/7seg_small/space_png.h"
657 #include "images/7seg_small/dash_png.h"
658 new_vframes(20,default_medium_7segment,
659         new VFramePng(_0_png),
660         new VFramePng(_1_png),
661         new VFramePng(_2_png),
662         new VFramePng(_3_png),
663         new VFramePng(_4_png),
664         new VFramePng(_5_png),
665         new VFramePng(_6_png),
666         new VFramePng(_7_png),
667         new VFramePng(_8_png),
668         new VFramePng(_9_png),
669         new VFramePng(colon_png),
670         new VFramePng(period_png),
671         new VFramePng(a_png),
672         new VFramePng(b_png),
673         new VFramePng(c_png),
674         new VFramePng(d_png),
675         new VFramePng(e_png),
676         new VFramePng(f_png),
677         new VFramePng(space_png),
678         new VFramePng(dash_png));
679
680 #include "images/hscroll_handle_up_png.h"
681 #include "images/hscroll_handle_hi_png.h"
682 #include "images/hscroll_handle_dn_png.h"
683 #include "images/hscroll_handle_bg_png.h"
684 #include "images/hscroll_left_up_png.h"
685 #include "images/hscroll_left_hi_png.h"
686 #include "images/hscroll_left_dn_png.h"
687 #include "images/hscroll_right_up_png.h"
688 #include "images/hscroll_right_hi_png.h"
689 #include "images/hscroll_right_dn_png.h"
690 new_vframes(10,default_hscroll_data,
691         new VFramePng(hscroll_handle_up_png),
692         new VFramePng(hscroll_handle_hi_png),
693         new VFramePng(hscroll_handle_dn_png),
694         new VFramePng(hscroll_handle_bg_png),
695         new VFramePng(hscroll_left_up_png),
696         new VFramePng(hscroll_left_hi_png),
697         new VFramePng(hscroll_left_dn_png),
698         new VFramePng(hscroll_right_up_png),
699         new VFramePng(hscroll_right_hi_png),
700         new VFramePng(hscroll_right_dn_png));
701
702 #include "images/vscroll_handle_up_png.h"
703 #include "images/vscroll_handle_hi_png.h"
704 #include "images/vscroll_handle_dn_png.h"
705 #include "images/vscroll_handle_bg_png.h"
706 #include "images/vscroll_left_up_png.h"
707 #include "images/vscroll_left_hi_png.h"
708 #include "images/vscroll_left_dn_png.h"
709 #include "images/vscroll_right_up_png.h"
710 #include "images/vscroll_right_hi_png.h"
711 #include "images/vscroll_right_dn_png.h"
712 new_vframes(10,default_vscroll_data,
713         new VFramePng(vscroll_handle_up_png),
714         new VFramePng(vscroll_handle_hi_png),
715         new VFramePng(vscroll_handle_dn_png),
716         new VFramePng(vscroll_handle_bg_png),
717         new VFramePng(vscroll_left_up_png),
718         new VFramePng(vscroll_left_hi_png),
719         new VFramePng(vscroll_left_dn_png),
720         new VFramePng(vscroll_right_up_png),
721         new VFramePng(vscroll_right_hi_png),
722         new VFramePng(vscroll_right_dn_png));
723
724 #include "images/default_icon_png.h"
725         default_icon_img = new VFramePng(default_icon_png);
726
727         type_to_icon = default_type_to_icon;
728         bar_data = default_bar;
729         check = default_check_image;
730         listbox_button = default_listbox_button;
731         listbox_bg = default_listbox_bg;
732         listbox_expand = default_listbox_expand;
733         listbox_column = default_listbox_column;
734         listbox_up = default_listbox_up;
735         listbox_dn = default_listbox_dn;
736         hscroll_data = default_hscroll_data;
737         vscroll_data = default_vscroll_data;
738         default_icon = default_icon_img;
739
740         listbox_title_overlap = 0;
741         listbox_title_margin = 0;
742         listbox_title_color = BLACK;
743         listbox_title_hotspot = xS(5);
744
745         listbox_border1 = DKGREY;
746         listbox_border2_hi = RED;
747         listbox_border2 = BLACK;
748         listbox_border3_hi = RED;
749         listbox_border3 = MEGREY;
750         listbox_border4 = WHITE;
751         listbox_selected = BLUE;
752         listbox_highlighted = LTGREY;
753         listbox_inactive = WHITE;
754         listbox_text = BLACK;
755
756         pan_data = 0;
757         pan_text_color = YELLOW;
758
759         generic_button_margin = xS(15);
760         draw_clock_background=1;
761
762 // Initialize
763         bg_color = ORANGE;
764         bg_shadow1 = DKGREY;
765         bg_shadow2 = BLACK;
766         bg_light1 = WHITE;
767         bg_light2 = bg_color;
768
769
770         border_light1 = bg_color;
771         border_light2 = MEGREY;
772         border_shadow1 = BLACK;
773         border_shadow2 = bg_color;
774
775         default_text_color = BLACK;
776         disabled_text_color = DMGREY;
777
778         button_light = MEGREY;           // bright corner
779 //      button_highlighted = LTGREY;  // face when highlighted
780         button_highlighted = 0xffe000;  // face when highlighted
781         button_down = MDGREY;         // face when down
782 //      button_up = MEGREY;           // face when up
783         button_up = 0xffc000;           // face when up
784         button_shadow = BLACK;       // dark corner
785         button_uphighlighted = RED;   // upper side when highlighted
786
787         tumble_data = 0;
788         tumble_duration = 150;
789
790         ok_images = default_ok_images;
791         cancel_images = default_cancel_images;
792         usethis_button_images = default_usethis_images;
793         filebox_descend_images = default_ok_images;
794
795         menu_light = LTCYAN;
796         menu_highlighted = LTBLUE;
797         menu_down = MDCYAN;
798         menu_up = MECYAN;
799         menu_shadow = DKCYAN;
800
801         menu_title_bg = default_menubar_data;
802         menu_popup_bg = default_menu_popup_bg;
803         menu_bar_bg = default_menu_bar_bg;
804
805         popupmenu_images = 0;
806
807
808         popupmenu_margin = xS(10);
809         popupmenu_btnup = 1;
810         popupmenu_triangle_margin = xS(10);
811
812         min_menu_w = 0;
813         menu_title_text = BLACK;
814         popup_title_text = BLACK;
815         menu_item_text = BLACK;
816         menu_highlighted_fontcolor = BLACK;
817         progress_text = BLACK;
818         grab_input_focus = 1;
819
820         text_default = BLACK;
821         highlight_inverse = WHITE ^ BLUE;
822         text_background = WHITE;
823         text_background_disarmed = 0xc08080;
824         text_background_hi = LTYELLOW;
825         text_background_noborder_hi = LTGREY;
826         text_background_noborder = -1;
827         text_border1 = DKGREY;
828         text_border2 = BLACK;
829         text_border2_hi = RED;
830         text_border3 = MEGREY;
831         text_border3_hi = LTPINK;
832         text_border4 = WHITE;
833         text_highlight = BLUE;
834         text_selected_highlight = SLBLUE;
835         text_inactive_highlight = MEGREY;
836
837         toggle_highlight_bg = 0;
838         toggle_text_margin = 0;
839
840 // Delays must all be different for repeaters
841         double_click = 300;
842         blink_rate = 250;
843         scroll_repeat = 150;
844         tooltip_delay = 1000;
845         tooltip_bg_color = YELLOW;
846         tooltips_enabled = 1;
847         textbox_focus_policy = 0;
848
849         filebox_margin = yS(110);
850         dirbox_margin = yS(90);
851         filebox_mode = LISTBOX_TEXT;
852         sprintf(filebox_filter, "*");
853         filebox_w = xS(640);
854         filebox_h = yS(480);
855         filebox_columntype[0] = FILEBOX_NAME;
856         filebox_columntype[1] = FILEBOX_SIZE;
857         filebox_columntype[2] = FILEBOX_DATE;
858         filebox_columntype[3] = FILEBOX_EXTENSION;
859         filebox_columnwidth[0] = xS(200);
860         filebox_columnwidth[1] = xS(100);
861         filebox_columnwidth[2] = xS(100);
862         filebox_columnwidth[3] = xS(100);
863         dirbox_columntype[0] = FILEBOX_NAME;
864         dirbox_columntype[1] = FILEBOX_DATE;
865         dirbox_columnwidth[0] = xS(200);
866         dirbox_columnwidth[1] = xS(100);
867
868         filebox_text_images = default_filebox_text_images;
869         filebox_icons_images = default_filebox_icons_images;
870         filebox_updir_images = default_filebox_updir_images;
871         filebox_newfolder_images = default_filebox_newfolder_images;
872         filebox_rename_images = default_filebox_rename_images;
873         filebox_delete_images = default_filebox_delete_images;
874         filebox_reload_images = default_filebox_reload_images;
875         filebox_szfmt_images = default_filebox_szfmt_images;
876         filebox_size_format = FILEBOX_SIZE_RAW;
877         directory_color = BLUE;
878         file_color = BLACK;
879
880         filebox_sortcolumn = 0;
881         filebox_sortorder = BC_ListBox::SORT_ASCENDING;
882         dirbox_sortcolumn = 0;
883         dirbox_sortorder = BC_ListBox::SORT_ASCENDING;
884
885
886         pot_images = default_pot_images;
887         pot_offset = 2;
888         pot_x1 = pot_images[0]->get_w() / 2 - xS(pot_offset);
889         pot_y1 = pot_images[0]->get_h() / 2 - yS(pot_offset);
890         pot_r = pot_x1;
891         pot_needle_color = BLACK;
892
893         progress_images = default_progress_images;
894
895         xmeter_images = 0;
896         ymeter_images = 0;
897         meter_font = SMALLFONT_3D;
898         meter_font_color = RED;
899         meter_title_w = xS(20);
900         meter_3d = 1;
901         medium_7segment = default_medium_7segment;
902
903         audiovideo_color = RED;
904
905         use_fontset = 0;
906
907 // Xft has priority over font set
908 #ifdef HAVE_XFT
909 // But Xft dies in 32 bit mode after some amount of drawing.
910         use_xft = 1;
911 #else
912         use_xft = 0;
913 #endif
914
915         drag_radius = xS(10);
916         recursive_resizing = 1;
917 }
918
919 void BC_Resources::del_vframes(VFrame *vframes[], int n)
920 {
921         while( --n >= 0 ) delete vframes[n];
922 }
923
924 BC_Resources::~BC_Resources()
925 {
926         delete id_lock;
927         delete create_window_lock;
928         del_vframes(default_type_to_icon, 6);
929         delete default_bar;
930         del_vframes(default_cancel_images, 3);
931         del_vframes(default_ok_images, 3);
932         del_vframes(default_usethis_images, 3);
933 #if 0
934         del_vframes(default_checkbox_images, 5);
935         del_vframes(default_radial_images, 5);
936         del_vframes(default_label_images, 5);
937 #endif
938         del_vframes(default_menuitem_data, 3);
939         del_vframes(default_menubar_data, 3);
940         delete default_menu_popup_bg;
941         delete default_menu_bar_bg;
942         delete default_check_image;
943         del_vframes(default_filebox_text_images, 3);
944         del_vframes(default_filebox_icons_images, 3);
945         del_vframes(default_filebox_updir_images, 3);
946         del_vframes(default_filebox_newfolder_images, 3);
947         del_vframes(default_filebox_rename_images, 3);
948         del_vframes(default_filebox_delete_images, 3);
949         del_vframes(default_filebox_reload_images, 3);
950         del_vframes(default_filebox_szfmt_images, 12);
951         del_vframes(default_listbox_button, 4);
952         delete default_listbox_bg;
953         del_vframes(default_listbox_expand, 5);
954         del_vframes(default_listbox_column, 3);
955         delete default_listbox_up;
956         delete default_listbox_dn;
957         del_vframes(default_pot_images, 3);
958         del_vframes(default_progress_images, 2);
959         del_vframes(default_medium_7segment, 20);
960         del_vframes(default_vscroll_data, 10);
961         del_vframes(default_hscroll_data, 10);
962         if( fontlist ) {
963                 fontlist->remove_all_objects();
964                 delete fontlist;
965         }
966         delete default_icon_img;
967         finit_font_defs();
968 }
969
970 int BC_Resources::initialize_display(BC_WindowBase *window)
971 {
972 // Set up IPC cleanup handlers
973 //      bc_init_ipc();
974
975 // Test for shm.  Must come before yuv test
976         init_shm(window);
977         return 0;
978 }
979
980
981 void BC_Resources::init_shm(BC_WindowBase *window)
982 {
983         use_shm = 0;
984         int (*old_handler)(Display *, XErrorEvent *) =
985                 XSetErrorHandler(BC_Resources::x_error_handler);
986
987         if(XShmQueryExtension(window->display))
988         {
989                 XShmSegmentInfo test_shm;
990                 memset(&test_shm,0,sizeof(test_shm));
991                 XImage *test_image = XShmCreateImage(window->display, window->vis,
992                         window->default_depth, ZPixmap, (char*)NULL, &test_shm, 5, 5);
993                 BC_Resources::error = 0;
994                 test_shm.shmid = shmget(IPC_PRIVATE, 5 * test_image->bytes_per_line, (IPC_CREAT | 0600));
995                 if(test_shm.shmid != -1) {
996                         char *data = (char *)shmat(test_shm.shmid, NULL, 0);
997                         if(data != (void *)-1) {
998                                 shmctl(test_shm.shmid, IPC_RMID, 0);
999                                 test_shm.shmaddr = data;
1000                                 test_shm.readOnly = 0;
1001
1002                                 if(XShmAttach(window->display, &test_shm)) {
1003                                         XSync(window->display, False);
1004                                         use_shm = 1;
1005                                 }
1006                                 shmdt(data);
1007                         }
1008                 }
1009
1010                 XDestroyImage(test_image);
1011                 if(BC_Resources::error) use_shm = 0;
1012         }
1013         XSetErrorHandler(old_handler);
1014 }
1015
1016
1017
1018
1019 BC_Synchronous* BC_Resources::get_synchronous()
1020 {
1021         return synchronous;
1022 }
1023
1024 void BC_Resources::set_synchronous(BC_Synchronous *synchronous)
1025 {
1026         this->synchronous = synchronous;
1027 }
1028
1029
1030
1031
1032 int BC_Resources::get_bg_color() { return bg_color; }
1033
1034 int BC_Resources::get_bg_shadow1() { return bg_shadow1; }
1035
1036 int BC_Resources::get_bg_shadow2() { return bg_shadow2; }
1037
1038 int BC_Resources::get_bg_light1() { return bg_light1; }
1039
1040 int BC_Resources::get_bg_light2() { return bg_light2; }
1041
1042
1043 int BC_Resources::get_id()
1044 {
1045         id_lock->lock("BC_Resources::get_id");
1046         int result = id++;
1047         id_lock->unlock();
1048         return result;
1049 }
1050
1051 int BC_Resources::get_filebox_id()
1052 {
1053         id_lock->lock("BC_Resources::get_filebox_id");
1054         int result = filebox_id++;
1055         id_lock->unlock();
1056         return result;
1057 }
1058
1059
1060 void BC_Resources::set_signals(BC_Signals *signal_handler)
1061 {
1062         BC_Resources::signal_handler = signal_handler;
1063 }
1064
1065 int BC_Resources::init_fontconfig(const char *search_path)
1066 {
1067         if( fontlist ) return 0;
1068         fontlist = new ArrayList<BC_FontEntry*>;
1069
1070 #define get_str(str,sep,ptr,cond) do { char *out = str; \
1071   while( *ptr && !strchr(sep,*ptr) && (cond) ) *out++ = *ptr++; \
1072   *out = 0; \
1073 } while(0)
1074
1075 #define skip_str(sep, ptr) do { \
1076   while( *ptr && strchr(sep,*ptr) ) ++ptr; \
1077 } while(0)
1078
1079         char find_command[BCTEXTLEN];
1080         char *fp = find_command, *ep = fp+sizeof(find_command)-1;
1081         fp += snprintf(fp, ep-fp, "%s", "find");
1082         const char *bc_font_path = getenv("BC_FONT_PATH");
1083 // if BC_FONT_PATH starts with ':', omit default path
1084         if( !(bc_font_path && bc_font_path[0] == ':') )
1085                 fp += snprintf(fp, ep-fp, " '%s'", search_path);
1086         if( bc_font_path ) {
1087                 const char *path = bc_font_path;
1088                 while( *path ) {
1089                         char font_path[BCTEXTLEN];
1090                         const char *cp = strchr(path,':');
1091                         int len = !cp ? strlen(path) : cp-path;
1092                         if( len > 0 ) {
1093                                 memcpy(font_path, path, len);
1094                                 font_path[len] = 0;  path += len;
1095                                 fp += snprintf(fp, ep-fp, " '%s'", font_path);
1096                         }
1097                         if( cp ) ++path;
1098                 }
1099         }
1100         fp += snprintf(fp, ep-fp, " -name 'fonts.scale' -print -exec cat {} \\;");
1101         FILE *in = popen(find_command, "r");
1102
1103         FT_Library freetype_library = 0;
1104 //      FT_Face freetype_face = 0;
1105 //      ft_Init_FreeType(&freetype_library);
1106
1107         char line[BCTEXTLEN], current_dir[BCTEXTLEN];
1108         current_dir[0] = 0;
1109
1110         while( !feof(in) && fgets(line, BCTEXTLEN, in) ) {
1111                 if(!strlen(line)) break;
1112
1113                 char *in_ptr = line;
1114
1115 // Get current directory
1116                 if(line[0] == '/') {
1117                         get_str(current_dir, "\n", in_ptr,1);
1118                         for( int i=strlen(current_dir); --i>=0 && current_dir[i]!='/'; )
1119                                 current_dir[i] = 0;
1120                         continue;
1121                 }
1122
1123 //printf("TitleMain::build_fonts %s\n", line);
1124                 BC_FontEntry *entry = new BC_FontEntry;
1125                 char string[BCTEXTLEN];
1126 // Path
1127                 get_str(string, "\n", in_ptr, in_ptr[0]!=' ' || in_ptr[1]!='-');
1128                 entry->path = cstrcat(2, current_dir, string);
1129 // Foundary
1130                 skip_str(" -", in_ptr);
1131                 get_str(string, "-\n", in_ptr, 1);
1132                 if( !string[0] ) { delete entry;  continue; }
1133                 entry->foundry = cstrdup(string);
1134                 if(*in_ptr == '-') in_ptr++;
1135 // Family
1136                 get_str(string, "-\n", in_ptr, 1);
1137                 if( !string[0] ) { delete entry;  continue; }
1138                 entry->family = cstrdup(string);
1139                 if(*in_ptr == '-') in_ptr++;
1140 // Weight
1141                 get_str(string, "-\n", in_ptr, 1);
1142                 entry->weight = cstrdup(string);
1143                 if(*in_ptr == '-') in_ptr++;
1144 // Slant
1145                 get_str(string, "-\n", in_ptr, 1);
1146                 entry->slant = cstrdup(string);
1147                 if(*in_ptr == '-') in_ptr++;
1148 // SWidth
1149                 get_str(string, "-\n", in_ptr, 1);
1150                 entry->swidth = cstrdup(string);
1151                 if(*in_ptr == '-') in_ptr++;
1152 // Adstyle
1153                 get_str(string, "-\n", in_ptr, 1);
1154                 entry->adstyle = cstrdup(string);
1155                 if(*in_ptr == '-') in_ptr++;
1156 // pixelsize
1157                 get_str(string, "-\n", in_ptr, 1);
1158                 entry->pixelsize = atol(string);
1159                 if(*in_ptr == '-') in_ptr++;
1160 // pointsize
1161                 get_str(string, "-\n", in_ptr, 1);
1162                 entry->pointsize = atol(string);
1163                 if(*in_ptr == '-') in_ptr++;
1164 // xres
1165                 get_str(string, "-\n", in_ptr, 1);
1166                 entry->xres = atol(string);
1167                 if(*in_ptr == '-') in_ptr++;
1168 // yres
1169                 get_str(string, "-\n", in_ptr, 1);
1170                 entry->yres = atol(string);
1171                 if(*in_ptr == '-') in_ptr++;
1172 // spacing
1173                 get_str(string, "-\n", in_ptr, 1);
1174                 entry->spacing = cstrdup(string);
1175                 if(*in_ptr == '-') in_ptr++;
1176 // avg_width
1177                 get_str(string, "-\n", in_ptr, 1);
1178                 entry->avg_width = atol(string);
1179                 if(*in_ptr == '-') in_ptr++;
1180 // registry
1181                 get_str(string, "-\n", in_ptr, 1);
1182                 entry->registry = cstrdup(string);
1183                 if(*in_ptr == '-') in_ptr++;
1184 // encoding
1185                 get_str(string, "-\n", in_ptr, 1);
1186                 entry->encoding = cstrdup(string);
1187                 if(*in_ptr == '-') in_ptr++;
1188
1189 // Add to list
1190 //printf("TitleMain::build_fonts 1 %s\n", entry->path);
1191 // This takes a real long time to do.  Instead just take all fonts
1192 //              if(!load_freetype_face(freetype_library,
1193 //                      freetype_face, entry->path) )
1194 // Fix parameters
1195                 sprintf(line, "%s (%s)", entry->family, entry->foundry);
1196                 entry->displayname = cstrdup(line);
1197
1198                 if(!strcasecmp(entry->weight, "demibold")) {
1199                         entry->fixed_style |= BC_FONT_BOLD;
1200                         entry->style |= FL_WEIGHT_DEMIBOLD;
1201                 }
1202                 else if(!strcasecmp(entry->weight, "bold")) {
1203                         entry->fixed_style |= BC_FONT_BOLD;
1204                         entry->style |= FL_WEIGHT_BOLD;
1205                 }
1206                 else {
1207                         entry->style |= FL_WEIGHT_NORMAL;
1208                 }
1209
1210                 if(!strcasecmp(entry->slant, "r")) {
1211                         entry->style |= FL_SLANT_ROMAN;
1212                 }
1213                 else if(!strcasecmp(entry->slant, "i")) {
1214                         entry->style |= FL_SLANT_ITALIC;
1215                         entry->fixed_style |= BC_FONT_ITALIC;
1216                 }
1217                 else if(!strcasecmp(entry->slant, "o")) {
1218                         entry->style |= FL_SLANT_OBLIQUE;
1219                         entry->fixed_style |= BC_FONT_ITALIC;
1220                 }
1221
1222                 if(!strcasecmp(entry->swidth, "normal"))
1223                         entry->style |= FL_WIDTH_NORMAL;
1224                 else if(!strcasecmp(entry->swidth, "ultracondensed"))
1225                         entry->style |= FL_WIDTH_ULTRACONDENSED;
1226                 else if(!strcasecmp(entry->swidth, "extracondensed"))
1227                         entry->style |= FL_WIDTH_EXTRACONDENSED;
1228                 else if(!strcasecmp(entry->swidth, "condensed"))
1229                         entry->style |= FL_WIDTH_CONDENSED;
1230                 else if(!strcasecmp(entry->swidth, "semicondensed"))
1231                         entry->style |= FL_WIDTH_SEMICONDENSED;
1232                 else if(!strcasecmp(entry->swidth, "semiexpanded"))
1233                         entry->style |= FL_WIDTH_SEMIEXPANDED;
1234                 else if(!strcasecmp(entry->swidth, "expanded"))
1235                         entry->style |= FL_WIDTH_EXPANDED;
1236                 else if(!strcasecmp(entry->swidth, "extraexpanded"))
1237                         entry->style |= FL_WIDTH_EXTRAEXPANDED;
1238                 else if(!strcasecmp(entry->swidth, "ultraexpanded"))
1239                         entry->style |= FL_WIDTH_ULTRAEXPANDED;
1240                 else
1241                         entry->style |= FL_WIDTH_NORMAL;
1242
1243                 fontlist->append(entry);
1244                 if( font_debug )
1245                         dump_font_entry(stdout, "font 0: ", entry);
1246
1247 //              printf("TitleMain::build_fonts %s: success\n",  entry->path);
1248 //printf("TitleMain::build_fonts 2\n");
1249         }
1250         pclose(in);
1251
1252         if( bc_font_path && bc_font_path[0] == ':' )
1253                 return 0;
1254
1255 // Load all the fonts from fontconfig
1256         FcPattern *pat;
1257         FcFontSet *fs;
1258         FcObjectSet *os;
1259         FcChar8 *family, *file, *foundry, *style, *format;
1260         int slant, spacing, width, weight;
1261         int force_style = 0;
1262 // if you want limit search to TrueType put 1
1263         int limit_to_trutype = 1;
1264         FcConfig *config;
1265         int i;
1266         char tmpstring[BCTEXTLEN];
1267         if(!fcInit())
1268                 return 1;
1269         config = fcConfigGetCurrent();
1270         fcConfigSetRescanInterval(config, 0);
1271
1272         pat = fcPatternCreate();
1273         os = fcObjectSetBuild( FC_FAMILY, FC_FILE, FC_FOUNDRY, FC_WEIGHT,
1274                 FC_WIDTH, FC_SLANT, FC_FONTFORMAT, FC_SPACING, FC_STYLE, (char *) 0);
1275         fcPatternAddBool(pat, FC_SCALABLE, true);
1276
1277         if(language[0]) {
1278                 char langstr[LEN_LANG * 3];
1279                 strcpy(langstr, language);
1280
1281                 if(region[0]) {
1282                         strcat(langstr, "-");
1283                         strcat(langstr, region);
1284                 }
1285
1286                 FcLangSet *ls =  fcLangSetCreate();
1287                 if(fcLangSetAdd(ls, (const FcChar8*)langstr))
1288                 if(fcPatternAddLangSet(pat, FC_LANG, ls))
1289                 fcLangSetDestroy(ls);
1290         }
1291
1292         fs = fcFontList(config, pat, os);
1293         fcPatternDestroy(pat);
1294         fcObjectSetDestroy(os);
1295
1296         for (i = 0; fs && i < fs->nfont; i++) {
1297                 FcPattern *font = fs->fonts[i];
1298                 force_style = 0;
1299                 fcPatternGetString(font, FC_FONTFORMAT, 0, &format);
1300                 //on this point you can limit font search
1301                 if(limit_to_trutype && strcmp((char *)format, "TrueType"))
1302                         continue;
1303
1304                 sprintf(tmpstring, "%s", format);
1305                 BC_FontEntry *entry = new BC_FontEntry;
1306                 if(fcPatternGetString(font, FC_FILE, 0, &file) == FcResultMatch) {
1307                         entry->path = cstrdup((char*)file);
1308                 }
1309
1310                 if(fcPatternGetString(font, FC_FOUNDRY, 0, &foundry) == FcResultMatch) {
1311                         entry->foundry = cstrdup((char*)foundry);
1312                 }
1313
1314                 if(fcPatternGetInteger(font, FC_WEIGHT, 0, &weight) == FcResultMatch) {
1315                         switch(weight) {
1316                         case FC_WEIGHT_THIN:
1317                         case FC_WEIGHT_EXTRALIGHT:
1318                         case FC_WEIGHT_LIGHT:
1319                         case FC_WEIGHT_BOOK:
1320                                 force_style = 1;
1321                                 entry->weight = cstrdup("medium");
1322                                 break;
1323
1324                         case FC_WEIGHT_NORMAL:
1325                         case FC_WEIGHT_MEDIUM:
1326                         default:
1327                                 entry->weight = cstrdup("medium");
1328                                 break;
1329
1330                         case FC_WEIGHT_BLACK:
1331                         case FC_WEIGHT_SEMIBOLD:
1332                         case FC_WEIGHT_BOLD:
1333                                 entry->weight = cstrdup("bold");
1334                                 entry->fixed_style |= BC_FONT_BOLD;
1335                                 break;
1336
1337                         case FC_WEIGHT_EXTRABOLD:
1338                         case FC_WEIGHT_EXTRABLACK:
1339                                 force_style = 1;
1340                                 entry->weight = cstrdup("bold");
1341                                 entry->fixed_style |= BC_FONT_BOLD;
1342                                 break;
1343                         }
1344                 }
1345
1346                 if(fcPatternGetString(font, FC_FAMILY, 0, &family) == FcResultMatch)
1347                         entry->family = cstrdup((char*)family);
1348
1349                 if(fcPatternGetInteger(font, FC_SLANT, 0, &slant) == FcResultMatch) {
1350                         switch(slant) {
1351                         case FC_SLANT_ROMAN:
1352                         default:
1353                                 entry->slant = cstrdup("r");
1354                                 entry->style |= FL_SLANT_ROMAN;
1355                                 break;
1356                         case FC_SLANT_ITALIC:
1357                                 entry->slant = cstrdup("i");
1358                                 entry->style |= FL_SLANT_ITALIC;
1359                                 entry->fixed_style |= BC_FONT_ITALIC;
1360                                 break;
1361                         case FC_SLANT_OBLIQUE:
1362                                 entry->slant = cstrdup("o");
1363                                 entry->style |= FL_SLANT_OBLIQUE;
1364                                 entry->fixed_style |= BC_FONT_ITALIC;
1365                                 break;
1366                         }
1367                 }
1368
1369                 if(fcPatternGetInteger(font, FC_WIDTH, 0, &width) == FcResultMatch) {
1370                         switch(width) {
1371                         case FC_WIDTH_ULTRACONDENSED:
1372                                 entry->swidth = cstrdup("ultracondensed");
1373                                 break;
1374
1375                         case FC_WIDTH_EXTRACONDENSED:
1376                                 entry->swidth = cstrdup("extracondensed");
1377                                 break;
1378
1379                         case FC_WIDTH_CONDENSED:
1380                                 entry->swidth = cstrdup("condensed");
1381                                 break;
1382                         case FC_WIDTH_SEMICONDENSED:
1383                                 entry->swidth = cstrdup("semicondensed");
1384                                 break;
1385
1386                         case FC_WIDTH_NORMAL:
1387                         default:
1388                                 entry->swidth = cstrdup("normal");
1389                                 break;
1390
1391                         case FC_WIDTH_SEMIEXPANDED:
1392                                 entry->swidth = cstrdup("semiexpanded");
1393                                 break;
1394
1395                         case FC_WIDTH_EXPANDED:
1396                                 entry->swidth = cstrdup("expanded");
1397                                 break;
1398
1399                         case FC_WIDTH_EXTRAEXPANDED:
1400                                 entry->swidth = cstrdup("extraexpanded");
1401                                 break;
1402
1403                         case FC_WIDTH_ULTRAEXPANDED:
1404                                 entry->swidth = cstrdup("ultraexpanded");
1405                                 break;
1406                         }
1407                 }
1408
1409                 if(fcPatternGetInteger(font, FC_SPACING, 0, &spacing) == FcResultMatch) {
1410                         switch(spacing) {
1411                         case 0:
1412                         default:
1413                                 entry->spacing = cstrdup("p");
1414                                 break;
1415
1416                         case 90:
1417                                 entry->spacing = cstrdup("d");
1418                                 break;
1419
1420                         case 100:
1421                                 entry->spacing = cstrdup("m");
1422                                 break;
1423
1424                         case 110:
1425                                 entry->spacing = cstrdup("c");
1426                                 break;
1427                         }
1428                 }
1429
1430                 // Add fake stuff for compatibility
1431                 entry->adstyle = cstrdup(" ");
1432                 entry->pixelsize = 0;
1433                 entry->pointsize = 0;
1434                 entry->xres = 0;
1435                 entry->yres = 0;
1436                 entry->avg_width = 0;
1437                 entry->registry = cstrdup("utf");
1438                 entry->encoding = cstrdup("8");
1439
1440                 if( fcPatternGetString(font, FC_STYLE, 0, &style) != FcResultMatch )
1441                         force_style = 0;
1442
1443                 // If font has a style unmanaged by titler plugin, force style to be displayed on name
1444                 // in this way we can shown all available fonts styles.
1445                 if(force_style) {
1446                         sprintf(tmpstring, "%s (%s)", entry->family, style);
1447                         entry->displayname = cstrdup(tmpstring);
1448                 }
1449                 else {
1450                         if(strcmp(entry->foundry, "unknown")) {
1451                                 sprintf(tmpstring, "%s (%s)", entry->family, entry->foundry);
1452                                 entry->displayname = cstrdup(tmpstring);
1453                         }
1454                         else {
1455                                 sprintf(tmpstring, "%s", entry->family);
1456                                 entry->displayname = cstrdup(tmpstring);
1457                         }
1458
1459                 }
1460                 fontlist->append(entry);
1461                 if( font_debug )
1462                         dump_font_entry(stdout, "font 1: ", entry);
1463         }
1464
1465         fcFontSetDestroy(fs);
1466         if(freetype_library)
1467                 ft_Done_FreeType(freetype_library);
1468 // for(int i = 0; i < fonts->total; i++)
1469 //      fonts->values[i]->dump();
1470
1471         fcConfigAppFontAddDir(0, (const FcChar8*)search_path);
1472         fcConfigSetRescanInterval(0, 0);
1473
1474         os = fcObjectSetBuild(FC_FAMILY, FC_FILE, FC_FOUNDRY, FC_WEIGHT,
1475                 FC_WIDTH, FC_SLANT, FC_SPACING, FC_STYLE, (char *)0);
1476         pat = fcPatternCreate();
1477         fcPatternAddBool(pat, FC_SCALABLE, true);
1478
1479         if(language[0])
1480         {
1481                 char langstr[LEN_LANG * 3];
1482                 strcpy(langstr, language);
1483
1484                 if(region[0])
1485                 {
1486                         strcat(langstr, "-");
1487                         strcat(langstr, region);
1488                 }
1489
1490                 FcLangSet *ls =  fcLangSetCreate();
1491                 if(fcLangSetAdd(ls, (const FcChar8*)langstr))
1492                         if(fcPatternAddLangSet(pat, FC_LANG, ls))
1493                 fcLangSetDestroy(ls);
1494         }
1495
1496         fs = fcFontList(0, pat, os);
1497         fcPatternDestroy(pat);
1498         fcObjectSetDestroy(os);
1499
1500         for(int i = 0; i < fs->nfont; i++)
1501         {
1502                 FcPattern *font = fs->fonts[i];
1503                 BC_FontEntry *entry = new BC_FontEntry;
1504
1505                 FcChar8 *strvalue;
1506                 if(fcPatternGetString(font, FC_FILE, 0, &strvalue) == FcResultMatch)
1507                 {
1508                         entry->path = new char[strlen((char*)strvalue) + 1];
1509                         strcpy(entry->path, (char*)strvalue);
1510                 }
1511
1512                 if(fcPatternGetString(font, FC_FOUNDRY, 0, &strvalue) == FcResultMatch)
1513                 {
1514                         entry->foundry = new char[strlen((char*)strvalue) + 1];
1515                         strcpy(entry->foundry, (char *)strvalue);
1516                 }
1517
1518                 if(fcPatternGetString(font, FC_FAMILY, 0, &strvalue) == FcResultMatch)
1519                 {
1520                         entry->family = new char[strlen((char*)strvalue) + 2];
1521                         strcpy(entry->family, (char*)strvalue);
1522                 }
1523
1524                 int intvalue;
1525                 if(fcPatternGetInteger(font, FC_SLANT, 0, &intvalue) == FcResultMatch)
1526                 {
1527                         switch(intvalue)
1528                         {
1529                         case FC_SLANT_ROMAN:
1530                         default:
1531                                 entry->style |= FL_SLANT_ROMAN;
1532                                 break;
1533
1534                         case FC_SLANT_ITALIC:
1535                                 entry->style |= FL_SLANT_ITALIC;
1536                                 break;
1537
1538                         case FC_SLANT_OBLIQUE:
1539                                 entry->style |= FL_SLANT_OBLIQUE;
1540                                 break;
1541                         }
1542                 }
1543
1544                 if(fcPatternGetInteger(font, FC_WEIGHT, 0, &intvalue) == FcResultMatch)
1545                 {
1546                         switch(intvalue)
1547                         {
1548                         case FC_WEIGHT_THIN:
1549                                 entry->style |= FL_WEIGHT_THIN;
1550                                 break;
1551
1552                         case FC_WEIGHT_EXTRALIGHT:
1553                                 entry->style |= FL_WEIGHT_EXTRALIGHT;
1554                                 break;
1555
1556                         case FC_WEIGHT_LIGHT:
1557                                 entry->style |= FL_WEIGHT_LIGHT;
1558                                 break;
1559
1560                         case FC_WEIGHT_BOOK:
1561                                 entry->style |= FL_WEIGHT_BOOK;
1562                                 break;
1563
1564                         case FC_WEIGHT_NORMAL:
1565                         default:
1566                                 entry->style |= FL_WEIGHT_NORMAL;
1567                                 break;
1568
1569                         case FC_WEIGHT_MEDIUM:
1570                                 entry->style |= FL_WEIGHT_MEDIUM;
1571                                 break;
1572
1573                         case FC_WEIGHT_DEMIBOLD:
1574                                 entry->style |= FL_WEIGHT_DEMIBOLD;
1575                                 break;
1576
1577                         case FC_WEIGHT_BOLD:
1578                                 entry->style |= FL_WEIGHT_BOLD;
1579                                 break;
1580
1581                         case FC_WEIGHT_EXTRABOLD:
1582                                 entry->style |= FL_WEIGHT_EXTRABOLD;
1583                                 break;
1584
1585                         case FC_WEIGHT_BLACK:
1586                                 entry->style |= FL_WEIGHT_BLACK;
1587                                 break;
1588
1589                         case FC_WEIGHT_EXTRABLACK:
1590                                 entry->style |= FL_WEIGHT_EXTRABLACK;
1591                                 break;
1592                         }
1593                 }
1594
1595                 if(fcPatternGetInteger(font, FC_WIDTH, 0, &intvalue) == FcResultMatch)
1596                 {
1597                         switch(intvalue)
1598                         {
1599                         case FC_WIDTH_ULTRACONDENSED:
1600                                 entry->style |= FL_WIDTH_ULTRACONDENSED;
1601                                 break;
1602
1603                         case FC_WIDTH_EXTRACONDENSED:
1604                                 entry->style |= FL_WIDTH_EXTRACONDENSED;
1605                                 break;
1606
1607                         case FC_WIDTH_CONDENSED:
1608                                 entry->style |= FL_WIDTH_CONDENSED;
1609                                 break;
1610
1611                         case FC_WIDTH_SEMICONDENSED:
1612                                 entry->style = FL_WIDTH_SEMICONDENSED;
1613                                 break;
1614
1615                         case FC_WIDTH_NORMAL:
1616                         default:
1617                                 entry->style |= FL_WIDTH_NORMAL;
1618                                 break;
1619
1620                         case FC_WIDTH_SEMIEXPANDED:
1621                                 entry->style |= FL_WIDTH_SEMIEXPANDED;
1622                                 break;
1623
1624                         case FC_WIDTH_EXPANDED:
1625                                 entry->style |= FL_WIDTH_EXPANDED;
1626                                 break;
1627
1628                         case FC_WIDTH_EXTRAEXPANDED:
1629                                 entry->style |= FL_WIDTH_EXTRAEXPANDED;
1630                                 break;
1631
1632                         case FC_WIDTH_ULTRAEXPANDED:
1633                                 entry->style |= FL_WIDTH_ULTRAEXPANDED;
1634                                 break;
1635                         }
1636                 }
1637                 if(fcPatternGetInteger(font, FC_SPACING, 0, &intvalue) == FcResultMatch)
1638                 {
1639                         switch(intvalue)
1640                         {
1641                         case FC_PROPORTIONAL:
1642                         default:
1643                                 entry->style |= FL_PROPORTIONAL;
1644                                 break;
1645
1646                         case FC_DUAL:
1647                                 entry->style |= FL_DUAL;
1648                                 break;
1649
1650                         case FC_MONO:
1651                                 entry->style |= FL_MONO;
1652                                 break;
1653
1654                         case FC_CHARCELL:
1655                                 entry->style |= FL_CHARCELL;
1656                                 break;
1657                         }
1658                 }
1659                 if(entry->foundry && strcmp(entry->foundry, "unknown"))
1660                 {
1661                         char tempstr[BCTEXTLEN];
1662                         sprintf(tempstr, "%s (%s)", entry->family, entry->foundry);
1663                         entry->displayname = new char[strlen(tempstr) + 1];
1664                         strcpy(entry->displayname, tempstr);
1665                 }
1666                 else
1667                 {
1668                         entry->displayname = new char[strlen(entry->family) + 1];
1669                         strcpy(entry->displayname, entry->family);
1670                 }
1671                 fontlist->append(entry);
1672                 if( font_debug )
1673                         dump_font_entry(stdout, "font 2: ", entry);
1674         }
1675         fcFontSetDestroy(fs);
1676         return 0;
1677 }
1678
1679 #define STYLE_MATCH(fst, stl, msk) ((fst) & (msk) & (stl)) && \
1680        !((fst) & ~(style) & (msk))
1681
1682 BC_FontEntry *BC_Resources::find_fontentry(const char *displayname, int style,
1683         int mask, int preferred)
1684 {
1685         BC_FontEntry *entry, *style_match, *preferred_match;
1686
1687         if(!fontlist)
1688                 return 0;
1689
1690         style_match = 0;
1691         preferred_match = 0;
1692
1693         if(displayname)
1694         {
1695                 for(int i = 0; i < fontlist->total; i++)
1696                 {
1697                         entry = fontlist->values[i];
1698
1699                         if(strcmp(entry->displayname, displayname) == 0 &&
1700                                         STYLE_MATCH(entry->style, style, mask))
1701                         {
1702                                 if(!style_match)
1703                                         style_match = entry;
1704                                 if(!preferred_match && entry->fixed_style == preferred)
1705                                         preferred_match = entry;
1706                         }
1707                 }
1708                 if(preferred_match)
1709                         return preferred_match;
1710
1711                 if(style_match)
1712                         return style_match;
1713         }
1714
1715 // No exact match - assume normal width font
1716         style |= FL_WIDTH_NORMAL;
1717         mask |= FL_WIDTH_MASK;
1718         style_match = 0;
1719         preferred_match = 0;
1720
1721         for(int i = 0; i < fontlist->total; i++)
1722         {
1723                 entry = fontlist->values[i];
1724
1725                 if(STYLE_MATCH(entry->style, style, mask))
1726                 {
1727                         if(!style_match)
1728                                 style_match = entry;
1729
1730                         if(!preferred_match && (entry->style & preferred))
1731                                 preferred_match = entry;
1732
1733                         if(!strncasecmp(displayname, entry->family,
1734                                         strlen(entry->family)))
1735                         return entry;
1736                 }
1737         }
1738
1739         if(preferred_match)
1740                 return preferred_match;
1741
1742         return style_match;
1743 }
1744
1745
1746 class utf8conv {
1747         uint8_t *obfr, *out, *oend;
1748         uint8_t *ibfr, *inp, *iend;
1749 public:
1750         utf8conv(void *out, int olen, void *inp, int ilen) {
1751                 this->obfr = this->out = (uint8_t*)out;
1752                 this->oend = this->out + olen;
1753                 this->ibfr = this->inp = (uint8_t*)inp;
1754                 this->iend = this->inp + ilen;
1755         }
1756         int cur() { return inp>=iend ? -1 : *inp; }
1757         int next() { return inp>=iend ? -1 : *inp++; }
1758         int next(int ch) { return out>=oend ? -1 : *out++ = ch; }
1759         int ilen() { return inp-ibfr; }
1760         int olen() { return out-obfr; }
1761         int wnext();
1762         int wnext(unsigned int v);
1763 };
1764
1765 int utf8conv::
1766 wnext(unsigned int v)
1767 {
1768   if( v < 0x00000080 ) { next(v);  return 1; }
1769   int n = v < 0x00000800 ? 2 : v < 0x00010000 ? 3 :
1770           v < 0x00200000 ? 4 : v < 0x04000000 ? 5 : 6;
1771   int m = (0xff00 >> n), i = n-1;
1772   next((v>>(6*i)) | m);
1773   while( --i >= 0 ) next(((v>>(6*i)) & 0x3f) | 0x80);
1774   return n;
1775 }
1776
1777 int utf8conv::
1778 wnext()
1779 {
1780   int v = 0, n = 0, ch = next();
1781   if( ch >= 0x80 ) {
1782     static const unsigned char byts[] = {
1783       1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 4, 5,
1784     };
1785     int i = ch - 0xc0;
1786     n = i<0 ? 0 : byts[i/4];
1787     for( v=ch, i=n; --i>=0; v+=next() ) v <<= 6;
1788     static const unsigned int ofs[6] = {
1789       0x00000000U, 0x00003080U, 0x000E2080U,
1790       0x03C82080U, 0xFA082080U, 0x82082080U
1791     };
1792     v -= ofs[n];
1793   }
1794   else
1795     v = ch;
1796   return v;
1797 }
1798
1799
1800 size_t BC_Resources::encode(const char *from_enc, const char *to_enc,
1801         char *input, int input_length, char *output, int output_length)
1802 {
1803         if( !from_enc || *from_enc == 0 ||
1804             !strcmp(from_enc,"UTF8") || !strcmp(from_enc, "US-ASCII") )
1805                 from_enc = "UTF-8";
1806
1807         if( !to_enc || *to_enc == 0 ||
1808             !strcmp(to_enc,"UTF8") || !strcmp(to_enc, "US-ASCII") )
1809                 to_enc = "UTF-8";
1810
1811         iconv_t cd;
1812         char *outbase = output;
1813         size_t inbytes = input_length < 0 ? strlen(input) : input_length;
1814         size_t outbytes = output_length;
1815
1816         if( inbytes && outbytes ) {
1817                 if( !strcmp(from_enc, to_enc) ) {
1818                         if( inbytes > outbytes ) inbytes = outbytes;
1819                         memcpy(output,  input, inbytes);
1820                         output += inbytes;
1821                         outbytes -= inbytes;
1822                 }
1823                 else if( !strcmp(from_enc, "UTF-8") && !strcmp(to_enc,"UTF32LE") ) {
1824                         utf8conv uc(0,0, input,inbytes);
1825                         uint32_t *op = (uint32_t *)output;
1826                         uint32_t *ep = (uint32_t *)(output+output_length);
1827                         for( int wch; op<ep && (wch=uc.wnext())>=0; *op++=wch );
1828                         output = (char *)op;
1829                         outbytes = (char*)ep - output;
1830                 }
1831                 else if( !strcmp(from_enc, "UTF32LE") && !strcmp(to_enc,"UTF-8") ) {
1832                         utf8conv uc(output,output_length, 0,0);
1833                         uint32_t *ip = (uint32_t *)input;
1834                         uint32_t *ep = (uint32_t *)(input+inbytes);
1835                         for( ; ip<ep && uc.wnext(*ip)>=0; ++ip );
1836                         output += uc.olen();
1837                         outbytes -= uc.olen();
1838                 }
1839                 else if( (cd = iconv_open(to_enc, from_enc)) != (iconv_t)-1 ) {
1840                         iconv(cd, &input, &inbytes, &output, &outbytes);
1841                         iconv_close(cd);
1842                 }
1843                 else {
1844                         printf(_("Conversion from %s to %s is not available\n"),
1845                                 from_enc, to_enc);
1846                 }
1847         }
1848         if( outbytes > sizeof(uint32_t) )
1849                 outbytes = sizeof(uint32_t);
1850         for( uint32_t i = 0; i < outbytes; i++)
1851                 output[i] = 0;
1852         return output - outbase;
1853 }
1854
1855 void BC_Resources::encode_to_utf8(char *buffer, int buflen)
1856 {
1857         if(BC_Resources::locale_utf8) return;
1858         char lbuf[buflen];
1859         encode(encoding, 0, buffer, -1, lbuf, buflen);
1860         strcpy(buffer, lbuf);
1861 }
1862
1863 int BC_Resources::find_font_by_char(FT_ULong char_code, char *path_new, const FT_Face oldface)
1864 {
1865         FcPattern *font, *ofont;
1866         FcChar8 *file;
1867         int result = 0;
1868
1869         *path_new = 0;
1870
1871         // Do not search control codes
1872         if(char_code < ' ')
1873                 return 0;
1874
1875         if( (ofont = fcFreeTypeQueryFace(oldface, (const FcChar8*)"", 4097, 0)) != 0 ) {
1876                 if( (font = find_similar_font(char_code, ofont)) != 0 ) {
1877                         if(fcPatternGetString(font, FC_FILE, 0, &file) == FcResultMatch) {
1878                                 strcpy(path_new, (char*)file);
1879                                 result = 1;
1880                         }
1881                         fcPatternDestroy(font);
1882                 }
1883                 fcPatternDestroy(ofont);
1884         }
1885         return result;
1886 }
1887
1888 FcPattern* BC_Resources::find_similar_font(FT_ULong char_code, FcPattern *oldfont)
1889 {
1890         FcPattern *pat, *font;
1891         FcFontSet *fs;
1892         FcObjectSet *os;
1893         FcCharSet *fcs;
1894         int ival;
1895
1896         // Do not search control codes
1897         if(char_code < ' ')
1898                 return 0;
1899
1900         fontconfig_lock.lock("BC_Resources::find_similar_font");
1901         pat = fcPatternCreate();
1902         os = fcObjectSetBuild(FC_FILE, FC_CHARSET, FC_SCALABLE, FC_FAMILY,
1903                 FC_SLANT, FC_WEIGHT, FC_WIDTH, (char *)0);
1904
1905         fcPatternAddBool(pat, FC_SCALABLE, true);
1906         fcs = fcCharSetCreate();
1907         if(fcCharSetAddChar(fcs, char_code))
1908                 fcPatternAddCharSet(pat, FC_CHARSET, fcs);
1909         fcCharSetDestroy(fcs);
1910         for( int i=0; i<(int)LEN_FCPROP; ++i ) {
1911                 if(fcPatternGetInteger(oldfont, fc_properties[i], 0, &ival) == FcResultMatch)
1912                         fcPatternAddInteger(pat, fc_properties[i], ival);
1913         }
1914         fs = fcFontList(0, pat, os);
1915         for( int i=LEN_FCPROP; --i>=0 && !fs->nfont; ) {
1916                 fcFontSetDestroy(fs);
1917                 fcPatternDel(pat, fc_properties[i]);
1918                 fs = fcFontList(0, pat, os);
1919         }
1920         fcPatternDestroy(pat);
1921         fcObjectSetDestroy(os);
1922
1923         pat = 0;
1924
1925         for (int i = 0; i < fs->nfont; i++)
1926         {
1927                 font = fs->fonts[i];
1928                 if(fcPatternGetCharSet(font, FC_CHARSET, 0, &fcs) == FcResultMatch)
1929                 {
1930                         if(fcCharSetHasChar(fcs, char_code))
1931                         {
1932                                 pat =  fcPatternDuplicate(font);
1933                                 break;
1934                         }
1935                 }
1936         }
1937         fcFontSetDestroy(fs);
1938         fontconfig_lock.unlock();
1939
1940         return pat;
1941 }
1942
1943 void BC_Resources::dump_fonts(FILE *fp)
1944 {
1945         for( int i=0; i<fontlist->total; ++i )
1946                 dump_font_entry(fp, "", fontlist->values[i]);
1947 }
1948
1949 void BC_Resources::dump_font_entry(FILE *fp, const char *cp,  BC_FontEntry *ep)
1950 {
1951         fprintf(fp,"%s%s = %s\n",cp,ep->displayname,ep->path);
1952         fprintf(fp,"  %s:%s:%s:%s:%s:%s:%d:%d:%d:%d:%d:%s:%d:%s:%s:%d\n",
1953                 ep->foundry, ep->family, ep->weight, ep->slant, ep->swidth, ep->adstyle,
1954                 ep->pixelsize, ep->pointsize, ep->xres, ep->yres, ep->style, ep->spacing,
1955                 ep->avg_width, ep->registry, ep->encoding, ep->fixed_style);
1956 }
1957