1 /*****************************************************************************
2 * video_text.c : text manipulation functions
3 *****************************************************************************
4 * Copyright (C) 1999, 2000 VideoLAN
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
21 *****************************************************************************/
23 /*****************************************************************************
25 *****************************************************************************/
28 #include <errno.h> /* errno */
29 #include <stdlib.h> /* free() */
30 #include <string.h> /* strerror() */
31 #include <fcntl.h> /* open() */
32 #include <unistd.h> /* read(), close() */
36 #include "video_text.h"
40 /*****************************************************************************
41 * vout_font_t: bitmap font
42 *****************************************************************************
43 * This structure is used when the system doesn't provide a convenient function
44 * to print simple characters in a buffer.
45 * VOUT_FIXED_FONTs are stored in raw mode, character after character, with a
46 * first array of characters followed by a second array of borders masks.
47 * Therefore the border masks can't be complete if the font has pixels on the
49 *****************************************************************************/
50 typedef struct vout_font_s
52 int i_type; /* font type */
53 int i_width; /* character width in pixels */
54 int i_height; /* character height in pixels */
55 int i_interspacing; /* characters interspacing in pixels */
56 int i_bytes_per_line; /* bytes per character line */
57 int i_bytes_per_char; /* bytes per character */
58 u16 i_first; /* first character */
59 u16 i_last; /* last character */
60 byte_t * p_data; /* font character data */
64 #define VOUT_FIXED_FONT 0 /* simple fixed font */
66 /*****************************************************************************
67 * vout_put_byte_t: PutByte function
68 *****************************************************************************
69 * These functions will transform masks in a set of pixels. For each pixel,
70 * character, then border and background masks are tested, and the first
71 * encountered color is set.
72 *****************************************************************************/
73 typedef void (vout_put_byte_t)( void *p_pic, int i_byte, int i_char, int i_border,
74 int i_bg, u32 i_char_color, u32 i_border_color, u32 i_bg_color );
77 /*****************************************************************************
79 *****************************************************************************/
81 /* PUT_BYTE_MASK: put pixels from a byte-wide mask. It uses a branching tree
82 * to optimize the number of tests. It is used in the PutByte functions.
83 * This macro works for 1, 2 and 4 Bpp. */
84 #define PUT_BYTE_MASK( i_mask, i_mask_color ) \
85 if( i_mask & 0xf0 ) /* one from 1111 */ \
87 if( i_mask & 0xc0 ) /* one from 1100 */ \
89 if( i_mask & 0x80 ) /* 1000 */ \
91 p_pic[0] = i_mask_color; \
92 if( i_mask & 0x40 ) /* 0100 */ \
94 p_pic[1] = i_mask_color; \
97 else /* not 1000 means 0100 */ \
99 p_pic[1] = i_mask_color; \
101 if( i_mask & 0x30 ) /* one from 0011 */ \
103 if( i_mask & 0x20 ) /* 0010 */ \
105 p_pic[2] = i_mask_color; \
106 if( i_mask & 0x10 ) /* 0001 */ \
108 p_pic[3] = i_mask_color; \
111 else /* not 0010 means 0001 */ \
113 p_pic[3] = i_mask_color; \
117 else /* not 1100 means 0011 */ \
119 if( i_mask & 0x20 ) /* 0010 */ \
121 p_pic[2] = i_mask_color; \
122 if( i_mask & 0x10 ) /* 0001 */ \
124 p_pic[3] = i_mask_color; \
127 else /* not 0010 means 0001 */ \
129 p_pic[3] = i_mask_color; \
133 if( i_mask & 0x0f ) \
135 if( i_mask & 0x0c ) /* one from 1100 */ \
137 if( i_mask & 0x08 ) /* 1000 */ \
139 p_pic[4] = i_mask_color; \
140 if( i_mask & 0x04 ) /* 0100 */ \
142 p_pic[5] = i_mask_color; \
145 else /* not 1000 means 0100 */ \
147 p_pic[5] = i_mask_color; \
149 if( i_mask & 0x03 ) /* one from 0011 */ \
151 if( i_mask & 0x02 ) /* 0010 */ \
153 p_pic[6] = i_mask_color; \
154 if( i_mask & 0x01 ) /* 0001 */ \
156 p_pic[7] = i_mask_color; \
159 else /* not 0010 means 0001 */ \
161 p_pic[7] = i_mask_color; \
165 else /* not 1100 means 0011 */ \
167 if( i_mask & 0x02 ) /* 0010 */ \
169 p_pic[6] = i_mask_color; \
170 if( i_mask & 0x01 ) /* 0001 */ \
172 p_pic[7] = i_mask_color; \
175 else /* not 0010 means 0001 */ \
177 p_pic[7] = i_mask_color; \
182 /*****************************************************************************
184 *****************************************************************************/
185 static void PutByte8 ( u8 *p_pic, int i_byte, int i_char, int i_border,
186 int i_bg, u32 i_char_color, u32 i_border_color,
188 static void PutByte16( u16 *p_pic, int i_byte, int i_char, int i_border,
189 int i_bg, u32 i_char_color, u32 i_border_color,
191 static void PutByte24( void *p_pic, int i_byte, byte_t i_char, byte_t i_border,
192 byte_t i_bg, u32 i_char_color, u32 i_border_color,
194 static void PutByte32( u32 *p_pic, int i_byte, byte_t i_char, byte_t i_border,
195 byte_t i_bg, u32 i_char_color, u32 i_border_color,
198 /*****************************************************************************
199 * vout_LoadFont: load a bitmap font from a file
200 *****************************************************************************
201 * This function will try to open a .psf font and load it. It will return
203 *****************************************************************************/
204 vout_font_t *vout_LoadFont( const char *psz_name )
206 int i_char, i_line; /* character and line indexes */
207 int i_file; /* source file */
208 byte_t pi_buffer[2]; /* file buffer */
209 vout_font_t * p_font; /* the font itself */
212 i_file = open( psz_name, O_RDONLY );
215 intf_DbgMsg("vout: can't open file '%s' (%s)", psz_name, strerror(errno));
219 /* Read magick number */
220 if( read( i_file, pi_buffer, 2 ) != 2 )
222 intf_ErrMsg("vout error: unexpected end of file '%s'", psz_name );
227 /* Allocate font descriptor */
228 p_font = malloc( sizeof( vout_font_t ) );
231 intf_ErrMsg( "vout error: cannot allocate vout_font_t (%s)",
238 switch( ((u16)pi_buffer[0] << 8) | pi_buffer[1] )
240 case 0x3604: /* .psf file */
242 * PSF font: simple fixed font. Only the first 256 characters are read.
243 * Those fonts are always 1 byte wide, and 256 or 512 characters long.
246 /* Read font header - two bytes indicate the font properties */
247 if( read( i_file, pi_buffer, 2 ) != 2)
249 intf_ErrMsg( "vout error: unexpected end of file '%s'", psz_name );
255 /* Copy font properties */
256 p_font->i_type = VOUT_FIXED_FONT;
258 p_font->i_height = pi_buffer[1];
259 p_font->i_interspacing = 8;
260 p_font->i_bytes_per_line = 1;
261 p_font->i_bytes_per_char = pi_buffer[1];
263 p_font->i_last = 255;
265 /* Allocate font space */
266 p_font->p_data = malloc( 2 * 256 * pi_buffer[1] );
267 if( p_font->p_data == NULL )
269 intf_ErrMsg( "vout error: cannot allocate font space (%s)",
277 if( read( i_file, p_font->p_data, 256 * pi_buffer[1] ) != 256 * pi_buffer[1] )
279 intf_ErrMsg("vout error: unexpected end of file '%s'", psz_name );
280 free( p_font->p_data );
286 /* Computes border masks - remember that masks have the same matrix as
287 * characters, so an empty character border is required to have a complete
289 for( i_char = 0; i_char <= 255; i_char++ )
291 for( i_line = 0; i_line < pi_buffer[1]; i_line++ )
294 p_font->p_data[ (i_char + 256) * pi_buffer[1] + i_line ] =
295 ((p_font->p_data[ i_char * pi_buffer[1] + i_line ] << 1) |
296 (p_font->p_data[ i_char * pi_buffer[1] + i_line ] >> 1) |
297 (i_line > 0 ? p_font->p_data[ i_char * pi_buffer[1] + i_line - 1]: 0) |
298 (i_line < pi_buffer[1] - 1 ? p_font->p_data[ i_char * pi_buffer[1] + i_line + 1]: 0))
299 & ~p_font->p_data[ i_char * pi_buffer[1] + i_line ];
305 intf_ErrMsg("vout error: file '%s' has an unknown format", psz_name );
313 intf_DbgMsg( "loaded %s: type %d, %d-%dx%d", psz_name, p_font->i_type,
314 p_font->i_width, p_font->i_interspacing, p_font->i_height );
318 /*****************************************************************************
319 * vout_UnloadFont: unload a font
320 *****************************************************************************
321 * This function free the resources allocated by vout_LoadFont
322 *****************************************************************************/
323 void vout_UnloadFont( vout_font_t *p_font )
325 intf_DbgMsg( "vout: unloading font %p", p_font );
326 free( p_font->p_data );
330 /*****************************************************************************
331 * vout_TextSize: return the dimensions of a text
332 *****************************************************************************
333 * This function is used to align text. It returns the width and height of a
335 *****************************************************************************/
336 void vout_TextSize( vout_font_t *p_font, int i_style, const char *psz_text, int *pi_width, int *pi_height )
338 switch( p_font->i_type )
340 case VOUT_FIXED_FONT:
341 *pi_width = ((i_style & WIDE_TEXT) ? p_font->i_interspacing * 2 : p_font->i_interspacing) *
342 (strlen( psz_text ) - 1) + p_font->i_width;
343 *pi_height = p_font->i_height;
344 if( i_style & ITALIC_TEXT )
346 *pi_width = *pi_height / 3;
351 intf_DbgMsg("error: unknown font type %d", p_font->i_type );
357 /*****************************************************************************
358 * vout_Print: low level printing function
359 *****************************************************************************
360 * This function prints a text, without clipping, in a buffer using a
361 * previously loaded bitmap font.
362 *****************************************************************************/
363 void vout_Print( vout_font_t *p_font, byte_t *p_pic, int i_bytes_per_pixel, int i_bytes_per_line,
364 u32 i_char_color, u32 i_border_color, u32 i_bg_color, int i_style, const char *psz_text, int i_percent)
366 byte_t *p_char, *p_border; /* character and border mask data */
367 int i_char_mask, i_border_mask, i_bg_mask; /* masks */
368 int i_line; /* current line in character */
369 int i_byte; /* current byte in character */
370 int i_interspacing; /* offset between two chars */
371 int i_font_bytes_per_line, i_font_height; /* font properties */
372 int i_position, i_end; /* current position */
373 vout_put_byte_t *p_PutByte; /* PutByte function */
375 /* FIXME: background: can be something else that whole byte ?? */
377 /* Select output function */
378 switch( i_bytes_per_pixel )
381 p_PutByte = (vout_put_byte_t *) PutByte8;
384 p_PutByte = (vout_put_byte_t *) PutByte16;
387 p_PutByte = (vout_put_byte_t *) PutByte24;
391 p_PutByte = (vout_put_byte_t *) PutByte32;
395 /* Choose masks and copy font data to local variables */
396 i_char_mask = (i_style & VOID_TEXT) ? 0 : 0xff;
397 i_border_mask = (i_style & OUTLINED_TEXT) ? 0xff : 0;
398 i_bg_mask = (i_style & OPAQUE_TEXT) ? 0xff : 0;
400 i_font_bytes_per_line = p_font->i_bytes_per_line;
401 i_font_height = p_font->i_height;
402 i_interspacing = i_bytes_per_pixel * ((i_style & WIDE_TEXT) ?
403 p_font->i_interspacing * 2 :
404 p_font->i_interspacing);
406 /* compute where to stop... */
407 i_end = (int) (i_percent * strlen(psz_text) / 100LL);
408 if(i_end > strlen(psz_text))
409 i_end = strlen(psz_text);
413 for( i_position = 0; i_position < i_end; i_position++ ,psz_text++ )
415 /* Check that the character is valid */
416 if( (*psz_text >= p_font->i_first) && (*psz_text <= p_font->i_last) )
418 /* Select character - bytes per char is always valid, event for
420 p_char = p_font->p_data + (*psz_text - p_font->i_first) * p_font->i_bytes_per_char;
421 p_border = p_char + (p_font->i_last - p_font->i_first + 1) * p_font->i_bytes_per_char;
423 /* Select base address for output */
424 switch( p_font->i_type )
426 case VOUT_FIXED_FONT:
428 * Simple fixed width font
431 /* Italic text: shift picture start right */
432 if( i_style & ITALIC_TEXT )
434 p_pic += i_bytes_per_pixel * (p_font->i_height / 3);
437 /* Print character */
438 for( i_line = 0; i_line < i_font_height; i_line ++ )
440 for( i_byte = 0; i_byte < i_font_bytes_per_line; i_byte++, p_char++, p_border++)
443 p_PutByte( p_pic + i_bytes_per_line * i_line, i_byte,
444 *p_char & i_char_mask, *p_border & i_border_mask, i_bg_mask,
445 i_char_color, i_border_color, i_bg_color );
448 /* Italic text: shift picture start left */
449 if( (i_style & ITALIC_TEXT) && !(i_line % 3) )
451 p_pic -= i_bytes_per_pixel;
455 /* Jump to next character */
456 p_pic += i_interspacing;
460 intf_DbgMsg("error: unknown font type %d", p_font->i_type );
469 /* following functions are local */
471 /*****************************************************************************
472 * PutByte8: print a fixed width font character byte in 1 Bpp
473 *****************************************************************************/
474 static void PutByte8( u8 *p_pic, int i_byte, int i_char, int i_border,
475 int i_bg, u32 i_char_color, u32 i_border_color,
478 /* Computes position offset and background mask */
480 i_bg &= ~(i_char | i_border);
482 /* Put character bits */
483 PUT_BYTE_MASK(i_char, i_char_color);
484 PUT_BYTE_MASK(i_border, i_border_color);
485 PUT_BYTE_MASK(i_bg, i_bg_color);
488 /*****************************************************************************
489 * PutByte16: print a fixed width font character byte in 2 Bpp
490 *****************************************************************************/
491 static void PutByte16( u16 *p_pic, int i_byte, int i_char, int i_border,
492 int i_bg, u32 i_char_color, u32 i_border_color,
495 /* Computes position offset and background mask */
497 i_bg &= ~(i_char | i_border);
499 /* Put character bits */
500 PUT_BYTE_MASK(i_char, i_char_color);
501 PUT_BYTE_MASK(i_border, i_border_color);
502 PUT_BYTE_MASK(i_bg, i_bg_color);
505 /*****************************************************************************
506 * PutByte24: print a fixed width font character byte in 3 Bpp
507 *****************************************************************************/
508 static void PutByte24( void *p_pic, int i_byte, byte_t i_char, byte_t i_border, byte_t i_bg,
509 u32 i_char_color, u32 i_border_color, u32 i_bg_color )
514 /*****************************************************************************
515 * PutByte32: print a fixed width font character byte in 4 Bpp
516 *****************************************************************************/
517 static void PutByte32( u32 *p_pic, int i_byte, byte_t i_char, byte_t i_border, byte_t i_bg,
518 u32 i_char_color, u32 i_border_color, u32 i_bg_color )
520 /* Computes position offset and background mask */
522 i_bg &= ~(i_char | i_border);
524 /* Put character bits */
525 PUT_BYTE_MASK(i_char, i_char_color);
526 PUT_BYTE_MASK(i_border, i_border_color);
527 PUT_BYTE_MASK(i_bg, i_bg_color);