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