1 /*****************************************************************************
2 * video_text.c : text manipulation functions
3 *****************************************************************************
4 * Copyright (C) 1999, 2000 VideoLAN
5 * $Id: video_text.c,v 1.22 2001/04/11 13:30:30 ej 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 <sys/param.h> /* for MAXPATHLEN */
48 extern main_t *p_main;
53 /*****************************************************************************
54 * vout_font_t: bitmap font
55 *****************************************************************************
56 * This structure is used when the system doesn't provide a convenient function
57 * to print simple characters in a buffer.
58 * VOUT_FIXED_FONTs are stored in raw mode, character after character, with a
59 * first array of characters followed by a second array of borders masks.
60 * Therefore the border masks can't be complete if the font has pixels on the
62 *****************************************************************************/
63 typedef struct vout_font_s
65 int i_type; /* font type */
66 int i_width; /* character width in pixels */
67 int i_height; /* character height in pixels */
68 int i_interspacing; /* characters interspacing in pixels */
69 int i_bytes_per_line; /* bytes per character line */
70 int i_bytes_per_char; /* bytes per character */
71 u16 i_first; /* first character */
72 u16 i_last; /* last character */
73 byte_t * p_data; /* font character data */
77 #define VOUT_FIXED_FONT 0 /* simple fixed font */
79 /*****************************************************************************
80 * vout_put_byte_t: PutByte function
81 *****************************************************************************
82 * These functions will transform masks in a set of pixels. For each pixel,
83 * character, then border and background masks are tested, and the first
84 * encountered color is set.
85 *****************************************************************************/
86 typedef void (vout_put_byte_t)( void *p_pic, int i_byte, int i_char, int i_border,
87 int i_bg, u32 i_char_color, u32 i_border_color, u32 i_bg_color );
90 /*****************************************************************************
92 *****************************************************************************/
94 /* PUT_BYTE_MASK: put pixels from a byte-wide mask. It uses a branching tree
95 * to optimize the number of tests. It is used in the PutByte functions.
96 * This macro works for 1, 2 and 4 Bpp. */
97 #define PUT_BYTE_MASK( i_mask, i_mask_color ) \
98 if( i_mask & 0xf0 ) /* one from 1111 */ \
100 if( i_mask & 0xc0 ) /* one from 1100 */ \
102 if( i_mask & 0x80 ) /* 1000 */ \
104 p_pic[0] = i_mask_color; \
105 if( i_mask & 0x40 ) /* 0100 */ \
107 p_pic[1] = i_mask_color; \
110 else /* not 1000 means 0100 */ \
112 p_pic[1] = i_mask_color; \
114 if( i_mask & 0x30 ) /* one from 0011 */ \
116 if( i_mask & 0x20 ) /* 0010 */ \
118 p_pic[2] = i_mask_color; \
119 if( i_mask & 0x10 ) /* 0001 */ \
121 p_pic[3] = i_mask_color; \
124 else /* not 0010 means 0001 */ \
126 p_pic[3] = i_mask_color; \
130 else /* not 1100 means 0011 */ \
132 if( i_mask & 0x20 ) /* 0010 */ \
134 p_pic[2] = i_mask_color; \
135 if( i_mask & 0x10 ) /* 0001 */ \
137 p_pic[3] = i_mask_color; \
140 else /* not 0010 means 0001 */ \
142 p_pic[3] = i_mask_color; \
146 if( i_mask & 0x0f ) \
148 if( i_mask & 0x0c ) /* one from 1100 */ \
150 if( i_mask & 0x08 ) /* 1000 */ \
152 p_pic[4] = i_mask_color; \
153 if( i_mask & 0x04 ) /* 0100 */ \
155 p_pic[5] = i_mask_color; \
158 else /* not 1000 means 0100 */ \
160 p_pic[5] = i_mask_color; \
162 if( i_mask & 0x03 ) /* one from 0011 */ \
164 if( i_mask & 0x02 ) /* 0010 */ \
166 p_pic[6] = i_mask_color; \
167 if( i_mask & 0x01 ) /* 0001 */ \
169 p_pic[7] = i_mask_color; \
172 else /* not 0010 means 0001 */ \
174 p_pic[7] = i_mask_color; \
178 else /* not 1100 means 0011 */ \
180 if( i_mask & 0x02 ) /* 0010 */ \
182 p_pic[6] = i_mask_color; \
183 if( i_mask & 0x01 ) /* 0001 */ \
185 p_pic[7] = i_mask_color; \
188 else /* not 0010 means 0001 */ \
190 p_pic[7] = i_mask_color; \
195 /*****************************************************************************
197 *****************************************************************************/
198 static void PutByte8 ( u8 *p_pic, int i_byte, int i_char, int i_border,
199 int i_bg, u32 i_char_color, u32 i_border_color,
201 static void PutByte16( u16 *p_pic, int i_byte, int i_char, int i_border,
202 int i_bg, u32 i_char_color, u32 i_border_color,
204 static void PutByte24( void *p_pic, int i_byte, byte_t i_char, byte_t i_border,
205 byte_t i_bg, u32 i_char_color, u32 i_border_color,
207 static void PutByte32( u32 *p_pic, int i_byte, byte_t i_char, byte_t i_border,
208 byte_t i_bg, u32 i_char_color, u32 i_border_color,
211 /*****************************************************************************
212 * vout_LoadFont: load a bitmap font from a file
213 *****************************************************************************
214 * This function will try to open a .psf font and load it. It will return
216 *****************************************************************************/
217 vout_font_t *vout_LoadFont( const char *psz_name )
219 static char * path[] = { "share", DATA_PATH, NULL, NULL };
221 char ** ppsz_path = path;
224 char * psz_vlcpath = beos_GetProgramPath();
225 int i_vlclen = strlen( psz_vlcpath );
226 #elif defined SYS_DARWIN1_3
227 static char once = 0;
228 static char app_path[ MAXPATHLEN ];
229 /* HACK TO CUT OUT trailing 'vlc' */
230 int i_pathlen = strlen( p_main->ppsz_argv[ 0 ] ) - 3;
232 int i_char, i_line; /* character and line indexes */
233 int i_file = -1; /* source file */
234 byte_t pi_buffer[2]; /* file buffer */
235 vout_font_t * p_font; /* the font itself */
241 strncpy( app_path, p_main->ppsz_argv[ 0 ], i_pathlen );
242 strcat( app_path, "share" );
243 path[ 2 ] = app_path ;
247 for( ; *ppsz_path != NULL ; ppsz_path++ )
250 /* Under BeOS, we need to add beos_GetProgramPath() to access
251 * files under the current directory */
252 if( strncmp( *ppsz_path, "/", 1 ) )
254 psz_file = malloc( strlen( psz_name ) + strlen( *ppsz_path )
256 if( psz_file == NULL )
260 sprintf( psz_file, "%s/%s/%s", psz_vlcpath, *ppsz_path, psz_name );
265 psz_file = malloc( strlen( psz_name ) + strlen( *ppsz_path ) + 2 );
266 if( psz_file == NULL )
270 sprintf( psz_file, "%s/%s", *ppsz_path, psz_name );
274 i_file = open( psz_file, O_RDONLY );
285 intf_DbgMsg( "vout error: can't open file '%s' (%s)",
286 psz_name, strerror(errno) );
290 /* Read magick number */
291 if( read( i_file, pi_buffer, 2 ) != 2 )
293 intf_ErrMsg("vout error: unexpected end of file '%s'", psz_name );
298 /* Allocate font descriptor */
299 p_font = malloc( sizeof( vout_font_t ) );
302 intf_ErrMsg( "vout error: cannot allocate vout_font_t (%s)",
309 switch( ((u16)pi_buffer[0] << 8) | pi_buffer[1] )
311 case 0x3604: /* .psf file */
313 * PSF font: simple fixed font. Only the first 256 characters are read.
314 * Those fonts are always 1 byte wide, and 256 or 512 characters long.
317 /* Read font header - two bytes indicate the font properties */
318 if( read( i_file, pi_buffer, 2 ) != 2)
320 intf_ErrMsg( "vout error: unexpected end of file '%s'", psz_name );
326 /* Copy font properties */
327 p_font->i_type = VOUT_FIXED_FONT;
329 p_font->i_height = pi_buffer[1];
330 p_font->i_interspacing = 8;
331 p_font->i_bytes_per_line = 1;
332 p_font->i_bytes_per_char = pi_buffer[1];
334 p_font->i_last = 255;
336 /* Allocate font space */
337 p_font->p_data = malloc( 2 * 256 * pi_buffer[1] );
338 if( p_font->p_data == NULL )
340 intf_ErrMsg( "vout error: cannot allocate font space (%s)",
348 if( read( i_file, p_font->p_data, 256 * pi_buffer[1] ) != 256 * pi_buffer[1] )
350 intf_ErrMsg("vout error: unexpected end of file '%s'", psz_name );
351 free( p_font->p_data );
357 /* Computes border masks - remember that masks have the same matrix as
358 * characters, so an empty character border is required to have a complete
360 for( i_char = 0; i_char <= 255; i_char++ )
362 for( i_line = 0; i_line < pi_buffer[1]; i_line++ )
365 p_font->p_data[ (i_char + 256) * pi_buffer[1] + i_line ] =
366 ((p_font->p_data[ i_char * pi_buffer[1] + i_line ] << 1) |
367 (p_font->p_data[ i_char * pi_buffer[1] + i_line ] >> 1) |
368 (i_line > 0 ? p_font->p_data[ i_char * pi_buffer[1] + i_line - 1]: 0) |
369 (i_line < pi_buffer[1] - 1 ? p_font->p_data[ i_char * pi_buffer[1] + i_line + 1]: 0))
370 & ~p_font->p_data[ i_char * pi_buffer[1] + i_line ];
376 intf_ErrMsg("vout error: file '%s' has an unknown format", psz_name );
384 intf_DbgMsg( "loaded %s: type %d, %d-%dx%d", psz_name, p_font->i_type,
385 p_font->i_width, p_font->i_interspacing, p_font->i_height );
389 /*****************************************************************************
390 * vout_UnloadFont: unload a font
391 *****************************************************************************
392 * This function free the resources allocated by vout_LoadFont
393 *****************************************************************************/
394 void vout_UnloadFont( vout_font_t *p_font )
396 intf_DbgMsg( "vout: unloading font %p", p_font );
397 free( p_font->p_data );
401 /*****************************************************************************
402 * vout_TextSize: return the dimensions of a text
403 *****************************************************************************
404 * This function is used to align text. It returns the width and height of a
406 *****************************************************************************/
407 void vout_TextSize( vout_font_t *p_font, int i_style, const char *psz_text, int *pi_width, int *pi_height )
409 switch( p_font->i_type )
411 case VOUT_FIXED_FONT:
412 *pi_width = ((i_style & WIDE_TEXT) ? p_font->i_interspacing * 2 : p_font->i_interspacing) *
413 (strlen( psz_text ) - 1) + p_font->i_width;
414 *pi_height = p_font->i_height;
415 if( i_style & ITALIC_TEXT )
417 *pi_width = *pi_height / 3;
422 intf_DbgMsg("error: unknown font type %d", p_font->i_type );
428 /*****************************************************************************
429 * vout_Print: low level printing function
430 *****************************************************************************
431 * This function prints a text, without clipping, in a buffer using a
432 * previously loaded bitmap font.
433 *****************************************************************************/
434 void vout_Print( vout_font_t *p_font, byte_t *p_pic, int i_bytes_per_pixel, int i_bytes_per_line,
435 u32 i_char_color, u32 i_border_color, u32 i_bg_color, int i_style, const char *psz_text, int i_percent)
437 byte_t *p_char, *p_border; /* character and border mask data */
438 int i_char_mask, i_border_mask, i_bg_mask; /* masks */
439 int i_line; /* current line in character */
440 int i_byte; /* current byte in character */
441 int i_interspacing; /* offset between two chars */
442 int i_font_bytes_per_line, i_font_height; /* font properties */
443 int i_position, i_end; /* current position */
444 vout_put_byte_t *p_PutByte; /* PutByte function */
446 /* FIXME: background: can be something else that whole byte ?? */
448 /* Select output function */
449 switch( i_bytes_per_pixel )
452 p_PutByte = (vout_put_byte_t *) PutByte8;
455 p_PutByte = (vout_put_byte_t *) PutByte16;
458 p_PutByte = (vout_put_byte_t *) PutByte24;
462 p_PutByte = (vout_put_byte_t *) PutByte32;
466 /* Choose masks and copy font data to local variables */
467 i_char_mask = (i_style & VOID_TEXT) ? 0 : 0xff;
468 i_border_mask = (i_style & OUTLINED_TEXT) ? 0xff : 0;
469 i_bg_mask = (i_style & OPAQUE_TEXT) ? 0xff : 0;
471 i_font_bytes_per_line = p_font->i_bytes_per_line;
472 i_font_height = p_font->i_height;
473 i_interspacing = i_bytes_per_pixel * ((i_style & WIDE_TEXT) ?
474 p_font->i_interspacing * 2 :
475 p_font->i_interspacing);
477 /* compute where to stop... */
478 i_end = (int) (i_percent * strlen(psz_text) / 100LL);
479 if(i_end > strlen(psz_text))
480 i_end = strlen(psz_text);
484 for( i_position = 0; i_position < i_end; i_position++ ,psz_text++ )
486 /* Check that the character is valid */
487 if( (*psz_text >= p_font->i_first) && (*psz_text <= p_font->i_last) )
489 /* Select character - bytes per char is always valid, event for
491 p_char = p_font->p_data + (*psz_text - p_font->i_first) * p_font->i_bytes_per_char;
492 p_border = p_char + (p_font->i_last - p_font->i_first + 1) * p_font->i_bytes_per_char;
494 /* Select base address for output */
495 switch( p_font->i_type )
497 case VOUT_FIXED_FONT:
499 * Simple fixed width font
502 /* Italic text: shift picture start right */
503 if( i_style & ITALIC_TEXT )
505 p_pic += i_bytes_per_pixel * (p_font->i_height / 3);
508 /* Print character */
509 for( i_line = 0; i_line < i_font_height; i_line ++ )
511 for( i_byte = 0; i_byte < i_font_bytes_per_line; i_byte++, p_char++, p_border++)
514 p_PutByte( p_pic + i_bytes_per_line * i_line, i_byte,
515 *p_char & i_char_mask, *p_border & i_border_mask, i_bg_mask,
516 i_char_color, i_border_color, i_bg_color );
519 /* Italic text: shift picture start left */
520 if( (i_style & ITALIC_TEXT) && !(i_line % 3) )
522 p_pic -= i_bytes_per_pixel;
526 /* Jump to next character */
527 p_pic += i_interspacing;
531 intf_DbgMsg("error: unknown font type %d", p_font->i_type );
540 /* following functions are local */
542 /*****************************************************************************
543 * PutByte8: print a fixed width font character byte in 1 Bpp
544 *****************************************************************************/
545 static void PutByte8( u8 *p_pic, int i_byte, int i_char, int i_border,
546 int i_bg, u32 i_char_color, u32 i_border_color,
549 /* Computes position offset and background mask */
551 i_bg &= ~(i_char | i_border);
553 /* Put character bits */
554 PUT_BYTE_MASK(i_char, i_char_color);
555 PUT_BYTE_MASK(i_border, i_border_color);
556 PUT_BYTE_MASK(i_bg, i_bg_color);
559 /*****************************************************************************
560 * PutByte16: print a fixed width font character byte in 2 Bpp
561 *****************************************************************************/
562 static void PutByte16( u16 *p_pic, int i_byte, int i_char, int i_border,
563 int i_bg, u32 i_char_color, u32 i_border_color,
566 /* Computes position offset and background mask */
568 i_bg &= ~(i_char | i_border);
570 /* Put character bits */
571 PUT_BYTE_MASK(i_char, i_char_color);
572 PUT_BYTE_MASK(i_border, i_border_color);
573 PUT_BYTE_MASK(i_bg, i_bg_color);
576 /*****************************************************************************
577 * PutByte24: print a fixed width font character byte in 3 Bpp
578 *****************************************************************************/
579 static void PutByte24( void *p_pic, int i_byte, byte_t i_char, byte_t i_border, byte_t i_bg,
580 u32 i_char_color, u32 i_border_color, u32 i_bg_color )
585 /*****************************************************************************
586 * PutByte32: print a fixed width font character byte in 4 Bpp
587 *****************************************************************************/
588 static void PutByte32( u32 *p_pic, int i_byte, byte_t i_char, byte_t i_border, byte_t i_bg,
589 u32 i_char_color, u32 i_border_color, u32 i_bg_color )
591 /* Computes position offset and background mask */
593 i_bg &= ~(i_char | i_border);
595 /* Put character bits */
596 PUT_BYTE_MASK(i_char, i_char_color);
597 PUT_BYTE_MASK(i_border, i_border_color);
598 PUT_BYTE_MASK(i_bg, i_bg_color);