1 /*****************************************************************************
2 * video_text.c : text manipulation functions
3 *****************************************************************************
4 * Copyright (C) 1999-2001 VideoLAN
5 * $Id: video_text.c,v 1.38 2002/11/10 18:04:24 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 <stdlib.h> /* free() */
29 #include <stdio.h> /* sprintf() */
30 #include <string.h> /* strerror() */
35 # include <errno.h> /* errno */
39 # include <fcntl.h> /* open() */
43 # include <unistd.h> /* read(), close() */
44 #elif defined( WIN32 ) && !defined( UNDER_CE )
49 #include "video_output.h"
50 #include "video_text.h"
52 /*****************************************************************************
53 * vout_font_t: bitmap font
54 *****************************************************************************
55 * This structure is used when the system doesn't provide a convenient function
56 * to print simple characters in a buffer.
57 * VOUT_FIXED_FONTs are stored in raw mode, character after character, with a
58 * first array of characters followed by a second array of borders masks.
59 * Therefore the border masks can't be complete if the font has pixels on the
61 *****************************************************************************/
64 int i_type; /* font type */
65 int i_width; /* character width in pixels */
66 int i_height; /* character height in pixels */
67 int i_interspacing; /* characters interspacing in pixels */
68 int i_bytes_per_line; /* bytes per character line */
69 int i_bytes_per_char; /* bytes per character */
70 u16 i_first; /* first character */
71 u16 i_last; /* last character */
72 byte_t * p_data; /* font character data */
76 #define VOUT_FIXED_FONT 0 /* simple fixed font */
78 /*****************************************************************************
79 * vout_put_byte_t: PutByte function
80 *****************************************************************************
81 * These functions will transform masks in a set of pixels. For each pixel,
82 * character, then border and background masks are tested, and the first
83 * encountered color is set.
84 *****************************************************************************/
85 typedef void (vout_put_byte_t)( void *p_pic, int i_byte, int i_char, int i_border,
86 int i_bg, u32 i_char_color, u32 i_border_color, u32 i_bg_color );
89 /*****************************************************************************
91 *****************************************************************************/
93 /* PUT_BYTE_MASK: put pixels from a byte-wide mask. It uses a branching tree
94 * to optimize the number of tests. It is used in the PutByte functions.
95 * This macro works for 1, 2 and 4 Bpp. */
96 #define PUT_BYTE_MASK( i_mask, i_mask_color ) \
97 if( i_mask & 0xf0 ) /* one from 1111 */ \
99 if( i_mask & 0xc0 ) /* one from 1100 */ \
101 if( i_mask & 0x80 ) /* 1000 */ \
103 p_pic[0] = i_mask_color; \
104 if( i_mask & 0x40 ) /* 0100 */ \
106 p_pic[1] = i_mask_color; \
109 else /* not 1000 means 0100 */ \
111 p_pic[1] = i_mask_color; \
113 if( i_mask & 0x30 ) /* one from 0011 */ \
115 if( i_mask & 0x20 ) /* 0010 */ \
117 p_pic[2] = i_mask_color; \
118 if( i_mask & 0x10 ) /* 0001 */ \
120 p_pic[3] = i_mask_color; \
123 else /* not 0010 means 0001 */ \
125 p_pic[3] = i_mask_color; \
129 else /* not 1100 means 0011 */ \
131 if( i_mask & 0x20 ) /* 0010 */ \
133 p_pic[2] = i_mask_color; \
134 if( i_mask & 0x10 ) /* 0001 */ \
136 p_pic[3] = i_mask_color; \
139 else /* not 0010 means 0001 */ \
141 p_pic[3] = i_mask_color; \
145 if( i_mask & 0x0f ) \
147 if( i_mask & 0x0c ) /* one from 1100 */ \
149 if( i_mask & 0x08 ) /* 1000 */ \
151 p_pic[4] = i_mask_color; \
152 if( i_mask & 0x04 ) /* 0100 */ \
154 p_pic[5] = i_mask_color; \
157 else /* not 1000 means 0100 */ \
159 p_pic[5] = i_mask_color; \
161 if( i_mask & 0x03 ) /* one from 0011 */ \
163 if( i_mask & 0x02 ) /* 0010 */ \
165 p_pic[6] = i_mask_color; \
166 if( i_mask & 0x01 ) /* 0001 */ \
168 p_pic[7] = i_mask_color; \
171 else /* not 0010 means 0001 */ \
173 p_pic[7] = i_mask_color; \
177 else /* not 1100 means 0011 */ \
179 if( i_mask & 0x02 ) /* 0010 */ \
181 p_pic[6] = i_mask_color; \
182 if( i_mask & 0x01 ) /* 0001 */ \
184 p_pic[7] = i_mask_color; \
187 else /* not 0010 means 0001 */ \
189 p_pic[7] = i_mask_color; \
194 /*****************************************************************************
196 *****************************************************************************/
197 static void PutByte8 ( u8 *p_pic, int i_byte, int i_char, int i_border,
198 int i_bg, u32 i_char_color, u32 i_border_color,
200 static void PutByte16( u16 *p_pic, int i_byte, int i_char, int i_border,
201 int i_bg, u32 i_char_color, u32 i_border_color,
203 static void PutByte24( void *p_pic, int i_byte, byte_t i_char, byte_t i_border,
204 byte_t i_bg, u32 i_char_color, u32 i_border_color,
206 static void PutByte32( u32 *p_pic, int i_byte, byte_t i_char, byte_t i_border,
207 byte_t i_bg, u32 i_char_color, u32 i_border_color,
210 /*****************************************************************************
211 * vout_LoadFont: load a bitmap font from a file
212 *****************************************************************************
213 * This function will try to open a .psf font and load it. It will return
215 *****************************************************************************/
216 vout_font_t *vout_LoadFont( vout_thread_t *p_vout, const char *psz_name )
218 static char * path[] = { "share", DATA_PATH, NULL, NULL };
220 char ** ppsz_path = path;
222 #if defined( SYS_BEOS ) || defined( SYS_DARWIN )
223 char * psz_vlcpath = system_GetProgramPath();
224 int i_vlclen = strlen( psz_vlcpath );
226 int i_char, i_line; /* character and line indexes */
227 int i_file = -1; /* source file */
228 byte_t pi_buffer[2]; /* file buffer */
229 vout_font_t * p_font; /* the font itself */
231 for( ; *ppsz_path != NULL ; ppsz_path++ )
233 #if defined( SYS_BEOS ) || defined( SYS_DARWIN )
234 /* Under BeOS, we need to add beos_GetProgramPath() to access
235 * files under the current directory */
236 if( strncmp( *ppsz_path, "/", 1 ) )
238 psz_file = malloc( strlen( psz_name ) + strlen( *ppsz_path )
240 if( psz_file == NULL )
244 sprintf( psz_file, "%s/%s/%s", psz_vlcpath, *ppsz_path, psz_name );
249 psz_file = malloc( strlen( psz_name ) + strlen( *ppsz_path ) + 2 );
250 if( psz_file == NULL )
254 sprintf( psz_file, "%s/%s", *ppsz_path, psz_name );
258 #ifndef UNDER_CE /* FIXME */
259 i_file = open( psz_file, O_RDONLY );
272 msg_Err( p_vout, "cannot open '%s' (%s)", psz_name, strerror(errno) );
274 msg_Err( p_vout, "cannot open '%s'", psz_name );
279 /* Read magic number */
280 if( read( i_file, pi_buffer, 2 ) != 2 )
282 msg_Err( p_vout, "unexpected end of file in '%s'", psz_name );
287 /* Allocate font descriptor */
288 p_font = malloc( sizeof( vout_font_t ) );
291 msg_Err( p_vout, "out of memory" );
297 switch( ((u16)pi_buffer[0] << 8) | pi_buffer[1] )
299 case 0x3604: /* .psf file */
301 * PSF font: simple fixed font. Only the first 256 characters are read.
302 * Those fonts are always 1 byte wide, and 256 or 512 characters long.
305 /* Read font header - two bytes indicate the font properties */
306 if( read( i_file, pi_buffer, 2 ) != 2)
308 msg_Err( p_vout, "unexpected end of file in '%s'", psz_name );
314 /* Copy font properties */
315 p_font->i_type = VOUT_FIXED_FONT;
317 p_font->i_height = pi_buffer[1];
318 p_font->i_interspacing = 8;
319 p_font->i_bytes_per_line = 1;
320 p_font->i_bytes_per_char = pi_buffer[1];
322 p_font->i_last = 255;
324 /* Allocate font space */
325 p_font->p_data = malloc( 2 * 256 * pi_buffer[1] );
326 if( p_font->p_data == NULL )
328 msg_Err( p_vout, "out of memory" );
335 if( read( i_file, p_font->p_data, 256 * pi_buffer[1] ) != 256 * pi_buffer[1] )
337 msg_Err( p_vout, "unexpected end of file in '%s'", psz_name );
338 free( p_font->p_data );
344 /* Compute border masks - remember that masks have the same matrix as
345 * characters, so an empty character border is required to have a
346 * complete border mask. */
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 msg_Err( p_vout, "file '%s' has an unknown format", psz_name );
370 msg_Err( p_vout, "loaded %s, type %d, %d-%dx%d", psz_name, p_font->i_type,
371 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 free( p_font->p_data );
393 /*****************************************************************************
394 * vout_TextSize: return the dimensions of a text
395 *****************************************************************************
396 * This function is used to align text. It returns the width and height of a
398 *****************************************************************************/
399 void vout_TextSize( vout_font_t *p_font, int i_style, const char *psz_text, int *pi_width, int *pi_height )
401 /* If no font was loaded, do nothing */
404 *pi_width = *pi_height = 0;
408 switch( p_font->i_type )
410 case VOUT_FIXED_FONT:
411 *pi_width = ((i_style & WIDE_TEXT) ? p_font->i_interspacing * 2 : p_font->i_interspacing) *
412 (strlen( psz_text ) - 1) + p_font->i_width;
413 *pi_height = p_font->i_height;
414 if( i_style & ITALIC_TEXT )
416 *pi_width = *pi_height / 3;
422 /*****************************************************************************
423 * vout_Print: low level printing function
424 *****************************************************************************
425 * This function prints a text, without clipping, in a buffer using a
426 * previously loaded bitmap font.
427 *****************************************************************************/
428 void vout_Print( vout_font_t *p_font, byte_t *p_pic, int i_bytes_per_pixel, int i_bytes_per_line,
429 u32 i_char_color, u32 i_border_color, u32 i_bg_color, int i_style, const char *psz_text, int i_percent)
431 byte_t *p_char, *p_border; /* character and border mask data */
432 int i_char_mask, i_border_mask, i_bg_mask; /* masks */
433 int i_line; /* current line in character */
434 int i_byte; /* current byte in character */
435 int i_interspacing; /* offset between two chars */
436 int i_font_bytes_per_line, i_font_height; /* font properties */
437 int i_position, i_end; /* current position */
438 vout_put_byte_t *p_PutByte; /* PutByte function */
440 /* If no font was loaded, do nothing */
446 /* FIXME: background: can be something else that whole byte ?? */
448 /* Select output function */
449 switch( i_bytes_per_pixel )
452 p_PutByte = (vout_put_byte_t *) PutByte8;
455 p_PutByte = (vout_put_byte_t *) PutByte16;
458 p_PutByte = (vout_put_byte_t *) PutByte24;
462 p_PutByte = (vout_put_byte_t *) PutByte32;
466 /* Choose masks and copy font data to local variables */
467 i_char_mask = (i_style & VOID_TEXT) ? 0 : 0xff;
468 i_border_mask = (i_style & OUTLINED_TEXT) ? 0xff : 0;
469 i_bg_mask = (i_style & OPAQUE_TEXT) ? 0xff : 0;
471 i_font_bytes_per_line = p_font->i_bytes_per_line;
472 i_font_height = p_font->i_height;
473 i_interspacing = i_bytes_per_pixel * ((i_style & WIDE_TEXT) ?
474 p_font->i_interspacing * 2 :
475 p_font->i_interspacing);
477 /* compute where to stop... */
478 i_end = (int) (i_percent * strlen(psz_text) / (s64)100);
479 if(i_end > strlen(psz_text))
480 i_end = strlen(psz_text);
484 for( i_position = 0; i_position < i_end; i_position++ ,psz_text++ )
486 /* Check that the character is valid */
487 if( (*psz_text >= p_font->i_first) && (*psz_text <= p_font->i_last) )
489 /* Select character - bytes per char is always valid, event for
491 p_char = p_font->p_data + (*psz_text - p_font->i_first) * p_font->i_bytes_per_char;
492 p_border = p_char + (p_font->i_last - p_font->i_first + 1) * p_font->i_bytes_per_char;
494 /* Select base address for output */
495 switch( p_font->i_type )
497 case VOUT_FIXED_FONT:
499 * Simple fixed width font
502 /* Italic text: shift picture start right */
503 if( i_style & ITALIC_TEXT )
505 p_pic += i_bytes_per_pixel * (p_font->i_height / 3);
508 /* Print character */
509 for( i_line = 0; i_line < i_font_height; i_line ++ )
511 for( i_byte = 0; i_byte < i_font_bytes_per_line; i_byte++, p_char++, p_border++)
514 p_PutByte( p_pic + i_bytes_per_line * i_line, i_byte,
515 *p_char & i_char_mask, *p_border & i_border_mask, i_bg_mask,
516 i_char_color, i_border_color, i_bg_color );
519 /* Italic text: shift picture start left */
520 if( (i_style & ITALIC_TEXT) && !(i_line % 3) )
522 p_pic -= i_bytes_per_pixel;
526 /* Jump to next character */
527 p_pic += i_interspacing;
534 /* following functions are local */
536 /*****************************************************************************
537 * PutByte8: print a fixed width font character byte in 1 Bpp
538 *****************************************************************************/
539 static void PutByte8( u8 *p_pic, int i_byte, int i_char, int i_border,
540 int i_bg, u32 i_char_color, u32 i_border_color,
543 /* Computes position offset and background mask */
545 i_bg &= ~(i_char | i_border);
547 /* Put character bits */
548 PUT_BYTE_MASK(i_char, i_char_color);
549 PUT_BYTE_MASK(i_border, i_border_color);
550 PUT_BYTE_MASK(i_bg, i_bg_color);
553 /*****************************************************************************
554 * PutByte16: print a fixed width font character byte in 2 Bpp
555 *****************************************************************************/
556 static void PutByte16( u16 *p_pic, int i_byte, int i_char, int i_border,
557 int i_bg, u32 i_char_color, u32 i_border_color,
560 /* Computes position offset and background mask */
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 * PutByte24: print a fixed width font character byte in 3 Bpp
572 *****************************************************************************/
573 static void PutByte24( void *p_pic, int i_byte, byte_t i_char, byte_t i_border, byte_t i_bg,
574 u32 i_char_color, u32 i_border_color, u32 i_bg_color )
579 /*****************************************************************************
580 * PutByte32: print a fixed width font character byte in 4 Bpp
581 *****************************************************************************/
582 static void PutByte32( u32 *p_pic, int i_byte, byte_t i_char, byte_t i_border, byte_t i_bg,
583 u32 i_char_color, u32 i_border_color, u32 i_bg_color )
585 /* Computes position offset and background mask */
587 i_bg &= ~(i_char | i_border);
589 /* Put character bits */
590 PUT_BYTE_MASK(i_char, i_char_color);
591 PUT_BYTE_MASK(i_border, i_border_color);
592 PUT_BYTE_MASK(i_bg, i_bg_color);