* Applied patch from Jon Lech Johansen <jon-vl@nanocrew.net> to compile
[vlc.git] / src / video_output / video_text.c
blob0a7a588f58d2309bdd9145019f404cbc77083c26
1 /*****************************************************************************
2 * video_text.c : text manipulation functions
3 *****************************************************************************
4 * Copyright (C) 1999, 2000 VideoLAN
5 * $Id: video_text.c,v 1.27 2001/05/31 01:37:08 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 /*****************************************************************************
26 * Preamble
27 *****************************************************************************/
28 #include "defs.h"
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() */
36 #ifdef HAVE_UNISTD_H
37 #include <unistd.h> /* read(), close() */
38 #elif defined( _MSC_VER ) && defined( _WIN32 )
39 #include <io.h>
40 #endif
42 #ifdef SYS_BEOS
43 # include "beos_specific.h"
44 #endif
46 #ifdef SYS_DARWIN1_3
47 # include "darwin_specific.h"
48 #endif
50 #include "config.h"
51 #include "common.h"
52 #include "video_text.h"
54 #include "intf_msg.h"
56 /*****************************************************************************
57 * vout_font_t: bitmap font
58 *****************************************************************************
59 * This structure is used when the system doesn't provide a convenient function
60 * to print simple characters in a buffer.
61 * VOUT_FIXED_FONTs are stored in raw mode, character after character, with a
62 * first array of characters followed by a second array of borders masks.
63 * Therefore the border masks can't be complete if the font has pixels on the
64 * border.
65 *****************************************************************************/
66 typedef struct vout_font_s
68 int i_type; /* font type */
69 int i_width; /* character width in pixels */
70 int i_height; /* character height in pixels */
71 int i_interspacing; /* characters interspacing in pixels */
72 int i_bytes_per_line; /* bytes per character line */
73 int i_bytes_per_char; /* bytes per character */
74 u16 i_first; /* first character */
75 u16 i_last; /* last character */
76 byte_t * p_data; /* font character data */
77 } vout_font_t;
79 /* Font types */
80 #define VOUT_FIXED_FONT 0 /* simple fixed font */
82 /*****************************************************************************
83 * vout_put_byte_t: PutByte function
84 *****************************************************************************
85 * These functions will transform masks in a set of pixels. For each pixel,
86 * character, then border and background masks are tested, and the first
87 * encountered color is set.
88 *****************************************************************************/
89 typedef void (vout_put_byte_t)( void *p_pic, int i_byte, int i_char, int i_border,
90 int i_bg, u32 i_char_color, u32 i_border_color, u32 i_bg_color );
93 /*****************************************************************************
94 * Macros
95 *****************************************************************************/
97 /* PUT_BYTE_MASK: put pixels from a byte-wide mask. It uses a branching tree
98 * to optimize the number of tests. It is used in the PutByte functions.
99 * This macro works for 1, 2 and 4 Bpp. */
100 #define PUT_BYTE_MASK( i_mask, i_mask_color ) \
101 if( i_mask & 0xf0 ) /* one from 1111 */ \
103 if( i_mask & 0xc0 ) /* one from 1100 */ \
105 if( i_mask & 0x80 ) /* 1000 */ \
107 p_pic[0] = i_mask_color; \
108 if( i_mask & 0x40 ) /* 0100 */ \
110 p_pic[1] = i_mask_color; \
113 else /* not 1000 means 0100 */ \
115 p_pic[1] = i_mask_color; \
117 if( i_mask & 0x30 ) /* one from 0011 */ \
119 if( i_mask & 0x20 ) /* 0010 */ \
121 p_pic[2] = i_mask_color; \
122 if( i_mask & 0x10 ) /* 0001 */ \
124 p_pic[3] = i_mask_color; \
127 else /* not 0010 means 0001 */ \
129 p_pic[3] = i_mask_color; \
133 else /* not 1100 means 0011 */ \
135 if( i_mask & 0x20 ) /* 0010 */ \
137 p_pic[2] = i_mask_color; \
138 if( i_mask & 0x10 ) /* 0001 */ \
140 p_pic[3] = i_mask_color; \
143 else /* not 0010 means 0001 */ \
145 p_pic[3] = i_mask_color; \
149 if( i_mask & 0x0f ) \
151 if( i_mask & 0x0c ) /* one from 1100 */ \
153 if( i_mask & 0x08 ) /* 1000 */ \
155 p_pic[4] = i_mask_color; \
156 if( i_mask & 0x04 ) /* 0100 */ \
158 p_pic[5] = i_mask_color; \
161 else /* not 1000 means 0100 */ \
163 p_pic[5] = i_mask_color; \
165 if( i_mask & 0x03 ) /* one from 0011 */ \
167 if( i_mask & 0x02 ) /* 0010 */ \
169 p_pic[6] = i_mask_color; \
170 if( i_mask & 0x01 ) /* 0001 */ \
172 p_pic[7] = i_mask_color; \
175 else /* not 0010 means 0001 */ \
177 p_pic[7] = i_mask_color; \
181 else /* not 1100 means 0011 */ \
183 if( i_mask & 0x02 ) /* 0010 */ \
185 p_pic[6] = i_mask_color; \
186 if( i_mask & 0x01 ) /* 0001 */ \
188 p_pic[7] = i_mask_color; \
191 else /* not 0010 means 0001 */ \
193 p_pic[7] = i_mask_color; \
198 /*****************************************************************************
199 * Local prototypes
200 *****************************************************************************/
201 static void PutByte8 ( u8 *p_pic, int i_byte, int i_char, int i_border,
202 int i_bg, u32 i_char_color, u32 i_border_color,
203 u32 i_bg_color );
204 static void PutByte16( u16 *p_pic, int i_byte, int i_char, int i_border,
205 int i_bg, u32 i_char_color, u32 i_border_color,
206 u32 i_bg_color );
207 static void PutByte24( void *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,
209 u32 i_bg_color );
210 static void PutByte32( u32 *p_pic, int i_byte, byte_t i_char, byte_t i_border,
211 byte_t i_bg, u32 i_char_color, u32 i_border_color,
212 u32 i_bg_color );
214 /*****************************************************************************
215 * vout_LoadFont: load a bitmap font from a file
216 *****************************************************************************
217 * This function will try to open a .psf font and load it. It will return
218 * NULL on error.
219 *****************************************************************************/
220 vout_font_t *vout_LoadFont( const char *psz_name )
222 static char * path[] = { "share", DATA_PATH, NULL, NULL };
224 char ** ppsz_path = path;
225 char * psz_file;
226 #if defined( SYS_BEOS ) || defined( SYS_DARWIN1_3 )
227 char * psz_vlcpath = system_GetProgramPath();
228 int i_vlclen = strlen( psz_vlcpath );
229 #endif
230 int i_char, i_line; /* character and line indexes */
231 int i_file = -1; /* source file */
232 byte_t pi_buffer[2]; /* file buffer */
233 vout_font_t * p_font; /* the font itself */
235 for( ; *ppsz_path != NULL ; ppsz_path++ )
237 #if defined( SYS_BEOS ) || defined( SYS_DARWIN1_3 )
238 /* Under BeOS, we need to add beos_GetProgramPath() to access
239 * files under the current directory */
240 if( strncmp( *ppsz_path, "/", 1 ) )
242 psz_file = malloc( strlen( psz_name ) + strlen( *ppsz_path )
243 + i_vlclen + 3 );
244 if( psz_file == NULL )
246 continue;
248 sprintf( psz_file, "%s/%s/%s", psz_vlcpath, *ppsz_path, psz_name );
250 else
251 #endif
253 psz_file = malloc( strlen( psz_name ) + strlen( *ppsz_path ) + 2 );
254 if( psz_file == NULL )
256 continue;
258 sprintf( psz_file, "%s/%s", *ppsz_path, psz_name );
261 /* Open file */
262 #ifndef WIN32
263 i_file = open( psz_file, O_RDONLY );
264 #else
265 i_file = open( psz_file, O_RDONLY | O_BINARY );
266 #endif
267 free( psz_file );
269 if( i_file != -1 )
271 break;
275 if( i_file == -1 )
277 intf_DbgMsg( "vout error: can't open file '%s' (%s)",
278 psz_name, strerror(errno) );
279 return( NULL );
282 /* Read magick number */
283 if( read( i_file, pi_buffer, 2 ) != 2 )
285 intf_ErrMsg("vout error: unexpected end of file '%s'", psz_name );
286 close( i_file );
287 return( NULL );
290 /* Allocate font descriptor */
291 p_font = malloc( sizeof( vout_font_t ) );
292 if( p_font == NULL )
294 intf_ErrMsg( "vout error: cannot allocate vout_font_t (%s)",
295 strerror(ENOMEM) );
296 close( i_file );
297 return( NULL );
300 /* Read file */
301 switch( ((u16)pi_buffer[0] << 8) | pi_buffer[1] )
303 case 0x3604: /* .psf file */
305 * PSF font: simple fixed font. Only the first 256 characters are read.
306 * Those fonts are always 1 byte wide, and 256 or 512 characters long.
309 /* Read font header - two bytes indicate the font properties */
310 if( read( i_file, pi_buffer, 2 ) != 2)
312 intf_ErrMsg( "vout error: unexpected end of file '%s'", psz_name );
313 free( p_font );
314 close( i_file );
315 return( NULL );
318 /* Copy font properties */
319 p_font->i_type = VOUT_FIXED_FONT;
320 p_font->i_width = 8;
321 p_font->i_height = pi_buffer[1];
322 p_font->i_interspacing = 8;
323 p_font->i_bytes_per_line = 1;
324 p_font->i_bytes_per_char = pi_buffer[1];
325 p_font->i_first = 0;
326 p_font->i_last = 255;
328 /* Allocate font space */
329 p_font->p_data = malloc( 2 * 256 * pi_buffer[1] );
330 if( p_font->p_data == NULL )
332 intf_ErrMsg( "vout error: cannot allocate font space (%s)",
333 strerror(ENOMEM) );
334 free( p_font );
335 close( i_file );
336 return( NULL );
339 /* Copy raw data */
340 if( read( i_file, p_font->p_data, 256 * pi_buffer[1] ) != 256 * pi_buffer[1] )
342 intf_ErrMsg("vout error: unexpected end of file '%s'", psz_name );
343 free( p_font->p_data );
344 free( p_font );
345 close( i_file );
346 return( NULL );
349 /* Computes border masks - remember that masks have the same matrix as
350 * characters, so an empty character border is required to have a complete
351 * border mask. */
352 for( i_char = 0; i_char <= 255; i_char++ )
354 for( i_line = 0; i_line < pi_buffer[1]; i_line++ )
357 p_font->p_data[ (i_char + 256) * pi_buffer[1] + i_line ] =
358 ((p_font->p_data[ i_char * pi_buffer[1] + i_line ] << 1) |
359 (p_font->p_data[ i_char * pi_buffer[1] + i_line ] >> 1) |
360 (i_line > 0 ? p_font->p_data[ i_char * pi_buffer[1] + i_line - 1]: 0) |
361 (i_line < pi_buffer[1] - 1 ? p_font->p_data[ i_char * pi_buffer[1] + i_line + 1]: 0))
362 & ~p_font->p_data[ i_char * pi_buffer[1] + i_line ];
366 break;
367 default:
368 intf_ErrMsg("vout error: file '%s' has an unknown format", psz_name );
369 free( p_font );
370 close( i_file );
371 return( NULL );
372 break;
376 intf_DbgMsg( "loaded %s: type %d, %d-%dx%d", psz_name, p_font->i_type,
377 p_font->i_width, p_font->i_interspacing, p_font->i_height );
378 return( p_font );
381 /*****************************************************************************
382 * vout_UnloadFont: unload a font
383 *****************************************************************************
384 * This function free the resources allocated by vout_LoadFont
385 *****************************************************************************/
386 void vout_UnloadFont( vout_font_t *p_font )
388 /* If no font was loaded, do nothing */
389 if( p_font == NULL )
391 return;
394 intf_DbgMsg( "vout: unloading font %p", p_font );
395 free( p_font->p_data );
396 free( p_font );
399 /*****************************************************************************
400 * vout_TextSize: return the dimensions of a text
401 *****************************************************************************
402 * This function is used to align text. It returns the width and height of a
403 * given text.
404 *****************************************************************************/
405 void vout_TextSize( vout_font_t *p_font, int i_style, const char *psz_text, int *pi_width, int *pi_height )
407 /* If no font was loaded, do nothing */
408 if( p_font == NULL )
410 *pi_width = *pi_height = 0;
411 return;
414 switch( p_font->i_type )
416 case VOUT_FIXED_FONT:
417 *pi_width = ((i_style & WIDE_TEXT) ? p_font->i_interspacing * 2 : p_font->i_interspacing) *
418 (strlen( psz_text ) - 1) + p_font->i_width;
419 *pi_height = p_font->i_height;
420 if( i_style & ITALIC_TEXT )
422 *pi_width = *pi_height / 3;
424 break;
425 #ifdef DEBUG
426 default:
427 intf_ErrMsg("error: unknown font type %d", p_font->i_type );
428 break;
429 #endif
433 /*****************************************************************************
434 * vout_Print: low level printing function
435 *****************************************************************************
436 * This function prints a text, without clipping, in a buffer using a
437 * previously loaded bitmap font.
438 *****************************************************************************/
439 void vout_Print( vout_font_t *p_font, byte_t *p_pic, int i_bytes_per_pixel, int i_bytes_per_line,
440 u32 i_char_color, u32 i_border_color, u32 i_bg_color, int i_style, const char *psz_text, int i_percent)
442 byte_t *p_char, *p_border; /* character and border mask data */
443 int i_char_mask, i_border_mask, i_bg_mask; /* masks */
444 int i_line; /* current line in character */
445 int i_byte; /* current byte in character */
446 int i_interspacing; /* offset between two chars */
447 int i_font_bytes_per_line, i_font_height; /* font properties */
448 int i_position, i_end; /* current position */
449 vout_put_byte_t *p_PutByte; /* PutByte function */
451 /* If no font was loaded, do nothing */
452 if( p_font == NULL )
454 return;
457 /* FIXME: background: can be something else that whole byte ?? */
459 /* Select output function */
460 switch( i_bytes_per_pixel )
462 case 1:
463 p_PutByte = (vout_put_byte_t *) PutByte8;
464 break;
465 case 2:
466 p_PutByte = (vout_put_byte_t *) PutByte16;
467 break;
468 case 3:
469 p_PutByte = (vout_put_byte_t *) PutByte24;
470 break;
471 case 4:
472 default:
473 p_PutByte = (vout_put_byte_t *) PutByte32;
474 break;
477 /* Choose masks and copy font data to local variables */
478 i_char_mask = (i_style & VOID_TEXT) ? 0 : 0xff;
479 i_border_mask = (i_style & OUTLINED_TEXT) ? 0xff : 0;
480 i_bg_mask = (i_style & OPAQUE_TEXT) ? 0xff : 0;
482 i_font_bytes_per_line = p_font->i_bytes_per_line;
483 i_font_height = p_font->i_height;
484 i_interspacing = i_bytes_per_pixel * ((i_style & WIDE_TEXT) ?
485 p_font->i_interspacing * 2 :
486 p_font->i_interspacing);
488 /* compute where to stop... */
489 i_end = (int) (i_percent * strlen(psz_text) / I64C(100));
490 if(i_end > strlen(psz_text))
491 i_end = strlen(psz_text);
494 /* Print text */
495 for( i_position = 0; i_position < i_end; i_position++ ,psz_text++ )
497 /* Check that the character is valid */
498 if( (*psz_text >= p_font->i_first) && (*psz_text <= p_font->i_last) )
500 /* Select character - bytes per char is always valid, event for
501 * non fixed fonts */
502 p_char = p_font->p_data + (*psz_text - p_font->i_first) * p_font->i_bytes_per_char;
503 p_border = p_char + (p_font->i_last - p_font->i_first + 1) * p_font->i_bytes_per_char;
505 /* Select base address for output */
506 switch( p_font->i_type )
508 case VOUT_FIXED_FONT:
510 * Simple fixed width font
513 /* Italic text: shift picture start right */
514 if( i_style & ITALIC_TEXT )
516 p_pic += i_bytes_per_pixel * (p_font->i_height / 3);
519 /* Print character */
520 for( i_line = 0; i_line < i_font_height; i_line ++ )
522 for( i_byte = 0; i_byte < i_font_bytes_per_line; i_byte++, p_char++, p_border++)
524 /* Put pixels */
525 p_PutByte( p_pic + i_bytes_per_line * i_line, i_byte,
526 *p_char & i_char_mask, *p_border & i_border_mask, i_bg_mask,
527 i_char_color, i_border_color, i_bg_color );
530 /* Italic text: shift picture start left */
531 if( (i_style & ITALIC_TEXT) && !(i_line % 3) )
533 p_pic -= i_bytes_per_pixel;
537 /* Jump to next character */
538 p_pic += i_interspacing;
539 break;
540 #ifdef DEBUG
541 default:
542 intf_ErrMsg("error: unknown font type %d", p_font->i_type );
543 break;
544 #endif
551 /* following functions are local */
553 /*****************************************************************************
554 * PutByte8: print a fixed width font character byte in 1 Bpp
555 *****************************************************************************/
556 static void PutByte8( u8 *p_pic, int i_byte, int i_char, int i_border,
557 int i_bg, u32 i_char_color, u32 i_border_color,
558 u32 i_bg_color )
560 /* Computes position offset and background mask */
561 p_pic += 8 * i_byte;
562 i_bg &= ~(i_char | i_border);
564 /* Put character bits */
565 PUT_BYTE_MASK(i_char, i_char_color);
566 PUT_BYTE_MASK(i_border, i_border_color);
567 PUT_BYTE_MASK(i_bg, i_bg_color);
570 /*****************************************************************************
571 * PutByte16: print a fixed width font character byte in 2 Bpp
572 *****************************************************************************/
573 static void PutByte16( u16 *p_pic, int i_byte, int i_char, int i_border,
574 int i_bg, u32 i_char_color, u32 i_border_color,
575 u32 i_bg_color )
577 /* Computes position offset and background mask */
578 p_pic += 8 * i_byte;
579 i_bg &= ~(i_char | i_border);
581 /* Put character bits */
582 PUT_BYTE_MASK(i_char, i_char_color);
583 PUT_BYTE_MASK(i_border, i_border_color);
584 PUT_BYTE_MASK(i_bg, i_bg_color);
587 /*****************************************************************************
588 * PutByte24: print a fixed width font character byte in 3 Bpp
589 *****************************************************************************/
590 static void PutByte24( void *p_pic, int i_byte, byte_t i_char, byte_t i_border, byte_t i_bg,
591 u32 i_char_color, u32 i_border_color, u32 i_bg_color )
593 /* XXX?? */
596 /*****************************************************************************
597 * PutByte32: print a fixed width font character byte in 4 Bpp
598 *****************************************************************************/
599 static void PutByte32( u32 *p_pic, int i_byte, byte_t i_char, byte_t i_border, byte_t i_bg,
600 u32 i_char_color, u32 i_border_color, u32 i_bg_color )
602 /* Computes position offset and background mask */
603 p_pic += 8 * i_byte;
604 i_bg &= ~(i_char | i_border);
606 /* Put character bits */
607 PUT_BYTE_MASK(i_char, i_char_color);
608 PUT_BYTE_MASK(i_border, i_border_color);
609 PUT_BYTE_MASK(i_bg, i_bg_color);