1 /*****************************************************************************
2 * video_text.c : text manipulation functions
3 *****************************************************************************
4 * Copyright (C) 1999-2001 VideoLAN
5 * $Id: video_text.c,v 1.33 2001/12/30 07:09:56 sam 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 *****************************************************************************/
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() */
34 #include <videolan/vlc.h>
37 # include <unistd.h> /* read(), close() */
38 #elif defined( _MSC_VER ) && defined( _WIN32 )
46 #include "video_text.h"
48 /*****************************************************************************
49 * vout_font_t: bitmap font
50 *****************************************************************************
51 * This structure is used when the system doesn't provide a convenient function
52 * to print simple characters in a buffer.
53 * VOUT_FIXED_FONTs are stored in raw mode, character after character, with a
54 * first array of characters followed by a second array of borders masks.
55 * Therefore the border masks can't be complete if the font has pixels on the
57 *****************************************************************************/
58 typedef struct vout_font_s
60 int i_type; /* font type */
61 int i_width; /* character width in pixels */
62 int i_height; /* character height in pixels */
63 int i_interspacing; /* characters interspacing in pixels */
64 int i_bytes_per_line; /* bytes per character line */
65 int i_bytes_per_char; /* bytes per character */
66 u16 i_first; /* first character */
67 u16 i_last; /* last character */
68 byte_t * p_data; /* font character data */
72 #define VOUT_FIXED_FONT 0 /* simple fixed font */
74 /*****************************************************************************
75 * vout_put_byte_t: PutByte function
76 *****************************************************************************
77 * These functions will transform masks in a set of pixels. For each pixel,
78 * character, then border and background masks are tested, and the first
79 * encountered color is set.
80 *****************************************************************************/
81 typedef void (vout_put_byte_t)( void *p_pic, int i_byte, int i_char, int i_border,
82 int i_bg, u32 i_char_color, u32 i_border_color, u32 i_bg_color );
85 /*****************************************************************************
87 *****************************************************************************/
89 /* PUT_BYTE_MASK: put pixels from a byte-wide mask. It uses a branching tree
90 * to optimize the number of tests. It is used in the PutByte functions.
91 * This macro works for 1, 2 and 4 Bpp. */
92 #define PUT_BYTE_MASK( i_mask, i_mask_color ) \
93 if( i_mask & 0xf0 ) /* one from 1111 */ \
95 if( i_mask & 0xc0 ) /* one from 1100 */ \
97 if( i_mask & 0x80 ) /* 1000 */ \
99 p_pic[0] = i_mask_color; \
100 if( i_mask & 0x40 ) /* 0100 */ \
102 p_pic[1] = i_mask_color; \
105 else /* not 1000 means 0100 */ \
107 p_pic[1] = i_mask_color; \
109 if( i_mask & 0x30 ) /* one from 0011 */ \
111 if( i_mask & 0x20 ) /* 0010 */ \
113 p_pic[2] = i_mask_color; \
114 if( i_mask & 0x10 ) /* 0001 */ \
116 p_pic[3] = i_mask_color; \
119 else /* not 0010 means 0001 */ \
121 p_pic[3] = i_mask_color; \
125 else /* not 1100 means 0011 */ \
127 if( i_mask & 0x20 ) /* 0010 */ \
129 p_pic[2] = i_mask_color; \
130 if( i_mask & 0x10 ) /* 0001 */ \
132 p_pic[3] = i_mask_color; \
135 else /* not 0010 means 0001 */ \
137 p_pic[3] = i_mask_color; \
141 if( i_mask & 0x0f ) \
143 if( i_mask & 0x0c ) /* one from 1100 */ \
145 if( i_mask & 0x08 ) /* 1000 */ \
147 p_pic[4] = i_mask_color; \
148 if( i_mask & 0x04 ) /* 0100 */ \
150 p_pic[5] = i_mask_color; \
153 else /* not 1000 means 0100 */ \
155 p_pic[5] = i_mask_color; \
157 if( i_mask & 0x03 ) /* one from 0011 */ \
159 if( i_mask & 0x02 ) /* 0010 */ \
161 p_pic[6] = i_mask_color; \
162 if( i_mask & 0x01 ) /* 0001 */ \
164 p_pic[7] = i_mask_color; \
167 else /* not 0010 means 0001 */ \
169 p_pic[7] = i_mask_color; \
173 else /* not 1100 means 0011 */ \
175 if( i_mask & 0x02 ) /* 0010 */ \
177 p_pic[6] = i_mask_color; \
178 if( i_mask & 0x01 ) /* 0001 */ \
180 p_pic[7] = i_mask_color; \
183 else /* not 0010 means 0001 */ \
185 p_pic[7] = i_mask_color; \
190 /*****************************************************************************
192 *****************************************************************************/
193 static void PutByte8 ( u8 *p_pic, int i_byte, int i_char, int i_border,
194 int i_bg, u32 i_char_color, u32 i_border_color,
196 static void PutByte16( u16 *p_pic, int i_byte, int i_char, int i_border,
197 int i_bg, u32 i_char_color, u32 i_border_color,
199 static void PutByte24( void *p_pic, int i_byte, byte_t i_char, byte_t i_border,
200 byte_t i_bg, u32 i_char_color, u32 i_border_color,
202 static void PutByte32( u32 *p_pic, int i_byte, byte_t i_char, byte_t i_border,
203 byte_t i_bg, u32 i_char_color, u32 i_border_color,
206 /*****************************************************************************
207 * vout_LoadFont: load a bitmap font from a file
208 *****************************************************************************
209 * This function will try to open a .psf font and load it. It will return
211 *****************************************************************************/
212 vout_font_t *vout_LoadFont( const char *psz_name )
214 static char * path[] = { "share", DATA_PATH, NULL, NULL };
216 char ** ppsz_path = path;
218 #if defined( SYS_BEOS ) || defined( SYS_DARWIN )
219 char * psz_vlcpath = system_GetProgramPath();
220 int i_vlclen = strlen( psz_vlcpath );
222 int i_char, i_line; /* character and line indexes */
223 int i_file = -1; /* source file */
224 byte_t pi_buffer[2]; /* file buffer */
225 vout_font_t * p_font; /* the font itself */
227 for( ; *ppsz_path != NULL ; ppsz_path++ )
229 #if defined( SYS_BEOS ) || defined( SYS_DARWIN )
230 /* Under BeOS, we need to add beos_GetProgramPath() to access
231 * files under the current directory */
232 if( strncmp( *ppsz_path, "/", 1 ) )
234 psz_file = malloc( strlen( psz_name ) + strlen( *ppsz_path )
236 if( psz_file == NULL )
240 sprintf( psz_file, "%s/%s/%s", psz_vlcpath, *ppsz_path, psz_name );
245 psz_file = malloc( strlen( psz_name ) + strlen( *ppsz_path ) + 2 );
246 if( psz_file == NULL )
250 sprintf( psz_file, "%s/%s", *ppsz_path, psz_name );
254 i_file = open( psz_file, O_RDONLY );
265 intf_DbgMsg( "vout error: can't open file '%s' (%s)",
266 psz_name, strerror(errno) );
270 /* Read magick number */
271 if( read( i_file, pi_buffer, 2 ) != 2 )
273 intf_ErrMsg("vout error: unexpected end of file '%s'", psz_name );
278 /* Allocate font descriptor */
279 p_font = malloc( sizeof( vout_font_t ) );
282 intf_ErrMsg( "vout error: cannot allocate vout_font_t (%s)",
289 switch( ((u16)pi_buffer[0] << 8) | pi_buffer[1] )
291 case 0x3604: /* .psf file */
293 * PSF font: simple fixed font. Only the first 256 characters are read.
294 * Those fonts are always 1 byte wide, and 256 or 512 characters long.
297 /* Read font header - two bytes indicate the font properties */
298 if( read( i_file, pi_buffer, 2 ) != 2)
300 intf_ErrMsg( "vout error: unexpected end of file '%s'", psz_name );
306 /* Copy font properties */
307 p_font->i_type = VOUT_FIXED_FONT;
309 p_font->i_height = pi_buffer[1];
310 p_font->i_interspacing = 8;
311 p_font->i_bytes_per_line = 1;
312 p_font->i_bytes_per_char = pi_buffer[1];
314 p_font->i_last = 255;
316 /* Allocate font space */
317 p_font->p_data = malloc( 2 * 256 * pi_buffer[1] );
318 if( p_font->p_data == NULL )
320 intf_ErrMsg( "vout error: cannot allocate font space (%s)",
328 if( read( i_file, p_font->p_data, 256 * pi_buffer[1] ) != 256 * pi_buffer[1] )
330 intf_ErrMsg("vout error: unexpected end of file '%s'", psz_name );
331 free( p_font->p_data );
337 /* Computes border masks - remember that masks have the same matrix as
338 * characters, so an empty character border is required to have a complete
340 for( i_char = 0; i_char <= 255; i_char++ )
342 for( i_line = 0; i_line < pi_buffer[1]; i_line++ )
345 p_font->p_data[ (i_char + 256) * pi_buffer[1] + i_line ] =
346 ((p_font->p_data[ i_char * pi_buffer[1] + i_line ] << 1) |
347 (p_font->p_data[ i_char * pi_buffer[1] + i_line ] >> 1) |
348 (i_line > 0 ? p_font->p_data[ i_char * pi_buffer[1] + i_line - 1]: 0) |
349 (i_line < pi_buffer[1] - 1 ? p_font->p_data[ i_char * pi_buffer[1] + i_line + 1]: 0))
350 & ~p_font->p_data[ i_char * pi_buffer[1] + i_line ];
356 intf_ErrMsg("vout error: file '%s' has an unknown format", psz_name );
364 intf_DbgMsg( "loaded %s: type %d, %d-%dx%d", psz_name, p_font->i_type,
365 p_font->i_width, p_font->i_interspacing, p_font->i_height );
369 /*****************************************************************************
370 * vout_UnloadFont: unload a font
371 *****************************************************************************
372 * This function free the resources allocated by vout_LoadFont
373 *****************************************************************************/
374 void vout_UnloadFont( vout_font_t *p_font )
376 /* If no font was loaded, do nothing */
382 intf_DbgMsg( "vout: unloading font %p", p_font );
383 free( p_font->p_data );
387 /*****************************************************************************
388 * vout_TextSize: return the dimensions of a text
389 *****************************************************************************
390 * This function is used to align text. It returns the width and height of a
392 *****************************************************************************/
393 void vout_TextSize( vout_font_t *p_font, int i_style, const char *psz_text, int *pi_width, int *pi_height )
395 /* If no font was loaded, do nothing */
398 *pi_width = *pi_height = 0;
402 switch( p_font->i_type )
404 case VOUT_FIXED_FONT:
405 *pi_width = ((i_style & WIDE_TEXT) ? p_font->i_interspacing * 2 : p_font->i_interspacing) *
406 (strlen( psz_text ) - 1) + p_font->i_width;
407 *pi_height = p_font->i_height;
408 if( i_style & ITALIC_TEXT )
410 *pi_width = *pi_height / 3;
415 intf_ErrMsg("error: unknown font type %d", p_font->i_type );
421 /*****************************************************************************
422 * vout_Print: low level printing function
423 *****************************************************************************
424 * This function prints a text, without clipping, in a buffer using a
425 * previously loaded bitmap font.
426 *****************************************************************************/
427 void vout_Print( vout_font_t *p_font, byte_t *p_pic, int i_bytes_per_pixel, int i_bytes_per_line,
428 u32 i_char_color, u32 i_border_color, u32 i_bg_color, int i_style, const char *psz_text, int i_percent)
430 byte_t *p_char, *p_border; /* character and border mask data */
431 int i_char_mask, i_border_mask, i_bg_mask; /* masks */
432 int i_line; /* current line in character */
433 int i_byte; /* current byte in character */
434 int i_interspacing; /* offset between two chars */
435 int i_font_bytes_per_line, i_font_height; /* font properties */
436 int i_position, i_end; /* current position */
437 vout_put_byte_t *p_PutByte; /* PutByte function */
439 /* If no font was loaded, do nothing */
445 /* FIXME: background: can be something else that whole byte ?? */
447 /* Select output function */
448 switch( i_bytes_per_pixel )
451 p_PutByte = (vout_put_byte_t *) PutByte8;
454 p_PutByte = (vout_put_byte_t *) PutByte16;
457 p_PutByte = (vout_put_byte_t *) PutByte24;
461 p_PutByte = (vout_put_byte_t *) PutByte32;
465 /* Choose masks and copy font data to local variables */
466 i_char_mask = (i_style & VOID_TEXT) ? 0 : 0xff;
467 i_border_mask = (i_style & OUTLINED_TEXT) ? 0xff : 0;
468 i_bg_mask = (i_style & OPAQUE_TEXT) ? 0xff : 0;
470 i_font_bytes_per_line = p_font->i_bytes_per_line;
471 i_font_height = p_font->i_height;
472 i_interspacing = i_bytes_per_pixel * ((i_style & WIDE_TEXT) ?
473 p_font->i_interspacing * 2 :
474 p_font->i_interspacing);
476 /* compute where to stop... */
477 i_end = (int) (i_percent * strlen(psz_text) / I64C(100));
478 if(i_end > strlen(psz_text))
479 i_end = strlen(psz_text);
483 for( i_position = 0; i_position < i_end; i_position++ ,psz_text++ )
485 /* Check that the character is valid */
486 if( (*psz_text >= p_font->i_first) && (*psz_text <= p_font->i_last) )
488 /* Select character - bytes per char is always valid, event for
490 p_char = p_font->p_data + (*psz_text - p_font->i_first) * p_font->i_bytes_per_char;
491 p_border = p_char + (p_font->i_last - p_font->i_first + 1) * p_font->i_bytes_per_char;
493 /* Select base address for output */
494 switch( p_font->i_type )
496 case VOUT_FIXED_FONT:
498 * Simple fixed width font
501 /* Italic text: shift picture start right */
502 if( i_style & ITALIC_TEXT )
504 p_pic += i_bytes_per_pixel * (p_font->i_height / 3);
507 /* Print character */
508 for( i_line = 0; i_line < i_font_height; i_line ++ )
510 for( i_byte = 0; i_byte < i_font_bytes_per_line; i_byte++, p_char++, p_border++)
513 p_PutByte( p_pic + i_bytes_per_line * i_line, i_byte,
514 *p_char & i_char_mask, *p_border & i_border_mask, i_bg_mask,
515 i_char_color, i_border_color, i_bg_color );
518 /* Italic text: shift picture start left */
519 if( (i_style & ITALIC_TEXT) && !(i_line % 3) )
521 p_pic -= i_bytes_per_pixel;
525 /* Jump to next character */
526 p_pic += i_interspacing;
530 intf_ErrMsg("error: unknown font type %d", p_font->i_type );
539 /* following functions are local */
541 /*****************************************************************************
542 * PutByte8: print a fixed width font character byte in 1 Bpp
543 *****************************************************************************/
544 static void PutByte8( u8 *p_pic, int i_byte, int i_char, int i_border,
545 int i_bg, u32 i_char_color, u32 i_border_color,
548 /* Computes position offset and background mask */
550 i_bg &= ~(i_char | i_border);
552 /* Put character bits */
553 PUT_BYTE_MASK(i_char, i_char_color);
554 PUT_BYTE_MASK(i_border, i_border_color);
555 PUT_BYTE_MASK(i_bg, i_bg_color);
558 /*****************************************************************************
559 * PutByte16: print a fixed width font character byte in 2 Bpp
560 *****************************************************************************/
561 static void PutByte16( u16 *p_pic, int i_byte, int i_char, int i_border,
562 int i_bg, u32 i_char_color, u32 i_border_color,
565 /* Computes position offset and background mask */
567 i_bg &= ~(i_char | i_border);
569 /* Put character bits */
570 PUT_BYTE_MASK(i_char, i_char_color);
571 PUT_BYTE_MASK(i_border, i_border_color);
572 PUT_BYTE_MASK(i_bg, i_bg_color);
575 /*****************************************************************************
576 * PutByte24: print a fixed width font character byte in 3 Bpp
577 *****************************************************************************/
578 static void PutByte24( void *p_pic, int i_byte, byte_t i_char, byte_t i_border, byte_t i_bg,
579 u32 i_char_color, u32 i_border_color, u32 i_bg_color )
584 /*****************************************************************************
585 * PutByte32: print a fixed width font character byte in 4 Bpp
586 *****************************************************************************/
587 static void PutByte32( u32 *p_pic, int i_byte, byte_t i_char, byte_t i_border, byte_t i_bg,
588 u32 i_char_color, u32 i_border_color, u32 i_bg_color )
590 /* Computes position offset and background mask */
592 i_bg &= ~(i_char | i_border);
594 /* Put character bits */
595 PUT_BYTE_MASK(i_char, i_char_color);
596 PUT_BYTE_MASK(i_border, i_border_color);
597 PUT_BYTE_MASK(i_bg, i_bg_color);