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