fix awdw solo vicon crash, fix nested clip for binfolders, open edit edl
[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         tumblepatch_data = 0;
789         tumble_duration = 150;
790
791         ok_images = default_ok_images;
792         cancel_images = default_cancel_images;
793         usethis_button_images = default_usethis_images;
794         filebox_descend_images = default_ok_images;
795
796         menu_light = LTCYAN;
797         menu_highlighted = LTBLUE;
798         menu_down = MDCYAN;
799         menu_up = MECYAN;
800         menu_shadow = DKCYAN;
801
802         menu_title_bg = default_menubar_data;
803         menu_popup_bg = default_menu_popup_bg;
804         menu_bar_bg = default_menu_bar_bg;
805
806         popupmenu_images = 0;
807
808
809         popupmenu_margin = xS(10);
810         popupmenu_btnup = 1;
811         popupmenu_triangle_margin = xS(10);
812
813         min_menu_w = 0;
814         menu_title_text = BLACK;
815         popup_title_text = BLACK;
816         menu_item_text = BLACK;
817         menu_highlighted_fontcolor = BLACK;
818         progress_text = BLACK;
819         grab_input_focus = 1;
820
821         text_default = BLACK;
822         highlight_inverse = WHITE ^ BLUE;
823         text_background = WHITE;
824         text_background_disarmed = 0xc08080;
825         text_background_hi = LTYELLOW;
826         text_background_noborder_hi = LTGREY;
827         text_background_noborder = -1;
828         text_border1 = DKGREY;
829         text_border2 = BLACK;
830         text_border2_hi = RED;
831         text_border3 = MEGREY;
832         text_border3_hi = LTPINK;
833         text_border4 = WHITE;
834         text_highlight = BLUE;
835         text_selected_highlight = SLBLUE;
836         text_inactive_highlight = MEGREY;
837
838         toggle_highlight_bg = 0;
839         toggle_text_margin = 0;
840
841 // Delays must all be different for repeaters
842         double_click = 300;
843         blink_rate = 250;
844         scroll_repeat = 150;
845         tooltip_delay = 1000;
846         tooltip_bg_color = YELLOW;
847         tooltips_enabled = 1;
848         textbox_focus_policy = 0;
849
850         filebox_margin = yS(110);
851         dirbox_margin = yS(90);
852         filebox_mode = LISTBOX_TEXT;
853         sprintf(filebox_filter, "*");
854         filebox_w = xS(640);
855         filebox_h = yS(480);
856         filebox_columntype[0] = FILEBOX_NAME;
857         filebox_columntype[1] = FILEBOX_SIZE;
858         filebox_columntype[2] = FILEBOX_DATE;
859         filebox_columntype[3] = FILEBOX_EXTENSION;
860         filebox_columnwidth[0] = xS(200);
861         filebox_columnwidth[1] = xS(100);
862         filebox_columnwidth[2] = xS(100);
863         filebox_columnwidth[3] = xS(100);
864         dirbox_columntype[0] = FILEBOX_NAME;
865         dirbox_columntype[1] = FILEBOX_DATE;
866         dirbox_columnwidth[0] = xS(200);
867         dirbox_columnwidth[1] = xS(100);
868
869         filebox_text_images = default_filebox_text_images;
870         filebox_icons_images = default_filebox_icons_images;
871         filebox_updir_images = default_filebox_updir_images;
872         filebox_newfolder_images = default_filebox_newfolder_images;
873         filebox_rename_images = default_filebox_rename_images;
874         filebox_delete_images = default_filebox_delete_images;
875         filebox_reload_images = default_filebox_reload_images;
876         filebox_szfmt_images = default_filebox_szfmt_images;
877         filebox_size_format = FILEBOX_SIZE_RAW;
878         directory_color = BLUE;
879         file_color = BLACK;
880
881         filebox_sortcolumn = 0;
882         filebox_sortorder = BC_ListBox::SORT_ASCENDING;
883         dirbox_sortcolumn = 0;
884         dirbox_sortorder = BC_ListBox::SORT_ASCENDING;
885
886
887         pot_images = default_pot_images;
888         pot_offset = 2;
889         pot_x1 = pot_images[0]->get_w() / 2 - xS(pot_offset);
890         pot_y1 = pot_images[0]->get_h() / 2 - yS(pot_offset);
891         pot_r = pot_x1;
892         pot_needle_color = BLACK;
893
894         progress_images = default_progress_images;
895
896         xmeter_images = 0;
897         ymeter_images = 0;
898         meter_font = SMALLFONT_3D;
899         meter_font_color = RED;
900         meter_title_w = xS(20);
901         meter_3d = 1;
902         medium_7segment = default_medium_7segment;
903
904         audiovideo_color = RED;
905
906         use_fontset = 0;
907
908 // Xft has priority over font set
909 #ifdef HAVE_XFT
910 // But Xft dies in 32 bit mode after some amount of drawing.
911         use_xft = 1;
912 #else
913         use_xft = 0;
914 #endif
915
916         drag_radius = xS(10);
917         recursive_resizing = 1;
918 }
919
920 void BC_Resources::del_vframes(VFrame *vframes[], int n)
921 {
922         while( --n >= 0 ) delete vframes[n];
923 }
924
925 BC_Resources::~BC_Resources()
926 {
927         delete id_lock;
928         delete create_window_lock;
929         del_vframes(default_type_to_icon, 6);
930         delete default_bar;
931         del_vframes(default_cancel_images, 3);
932         del_vframes(default_ok_images, 3);
933         del_vframes(default_usethis_images, 3);
934 #if 0
935         del_vframes(default_checkbox_images, 5);
936         del_vframes(default_radial_images, 5);
937         del_vframes(default_label_images, 5);
938 #endif
939         del_vframes(default_menuitem_data, 3);
940         del_vframes(default_menubar_data, 3);
941         delete default_menu_popup_bg;
942         delete default_menu_bar_bg;
943         delete default_check_image;
944         del_vframes(default_filebox_text_images, 3);
945         del_vframes(default_filebox_icons_images, 3);
946         del_vframes(default_filebox_updir_images, 3);
947         del_vframes(default_filebox_newfolder_images, 3);
948         del_vframes(default_filebox_rename_images, 3);
949         del_vframes(default_filebox_delete_images, 3);
950         del_vframes(default_filebox_reload_images, 3);
951         del_vframes(default_filebox_szfmt_images, 12);
952         del_vframes(default_listbox_button, 4);
953         delete default_listbox_bg;
954         del_vframes(default_listbox_expand, 5);
955         del_vframes(default_listbox_column, 3);
956         delete default_listbox_up;
957         delete default_listbox_dn;
958         del_vframes(default_pot_images, 3);
959         del_vframes(default_progress_images, 2);
960         del_vframes(default_medium_7segment, 20);
961         del_vframes(default_vscroll_data, 10);
962         del_vframes(default_hscroll_data, 10);
963         if( fontlist ) {
964                 fontlist->remove_all_objects();
965                 delete fontlist;
966         }
967         delete default_icon_img;
968         finit_font_defs();
969 }
970
971 int BC_Resources::initialize_display(BC_WindowBase *window)
972 {
973 // Set up IPC cleanup handlers
974 //      bc_init_ipc();
975
976 // Test for shm.  Must come before yuv test
977         init_shm(window);
978         return 0;
979 }
980
981
982 void BC_Resources::init_shm(BC_WindowBase *window)
983 {
984         use_shm = 0;
985         int (*old_handler)(Display *, XErrorEvent *) =
986                 XSetErrorHandler(BC_Resources::x_error_handler);
987
988         if(XShmQueryExtension(window->display))
989         {
990                 XShmSegmentInfo test_shm;
991                 memset(&test_shm,0,sizeof(test_shm));
992                 XImage *test_image = XShmCreateImage(window->display, window->vis,
993                         window->default_depth, ZPixmap, (char*)NULL, &test_shm, 5, 5);
994                 BC_Resources::error = 0;
995                 test_shm.shmid = shmget(IPC_PRIVATE, 5 * test_image->bytes_per_line, (IPC_CREAT | 0600));
996                 if(test_shm.shmid != -1) {
997                         char *data = (char *)shmat(test_shm.shmid, NULL, 0);
998                         if(data != (void *)-1) {
999                                 shmctl(test_shm.shmid, IPC_RMID, 0);
1000                                 test_shm.shmaddr = data;
1001                                 test_shm.readOnly = 0;
1002
1003                                 if(XShmAttach(window->display, &test_shm)) {
1004                                         XSync(window->display, False);
1005                                         use_shm = 1;
1006                                 }
1007                                 shmdt(data);
1008                         }
1009                 }
1010
1011                 XDestroyImage(test_image);
1012                 if(BC_Resources::error) use_shm = 0;
1013         }
1014         XSetErrorHandler(old_handler);
1015 }
1016
1017
1018
1019
1020 BC_Synchronous* BC_Resources::get_synchronous()
1021 {
1022         return synchronous;
1023 }
1024
1025 void BC_Resources::set_synchronous(BC_Synchronous *synchronous)
1026 {
1027         this->synchronous = synchronous;
1028 }
1029
1030
1031
1032
1033 int BC_Resources::get_bg_color() { return bg_color; }
1034
1035 int BC_Resources::get_bg_shadow1() { return bg_shadow1; }
1036
1037 int BC_Resources::get_bg_shadow2() { return bg_shadow2; }
1038
1039 int BC_Resources::get_bg_light1() { return bg_light1; }
1040
1041 int BC_Resources::get_bg_light2() { return bg_light2; }
1042
1043
1044 int BC_Resources::get_id()
1045 {
1046         id_lock->lock("BC_Resources::get_id");
1047         int result = id++;
1048         id_lock->unlock();
1049         return result;
1050 }
1051
1052 int BC_Resources::get_filebox_id()
1053 {
1054         id_lock->lock("BC_Resources::get_filebox_id");
1055         int result = filebox_id++;
1056         id_lock->unlock();
1057         return result;
1058 }
1059
1060
1061 void BC_Resources::set_signals(BC_Signals *signal_handler)
1062 {
1063         BC_Resources::signal_handler = signal_handler;
1064 }
1065
1066 int BC_Resources::init_fontconfig(const char *search_path)
1067 {
1068         if( fontlist ) return 0;
1069         fontlist = new ArrayList<BC_FontEntry*>;
1070
1071 #define get_str(str,sep,ptr,cond) do { char *out = str; \
1072   while( *ptr && !strchr(sep,*ptr) && (cond) ) *out++ = *ptr++; \
1073   *out = 0; \
1074 } while(0)
1075
1076 #define skip_str(sep, ptr) do { \
1077   while( *ptr && strchr(sep,*ptr) ) ++ptr; \
1078 } while(0)
1079
1080         char find_command[BCTEXTLEN];
1081         char *fp = find_command, *ep = fp+sizeof(find_command)-1;
1082         fp += snprintf(fp, ep-fp, "%s", "find");
1083         const char *bc_font_path = getenv("BC_FONT_PATH");
1084 // if BC_FONT_PATH starts with ':', omit default path
1085         if( !(bc_font_path && bc_font_path[0] == ':') )
1086                 fp += snprintf(fp, ep-fp, " '%s'", search_path);
1087         if( bc_font_path ) {
1088                 const char *path = bc_font_path;
1089                 while( *path ) {
1090                         char font_path[BCTEXTLEN];
1091                         const char *cp = strchr(path,':');
1092                         int len = !cp ? strlen(path) : cp-path;
1093                         if( len > 0 ) {
1094                                 memcpy(font_path, path, len);
1095                                 font_path[len] = 0;  path += len;
1096                                 fp += snprintf(fp, ep-fp, " '%s'", font_path);
1097                         }
1098                         if( cp ) ++path;
1099                 }
1100         }
1101         fp += snprintf(fp, ep-fp, " -name 'fonts.scale' -print -exec cat {} \\;");
1102         FILE *in = popen(find_command, "r");
1103
1104         FT_Library freetype_library = 0;
1105 //      FT_Face freetype_face = 0;
1106 //      ft_Init_FreeType(&freetype_library);
1107
1108         char line[BCTEXTLEN], current_dir[BCTEXTLEN];
1109         current_dir[0] = 0;
1110
1111         while( !feof(in) && fgets(line, BCTEXTLEN, in) ) {
1112                 if(!strlen(line)) break;
1113
1114                 char *in_ptr = line;
1115
1116 // Get current directory
1117                 if(line[0] == '/') {
1118                         get_str(current_dir, "\n", in_ptr,1);
1119                         for( int i=strlen(current_dir); --i>=0 && current_dir[i]!='/'; )
1120                                 current_dir[i] = 0;
1121                         continue;
1122                 }
1123
1124 //printf("TitleMain::build_fonts %s\n", line);
1125                 BC_FontEntry *entry = new BC_FontEntry;
1126                 char string[BCTEXTLEN];
1127 // Path
1128                 get_str(string, "\n", in_ptr, in_ptr[0]!=' ' || in_ptr[1]!='-');
1129                 entry->path = cstrcat(2, current_dir, string);
1130 // Foundary
1131                 skip_str(" -", in_ptr);
1132                 get_str(string, "-\n", in_ptr, 1);
1133                 if( !string[0] ) { delete entry;  continue; }
1134                 entry->foundry = cstrdup(string);
1135                 if(*in_ptr == '-') in_ptr++;
1136 // Family
1137                 get_str(string, "-\n", in_ptr, 1);
1138                 if( !string[0] ) { delete entry;  continue; }
1139                 entry->family = cstrdup(string);
1140                 if(*in_ptr == '-') in_ptr++;
1141 // Weight
1142                 get_str(string, "-\n", in_ptr, 1);
1143                 entry->weight = cstrdup(string);
1144                 if(*in_ptr == '-') in_ptr++;
1145 // Slant
1146                 get_str(string, "-\n", in_ptr, 1);
1147                 entry->slant = cstrdup(string);
1148                 if(*in_ptr == '-') in_ptr++;
1149 // SWidth
1150                 get_str(string, "-\n", in_ptr, 1);
1151                 entry->swidth = cstrdup(string);
1152                 if(*in_ptr == '-') in_ptr++;
1153 // Adstyle
1154                 get_str(string, "-\n", in_ptr, 1);
1155                 entry->adstyle = cstrdup(string);
1156                 if(*in_ptr == '-') in_ptr++;
1157 // pixelsize
1158                 get_str(string, "-\n", in_ptr, 1);
1159                 entry->pixelsize = atol(string);
1160                 if(*in_ptr == '-') in_ptr++;
1161 // pointsize
1162                 get_str(string, "-\n", in_ptr, 1);
1163                 entry->pointsize = atol(string);
1164                 if(*in_ptr == '-') in_ptr++;
1165 // xres
1166                 get_str(string, "-\n", in_ptr, 1);
1167                 entry->xres = atol(string);
1168                 if(*in_ptr == '-') in_ptr++;
1169 // yres
1170                 get_str(string, "-\n", in_ptr, 1);
1171                 entry->yres = atol(string);
1172                 if(*in_ptr == '-') in_ptr++;
1173 // spacing
1174                 get_str(string, "-\n", in_ptr, 1);
1175                 entry->spacing = cstrdup(string);
1176                 if(*in_ptr == '-') in_ptr++;
1177 // avg_width
1178                 get_str(string, "-\n", in_ptr, 1);
1179                 entry->avg_width = atol(string);
1180                 if(*in_ptr == '-') in_ptr++;
1181 // registry
1182                 get_str(string, "-\n", in_ptr, 1);
1183                 entry->registry = cstrdup(string);
1184                 if(*in_ptr == '-') in_ptr++;
1185 // encoding
1186                 get_str(string, "-\n", in_ptr, 1);
1187                 entry->encoding = cstrdup(string);
1188                 if(*in_ptr == '-') in_ptr++;
1189
1190 // Add to list
1191 //printf("TitleMain::build_fonts 1 %s\n", entry->path);
1192 // This takes a real long time to do.  Instead just take all fonts
1193 //              if(!load_freetype_face(freetype_library,
1194 //                      freetype_face, entry->path) )
1195 // Fix parameters
1196                 sprintf(line, "%s (%s)", entry->family, entry->foundry);
1197                 entry->displayname = cstrdup(line);
1198
1199                 if(!strcasecmp(entry->weight, "demibold")) {
1200                         entry->fixed_style |= BC_FONT_BOLD;
1201                         entry->style |= FL_WEIGHT_DEMIBOLD;
1202                 }
1203                 else if(!strcasecmp(entry->weight, "bold")) {
1204                         entry->fixed_style |= BC_FONT_BOLD;
1205                         entry->style |= FL_WEIGHT_BOLD;
1206                 }
1207                 else {
1208                         entry->style |= FL_WEIGHT_NORMAL;
1209                 }
1210
1211                 if(!strcasecmp(entry->slant, "r")) {
1212                         entry->style |= FL_SLANT_ROMAN;
1213                 }
1214                 else if(!strcasecmp(entry->slant, "i")) {
1215                         entry->style |= FL_SLANT_ITALIC;
1216                         entry->fixed_style |= BC_FONT_ITALIC;
1217                 }
1218                 else if(!strcasecmp(entry->slant, "o")) {
1219                         entry->style |= FL_SLANT_OBLIQUE;
1220                         entry->fixed_style |= BC_FONT_ITALIC;
1221                 }
1222
1223                 if(!strcasecmp(entry->swidth, "normal"))
1224                         entry->style |= FL_WIDTH_NORMAL;
1225                 else if(!strcasecmp(entry->swidth, "ultracondensed"))
1226                         entry->style |= FL_WIDTH_ULTRACONDENSED;
1227                 else if(!strcasecmp(entry->swidth, "extracondensed"))
1228                         entry->style |= FL_WIDTH_EXTRACONDENSED;
1229                 else if(!strcasecmp(entry->swidth, "condensed"))
1230                         entry->style |= FL_WIDTH_CONDENSED;
1231                 else if(!strcasecmp(entry->swidth, "semicondensed"))
1232                         entry->style |= FL_WIDTH_SEMICONDENSED;
1233                 else if(!strcasecmp(entry->swidth, "semiexpanded"))
1234                         entry->style |= FL_WIDTH_SEMIEXPANDED;
1235                 else if(!strcasecmp(entry->swidth, "expanded"))
1236                         entry->style |= FL_WIDTH_EXPANDED;
1237                 else if(!strcasecmp(entry->swidth, "extraexpanded"))
1238                         entry->style |= FL_WIDTH_EXTRAEXPANDED;
1239                 else if(!strcasecmp(entry->swidth, "ultraexpanded"))
1240                         entry->style |= FL_WIDTH_ULTRAEXPANDED;
1241                 else
1242                         entry->style |= FL_WIDTH_NORMAL;
1243
1244                 fontlist->append(entry);
1245                 if( font_debug )
1246                         dump_font_entry(stdout, "font 0: ", entry);
1247
1248 //              printf("TitleMain::build_fonts %s: success\n",  entry->path);
1249 //printf("TitleMain::build_fonts 2\n");
1250         }
1251         pclose(in);
1252
1253         if( bc_font_path && bc_font_path[0] == ':' )
1254                 return 0;
1255
1256 // Load all the fonts from fontconfig
1257         FcPattern *pat;
1258         FcFontSet *fs;
1259         FcObjectSet *os;
1260         FcChar8 *family, *file, *foundry, *style, *format;
1261         int slant, spacing, width, weight;
1262         int force_style = 0;
1263 // if you want limit search to TrueType put 1
1264         int limit_to_trutype = 1;
1265         FcConfig *config;
1266         int i;
1267         char tmpstring[BCTEXTLEN];
1268         if(!fcInit())
1269                 return 1;
1270         config = fcConfigGetCurrent();
1271         fcConfigSetRescanInterval(config, 0);
1272
1273         pat = fcPatternCreate();
1274         os = fcObjectSetBuild( FC_FAMILY, FC_FILE, FC_FOUNDRY, FC_WEIGHT,
1275                 FC_WIDTH, FC_SLANT, FC_FONTFORMAT, FC_SPACING, FC_STYLE, (char *) 0);
1276         fcPatternAddBool(pat, FC_SCALABLE, true);
1277
1278         if(language[0]) {
1279                 char langstr[LEN_LANG * 3];
1280                 strcpy(langstr, language);
1281
1282                 if(region[0]) {
1283                         strcat(langstr, "-");
1284                         strcat(langstr, region);
1285                 }
1286
1287                 FcLangSet *ls =  fcLangSetCreate();
1288                 if(fcLangSetAdd(ls, (const FcChar8*)langstr))
1289                 if(fcPatternAddLangSet(pat, FC_LANG, ls))
1290                 fcLangSetDestroy(ls);
1291         }
1292
1293         fs = fcFontList(config, pat, os);
1294         fcPatternDestroy(pat);
1295         fcObjectSetDestroy(os);
1296
1297         for (i = 0; fs && i < fs->nfont; i++) {
1298                 FcPattern *font = fs->fonts[i];
1299                 force_style = 0;
1300                 fcPatternGetString(font, FC_FONTFORMAT, 0, &format);
1301                 //on this point you can limit font search
1302                 if(limit_to_trutype && strcmp((char *)format, "TrueType"))
1303                         continue;
1304
1305                 sprintf(tmpstring, "%s", format);
1306                 BC_FontEntry *entry = new BC_FontEntry;
1307                 if(fcPatternGetString(font, FC_FILE, 0, &file) == FcResultMatch) {
1308                         entry->path = cstrdup((char*)file);
1309                 }
1310
1311                 if(fcPatternGetString(font, FC_FOUNDRY, 0, &foundry) == FcResultMatch) {
1312                         entry->foundry = cstrdup((char*)foundry);
1313                 }
1314
1315                 if(fcPatternGetInteger(font, FC_WEIGHT, 0, &weight) == FcResultMatch) {
1316                         switch(weight) {
1317                         case FC_WEIGHT_THIN:
1318                         case FC_WEIGHT_EXTRALIGHT:
1319                         case FC_WEIGHT_LIGHT:
1320                         case FC_WEIGHT_BOOK:
1321                                 force_style = 1;
1322                                 entry->weight = cstrdup("medium");
1323                                 break;
1324
1325                         case FC_WEIGHT_NORMAL:
1326                         case FC_WEIGHT_MEDIUM:
1327                         default:
1328                                 entry->weight = cstrdup("medium");
1329                                 break;
1330
1331                         case FC_WEIGHT_BLACK:
1332                         case FC_WEIGHT_SEMIBOLD:
1333                         case FC_WEIGHT_BOLD:
1334                                 entry->weight = cstrdup("bold");
1335                                 entry->fixed_style |= BC_FONT_BOLD;
1336                                 break;
1337
1338                         case FC_WEIGHT_EXTRABOLD:
1339                         case FC_WEIGHT_EXTRABLACK:
1340                                 force_style = 1;
1341                                 entry->weight = cstrdup("bold");
1342                                 entry->fixed_style |= BC_FONT_BOLD;
1343                                 break;
1344                         }
1345                 }
1346
1347                 if(fcPatternGetString(font, FC_FAMILY, 0, &family) == FcResultMatch)
1348                         entry->family = cstrdup((char*)family);
1349
1350                 if(fcPatternGetInteger(font, FC_SLANT, 0, &slant) == FcResultMatch) {
1351                         switch(slant) {
1352                         case FC_SLANT_ROMAN:
1353                         default:
1354                                 entry->slant = cstrdup("r");
1355                                 entry->style |= FL_SLANT_ROMAN;
1356                                 break;
1357                         case FC_SLANT_ITALIC:
1358                                 entry->slant = cstrdup("i");
1359                                 entry->style |= FL_SLANT_ITALIC;
1360                                 entry->fixed_style |= BC_FONT_ITALIC;
1361                                 break;
1362                         case FC_SLANT_OBLIQUE:
1363                                 entry->slant = cstrdup("o");
1364                                 entry->style |= FL_SLANT_OBLIQUE;
1365                                 entry->fixed_style |= BC_FONT_ITALIC;
1366                                 break;
1367                         }
1368                 }
1369
1370                 if(fcPatternGetInteger(font, FC_WIDTH, 0, &width) == FcResultMatch) {
1371                         switch(width) {
1372                         case FC_WIDTH_ULTRACONDENSED:
1373                                 entry->swidth = cstrdup("ultracondensed");
1374                                 break;
1375
1376                         case FC_WIDTH_EXTRACONDENSED:
1377                                 entry->swidth = cstrdup("extracondensed");
1378                                 break;
1379
1380                         case FC_WIDTH_CONDENSED:
1381                                 entry->swidth = cstrdup("condensed");
1382                                 break;
1383                         case FC_WIDTH_SEMICONDENSED:
1384                                 entry->swidth = cstrdup("semicondensed");
1385                                 break;
1386
1387                         case FC_WIDTH_NORMAL:
1388                         default:
1389                                 entry->swidth = cstrdup("normal");
1390                                 break;
1391
1392                         case FC_WIDTH_SEMIEXPANDED:
1393                                 entry->swidth = cstrdup("semiexpanded");
1394                                 break;
1395
1396                         case FC_WIDTH_EXPANDED:
1397                                 entry->swidth = cstrdup("expanded");
1398                                 break;
1399
1400                         case FC_WIDTH_EXTRAEXPANDED:
1401                                 entry->swidth = cstrdup("extraexpanded");
1402                                 break;
1403
1404                         case FC_WIDTH_ULTRAEXPANDED:
1405                                 entry->swidth = cstrdup("ultraexpanded");
1406                                 break;
1407                         }
1408                 }
1409
1410                 if(fcPatternGetInteger(font, FC_SPACING, 0, &spacing) == FcResultMatch) {
1411                         switch(spacing) {
1412                         case 0:
1413                         default:
1414                                 entry->spacing = cstrdup("p");
1415                                 break;
1416
1417                         case 90:
1418                                 entry->spacing = cstrdup("d");
1419                                 break;
1420
1421                         case 100:
1422                                 entry->spacing = cstrdup("m");
1423                                 break;
1424
1425                         case 110:
1426                                 entry->spacing = cstrdup("c");
1427                                 break;
1428                         }
1429                 }
1430
1431                 // Add fake stuff for compatibility
1432                 entry->adstyle = cstrdup(" ");
1433                 entry->pixelsize = 0;
1434                 entry->pointsize = 0;
1435                 entry->xres = 0;
1436                 entry->yres = 0;
1437                 entry->avg_width = 0;
1438                 entry->registry = cstrdup("utf");
1439                 entry->encoding = cstrdup("8");
1440
1441                 if( fcPatternGetString(font, FC_STYLE, 0, &style) != FcResultMatch )
1442                         force_style = 0;
1443
1444                 // If font has a style unmanaged by titler plugin, force style to be displayed on name
1445                 // in this way we can shown all available fonts styles.
1446                 if(force_style) {
1447                         sprintf(tmpstring, "%s (%s)", entry->family, style);
1448                         entry->displayname = cstrdup(tmpstring);
1449                 }
1450                 else {
1451                         if(strcmp(entry->foundry, "unknown")) {
1452                                 sprintf(tmpstring, "%s (%s)", entry->family, entry->foundry);
1453                                 entry->displayname = cstrdup(tmpstring);
1454                         }
1455                         else {
1456                                 sprintf(tmpstring, "%s", entry->family);
1457                                 entry->displayname = cstrdup(tmpstring);
1458                         }
1459
1460                 }
1461                 fontlist->append(entry);
1462                 if( font_debug )
1463                         dump_font_entry(stdout, "font 1: ", entry);
1464         }
1465
1466         fcFontSetDestroy(fs);
1467         if(freetype_library)
1468                 ft_Done_FreeType(freetype_library);
1469 // for(int i = 0; i < fonts->total; i++)
1470 //      fonts->values[i]->dump();
1471
1472         fcConfigAppFontAddDir(0, (const FcChar8*)search_path);
1473         fcConfigSetRescanInterval(0, 0);
1474
1475         os = fcObjectSetBuild(FC_FAMILY, FC_FILE, FC_FOUNDRY, FC_WEIGHT,
1476                 FC_WIDTH, FC_SLANT, FC_SPACING, FC_STYLE, (char *)0);
1477         pat = fcPatternCreate();
1478         fcPatternAddBool(pat, FC_SCALABLE, true);
1479
1480         if(language[0])
1481         {
1482                 char langstr[LEN_LANG * 3];
1483                 strcpy(langstr, language);
1484
1485                 if(region[0])
1486                 {
1487                         strcat(langstr, "-");
1488                         strcat(langstr, region);
1489                 }
1490
1491                 FcLangSet *ls =  fcLangSetCreate();
1492                 if(fcLangSetAdd(ls, (const FcChar8*)langstr))
1493                         if(fcPatternAddLangSet(pat, FC_LANG, ls))
1494                 fcLangSetDestroy(ls);
1495         }
1496
1497         fs = fcFontList(0, pat, os);
1498         fcPatternDestroy(pat);
1499         fcObjectSetDestroy(os);
1500
1501         for(int i = 0; i < fs->nfont; i++)
1502         {
1503                 FcPattern *font = fs->fonts[i];
1504                 BC_FontEntry *entry = new BC_FontEntry;
1505
1506                 FcChar8 *strvalue;
1507                 if(fcPatternGetString(font, FC_FILE, 0, &strvalue) == FcResultMatch)
1508                 {
1509                         entry->path = new char[strlen((char*)strvalue) + 1];
1510                         strcpy(entry->path, (char*)strvalue);
1511                 }
1512
1513                 if(fcPatternGetString(font, FC_FOUNDRY, 0, &strvalue) == FcResultMatch)
1514                 {
1515                         entry->foundry = new char[strlen((char*)strvalue) + 1];
1516                         strcpy(entry->foundry, (char *)strvalue);
1517                 }
1518
1519                 if(fcPatternGetString(font, FC_FAMILY, 0, &strvalue) == FcResultMatch)
1520                 {
1521                         entry->family = new char[strlen((char*)strvalue) + 2];
1522                         strcpy(entry->family, (char*)strvalue);
1523                 }
1524
1525                 int intvalue;
1526                 if(fcPatternGetInteger(font, FC_SLANT, 0, &intvalue) == FcResultMatch)
1527                 {
1528                         switch(intvalue)
1529                         {
1530                         case FC_SLANT_ROMAN:
1531                         default:
1532                                 entry->style |= FL_SLANT_ROMAN;
1533                                 break;
1534
1535                         case FC_SLANT_ITALIC:
1536                                 entry->style |= FL_SLANT_ITALIC;
1537                                 break;
1538
1539                         case FC_SLANT_OBLIQUE:
1540                                 entry->style |= FL_SLANT_OBLIQUE;
1541                                 break;
1542                         }
1543                 }
1544
1545                 if(fcPatternGetInteger(font, FC_WEIGHT, 0, &intvalue) == FcResultMatch)
1546                 {
1547                         switch(intvalue)
1548                         {
1549                         case FC_WEIGHT_THIN:
1550                                 entry->style |= FL_WEIGHT_THIN;
1551                                 break;
1552
1553                         case FC_WEIGHT_EXTRALIGHT:
1554                                 entry->style |= FL_WEIGHT_EXTRALIGHT;
1555                                 break;
1556
1557                         case FC_WEIGHT_LIGHT:
1558                                 entry->style |= FL_WEIGHT_LIGHT;
1559                                 break;
1560
1561                         case FC_WEIGHT_BOOK:
1562                                 entry->style |= FL_WEIGHT_BOOK;
1563                                 break;
1564
1565                         case FC_WEIGHT_NORMAL:
1566                         default:
1567                                 entry->style |= FL_WEIGHT_NORMAL;
1568                                 break;
1569
1570                         case FC_WEIGHT_MEDIUM:
1571                                 entry->style |= FL_WEIGHT_MEDIUM;
1572                                 break;
1573
1574                         case FC_WEIGHT_DEMIBOLD:
1575                                 entry->style |= FL_WEIGHT_DEMIBOLD;
1576                                 break;
1577
1578                         case FC_WEIGHT_BOLD:
1579                                 entry->style |= FL_WEIGHT_BOLD;
1580                                 break;
1581
1582                         case FC_WEIGHT_EXTRABOLD:
1583                                 entry->style |= FL_WEIGHT_EXTRABOLD;
1584                                 break;
1585
1586                         case FC_WEIGHT_BLACK:
1587                                 entry->style |= FL_WEIGHT_BLACK;
1588                                 break;
1589
1590                         case FC_WEIGHT_EXTRABLACK:
1591                                 entry->style |= FL_WEIGHT_EXTRABLACK;
1592                                 break;
1593                         }
1594                 }
1595
1596                 if(fcPatternGetInteger(font, FC_WIDTH, 0, &intvalue) == FcResultMatch)
1597                 {
1598                         switch(intvalue)
1599                         {
1600                         case FC_WIDTH_ULTRACONDENSED:
1601                                 entry->style |= FL_WIDTH_ULTRACONDENSED;
1602                                 break;
1603
1604                         case FC_WIDTH_EXTRACONDENSED:
1605                                 entry->style |= FL_WIDTH_EXTRACONDENSED;
1606                                 break;
1607
1608                         case FC_WIDTH_CONDENSED:
1609                                 entry->style |= FL_WIDTH_CONDENSED;
1610                                 break;
1611
1612                         case FC_WIDTH_SEMICONDENSED:
1613                                 entry->style = FL_WIDTH_SEMICONDENSED;
1614                                 break;
1615
1616                         case FC_WIDTH_NORMAL:
1617                         default:
1618                                 entry->style |= FL_WIDTH_NORMAL;
1619                                 break;
1620
1621                         case FC_WIDTH_SEMIEXPANDED:
1622                                 entry->style |= FL_WIDTH_SEMIEXPANDED;
1623                                 break;
1624
1625                         case FC_WIDTH_EXPANDED:
1626                                 entry->style |= FL_WIDTH_EXPANDED;
1627                                 break;
1628
1629                         case FC_WIDTH_EXTRAEXPANDED:
1630                                 entry->style |= FL_WIDTH_EXTRAEXPANDED;
1631                                 break;
1632
1633                         case FC_WIDTH_ULTRAEXPANDED:
1634                                 entry->style |= FL_WIDTH_ULTRAEXPANDED;
1635                                 break;
1636                         }
1637                 }
1638                 if(fcPatternGetInteger(font, FC_SPACING, 0, &intvalue) == FcResultMatch)
1639                 {
1640                         switch(intvalue)
1641                         {
1642                         case FC_PROPORTIONAL:
1643                         default:
1644                                 entry->style |= FL_PROPORTIONAL;
1645                                 break;
1646
1647                         case FC_DUAL:
1648                                 entry->style |= FL_DUAL;
1649                                 break;
1650
1651                         case FC_MONO:
1652                                 entry->style |= FL_MONO;
1653                                 break;
1654
1655                         case FC_CHARCELL:
1656                                 entry->style |= FL_CHARCELL;
1657                                 break;
1658                         }
1659                 }
1660                 if(entry->foundry && strcmp(entry->foundry, "unknown"))
1661                 {
1662                         char tempstr[BCTEXTLEN];
1663                         sprintf(tempstr, "%s (%s)", entry->family, entry->foundry);
1664                         entry->displayname = new char[strlen(tempstr) + 1];
1665                         strcpy(entry->displayname, tempstr);
1666                 }
1667                 else
1668                 {
1669                         entry->displayname = new char[strlen(entry->family) + 1];
1670                         strcpy(entry->displayname, entry->family);
1671                 }
1672                 fontlist->append(entry);
1673                 if( font_debug )
1674                         dump_font_entry(stdout, "font 2: ", entry);
1675         }
1676         fcFontSetDestroy(fs);
1677         return 0;
1678 }
1679
1680 #define STYLE_MATCH(fst, stl, msk) ((fst) & (msk) & (stl)) && \
1681        !((fst) & ~(style) & (msk))
1682
1683 BC_FontEntry *BC_Resources::find_fontentry(const char *displayname, int style,
1684         int mask, int preferred)
1685 {
1686         BC_FontEntry *entry, *style_match, *preferred_match;
1687
1688         if(!fontlist)
1689                 return 0;
1690
1691         style_match = 0;
1692         preferred_match = 0;
1693
1694         if(displayname)
1695         {
1696                 for(int i = 0; i < fontlist->total; i++)
1697                 {
1698                         entry = fontlist->values[i];
1699
1700                         if(strcmp(entry->displayname, displayname) == 0 &&
1701                                         STYLE_MATCH(entry->style, style, mask))
1702                         {
1703                                 if(!style_match)
1704                                         style_match = entry;
1705                                 if(!preferred_match && entry->fixed_style == preferred)
1706                                         preferred_match = entry;
1707                         }
1708                 }
1709                 if(preferred_match)
1710                         return preferred_match;
1711
1712                 if(style_match)
1713                         return style_match;
1714         }
1715
1716 // No exact match - assume normal width font
1717         style |= FL_WIDTH_NORMAL;
1718         mask |= FL_WIDTH_MASK;
1719         style_match = 0;
1720         preferred_match = 0;
1721
1722         for(int i = 0; i < fontlist->total; i++)
1723         {
1724                 entry = fontlist->values[i];
1725
1726                 if(STYLE_MATCH(entry->style, style, mask))
1727                 {
1728                         if(!style_match)
1729                                 style_match = entry;
1730
1731                         if(!preferred_match && (entry->style & preferred))
1732                                 preferred_match = entry;
1733
1734                         if(!strncasecmp(displayname, entry->family,
1735                                         strlen(entry->family)))
1736                         return entry;
1737                 }
1738         }
1739
1740         if(preferred_match)
1741                 return preferred_match;
1742
1743         return style_match;
1744 }
1745
1746
1747 class utf8conv {
1748         uint8_t *obfr, *out, *oend;
1749         uint8_t *ibfr, *inp, *iend;
1750 public:
1751         utf8conv(void *out, int olen, void *inp, int ilen) {
1752                 this->obfr = this->out = (uint8_t*)out;
1753                 this->oend = this->out + olen;
1754                 this->ibfr = this->inp = (uint8_t*)inp;
1755                 this->iend = this->inp + ilen;
1756         }
1757         int cur() { return inp>=iend ? -1 : *inp; }
1758         int next() { return inp>=iend ? -1 : *inp++; }
1759         int next(int ch) { return out>=oend ? -1 : *out++ = ch; }
1760         int ilen() { return inp-ibfr; }
1761         int olen() { return out-obfr; }
1762         int wnext();
1763         int wnext(unsigned int v);
1764 };
1765
1766 int utf8conv::
1767 wnext(unsigned int v)
1768 {
1769   if( v < 0x00000080 ) { next(v);  return 1; }
1770   int n = v < 0x00000800 ? 2 : v < 0x00010000 ? 3 :
1771           v < 0x00200000 ? 4 : v < 0x04000000 ? 5 : 6;
1772   int m = (0xff00 >> n), i = n-1;
1773   next((v>>(6*i)) | m);
1774   while( --i >= 0 ) next(((v>>(6*i)) & 0x3f) | 0x80);
1775   return n;
1776 }
1777
1778 int utf8conv::
1779 wnext()
1780 {
1781   int v = 0, n = 0, ch = next();
1782   if( ch >= 0x80 ) {
1783     static const unsigned char byts[] = {
1784       1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 4, 5,
1785     };
1786     int i = ch - 0xc0;
1787     n = i<0 ? 0 : byts[i/4];
1788     for( v=ch, i=n; --i>=0; v+=next() ) v <<= 6;
1789     static const unsigned int ofs[6] = {
1790       0x00000000U, 0x00003080U, 0x000E2080U,
1791       0x03C82080U, 0xFA082080U, 0x82082080U
1792     };
1793     v -= ofs[n];
1794   }
1795   else
1796     v = ch;
1797   return v;
1798 }
1799
1800
1801 size_t BC_Resources::encode(const char *from_enc, const char *to_enc,
1802         char *input, int input_length, char *output, int output_length)
1803 {
1804         if( !from_enc || *from_enc == 0 ||
1805             !strcmp(from_enc,"UTF8") || !strcmp(from_enc, "US-ASCII") )
1806                 from_enc = "UTF-8";
1807
1808         if( !to_enc || *to_enc == 0 ||
1809             !strcmp(to_enc,"UTF8") || !strcmp(to_enc, "US-ASCII") )
1810                 to_enc = "UTF-8";
1811
1812         iconv_t cd;
1813         char *outbase = output;
1814         size_t inbytes = input_length < 0 ? strlen(input) : input_length;
1815         size_t outbytes = output_length;
1816
1817         if( inbytes && outbytes ) {
1818                 if( !strcmp(from_enc, to_enc) ) {
1819                         if( inbytes > outbytes ) inbytes = outbytes;
1820                         memcpy(output,  input, inbytes);
1821                         output += inbytes;
1822                         outbytes -= inbytes;
1823                 }
1824                 else if( !strcmp(from_enc, "UTF-8") && !strcmp(to_enc,"UTF32LE") ) {
1825                         utf8conv uc(0,0, input,inbytes);
1826                         uint32_t *op = (uint32_t *)output;
1827                         uint32_t *ep = (uint32_t *)(output+output_length);
1828                         for( int wch; op<ep && (wch=uc.wnext())>=0; *op++=wch );
1829                         output = (char *)op;
1830                         outbytes = (char*)ep - output;
1831                 }
1832                 else if( !strcmp(from_enc, "UTF32LE") && !strcmp(to_enc,"UTF-8") ) {
1833                         utf8conv uc(output,output_length, 0,0);
1834                         uint32_t *ip = (uint32_t *)input;
1835                         uint32_t *ep = (uint32_t *)(input+inbytes);
1836                         for( ; ip<ep && uc.wnext(*ip)>=0; ++ip );
1837                         output += uc.olen();
1838                         outbytes -= uc.olen();
1839                 }
1840                 else if( (cd = iconv_open(to_enc, from_enc)) != (iconv_t)-1 ) {
1841                         iconv(cd, &input, &inbytes, &output, &outbytes);
1842                         iconv_close(cd);
1843                 }
1844                 else {
1845                         printf(_("Conversion from %s to %s is not available\n"),
1846                                 from_enc, to_enc);
1847                 }
1848         }
1849         if( outbytes > sizeof(uint32_t) )
1850                 outbytes = sizeof(uint32_t);
1851         for( uint32_t i = 0; i < outbytes; i++)
1852                 output[i] = 0;
1853         return output - outbase;
1854 }
1855
1856 void BC_Resources::encode_to_utf8(char *buffer, int buflen)
1857 {
1858         if(BC_Resources::locale_utf8) return;
1859         char lbuf[buflen];
1860         encode(encoding, 0, buffer, -1, lbuf, buflen);
1861         strcpy(buffer, lbuf);
1862 }
1863
1864 int BC_Resources::find_font_by_char(FT_ULong char_code, char *path_new, const FT_Face oldface)
1865 {
1866         FcPattern *font, *ofont;
1867         FcChar8 *file;
1868         int result = 0;
1869
1870         *path_new = 0;
1871
1872         // Do not search control codes
1873         if(char_code < ' ')
1874                 return 0;
1875
1876         if( (ofont = fcFreeTypeQueryFace(oldface, (const FcChar8*)"", 4097, 0)) != 0 ) {
1877                 if( (font = find_similar_font(char_code, ofont)) != 0 ) {
1878                         if(fcPatternGetString(font, FC_FILE, 0, &file) == FcResultMatch) {
1879                                 strcpy(path_new, (char*)file);
1880                                 result = 1;
1881                         }
1882                         fcPatternDestroy(font);
1883                 }
1884                 fcPatternDestroy(ofont);
1885         }
1886         return result;
1887 }
1888
1889 FcPattern* BC_Resources::find_similar_font(FT_ULong char_code, FcPattern *oldfont)
1890 {
1891         FcPattern *pat, *font;
1892         FcFontSet *fs;
1893         FcObjectSet *os;
1894         FcCharSet *fcs;
1895         int ival;
1896
1897         // Do not search control codes
1898         if(char_code < ' ')
1899                 return 0;
1900
1901         fontconfig_lock.lock("BC_Resources::find_similar_font");
1902         pat = fcPatternCreate();
1903         os = fcObjectSetBuild(FC_FILE, FC_CHARSET, FC_SCALABLE, FC_FAMILY,
1904                 FC_SLANT, FC_WEIGHT, FC_WIDTH, (char *)0);
1905
1906         fcPatternAddBool(pat, FC_SCALABLE, true);
1907         fcs = fcCharSetCreate();
1908         if(fcCharSetAddChar(fcs, char_code))
1909                 fcPatternAddCharSet(pat, FC_CHARSET, fcs);
1910         fcCharSetDestroy(fcs);
1911         for( int i=0; i<(int)LEN_FCPROP; ++i ) {
1912                 if(fcPatternGetInteger(oldfont, fc_properties[i], 0, &ival) == FcResultMatch)
1913                         fcPatternAddInteger(pat, fc_properties[i], ival);
1914         }
1915         fs = fcFontList(0, pat, os);
1916         for( int i=LEN_FCPROP; --i>=0 && !fs->nfont; ) {
1917                 fcFontSetDestroy(fs);
1918                 fcPatternDel(pat, fc_properties[i]);
1919                 fs = fcFontList(0, pat, os);
1920         }
1921         fcPatternDestroy(pat);
1922         fcObjectSetDestroy(os);
1923
1924         pat = 0;
1925
1926         for (int i = 0; i < fs->nfont; i++)
1927         {
1928                 font = fs->fonts[i];
1929                 if(fcPatternGetCharSet(font, FC_CHARSET, 0, &fcs) == FcResultMatch)
1930                 {
1931                         if(fcCharSetHasChar(fcs, char_code))
1932                         {
1933                                 pat =  fcPatternDuplicate(font);
1934                                 break;
1935                         }
1936                 }
1937         }
1938         fcFontSetDestroy(fs);
1939         fontconfig_lock.unlock();
1940
1941         return pat;
1942 }
1943
1944 void BC_Resources::dump_fonts(FILE *fp)
1945 {
1946         for( int i=0; i<fontlist->total; ++i )
1947                 dump_font_entry(fp, "", fontlist->values[i]);
1948 }
1949
1950 void BC_Resources::dump_font_entry(FILE *fp, const char *cp,  BC_FontEntry *ep)
1951 {
1952         fprintf(fp,"%s%s = %s\n",cp,ep->displayname,ep->path);
1953         fprintf(fp,"  %s:%s:%s:%s:%s:%s:%d:%d:%d:%d:%d:%s:%d:%s:%s:%d\n",
1954                 ep->foundry, ep->family, ep->weight, ep->slant, ep->swidth, ep->adstyle,
1955                 ep->pixelsize, ep->pointsize, ep->xres, ep->yres, ep->style, ep->spacing,
1956                 ep->avg_width, ep->registry, ep->encoding, ep->fixed_style);
1957 }
1958