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