1 /*****************************************************************************
2 * video_text.c : text manipulation functions
3 *****************************************************************************
4 * Copyright (C) 1999-2001 VideoLAN
5 * $Id: video_text.c,v 1.35 2002/06/01 12:32:02 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() */
37 # include <unistd.h> /* read(), close() */
38 #elif defined( _MSC_VER ) && defined( _WIN32 )
47 #include "video_output.h"
48 #include "video_text.h"
50 /*****************************************************************************
51 * vout_font_t: bitmap font
52 *****************************************************************************
53 * This structure is used when the system doesn't provide a convenient function
54 * to print simple characters in a buffer.
55 * VOUT_FIXED_FONTs are stored in raw mode, character after character, with a
56 * first array of characters followed by a second array of borders masks.
57 * Therefore the border masks can't be complete if the font has pixels on the
59 *****************************************************************************/
62 int i_type; /* font type */
63 int i_width; /* character width in pixels */
64 int i_height; /* character height in pixels */
65 int i_interspacing; /* characters interspacing in pixels */
66 int i_bytes_per_line; /* bytes per character line */
67 int i_bytes_per_char; /* bytes per character */
68 u16 i_first; /* first character */
69 u16 i_last; /* last character */
70 byte_t * p_data; /* font character data */
74 #define VOUT_FIXED_FONT 0 /* simple fixed font */
76 /*****************************************************************************
77 * vout_put_byte_t: PutByte function
78 *****************************************************************************
79 * These functions will transform masks in a set of pixels. For each pixel,
80 * character, then border and background masks are tested, and the first
81 * encountered color is set.
82 *****************************************************************************/
83 typedef void (vout_put_byte_t)( void *p_pic, int i_byte, int i_char, int i_border,
84 int i_bg, u32 i_char_color, u32 i_border_color, u32 i_bg_color );
87 /*****************************************************************************
89 *****************************************************************************/
91 /* PUT_BYTE_MASK: put pixels from a byte-wide mask. It uses a branching tree
92 * to optimize the number of tests. It is used in the PutByte functions.
93 * This macro works for 1, 2 and 4 Bpp. */
94 #define PUT_BYTE_MASK( i_mask, i_mask_color ) \
95 if( i_mask & 0xf0 ) /* one from 1111 */ \
97 if( i_mask & 0xc0 ) /* one from 1100 */ \
99 if( i_mask & 0x80 ) /* 1000 */ \
101 p_pic[0] = i_mask_color; \
102 if( i_mask & 0x40 ) /* 0100 */ \
104 p_pic[1] = i_mask_color; \
107 else /* not 1000 means 0100 */ \
109 p_pic[1] = i_mask_color; \
111 if( i_mask & 0x30 ) /* one from 0011 */ \
113 if( i_mask & 0x20 ) /* 0010 */ \
115 p_pic[2] = i_mask_color; \
116 if( i_mask & 0x10 ) /* 0001 */ \
118 p_pic[3] = i_mask_color; \
121 else /* not 0010 means 0001 */ \
123 p_pic[3] = i_mask_color; \
127 else /* not 1100 means 0011 */ \
129 if( i_mask & 0x20 ) /* 0010 */ \
131 p_pic[2] = i_mask_color; \
132 if( i_mask & 0x10 ) /* 0001 */ \
134 p_pic[3] = i_mask_color; \
137 else /* not 0010 means 0001 */ \
139 p_pic[3] = i_mask_color; \
143 if( i_mask & 0x0f ) \
145 if( i_mask & 0x0c ) /* one from 1100 */ \
147 if( i_mask & 0x08 ) /* 1000 */ \
149 p_pic[4] = i_mask_color; \
150 if( i_mask & 0x04 ) /* 0100 */ \
152 p_pic[5] = i_mask_color; \
155 else /* not 1000 means 0100 */ \
157 p_pic[5] = i_mask_color; \
159 if( i_mask & 0x03 ) /* one from 0011 */ \
161 if( i_mask & 0x02 ) /* 0010 */ \
163 p_pic[6] = i_mask_color; \
164 if( i_mask & 0x01 ) /* 0001 */ \
166 p_pic[7] = i_mask_color; \
169 else /* not 0010 means 0001 */ \
171 p_pic[7] = i_mask_color; \
175 else /* not 1100 means 0011 */ \
177 if( i_mask & 0x02 ) /* 0010 */ \
179 p_pic[6] = i_mask_color; \
180 if( i_mask & 0x01 ) /* 0001 */ \
182 p_pic[7] = i_mask_color; \
185 else /* not 0010 means 0001 */ \
187 p_pic[7] = i_mask_color; \
192 /*****************************************************************************
194 *****************************************************************************/
195 static void PutByte8 ( u8 *p_pic, int i_byte, int i_char, int i_border,
196 int i_bg, u32 i_char_color, u32 i_border_color,
198 static void PutByte16( u16 *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 PutByte24( void *p_pic, int i_byte, byte_t i_char, byte_t i_border,
202 byte_t i_bg, u32 i_char_color, u32 i_border_color,
204 static void PutByte32( u32 *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,
208 /*****************************************************************************
209 * vout_LoadFont: load a bitmap font from a file
210 *****************************************************************************
211 * This function will try to open a .psf font and load it. It will return
213 *****************************************************************************/
214 vout_font_t *vout_LoadFont( vout_thread_t *p_vout, const char *psz_name )
216 static char * path[] = { "share", DATA_PATH, NULL, NULL };
218 char ** ppsz_path = path;
220 #if defined( SYS_BEOS ) || defined( SYS_DARWIN )
221 char * psz_vlcpath = system_GetProgramPath();
222 int i_vlclen = strlen( psz_vlcpath );
224 int i_char, i_line; /* character and line indexes */
225 int i_file = -1; /* source file */
226 byte_t pi_buffer[2]; /* file buffer */
227 vout_font_t * p_font; /* the font itself */
229 for( ; *ppsz_path != NULL ; ppsz_path++ )
231 #if defined( SYS_BEOS ) || defined( SYS_DARWIN )
232 /* Under BeOS, we need to add beos_GetProgramPath() to access
233 * files under the current directory */
234 if( strncmp( *ppsz_path, "/", 1 ) )
236 psz_file = malloc( strlen( psz_name ) + strlen( *ppsz_path )
238 if( psz_file == NULL )
242 sprintf( psz_file, "%s/%s/%s", psz_vlcpath, *ppsz_path, psz_name );
247 psz_file = malloc( strlen( psz_name ) + strlen( *ppsz_path ) + 2 );
248 if( psz_file == NULL )
252 sprintf( psz_file, "%s/%s", *ppsz_path, psz_name );
256 i_file = open( psz_file, O_RDONLY );
267 msg_Err( p_vout, "cannot open '%s' (%s)", psz_name, strerror(errno) );
271 /* Read magic number */
272 if( read( i_file, pi_buffer, 2 ) != 2 )
274 msg_Err( p_vout, "unexpected end of file in '%s'", psz_name );
279 /* Allocate font descriptor */
280 p_font = malloc( sizeof( vout_font_t ) );
283 msg_Err( p_vout, "out of memory" );
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 msg_Err( p_vout, "unexpected end of file in '%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 msg_Err( p_vout, "out of memory" );
327 if( read( i_file, p_font->p_data, 256 * pi_buffer[1] ) != 256 * pi_buffer[1] )
329 msg_Err( p_vout, "unexpected end of file in '%s'", psz_name );
330 free( p_font->p_data );
336 /* Compute border masks - remember that masks have the same matrix as
337 * characters, so an empty character border is required to have a
338 * complete border mask. */
339 for( i_char = 0; i_char <= 255; i_char++ )
341 for( i_line = 0; i_line < pi_buffer[1]; i_line++ )
344 p_font->p_data[ (i_char + 256) * pi_buffer[1] + i_line ] =
345 ((p_font->p_data[ i_char * pi_buffer[1] + i_line ] << 1) |
346 (p_font->p_data[ i_char * pi_buffer[1] + i_line ] >> 1) |
347 (i_line > 0 ? p_font->p_data[ i_char * pi_buffer[1] + i_line - 1]: 0) |
348 (i_line < pi_buffer[1] - 1 ? p_font->p_data[ i_char * pi_buffer[1] + i_line + 1]: 0))
349 & ~p_font->p_data[ i_char * pi_buffer[1] + i_line ];
355 msg_Err( p_vout, "file '%s' has an unknown format", psz_name );
362 msg_Err( p_vout, "loaded %s, type %d, %d-%dx%d", psz_name, p_font->i_type,
363 p_font->i_width, p_font->i_interspacing, p_font->i_height );
368 /*****************************************************************************
369 * vout_UnloadFont: unload a font
370 *****************************************************************************
371 * This function free the resources allocated by vout_LoadFont
372 *****************************************************************************/
373 void vout_UnloadFont( vout_font_t *p_font )
375 /* If no font was loaded, do nothing */
381 free( p_font->p_data );
385 /*****************************************************************************
386 * vout_TextSize: return the dimensions of a text
387 *****************************************************************************
388 * This function is used to align text. It returns the width and height of a
390 *****************************************************************************/
391 void vout_TextSize( vout_font_t *p_font, int i_style, const char *psz_text, int *pi_width, int *pi_height )
393 /* If no font was loaded, do nothing */
396 *pi_width = *pi_height = 0;
400 switch( p_font->i_type )
402 case VOUT_FIXED_FONT:
403 *pi_width = ((i_style & WIDE_TEXT) ? p_font->i_interspacing * 2 : p_font->i_interspacing) *
404 (strlen( psz_text ) - 1) + p_font->i_width;
405 *pi_height = p_font->i_height;
406 if( i_style & ITALIC_TEXT )
408 *pi_width = *pi_height / 3;
414 /*****************************************************************************
415 * vout_Print: low level printing function
416 *****************************************************************************
417 * This function prints a text, without clipping, in a buffer using a
418 * previously loaded bitmap font.
419 *****************************************************************************/
420 void vout_Print( vout_font_t *p_font, byte_t *p_pic, int i_bytes_per_pixel, int i_bytes_per_line,
421 u32 i_char_color, u32 i_border_color, u32 i_bg_color, int i_style, const char *psz_text, int i_percent)
423 byte_t *p_char, *p_border; /* character and border mask data */
424 int i_char_mask, i_border_mask, i_bg_mask; /* masks */
425 int i_line; /* current line in character */
426 int i_byte; /* current byte in character */
427 int i_interspacing; /* offset between two chars */
428 int i_font_bytes_per_line, i_font_height; /* font properties */
429 int i_position, i_end; /* current position */
430 vout_put_byte_t *p_PutByte; /* PutByte function */
432 /* If no font was loaded, do nothing */
438 /* FIXME: background: can be something else that whole byte ?? */
440 /* Select output function */
441 switch( i_bytes_per_pixel )
444 p_PutByte = (vout_put_byte_t *) PutByte8;
447 p_PutByte = (vout_put_byte_t *) PutByte16;
450 p_PutByte = (vout_put_byte_t *) PutByte24;
454 p_PutByte = (vout_put_byte_t *) PutByte32;
458 /* Choose masks and copy font data to local variables */
459 i_char_mask = (i_style & VOID_TEXT) ? 0 : 0xff;
460 i_border_mask = (i_style & OUTLINED_TEXT) ? 0xff : 0;
461 i_bg_mask = (i_style & OPAQUE_TEXT) ? 0xff : 0;
463 i_font_bytes_per_line = p_font->i_bytes_per_line;
464 i_font_height = p_font->i_height;
465 i_interspacing = i_bytes_per_pixel * ((i_style & WIDE_TEXT) ?
466 p_font->i_interspacing * 2 :
467 p_font->i_interspacing);
469 /* compute where to stop... */
470 i_end = (int) (i_percent * strlen(psz_text) / I64C(100));
471 if(i_end > strlen(psz_text))
472 i_end = strlen(psz_text);
476 for( i_position = 0; i_position < i_end; i_position++ ,psz_text++ )
478 /* Check that the character is valid */
479 if( (*psz_text >= p_font->i_first) && (*psz_text <= p_font->i_last) )
481 /* Select character - bytes per char is always valid, event for
483 p_char = p_font->p_data + (*psz_text - p_font->i_first) * p_font->i_bytes_per_char;
484 p_border = p_char + (p_font->i_last - p_font->i_first + 1) * p_font->i_bytes_per_char;
486 /* Select base address for output */
487 switch( p_font->i_type )
489 case VOUT_FIXED_FONT:
491 * Simple fixed width font
494 /* Italic text: shift picture start right */
495 if( i_style & ITALIC_TEXT )
497 p_pic += i_bytes_per_pixel * (p_font->i_height / 3);
500 /* Print character */
501 for( i_line = 0; i_line < i_font_height; i_line ++ )
503 for( i_byte = 0; i_byte < i_font_bytes_per_line; i_byte++, p_char++, p_border++)
506 p_PutByte( p_pic + i_bytes_per_line * i_line, i_byte,
507 *p_char & i_char_mask, *p_border & i_border_mask, i_bg_mask,
508 i_char_color, i_border_color, i_bg_color );
511 /* Italic text: shift picture start left */
512 if( (i_style & ITALIC_TEXT) && !(i_line % 3) )
514 p_pic -= i_bytes_per_pixel;
518 /* Jump to next character */
519 p_pic += i_interspacing;
526 /* following functions are local */
528 /*****************************************************************************
529 * PutByte8: print a fixed width font character byte in 1 Bpp
530 *****************************************************************************/
531 static void PutByte8( u8 *p_pic, int i_byte, int i_char, int i_border,
532 int i_bg, u32 i_char_color, u32 i_border_color,
535 /* Computes position offset and background mask */
537 i_bg &= ~(i_char | i_border);
539 /* Put character bits */
540 PUT_BYTE_MASK(i_char, i_char_color);
541 PUT_BYTE_MASK(i_border, i_border_color);
542 PUT_BYTE_MASK(i_bg, i_bg_color);
545 /*****************************************************************************
546 * PutByte16: print a fixed width font character byte in 2 Bpp
547 *****************************************************************************/
548 static void PutByte16( u16 *p_pic, int i_byte, int i_char, int i_border,
549 int i_bg, u32 i_char_color, u32 i_border_color,
552 /* Computes position offset and background mask */
554 i_bg &= ~(i_char | i_border);
556 /* Put character bits */
557 PUT_BYTE_MASK(i_char, i_char_color);
558 PUT_BYTE_MASK(i_border, i_border_color);
559 PUT_BYTE_MASK(i_bg, i_bg_color);
562 /*****************************************************************************
563 * PutByte24: print a fixed width font character byte in 3 Bpp
564 *****************************************************************************/
565 static void PutByte24( void *p_pic, int i_byte, byte_t i_char, byte_t i_border, byte_t i_bg,
566 u32 i_char_color, u32 i_border_color, u32 i_bg_color )
571 /*****************************************************************************
572 * PutByte32: print a fixed width font character byte in 4 Bpp
573 *****************************************************************************/
574 static void PutByte32( u32 *p_pic, int i_byte, byte_t i_char, byte_t i_border, byte_t i_bg,
575 u32 i_char_color, u32 i_border_color, u32 i_bg_color )
577 /* Computes position offset and background mask */
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);