]> git.sesse.net Git - vlc/blob - src/video_output/video_text.c
* Ported Glide and MGA plugins to the new module API. MGA never worked,
[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 <stdio.h>                                              /* sprintf() */
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     static char * path[] = { "share", DATA_PATH, NULL };
208
209     char **             ppsz_path = path;
210     char *              psz_file;
211 #ifdef SYS_BEOS
212     char *              psz_vlcpath = beos_GetProgramPath();
213     int                 i_vlclen = strlen( psz_vlcpath );
214 #endif
215     int                 i_char, i_line;        /* character and line indexes */
216     int                 i_file = -1;                          /* source file */
217     byte_t              pi_buffer[2];                         /* file buffer */
218     vout_font_t *       p_font;                           /* the font itself */
219
220     for( ; *ppsz_path != NULL ; ppsz_path++ )
221     {
222 #ifdef SYS_BEOS
223         /* Under BeOS, we need to add beos_GetProgramPath() to access
224          * files under the current directory */
225         if( strncmp( *ppsz_path, "/", 1 ) )
226         {
227             psz_file = malloc( strlen( psz_name ) + strlen( *ppsz_path )
228                                 + i_vlclen + 3 );
229             if( psz_file == NULL )
230             {
231                 continue;
232             }
233             sprintf( psz_file, "%s/%s/%s", psz_vlcpath, *ppsz_path, psz_name );
234         }
235         else
236 #endif
237         {
238             psz_file = malloc( strlen( psz_name ) + strlen( *ppsz_path ) + 2 );
239             if( psz_file == NULL )
240             {
241                 continue;
242             }
243             sprintf( psz_file, "%s/%s", *ppsz_path, psz_name );
244         }
245
246         /* Open file */
247         i_file = open( psz_file, O_RDONLY );
248         free( psz_file );
249
250         if( i_file != -1 )
251         {
252             break;
253         }
254     }
255
256     if( i_file == -1 )
257     {
258         intf_DbgMsg( "vout error: can't open file '%s' (%s)",
259                      psz_name, strerror(errno) );
260         return( NULL );
261     }
262
263     /* Read magick number */
264     if( read( i_file, pi_buffer, 2 ) != 2 )
265     {
266         intf_ErrMsg("vout error: unexpected end of file '%s'", psz_name );
267         close( i_file );
268         return( NULL );
269     }
270
271     /* Allocate font descriptor */
272     p_font = malloc( sizeof( vout_font_t ) );
273     if( p_font == NULL )
274     {
275         intf_ErrMsg( "vout error: cannot allocate vout_font_t (%s)",
276                      strerror(ENOMEM) );
277         close( i_file );
278         return( NULL );
279     }
280
281     /* Read file */
282     switch( ((u16)pi_buffer[0] << 8) | pi_buffer[1] )
283     {
284     case 0x3604:                                              /* .psf file */
285         /*
286          * PSF font: simple fixed font. Only the first 256 characters are read.
287          * Those fonts are always 1 byte wide, and 256 or 512 characters long.
288          */
289
290         /* Read font header - two bytes indicate the font properties */
291         if( read( i_file, pi_buffer, 2 ) != 2)
292         {
293             intf_ErrMsg( "vout error: unexpected end of file '%s'", psz_name );
294             free( p_font );
295             close( i_file );
296             return( NULL );
297         }
298
299         /* Copy font properties */
300         p_font->i_type =                VOUT_FIXED_FONT;
301         p_font->i_width =               8;
302         p_font->i_height =              pi_buffer[1];
303         p_font->i_interspacing =        8;
304         p_font->i_bytes_per_line =      1;
305         p_font->i_bytes_per_char =      pi_buffer[1];
306         p_font->i_first =               0;
307         p_font->i_last =                255;
308
309         /* Allocate font space */
310         p_font->p_data = malloc( 2 * 256 * pi_buffer[1] );
311         if( p_font->p_data == NULL )
312         {
313             intf_ErrMsg( "vout error: cannot allocate font space (%s)",
314                          strerror(ENOMEM) );
315             free( p_font );
316             close( i_file );
317             return( NULL );
318         }
319
320         /* Copy raw data */
321         if( read( i_file, p_font->p_data, 256 * pi_buffer[1] ) != 256 * pi_buffer[1] )
322         {
323             intf_ErrMsg("vout error: unexpected end of file '%s'", psz_name );
324             free( p_font->p_data );
325             free( p_font );
326             close( i_file );
327             return( NULL );
328         }
329
330         /* Computes border masks - remember that masks have the same matrix as
331          * characters, so an empty character border is required to have a complete
332          * border mask. */
333         for( i_char = 0; i_char <= 255; i_char++ )
334         {
335             for( i_line = 0; i_line < pi_buffer[1]; i_line++ )
336             {
337
338                 p_font->p_data[ (i_char + 256) * pi_buffer[1] + i_line ] =
339                     ((p_font->p_data[ i_char * pi_buffer[1] + i_line ] << 1) |
340                      (p_font->p_data[ i_char * pi_buffer[1] + i_line ] >> 1) |
341                      (i_line > 0 ? p_font->p_data[ i_char * pi_buffer[1] + i_line - 1]: 0) |
342                      (i_line < pi_buffer[1] - 1 ? p_font->p_data[ i_char * pi_buffer[1] + i_line + 1]: 0))
343                     & ~p_font->p_data[ i_char * pi_buffer[1] + i_line ];
344             }
345         }
346
347         break;
348     default:
349         intf_ErrMsg("vout error: file '%s' has an unknown format", psz_name );
350         free( p_font );
351         close( i_file );
352         return( NULL );
353         break;
354     }
355
356
357     intf_DbgMsg( "loaded %s: type %d, %d-%dx%d", psz_name, p_font->i_type,
358                  p_font->i_width, p_font->i_interspacing, p_font->i_height );
359     return( p_font );
360 }
361
362 /*****************************************************************************
363  * vout_UnloadFont: unload a font
364  *****************************************************************************
365  * This function free the resources allocated by vout_LoadFont
366  *****************************************************************************/
367 void vout_UnloadFont( vout_font_t *p_font )
368 {
369     intf_DbgMsg( "vout: unloading font %p", p_font );
370     free( p_font->p_data );
371     free( p_font );
372 }
373
374 /*****************************************************************************
375  * vout_TextSize: return the dimensions of a text
376  *****************************************************************************
377  * This function is used to align text. It returns the width and height of a
378  * given text.
379  *****************************************************************************/
380 void vout_TextSize( vout_font_t *p_font, int i_style, const char *psz_text, int *pi_width, int *pi_height )
381 {
382     switch( p_font->i_type )
383     {
384     case VOUT_FIXED_FONT:
385         *pi_width  = ((i_style & WIDE_TEXT) ? p_font->i_interspacing * 2 : p_font->i_interspacing) *
386             (strlen( psz_text ) - 1) + p_font->i_width;
387         *pi_height = p_font->i_height;
388         if( i_style & ITALIC_TEXT )
389         {
390             *pi_width = *pi_height / 3;
391         }
392         break;
393 #ifdef DEBUG
394     default:
395         intf_DbgMsg("error: unknown font type %d", p_font->i_type );
396         break;
397 #endif
398     }
399 }
400
401 /*****************************************************************************
402  * vout_Print: low level printing function
403  *****************************************************************************
404  * This function prints a text, without clipping, in a buffer using a
405  * previously loaded bitmap font.
406  *****************************************************************************/
407 void vout_Print( vout_font_t *p_font, byte_t *p_pic, int i_bytes_per_pixel, int i_bytes_per_line,
408                  u32 i_char_color, u32 i_border_color, u32 i_bg_color, int i_style, const char *psz_text, int i_percent)
409 {
410     byte_t      *p_char, *p_border;        /* character and border mask data */
411     int         i_char_mask, i_border_mask, i_bg_mask;              /* masks */
412     int         i_line;                         /* current line in character */
413     int         i_byte;                         /* current byte in character */
414     int         i_interspacing;                  /* offset between two chars */
415     int         i_font_bytes_per_line, i_font_height;     /* font properties */
416     int         i_position, i_end;                      /* current position  */
417     vout_put_byte_t *p_PutByte;                          /* PutByte function */
418
419     /* FIXME: background: can be something else that whole byte ?? */
420
421     /* Select output function */
422     switch( i_bytes_per_pixel )
423     {
424     case 1:
425         p_PutByte = (vout_put_byte_t *) PutByte8;
426         break;
427     case 2:
428         p_PutByte = (vout_put_byte_t *) PutByte16;
429         break;
430     case 3:
431         p_PutByte = (vout_put_byte_t *) PutByte24;
432         break;
433     case 4:
434     default:
435         p_PutByte = (vout_put_byte_t *) PutByte32;
436         break;
437     }
438
439     /* Choose masks and copy font data to local variables */
440     i_char_mask =               (i_style & VOID_TEXT) ?         0 : 0xff;
441     i_border_mask =             (i_style & OUTLINED_TEXT) ?     0xff : 0;
442     i_bg_mask =                 (i_style & OPAQUE_TEXT) ?       0xff : 0;
443
444     i_font_bytes_per_line =     p_font->i_bytes_per_line;
445     i_font_height =             p_font->i_height;
446     i_interspacing =            i_bytes_per_pixel * ((i_style & WIDE_TEXT) ?
447                                                      p_font->i_interspacing * 2 :
448                                                      p_font->i_interspacing);
449
450     /* compute where to stop... */
451     i_end = (int) (i_percent * strlen(psz_text) / 100LL);
452     if(i_end > strlen(psz_text))
453         i_end = strlen(psz_text);
454     
455     
456     /* Print text */
457     for( i_position = 0; i_position < i_end; i_position++ ,psz_text++ )
458     {
459         /* Check that the character is valid */
460         if( (*psz_text >= p_font->i_first) && (*psz_text <= p_font->i_last) )
461         {
462             /* Select character - bytes per char is always valid, event for
463              * non fixed fonts */
464             p_char =    p_font->p_data + (*psz_text - p_font->i_first) * p_font->i_bytes_per_char;
465             p_border =  p_char + (p_font->i_last - p_font->i_first + 1) * p_font->i_bytes_per_char;
466
467             /* Select base address for output */
468             switch( p_font->i_type )
469             {
470             case VOUT_FIXED_FONT:
471                 /*
472                  * Simple fixed width font
473                  */
474
475                 /* Italic text: shift picture start right */
476                 if( i_style & ITALIC_TEXT )
477                 {
478                     p_pic += i_bytes_per_pixel * (p_font->i_height / 3);
479                 }
480
481                 /* Print character */
482                 for( i_line = 0; i_line < i_font_height; i_line ++ )
483                 {
484                     for( i_byte = 0; i_byte < i_font_bytes_per_line; i_byte++, p_char++, p_border++)
485                     {
486                         /* Put pixels */
487                         p_PutByte( p_pic + i_bytes_per_line * i_line, i_byte,
488                                    *p_char & i_char_mask, *p_border & i_border_mask, i_bg_mask,
489                                    i_char_color, i_border_color, i_bg_color );
490                     }
491
492                     /* Italic text: shift picture start left */
493                     if( (i_style & ITALIC_TEXT) && !(i_line % 3) )
494                     {
495                         p_pic -= i_bytes_per_pixel;
496                     }
497                 }
498
499                 /* Jump to next character */
500                 p_pic += i_interspacing;
501                 break;
502 #ifdef DEBUG
503             default:
504                 intf_DbgMsg("error: unknown font type %d", p_font->i_type );
505                 break;
506 #endif
507             }
508         }
509     
510     }
511 }
512
513 /* following functions are local */
514
515 /*****************************************************************************
516  * PutByte8: print a fixed width font character byte in 1 Bpp
517  *****************************************************************************/
518 static void PutByte8( u8 *p_pic, int i_byte, int i_char, int i_border,
519                        int i_bg, u32 i_char_color, u32 i_border_color,
520                        u32 i_bg_color )
521 {
522     /* Computes position offset and background mask */
523     p_pic += 8 * i_byte;
524     i_bg &= ~(i_char | i_border);
525
526     /* Put character bits */
527     PUT_BYTE_MASK(i_char, i_char_color);
528     PUT_BYTE_MASK(i_border, i_border_color);
529     PUT_BYTE_MASK(i_bg, i_bg_color);
530 }
531
532 /*****************************************************************************
533  * PutByte16: print a fixed width font character byte in 2 Bpp
534  *****************************************************************************/
535 static void PutByte16( u16 *p_pic, int i_byte, int i_char, int i_border,
536                        int i_bg, u32 i_char_color, u32 i_border_color,
537                        u32 i_bg_color )
538 {
539     /* Computes position offset and background mask */
540     p_pic += 8 * i_byte;
541     i_bg &= ~(i_char | i_border);
542
543     /* Put character bits */
544     PUT_BYTE_MASK(i_char, i_char_color);
545     PUT_BYTE_MASK(i_border, i_border_color);
546     PUT_BYTE_MASK(i_bg, i_bg_color);
547 }
548
549 /*****************************************************************************
550  * PutByte24: print a fixed width font character byte in 3 Bpp
551  *****************************************************************************/
552 static void PutByte24( void *p_pic, int i_byte, byte_t i_char, byte_t i_border, byte_t i_bg,
553                        u32 i_char_color, u32 i_border_color, u32 i_bg_color )
554 {
555     /* XXX?? */
556 }
557
558 /*****************************************************************************
559  * PutByte32: print a fixed width font character byte in 4 Bpp
560  *****************************************************************************/
561 static void PutByte32( u32 *p_pic, int i_byte, byte_t i_char, byte_t i_border, byte_t i_bg,
562                        u32 i_char_color, u32 i_border_color, u32 i_bg_color )
563 {
564     /* Computes position offset and background mask */
565     p_pic += 8 * i_byte;
566     i_bg &= ~(i_char | i_border);
567
568     /* Put character bits */
569     PUT_BYTE_MASK(i_char, i_char_color);
570     PUT_BYTE_MASK(i_border, i_border_color);
571     PUT_BYTE_MASK(i_bg, i_bg_color);
572 }
573