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