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)\n", 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'\n", psz_name );
227 /* Allocate font descriptor */
228 p_font = malloc( sizeof( vout_font_t ) );
231 intf_ErrMsg("vout error: %s\n", strerror(ENOMEM));
237 switch( ((u16)pi_buffer[0] << 8) | pi_buffer[1] )
239 case 0x3604: /* .psf file */
241 * PSF font: simple fixed font. Only the first 256 characters are read.
242 * Those fonts are always 1 byte wide, and 256 or 512 characters long.
245 /* Read font header - two bytes indicate the font properties */
246 if( read( i_file, pi_buffer, 2 ) != 2)
248 intf_ErrMsg("error: unexpected end of file '%s'\n", psz_name );
254 /* Copy font properties */
255 p_font->i_type = VOUT_FIXED_FONT;
257 p_font->i_height = pi_buffer[1];
258 p_font->i_interspacing = 8;
259 p_font->i_bytes_per_line = 1;
260 p_font->i_bytes_per_char = pi_buffer[1];
262 p_font->i_last = 255;
264 /* Allocate font space */
265 p_font->p_data = malloc( 2 * 256 * pi_buffer[1] );
266 if( p_font->p_data == NULL )
268 intf_ErrMsg("error: %s\n", strerror(ENOMEM));
275 if( read( i_file, p_font->p_data, 256 * pi_buffer[1] ) != 256 * pi_buffer[1] )
277 intf_ErrMsg("error: unexpected end of file '%s'\n", psz_name );
278 free( p_font->p_data );
284 /* Computes border masks - remember that masks have the same matrix as
285 * characters, so an empty character border is required to have a complete
287 for( i_char = 0; i_char <= 255; i_char++ )
289 for( i_line = 0; i_line < pi_buffer[1]; i_line++ )
292 p_font->p_data[ (i_char + 256) * pi_buffer[1] + i_line ] =
293 ((p_font->p_data[ i_char * pi_buffer[1] + i_line ] << 1) |
294 (p_font->p_data[ i_char * pi_buffer[1] + i_line ] >> 1) |
295 (i_line > 0 ? p_font->p_data[ i_char * pi_buffer[1] + i_line - 1]: 0) |
296 (i_line < pi_buffer[1] - 1 ? p_font->p_data[ i_char * pi_buffer[1] + i_line + 1]: 0))
297 & ~p_font->p_data[ i_char * pi_buffer[1] + i_line ];
303 intf_ErrMsg("error: file '%s' has an unknown format\n", psz_name );
311 intf_DbgMsg( "loaded %s: type %d, %d-%dx%d\n", psz_name, p_font->i_type,
312 p_font->i_width, p_font->i_interspacing, p_font->i_height );
316 /*****************************************************************************
317 * vout_UnloadFont: unload a font
318 *****************************************************************************
319 * This function free the resources allocated by vout_LoadFont
320 *****************************************************************************/
321 void vout_UnloadFont( vout_font_t *p_font )
323 intf_DbgMsg( "vout: unloading font %p\n", p_font );
324 free( p_font->p_data );
328 /*****************************************************************************
329 * vout_TextSize: return the dimensions of a text
330 *****************************************************************************
331 * This function is used to align text. It returns the width and height of a
333 *****************************************************************************/
334 void vout_TextSize( vout_font_t *p_font, int i_style, const char *psz_text, int *pi_width, int *pi_height )
336 switch( p_font->i_type )
338 case VOUT_FIXED_FONT:
339 *pi_width = ((i_style & WIDE_TEXT) ? p_font->i_interspacing * 2 : p_font->i_interspacing) *
340 (strlen( psz_text ) - 1) + p_font->i_width;
341 *pi_height = p_font->i_height;
342 if( i_style & ITALIC_TEXT )
344 *pi_width = *pi_height / 3;
349 intf_DbgMsg("error: unknown font type %d\n", p_font->i_type );
355 /*****************************************************************************
356 * vout_Print: low level printing function
357 *****************************************************************************
358 * This function prints a text, without clipping, in a buffer using a
359 * previously loaded bitmap font.
360 *****************************************************************************/
361 void vout_Print( vout_font_t *p_font, byte_t *p_pic, int i_bytes_per_pixel, int i_bytes_per_line,
362 u32 i_char_color, u32 i_border_color, u32 i_bg_color, int i_style, const char *psz_text )
364 byte_t *p_char, *p_border; /* character and border mask data */
365 int i_char_mask, i_border_mask, i_bg_mask; /* masks */
366 int i_line; /* current line in character */
367 int i_byte; /* current byte in character */
368 int i_interspacing; /* offset between two chars */
369 int i_font_bytes_per_line, i_font_height; /* font properties */
370 vout_put_byte_t *p_PutByte; /* PutByte function */
372 /* FIXME: background: can be something else that whole byte ?? */
374 /* Select output function */
375 switch( i_bytes_per_pixel )
378 p_PutByte = (vout_put_byte_t *) PutByte8;
381 p_PutByte = (vout_put_byte_t *) PutByte16;
384 p_PutByte = (vout_put_byte_t *) PutByte24;
388 p_PutByte = (vout_put_byte_t *) PutByte32;
392 /* Choose masks and copy font data to local variables */
393 i_char_mask = (i_style & VOID_TEXT) ? 0 : 0xff;
394 i_border_mask = (i_style & OUTLINED_TEXT) ? 0xff : 0;
395 i_bg_mask = (i_style & OPAQUE_TEXT) ? 0xff : 0;
397 i_font_bytes_per_line = p_font->i_bytes_per_line;
398 i_font_height = p_font->i_height;
399 i_interspacing = i_bytes_per_pixel * ((i_style & WIDE_TEXT) ?
400 p_font->i_interspacing * 2 :
401 p_font->i_interspacing);
404 for( ; *psz_text != '\0'; psz_text++ )
406 /* Check that the character is valid */
407 if( (*psz_text >= p_font->i_first) && (*psz_text <= p_font->i_last) )
409 /* Select character - bytes per char is always valid, event for
411 p_char = p_font->p_data + (*psz_text - p_font->i_first) * p_font->i_bytes_per_char;
412 p_border = p_char + (p_font->i_last - p_font->i_first + 1) * p_font->i_bytes_per_char;
414 /* Select base address for output */
415 switch( p_font->i_type )
417 case VOUT_FIXED_FONT:
419 * Simple fixed width font
422 /* Italic text: shift picture start right */
423 if( i_style & ITALIC_TEXT )
425 p_pic += i_bytes_per_pixel * (p_font->i_height / 3);
428 /* Print character */
429 for( i_line = 0; i_line < i_font_height; i_line ++ )
431 for( i_byte = 0; i_byte < i_font_bytes_per_line; i_byte++, p_char++, p_border++)
434 p_PutByte( p_pic + i_bytes_per_line * i_line, i_byte,
435 *p_char & i_char_mask, *p_border & i_border_mask, i_bg_mask,
436 i_char_color, i_border_color, i_bg_color );
439 /* Italic text: shift picture start left */
440 if( (i_style & ITALIC_TEXT) && !(i_line % 3) )
442 p_pic -= i_bytes_per_pixel;
446 /* Jump to next character */
447 p_pic += i_interspacing;
451 intf_DbgMsg("error: unknown font type %d\n", p_font->i_type );
459 /* following functions are local */
461 /*****************************************************************************
462 * PutByte8: print a fixed width font character byte in 1 Bpp
463 *****************************************************************************/
464 static void PutByte8( u8 *p_pic, int i_byte, int i_char, int i_border,
465 int i_bg, u32 i_char_color, u32 i_border_color,
468 /* Computes position offset and background mask */
470 i_bg &= ~(i_char | i_border);
472 /* Put character bits */
473 PUT_BYTE_MASK(i_char, i_char_color);
474 PUT_BYTE_MASK(i_border, i_border_color);
475 PUT_BYTE_MASK(i_bg, i_bg_color);
478 /*****************************************************************************
479 * PutByte16: print a fixed width font character byte in 2 Bpp
480 *****************************************************************************/
481 static void PutByte16( u16 *p_pic, int i_byte, int i_char, int i_border,
482 int i_bg, u32 i_char_color, u32 i_border_color,
485 /* Computes position offset and background mask */
487 i_bg &= ~(i_char | i_border);
489 /* Put character bits */
490 PUT_BYTE_MASK(i_char, i_char_color);
491 PUT_BYTE_MASK(i_border, i_border_color);
492 PUT_BYTE_MASK(i_bg, i_bg_color);
495 /*****************************************************************************
496 * PutByte24: print a fixed width font character byte in 3 Bpp
497 *****************************************************************************/
498 static void PutByte24( void *p_pic, int i_byte, byte_t i_char, byte_t i_border, byte_t i_bg,
499 u32 i_char_color, u32 i_border_color, u32 i_bg_color )
504 /*****************************************************************************
505 * PutByte32: print a fixed width font character byte in 4 Bpp
506 *****************************************************************************/
507 static void PutByte32( u32 *p_pic, int i_byte, byte_t i_char, byte_t i_border, byte_t i_bg,
508 u32 i_char_color, u32 i_border_color, u32 i_bg_color )
510 /* Computes position offset and background mask */
512 i_bg &= ~(i_char | i_border);
514 /* Put character bits */
515 PUT_BYTE_MASK(i_char, i_char_color);
516 PUT_BYTE_MASK(i_border, i_border_color);
517 PUT_BYTE_MASK(i_bg, i_bg_color);