1 /*******************************************************************************
2 * video_text.c : text manipulation functions
4 *******************************************************************************/
6 /*******************************************************************************
8 *******************************************************************************/
18 #include "video_text.h"
21 /*******************************************************************************
22 * vout_font_t: bitmap font
23 *******************************************************************************
24 * This structure is used when the system doesn't provide a convenient function
25 * to print simple characters in a buffer.
26 * VOUT_FIXED_FONTs are stored in raw mode, character after character, with a
27 * first array of characters followed by a second array of borders masks.
28 * Therefore the border masks can't be complete if the font has pixels on the
30 *******************************************************************************/
31 typedef struct vout_font_s
33 int i_type; /* font type */
34 int i_width; /* character width in pixels */
35 int i_height; /* character height in pixels */
36 int i_interspacing; /* characters interspacing in pixels */
37 int i_bytes_per_line; /* bytes per character line */
38 int i_bytes_per_char; /* bytes per character */
39 u16 i_first; /* first character */
40 u16 i_last; /* last character */
41 byte_t * p_data; /* font character data */
45 #define VOUT_FIXED_FONT 0 /* simple fixed font */
47 /*******************************************************************************
48 * vout_put_byte_t: PutByte function
49 *******************************************************************************
50 * These functions will transform masks in a set of pixels. For each pixel,
51 * character, then border and background masks are tested, and the first
52 * encountered color is set.
53 *******************************************************************************/
54 typedef void (vout_put_byte_t)( void *p_pic, int i_byte, int i_char, int i_border,
55 int i_bg, u32 i_char_color, u32 i_border_color, u32 i_bg_color );
58 /*******************************************************************************
60 *******************************************************************************/
62 /* PUT_BYTE_MASK: put pixels from a byte-wide mask. It uses a branching tree
63 * to optimize the number of tests. It is used in the PutByte functions.
64 * This macro works for 1, 2 and 4 Bpp. */
65 #define PUT_BYTE_MASK( i_mask, i_mask_color ) \
66 if( i_mask & 0xf0 ) /* one from 1111 */ \
68 if( i_mask & 0xc0 ) /* one from 1100 */ \
70 if( i_mask & 0x80 ) /* 1000 */ \
72 p_pic[0] = i_mask_color; \
73 if( i_mask & 0x40 ) /* 0100 */ \
75 p_pic[1] = i_mask_color; \
78 else /* not 1000 means 0100 */ \
80 p_pic[1] = i_mask_color; \
82 if( i_mask & 0x30 ) /* one from 0011 */ \
84 if( i_mask & 0x20 ) /* 0010 */ \
86 p_pic[2] = i_mask_color; \
87 if( i_mask & 0x10 ) /* 0001 */ \
89 p_pic[3] = i_mask_color; \
92 else /* not 0010 means 0001 */ \
94 p_pic[3] = i_mask_color; \
98 else /* not 1100 means 0011 */ \
100 if( i_mask & 0x20 ) /* 0010 */ \
102 p_pic[2] = i_mask_color; \
103 if( i_mask & 0x10 ) /* 0001 */ \
105 p_pic[3] = i_mask_color; \
108 else /* not 0010 means 0001 */ \
110 p_pic[3] = i_mask_color; \
114 if( i_mask & 0x0f ) \
116 if( i_mask & 0x0c ) /* one from 1100 */ \
118 if( i_mask & 0x08 ) /* 1000 */ \
120 p_pic[4] = i_mask_color; \
121 if( i_mask & 0x04 ) /* 0100 */ \
123 p_pic[5] = i_mask_color; \
126 else /* not 1000 means 0100 */ \
128 p_pic[5] = i_mask_color; \
130 if( i_mask & 0x03 ) /* one from 0011 */ \
132 if( i_mask & 0x02 ) /* 0010 */ \
134 p_pic[6] = i_mask_color; \
135 if( i_mask & 0x01 ) /* 0001 */ \
137 p_pic[7] = i_mask_color; \
140 else /* not 0010 means 0001 */ \
142 p_pic[7] = i_mask_color; \
146 else /* not 1100 means 0011 */ \
148 if( i_mask & 0x02 ) /* 0010 */ \
150 p_pic[6] = i_mask_color; \
151 if( i_mask & 0x01 ) /* 0001 */ \
153 p_pic[7] = i_mask_color; \
156 else /* not 0010 means 0001 */ \
158 p_pic[7] = i_mask_color; \
163 /*****************************************************************************
165 *****************************************************************************/
166 static void PutByte8 ( u8 *p_pic, int i_byte, int i_char, int i_border,
167 int i_bg, u32 i_char_color, u32 i_border_color,
169 static void PutByte16( u16 *p_pic, int i_byte, int i_char, int i_border,
170 int i_bg, u32 i_char_color, u32 i_border_color,
172 static void PutByte24( void *p_pic, int i_byte, byte_t i_char, byte_t i_border,
173 byte_t i_bg, u32 i_char_color, u32 i_border_color,
175 static void PutByte32( u32 *p_pic, int i_byte, byte_t i_char, byte_t i_border,
176 byte_t i_bg, u32 i_char_color, u32 i_border_color,
179 /*****************************************************************************
180 * vout_LoadFont: load a bitmap font from a file
181 *****************************************************************************
182 * This function will try to open a .psf font and load it. It will return
184 *****************************************************************************/
185 vout_font_t *vout_LoadFont( const char *psz_name )
187 int i_char, i_line; /* character and line indexes */
188 int i_file; /* source file */
189 byte_t pi_buffer[2]; /* file buffer */
190 vout_font_t * p_font; /* the font itself */
193 i_file = open( psz_name, O_RDONLY );
196 intf_ErrMsg("error: can't open file '%s' (%s)\n", psz_name, strerror(errno));
200 /* Read magick number */
201 if( read( i_file, pi_buffer, 2 ) != 2 )
203 intf_ErrMsg("error: unexpected end of file '%s'\n", psz_name );
208 /* Allocate font descriptor */
209 p_font = malloc( sizeof( vout_font_t ) );
212 intf_ErrMsg("error: %s\n", strerror(ENOMEM));
218 switch( ((u16)pi_buffer[0] << 8) | pi_buffer[1] )
220 case 0x3604: /* .psf file */
222 * PSF font: simple fixed font. Only the first 256 characters are read.
223 * Those fonts are always 1 byte width, and 256 or 512 characters long.
226 /* Read font header - two bytes indicate the font properties */
227 if( read( i_file, pi_buffer, 2 ) != 2)
229 intf_ErrMsg("error: unexpected end of file '%s'\n", psz_name );
235 /* Copy font properties */
236 p_font->i_type = VOUT_FIXED_FONT;
238 p_font->i_height = pi_buffer[1];
239 p_font->i_interspacing = 8;
240 p_font->i_bytes_per_line = 1;
241 p_font->i_bytes_per_char = pi_buffer[1];
243 p_font->i_last = 255;
245 /* Allocate font space */
246 p_font->p_data = malloc( 2 * 256 * pi_buffer[1] );
247 if( p_font->p_data == NULL )
249 intf_ErrMsg("error: %s\n", strerror(ENOMEM));
256 if( read( i_file, p_font->p_data, 256 * pi_buffer[1] ) != 256 * pi_buffer[1] )
258 intf_ErrMsg("error: unexpected end of file '%s'\n", psz_name );
259 free( p_font->p_data );
265 /* Computes border masks - remember that masks have the same matrix as
266 * characters, so an empty character border is required to have a complete
268 for( i_char = 0; i_char <= 255; i_char++ )
270 for( i_line = 0; i_line < pi_buffer[1]; i_line++ )
273 p_font->p_data[ (i_char + 256) * pi_buffer[1] + i_line ] =
274 ((p_font->p_data[ i_char * pi_buffer[1] + i_line ] << 1) |
275 (p_font->p_data[ i_char * pi_buffer[1] + i_line ] >> 1) |
276 (i_line > 0 ? p_font->p_data[ i_char * pi_buffer[1] + i_line - 1]: 0) |
277 (i_line < pi_buffer[1] - 1 ? p_font->p_data[ i_char * pi_buffer[1] + i_line + 1]: 0))
278 & ~p_font->p_data[ i_char * pi_buffer[1] + i_line ];
284 intf_ErrMsg("error: file '%s' has an unknown format\n", psz_name );
292 intf_DbgMsg( "loaded %s: type %d, %d-%dx%d\n", psz_name, p_font->i_type,
293 p_font->i_width, p_font->i_interspacing, p_font->i_height );
297 /*******************************************************************************
298 * vout_UnloadFont: unload a font
299 *******************************************************************************
300 * This function free the resources allocated by vout_LoadFont
301 *******************************************************************************/
302 void vout_UnloadFont( vout_font_t *p_font )
304 intf_DbgMsg( "font %p\n", p_font );
305 free( p_font->p_data );
309 /*******************************************************************************
310 * vout_TextSize: return the dimensions of a text
311 *******************************************************************************
312 * This function is used to align text. It returns the width and height of a
314 *******************************************************************************/
315 void vout_TextSize( vout_font_t *p_font, int i_style, const char *psz_text, int *pi_width, int *pi_height )
317 switch( p_font->i_type )
319 case VOUT_FIXED_FONT:
320 *pi_width = ((i_style & WIDE_TEXT) ? p_font->i_interspacing * 2 : p_font->i_interspacing) *
321 (strlen( psz_text ) - 1) + p_font->i_width;
322 *pi_height = p_font->i_height;
323 if( i_style & ITALIC_TEXT )
325 *pi_width = *pi_height / 3;
330 intf_DbgMsg("error: unknown font type %d\n", p_font->i_type );
336 /*******************************************************************************
337 * vout_Print: low level printing function
338 *******************************************************************************
339 * This function prints a text, without clipping, in a buffer using a previously
340 * loaded bitmap font.
341 *******************************************************************************/
342 void vout_Print( vout_font_t *p_font, byte_t *p_pic, int i_bytes_per_pixel, int i_bytes_per_line,
343 u32 i_char_color, u32 i_border_color, u32 i_bg_color, int i_style, const char *psz_text )
345 byte_t *p_char, *p_border; /* character and border mask data */
346 int i_char_mask, i_border_mask, i_bg_mask; /* masks */
347 int i_line; /* current line in character */
348 int i_byte; /* current byte in character */
349 int i_interspacing; /* offset between two chars */
350 int i_font_bytes_per_line, i_font_height; /* font properties */
351 vout_put_byte_t *p_PutByte; /* PutByte function */
353 //?? background: can be something else that whole byte
355 /* Select output function */
356 switch( i_bytes_per_pixel )
359 p_PutByte = (vout_put_byte_t *) PutByte8;
362 p_PutByte = (vout_put_byte_t *) PutByte16;
365 p_PutByte = (vout_put_byte_t *) PutByte24;
369 p_PutByte = (vout_put_byte_t *) PutByte32;
373 /* Choose masks and copy font data to local variables */
374 i_char_mask = (i_style & VOID_TEXT) ? 0 : 0xff;
375 i_border_mask = (i_style & OUTLINED_TEXT) ? 0xff : 0;
376 i_bg_mask = (i_style & OPAQUE_TEXT) ? 0xff : 0;
378 i_font_bytes_per_line = p_font->i_bytes_per_line;
379 i_font_height = p_font->i_height;
380 i_interspacing = i_bytes_per_pixel * ((i_style & WIDE_TEXT) ?
381 p_font->i_interspacing * 2 :
382 p_font->i_interspacing);
385 for( ; *psz_text != '\0'; psz_text++ )
387 /* Check that the character is valid */
388 if( (*psz_text >= p_font->i_first) && (*psz_text <= p_font->i_last) )
390 /* Select character - bytes per char is always valid, event for
392 p_char = p_font->p_data + (*psz_text - p_font->i_first) * p_font->i_bytes_per_char;
393 p_border = p_char + (p_font->i_last - p_font->i_first + 1) * p_font->i_bytes_per_char;
395 /* Select base address for output */
396 switch( p_font->i_type )
398 case VOUT_FIXED_FONT:
400 * Simple fixed width font
403 /* Italic text: shift picture start right */
404 if( i_style & ITALIC_TEXT )
406 p_pic += i_bytes_per_pixel * (p_font->i_height / 3);
409 /* Print character */
410 for( i_line = 0; i_line < i_font_height; i_line ++ )
412 for( i_byte = 0; i_byte < i_font_bytes_per_line; i_byte++, p_char++, p_border++)
415 p_PutByte( p_pic + i_bytes_per_line * i_line, i_byte,
416 *p_char & i_char_mask, *p_border & i_border_mask, i_bg_mask,
417 i_char_color, i_border_color, i_bg_color );
420 /* Italic text: shift picture start left */
421 if( (i_style & ITALIC_TEXT) && !(i_line % 3) )
423 p_pic -= i_bytes_per_pixel;
427 /* Jump to next character */
428 p_pic += i_interspacing;
432 intf_DbgMsg("error: unknown font type %d\n", p_font->i_type );
440 /* following functions are local */
442 /*****************************************************************************
443 * PutByte8: print a fixed width font character byte in 1 Bpp
444 *****************************************************************************/
445 static void PutByte8( u8 *p_pic, int i_byte, int i_char, int i_border,
446 int i_bg, u32 i_char_color, u32 i_border_color,
449 /* Computes position offset and background mask */
451 i_bg &= ~(i_char | i_border);
453 /* Put character bits */
454 PUT_BYTE_MASK(i_char, i_char_color);
455 PUT_BYTE_MASK(i_border, i_border_color);
456 PUT_BYTE_MASK(i_bg, i_bg_color);
459 /*****************************************************************************
460 * PutByte16: print a fixed width font character byte in 2 Bpp
461 *****************************************************************************/
462 static void PutByte16( u16 *p_pic, int i_byte, int i_char, int i_border,
463 int i_bg, u32 i_char_color, u32 i_border_color,
466 /* Computes position offset and background mask */
468 i_bg &= ~(i_char | i_border);
470 /* Put character bits */
471 PUT_BYTE_MASK(i_char, i_char_color);
472 PUT_BYTE_MASK(i_border, i_border_color);
473 PUT_BYTE_MASK(i_bg, i_bg_color);
476 /*****************************************************************************
477 * PutByte24: print a fixed width font character byte in 3 Bpp
478 *****************************************************************************/
479 static void PutByte24( void *p_pic, int i_byte, byte_t i_char, byte_t i_border, byte_t i_bg,
480 u32 i_char_color, u32 i_border_color, u32 i_bg_color )
485 /*****************************************************************************
486 * PutByte32: print a fixed width font character byte in 4 Bpp
487 *****************************************************************************/
488 static void PutByte32( u32 *p_pic, int i_byte, byte_t i_char, byte_t i_border, byte_t i_bg,
489 u32 i_char_color, u32 i_border_color, u32 i_bg_color )
491 /* Computes position offset and background mask */
493 i_bg &= ~(i_char | i_border);
495 /* Put character bits */
496 PUT_BYTE_MASK(i_char, i_char_color);
497 PUT_BYTE_MASK(i_border, i_border_color);
498 PUT_BYTE_MASK(i_bg, i_bg_color);