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