1 /*****************************************************************************
2 * video_text.c : text manipulation functions
3 *****************************************************************************
4 * Copyright (C) 1999, 2000 VideoLAN
5 * $Id: video_text.c,v 1.26 2001/04/28 03:36:25 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() */
35 #include <unistd.h> /* read(), close() */
38 # include "beos_specific.h"
42 # include "darwin_specific.h"
47 #include "video_text.h"
51 /*****************************************************************************
52 * vout_font_t: bitmap font
53 *****************************************************************************
54 * This structure is used when the system doesn't provide a convenient function
55 * to print simple characters in a buffer.
56 * VOUT_FIXED_FONTs are stored in raw mode, character after character, with a
57 * first array of characters followed by a second array of borders masks.
58 * Therefore the border masks can't be complete if the font has pixels on the
60 *****************************************************************************/
61 typedef struct vout_font_s
63 int i_type; /* font type */
64 int i_width; /* character width in pixels */
65 int i_height; /* character height in pixels */
66 int i_interspacing; /* characters interspacing in pixels */
67 int i_bytes_per_line; /* bytes per character line */
68 int i_bytes_per_char; /* bytes per character */
69 u16 i_first; /* first character */
70 u16 i_last; /* last character */
71 byte_t * p_data; /* font character data */
75 #define VOUT_FIXED_FONT 0 /* simple fixed font */
77 /*****************************************************************************
78 * vout_put_byte_t: PutByte function
79 *****************************************************************************
80 * These functions will transform masks in a set of pixels. For each pixel,
81 * character, then border and background masks are tested, and the first
82 * encountered color is set.
83 *****************************************************************************/
84 typedef void (vout_put_byte_t)( void *p_pic, int i_byte, int i_char, int i_border,
85 int i_bg, u32 i_char_color, u32 i_border_color, u32 i_bg_color );
88 /*****************************************************************************
90 *****************************************************************************/
92 /* PUT_BYTE_MASK: put pixels from a byte-wide mask. It uses a branching tree
93 * to optimize the number of tests. It is used in the PutByte functions.
94 * This macro works for 1, 2 and 4 Bpp. */
95 #define PUT_BYTE_MASK( i_mask, i_mask_color ) \
96 if( i_mask & 0xf0 ) /* one from 1111 */ \
98 if( i_mask & 0xc0 ) /* one from 1100 */ \
100 if( i_mask & 0x80 ) /* 1000 */ \
102 p_pic[0] = i_mask_color; \
103 if( i_mask & 0x40 ) /* 0100 */ \
105 p_pic[1] = i_mask_color; \
108 else /* not 1000 means 0100 */ \
110 p_pic[1] = i_mask_color; \
112 if( i_mask & 0x30 ) /* one from 0011 */ \
114 if( i_mask & 0x20 ) /* 0010 */ \
116 p_pic[2] = i_mask_color; \
117 if( i_mask & 0x10 ) /* 0001 */ \
119 p_pic[3] = i_mask_color; \
122 else /* not 0010 means 0001 */ \
124 p_pic[3] = i_mask_color; \
128 else /* not 1100 means 0011 */ \
130 if( i_mask & 0x20 ) /* 0010 */ \
132 p_pic[2] = i_mask_color; \
133 if( i_mask & 0x10 ) /* 0001 */ \
135 p_pic[3] = i_mask_color; \
138 else /* not 0010 means 0001 */ \
140 p_pic[3] = i_mask_color; \
144 if( i_mask & 0x0f ) \
146 if( i_mask & 0x0c ) /* one from 1100 */ \
148 if( i_mask & 0x08 ) /* 1000 */ \
150 p_pic[4] = i_mask_color; \
151 if( i_mask & 0x04 ) /* 0100 */ \
153 p_pic[5] = i_mask_color; \
156 else /* not 1000 means 0100 */ \
158 p_pic[5] = i_mask_color; \
160 if( i_mask & 0x03 ) /* one from 0011 */ \
162 if( i_mask & 0x02 ) /* 0010 */ \
164 p_pic[6] = i_mask_color; \
165 if( i_mask & 0x01 ) /* 0001 */ \
167 p_pic[7] = i_mask_color; \
170 else /* not 0010 means 0001 */ \
172 p_pic[7] = i_mask_color; \
176 else /* not 1100 means 0011 */ \
178 if( i_mask & 0x02 ) /* 0010 */ \
180 p_pic[6] = i_mask_color; \
181 if( i_mask & 0x01 ) /* 0001 */ \
183 p_pic[7] = i_mask_color; \
186 else /* not 0010 means 0001 */ \
188 p_pic[7] = i_mask_color; \
193 /*****************************************************************************
195 *****************************************************************************/
196 static void PutByte8 ( u8 *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 PutByte16( u16 *p_pic, int i_byte, int i_char, int i_border,
200 int i_bg, u32 i_char_color, u32 i_border_color,
202 static void PutByte24( void *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,
205 static void PutByte32( u32 *p_pic, int i_byte, byte_t i_char, byte_t i_border,
206 byte_t i_bg, u32 i_char_color, u32 i_border_color,
209 /*****************************************************************************
210 * vout_LoadFont: load a bitmap font from a file
211 *****************************************************************************
212 * This function will try to open a .psf font and load it. It will return
214 *****************************************************************************/
215 vout_font_t *vout_LoadFont( const char *psz_name )
217 static char * path[] = { "share", DATA_PATH, NULL, NULL };
219 char ** ppsz_path = path;
221 #if defined( SYS_BEOS ) || defined( SYS_DARWIN1_3 )
222 char * psz_vlcpath = system_GetProgramPath();
223 int i_vlclen = strlen( psz_vlcpath );
225 int i_char, i_line; /* character and line indexes */
226 int i_file = -1; /* source file */
227 byte_t pi_buffer[2]; /* file buffer */
228 vout_font_t * p_font; /* the font itself */
230 for( ; *ppsz_path != NULL ; ppsz_path++ )
232 #if defined( SYS_BEOS ) || defined( SYS_DARWIN1_3 )
233 /* Under BeOS, we need to add beos_GetProgramPath() to access
234 * files under the current directory */
235 if( strncmp( *ppsz_path, "/", 1 ) )
237 psz_file = malloc( strlen( psz_name ) + strlen( *ppsz_path )
239 if( psz_file == NULL )
243 sprintf( psz_file, "%s/%s/%s", psz_vlcpath, *ppsz_path, psz_name );
248 psz_file = malloc( strlen( psz_name ) + strlen( *ppsz_path ) + 2 );
249 if( psz_file == NULL )
253 sprintf( psz_file, "%s/%s", *ppsz_path, psz_name );
258 i_file = open( psz_file, O_RDONLY );
260 i_file = open( psz_file, O_RDONLY | O_BINARY );
272 intf_DbgMsg( "vout error: can't open file '%s' (%s)",
273 psz_name, strerror(errno) );
277 /* Read magick number */
278 if( read( i_file, pi_buffer, 2 ) != 2 )
280 intf_ErrMsg("vout error: unexpected end of file '%s'", psz_name );
285 /* Allocate font descriptor */
286 p_font = malloc( sizeof( vout_font_t ) );
289 intf_ErrMsg( "vout error: cannot allocate vout_font_t (%s)",
296 switch( ((u16)pi_buffer[0] << 8) | pi_buffer[1] )
298 case 0x3604: /* .psf file */
300 * PSF font: simple fixed font. Only the first 256 characters are read.
301 * Those fonts are always 1 byte wide, and 256 or 512 characters long.
304 /* Read font header - two bytes indicate the font properties */
305 if( read( i_file, pi_buffer, 2 ) != 2)
307 intf_ErrMsg( "vout error: unexpected end of file '%s'", psz_name );
313 /* Copy font properties */
314 p_font->i_type = VOUT_FIXED_FONT;
316 p_font->i_height = pi_buffer[1];
317 p_font->i_interspacing = 8;
318 p_font->i_bytes_per_line = 1;
319 p_font->i_bytes_per_char = pi_buffer[1];
321 p_font->i_last = 255;
323 /* Allocate font space */
324 p_font->p_data = malloc( 2 * 256 * pi_buffer[1] );
325 if( p_font->p_data == NULL )
327 intf_ErrMsg( "vout error: cannot allocate font space (%s)",
335 if( read( i_file, p_font->p_data, 256 * pi_buffer[1] ) != 256 * pi_buffer[1] )
337 intf_ErrMsg("vout error: unexpected end of file '%s'", psz_name );
338 free( p_font->p_data );
344 /* Computes border masks - remember that masks have the same matrix as
345 * characters, so an empty character border is required to have a complete
347 for( i_char = 0; i_char <= 255; i_char++ )
349 for( i_line = 0; i_line < pi_buffer[1]; i_line++ )
352 p_font->p_data[ (i_char + 256) * pi_buffer[1] + i_line ] =
353 ((p_font->p_data[ i_char * pi_buffer[1] + i_line ] << 1) |
354 (p_font->p_data[ i_char * pi_buffer[1] + i_line ] >> 1) |
355 (i_line > 0 ? p_font->p_data[ i_char * pi_buffer[1] + i_line - 1]: 0) |
356 (i_line < pi_buffer[1] - 1 ? p_font->p_data[ i_char * pi_buffer[1] + i_line + 1]: 0))
357 & ~p_font->p_data[ i_char * pi_buffer[1] + i_line ];
363 intf_ErrMsg("vout error: file '%s' has an unknown format", psz_name );
371 intf_DbgMsg( "loaded %s: type %d, %d-%dx%d", psz_name, p_font->i_type,
372 p_font->i_width, p_font->i_interspacing, p_font->i_height );
376 /*****************************************************************************
377 * vout_UnloadFont: unload a font
378 *****************************************************************************
379 * This function free the resources allocated by vout_LoadFont
380 *****************************************************************************/
381 void vout_UnloadFont( vout_font_t *p_font )
383 /* If no font was loaded, do nothing */
389 intf_DbgMsg( "vout: unloading font %p", p_font );
390 free( p_font->p_data );
394 /*****************************************************************************
395 * vout_TextSize: return the dimensions of a text
396 *****************************************************************************
397 * This function is used to align text. It returns the width and height of a
399 *****************************************************************************/
400 void vout_TextSize( vout_font_t *p_font, int i_style, const char *psz_text, int *pi_width, int *pi_height )
402 /* If no font was loaded, do nothing */
405 *pi_width = *pi_height = 0;
409 switch( p_font->i_type )
411 case VOUT_FIXED_FONT:
412 *pi_width = ((i_style & WIDE_TEXT) ? p_font->i_interspacing * 2 : p_font->i_interspacing) *
413 (strlen( psz_text ) - 1) + p_font->i_width;
414 *pi_height = p_font->i_height;
415 if( i_style & ITALIC_TEXT )
417 *pi_width = *pi_height / 3;
422 intf_ErrMsg("error: unknown font type %d", p_font->i_type );
428 /*****************************************************************************
429 * vout_Print: low level printing function
430 *****************************************************************************
431 * This function prints a text, without clipping, in a buffer using a
432 * previously loaded bitmap font.
433 *****************************************************************************/
434 void vout_Print( vout_font_t *p_font, byte_t *p_pic, int i_bytes_per_pixel, int i_bytes_per_line,
435 u32 i_char_color, u32 i_border_color, u32 i_bg_color, int i_style, const char *psz_text, int i_percent)
437 byte_t *p_char, *p_border; /* character and border mask data */
438 int i_char_mask, i_border_mask, i_bg_mask; /* masks */
439 int i_line; /* current line in character */
440 int i_byte; /* current byte in character */
441 int i_interspacing; /* offset between two chars */
442 int i_font_bytes_per_line, i_font_height; /* font properties */
443 int i_position, i_end; /* current position */
444 vout_put_byte_t *p_PutByte; /* PutByte function */
446 /* If no font was loaded, do nothing */
452 /* FIXME: background: can be something else that whole byte ?? */
454 /* Select output function */
455 switch( i_bytes_per_pixel )
458 p_PutByte = (vout_put_byte_t *) PutByte8;
461 p_PutByte = (vout_put_byte_t *) PutByte16;
464 p_PutByte = (vout_put_byte_t *) PutByte24;
468 p_PutByte = (vout_put_byte_t *) PutByte32;
472 /* Choose masks and copy font data to local variables */
473 i_char_mask = (i_style & VOID_TEXT) ? 0 : 0xff;
474 i_border_mask = (i_style & OUTLINED_TEXT) ? 0xff : 0;
475 i_bg_mask = (i_style & OPAQUE_TEXT) ? 0xff : 0;
477 i_font_bytes_per_line = p_font->i_bytes_per_line;
478 i_font_height = p_font->i_height;
479 i_interspacing = i_bytes_per_pixel * ((i_style & WIDE_TEXT) ?
480 p_font->i_interspacing * 2 :
481 p_font->i_interspacing);
483 /* compute where to stop... */
484 i_end = (int) (i_percent * strlen(psz_text) / 100LL);
485 if(i_end > strlen(psz_text))
486 i_end = strlen(psz_text);
490 for( i_position = 0; i_position < i_end; i_position++ ,psz_text++ )
492 /* Check that the character is valid */
493 if( (*psz_text >= p_font->i_first) && (*psz_text <= p_font->i_last) )
495 /* Select character - bytes per char is always valid, event for
497 p_char = p_font->p_data + (*psz_text - p_font->i_first) * p_font->i_bytes_per_char;
498 p_border = p_char + (p_font->i_last - p_font->i_first + 1) * p_font->i_bytes_per_char;
500 /* Select base address for output */
501 switch( p_font->i_type )
503 case VOUT_FIXED_FONT:
505 * Simple fixed width font
508 /* Italic text: shift picture start right */
509 if( i_style & ITALIC_TEXT )
511 p_pic += i_bytes_per_pixel * (p_font->i_height / 3);
514 /* Print character */
515 for( i_line = 0; i_line < i_font_height; i_line ++ )
517 for( i_byte = 0; i_byte < i_font_bytes_per_line; i_byte++, p_char++, p_border++)
520 p_PutByte( p_pic + i_bytes_per_line * i_line, i_byte,
521 *p_char & i_char_mask, *p_border & i_border_mask, i_bg_mask,
522 i_char_color, i_border_color, i_bg_color );
525 /* Italic text: shift picture start left */
526 if( (i_style & ITALIC_TEXT) && !(i_line % 3) )
528 p_pic -= i_bytes_per_pixel;
532 /* Jump to next character */
533 p_pic += i_interspacing;
537 intf_ErrMsg("error: unknown font type %d", p_font->i_type );
546 /* following functions are local */
548 /*****************************************************************************
549 * PutByte8: print a fixed width font character byte in 1 Bpp
550 *****************************************************************************/
551 static void PutByte8( u8 *p_pic, int i_byte, int i_char, int i_border,
552 int i_bg, u32 i_char_color, u32 i_border_color,
555 /* Computes position offset and background mask */
557 i_bg &= ~(i_char | i_border);
559 /* Put character bits */
560 PUT_BYTE_MASK(i_char, i_char_color);
561 PUT_BYTE_MASK(i_border, i_border_color);
562 PUT_BYTE_MASK(i_bg, i_bg_color);
565 /*****************************************************************************
566 * PutByte16: print a fixed width font character byte in 2 Bpp
567 *****************************************************************************/
568 static void PutByte16( u16 *p_pic, int i_byte, int i_char, int i_border,
569 int i_bg, u32 i_char_color, u32 i_border_color,
572 /* Computes position offset and background mask */
574 i_bg &= ~(i_char | i_border);
576 /* Put character bits */
577 PUT_BYTE_MASK(i_char, i_char_color);
578 PUT_BYTE_MASK(i_border, i_border_color);
579 PUT_BYTE_MASK(i_bg, i_bg_color);
582 /*****************************************************************************
583 * PutByte24: print a fixed width font character byte in 3 Bpp
584 *****************************************************************************/
585 static void PutByte24( void *p_pic, int i_byte, byte_t i_char, byte_t i_border, byte_t i_bg,
586 u32 i_char_color, u32 i_border_color, u32 i_bg_color )
591 /*****************************************************************************
592 * PutByte32: print a fixed width font character byte in 4 Bpp
593 *****************************************************************************/
594 static void PutByte32( u32 *p_pic, int i_byte, byte_t i_char, byte_t i_border, byte_t i_bg,
595 u32 i_char_color, u32 i_border_color, u32 i_bg_color )
597 /* Computes position offset and background mask */
599 i_bg &= ~(i_char | i_border);
601 /* Put character bits */
602 PUT_BYTE_MASK(i_char, i_char_color);
603 PUT_BYTE_MASK(i_border, i_border_color);
604 PUT_BYTE_MASK(i_bg, i_bg_color);