]> git.sesse.net Git - vlc/blob - src/video_output/video_text.c
Le retour du demoronifier qui n'est pas content.
[vlc] / src / video_output / video_text.c
1 /*****************************************************************************
2  * video_text.c : text manipulation functions
3  * (c)1999 VideoLAN
4  *****************************************************************************/
5
6 /*****************************************************************************
7  * Preamble
8  *****************************************************************************/
9 #include <errno.h>
10 #include <fcntl.h>
11 #include <stdlib.h>
12 #include <stdio.h>
13 #include <string.h>
14 #include <unistd.h>
15
16 #include "common.h"
17 #include "config.h"
18 #include "video_text.h"
19 #include "intf_msg.h"
20
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
29  * border.
30  *****************************************************************************/
31 typedef struct vout_font_s
32 {
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 */
42 } vout_font_t;
43
44 /* Font types */
45 #define VOUT_FIXED_FONT       0                         /* simple fixed font */
46
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 );
56
57
58 /*****************************************************************************
59  * Macros
60  *****************************************************************************/
61
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 */ \
67 {                                                                             \
68     if( i_mask & 0xc0 )                                   /* one from 1100 */ \
69     {                                                                         \
70         if( i_mask & 0x80 )                                        /* 1000 */ \
71         {                                                                     \
72             p_pic[0] = i_mask_color;                                          \
73             if( i_mask & 0x40 )                                    /* 0100 */ \
74             {                                                                 \
75                 p_pic[1] = i_mask_color;                                      \
76             }                                                                 \
77         }                                                                     \
78         else                                        /* not 1000 means 0100 */ \
79         {                                                                     \
80             p_pic[1] = i_mask_color;                                          \
81         }                                                                     \
82         if( i_mask & 0x30 )                               /* one from 0011 */ \
83         {                                                                     \
84             if( i_mask & 0x20 )                                    /* 0010 */ \
85             {                                                                 \
86                 p_pic[2] = i_mask_color;                                      \
87                 if( i_mask & 0x10 )                                /* 0001 */ \
88                 {                                                             \
89                     p_pic[3] = i_mask_color;                                  \
90                 }                                                             \
91             }                                                                 \
92             else                                    /* not 0010 means 0001 */ \
93             {                                                                 \
94                  p_pic[3] = i_mask_color;                                     \
95             }                                                                 \
96         }                                                                     \
97     }                                                                         \
98     else                                            /* not 1100 means 0011 */ \
99     {                                                                         \
100         if( i_mask & 0x20 )                                        /* 0010 */ \
101         {                                                                     \
102             p_pic[2] = i_mask_color;                                          \
103             if( i_mask & 0x10 )                                    /* 0001 */ \
104             {                                                                 \
105                 p_pic[3] = i_mask_color;                                      \
106             }                                                                 \
107         }                                                                     \
108         else                                        /* not 0010 means 0001 */ \
109         {                                                                     \
110             p_pic[3] = i_mask_color;                                          \
111         }                                                                     \
112     }                                                                         \
113 }                                                                             \
114 if( i_mask & 0x0f )                                                           \
115 {                                                                             \
116     if( i_mask & 0x0c )                       /* one from 1100 */             \
117     {                                                                         \
118         if( i_mask & 0x08 )                                        /* 1000 */ \
119         {                                                                     \
120             p_pic[4] = i_mask_color;                                          \
121             if( i_mask & 0x04 )                                    /* 0100 */ \
122             {                                                                 \
123                 p_pic[5] = i_mask_color;                                      \
124             }                                                                 \
125         }                                                                     \
126         else                                        /* not 1000 means 0100 */ \
127         {                                                                     \
128             p_pic[5] = i_mask_color;                                          \
129         }                                                                     \
130         if( i_mask & 0x03 )                               /* one from 0011 */ \
131         {                                                                     \
132             if( i_mask & 0x02 )                                    /* 0010 */ \
133             {                                                                 \
134                 p_pic[6] = i_mask_color;                                      \
135                 if( i_mask & 0x01 )                                /* 0001 */ \
136                 {                                                             \
137                     p_pic[7] = i_mask_color;                                  \
138                 }                                                             \
139             }                                                                 \
140             else                                    /* not 0010 means 0001 */ \
141             {                                                                 \
142                  p_pic[7] = i_mask_color;                                     \
143             }                                                                 \
144         }                                                                     \
145     }                                                                         \
146     else                                            /* not 1100 means 0011 */ \
147     {                                                                         \
148         if( i_mask & 0x02 )                                        /* 0010 */ \
149         {                                                                     \
150             p_pic[6] = i_mask_color;                                          \
151             if( i_mask & 0x01 )                                    /* 0001 */ \
152             {                                                                 \
153                 p_pic[7] = i_mask_color;                                      \
154             }                                                                 \
155         }                                                                     \
156         else                                        /* not 0010 means 0001 */ \
157         {                                                                     \
158             p_pic[7] = i_mask_color;                                          \
159         }                                                                     \
160     }                                                                         \
161 }
162
163 /*****************************************************************************
164  * Local prototypes
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,
168                        u32 i_bg_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,
171                        u32 i_bg_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,
174                        u32 i_bg_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,
177                        u32 i_bg_color );
178
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
183  * NULL on error.
184  *****************************************************************************/
185 vout_font_t *vout_LoadFont( const char *psz_name )
186 {
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 */
191
192     /* Open file */
193     i_file = open( psz_name, O_RDONLY );
194     if( i_file == -1 )
195     {
196         intf_ErrMsg("error: can't open file '%s' (%s)\n", psz_name, strerror(errno));
197         return( NULL );
198     }
199
200     /* Read magick number */
201     if( read( i_file, pi_buffer, 2 ) != 2 )
202     {
203         intf_ErrMsg("error: unexpected end of file '%s'\n", psz_name );
204         close( i_file );
205         return( NULL );
206     }
207
208     /* Allocate font descriptor */
209     p_font = malloc( sizeof( vout_font_t ) );
210     if( p_font == NULL )
211     {
212         intf_ErrMsg("error: %s\n", strerror(ENOMEM));
213         close( i_file );
214         return( NULL );
215     }
216
217     /* Read file */
218     switch( ((u16)pi_buffer[0] << 8) | pi_buffer[1] )
219     {
220     case 0x3604:                                              /* .psf file */
221         /*
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.
224          */
225
226         /* Read font header - two bytes indicate the font properties */
227         if( read( i_file, pi_buffer, 2 ) != 2)
228         {
229             intf_ErrMsg("error: unexpected end of file '%s'\n", psz_name );
230             free( p_font );
231             close( i_file );
232             return( NULL );
233         }
234
235         /* Copy font properties */
236         p_font->i_type =                VOUT_FIXED_FONT;
237         p_font->i_width =               8;
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];
242         p_font->i_first =               0;
243         p_font->i_last =                255;
244
245         /* Allocate font space */
246         p_font->p_data = malloc( 2 * 256 * pi_buffer[1] );
247         if( p_font->p_data == NULL )
248         {
249             intf_ErrMsg("error: %s\n", strerror(ENOMEM));
250             free( p_font );
251             close( i_file );
252             return( NULL );
253         }
254
255         /* Copy raw data */
256         if( read( i_file, p_font->p_data, 256 * pi_buffer[1] ) != 256 * pi_buffer[1] )
257         {
258             intf_ErrMsg("error: unexpected end of file '%s'\n", psz_name );
259             free( p_font->p_data );
260             free( p_font );
261             close( i_file );
262             return( NULL );
263         }
264
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
267          * border mask. */
268         for( i_char = 0; i_char <= 255; i_char++ )
269         {
270             for( i_line = 0; i_line < pi_buffer[1]; i_line++ )
271             {
272
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 ];
279             }
280         }
281
282         break;
283     default:
284         intf_ErrMsg("error: file '%s' has an unknown format\n", psz_name );
285         free( p_font );
286         close( i_file );
287         return( NULL );
288         break;
289     }
290
291
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 );
294     return( p_font );
295 }
296
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 )
303 {
304     intf_DbgMsg( "font %p\n", p_font );
305     free( p_font->p_data );
306     free( p_font );
307 }
308
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
313  * given text.
314  *****************************************************************************/
315 void vout_TextSize( vout_font_t *p_font, int i_style, const char *psz_text, int *pi_width, int *pi_height )
316 {
317     switch( p_font->i_type )
318     {
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 )
324         {
325             *pi_width = *pi_height / 3;
326         }
327         break;
328 #ifdef DEBUG
329     default:
330         intf_DbgMsg("error: unknown font type %d\n", p_font->i_type );
331         break;
332 #endif
333     }
334 }
335
336 /*****************************************************************************
337  * vout_Print: low level printing function
338  *****************************************************************************
339  * This function prints a text, without clipping, in a buffer using a
340  * previously 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 )
344 {
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 */
352
353     /* FIXME: background: can be something else that whole byte ?? */
354
355     /* Select output function */
356     switch( i_bytes_per_pixel )
357     {
358     case 1:
359         p_PutByte = (vout_put_byte_t *) PutByte8;
360         break;
361     case 2:
362         p_PutByte = (vout_put_byte_t *) PutByte16;
363         break;
364     case 3:
365         p_PutByte = (vout_put_byte_t *) PutByte24;
366         break;
367     case 4:
368     default:
369         p_PutByte = (vout_put_byte_t *) PutByte32;
370         break;
371     }
372
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;
377
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);
383
384     /* Print text */
385     for( ; *psz_text != '\0'; psz_text++ )
386     {
387         /* Check that the character is valid */
388         if( (*psz_text >= p_font->i_first) && (*psz_text <= p_font->i_last) )
389         {
390             /* Select character - bytes per char is always valid, event for
391              * non fixed fonts */
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;
394
395             /* Select base address for output */
396             switch( p_font->i_type )
397             {
398             case VOUT_FIXED_FONT:
399                 /*
400                  * Simple fixed width font
401                  */
402
403                 /* Italic text: shift picture start right */
404                 if( i_style & ITALIC_TEXT )
405                 {
406                     p_pic += i_bytes_per_pixel * (p_font->i_height / 3);
407                 }
408
409                 /* Print character */
410                 for( i_line = 0; i_line < i_font_height; i_line ++ )
411                 {
412                     for( i_byte = 0; i_byte < i_font_bytes_per_line; i_byte++, p_char++, p_border++)
413                     {
414                         /* Put pixels */
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 );
418                     }
419
420                     /* Italic text: shift picture start left */
421                     if( (i_style & ITALIC_TEXT) && !(i_line % 3) )
422                     {
423                         p_pic -= i_bytes_per_pixel;
424                     }
425                 }
426
427                 /* Jump to next character */
428                 p_pic += i_interspacing;
429                 break;
430 #ifdef DEBUG
431             default:
432                 intf_DbgMsg("error: unknown font type %d\n", p_font->i_type );
433                 break;
434 #endif
435             }
436         }
437     }
438 }
439
440 /* following functions are local */
441
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,
447                        u32 i_bg_color )
448 {
449     /* Computes position offset and background mask */
450     p_pic += 8 * i_byte;
451     i_bg &= ~(i_char | i_border);
452
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);
457 }
458
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,
464                        u32 i_bg_color )
465 {
466     /* Computes position offset and background mask */
467     p_pic += 8 * i_byte;
468     i_bg &= ~(i_char | i_border);
469
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);
474 }
475
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 )
481 {
482     /* XXX?? */
483 }
484
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 )
490 {
491     /* Computes position offset and background mask */
492     p_pic += 8 * i_byte;
493     i_bg &= ~(i_char | i_border);
494
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);
499 }
500