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