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 GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public
19 * License along with this program; if not, write to the
20 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
29 #include <errno.h> /* errno */
30 #include <stdlib.h> /* free() */
31 #include <string.h> /* strerror() */
32 #include <fcntl.h> /* open() */
33 #include <unistd.h> /* read(), close() */
37 #include "video_text.h"
41 /*****************************************************************************
42 * vout_font_t: bitmap font
43 *****************************************************************************
44 * This structure is used when the system doesn't provide a convenient function
45 * to print simple characters in a buffer.
46 * VOUT_FIXED_FONTs are stored in raw mode, character after character, with a
47 * first array of characters followed by a second array of borders masks.
48 * Therefore the border masks can't be complete if the font has pixels on the
50 *****************************************************************************/
51 typedef struct vout_font_s
53 int i_type; /* font type */
54 int i_width; /* character width in pixels */
55 int i_height; /* character height in pixels */
56 int i_interspacing; /* characters interspacing in pixels */
57 int i_bytes_per_line; /* bytes per character line */
58 int i_bytes_per_char; /* bytes per character */
59 u16 i_first; /* first character */
60 u16 i_last; /* last character */
61 byte_t * p_data; /* font character data */
65 #define VOUT_FIXED_FONT 0 /* simple fixed font */
67 /*****************************************************************************
68 * vout_put_byte_t: PutByte function
69 *****************************************************************************
70 * These functions will transform masks in a set of pixels. For each pixel,
71 * character, then border and background masks are tested, and the first
72 * encountered color is set.
73 *****************************************************************************/
74 typedef void (vout_put_byte_t)( void *p_pic, int i_byte, int i_char, int i_border,
75 int i_bg, u32 i_char_color, u32 i_border_color, u32 i_bg_color );
78 /*****************************************************************************
80 *****************************************************************************/
82 /* PUT_BYTE_MASK: put pixels from a byte-wide mask. It uses a branching tree
83 * to optimize the number of tests. It is used in the PutByte functions.
84 * This macro works for 1, 2 and 4 Bpp. */
85 #define PUT_BYTE_MASK( i_mask, i_mask_color ) \
86 if( i_mask & 0xf0 ) /* one from 1111 */ \
88 if( i_mask & 0xc0 ) /* one from 1100 */ \
90 if( i_mask & 0x80 ) /* 1000 */ \
92 p_pic[0] = i_mask_color; \
93 if( i_mask & 0x40 ) /* 0100 */ \
95 p_pic[1] = i_mask_color; \
98 else /* not 1000 means 0100 */ \
100 p_pic[1] = i_mask_color; \
102 if( i_mask & 0x30 ) /* one from 0011 */ \
104 if( i_mask & 0x20 ) /* 0010 */ \
106 p_pic[2] = i_mask_color; \
107 if( i_mask & 0x10 ) /* 0001 */ \
109 p_pic[3] = i_mask_color; \
112 else /* not 0010 means 0001 */ \
114 p_pic[3] = i_mask_color; \
118 else /* not 1100 means 0011 */ \
120 if( i_mask & 0x20 ) /* 0010 */ \
122 p_pic[2] = i_mask_color; \
123 if( i_mask & 0x10 ) /* 0001 */ \
125 p_pic[3] = i_mask_color; \
128 else /* not 0010 means 0001 */ \
130 p_pic[3] = i_mask_color; \
134 if( i_mask & 0x0f ) \
136 if( i_mask & 0x0c ) /* one from 1100 */ \
138 if( i_mask & 0x08 ) /* 1000 */ \
140 p_pic[4] = i_mask_color; \
141 if( i_mask & 0x04 ) /* 0100 */ \
143 p_pic[5] = i_mask_color; \
146 else /* not 1000 means 0100 */ \
148 p_pic[5] = i_mask_color; \
150 if( i_mask & 0x03 ) /* one from 0011 */ \
152 if( i_mask & 0x02 ) /* 0010 */ \
154 p_pic[6] = i_mask_color; \
155 if( i_mask & 0x01 ) /* 0001 */ \
157 p_pic[7] = i_mask_color; \
160 else /* not 0010 means 0001 */ \
162 p_pic[7] = i_mask_color; \
166 else /* not 1100 means 0011 */ \
168 if( i_mask & 0x02 ) /* 0010 */ \
170 p_pic[6] = i_mask_color; \
171 if( i_mask & 0x01 ) /* 0001 */ \
173 p_pic[7] = i_mask_color; \
176 else /* not 0010 means 0001 */ \
178 p_pic[7] = i_mask_color; \
183 /*****************************************************************************
185 *****************************************************************************/
186 static void PutByte8 ( u8 *p_pic, int i_byte, int i_char, int i_border,
187 int i_bg, u32 i_char_color, u32 i_border_color,
189 static void PutByte16( u16 *p_pic, int i_byte, int i_char, int i_border,
190 int i_bg, u32 i_char_color, u32 i_border_color,
192 static void PutByte24( void *p_pic, int i_byte, byte_t i_char, byte_t i_border,
193 byte_t i_bg, u32 i_char_color, u32 i_border_color,
195 static void PutByte32( u32 *p_pic, int i_byte, byte_t i_char, byte_t i_border,
196 byte_t i_bg, u32 i_char_color, u32 i_border_color,
199 /*****************************************************************************
200 * vout_LoadFont: load a bitmap font from a file
201 *****************************************************************************
202 * This function will try to open a .psf font and load it. It will return
204 *****************************************************************************/
205 vout_font_t *vout_LoadFont( const char *psz_name )
207 int i_char, i_line; /* character and line indexes */
208 int i_file; /* source file */
209 byte_t pi_buffer[2]; /* file buffer */
210 vout_font_t * p_font; /* the font itself */
213 i_file = open( psz_name, O_RDONLY );
216 intf_ErrMsg("error: can't open file '%s' (%s)\n", psz_name, strerror(errno));
220 /* Read magick number */
221 if( read( i_file, pi_buffer, 2 ) != 2 )
223 intf_ErrMsg("error: unexpected end of file '%s'\n", psz_name );
228 /* Allocate font descriptor */
229 p_font = malloc( sizeof( vout_font_t ) );
232 intf_ErrMsg("error: %s\n", strerror(ENOMEM));
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 width, 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("error: unexpected end of file '%s'\n", 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("error: %s\n", strerror(ENOMEM));
276 if( read( i_file, p_font->p_data, 256 * pi_buffer[1] ) != 256 * pi_buffer[1] )
278 intf_ErrMsg("error: unexpected end of file '%s'\n", psz_name );
279 free( p_font->p_data );
285 /* Computes border masks - remember that masks have the same matrix as
286 * characters, so an empty character border is required to have a complete
288 for( i_char = 0; i_char <= 255; i_char++ )
290 for( i_line = 0; i_line < pi_buffer[1]; i_line++ )
293 p_font->p_data[ (i_char + 256) * pi_buffer[1] + i_line ] =
294 ((p_font->p_data[ i_char * pi_buffer[1] + i_line ] << 1) |
295 (p_font->p_data[ i_char * pi_buffer[1] + i_line ] >> 1) |
296 (i_line > 0 ? p_font->p_data[ i_char * pi_buffer[1] + i_line - 1]: 0) |
297 (i_line < pi_buffer[1] - 1 ? p_font->p_data[ i_char * pi_buffer[1] + i_line + 1]: 0))
298 & ~p_font->p_data[ i_char * pi_buffer[1] + i_line ];
304 intf_ErrMsg("error: file '%s' has an unknown format\n", psz_name );
312 intf_DbgMsg( "loaded %s: type %d, %d-%dx%d\n", psz_name, p_font->i_type,
313 p_font->i_width, p_font->i_interspacing, p_font->i_height );
317 /*****************************************************************************
318 * vout_UnloadFont: unload a font
319 *****************************************************************************
320 * This function free the resources allocated by vout_LoadFont
321 *****************************************************************************/
322 void vout_UnloadFont( vout_font_t *p_font )
324 intf_DbgMsg( "font %p\n", p_font );
325 free( p_font->p_data );
329 /*****************************************************************************
330 * vout_TextSize: return the dimensions of a text
331 *****************************************************************************
332 * This function is used to align text. It returns the width and height of a
334 *****************************************************************************/
335 void vout_TextSize( vout_font_t *p_font, int i_style, const char *psz_text, int *pi_width, int *pi_height )
337 switch( p_font->i_type )
339 case VOUT_FIXED_FONT:
340 *pi_width = ((i_style & WIDE_TEXT) ? p_font->i_interspacing * 2 : p_font->i_interspacing) *
341 (strlen( psz_text ) - 1) + p_font->i_width;
342 *pi_height = p_font->i_height;
343 if( i_style & ITALIC_TEXT )
345 *pi_width = *pi_height / 3;
350 intf_DbgMsg("error: unknown font type %d\n", p_font->i_type );
356 /*****************************************************************************
357 * vout_Print: low level printing function
358 *****************************************************************************
359 * This function prints a text, without clipping, in a buffer using a
360 * previously loaded bitmap font.
361 *****************************************************************************/
362 void vout_Print( vout_font_t *p_font, byte_t *p_pic, int i_bytes_per_pixel, int i_bytes_per_line,
363 u32 i_char_color, u32 i_border_color, u32 i_bg_color, int i_style, const char *psz_text )
365 byte_t *p_char, *p_border; /* character and border mask data */
366 int i_char_mask, i_border_mask, i_bg_mask; /* masks */
367 int i_line; /* current line in character */
368 int i_byte; /* current byte in character */
369 int i_interspacing; /* offset between two chars */
370 int i_font_bytes_per_line, i_font_height; /* font properties */
371 vout_put_byte_t *p_PutByte; /* PutByte function */
373 /* FIXME: background: can be something else that whole byte ?? */
375 /* Select output function */
376 switch( i_bytes_per_pixel )
379 p_PutByte = (vout_put_byte_t *) PutByte8;
382 p_PutByte = (vout_put_byte_t *) PutByte16;
385 p_PutByte = (vout_put_byte_t *) PutByte24;
389 p_PutByte = (vout_put_byte_t *) PutByte32;
393 /* Choose masks and copy font data to local variables */
394 i_char_mask = (i_style & VOID_TEXT) ? 0 : 0xff;
395 i_border_mask = (i_style & OUTLINED_TEXT) ? 0xff : 0;
396 i_bg_mask = (i_style & OPAQUE_TEXT) ? 0xff : 0;
398 i_font_bytes_per_line = p_font->i_bytes_per_line;
399 i_font_height = p_font->i_height;
400 i_interspacing = i_bytes_per_pixel * ((i_style & WIDE_TEXT) ?
401 p_font->i_interspacing * 2 :
402 p_font->i_interspacing);
405 for( ; *psz_text != '\0'; psz_text++ )
407 /* Check that the character is valid */
408 if( (*psz_text >= p_font->i_first) && (*psz_text <= p_font->i_last) )
410 /* Select character - bytes per char is always valid, event for
412 p_char = p_font->p_data + (*psz_text - p_font->i_first) * p_font->i_bytes_per_char;
413 p_border = p_char + (p_font->i_last - p_font->i_first + 1) * p_font->i_bytes_per_char;
415 /* Select base address for output */
416 switch( p_font->i_type )
418 case VOUT_FIXED_FONT:
420 * Simple fixed width font
423 /* Italic text: shift picture start right */
424 if( i_style & ITALIC_TEXT )
426 p_pic += i_bytes_per_pixel * (p_font->i_height / 3);
429 /* Print character */
430 for( i_line = 0; i_line < i_font_height; i_line ++ )
432 for( i_byte = 0; i_byte < i_font_bytes_per_line; i_byte++, p_char++, p_border++)
435 p_PutByte( p_pic + i_bytes_per_line * i_line, i_byte,
436 *p_char & i_char_mask, *p_border & i_border_mask, i_bg_mask,
437 i_char_color, i_border_color, i_bg_color );
440 /* Italic text: shift picture start left */
441 if( (i_style & ITALIC_TEXT) && !(i_line % 3) )
443 p_pic -= i_bytes_per_pixel;
447 /* Jump to next character */
448 p_pic += i_interspacing;
452 intf_DbgMsg("error: unknown font type %d\n", p_font->i_type );
460 /* following functions are local */
462 /*****************************************************************************
463 * PutByte8: print a fixed width font character byte in 1 Bpp
464 *****************************************************************************/
465 static void PutByte8( u8 *p_pic, int i_byte, int i_char, int i_border,
466 int i_bg, u32 i_char_color, u32 i_border_color,
469 /* Computes position offset and background mask */
471 i_bg &= ~(i_char | i_border);
473 /* Put character bits */
474 PUT_BYTE_MASK(i_char, i_char_color);
475 PUT_BYTE_MASK(i_border, i_border_color);
476 PUT_BYTE_MASK(i_bg, i_bg_color);
479 /*****************************************************************************
480 * PutByte16: print a fixed width font character byte in 2 Bpp
481 *****************************************************************************/
482 static void PutByte16( u16 *p_pic, int i_byte, int i_char, int i_border,
483 int i_bg, u32 i_char_color, u32 i_border_color,
486 /* Computes position offset and background mask */
488 i_bg &= ~(i_char | i_border);
490 /* Put character bits */
491 PUT_BYTE_MASK(i_char, i_char_color);
492 PUT_BYTE_MASK(i_border, i_border_color);
493 PUT_BYTE_MASK(i_bg, i_bg_color);
496 /*****************************************************************************
497 * PutByte24: print a fixed width font character byte in 3 Bpp
498 *****************************************************************************/
499 static void PutByte24( void *p_pic, int i_byte, byte_t i_char, byte_t i_border, byte_t i_bg,
500 u32 i_char_color, u32 i_border_color, u32 i_bg_color )
505 /*****************************************************************************
506 * PutByte32: print a fixed width font character byte in 4 Bpp
507 *****************************************************************************/
508 static void PutByte32( u32 *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 )
511 /* Computes position offset and background mask */
513 i_bg &= ~(i_char | i_border);
515 /* Put character bits */
516 PUT_BYTE_MASK(i_char, i_char_color);
517 PUT_BYTE_MASK(i_border, i_border_color);
518 PUT_BYTE_MASK(i_bg, i_bg_color);