1 /*****************************************************************************
2 * video_text.c : text manipulation functions
3 *****************************************************************************
4 * Copyright (C) 1999, 2000 VideoLAN
5 * $Id: video_text.c,v 1.24 2001/04/25 09:31:14 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 );
257 i_file = open( psz_file, O_RDONLY );
268 intf_DbgMsg( "vout error: can't open file '%s' (%s)",
269 psz_name, strerror(errno) );
273 /* Read magick number */
274 if( read( i_file, pi_buffer, 2 ) != 2 )
276 intf_ErrMsg("vout error: unexpected end of file '%s'", psz_name );
281 /* Allocate font descriptor */
282 p_font = malloc( sizeof( vout_font_t ) );
285 intf_ErrMsg( "vout error: cannot allocate vout_font_t (%s)",
292 switch( ((u16)pi_buffer[0] << 8) | pi_buffer[1] )
294 case 0x3604: /* .psf file */
296 * PSF font: simple fixed font. Only the first 256 characters are read.
297 * Those fonts are always 1 byte wide, and 256 or 512 characters long.
300 /* Read font header - two bytes indicate the font properties */
301 if( read( i_file, pi_buffer, 2 ) != 2)
303 intf_ErrMsg( "vout error: unexpected end of file '%s'", psz_name );
309 /* Copy font properties */
310 p_font->i_type = VOUT_FIXED_FONT;
312 p_font->i_height = pi_buffer[1];
313 p_font->i_interspacing = 8;
314 p_font->i_bytes_per_line = 1;
315 p_font->i_bytes_per_char = pi_buffer[1];
317 p_font->i_last = 255;
319 /* Allocate font space */
320 p_font->p_data = malloc( 2 * 256 * pi_buffer[1] );
321 if( p_font->p_data == NULL )
323 intf_ErrMsg( "vout error: cannot allocate font space (%s)",
331 if( read( i_file, p_font->p_data, 256 * pi_buffer[1] ) != 256 * pi_buffer[1] )
333 intf_ErrMsg("vout error: unexpected end of file '%s'", psz_name );
334 free( p_font->p_data );
340 /* Computes border masks - remember that masks have the same matrix as
341 * characters, so an empty character border is required to have a complete
343 for( i_char = 0; i_char <= 255; i_char++ )
345 for( i_line = 0; i_line < pi_buffer[1]; i_line++ )
348 p_font->p_data[ (i_char + 256) * pi_buffer[1] + i_line ] =
349 ((p_font->p_data[ i_char * pi_buffer[1] + i_line ] << 1) |
350 (p_font->p_data[ i_char * pi_buffer[1] + i_line ] >> 1) |
351 (i_line > 0 ? p_font->p_data[ i_char * pi_buffer[1] + i_line - 1]: 0) |
352 (i_line < pi_buffer[1] - 1 ? p_font->p_data[ i_char * pi_buffer[1] + i_line + 1]: 0))
353 & ~p_font->p_data[ i_char * pi_buffer[1] + i_line ];
359 intf_ErrMsg("vout error: file '%s' has an unknown format", psz_name );
367 intf_DbgMsg( "loaded %s: type %d, %d-%dx%d", psz_name, p_font->i_type,
368 p_font->i_width, p_font->i_interspacing, p_font->i_height );
372 /*****************************************************************************
373 * vout_UnloadFont: unload a font
374 *****************************************************************************
375 * This function free the resources allocated by vout_LoadFont
376 *****************************************************************************/
377 void vout_UnloadFont( vout_font_t *p_font )
379 /* If no font was loaded, do nothing */
385 intf_DbgMsg( "vout: unloading font %p", p_font );
386 free( p_font->p_data );
390 /*****************************************************************************
391 * vout_TextSize: return the dimensions of a text
392 *****************************************************************************
393 * This function is used to align text. It returns the width and height of a
395 *****************************************************************************/
396 void vout_TextSize( vout_font_t *p_font, int i_style, const char *psz_text, int *pi_width, int *pi_height )
398 /* If no font was loaded, do nothing */
401 *pi_width = *pi_height = 0;
405 switch( p_font->i_type )
407 case VOUT_FIXED_FONT:
408 *pi_width = ((i_style & WIDE_TEXT) ? p_font->i_interspacing * 2 : p_font->i_interspacing) *
409 (strlen( psz_text ) - 1) + p_font->i_width;
410 *pi_height = p_font->i_height;
411 if( i_style & ITALIC_TEXT )
413 *pi_width = *pi_height / 3;
418 intf_DbgMsg("error: unknown font type %d", p_font->i_type );
424 /*****************************************************************************
425 * vout_Print: low level printing function
426 *****************************************************************************
427 * This function prints a text, without clipping, in a buffer using a
428 * previously loaded bitmap font.
429 *****************************************************************************/
430 void vout_Print( vout_font_t *p_font, byte_t *p_pic, int i_bytes_per_pixel, int i_bytes_per_line,
431 u32 i_char_color, u32 i_border_color, u32 i_bg_color, int i_style, const char *psz_text, int i_percent)
433 byte_t *p_char, *p_border; /* character and border mask data */
434 int i_char_mask, i_border_mask, i_bg_mask; /* masks */
435 int i_line; /* current line in character */
436 int i_byte; /* current byte in character */
437 int i_interspacing; /* offset between two chars */
438 int i_font_bytes_per_line, i_font_height; /* font properties */
439 int i_position, i_end; /* current position */
440 vout_put_byte_t *p_PutByte; /* PutByte function */
442 /* If no font was loaded, do nothing */
448 /* FIXME: background: can be something else that whole byte ?? */
450 /* Select output function */
451 switch( i_bytes_per_pixel )
454 p_PutByte = (vout_put_byte_t *) PutByte8;
457 p_PutByte = (vout_put_byte_t *) PutByte16;
460 p_PutByte = (vout_put_byte_t *) PutByte24;
464 p_PutByte = (vout_put_byte_t *) PutByte32;
468 /* Choose masks and copy font data to local variables */
469 i_char_mask = (i_style & VOID_TEXT) ? 0 : 0xff;
470 i_border_mask = (i_style & OUTLINED_TEXT) ? 0xff : 0;
471 i_bg_mask = (i_style & OPAQUE_TEXT) ? 0xff : 0;
473 i_font_bytes_per_line = p_font->i_bytes_per_line;
474 i_font_height = p_font->i_height;
475 i_interspacing = i_bytes_per_pixel * ((i_style & WIDE_TEXT) ?
476 p_font->i_interspacing * 2 :
477 p_font->i_interspacing);
479 /* compute where to stop... */
480 i_end = (int) (i_percent * strlen(psz_text) / 100LL);
481 if(i_end > strlen(psz_text))
482 i_end = strlen(psz_text);
486 for( i_position = 0; i_position < i_end; i_position++ ,psz_text++ )
488 /* Check that the character is valid */
489 if( (*psz_text >= p_font->i_first) && (*psz_text <= p_font->i_last) )
491 /* Select character - bytes per char is always valid, event for
493 p_char = p_font->p_data + (*psz_text - p_font->i_first) * p_font->i_bytes_per_char;
494 p_border = p_char + (p_font->i_last - p_font->i_first + 1) * p_font->i_bytes_per_char;
496 /* Select base address for output */
497 switch( p_font->i_type )
499 case VOUT_FIXED_FONT:
501 * Simple fixed width font
504 /* Italic text: shift picture start right */
505 if( i_style & ITALIC_TEXT )
507 p_pic += i_bytes_per_pixel * (p_font->i_height / 3);
510 /* Print character */
511 for( i_line = 0; i_line < i_font_height; i_line ++ )
513 for( i_byte = 0; i_byte < i_font_bytes_per_line; i_byte++, p_char++, p_border++)
516 p_PutByte( p_pic + i_bytes_per_line * i_line, i_byte,
517 *p_char & i_char_mask, *p_border & i_border_mask, i_bg_mask,
518 i_char_color, i_border_color, i_bg_color );
521 /* Italic text: shift picture start left */
522 if( (i_style & ITALIC_TEXT) && !(i_line % 3) )
524 p_pic -= i_bytes_per_pixel;
528 /* Jump to next character */
529 p_pic += i_interspacing;
533 intf_DbgMsg("error: unknown font type %d", p_font->i_type );
542 /* following functions are local */
544 /*****************************************************************************
545 * PutByte8: print a fixed width font character byte in 1 Bpp
546 *****************************************************************************/
547 static void PutByte8( u8 *p_pic, int i_byte, int i_char, int i_border,
548 int i_bg, u32 i_char_color, u32 i_border_color,
551 /* Computes position offset and background mask */
553 i_bg &= ~(i_char | i_border);
555 /* Put character bits */
556 PUT_BYTE_MASK(i_char, i_char_color);
557 PUT_BYTE_MASK(i_border, i_border_color);
558 PUT_BYTE_MASK(i_bg, i_bg_color);
561 /*****************************************************************************
562 * PutByte16: print a fixed width font character byte in 2 Bpp
563 *****************************************************************************/
564 static void PutByte16( u16 *p_pic, int i_byte, int i_char, int i_border,
565 int i_bg, u32 i_char_color, u32 i_border_color,
568 /* Computes position offset and background mask */
570 i_bg &= ~(i_char | i_border);
572 /* Put character bits */
573 PUT_BYTE_MASK(i_char, i_char_color);
574 PUT_BYTE_MASK(i_border, i_border_color);
575 PUT_BYTE_MASK(i_bg, i_bg_color);
578 /*****************************************************************************
579 * PutByte24: print a fixed width font character byte in 3 Bpp
580 *****************************************************************************/
581 static void PutByte24( void *p_pic, int i_byte, byte_t i_char, byte_t i_border, byte_t i_bg,
582 u32 i_char_color, u32 i_border_color, u32 i_bg_color )
587 /*****************************************************************************
588 * PutByte32: print a fixed width font character byte in 4 Bpp
589 *****************************************************************************/
590 static void PutByte32( u32 *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 /* Computes position offset and background mask */
595 i_bg &= ~(i_char | i_border);
597 /* Put character bits */
598 PUT_BYTE_MASK(i_char, i_char_color);
599 PUT_BYTE_MASK(i_border, i_border_color);
600 PUT_BYTE_MASK(i_bg, i_bg_color);