1 /*****************************************************************************
2 * video_text.c : text manipulation functions
3 *****************************************************************************
4 * Copyright (C) 1999, 2000 VideoLAN
5 * $Id: video_text.c,v 1.21 2001/04/06 18:18:10 massiot Exp $
7 * Authors: Vincent Seguin <seguin@via.ecp.fr>
8 * Samuel Hocevar <sam@zoy.org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
30 #include <errno.h> /* errno */
31 #include <stdlib.h> /* free() */
32 #include <stdio.h> /* sprintf() */
33 #include <string.h> /* strerror() */
34 #include <fcntl.h> /* open() */
35 #include <unistd.h> /* read(), close() */
38 #include "beos_specific.h"
43 #include "video_text.h"
46 // #include <CFBundle.h>
47 #include <sys/param.h> /* for MAXPATHLEN */
49 extern main_t *p_main;
54 /*****************************************************************************
55 * vout_font_t: bitmap font
56 *****************************************************************************
57 * This structure is used when the system doesn't provide a convenient function
58 * to print simple characters in a buffer.
59 * VOUT_FIXED_FONTs are stored in raw mode, character after character, with a
60 * first array of characters followed by a second array of borders masks.
61 * Therefore the border masks can't be complete if the font has pixels on the
63 *****************************************************************************/
64 typedef struct vout_font_s
66 int i_type; /* font type */
67 int i_width; /* character width in pixels */
68 int i_height; /* character height in pixels */
69 int i_interspacing; /* characters interspacing in pixels */
70 int i_bytes_per_line; /* bytes per character line */
71 int i_bytes_per_char; /* bytes per character */
72 u16 i_first; /* first character */
73 u16 i_last; /* last character */
74 byte_t * p_data; /* font character data */
78 #define VOUT_FIXED_FONT 0 /* simple fixed font */
80 /*****************************************************************************
81 * vout_put_byte_t: PutByte function
82 *****************************************************************************
83 * These functions will transform masks in a set of pixels. For each pixel,
84 * character, then border and background masks are tested, and the first
85 * encountered color is set.
86 *****************************************************************************/
87 typedef void (vout_put_byte_t)( void *p_pic, int i_byte, int i_char, int i_border,
88 int i_bg, u32 i_char_color, u32 i_border_color, u32 i_bg_color );
91 /*****************************************************************************
93 *****************************************************************************/
95 /* PUT_BYTE_MASK: put pixels from a byte-wide mask. It uses a branching tree
96 * to optimize the number of tests. It is used in the PutByte functions.
97 * This macro works for 1, 2 and 4 Bpp. */
98 #define PUT_BYTE_MASK( i_mask, i_mask_color ) \
99 if( i_mask & 0xf0 ) /* one from 1111 */ \
101 if( i_mask & 0xc0 ) /* one from 1100 */ \
103 if( i_mask & 0x80 ) /* 1000 */ \
105 p_pic[0] = i_mask_color; \
106 if( i_mask & 0x40 ) /* 0100 */ \
108 p_pic[1] = i_mask_color; \
111 else /* not 1000 means 0100 */ \
113 p_pic[1] = i_mask_color; \
115 if( i_mask & 0x30 ) /* one from 0011 */ \
117 if( i_mask & 0x20 ) /* 0010 */ \
119 p_pic[2] = i_mask_color; \
120 if( i_mask & 0x10 ) /* 0001 */ \
122 p_pic[3] = i_mask_color; \
125 else /* not 0010 means 0001 */ \
127 p_pic[3] = i_mask_color; \
131 else /* not 1100 means 0011 */ \
133 if( i_mask & 0x20 ) /* 0010 */ \
135 p_pic[2] = i_mask_color; \
136 if( i_mask & 0x10 ) /* 0001 */ \
138 p_pic[3] = i_mask_color; \
141 else /* not 0010 means 0001 */ \
143 p_pic[3] = i_mask_color; \
147 if( i_mask & 0x0f ) \
149 if( i_mask & 0x0c ) /* one from 1100 */ \
151 if( i_mask & 0x08 ) /* 1000 */ \
153 p_pic[4] = i_mask_color; \
154 if( i_mask & 0x04 ) /* 0100 */ \
156 p_pic[5] = i_mask_color; \
159 else /* not 1000 means 0100 */ \
161 p_pic[5] = i_mask_color; \
163 if( i_mask & 0x03 ) /* one from 0011 */ \
165 if( i_mask & 0x02 ) /* 0010 */ \
167 p_pic[6] = i_mask_color; \
168 if( i_mask & 0x01 ) /* 0001 */ \
170 p_pic[7] = i_mask_color; \
173 else /* not 0010 means 0001 */ \
175 p_pic[7] = i_mask_color; \
179 else /* not 1100 means 0011 */ \
181 if( i_mask & 0x02 ) /* 0010 */ \
183 p_pic[6] = i_mask_color; \
184 if( i_mask & 0x01 ) /* 0001 */ \
186 p_pic[7] = i_mask_color; \
189 else /* not 0010 means 0001 */ \
191 p_pic[7] = i_mask_color; \
196 /*****************************************************************************
198 *****************************************************************************/
199 static void PutByte8 ( u8 *p_pic, int i_byte, int i_char, int i_border,
200 int i_bg, u32 i_char_color, u32 i_border_color,
202 static void PutByte16( u16 *p_pic, int i_byte, int i_char, int i_border,
203 int i_bg, u32 i_char_color, u32 i_border_color,
205 static void PutByte24( void *p_pic, int i_byte, byte_t i_char, byte_t i_border,
206 byte_t i_bg, u32 i_char_color, u32 i_border_color,
208 static void PutByte32( u32 *p_pic, int i_byte, byte_t i_char, byte_t i_border,
209 byte_t i_bg, u32 i_char_color, u32 i_border_color,
212 /*****************************************************************************
213 * vout_LoadFont: load a bitmap font from a file
214 *****************************************************************************
215 * This function will try to open a .psf font and load it. It will return
217 *****************************************************************************/
218 vout_font_t *vout_LoadFont( const char *psz_name )
220 static char * path[] = { "share", DATA_PATH, NULL, NULL };
222 char ** ppsz_path = path;
225 char * psz_vlcpath = beos_GetProgramPath();
226 int i_vlclen = strlen( psz_vlcpath );
227 #elif defined SYS_DARWIN1_3
228 static char once = 0;
229 static char app_path[ MAXPATHLEN ];
230 /* HACK TO CUT OUT trailing 'vlc' */
231 int i_pathlen = strlen( p_main->ppsz_argv[ 0 ] ) - 3;
232 // CFBundleRef mainBundle;
234 int i_char, i_line; /* character and line indexes */
235 int i_file = -1; /* source file */
236 byte_t pi_buffer[2]; /* file buffer */
237 vout_font_t * p_font; /* the font itself */
240 // Get the main bundle for the app
241 // mainBundle = CFBundleGetMainBundle();
246 strncpy( app_path, p_main->ppsz_argv[ 0 ], i_pathlen );
247 intf_ErrMsg( "%s", p_main->ppsz_argv[ 0 ] );
248 strcat( app_path, "share" );
249 path[ 2 ] = app_path ;
250 intf_ErrMsg( "%s", path[ 2 ] );
254 for( ; *ppsz_path != NULL ; ppsz_path++ )
257 /* Under BeOS, we need to add beos_GetProgramPath() to access
258 * files under the current directory */
259 if( strncmp( *ppsz_path, "/", 1 ) )
261 psz_file = malloc( strlen( psz_name ) + strlen( *ppsz_path )
263 if( psz_file == NULL )
267 sprintf( psz_file, "%s/%s/%s", psz_vlcpath, *ppsz_path, psz_name );
272 psz_file = malloc( strlen( psz_name ) + strlen( *ppsz_path ) + 2 );
273 if( psz_file == NULL )
277 sprintf( psz_file, "%s/%s", *ppsz_path, psz_name );
281 i_file = open( psz_file, O_RDONLY );
292 intf_DbgMsg( "vout error: can't open file '%s' (%s)",
293 psz_name, strerror(errno) );
297 /* Read magick number */
298 if( read( i_file, pi_buffer, 2 ) != 2 )
300 intf_ErrMsg("vout error: unexpected end of file '%s'", psz_name );
305 /* Allocate font descriptor */
306 p_font = malloc( sizeof( vout_font_t ) );
309 intf_ErrMsg( "vout error: cannot allocate vout_font_t (%s)",
316 switch( ((u16)pi_buffer[0] << 8) | pi_buffer[1] )
318 case 0x3604: /* .psf file */
320 * PSF font: simple fixed font. Only the first 256 characters are read.
321 * Those fonts are always 1 byte wide, and 256 or 512 characters long.
324 /* Read font header - two bytes indicate the font properties */
325 if( read( i_file, pi_buffer, 2 ) != 2)
327 intf_ErrMsg( "vout error: unexpected end of file '%s'", psz_name );
333 /* Copy font properties */
334 p_font->i_type = VOUT_FIXED_FONT;
336 p_font->i_height = pi_buffer[1];
337 p_font->i_interspacing = 8;
338 p_font->i_bytes_per_line = 1;
339 p_font->i_bytes_per_char = pi_buffer[1];
341 p_font->i_last = 255;
343 /* Allocate font space */
344 p_font->p_data = malloc( 2 * 256 * pi_buffer[1] );
345 if( p_font->p_data == NULL )
347 intf_ErrMsg( "vout error: cannot allocate font space (%s)",
355 if( read( i_file, p_font->p_data, 256 * pi_buffer[1] ) != 256 * pi_buffer[1] )
357 intf_ErrMsg("vout error: unexpected end of file '%s'", psz_name );
358 free( p_font->p_data );
364 /* Computes border masks - remember that masks have the same matrix as
365 * characters, so an empty character border is required to have a complete
367 for( i_char = 0; i_char <= 255; i_char++ )
369 for( i_line = 0; i_line < pi_buffer[1]; i_line++ )
372 p_font->p_data[ (i_char + 256) * pi_buffer[1] + i_line ] =
373 ((p_font->p_data[ i_char * pi_buffer[1] + i_line ] << 1) |
374 (p_font->p_data[ i_char * pi_buffer[1] + i_line ] >> 1) |
375 (i_line > 0 ? p_font->p_data[ i_char * pi_buffer[1] + i_line - 1]: 0) |
376 (i_line < pi_buffer[1] - 1 ? p_font->p_data[ i_char * pi_buffer[1] + i_line + 1]: 0))
377 & ~p_font->p_data[ i_char * pi_buffer[1] + i_line ];
383 intf_ErrMsg("vout error: file '%s' has an unknown format", psz_name );
391 intf_DbgMsg( "loaded %s: type %d, %d-%dx%d", psz_name, p_font->i_type,
392 p_font->i_width, p_font->i_interspacing, p_font->i_height );
396 /*****************************************************************************
397 * vout_UnloadFont: unload a font
398 *****************************************************************************
399 * This function free the resources allocated by vout_LoadFont
400 *****************************************************************************/
401 void vout_UnloadFont( vout_font_t *p_font )
403 intf_DbgMsg( "vout: unloading font %p", p_font );
404 free( p_font->p_data );
408 /*****************************************************************************
409 * vout_TextSize: return the dimensions of a text
410 *****************************************************************************
411 * This function is used to align text. It returns the width and height of a
413 *****************************************************************************/
414 void vout_TextSize( vout_font_t *p_font, int i_style, const char *psz_text, int *pi_width, int *pi_height )
416 switch( p_font->i_type )
418 case VOUT_FIXED_FONT:
419 *pi_width = ((i_style & WIDE_TEXT) ? p_font->i_interspacing * 2 : p_font->i_interspacing) *
420 (strlen( psz_text ) - 1) + p_font->i_width;
421 *pi_height = p_font->i_height;
422 if( i_style & ITALIC_TEXT )
424 *pi_width = *pi_height / 3;
429 intf_DbgMsg("error: unknown font type %d", p_font->i_type );
435 /*****************************************************************************
436 * vout_Print: low level printing function
437 *****************************************************************************
438 * This function prints a text, without clipping, in a buffer using a
439 * previously loaded bitmap font.
440 *****************************************************************************/
441 void vout_Print( vout_font_t *p_font, byte_t *p_pic, int i_bytes_per_pixel, int i_bytes_per_line,
442 u32 i_char_color, u32 i_border_color, u32 i_bg_color, int i_style, const char *psz_text, int i_percent)
444 byte_t *p_char, *p_border; /* character and border mask data */
445 int i_char_mask, i_border_mask, i_bg_mask; /* masks */
446 int i_line; /* current line in character */
447 int i_byte; /* current byte in character */
448 int i_interspacing; /* offset between two chars */
449 int i_font_bytes_per_line, i_font_height; /* font properties */
450 int i_position, i_end; /* current position */
451 vout_put_byte_t *p_PutByte; /* PutByte function */
453 /* FIXME: background: can be something else that whole byte ?? */
455 /* Select output function */
456 switch( i_bytes_per_pixel )
459 p_PutByte = (vout_put_byte_t *) PutByte8;
462 p_PutByte = (vout_put_byte_t *) PutByte16;
465 p_PutByte = (vout_put_byte_t *) PutByte24;
469 p_PutByte = (vout_put_byte_t *) PutByte32;
473 /* Choose masks and copy font data to local variables */
474 i_char_mask = (i_style & VOID_TEXT) ? 0 : 0xff;
475 i_border_mask = (i_style & OUTLINED_TEXT) ? 0xff : 0;
476 i_bg_mask = (i_style & OPAQUE_TEXT) ? 0xff : 0;
478 i_font_bytes_per_line = p_font->i_bytes_per_line;
479 i_font_height = p_font->i_height;
480 i_interspacing = i_bytes_per_pixel * ((i_style & WIDE_TEXT) ?
481 p_font->i_interspacing * 2 :
482 p_font->i_interspacing);
484 /* compute where to stop... */
485 i_end = (int) (i_percent * strlen(psz_text) / 100LL);
486 if(i_end > strlen(psz_text))
487 i_end = strlen(psz_text);
491 for( i_position = 0; i_position < i_end; i_position++ ,psz_text++ )
493 /* Check that the character is valid */
494 if( (*psz_text >= p_font->i_first) && (*psz_text <= p_font->i_last) )
496 /* Select character - bytes per char is always valid, event for
498 p_char = p_font->p_data + (*psz_text - p_font->i_first) * p_font->i_bytes_per_char;
499 p_border = p_char + (p_font->i_last - p_font->i_first + 1) * p_font->i_bytes_per_char;
501 /* Select base address for output */
502 switch( p_font->i_type )
504 case VOUT_FIXED_FONT:
506 * Simple fixed width font
509 /* Italic text: shift picture start right */
510 if( i_style & ITALIC_TEXT )
512 p_pic += i_bytes_per_pixel * (p_font->i_height / 3);
515 /* Print character */
516 for( i_line = 0; i_line < i_font_height; i_line ++ )
518 for( i_byte = 0; i_byte < i_font_bytes_per_line; i_byte++, p_char++, p_border++)
521 p_PutByte( p_pic + i_bytes_per_line * i_line, i_byte,
522 *p_char & i_char_mask, *p_border & i_border_mask, i_bg_mask,
523 i_char_color, i_border_color, i_bg_color );
526 /* Italic text: shift picture start left */
527 if( (i_style & ITALIC_TEXT) && !(i_line % 3) )
529 p_pic -= i_bytes_per_pixel;
533 /* Jump to next character */
534 p_pic += i_interspacing;
538 intf_DbgMsg("error: unknown font type %d", p_font->i_type );
547 /* following functions are local */
549 /*****************************************************************************
550 * PutByte8: print a fixed width font character byte in 1 Bpp
551 *****************************************************************************/
552 static void PutByte8( u8 *p_pic, int i_byte, int i_char, int i_border,
553 int i_bg, u32 i_char_color, u32 i_border_color,
556 /* Computes position offset and background mask */
558 i_bg &= ~(i_char | i_border);
560 /* Put character bits */
561 PUT_BYTE_MASK(i_char, i_char_color);
562 PUT_BYTE_MASK(i_border, i_border_color);
563 PUT_BYTE_MASK(i_bg, i_bg_color);
566 /*****************************************************************************
567 * PutByte16: print a fixed width font character byte in 2 Bpp
568 *****************************************************************************/
569 static void PutByte16( u16 *p_pic, int i_byte, int i_char, int i_border,
570 int i_bg, u32 i_char_color, u32 i_border_color,
573 /* Computes position offset and background mask */
575 i_bg &= ~(i_char | i_border);
577 /* Put character bits */
578 PUT_BYTE_MASK(i_char, i_char_color);
579 PUT_BYTE_MASK(i_border, i_border_color);
580 PUT_BYTE_MASK(i_bg, i_bg_color);
583 /*****************************************************************************
584 * PutByte24: print a fixed width font character byte in 3 Bpp
585 *****************************************************************************/
586 static void PutByte24( void *p_pic, int i_byte, byte_t i_char, byte_t i_border, byte_t i_bg,
587 u32 i_char_color, u32 i_border_color, u32 i_bg_color )
592 /*****************************************************************************
593 * PutByte32: print a fixed width font character byte in 4 Bpp
594 *****************************************************************************/
595 static void PutByte32( u32 *p_pic, int i_byte, byte_t i_char, byte_t i_border, byte_t i_bg,
596 u32 i_char_color, u32 i_border_color, u32 i_bg_color )
598 /* Computes position offset and background mask */
600 i_bg &= ~(i_char | i_border);
602 /* Put character bits */
603 PUT_BYTE_MASK(i_char, i_char_color);
604 PUT_BYTE_MASK(i_border, i_border_color);
605 PUT_BYTE_MASK(i_bg, i_bg_color);