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 *****************************************************************************/
27 #include <errno.h> /* errno */
28 #include <stdlib.h> /* free() */
29 #include <string.h> /* strerror() */
30 #include <fcntl.h> /* open() */
31 #include <unistd.h> /* read(), close() */
35 #include "video_text.h"
39 /*****************************************************************************
40 * vout_font_t: bitmap font
41 *****************************************************************************
42 * This structure is used when the system doesn't provide a convenient function
43 * to print simple characters in a buffer.
44 * VOUT_FIXED_FONTs are stored in raw mode, character after character, with a
45 * first array of characters followed by a second array of borders masks.
46 * Therefore the border masks can't be complete if the font has pixels on the
48 *****************************************************************************/
49 typedef struct vout_font_s
51 int i_type; /* font type */
52 int i_width; /* character width in pixels */
53 int i_height; /* character height in pixels */
54 int i_interspacing; /* characters interspacing in pixels */
55 int i_bytes_per_line; /* bytes per character line */
56 int i_bytes_per_char; /* bytes per character */
57 u16 i_first; /* first character */
58 u16 i_last; /* last character */
59 byte_t * p_data; /* font character data */
63 #define VOUT_FIXED_FONT 0 /* simple fixed font */
65 /*****************************************************************************
66 * vout_put_byte_t: PutByte function
67 *****************************************************************************
68 * These functions will transform masks in a set of pixels. For each pixel,
69 * character, then border and background masks are tested, and the first
70 * encountered color is set.
71 *****************************************************************************/
72 typedef void (vout_put_byte_t)( void *p_pic, int i_byte, int i_char, int i_border,
73 int i_bg, u32 i_char_color, u32 i_border_color, u32 i_bg_color );
76 /*****************************************************************************
78 *****************************************************************************/
80 /* PUT_BYTE_MASK: put pixels from a byte-wide mask. It uses a branching tree
81 * to optimize the number of tests. It is used in the PutByte functions.
82 * This macro works for 1, 2 and 4 Bpp. */
83 #define PUT_BYTE_MASK( i_mask, i_mask_color ) \
84 if( i_mask & 0xf0 ) /* one from 1111 */ \
86 if( i_mask & 0xc0 ) /* one from 1100 */ \
88 if( i_mask & 0x80 ) /* 1000 */ \
90 p_pic[0] = i_mask_color; \
91 if( i_mask & 0x40 ) /* 0100 */ \
93 p_pic[1] = i_mask_color; \
96 else /* not 1000 means 0100 */ \
98 p_pic[1] = i_mask_color; \
100 if( i_mask & 0x30 ) /* one from 0011 */ \
102 if( i_mask & 0x20 ) /* 0010 */ \
104 p_pic[2] = i_mask_color; \
105 if( i_mask & 0x10 ) /* 0001 */ \
107 p_pic[3] = i_mask_color; \
110 else /* not 0010 means 0001 */ \
112 p_pic[3] = i_mask_color; \
116 else /* not 1100 means 0011 */ \
118 if( i_mask & 0x20 ) /* 0010 */ \
120 p_pic[2] = i_mask_color; \
121 if( i_mask & 0x10 ) /* 0001 */ \
123 p_pic[3] = i_mask_color; \
126 else /* not 0010 means 0001 */ \
128 p_pic[3] = i_mask_color; \
132 if( i_mask & 0x0f ) \
134 if( i_mask & 0x0c ) /* one from 1100 */ \
136 if( i_mask & 0x08 ) /* 1000 */ \
138 p_pic[4] = i_mask_color; \
139 if( i_mask & 0x04 ) /* 0100 */ \
141 p_pic[5] = i_mask_color; \
144 else /* not 1000 means 0100 */ \
146 p_pic[5] = i_mask_color; \
148 if( i_mask & 0x03 ) /* one from 0011 */ \
150 if( i_mask & 0x02 ) /* 0010 */ \
152 p_pic[6] = i_mask_color; \
153 if( i_mask & 0x01 ) /* 0001 */ \
155 p_pic[7] = i_mask_color; \
158 else /* not 0010 means 0001 */ \
160 p_pic[7] = i_mask_color; \
164 else /* not 1100 means 0011 */ \
166 if( i_mask & 0x02 ) /* 0010 */ \
168 p_pic[6] = i_mask_color; \
169 if( i_mask & 0x01 ) /* 0001 */ \
171 p_pic[7] = i_mask_color; \
174 else /* not 0010 means 0001 */ \
176 p_pic[7] = i_mask_color; \
181 /*****************************************************************************
183 *****************************************************************************/
184 static void PutByte8 ( u8 *p_pic, int i_byte, int i_char, int i_border,
185 int i_bg, u32 i_char_color, u32 i_border_color,
187 static void PutByte16( u16 *p_pic, int i_byte, int i_char, int i_border,
188 int i_bg, u32 i_char_color, u32 i_border_color,
190 static void PutByte24( void *p_pic, int i_byte, byte_t i_char, byte_t i_border,
191 byte_t i_bg, u32 i_char_color, u32 i_border_color,
193 static void PutByte32( u32 *p_pic, int i_byte, byte_t i_char, byte_t i_border,
194 byte_t i_bg, u32 i_char_color, u32 i_border_color,
197 /*****************************************************************************
198 * vout_LoadFont: load a bitmap font from a file
199 *****************************************************************************
200 * This function will try to open a .psf font and load it. It will return
202 *****************************************************************************/
203 vout_font_t *vout_LoadFont( const char *psz_name )
205 int i_char, i_line; /* character and line indexes */
206 int i_file; /* source file */
207 byte_t pi_buffer[2]; /* file buffer */
208 vout_font_t * p_font; /* the font itself */
211 i_file = open( psz_name, O_RDONLY );
214 intf_ErrMsg("error: can't open file '%s' (%s)\n", psz_name, strerror(errno));
218 /* Read magick number */
219 if( read( i_file, pi_buffer, 2 ) != 2 )
221 intf_ErrMsg("error: unexpected end of file '%s'\n", psz_name );
226 /* Allocate font descriptor */
227 p_font = malloc( sizeof( vout_font_t ) );
230 intf_ErrMsg("error: %s\n", strerror(ENOMEM));
236 switch( ((u16)pi_buffer[0] << 8) | pi_buffer[1] )
238 case 0x3604: /* .psf file */
240 * PSF font: simple fixed font. Only the first 256 characters are read.
241 * Those fonts are always 1 byte width, and 256 or 512 characters long.
244 /* Read font header - two bytes indicate the font properties */
245 if( read( i_file, pi_buffer, 2 ) != 2)
247 intf_ErrMsg("error: unexpected end of file '%s'\n", psz_name );
253 /* Copy font properties */
254 p_font->i_type = VOUT_FIXED_FONT;
256 p_font->i_height = pi_buffer[1];
257 p_font->i_interspacing = 8;
258 p_font->i_bytes_per_line = 1;
259 p_font->i_bytes_per_char = pi_buffer[1];
261 p_font->i_last = 255;
263 /* Allocate font space */
264 p_font->p_data = malloc( 2 * 256 * pi_buffer[1] );
265 if( p_font->p_data == NULL )
267 intf_ErrMsg("error: %s\n", strerror(ENOMEM));
274 if( read( i_file, p_font->p_data, 256 * pi_buffer[1] ) != 256 * pi_buffer[1] )
276 intf_ErrMsg("error: unexpected end of file '%s'\n", psz_name );
277 free( p_font->p_data );
283 /* Computes border masks - remember that masks have the same matrix as
284 * characters, so an empty character border is required to have a complete
286 for( i_char = 0; i_char <= 255; i_char++ )
288 for( i_line = 0; i_line < pi_buffer[1]; i_line++ )
291 p_font->p_data[ (i_char + 256) * pi_buffer[1] + i_line ] =
292 ((p_font->p_data[ i_char * pi_buffer[1] + i_line ] << 1) |
293 (p_font->p_data[ i_char * pi_buffer[1] + i_line ] >> 1) |
294 (i_line > 0 ? p_font->p_data[ i_char * pi_buffer[1] + i_line - 1]: 0) |
295 (i_line < pi_buffer[1] - 1 ? p_font->p_data[ i_char * pi_buffer[1] + i_line + 1]: 0))
296 & ~p_font->p_data[ i_char * pi_buffer[1] + i_line ];
302 intf_ErrMsg("error: file '%s' has an unknown format\n", psz_name );
310 intf_DbgMsg( "loaded %s: type %d, %d-%dx%d\n", psz_name, p_font->i_type,
311 p_font->i_width, p_font->i_interspacing, p_font->i_height );
315 /*****************************************************************************
316 * vout_UnloadFont: unload a font
317 *****************************************************************************
318 * This function free the resources allocated by vout_LoadFont
319 *****************************************************************************/
320 void vout_UnloadFont( vout_font_t *p_font )
322 intf_DbgMsg( "font %p\n", p_font );
323 free( p_font->p_data );
327 /*****************************************************************************
328 * vout_TextSize: return the dimensions of a text
329 *****************************************************************************
330 * This function is used to align text. It returns the width and height of a
332 *****************************************************************************/
333 void vout_TextSize( vout_font_t *p_font, int i_style, const char *psz_text, int *pi_width, int *pi_height )
335 switch( p_font->i_type )
337 case VOUT_FIXED_FONT:
338 *pi_width = ((i_style & WIDE_TEXT) ? p_font->i_interspacing * 2 : p_font->i_interspacing) *
339 (strlen( psz_text ) - 1) + p_font->i_width;
340 *pi_height = p_font->i_height;
341 if( i_style & ITALIC_TEXT )
343 *pi_width = *pi_height / 3;
348 intf_DbgMsg("error: unknown font type %d\n", p_font->i_type );
354 /*****************************************************************************
355 * vout_Print: low level printing function
356 *****************************************************************************
357 * This function prints a text, without clipping, in a buffer using a
358 * previously loaded bitmap font.
359 *****************************************************************************/
360 void vout_Print( vout_font_t *p_font, byte_t *p_pic, int i_bytes_per_pixel, int i_bytes_per_line,
361 u32 i_char_color, u32 i_border_color, u32 i_bg_color, int i_style, const char *psz_text )
363 byte_t *p_char, *p_border; /* character and border mask data */
364 int i_char_mask, i_border_mask, i_bg_mask; /* masks */
365 int i_line; /* current line in character */
366 int i_byte; /* current byte in character */
367 int i_interspacing; /* offset between two chars */
368 int i_font_bytes_per_line, i_font_height; /* font properties */
369 vout_put_byte_t *p_PutByte; /* PutByte function */
371 /* FIXME: background: can be something else that whole byte ?? */
373 /* Select output function */
374 switch( i_bytes_per_pixel )
377 p_PutByte = (vout_put_byte_t *) PutByte8;
380 p_PutByte = (vout_put_byte_t *) PutByte16;
383 p_PutByte = (vout_put_byte_t *) PutByte24;
387 p_PutByte = (vout_put_byte_t *) PutByte32;
391 /* Choose masks and copy font data to local variables */
392 i_char_mask = (i_style & VOID_TEXT) ? 0 : 0xff;
393 i_border_mask = (i_style & OUTLINED_TEXT) ? 0xff : 0;
394 i_bg_mask = (i_style & OPAQUE_TEXT) ? 0xff : 0;
396 i_font_bytes_per_line = p_font->i_bytes_per_line;
397 i_font_height = p_font->i_height;
398 i_interspacing = i_bytes_per_pixel * ((i_style & WIDE_TEXT) ?
399 p_font->i_interspacing * 2 :
400 p_font->i_interspacing);
403 for( ; *psz_text != '\0'; psz_text++ )
405 /* Check that the character is valid */
406 if( (*psz_text >= p_font->i_first) && (*psz_text <= p_font->i_last) )
408 /* Select character - bytes per char is always valid, event for
410 p_char = p_font->p_data + (*psz_text - p_font->i_first) * p_font->i_bytes_per_char;
411 p_border = p_char + (p_font->i_last - p_font->i_first + 1) * p_font->i_bytes_per_char;
413 /* Select base address for output */
414 switch( p_font->i_type )
416 case VOUT_FIXED_FONT:
418 * Simple fixed width font
421 /* Italic text: shift picture start right */
422 if( i_style & ITALIC_TEXT )
424 p_pic += i_bytes_per_pixel * (p_font->i_height / 3);
427 /* Print character */
428 for( i_line = 0; i_line < i_font_height; i_line ++ )
430 for( i_byte = 0; i_byte < i_font_bytes_per_line; i_byte++, p_char++, p_border++)
433 p_PutByte( p_pic + i_bytes_per_line * i_line, i_byte,
434 *p_char & i_char_mask, *p_border & i_border_mask, i_bg_mask,
435 i_char_color, i_border_color, i_bg_color );
438 /* Italic text: shift picture start left */
439 if( (i_style & ITALIC_TEXT) && !(i_line % 3) )
441 p_pic -= i_bytes_per_pixel;
445 /* Jump to next character */
446 p_pic += i_interspacing;
450 intf_DbgMsg("error: unknown font type %d\n", p_font->i_type );
458 /* following functions are local */
460 /*****************************************************************************
461 * PutByte8: print a fixed width font character byte in 1 Bpp
462 *****************************************************************************/
463 static void PutByte8( u8 *p_pic, int i_byte, int i_char, int i_border,
464 int i_bg, u32 i_char_color, u32 i_border_color,
467 /* Computes position offset and background mask */
469 i_bg &= ~(i_char | i_border);
471 /* Put character bits */
472 PUT_BYTE_MASK(i_char, i_char_color);
473 PUT_BYTE_MASK(i_border, i_border_color);
474 PUT_BYTE_MASK(i_bg, i_bg_color);
477 /*****************************************************************************
478 * PutByte16: print a fixed width font character byte in 2 Bpp
479 *****************************************************************************/
480 static void PutByte16( u16 *p_pic, int i_byte, int i_char, int i_border,
481 int i_bg, u32 i_char_color, u32 i_border_color,
484 /* Computes position offset and background mask */
486 i_bg &= ~(i_char | i_border);
488 /* Put character bits */
489 PUT_BYTE_MASK(i_char, i_char_color);
490 PUT_BYTE_MASK(i_border, i_border_color);
491 PUT_BYTE_MASK(i_bg, i_bg_color);
494 /*****************************************************************************
495 * PutByte24: print a fixed width font character byte in 3 Bpp
496 *****************************************************************************/
497 static void PutByte24( void *p_pic, int i_byte, byte_t i_char, byte_t i_border, byte_t i_bg,
498 u32 i_char_color, u32 i_border_color, u32 i_bg_color )
503 /*****************************************************************************
504 * PutByte32: print a fixed width font character byte in 4 Bpp
505 *****************************************************************************/
506 static void PutByte32( u32 *p_pic, int i_byte, byte_t i_char, byte_t i_border, byte_t i_bg,
507 u32 i_char_color, u32 i_border_color, u32 i_bg_color )
509 /* Computes position offset and background mask */
511 i_bg &= ~(i_char | i_border);
513 /* Put character bits */
514 PUT_BYTE_MASK(i_char, i_char_color);
515 PUT_BYTE_MASK(i_border, i_border_color);
516 PUT_BYTE_MASK(i_bg, i_bg_color);