]> git.sesse.net Git - vlc/blob - src/video_output/video_text.c
. normalement on devrait se prendre 1 seul mail par commit gr�ce aux
[vlc] / src / video_output / video_text.c
1 /*****************************************************************************
2  * video_text.c : text manipulation functions
3  *****************************************************************************
4  * Copyright (C) 1999, 2000 VideoLAN
5  *
6  * Authors:
7  *
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.
12  *
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.
17  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include "defs.h"
28
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() */
34
35 #include "config.h"
36 #include "common.h"
37 #include "video_text.h"
38
39 #include "intf_msg.h"
40
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
49  * border.
50  *****************************************************************************/
51 typedef struct vout_font_s
52 {
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 */
62 } vout_font_t;
63
64 /* Font types */
65 #define VOUT_FIXED_FONT       0                         /* simple fixed font */
66
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 );
76
77
78 /*****************************************************************************
79  * Macros
80  *****************************************************************************/
81
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 */ \
87 {                                                                             \
88     if( i_mask & 0xc0 )                                   /* one from 1100 */ \
89     {                                                                         \
90         if( i_mask & 0x80 )                                        /* 1000 */ \
91         {                                                                     \
92             p_pic[0] = i_mask_color;                                          \
93             if( i_mask & 0x40 )                                    /* 0100 */ \
94             {                                                                 \
95                 p_pic[1] = i_mask_color;                                      \
96             }                                                                 \
97         }                                                                     \
98         else                                        /* not 1000 means 0100 */ \
99         {                                                                     \
100             p_pic[1] = i_mask_color;                                          \
101         }                                                                     \
102         if( i_mask & 0x30 )                               /* one from 0011 */ \
103         {                                                                     \
104             if( i_mask & 0x20 )                                    /* 0010 */ \
105             {                                                                 \
106                 p_pic[2] = i_mask_color;                                      \
107                 if( i_mask & 0x10 )                                /* 0001 */ \
108                 {                                                             \
109                     p_pic[3] = i_mask_color;                                  \
110                 }                                                             \
111             }                                                                 \
112             else                                    /* not 0010 means 0001 */ \
113             {                                                                 \
114                  p_pic[3] = i_mask_color;                                     \
115             }                                                                 \
116         }                                                                     \
117     }                                                                         \
118     else                                            /* not 1100 means 0011 */ \
119     {                                                                         \
120         if( i_mask & 0x20 )                                        /* 0010 */ \
121         {                                                                     \
122             p_pic[2] = i_mask_color;                                          \
123             if( i_mask & 0x10 )                                    /* 0001 */ \
124             {                                                                 \
125                 p_pic[3] = i_mask_color;                                      \
126             }                                                                 \
127         }                                                                     \
128         else                                        /* not 0010 means 0001 */ \
129         {                                                                     \
130             p_pic[3] = i_mask_color;                                          \
131         }                                                                     \
132     }                                                                         \
133 }                                                                             \
134 if( i_mask & 0x0f )                                                           \
135 {                                                                             \
136     if( i_mask & 0x0c )                       /* one from 1100 */             \
137     {                                                                         \
138         if( i_mask & 0x08 )                                        /* 1000 */ \
139         {                                                                     \
140             p_pic[4] = i_mask_color;                                          \
141             if( i_mask & 0x04 )                                    /* 0100 */ \
142             {                                                                 \
143                 p_pic[5] = i_mask_color;                                      \
144             }                                                                 \
145         }                                                                     \
146         else                                        /* not 1000 means 0100 */ \
147         {                                                                     \
148             p_pic[5] = i_mask_color;                                          \
149         }                                                                     \
150         if( i_mask & 0x03 )                               /* one from 0011 */ \
151         {                                                                     \
152             if( i_mask & 0x02 )                                    /* 0010 */ \
153             {                                                                 \
154                 p_pic[6] = i_mask_color;                                      \
155                 if( i_mask & 0x01 )                                /* 0001 */ \
156                 {                                                             \
157                     p_pic[7] = i_mask_color;                                  \
158                 }                                                             \
159             }                                                                 \
160             else                                    /* not 0010 means 0001 */ \
161             {                                                                 \
162                  p_pic[7] = i_mask_color;                                     \
163             }                                                                 \
164         }                                                                     \
165     }                                                                         \
166     else                                            /* not 1100 means 0011 */ \
167     {                                                                         \
168         if( i_mask & 0x02 )                                        /* 0010 */ \
169         {                                                                     \
170             p_pic[6] = i_mask_color;                                          \
171             if( i_mask & 0x01 )                                    /* 0001 */ \
172             {                                                                 \
173                 p_pic[7] = i_mask_color;                                      \
174             }                                                                 \
175         }                                                                     \
176         else                                        /* not 0010 means 0001 */ \
177         {                                                                     \
178             p_pic[7] = i_mask_color;                                          \
179         }                                                                     \
180     }                                                                         \
181 }
182
183 /*****************************************************************************
184  * Local prototypes
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,
188                        u32 i_bg_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,
191                        u32 i_bg_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,
194                        u32 i_bg_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,
197                        u32 i_bg_color );
198
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
203  * NULL on error.
204  *****************************************************************************/
205 vout_font_t *vout_LoadFont( const char *psz_name )
206 {
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 */
211
212     /* Open file */
213     i_file = open( psz_name, O_RDONLY );
214     if( i_file == -1 )
215     {
216         intf_ErrMsg("error: can't open file '%s' (%s)\n", psz_name, strerror(errno));
217         return( NULL );
218     }
219
220     /* Read magick number */
221     if( read( i_file, pi_buffer, 2 ) != 2 )
222     {
223         intf_ErrMsg("error: unexpected end of file '%s'\n", psz_name );
224         close( i_file );
225         return( NULL );
226     }
227
228     /* Allocate font descriptor */
229     p_font = malloc( sizeof( vout_font_t ) );
230     if( p_font == NULL )
231     {
232         intf_ErrMsg("error: %s\n", strerror(ENOMEM));
233         close( i_file );
234         return( NULL );
235     }
236
237     /* Read file */
238     switch( ((u16)pi_buffer[0] << 8) | pi_buffer[1] )
239     {
240     case 0x3604:                                              /* .psf file */
241         /*
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.
244          */
245
246         /* Read font header - two bytes indicate the font properties */
247         if( read( i_file, pi_buffer, 2 ) != 2)
248         {
249             intf_ErrMsg("error: unexpected end of file '%s'\n", psz_name );
250             free( p_font );
251             close( i_file );
252             return( NULL );
253         }
254
255         /* Copy font properties */
256         p_font->i_type =                VOUT_FIXED_FONT;
257         p_font->i_width =               8;
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];
262         p_font->i_first =               0;
263         p_font->i_last =                255;
264
265         /* Allocate font space */
266         p_font->p_data = malloc( 2 * 256 * pi_buffer[1] );
267         if( p_font->p_data == NULL )
268         {
269             intf_ErrMsg("error: %s\n", strerror(ENOMEM));
270             free( p_font );
271             close( i_file );
272             return( NULL );
273         }
274
275         /* Copy raw data */
276         if( read( i_file, p_font->p_data, 256 * pi_buffer[1] ) != 256 * pi_buffer[1] )
277         {
278             intf_ErrMsg("error: unexpected end of file '%s'\n", psz_name );
279             free( p_font->p_data );
280             free( p_font );
281             close( i_file );
282             return( NULL );
283         }
284
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
287          * border mask. */
288         for( i_char = 0; i_char <= 255; i_char++ )
289         {
290             for( i_line = 0; i_line < pi_buffer[1]; i_line++ )
291             {
292
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 ];
299             }
300         }
301
302         break;
303     default:
304         intf_ErrMsg("error: file '%s' has an unknown format\n", psz_name );
305         free( p_font );
306         close( i_file );
307         return( NULL );
308         break;
309     }
310
311
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 );
314     return( p_font );
315 }
316
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 )
323 {
324     intf_DbgMsg( "font %p\n", p_font );
325     free( p_font->p_data );
326     free( p_font );
327 }
328
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
333  * given text.
334  *****************************************************************************/
335 void vout_TextSize( vout_font_t *p_font, int i_style, const char *psz_text, int *pi_width, int *pi_height )
336 {
337     switch( p_font->i_type )
338     {
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 )
344         {
345             *pi_width = *pi_height / 3;
346         }
347         break;
348 #ifdef DEBUG
349     default:
350         intf_DbgMsg("error: unknown font type %d\n", p_font->i_type );
351         break;
352 #endif
353     }
354 }
355
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 )
364 {
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 */
372
373     /* FIXME: background: can be something else that whole byte ?? */
374
375     /* Select output function */
376     switch( i_bytes_per_pixel )
377     {
378     case 1:
379         p_PutByte = (vout_put_byte_t *) PutByte8;
380         break;
381     case 2:
382         p_PutByte = (vout_put_byte_t *) PutByte16;
383         break;
384     case 3:
385         p_PutByte = (vout_put_byte_t *) PutByte24;
386         break;
387     case 4:
388     default:
389         p_PutByte = (vout_put_byte_t *) PutByte32;
390         break;
391     }
392
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;
397
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);
403
404     /* Print text */
405     for( ; *psz_text != '\0'; psz_text++ )
406     {
407         /* Check that the character is valid */
408         if( (*psz_text >= p_font->i_first) && (*psz_text <= p_font->i_last) )
409         {
410             /* Select character - bytes per char is always valid, event for
411              * non fixed fonts */
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;
414
415             /* Select base address for output */
416             switch( p_font->i_type )
417             {
418             case VOUT_FIXED_FONT:
419                 /*
420                  * Simple fixed width font
421                  */
422
423                 /* Italic text: shift picture start right */
424                 if( i_style & ITALIC_TEXT )
425                 {
426                     p_pic += i_bytes_per_pixel * (p_font->i_height / 3);
427                 }
428
429                 /* Print character */
430                 for( i_line = 0; i_line < i_font_height; i_line ++ )
431                 {
432                     for( i_byte = 0; i_byte < i_font_bytes_per_line; i_byte++, p_char++, p_border++)
433                     {
434                         /* Put pixels */
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 );
438                     }
439
440                     /* Italic text: shift picture start left */
441                     if( (i_style & ITALIC_TEXT) && !(i_line % 3) )
442                     {
443                         p_pic -= i_bytes_per_pixel;
444                     }
445                 }
446
447                 /* Jump to next character */
448                 p_pic += i_interspacing;
449                 break;
450 #ifdef DEBUG
451             default:
452                 intf_DbgMsg("error: unknown font type %d\n", p_font->i_type );
453                 break;
454 #endif
455             }
456         }
457     }
458 }
459
460 /* following functions are local */
461
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,
467                        u32 i_bg_color )
468 {
469     /* Computes position offset and background mask */
470     p_pic += 8 * i_byte;
471     i_bg &= ~(i_char | i_border);
472
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);
477 }
478
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,
484                        u32 i_bg_color )
485 {
486     /* Computes position offset and background mask */
487     p_pic += 8 * i_byte;
488     i_bg &= ~(i_char | i_border);
489
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);
494 }
495
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 )
501 {
502     /* XXX?? */
503 }
504
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 )
510 {
511     /* Computes position offset and background mask */
512     p_pic += 8 * i_byte;
513     i_bg &= ~(i_char | i_border);
514
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);
519 }
520