1 /*****************************************************************************
2 * video_text.c : text manipulation functions
3 *****************************************************************************
4 * Copyright (C) 1999, 2000 VideoLAN
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
21 *****************************************************************************/
23 /*****************************************************************************
25 *****************************************************************************/
28 #include <errno.h> /* errno */
29 #include <stdlib.h> /* free() */
30 #include <stdio.h> /* sprintf() */
31 #include <string.h> /* strerror() */
32 #include <fcntl.h> /* open() */
33 #include <unistd.h> /* read(), close() */
37 #include "video_text.h"
41 /*****************************************************************************
42 * vout_font_t: bitmap font
43 *****************************************************************************
44 * This structure is used when the system doesn't provide a convenient function
45 * to print simple characters in a buffer.
46 * VOUT_FIXED_FONTs are stored in raw mode, character after character, with a
47 * first array of characters followed by a second array of borders masks.
48 * Therefore the border masks can't be complete if the font has pixels on the
50 *****************************************************************************/
51 typedef struct vout_font_s
53 int i_type; /* font type */
54 int i_width; /* character width in pixels */
55 int i_height; /* character height in pixels */
56 int i_interspacing; /* characters interspacing in pixels */
57 int i_bytes_per_line; /* bytes per character line */
58 int i_bytes_per_char; /* bytes per character */
59 u16 i_first; /* first character */
60 u16 i_last; /* last character */
61 byte_t * p_data; /* font character data */
65 #define VOUT_FIXED_FONT 0 /* simple fixed font */
67 /*****************************************************************************
68 * vout_put_byte_t: PutByte function
69 *****************************************************************************
70 * These functions will transform masks in a set of pixels. For each pixel,
71 * character, then border and background masks are tested, and the first
72 * encountered color is set.
73 *****************************************************************************/
74 typedef void (vout_put_byte_t)( void *p_pic, int i_byte, int i_char, int i_border,
75 int i_bg, u32 i_char_color, u32 i_border_color, u32 i_bg_color );
78 /*****************************************************************************
80 *****************************************************************************/
82 /* PUT_BYTE_MASK: put pixels from a byte-wide mask. It uses a branching tree
83 * to optimize the number of tests. It is used in the PutByte functions.
84 * This macro works for 1, 2 and 4 Bpp. */
85 #define PUT_BYTE_MASK( i_mask, i_mask_color ) \
86 if( i_mask & 0xf0 ) /* one from 1111 */ \
88 if( i_mask & 0xc0 ) /* one from 1100 */ \
90 if( i_mask & 0x80 ) /* 1000 */ \
92 p_pic[0] = i_mask_color; \
93 if( i_mask & 0x40 ) /* 0100 */ \
95 p_pic[1] = i_mask_color; \
98 else /* not 1000 means 0100 */ \
100 p_pic[1] = i_mask_color; \
102 if( i_mask & 0x30 ) /* one from 0011 */ \
104 if( i_mask & 0x20 ) /* 0010 */ \
106 p_pic[2] = i_mask_color; \
107 if( i_mask & 0x10 ) /* 0001 */ \
109 p_pic[3] = i_mask_color; \
112 else /* not 0010 means 0001 */ \
114 p_pic[3] = i_mask_color; \
118 else /* not 1100 means 0011 */ \
120 if( i_mask & 0x20 ) /* 0010 */ \
122 p_pic[2] = i_mask_color; \
123 if( i_mask & 0x10 ) /* 0001 */ \
125 p_pic[3] = i_mask_color; \
128 else /* not 0010 means 0001 */ \
130 p_pic[3] = i_mask_color; \
134 if( i_mask & 0x0f ) \
136 if( i_mask & 0x0c ) /* one from 1100 */ \
138 if( i_mask & 0x08 ) /* 1000 */ \
140 p_pic[4] = i_mask_color; \
141 if( i_mask & 0x04 ) /* 0100 */ \
143 p_pic[5] = i_mask_color; \
146 else /* not 1000 means 0100 */ \
148 p_pic[5] = i_mask_color; \
150 if( i_mask & 0x03 ) /* one from 0011 */ \
152 if( i_mask & 0x02 ) /* 0010 */ \
154 p_pic[6] = i_mask_color; \
155 if( i_mask & 0x01 ) /* 0001 */ \
157 p_pic[7] = i_mask_color; \
160 else /* not 0010 means 0001 */ \
162 p_pic[7] = i_mask_color; \
166 else /* not 1100 means 0011 */ \
168 if( i_mask & 0x02 ) /* 0010 */ \
170 p_pic[6] = i_mask_color; \
171 if( i_mask & 0x01 ) /* 0001 */ \
173 p_pic[7] = i_mask_color; \
176 else /* not 0010 means 0001 */ \
178 p_pic[7] = i_mask_color; \
183 /*****************************************************************************
185 *****************************************************************************/
186 static void PutByte8 ( u8 *p_pic, int i_byte, int i_char, int i_border,
187 int i_bg, u32 i_char_color, u32 i_border_color,
189 static void PutByte16( u16 *p_pic, int i_byte, int i_char, int i_border,
190 int i_bg, u32 i_char_color, u32 i_border_color,
192 static void PutByte24( void *p_pic, int i_byte, byte_t i_char, byte_t i_border,
193 byte_t i_bg, u32 i_char_color, u32 i_border_color,
195 static void PutByte32( u32 *p_pic, int i_byte, byte_t i_char, byte_t i_border,
196 byte_t i_bg, u32 i_char_color, u32 i_border_color,
199 /*****************************************************************************
200 * vout_LoadFont: load a bitmap font from a file
201 *****************************************************************************
202 * This function will try to open a .psf font and load it. It will return
204 *****************************************************************************/
205 vout_font_t *vout_LoadFont( const char *psz_name )
207 static char * path[] = { "share", DATA_PATH, NULL };
209 char ** ppsz_path = path;
212 char * psz_vlcpath = beos_GetProgramPath();
213 int i_vlclen = strlen( psz_vlcpath );
215 int i_char, i_line; /* character and line indexes */
216 int i_file = -1; /* source file */
217 byte_t pi_buffer[2]; /* file buffer */
218 vout_font_t * p_font; /* the font itself */
220 for( ; *ppsz_path != NULL ; ppsz_path++ )
223 /* Under BeOS, we need to add beos_GetProgramPath() to access
224 * files under the current directory */
225 if( strncmp( *ppsz_path, "/", 1 ) )
227 psz_file = malloc( strlen( psz_name ) + strlen( *ppsz_path )
229 if( psz_file == NULL )
233 sprintf( psz_file, "%s/%s/%s", psz_vlcpath, *ppsz_path, psz_name );
238 psz_file = malloc( strlen( psz_name ) + strlen( *ppsz_path ) + 2 );
239 if( psz_file == NULL )
243 sprintf( psz_file, "%s/%s", *ppsz_path, psz_name );
247 i_file = open( psz_file, O_RDONLY );
258 intf_DbgMsg( "vout error: can't open file '%s' (%s)",
259 psz_name, strerror(errno) );
263 /* Read magick number */
264 if( read( i_file, pi_buffer, 2 ) != 2 )
266 intf_ErrMsg("vout error: unexpected end of file '%s'", psz_name );
271 /* Allocate font descriptor */
272 p_font = malloc( sizeof( vout_font_t ) );
275 intf_ErrMsg( "vout error: cannot allocate vout_font_t (%s)",
282 switch( ((u16)pi_buffer[0] << 8) | pi_buffer[1] )
284 case 0x3604: /* .psf file */
286 * PSF font: simple fixed font. Only the first 256 characters are read.
287 * Those fonts are always 1 byte wide, and 256 or 512 characters long.
290 /* Read font header - two bytes indicate the font properties */
291 if( read( i_file, pi_buffer, 2 ) != 2)
293 intf_ErrMsg( "vout error: unexpected end of file '%s'", psz_name );
299 /* Copy font properties */
300 p_font->i_type = VOUT_FIXED_FONT;
302 p_font->i_height = pi_buffer[1];
303 p_font->i_interspacing = 8;
304 p_font->i_bytes_per_line = 1;
305 p_font->i_bytes_per_char = pi_buffer[1];
307 p_font->i_last = 255;
309 /* Allocate font space */
310 p_font->p_data = malloc( 2 * 256 * pi_buffer[1] );
311 if( p_font->p_data == NULL )
313 intf_ErrMsg( "vout error: cannot allocate font space (%s)",
321 if( read( i_file, p_font->p_data, 256 * pi_buffer[1] ) != 256 * pi_buffer[1] )
323 intf_ErrMsg("vout error: unexpected end of file '%s'", psz_name );
324 free( p_font->p_data );
330 /* Computes border masks - remember that masks have the same matrix as
331 * characters, so an empty character border is required to have a complete
333 for( i_char = 0; i_char <= 255; i_char++ )
335 for( i_line = 0; i_line < pi_buffer[1]; i_line++ )
338 p_font->p_data[ (i_char + 256) * pi_buffer[1] + i_line ] =
339 ((p_font->p_data[ i_char * pi_buffer[1] + i_line ] << 1) |
340 (p_font->p_data[ i_char * pi_buffer[1] + i_line ] >> 1) |
341 (i_line > 0 ? p_font->p_data[ i_char * pi_buffer[1] + i_line - 1]: 0) |
342 (i_line < pi_buffer[1] - 1 ? p_font->p_data[ i_char * pi_buffer[1] + i_line + 1]: 0))
343 & ~p_font->p_data[ i_char * pi_buffer[1] + i_line ];
349 intf_ErrMsg("vout error: file '%s' has an unknown format", psz_name );
357 intf_DbgMsg( "loaded %s: type %d, %d-%dx%d", psz_name, p_font->i_type,
358 p_font->i_width, p_font->i_interspacing, p_font->i_height );
362 /*****************************************************************************
363 * vout_UnloadFont: unload a font
364 *****************************************************************************
365 * This function free the resources allocated by vout_LoadFont
366 *****************************************************************************/
367 void vout_UnloadFont( vout_font_t *p_font )
369 intf_DbgMsg( "vout: unloading font %p", p_font );
370 free( p_font->p_data );
374 /*****************************************************************************
375 * vout_TextSize: return the dimensions of a text
376 *****************************************************************************
377 * This function is used to align text. It returns the width and height of a
379 *****************************************************************************/
380 void vout_TextSize( vout_font_t *p_font, int i_style, const char *psz_text, int *pi_width, int *pi_height )
382 switch( p_font->i_type )
384 case VOUT_FIXED_FONT:
385 *pi_width = ((i_style & WIDE_TEXT) ? p_font->i_interspacing * 2 : p_font->i_interspacing) *
386 (strlen( psz_text ) - 1) + p_font->i_width;
387 *pi_height = p_font->i_height;
388 if( i_style & ITALIC_TEXT )
390 *pi_width = *pi_height / 3;
395 intf_DbgMsg("error: unknown font type %d", p_font->i_type );
401 /*****************************************************************************
402 * vout_Print: low level printing function
403 *****************************************************************************
404 * This function prints a text, without clipping, in a buffer using a
405 * previously loaded bitmap font.
406 *****************************************************************************/
407 void vout_Print( vout_font_t *p_font, byte_t *p_pic, int i_bytes_per_pixel, int i_bytes_per_line,
408 u32 i_char_color, u32 i_border_color, u32 i_bg_color, int i_style, const char *psz_text, int i_percent)
410 byte_t *p_char, *p_border; /* character and border mask data */
411 int i_char_mask, i_border_mask, i_bg_mask; /* masks */
412 int i_line; /* current line in character */
413 int i_byte; /* current byte in character */
414 int i_interspacing; /* offset between two chars */
415 int i_font_bytes_per_line, i_font_height; /* font properties */
416 int i_position, i_end; /* current position */
417 vout_put_byte_t *p_PutByte; /* PutByte function */
419 /* FIXME: background: can be something else that whole byte ?? */
421 /* Select output function */
422 switch( i_bytes_per_pixel )
425 p_PutByte = (vout_put_byte_t *) PutByte8;
428 p_PutByte = (vout_put_byte_t *) PutByte16;
431 p_PutByte = (vout_put_byte_t *) PutByte24;
435 p_PutByte = (vout_put_byte_t *) PutByte32;
439 /* Choose masks and copy font data to local variables */
440 i_char_mask = (i_style & VOID_TEXT) ? 0 : 0xff;
441 i_border_mask = (i_style & OUTLINED_TEXT) ? 0xff : 0;
442 i_bg_mask = (i_style & OPAQUE_TEXT) ? 0xff : 0;
444 i_font_bytes_per_line = p_font->i_bytes_per_line;
445 i_font_height = p_font->i_height;
446 i_interspacing = i_bytes_per_pixel * ((i_style & WIDE_TEXT) ?
447 p_font->i_interspacing * 2 :
448 p_font->i_interspacing);
450 /* compute where to stop... */
451 i_end = (int) (i_percent * strlen(psz_text) / 100LL);
452 if(i_end > strlen(psz_text))
453 i_end = strlen(psz_text);
457 for( i_position = 0; i_position < i_end; i_position++ ,psz_text++ )
459 /* Check that the character is valid */
460 if( (*psz_text >= p_font->i_first) && (*psz_text <= p_font->i_last) )
462 /* Select character - bytes per char is always valid, event for
464 p_char = p_font->p_data + (*psz_text - p_font->i_first) * p_font->i_bytes_per_char;
465 p_border = p_char + (p_font->i_last - p_font->i_first + 1) * p_font->i_bytes_per_char;
467 /* Select base address for output */
468 switch( p_font->i_type )
470 case VOUT_FIXED_FONT:
472 * Simple fixed width font
475 /* Italic text: shift picture start right */
476 if( i_style & ITALIC_TEXT )
478 p_pic += i_bytes_per_pixel * (p_font->i_height / 3);
481 /* Print character */
482 for( i_line = 0; i_line < i_font_height; i_line ++ )
484 for( i_byte = 0; i_byte < i_font_bytes_per_line; i_byte++, p_char++, p_border++)
487 p_PutByte( p_pic + i_bytes_per_line * i_line, i_byte,
488 *p_char & i_char_mask, *p_border & i_border_mask, i_bg_mask,
489 i_char_color, i_border_color, i_bg_color );
492 /* Italic text: shift picture start left */
493 if( (i_style & ITALIC_TEXT) && !(i_line % 3) )
495 p_pic -= i_bytes_per_pixel;
499 /* Jump to next character */
500 p_pic += i_interspacing;
504 intf_DbgMsg("error: unknown font type %d", p_font->i_type );
513 /* following functions are local */
515 /*****************************************************************************
516 * PutByte8: print a fixed width font character byte in 1 Bpp
517 *****************************************************************************/
518 static void PutByte8( u8 *p_pic, int i_byte, int i_char, int i_border,
519 int i_bg, u32 i_char_color, u32 i_border_color,
522 /* Computes position offset and background mask */
524 i_bg &= ~(i_char | i_border);
526 /* Put character bits */
527 PUT_BYTE_MASK(i_char, i_char_color);
528 PUT_BYTE_MASK(i_border, i_border_color);
529 PUT_BYTE_MASK(i_bg, i_bg_color);
532 /*****************************************************************************
533 * PutByte16: print a fixed width font character byte in 2 Bpp
534 *****************************************************************************/
535 static void PutByte16( u16 *p_pic, int i_byte, int i_char, int i_border,
536 int i_bg, u32 i_char_color, u32 i_border_color,
539 /* Computes position offset and background mask */
541 i_bg &= ~(i_char | i_border);
543 /* Put character bits */
544 PUT_BYTE_MASK(i_char, i_char_color);
545 PUT_BYTE_MASK(i_border, i_border_color);
546 PUT_BYTE_MASK(i_bg, i_bg_color);
549 /*****************************************************************************
550 * PutByte24: print a fixed width font character byte in 3 Bpp
551 *****************************************************************************/
552 static void PutByte24( void *p_pic, int i_byte, byte_t i_char, byte_t i_border, byte_t i_bg,
553 u32 i_char_color, u32 i_border_color, u32 i_bg_color )
558 /*****************************************************************************
559 * PutByte32: print a fixed width font character byte in 4 Bpp
560 *****************************************************************************/
561 static void PutByte32( u32 *p_pic, int i_byte, byte_t i_char, byte_t i_border, byte_t i_bg,
562 u32 i_char_color, u32 i_border_color, u32 i_bg_color )
564 /* Computes position offset and background mask */
566 i_bg &= ~(i_char | i_border);
568 /* Put character bits */
569 PUT_BYTE_MASK(i_char, i_char_color);
570 PUT_BYTE_MASK(i_border, i_border_color);
571 PUT_BYTE_MASK(i_bg, i_bg_color);