]> git.sesse.net Git - vlc/blob - src/video_output/video_text.c
* COMPLETE CVS BREAKAGE !! The MAIN branch is going to be a playground
[vlc] / src / video_output / video_text.c
1 /*****************************************************************************
2  * video_text.c : text manipulation functions
3  *****************************************************************************
4  * Copyright (C) 1999-2001 VideoLAN
5  * $Id: video_text.c,v 1.32 2001/12/09 17:01:37 sam Exp $
6  *
7  * Authors: Vincent Seguin <seguin@via.ecp.fr>
8  *          Samuel Hocevar <sam@zoy.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  * 
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include "defs.h"
29
30 #include <errno.h>                                                  /* errno */
31 #include <stdlib.h>                                                /* free() */
32 #include <stdio.h>                                              /* sprintf() */
33 #include <string.h>                                            /* strerror() */
34 #include <fcntl.h>                                                 /* open() */
35
36 #ifdef HAVE_UNISTD_H
37 #   include <unistd.h>                                    /* read(), close() */
38 #elif defined( _MSC_VER ) && defined( _WIN32 )
39 #   include <io.h>
40 #endif
41
42 #ifdef SYS_BEOS
43 #   include "beos_specific.h"
44 #endif
45
46 #ifdef SYS_DARWIN
47 #   include "darwin_specific.h"
48 #endif
49
50 #if defined( WIN32 )
51 #   include <io.h>
52 #endif
53
54 #include "common.h"
55 #include "video_text.h"
56
57 #include "intf_msg.h"
58
59 /*****************************************************************************
60  * vout_font_t: bitmap font
61  *****************************************************************************
62  * This structure is used when the system doesn't provide a convenient function
63  * to print simple characters in a buffer.
64  * VOUT_FIXED_FONTs are stored in raw mode, character after character, with a
65  * first array of characters followed by a second array of borders masks.
66  * Therefore the border masks can't be complete if the font has pixels on the
67  * border.
68  *****************************************************************************/
69 typedef struct vout_font_s
70 {
71     int                 i_type;                                 /* font type */
72     int                 i_width;                /* character width in pixels */
73     int                 i_height;              /* character height in pixels */
74     int                 i_interspacing; /* characters interspacing in pixels */
75     int                 i_bytes_per_line;        /* bytes per character line */
76     int                 i_bytes_per_char;             /* bytes per character */
77     u16                 i_first;                          /* first character */
78     u16                 i_last;                            /* last character */
79     byte_t *            p_data;                       /* font character data */
80 } vout_font_t;
81
82 /* Font types */
83 #define VOUT_FIXED_FONT       0                         /* simple fixed font */
84
85 /*****************************************************************************
86  * vout_put_byte_t: PutByte function
87  *****************************************************************************
88  * These functions will transform masks in a set of pixels. For each pixel,
89  * character, then border and background masks are tested, and the first
90  * encountered color is set.
91  *****************************************************************************/
92 typedef void (vout_put_byte_t)( void *p_pic, int i_byte, int i_char, int i_border,
93                                 int i_bg, u32 i_char_color, u32 i_border_color, u32 i_bg_color );
94
95
96 /*****************************************************************************
97  * Macros
98  *****************************************************************************/
99
100 /* PUT_BYTE_MASK: put pixels from a byte-wide mask. It uses a branching tree
101  * to optimize the number of tests. It is used in the PutByte functions.
102  * This macro works for 1, 2 and 4 Bpp. */
103 #define PUT_BYTE_MASK( i_mask, i_mask_color )                                 \
104 if( i_mask & 0xf0 )                                       /* one from 1111 */ \
105 {                                                                             \
106     if( i_mask & 0xc0 )                                   /* one from 1100 */ \
107     {                                                                         \
108         if( i_mask & 0x80 )                                        /* 1000 */ \
109         {                                                                     \
110             p_pic[0] = i_mask_color;                                          \
111             if( i_mask & 0x40 )                                    /* 0100 */ \
112             {                                                                 \
113                 p_pic[1] = i_mask_color;                                      \
114             }                                                                 \
115         }                                                                     \
116         else                                        /* not 1000 means 0100 */ \
117         {                                                                     \
118             p_pic[1] = i_mask_color;                                          \
119         }                                                                     \
120         if( i_mask & 0x30 )                               /* one from 0011 */ \
121         {                                                                     \
122             if( i_mask & 0x20 )                                    /* 0010 */ \
123             {                                                                 \
124                 p_pic[2] = i_mask_color;                                      \
125                 if( i_mask & 0x10 )                                /* 0001 */ \
126                 {                                                             \
127                     p_pic[3] = i_mask_color;                                  \
128                 }                                                             \
129             }                                                                 \
130             else                                    /* not 0010 means 0001 */ \
131             {                                                                 \
132                  p_pic[3] = i_mask_color;                                     \
133             }                                                                 \
134         }                                                                     \
135     }                                                                         \
136     else                                            /* not 1100 means 0011 */ \
137     {                                                                         \
138         if( i_mask & 0x20 )                                        /* 0010 */ \
139         {                                                                     \
140             p_pic[2] = i_mask_color;                                          \
141             if( i_mask & 0x10 )                                    /* 0001 */ \
142             {                                                                 \
143                 p_pic[3] = i_mask_color;                                      \
144             }                                                                 \
145         }                                                                     \
146         else                                        /* not 0010 means 0001 */ \
147         {                                                                     \
148             p_pic[3] = i_mask_color;                                          \
149         }                                                                     \
150     }                                                                         \
151 }                                                                             \
152 if( i_mask & 0x0f )                                                           \
153 {                                                                             \
154     if( i_mask & 0x0c )                       /* one from 1100 */             \
155     {                                                                         \
156         if( i_mask & 0x08 )                                        /* 1000 */ \
157         {                                                                     \
158             p_pic[4] = i_mask_color;                                          \
159             if( i_mask & 0x04 )                                    /* 0100 */ \
160             {                                                                 \
161                 p_pic[5] = i_mask_color;                                      \
162             }                                                                 \
163         }                                                                     \
164         else                                        /* not 1000 means 0100 */ \
165         {                                                                     \
166             p_pic[5] = i_mask_color;                                          \
167         }                                                                     \
168         if( i_mask & 0x03 )                               /* one from 0011 */ \
169         {                                                                     \
170             if( i_mask & 0x02 )                                    /* 0010 */ \
171             {                                                                 \
172                 p_pic[6] = i_mask_color;                                      \
173                 if( i_mask & 0x01 )                                /* 0001 */ \
174                 {                                                             \
175                     p_pic[7] = i_mask_color;                                  \
176                 }                                                             \
177             }                                                                 \
178             else                                    /* not 0010 means 0001 */ \
179             {                                                                 \
180                  p_pic[7] = i_mask_color;                                     \
181             }                                                                 \
182         }                                                                     \
183     }                                                                         \
184     else                                            /* not 1100 means 0011 */ \
185     {                                                                         \
186         if( i_mask & 0x02 )                                        /* 0010 */ \
187         {                                                                     \
188             p_pic[6] = i_mask_color;                                          \
189             if( i_mask & 0x01 )                                    /* 0001 */ \
190             {                                                                 \
191                 p_pic[7] = i_mask_color;                                      \
192             }                                                                 \
193         }                                                                     \
194         else                                        /* not 0010 means 0001 */ \
195         {                                                                     \
196             p_pic[7] = i_mask_color;                                          \
197         }                                                                     \
198     }                                                                         \
199 }
200
201 /*****************************************************************************
202  * Local prototypes
203  *****************************************************************************/
204 static void PutByte8 ( u8 *p_pic, int i_byte, int i_char, int i_border,
205                        int i_bg, u32 i_char_color, u32 i_border_color,
206                        u32 i_bg_color );
207 static void PutByte16( u16 *p_pic, int i_byte, int i_char, int i_border,
208                        int i_bg, u32 i_char_color, u32 i_border_color,
209                        u32 i_bg_color );
210 static void PutByte24( void *p_pic, int i_byte, byte_t i_char, byte_t i_border,
211                        byte_t i_bg, u32 i_char_color, u32 i_border_color,
212                        u32 i_bg_color );
213 static void PutByte32( u32 *p_pic, int i_byte, byte_t i_char, byte_t i_border,
214                        byte_t i_bg, u32 i_char_color, u32 i_border_color,
215                        u32 i_bg_color );
216
217 /*****************************************************************************
218  * vout_LoadFont: load a bitmap font from a file
219  *****************************************************************************
220  * This function will try to open a .psf font and load it. It will return
221  * NULL on error.
222  *****************************************************************************/
223 vout_font_t *vout_LoadFont( const char *psz_name )
224 {
225     static char * path[] = { "share", DATA_PATH, NULL, NULL };
226
227     char **             ppsz_path = path;
228     char *              psz_file;
229 #if defined( SYS_BEOS ) || defined( SYS_DARWIN )
230     char *              psz_vlcpath = system_GetProgramPath();
231     int                 i_vlclen = strlen( psz_vlcpath );
232 #endif
233     int                 i_char, i_line;        /* character and line indexes */
234     int                 i_file = -1;                          /* source file */
235     byte_t              pi_buffer[2];                         /* file buffer */
236     vout_font_t *       p_font;                           /* the font itself */
237
238     for( ; *ppsz_path != NULL ; ppsz_path++ )
239     {
240 #if defined( SYS_BEOS ) || defined( SYS_DARWIN )
241         /* Under BeOS, we need to add beos_GetProgramPath() to access
242          * files under the current directory */
243         if( strncmp( *ppsz_path, "/", 1 ) )
244         {
245             psz_file = malloc( strlen( psz_name ) + strlen( *ppsz_path )
246                                 + i_vlclen + 3 );
247             if( psz_file == NULL )
248             {
249                 continue;
250             }
251             sprintf( psz_file, "%s/%s/%s", psz_vlcpath, *ppsz_path, psz_name );
252         }
253         else
254 #endif
255         {
256             psz_file = malloc( strlen( psz_name ) + strlen( *ppsz_path ) + 2 );
257             if( psz_file == NULL )
258             {
259                 continue;
260             }
261             sprintf( psz_file, "%s/%s", *ppsz_path, psz_name );
262         }
263
264         /* Open file */
265         i_file = open( psz_file, O_RDONLY );
266         free( psz_file );
267
268         if( i_file != -1 )
269         {
270             break;
271         }
272     }
273
274     if( i_file == -1 )
275     {
276         intf_DbgMsg( "vout error: can't open file '%s' (%s)",
277                      psz_name, strerror(errno) );
278         return( NULL );
279     }
280
281     /* Read magick number */
282     if( read( i_file, pi_buffer, 2 ) != 2 )
283     {
284         intf_ErrMsg("vout error: unexpected end of file '%s'", psz_name );
285         close( i_file );
286         return( NULL );
287     }
288
289     /* Allocate font descriptor */
290     p_font = malloc( sizeof( vout_font_t ) );
291     if( p_font == NULL )
292     {
293         intf_ErrMsg( "vout error: cannot allocate vout_font_t (%s)",
294                      strerror(ENOMEM) );
295         close( i_file );
296         return( NULL );
297     }
298
299     /* Read file */
300     switch( ((u16)pi_buffer[0] << 8) | pi_buffer[1] )
301     {
302     case 0x3604:                                              /* .psf file */
303         /*
304          * PSF font: simple fixed font. Only the first 256 characters are read.
305          * Those fonts are always 1 byte wide, and 256 or 512 characters long.
306          */
307
308         /* Read font header - two bytes indicate the font properties */
309         if( read( i_file, pi_buffer, 2 ) != 2)
310         {
311             intf_ErrMsg( "vout error: unexpected end of file '%s'", psz_name );
312             free( p_font );
313             close( i_file );
314             return( NULL );
315         }
316
317         /* Copy font properties */
318         p_font->i_type =                VOUT_FIXED_FONT;
319         p_font->i_width =               8;
320         p_font->i_height =              pi_buffer[1];
321         p_font->i_interspacing =        8;
322         p_font->i_bytes_per_line =      1;
323         p_font->i_bytes_per_char =      pi_buffer[1];
324         p_font->i_first =               0;
325         p_font->i_last =                255;
326
327         /* Allocate font space */
328         p_font->p_data = malloc( 2 * 256 * pi_buffer[1] );
329         if( p_font->p_data == NULL )
330         {
331             intf_ErrMsg( "vout error: cannot allocate font space (%s)",
332                          strerror(ENOMEM) );
333             free( p_font );
334             close( i_file );
335             return( NULL );
336         }
337
338         /* Copy raw data */
339         if( read( i_file, p_font->p_data, 256 * pi_buffer[1] ) != 256 * pi_buffer[1] )
340         {
341             intf_ErrMsg("vout error: unexpected end of file '%s'", psz_name );
342             free( p_font->p_data );
343             free( p_font );
344             close( i_file );
345             return( NULL );
346         }
347
348         /* Computes border masks - remember that masks have the same matrix as
349          * characters, so an empty character border is required to have a complete
350          * border mask. */
351         for( i_char = 0; i_char <= 255; i_char++ )
352         {
353             for( i_line = 0; i_line < pi_buffer[1]; i_line++ )
354             {
355
356                 p_font->p_data[ (i_char + 256) * pi_buffer[1] + i_line ] =
357                     ((p_font->p_data[ i_char * pi_buffer[1] + i_line ] << 1) |
358                      (p_font->p_data[ i_char * pi_buffer[1] + i_line ] >> 1) |
359                      (i_line > 0 ? p_font->p_data[ i_char * pi_buffer[1] + i_line - 1]: 0) |
360                      (i_line < pi_buffer[1] - 1 ? p_font->p_data[ i_char * pi_buffer[1] + i_line + 1]: 0))
361                     & ~p_font->p_data[ i_char * pi_buffer[1] + i_line ];
362             }
363         }
364
365         break;
366     default:
367         intf_ErrMsg("vout error: file '%s' has an unknown format", psz_name );
368         free( p_font );
369         close( i_file );
370         return( NULL );
371         break;
372     }
373
374
375     intf_DbgMsg( "loaded %s: type %d, %d-%dx%d", psz_name, p_font->i_type,
376                  p_font->i_width, p_font->i_interspacing, p_font->i_height );
377     return( p_font );
378 }
379
380 /*****************************************************************************
381  * vout_UnloadFont: unload a font
382  *****************************************************************************
383  * This function free the resources allocated by vout_LoadFont
384  *****************************************************************************/
385 void vout_UnloadFont( vout_font_t *p_font )
386 {
387     /* If no font was loaded, do nothing */
388     if( p_font == NULL )
389     {
390         return;
391     }
392
393     intf_DbgMsg( "vout: unloading font %p", p_font );
394     free( p_font->p_data );
395     free( p_font );
396 }
397
398 /*****************************************************************************
399  * vout_TextSize: return the dimensions of a text
400  *****************************************************************************
401  * This function is used to align text. It returns the width and height of a
402  * given text.
403  *****************************************************************************/
404 void vout_TextSize( vout_font_t *p_font, int i_style, const char *psz_text, int *pi_width, int *pi_height )
405 {
406     /* If no font was loaded, do nothing */
407     if( p_font == NULL )
408     {
409         *pi_width = *pi_height = 0;
410         return;
411     }
412
413     switch( p_font->i_type )
414     {
415     case VOUT_FIXED_FONT:
416         *pi_width  = ((i_style & WIDE_TEXT) ? p_font->i_interspacing * 2 : p_font->i_interspacing) *
417             (strlen( psz_text ) - 1) + p_font->i_width;
418         *pi_height = p_font->i_height;
419         if( i_style & ITALIC_TEXT )
420         {
421             *pi_width = *pi_height / 3;
422         }
423         break;
424 #ifdef DEBUG
425     default:
426         intf_ErrMsg("error: unknown font type %d", p_font->i_type );
427         break;
428 #endif
429     }
430 }
431
432 /*****************************************************************************
433  * vout_Print: low level printing function
434  *****************************************************************************
435  * This function prints a text, without clipping, in a buffer using a
436  * previously loaded bitmap font.
437  *****************************************************************************/
438 void vout_Print( vout_font_t *p_font, byte_t *p_pic, int i_bytes_per_pixel, int i_bytes_per_line,
439                  u32 i_char_color, u32 i_border_color, u32 i_bg_color, int i_style, const char *psz_text, int i_percent)
440 {
441     byte_t      *p_char, *p_border;        /* character and border mask data */
442     int         i_char_mask, i_border_mask, i_bg_mask;              /* masks */
443     int         i_line;                         /* current line in character */
444     int         i_byte;                         /* current byte in character */
445     int         i_interspacing;                  /* offset between two chars */
446     int         i_font_bytes_per_line, i_font_height;     /* font properties */
447     int         i_position, i_end;                      /* current position  */
448     vout_put_byte_t *p_PutByte;                          /* PutByte function */
449
450     /* If no font was loaded, do nothing */
451     if( p_font == NULL )
452     {
453         return;
454     }
455
456     /* FIXME: background: can be something else that whole byte ?? */
457
458     /* Select output function */
459     switch( i_bytes_per_pixel )
460     {
461     case 1:
462         p_PutByte = (vout_put_byte_t *) PutByte8;
463         break;
464     case 2:
465         p_PutByte = (vout_put_byte_t *) PutByte16;
466         break;
467     case 3:
468         p_PutByte = (vout_put_byte_t *) PutByte24;
469         break;
470     case 4:
471     default:
472         p_PutByte = (vout_put_byte_t *) PutByte32;
473         break;
474     }
475
476     /* Choose masks and copy font data to local variables */
477     i_char_mask =               (i_style & VOID_TEXT) ?         0 : 0xff;
478     i_border_mask =             (i_style & OUTLINED_TEXT) ?     0xff : 0;
479     i_bg_mask =                 (i_style & OPAQUE_TEXT) ?       0xff : 0;
480
481     i_font_bytes_per_line =     p_font->i_bytes_per_line;
482     i_font_height =             p_font->i_height;
483     i_interspacing =            i_bytes_per_pixel * ((i_style & WIDE_TEXT) ?
484                                                      p_font->i_interspacing * 2 :
485                                                      p_font->i_interspacing);
486
487     /* compute where to stop... */
488     i_end = (int) (i_percent * strlen(psz_text) / I64C(100));
489     if(i_end > strlen(psz_text))
490         i_end = strlen(psz_text);
491     
492     
493     /* Print text */
494     for( i_position = 0; i_position < i_end; i_position++ ,psz_text++ )
495     {
496         /* Check that the character is valid */
497         if( (*psz_text >= p_font->i_first) && (*psz_text <= p_font->i_last) )
498         {
499             /* Select character - bytes per char is always valid, event for
500              * non fixed fonts */
501             p_char =    p_font->p_data + (*psz_text - p_font->i_first) * p_font->i_bytes_per_char;
502             p_border =  p_char + (p_font->i_last - p_font->i_first + 1) * p_font->i_bytes_per_char;
503
504             /* Select base address for output */
505             switch( p_font->i_type )
506             {
507             case VOUT_FIXED_FONT:
508                 /*
509                  * Simple fixed width font
510                  */
511
512                 /* Italic text: shift picture start right */
513                 if( i_style & ITALIC_TEXT )
514                 {
515                     p_pic += i_bytes_per_pixel * (p_font->i_height / 3);
516                 }
517
518                 /* Print character */
519                 for( i_line = 0; i_line < i_font_height; i_line ++ )
520                 {
521                     for( i_byte = 0; i_byte < i_font_bytes_per_line; i_byte++, p_char++, p_border++)
522                     {
523                         /* Put pixels */
524                         p_PutByte( p_pic + i_bytes_per_line * i_line, i_byte,
525                                    *p_char & i_char_mask, *p_border & i_border_mask, i_bg_mask,
526                                    i_char_color, i_border_color, i_bg_color );
527                     }
528
529                     /* Italic text: shift picture start left */
530                     if( (i_style & ITALIC_TEXT) && !(i_line % 3) )
531                     {
532                         p_pic -= i_bytes_per_pixel;
533                     }
534                 }
535
536                 /* Jump to next character */
537                 p_pic += i_interspacing;
538                 break;
539 #ifdef DEBUG
540             default:
541                 intf_ErrMsg("error: unknown font type %d", p_font->i_type );
542                 break;
543 #endif
544             }
545         }
546     
547     }
548 }
549
550 /* following functions are local */
551
552 /*****************************************************************************
553  * PutByte8: print a fixed width font character byte in 1 Bpp
554  *****************************************************************************/
555 static void PutByte8( u8 *p_pic, int i_byte, int i_char, int i_border,
556                        int i_bg, u32 i_char_color, u32 i_border_color,
557                        u32 i_bg_color )
558 {
559     /* Computes position offset and background mask */
560     p_pic += 8 * i_byte;
561     i_bg &= ~(i_char | i_border);
562
563     /* Put character bits */
564     PUT_BYTE_MASK(i_char, i_char_color);
565     PUT_BYTE_MASK(i_border, i_border_color);
566     PUT_BYTE_MASK(i_bg, i_bg_color);
567 }
568
569 /*****************************************************************************
570  * PutByte16: print a fixed width font character byte in 2 Bpp
571  *****************************************************************************/
572 static void PutByte16( u16 *p_pic, int i_byte, int i_char, int i_border,
573                        int i_bg, u32 i_char_color, u32 i_border_color,
574                        u32 i_bg_color )
575 {
576     /* Computes position offset and background mask */
577     p_pic += 8 * i_byte;
578     i_bg &= ~(i_char | i_border);
579
580     /* Put character bits */
581     PUT_BYTE_MASK(i_char, i_char_color);
582     PUT_BYTE_MASK(i_border, i_border_color);
583     PUT_BYTE_MASK(i_bg, i_bg_color);
584 }
585
586 /*****************************************************************************
587  * PutByte24: print a fixed width font character byte in 3 Bpp
588  *****************************************************************************/
589 static void PutByte24( void *p_pic, int i_byte, byte_t i_char, byte_t i_border, byte_t i_bg,
590                        u32 i_char_color, u32 i_border_color, u32 i_bg_color )
591 {
592     /* XXX?? */
593 }
594
595 /*****************************************************************************
596  * PutByte32: print a fixed width font character byte in 4 Bpp
597  *****************************************************************************/
598 static void PutByte32( u32 *p_pic, int i_byte, byte_t i_char, byte_t i_border, byte_t i_bg,
599                        u32 i_char_color, u32 i_border_color, u32 i_bg_color )
600 {
601     /* Computes position offset and background mask */
602     p_pic += 8 * i_byte;
603     i_bg &= ~(i_char | i_border);
604
605     /* Put character bits */
606     PUT_BYTE_MASK(i_char, i_char_color);
607     PUT_BYTE_MASK(i_border, i_border_color);
608     PUT_BYTE_MASK(i_bg, i_bg_color);
609 }
610