Calculate and draw osd accurately.
[mplayer/greg.git] / libmenu / menu.c
blob358202b715e4a524a0b5724b80d107dec3ca3606
2 #include "config.h"
3 #include "mp_msg.h"
4 #include "help_mp.h"
6 #include <stdlib.h>
7 #include <stdio.h>
8 #include <string.h>
9 #include <fcntl.h>
10 #include <unistd.h>
12 #include "libvo/osd.h"
13 #include "libvo/font_load.h"
14 #include "libvo/sub.h"
15 #include "osdep/keycodes.h"
16 #include "asxparser.h"
17 #include "stream/stream.h"
18 #include "input/input.h"
20 #include "libmpcodecs/img_format.h"
21 #include "libmpcodecs/mp_image.h"
22 #include "m_option.h"
23 #include "m_struct.h"
24 #include "menu.h"
26 extern menu_info_t menu_info_cmdlist;
27 extern menu_info_t menu_info_chapsel;
28 extern menu_info_t menu_info_pt;
29 extern menu_info_t menu_info_filesel;
30 extern menu_info_t menu_info_txt;
31 extern menu_info_t menu_info_console;
32 extern menu_info_t menu_info_pref;
33 #ifdef HAS_DVBIN_SUPPORT
34 extern menu_info_t menu_info_dvbsel;
35 #endif
38 menu_info_t* menu_info_list[] = {
39 &menu_info_pt,
40 &menu_info_cmdlist,
41 &menu_info_chapsel,
42 &menu_info_filesel,
43 &menu_info_txt,
44 &menu_info_console,
45 #ifdef HAS_DVBIN_SUPPORT
46 &menu_info_dvbsel,
47 #endif
48 &menu_info_pref,
49 NULL
52 typedef struct key_cmd_s {
53 int key;
54 char *cmd;
55 } key_cmd_t;
57 typedef struct menu_cmd_bindings_s {
58 char *name;
59 key_cmd_t *bindings;
60 int binding_num;
61 struct menu_cmd_bindings_s *parent;
62 } menu_cmd_bindings_t;
64 struct menu_def_st {
65 char* name;
66 menu_info_t* type;
67 void* cfg;
68 char* args;
71 static struct MPContext *menu_ctx = NULL;
72 static menu_def_t* menu_list = NULL;
73 static int menu_count = 0;
74 static menu_cmd_bindings_t *cmd_bindings = NULL;
75 static int cmd_bindings_num = 0;
78 menu_cmd_bindings_t *get_cmd_bindings(const char *name) {
79 int i;
80 for (i = 0; i < cmd_bindings_num; ++i)
81 if (!strcasecmp(cmd_bindings[i].name, name))
82 return &cmd_bindings[i];
83 return NULL;
86 static int menu_parse_config(char* buffer) {
87 char *element,*body, **attribs, *name;
88 menu_info_t* minfo = NULL;
89 int r,i;
90 ASX_Parser_t* parser = asx_parser_new();
92 while(1) {
93 r = asx_get_element(parser,&buffer,&element,&body,&attribs);
94 if(r < 0) {
95 mp_msg(MSGT_GLOBAL,MSGL_WARN,MSGTR_LIBMENU_SyntaxErrorAtLine,parser->line);
96 asx_parser_free(parser);
97 return 0;
98 } else if(r == 0) {
99 asx_parser_free(parser);
100 return 1;
102 // Has it a name ?
103 name = asx_get_attrib("name",attribs);
104 if(!name) {
105 mp_msg(MSGT_GLOBAL,MSGL_WARN,MSGTR_LIBMENU_MenuDefinitionsNeedANameAttrib,parser->line);
106 free(element);
107 if(body) free(body);
108 asx_free_attribs(attribs);
109 continue;
112 if (!strcasecmp(element, "keybindings")) {
113 menu_cmd_bindings_t *bindings = cmd_bindings;
114 char *parent_bindings;
115 cmd_bindings = realloc(cmd_bindings,
116 (cmd_bindings_num+1)*sizeof(menu_cmd_bindings_t));
117 for (i = 0; i < cmd_bindings_num; ++i)
118 if (cmd_bindings[i].parent)
119 cmd_bindings[i].parent = cmd_bindings[i].parent-bindings+cmd_bindings;
120 bindings = &cmd_bindings[cmd_bindings_num];
121 memset(bindings, 0, sizeof(menu_cmd_bindings_t));
122 bindings->name = name;
123 parent_bindings = asx_get_attrib("parent",attribs);
124 if (parent_bindings) {
125 bindings->parent = get_cmd_bindings(parent_bindings);
126 free(parent_bindings);
128 free(element);
129 asx_free_attribs(attribs);
130 if (body) {
131 char *bd = body;
132 char *b, *key, *cmd;
133 int keycode;
134 for(;;) {
135 r = asx_get_element(parser,&bd,&element,&b,&attribs);
136 if(r < 0) {
137 mp_msg(MSGT_GLOBAL,MSGL_WARN,MSGTR_LIBMENU_SyntaxErrorAtLine,
138 parser->line);
139 free(body);
140 asx_parser_free(parser);
141 return 0;
143 if(r == 0)
144 break;
145 if (!strcasecmp(element, "binding")) {
146 key = asx_get_attrib("key",attribs);
147 cmd = asx_get_attrib("cmd",attribs);
148 if (key && (keycode = mp_input_get_key_from_name(key)) >= 0) {
149 keycode &= ~MP_NO_REPEAT_KEY;
150 mp_msg(MSGT_GLOBAL,MSGL_V,
151 "[libmenu] got keybinding element %d %s=>[%s].\n",
152 keycode, key, cmd ? cmd : "");
153 bindings->bindings = realloc(bindings->bindings,
154 (bindings->binding_num+1)*sizeof(key_cmd_t));
155 bindings->bindings[bindings->binding_num].key = keycode;
156 bindings->bindings[bindings->binding_num].cmd = cmd;
157 ++bindings->binding_num;
159 else
160 free(cmd);
161 free(key);
163 free(element);
164 asx_free_attribs(attribs);
165 free(b);
167 free(body);
169 ++cmd_bindings_num;
170 continue;
172 // Try to find this menu type in our list
173 for(i = 0, minfo = NULL ; menu_info_list[i] ; i++) {
174 if(strcasecmp(element,menu_info_list[i]->name) == 0) {
175 minfo = menu_info_list[i];
176 break;
179 // Got it : add this to our list
180 if(minfo) {
181 menu_list = realloc(menu_list,(menu_count+2)*sizeof(menu_def_t));
182 menu_list[menu_count].name = name;
183 menu_list[menu_count].type = minfo;
184 menu_list[menu_count].cfg = m_struct_alloc(&minfo->priv_st);
185 menu_list[menu_count].args = body;
186 // Setup the attribs
187 for(i = 0 ; attribs[2*i] ; i++) {
188 if(strcasecmp(attribs[2*i],"name") == 0) continue;
189 if(!m_struct_set(&minfo->priv_st,menu_list[menu_count].cfg,attribs[2*i], attribs[2*i+1]))
190 mp_msg(MSGT_GLOBAL,MSGL_WARN,MSGTR_LIBMENU_BadAttrib,attribs[2*i],attribs[2*i+1],
191 name,parser->line);
193 menu_count++;
194 memset(&menu_list[menu_count],0,sizeof(menu_def_t));
195 } else {
196 mp_msg(MSGT_GLOBAL,MSGL_WARN,MSGTR_LIBMENU_UnknownMenuType,element,parser->line);
197 free(name);
198 if(body) free(body);
201 free(element);
202 asx_free_attribs(attribs);
208 /// This will build the menu_defs list from the cfg file
209 #define BUF_STEP 1024
210 #define BUF_MIN 128
211 #define BUF_MAX BUF_STEP*1024
212 int menu_init(struct MPContext *mpctx, char* cfg_file) {
213 char* buffer = NULL;
214 int bl = BUF_STEP, br = 0;
215 int f, fd;
216 #ifndef HAVE_FREETYPE
217 if(vo_font == NULL)
218 return 0;
219 #endif
220 fd = open(cfg_file, O_RDONLY);
221 if(fd < 0) {
222 mp_msg(MSGT_GLOBAL,MSGL_WARN,MSGTR_LIBMENU_CantOpenConfigFile,cfg_file);
223 return 0;
225 buffer = malloc(bl);
226 while(1) {
227 int r;
228 if(bl - br < BUF_MIN) {
229 if(bl >= BUF_MAX) {
230 mp_msg(MSGT_GLOBAL,MSGL_WARN,MSGTR_LIBMENU_ConfigFileIsTooBig,BUF_MAX/1024);
231 close(fd);
232 free(buffer);
233 return 0;
235 bl += BUF_STEP;
236 buffer = realloc(buffer,bl);
238 r = read(fd,buffer+br,bl-br);
239 if(r == 0) break;
240 br += r;
242 if(!br) {
243 mp_msg(MSGT_GLOBAL,MSGL_WARN,MSGTR_LIBMENU_ConfigFileIsEmpty);
244 return 0;
246 buffer[br-1] = '\0';
248 close(fd);
250 menu_ctx = mpctx;
251 f = menu_parse_config(buffer);
252 free(buffer);
253 return f;
256 // Destroy all this stuff
257 void menu_uninit(void) {
258 int i;
259 for(i = 0 ; menu_list && menu_list[i].name ; i++) {
260 free(menu_list[i].name);
261 m_struct_free(&menu_list[i].type->priv_st,menu_list[i].cfg);
262 if(menu_list[i].args) free(menu_list[i].args);
264 free(menu_list);
265 menu_count = 0;
266 for (i = 0; i < cmd_bindings_num; ++i) {
267 free(cmd_bindings[i].name);
268 while(cmd_bindings[i].binding_num > 0)
269 free(cmd_bindings[i].bindings[--cmd_bindings[i].binding_num].cmd);
270 free(cmd_bindings[i].bindings);
272 free(cmd_bindings);
275 /// Default read_key function
276 int menu_dflt_read_key(menu_t* menu,int cmd) {
277 int i;
278 menu_cmd_bindings_t *bindings = get_cmd_bindings(menu->type->name);
279 if (!bindings)
280 bindings = get_cmd_bindings(menu->type->type->name);
281 if (!bindings)
282 bindings = get_cmd_bindings("default");
283 while (bindings) {
284 for (i = 0; i < bindings->binding_num; ++i) {
285 if (bindings->bindings[i].key == cmd) {
286 if (bindings->bindings[i].cmd)
287 mp_input_parse_and_queue_cmds(bindings->bindings[i].cmd);
288 return 1;
291 bindings = bindings->parent;
293 return 0;
296 menu_t* menu_open(char *name) {
297 menu_t* m;
298 int i;
300 for(i = 0 ; menu_list[i].name != NULL ; i++) {
301 if(strcmp(name,menu_list[i].name) == 0)
302 break;
304 if(menu_list[i].name == NULL) {
305 mp_msg(MSGT_GLOBAL,MSGL_WARN,MSGTR_LIBMENU_MenuNotFound,name);
306 return NULL;
308 m = calloc(1,sizeof(menu_t));
309 m->priv_st = &(menu_list[i].type->priv_st);
310 m->priv = m_struct_copy(m->priv_st,menu_list[i].cfg);
311 m->ctx = menu_ctx;
312 m->type = &menu_list[i];
313 if(menu_list[i].type->open(m,menu_list[i].args))
314 return m;
315 if(m->priv)
316 m_struct_free(m->priv_st,m->priv);
317 free(m);
318 mp_msg(MSGT_GLOBAL,MSGL_WARN,MSGTR_LIBMENU_MenuInitFailed,name);
319 return NULL;
322 void menu_draw(menu_t* menu,mp_image_t* mpi) {
323 if(menu->show && menu->draw)
324 menu->draw(menu,mpi);
327 void menu_read_cmd(menu_t* menu,int cmd) {
328 if(menu->read_cmd)
329 menu->read_cmd(menu,cmd);
332 void menu_close(menu_t* menu) {
333 if(menu->close)
334 menu->close(menu);
335 if(menu->priv)
336 m_struct_free(menu->priv_st,menu->priv);
337 free(menu);
340 void menu_read_key(menu_t* menu,int cmd) {
341 if(menu->read_key)
342 menu->read_key(menu,cmd);
343 else
344 menu_dflt_read_key(menu,cmd);
347 ///////////////////////////// Helpers ////////////////////////////////////
349 typedef void (*draw_alpha_f)(int w,int h, unsigned char* src, unsigned char *srca, int srcstride, unsigned char* dstbase,int dststride);
351 inline static draw_alpha_f get_draw_alpha(uint32_t fmt) {
352 switch(fmt) {
353 case IMGFMT_BGR15:
354 case IMGFMT_RGB15:
355 return vo_draw_alpha_rgb15;
356 case IMGFMT_BGR16:
357 case IMGFMT_RGB16:
358 return vo_draw_alpha_rgb16;
359 case IMGFMT_BGR24:
360 case IMGFMT_RGB24:
361 return vo_draw_alpha_rgb24;
362 case IMGFMT_BGR32:
363 case IMGFMT_RGB32:
364 return vo_draw_alpha_rgb32;
365 case IMGFMT_YV12:
366 case IMGFMT_I420:
367 case IMGFMT_IYUV:
368 case IMGFMT_YVU9:
369 case IMGFMT_IF09:
370 case IMGFMT_Y800:
371 case IMGFMT_Y8:
372 return vo_draw_alpha_yv12;
373 case IMGFMT_YUY2:
374 return vo_draw_alpha_yuy2;
375 case IMGFMT_UYVY:
376 return vo_draw_alpha_uyvy;
379 return NULL;
382 // return the real height of a char:
383 static inline int get_height(int c,int h){
384 int font;
385 if ((font=vo_font->font[c])>=0)
386 if(h<vo_font->pic_a[font]->h) h=vo_font->pic_a[font]->h;
387 return h;
390 static void render_txt(char *txt)
392 while (*txt) {
393 int c = utf8_get_char((const char**)&txt);
394 render_one_glyph(vo_font, c);
398 #ifdef USE_FRIBIDI
399 #include <fribidi/fribidi.h>
400 #include "libavutil/common.h"
401 char *menu_fribidi_charset = NULL;
402 int menu_flip_hebrew = 0;
403 int menu_fribidi_flip_commas = 0;
405 static char *menu_fribidi(char *txt)
407 static int char_set_num = -1;
408 static FriBidiChar *logical, *visual;
409 static size_t buffer_size = 1024;
410 static char *outputstr;
412 FriBidiCharType base;
413 fribidi_boolean log2vis;
414 size_t len;
416 if (menu_flip_hebrew) {
417 len = strlen(txt);
418 if (char_set_num == -1) {
419 fribidi_set_mirroring (1);
420 fribidi_set_reorder_nsm (0);
421 char_set_num = fribidi_parse_charset("UTF-8");
422 buffer_size = FFMAX(1024,len+1);
423 logical = malloc(buffer_size);
424 visual = malloc(buffer_size);
425 outputstr = malloc(buffer_size);
426 } else if (len+1 > buffer_size) {
427 buffer_size = len+1;
428 logical = realloc(logical, buffer_size);
429 visual = realloc(visual, buffer_size);
430 outputstr = realloc(outputstr, buffer_size);
432 len = fribidi_charset_to_unicode (char_set_num, txt, len, logical);
433 base = menu_fribidi_flip_commas?FRIBIDI_TYPE_ON:FRIBIDI_TYPE_L;
434 log2vis = fribidi_log2vis (logical, len, &base, visual, NULL, NULL, NULL);
435 if (log2vis) {
436 len = fribidi_remove_bidi_marks (visual, len, NULL, NULL, NULL);
437 fribidi_unicode_to_charset (char_set_num, visual, len, outputstr);
438 return outputstr;
441 return txt;
443 #endif
445 void menu_draw_text(mp_image_t* mpi,char* txt, int x, int y) {
446 draw_alpha_f draw_alpha = get_draw_alpha(mpi->imgfmt);
447 int font;
449 if(!draw_alpha) {
450 mp_msg(MSGT_GLOBAL,MSGL_WARN,MSGTR_LIBMENU_UnsupportedOutformat);
451 return;
454 #ifdef USE_FRIBIDI
455 txt = menu_fribidi(txt);
456 #endif
457 render_txt(txt);
459 while (*txt) {
460 int c=utf8_get_char((const char**)&txt);
461 if ((font=vo_font->font[c])>=0 && (x + vo_font->width[c] <= mpi->w) && (y + vo_font->pic_a[font]->h <= mpi->h))
462 draw_alpha(vo_font->width[c], vo_font->pic_a[font]->h,
463 vo_font->pic_b[font]->bmp+vo_font->start[c],
464 vo_font->pic_a[font]->bmp+vo_font->start[c],
465 vo_font->pic_a[font]->w,
466 mpi->planes[0] + y * mpi->stride[0] + x * (mpi->bpp>>3),
467 mpi->stride[0]);
468 x+=vo_font->width[c]+vo_font->charspace;
473 void menu_draw_text_full(mp_image_t* mpi,char* txt,
474 int x, int y,int w, int h,
475 int vspace, int warp, int align, int anchor) {
476 int need_w,need_h;
477 int sy, ymin, ymax;
478 int sx, xmin, xmax, xmid, xrmin;
479 int ll = 0;
480 int font;
481 draw_alpha_f draw_alpha = get_draw_alpha(mpi->imgfmt);
483 if(!draw_alpha) {
484 mp_msg(MSGT_GLOBAL,MSGL_WARN,MSGTR_LIBMENU_UnsupportedOutformat);
485 return;
488 #ifdef USE_FRIBIDI
489 txt = menu_fribidi(txt);
490 #endif
491 render_txt(txt);
493 if(x > mpi->w || y > mpi->h)
494 return;
496 if(anchor & MENU_TEXT_VCENTER) {
497 if(h <= 0) h = mpi->h;
498 ymin = y - h/2;
499 ymax = y + h/2;
500 } else if(anchor & MENU_TEXT_BOT) {
501 if(h <= 0) h = mpi->h - y;
502 ymin = y - h;
503 ymax = y;
504 } else {
505 if(h <= 0) h = mpi->h - y;
506 ymin = y;
507 ymax = y + h;
510 if(anchor & MENU_TEXT_HCENTER) {
511 if(w <= 0) w = mpi->w;
512 xmin = x - w/2;
513 xmax = x + w/2;
514 } else if(anchor & MENU_TEXT_RIGHT) {
515 if(w <= 0) w = mpi->w -x;
516 xmin = x - w;
517 xmax = x;
518 } else {
519 if(w <= 0) w = mpi->w -x;
520 xmin = x;
521 xmax = x + w;
524 // How many space do we need to draw this ?
525 menu_text_size(txt,w,vspace,warp,&need_w,&need_h);
527 // Find the first line
528 if(align & MENU_TEXT_VCENTER)
529 sy = ymin + ((h - need_h)/2);
530 else if(align & MENU_TEXT_BOT)
531 sy = ymax - need_h - 1;
532 else
533 sy = y;
535 #if 0
536 // Find the first col
537 if(align & MENU_TEXT_HCENTER)
538 sx = xmin + ((w - need_w)/2);
539 else if(align & MENU_TEXT_RIGHT)
540 sx = xmax - need_w;
541 #endif
543 xmid = xmin + (xmax - xmin) / 2;
544 xrmin = xmin;
545 // Clamp the bb to the mpi size
546 if(ymin < 0) ymin = 0;
547 if(xmin < 0) xmin = 0;
548 if(ymax > mpi->h) ymax = mpi->h;
549 if(xmax > mpi->w) xmax = mpi->w;
551 // Jump some the beginnig text if needed
552 while(sy < ymin && *txt) {
553 int c=utf8_get_char((const char**)&txt);
554 if(c == '\n' || (warp && ll + vo_font->width[c] > w)) {
555 ll = 0;
556 sy += vo_font->height + vspace;
557 if(c == '\n') continue;
559 ll += vo_font->width[c]+vo_font->charspace;
561 if(*txt == '\0') // Nothing left to draw
562 return;
564 while(sy < ymax && *txt) {
565 char* line_end = NULL;
566 int n;
568 if(txt[0] == '\n') { // New line
569 sy += vo_font->height + vspace;
570 txt++;
571 continue;
574 // Get the length and end of this line
575 for(n = 0, ll = 0 ; txt[n] != '\0' && txt[n] != '\n' ; n++) {
576 unsigned char c = txt[n];
577 if(warp && ll + vo_font->width[c] > w) break;
578 ll += vo_font->width[c]+vo_font->charspace;
580 line_end = &txt[n];
581 ll -= vo_font->charspace;
584 if(align & (MENU_TEXT_HCENTER|MENU_TEXT_RIGHT)) {
585 // Too long line
586 if(ll > xmax-xmin) {
587 if(align & MENU_TEXT_HCENTER) {
588 int mid = ll/2;
589 // Find the middle point
590 for(n--, ll = 0 ; n <= 0 ; n--) {
591 ll += vo_font->width[(int)txt[n]]+vo_font->charspace;
592 if(ll - vo_font->charspace > mid) break;
594 ll -= vo_font->charspace;
595 sx = xmid + mid - ll;
596 } else// MENU_TEXT_RIGHT)
597 sx = xmax + vo_font->charspace;
599 // We are after the start point -> go back
600 if(sx > xmin) {
601 for(n-- ; n <= 0 ; n--) {
602 unsigned char c = txt[n];
603 if(sx - vo_font->width[c] - vo_font->charspace < xmin) break;
604 sx -= vo_font->width[c]+vo_font->charspace;
606 } else { // We are before the start point -> go forward
607 for( ; sx < xmin && (&txt[n]) != line_end ; n++) {
608 unsigned char c = txt[n];
609 sx += vo_font->width[c]+vo_font->charspace;
612 txt = &txt[n]; // Jump to the new start char
613 } else {
614 if(align & MENU_TEXT_HCENTER)
615 sx = xmid - ll/2;
616 else
617 sx = xmax - 1 - ll;
619 } else {
620 for(sx = xrmin ; sx < xmin && txt != line_end ; txt++) {
621 unsigned char c = txt[n];
622 sx += vo_font->width[c]+vo_font->charspace;
626 while(sx < xmax && txt != line_end) {
627 int c=utf8_get_char((const char**)&txt);
628 font = vo_font->font[c];
629 if(font >= 0) {
630 int cs = (vo_font->pic_a[font]->h - vo_font->height) / 2;
631 if ((sx + vo_font->width[c] <= xmax) && (sy + vo_font->height <= ymax) )
632 draw_alpha(vo_font->width[c], vo_font->height,
633 vo_font->pic_b[font]->bmp+vo_font->start[c] +
634 cs * vo_font->pic_a[font]->w,
635 vo_font->pic_a[font]->bmp+vo_font->start[c] +
636 cs * vo_font->pic_a[font]->w,
637 vo_font->pic_a[font]->w,
638 mpi->planes[0] + sy * mpi->stride[0] + sx * (mpi->bpp>>3),
639 mpi->stride[0]);
640 // else
641 //printf("Can't draw '%c'\n",c);
643 sx+=vo_font->width[c]+vo_font->charspace;
645 txt = line_end;
646 if(txt[0] == '\0') break;
647 sy += vo_font->height + vspace;
651 int menu_text_length(char* txt) {
652 int l = 0;
653 render_txt(txt);
654 while (*txt) {
655 int c=utf8_get_char((const char**)&txt);
656 l += vo_font->width[c]+vo_font->charspace;
658 return l - vo_font->charspace;
661 void menu_text_size(char* txt,int max_width, int vspace, int warp, int* _w, int* _h) {
662 int l = 1, i = 0;
663 int w = 0;
665 render_txt(txt);
666 while (*txt) {
667 int c=utf8_get_char((const char**)&txt);
668 if(c == '\n' || (warp && i + vo_font->width[c] >= max_width)) {
669 i -= vo_font->charspace;
670 if (i > w) w = i;
671 if(*txt)
672 l++;
673 i = 0;
674 if(c == '\n') continue;
676 i += vo_font->width[c]+vo_font->charspace;
678 if (i > 0) {
679 i -= vo_font->charspace;
680 if (i > w) w = i;
683 *_w = w;
684 *_h = (l-1) * (vo_font->height + vspace) + vo_font->height;
688 int menu_text_num_lines(char* txt, int max_width) {
689 int l = 1, i = 0;
690 render_txt(txt);
691 while (*txt) {
692 int c=utf8_get_char((const char**)&txt);
693 if(c == '\n' || i + vo_font->width[c] > max_width) {
694 l++;
695 i = 0;
696 if(c == '\n') continue;
698 i += vo_font->width[c]+vo_font->charspace;
700 return l;
703 char* menu_text_get_next_line(char* txt, int max_width) {
704 int i = 0;
705 render_txt(txt);
706 while (*txt) {
707 int c=utf8_get_char((const char**)&txt);
708 if(c == '\n') {
709 txt++;
710 break;
712 i += vo_font->width[c];
713 if(i >= max_width)
714 break;
715 i += vo_font->charspace;
717 return txt;
721 void menu_draw_box(mp_image_t* mpi,unsigned char grey,unsigned char alpha, int x, int y, int w, int h) {
722 draw_alpha_f draw_alpha = get_draw_alpha(mpi->imgfmt);
723 int g;
725 if(!draw_alpha) {
726 mp_msg(MSGT_GLOBAL,MSGL_WARN,MSGTR_LIBMENU_UnsupportedOutformat);
727 return;
730 if(x > mpi->w || y > mpi->h) return;
732 if(x < 0) w += x, x = 0;
733 if(x+w > mpi->w) w = mpi->w-x;
734 if(y < 0) h += y, y = 0;
735 if(y+h > mpi->h) h = mpi->h-y;
737 g = ((256-alpha)*grey)>>8;
738 if(g < 1) g = 1;
741 int stride = (w+7)&(~7); // round to 8
742 char pic[stride*h],pic_alpha[stride*h];
743 memset(pic,g,stride*h);
744 memset(pic_alpha,alpha,stride*h);
745 draw_alpha(w,h,pic,pic_alpha,stride,
746 mpi->planes[0] + y * mpi->stride[0] + x * (mpi->bpp>>3),
747 mpi->stride[0]);