1 /*****************************************************************************
2 * video_text.c : text manipulation functions
3 *****************************************************************************
4 * Copyright (C) 1999-2001 VideoLAN
5 * $Id: video_text.c,v 1.32 2001/12/09 17:01:37 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 *****************************************************************************/
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() */
37 # include <unistd.h> /* read(), close() */
38 #elif defined( _MSC_VER ) && defined( _WIN32 )
43 # include "beos_specific.h"
47 # include "darwin_specific.h"
55 #include "video_text.h"
59 /*****************************************************************************
60 * vout_font_t: bitmap font
61 *****************************************************************************
62 * This structure is used when the system doesn't provide a convenient function
63 * to print simple characters in a buffer.
64 * VOUT_FIXED_FONTs are stored in raw mode, character after character, with a
65 * first array of characters followed by a second array of borders masks.
66 * Therefore the border masks can't be complete if the font has pixels on the
68 *****************************************************************************/
69 typedef struct vout_font_s
71 int i_type; /* font type */
72 int i_width; /* character width in pixels */
73 int i_height; /* character height in pixels */
74 int i_interspacing; /* characters interspacing in pixels */
75 int i_bytes_per_line; /* bytes per character line */
76 int i_bytes_per_char; /* bytes per character */
77 u16 i_first; /* first character */
78 u16 i_last; /* last character */
79 byte_t * p_data; /* font character data */
83 #define VOUT_FIXED_FONT 0 /* simple fixed font */
85 /*****************************************************************************
86 * vout_put_byte_t: PutByte function
87 *****************************************************************************
88 * These functions will transform masks in a set of pixels. For each pixel,
89 * character, then border and background masks are tested, and the first
90 * encountered color is set.
91 *****************************************************************************/
92 typedef void (vout_put_byte_t)( void *p_pic, int i_byte, int i_char, int i_border,
93 int i_bg, u32 i_char_color, u32 i_border_color, u32 i_bg_color );
96 /*****************************************************************************
98 *****************************************************************************/
100 /* PUT_BYTE_MASK: put pixels from a byte-wide mask. It uses a branching tree
101 * to optimize the number of tests. It is used in the PutByte functions.
102 * This macro works for 1, 2 and 4 Bpp. */
103 #define PUT_BYTE_MASK( i_mask, i_mask_color ) \
104 if( i_mask & 0xf0 ) /* one from 1111 */ \
106 if( i_mask & 0xc0 ) /* one from 1100 */ \
108 if( i_mask & 0x80 ) /* 1000 */ \
110 p_pic[0] = i_mask_color; \
111 if( i_mask & 0x40 ) /* 0100 */ \
113 p_pic[1] = i_mask_color; \
116 else /* not 1000 means 0100 */ \
118 p_pic[1] = i_mask_color; \
120 if( i_mask & 0x30 ) /* one from 0011 */ \
122 if( i_mask & 0x20 ) /* 0010 */ \
124 p_pic[2] = i_mask_color; \
125 if( i_mask & 0x10 ) /* 0001 */ \
127 p_pic[3] = i_mask_color; \
130 else /* not 0010 means 0001 */ \
132 p_pic[3] = i_mask_color; \
136 else /* not 1100 means 0011 */ \
138 if( i_mask & 0x20 ) /* 0010 */ \
140 p_pic[2] = i_mask_color; \
141 if( i_mask & 0x10 ) /* 0001 */ \
143 p_pic[3] = i_mask_color; \
146 else /* not 0010 means 0001 */ \
148 p_pic[3] = i_mask_color; \
152 if( i_mask & 0x0f ) \
154 if( i_mask & 0x0c ) /* one from 1100 */ \
156 if( i_mask & 0x08 ) /* 1000 */ \
158 p_pic[4] = i_mask_color; \
159 if( i_mask & 0x04 ) /* 0100 */ \
161 p_pic[5] = i_mask_color; \
164 else /* not 1000 means 0100 */ \
166 p_pic[5] = i_mask_color; \
168 if( i_mask & 0x03 ) /* one from 0011 */ \
170 if( i_mask & 0x02 ) /* 0010 */ \
172 p_pic[6] = i_mask_color; \
173 if( i_mask & 0x01 ) /* 0001 */ \
175 p_pic[7] = i_mask_color; \
178 else /* not 0010 means 0001 */ \
180 p_pic[7] = i_mask_color; \
184 else /* not 1100 means 0011 */ \
186 if( i_mask & 0x02 ) /* 0010 */ \
188 p_pic[6] = i_mask_color; \
189 if( i_mask & 0x01 ) /* 0001 */ \
191 p_pic[7] = i_mask_color; \
194 else /* not 0010 means 0001 */ \
196 p_pic[7] = i_mask_color; \
201 /*****************************************************************************
203 *****************************************************************************/
204 static void PutByte8 ( u8 *p_pic, int i_byte, int i_char, int i_border,
205 int i_bg, u32 i_char_color, u32 i_border_color,
207 static void PutByte16( u16 *p_pic, int i_byte, int i_char, int i_border,
208 int i_bg, u32 i_char_color, u32 i_border_color,
210 static void PutByte24( void *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,
213 static void PutByte32( u32 *p_pic, int i_byte, byte_t i_char, byte_t i_border,
214 byte_t i_bg, u32 i_char_color, u32 i_border_color,
217 /*****************************************************************************
218 * vout_LoadFont: load a bitmap font from a file
219 *****************************************************************************
220 * This function will try to open a .psf font and load it. It will return
222 *****************************************************************************/
223 vout_font_t *vout_LoadFont( const char *psz_name )
225 static char * path[] = { "share", DATA_PATH, NULL, NULL };
227 char ** ppsz_path = path;
229 #if defined( SYS_BEOS ) || defined( SYS_DARWIN )
230 char * psz_vlcpath = system_GetProgramPath();
231 int i_vlclen = strlen( psz_vlcpath );
233 int i_char, i_line; /* character and line indexes */
234 int i_file = -1; /* source file */
235 byte_t pi_buffer[2]; /* file buffer */
236 vout_font_t * p_font; /* the font itself */
238 for( ; *ppsz_path != NULL ; ppsz_path++ )
240 #if defined( SYS_BEOS ) || defined( SYS_DARWIN )
241 /* Under BeOS, we need to add beos_GetProgramPath() to access
242 * files under the current directory */
243 if( strncmp( *ppsz_path, "/", 1 ) )
245 psz_file = malloc( strlen( psz_name ) + strlen( *ppsz_path )
247 if( psz_file == NULL )
251 sprintf( psz_file, "%s/%s/%s", psz_vlcpath, *ppsz_path, psz_name );
256 psz_file = malloc( strlen( psz_name ) + strlen( *ppsz_path ) + 2 );
257 if( psz_file == NULL )
261 sprintf( psz_file, "%s/%s", *ppsz_path, psz_name );
265 i_file = open( psz_file, O_RDONLY );
276 intf_DbgMsg( "vout error: can't open file '%s' (%s)",
277 psz_name, strerror(errno) );
281 /* Read magick number */
282 if( read( i_file, pi_buffer, 2 ) != 2 )
284 intf_ErrMsg("vout error: unexpected end of file '%s'", psz_name );
289 /* Allocate font descriptor */
290 p_font = malloc( sizeof( vout_font_t ) );
293 intf_ErrMsg( "vout error: cannot allocate vout_font_t (%s)",
300 switch( ((u16)pi_buffer[0] << 8) | pi_buffer[1] )
302 case 0x3604: /* .psf file */
304 * PSF font: simple fixed font. Only the first 256 characters are read.
305 * Those fonts are always 1 byte wide, and 256 or 512 characters long.
308 /* Read font header - two bytes indicate the font properties */
309 if( read( i_file, pi_buffer, 2 ) != 2)
311 intf_ErrMsg( "vout error: unexpected end of file '%s'", psz_name );
317 /* Copy font properties */
318 p_font->i_type = VOUT_FIXED_FONT;
320 p_font->i_height = pi_buffer[1];
321 p_font->i_interspacing = 8;
322 p_font->i_bytes_per_line = 1;
323 p_font->i_bytes_per_char = pi_buffer[1];
325 p_font->i_last = 255;
327 /* Allocate font space */
328 p_font->p_data = malloc( 2 * 256 * pi_buffer[1] );
329 if( p_font->p_data == NULL )
331 intf_ErrMsg( "vout error: cannot allocate font space (%s)",
339 if( read( i_file, p_font->p_data, 256 * pi_buffer[1] ) != 256 * pi_buffer[1] )
341 intf_ErrMsg("vout error: unexpected end of file '%s'", psz_name );
342 free( p_font->p_data );
348 /* Computes border masks - remember that masks have the same matrix as
349 * characters, so an empty character border is required to have a complete
351 for( i_char = 0; i_char <= 255; i_char++ )
353 for( i_line = 0; i_line < pi_buffer[1]; i_line++ )
356 p_font->p_data[ (i_char + 256) * pi_buffer[1] + i_line ] =
357 ((p_font->p_data[ i_char * pi_buffer[1] + i_line ] << 1) |
358 (p_font->p_data[ i_char * pi_buffer[1] + i_line ] >> 1) |
359 (i_line > 0 ? p_font->p_data[ i_char * pi_buffer[1] + i_line - 1]: 0) |
360 (i_line < pi_buffer[1] - 1 ? p_font->p_data[ i_char * pi_buffer[1] + i_line + 1]: 0))
361 & ~p_font->p_data[ i_char * pi_buffer[1] + i_line ];
367 intf_ErrMsg("vout error: file '%s' has an unknown format", psz_name );
375 intf_DbgMsg( "loaded %s: type %d, %d-%dx%d", psz_name, p_font->i_type,
376 p_font->i_width, p_font->i_interspacing, p_font->i_height );
380 /*****************************************************************************
381 * vout_UnloadFont: unload a font
382 *****************************************************************************
383 * This function free the resources allocated by vout_LoadFont
384 *****************************************************************************/
385 void vout_UnloadFont( vout_font_t *p_font )
387 /* If no font was loaded, do nothing */
393 intf_DbgMsg( "vout: unloading font %p", p_font );
394 free( p_font->p_data );
398 /*****************************************************************************
399 * vout_TextSize: return the dimensions of a text
400 *****************************************************************************
401 * This function is used to align text. It returns the width and height of a
403 *****************************************************************************/
404 void vout_TextSize( vout_font_t *p_font, int i_style, const char *psz_text, int *pi_width, int *pi_height )
406 /* If no font was loaded, do nothing */
409 *pi_width = *pi_height = 0;
413 switch( p_font->i_type )
415 case VOUT_FIXED_FONT:
416 *pi_width = ((i_style & WIDE_TEXT) ? p_font->i_interspacing * 2 : p_font->i_interspacing) *
417 (strlen( psz_text ) - 1) + p_font->i_width;
418 *pi_height = p_font->i_height;
419 if( i_style & ITALIC_TEXT )
421 *pi_width = *pi_height / 3;
426 intf_ErrMsg("error: unknown font type %d", p_font->i_type );
432 /*****************************************************************************
433 * vout_Print: low level printing function
434 *****************************************************************************
435 * This function prints a text, without clipping, in a buffer using a
436 * previously loaded bitmap font.
437 *****************************************************************************/
438 void vout_Print( vout_font_t *p_font, byte_t *p_pic, int i_bytes_per_pixel, int i_bytes_per_line,
439 u32 i_char_color, u32 i_border_color, u32 i_bg_color, int i_style, const char *psz_text, int i_percent)
441 byte_t *p_char, *p_border; /* character and border mask data */
442 int i_char_mask, i_border_mask, i_bg_mask; /* masks */
443 int i_line; /* current line in character */
444 int i_byte; /* current byte in character */
445 int i_interspacing; /* offset between two chars */
446 int i_font_bytes_per_line, i_font_height; /* font properties */
447 int i_position, i_end; /* current position */
448 vout_put_byte_t *p_PutByte; /* PutByte function */
450 /* If no font was loaded, do nothing */
456 /* FIXME: background: can be something else that whole byte ?? */
458 /* Select output function */
459 switch( i_bytes_per_pixel )
462 p_PutByte = (vout_put_byte_t *) PutByte8;
465 p_PutByte = (vout_put_byte_t *) PutByte16;
468 p_PutByte = (vout_put_byte_t *) PutByte24;
472 p_PutByte = (vout_put_byte_t *) PutByte32;
476 /* Choose masks and copy font data to local variables */
477 i_char_mask = (i_style & VOID_TEXT) ? 0 : 0xff;
478 i_border_mask = (i_style & OUTLINED_TEXT) ? 0xff : 0;
479 i_bg_mask = (i_style & OPAQUE_TEXT) ? 0xff : 0;
481 i_font_bytes_per_line = p_font->i_bytes_per_line;
482 i_font_height = p_font->i_height;
483 i_interspacing = i_bytes_per_pixel * ((i_style & WIDE_TEXT) ?
484 p_font->i_interspacing * 2 :
485 p_font->i_interspacing);
487 /* compute where to stop... */
488 i_end = (int) (i_percent * strlen(psz_text) / I64C(100));
489 if(i_end > strlen(psz_text))
490 i_end = strlen(psz_text);
494 for( i_position = 0; i_position < i_end; i_position++ ,psz_text++ )
496 /* Check that the character is valid */
497 if( (*psz_text >= p_font->i_first) && (*psz_text <= p_font->i_last) )
499 /* Select character - bytes per char is always valid, event for
501 p_char = p_font->p_data + (*psz_text - p_font->i_first) * p_font->i_bytes_per_char;
502 p_border = p_char + (p_font->i_last - p_font->i_first + 1) * p_font->i_bytes_per_char;
504 /* Select base address for output */
505 switch( p_font->i_type )
507 case VOUT_FIXED_FONT:
509 * Simple fixed width font
512 /* Italic text: shift picture start right */
513 if( i_style & ITALIC_TEXT )
515 p_pic += i_bytes_per_pixel * (p_font->i_height / 3);
518 /* Print character */
519 for( i_line = 0; i_line < i_font_height; i_line ++ )
521 for( i_byte = 0; i_byte < i_font_bytes_per_line; i_byte++, p_char++, p_border++)
524 p_PutByte( p_pic + i_bytes_per_line * i_line, i_byte,
525 *p_char & i_char_mask, *p_border & i_border_mask, i_bg_mask,
526 i_char_color, i_border_color, i_bg_color );
529 /* Italic text: shift picture start left */
530 if( (i_style & ITALIC_TEXT) && !(i_line % 3) )
532 p_pic -= i_bytes_per_pixel;
536 /* Jump to next character */
537 p_pic += i_interspacing;
541 intf_ErrMsg("error: unknown font type %d", p_font->i_type );
550 /* following functions are local */
552 /*****************************************************************************
553 * PutByte8: print a fixed width font character byte in 1 Bpp
554 *****************************************************************************/
555 static void PutByte8( u8 *p_pic, int i_byte, int i_char, int i_border,
556 int i_bg, u32 i_char_color, u32 i_border_color,
559 /* Computes position offset and background mask */
561 i_bg &= ~(i_char | i_border);
563 /* Put character bits */
564 PUT_BYTE_MASK(i_char, i_char_color);
565 PUT_BYTE_MASK(i_border, i_border_color);
566 PUT_BYTE_MASK(i_bg, i_bg_color);
569 /*****************************************************************************
570 * PutByte16: print a fixed width font character byte in 2 Bpp
571 *****************************************************************************/
572 static void PutByte16( u16 *p_pic, int i_byte, int i_char, int i_border,
573 int i_bg, u32 i_char_color, u32 i_border_color,
576 /* Computes position offset and background mask */
578 i_bg &= ~(i_char | i_border);
580 /* Put character bits */
581 PUT_BYTE_MASK(i_char, i_char_color);
582 PUT_BYTE_MASK(i_border, i_border_color);
583 PUT_BYTE_MASK(i_bg, i_bg_color);
586 /*****************************************************************************
587 * PutByte24: print a fixed width font character byte in 3 Bpp
588 *****************************************************************************/
589 static void PutByte24( void *p_pic, int i_byte, byte_t i_char, byte_t i_border, byte_t i_bg,
590 u32 i_char_color, u32 i_border_color, u32 i_bg_color )
595 /*****************************************************************************
596 * PutByte32: print a fixed width font character byte in 4 Bpp
597 *****************************************************************************/
598 static void PutByte32( u32 *p_pic, int i_byte, byte_t i_char, byte_t i_border, byte_t i_bg,
599 u32 i_char_color, u32 i_border_color, u32 i_bg_color )
601 /* Computes position offset and background mask */
603 i_bg &= ~(i_char | i_border);
605 /* Put character bits */
606 PUT_BYTE_MASK(i_char, i_char_color);
607 PUT_BYTE_MASK(i_border, i_border_color);
608 PUT_BYTE_MASK(i_bg, i_bg_color);