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