]> git.sesse.net Git - vlc/blob - src/video_output/video_text.c
* ALL: WinCE compilation fixes (mostly nonexistent headers). A lot of
[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.38 2002/11/10 18:04:24 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 <stdlib.h>                                                /* free() */
29 #include <stdio.h>                                              /* sprintf() */
30 #include <string.h>                                            /* strerror() */
31
32 #include <vlc/vlc.h>
33
34 #ifdef HAVE_ERRNO_H
35 #   include <errno.h>                                               /* errno */
36 #endif
37
38 #ifdef HAVE_FCNTL_H
39 #   include <fcntl.h>                                              /* open() */
40 #endif
41
42 #ifdef HAVE_UNISTD_H
43 #   include <unistd.h>                                    /* read(), close() */
44 #elif defined( WIN32 ) && !defined( UNDER_CE )
45 #   include <io.h>
46 #endif
47
48 #include "video.h"
49 #include "video_output.h"
50 #include "video_text.h"
51
52 /*****************************************************************************
53  * vout_font_t: bitmap font
54  *****************************************************************************
55  * This structure is used when the system doesn't provide a convenient function
56  * to print simple characters in a buffer.
57  * VOUT_FIXED_FONTs are stored in raw mode, character after character, with a
58  * first array of characters followed by a second array of borders masks.
59  * Therefore the border masks can't be complete if the font has pixels on the
60  * border.
61  *****************************************************************************/
62 struct vout_font_t
63 {
64     int                 i_type;                                 /* font type */
65     int                 i_width;                /* character width in pixels */
66     int                 i_height;              /* character height in pixels */
67     int                 i_interspacing; /* characters interspacing in pixels */
68     int                 i_bytes_per_line;        /* bytes per character line */
69     int                 i_bytes_per_char;             /* bytes per character */
70     u16                 i_first;                          /* first character */
71     u16                 i_last;                            /* last character */
72     byte_t *            p_data;                       /* font character data */
73 };
74
75 /* Font types */
76 #define VOUT_FIXED_FONT       0                         /* simple fixed font */
77
78 /*****************************************************************************
79  * vout_put_byte_t: PutByte function
80  *****************************************************************************
81  * These functions will transform masks in a set of pixels. For each pixel,
82  * character, then border and background masks are tested, and the first
83  * encountered color is set.
84  *****************************************************************************/
85 typedef void (vout_put_byte_t)( void *p_pic, int i_byte, int i_char, int i_border,
86                                 int i_bg, u32 i_char_color, u32 i_border_color, u32 i_bg_color );
87
88
89 /*****************************************************************************
90  * Macros
91  *****************************************************************************/
92
93 /* PUT_BYTE_MASK: put pixels from a byte-wide mask. It uses a branching tree
94  * to optimize the number of tests. It is used in the PutByte functions.
95  * This macro works for 1, 2 and 4 Bpp. */
96 #define PUT_BYTE_MASK( i_mask, i_mask_color )                                 \
97 if( i_mask & 0xf0 )                                       /* one from 1111 */ \
98 {                                                                             \
99     if( i_mask & 0xc0 )                                   /* one from 1100 */ \
100     {                                                                         \
101         if( i_mask & 0x80 )                                        /* 1000 */ \
102         {                                                                     \
103             p_pic[0] = i_mask_color;                                          \
104             if( i_mask & 0x40 )                                    /* 0100 */ \
105             {                                                                 \
106                 p_pic[1] = i_mask_color;                                      \
107             }                                                                 \
108         }                                                                     \
109         else                                        /* not 1000 means 0100 */ \
110         {                                                                     \
111             p_pic[1] = i_mask_color;                                          \
112         }                                                                     \
113         if( i_mask & 0x30 )                               /* one from 0011 */ \
114         {                                                                     \
115             if( i_mask & 0x20 )                                    /* 0010 */ \
116             {                                                                 \
117                 p_pic[2] = i_mask_color;                                      \
118                 if( i_mask & 0x10 )                                /* 0001 */ \
119                 {                                                             \
120                     p_pic[3] = i_mask_color;                                  \
121                 }                                                             \
122             }                                                                 \
123             else                                    /* not 0010 means 0001 */ \
124             {                                                                 \
125                  p_pic[3] = i_mask_color;                                     \
126             }                                                                 \
127         }                                                                     \
128     }                                                                         \
129     else                                            /* not 1100 means 0011 */ \
130     {                                                                         \
131         if( i_mask & 0x20 )                                        /* 0010 */ \
132         {                                                                     \
133             p_pic[2] = i_mask_color;                                          \
134             if( i_mask & 0x10 )                                    /* 0001 */ \
135             {                                                                 \
136                 p_pic[3] = i_mask_color;                                      \
137             }                                                                 \
138         }                                                                     \
139         else                                        /* not 0010 means 0001 */ \
140         {                                                                     \
141             p_pic[3] = i_mask_color;                                          \
142         }                                                                     \
143     }                                                                         \
144 }                                                                             \
145 if( i_mask & 0x0f )                                                           \
146 {                                                                             \
147     if( i_mask & 0x0c )                       /* one from 1100 */             \
148     {                                                                         \
149         if( i_mask & 0x08 )                                        /* 1000 */ \
150         {                                                                     \
151             p_pic[4] = i_mask_color;                                          \
152             if( i_mask & 0x04 )                                    /* 0100 */ \
153             {                                                                 \
154                 p_pic[5] = i_mask_color;                                      \
155             }                                                                 \
156         }                                                                     \
157         else                                        /* not 1000 means 0100 */ \
158         {                                                                     \
159             p_pic[5] = i_mask_color;                                          \
160         }                                                                     \
161         if( i_mask & 0x03 )                               /* one from 0011 */ \
162         {                                                                     \
163             if( i_mask & 0x02 )                                    /* 0010 */ \
164             {                                                                 \
165                 p_pic[6] = i_mask_color;                                      \
166                 if( i_mask & 0x01 )                                /* 0001 */ \
167                 {                                                             \
168                     p_pic[7] = i_mask_color;                                  \
169                 }                                                             \
170             }                                                                 \
171             else                                    /* not 0010 means 0001 */ \
172             {                                                                 \
173                  p_pic[7] = i_mask_color;                                     \
174             }                                                                 \
175         }                                                                     \
176     }                                                                         \
177     else                                            /* not 1100 means 0011 */ \
178     {                                                                         \
179         if( i_mask & 0x02 )                                        /* 0010 */ \
180         {                                                                     \
181             p_pic[6] = i_mask_color;                                          \
182             if( i_mask & 0x01 )                                    /* 0001 */ \
183             {                                                                 \
184                 p_pic[7] = i_mask_color;                                      \
185             }                                                                 \
186         }                                                                     \
187         else                                        /* not 0010 means 0001 */ \
188         {                                                                     \
189             p_pic[7] = i_mask_color;                                          \
190         }                                                                     \
191     }                                                                         \
192 }
193
194 /*****************************************************************************
195  * Local prototypes
196  *****************************************************************************/
197 static void PutByte8 ( u8 *p_pic, int i_byte, int i_char, int i_border,
198                        int i_bg, u32 i_char_color, u32 i_border_color,
199                        u32 i_bg_color );
200 static void PutByte16( u16 *p_pic, int i_byte, int i_char, int i_border,
201                        int i_bg, u32 i_char_color, u32 i_border_color,
202                        u32 i_bg_color );
203 static void PutByte24( void *p_pic, int i_byte, byte_t i_char, byte_t i_border,
204                        byte_t i_bg, u32 i_char_color, u32 i_border_color,
205                        u32 i_bg_color );
206 static void PutByte32( u32 *p_pic, int i_byte, byte_t i_char, byte_t i_border,
207                        byte_t i_bg, u32 i_char_color, u32 i_border_color,
208                        u32 i_bg_color );
209
210 /*****************************************************************************
211  * vout_LoadFont: load a bitmap font from a file
212  *****************************************************************************
213  * This function will try to open a .psf font and load it. It will return
214  * NULL on error.
215  *****************************************************************************/
216 vout_font_t *vout_LoadFont( vout_thread_t *p_vout, const char *psz_name )
217 {
218     static char * path[] = { "share", DATA_PATH, NULL, NULL };
219
220     char **             ppsz_path = path;
221     char *              psz_file;
222 #if defined( SYS_BEOS ) || defined( SYS_DARWIN )
223     char *              psz_vlcpath = system_GetProgramPath();
224     int                 i_vlclen = strlen( psz_vlcpath );
225 #endif
226     int                 i_char, i_line;        /* character and line indexes */
227     int                 i_file = -1;                          /* source file */
228     byte_t              pi_buffer[2];                         /* file buffer */
229     vout_font_t *       p_font;                           /* the font itself */
230
231     for( ; *ppsz_path != NULL ; ppsz_path++ )
232     {
233 #if defined( SYS_BEOS ) || defined( SYS_DARWIN )
234         /* Under BeOS, we need to add beos_GetProgramPath() to access
235          * files under the current directory */
236         if( strncmp( *ppsz_path, "/", 1 ) )
237         {
238             psz_file = malloc( strlen( psz_name ) + strlen( *ppsz_path )
239                                 + i_vlclen + 3 );
240             if( psz_file == NULL )
241             {
242                 continue;
243             }
244             sprintf( psz_file, "%s/%s/%s", psz_vlcpath, *ppsz_path, psz_name );
245         }
246         else
247 #endif
248         {
249             psz_file = malloc( strlen( psz_name ) + strlen( *ppsz_path ) + 2 );
250             if( psz_file == NULL )
251             {
252                 continue;
253             }
254             sprintf( psz_file, "%s/%s", *ppsz_path, psz_name );
255         }
256
257         /* Open file */
258 #ifndef UNDER_CE /* FIXME */
259         i_file = open( psz_file, O_RDONLY );
260 #endif
261         free( psz_file );
262
263         if( i_file != -1 )
264         {
265             break;
266         }
267     }
268
269     if( i_file == -1 )
270     {
271 #ifdef HAVE_ERRNO_H
272         msg_Err( p_vout, "cannot open '%s' (%s)", psz_name, strerror(errno) );
273 #else
274         msg_Err( p_vout, "cannot open '%s'", psz_name );
275 #endif
276         return( NULL );
277     }
278
279     /* Read magic number */
280     if( read( i_file, pi_buffer, 2 ) != 2 )
281     {
282         msg_Err( p_vout, "unexpected end of file in '%s'", psz_name );
283         close( i_file );
284         return( NULL );
285     }
286
287     /* Allocate font descriptor */
288     p_font = malloc( sizeof( vout_font_t ) );
289     if( p_font == NULL )
290     {
291         msg_Err( p_vout, "out of memory" );
292         close( i_file );
293         return( NULL );
294     }
295
296     /* Read file */
297     switch( ((u16)pi_buffer[0] << 8) | pi_buffer[1] )
298     {
299     case 0x3604:                                              /* .psf file */
300         /*
301          * PSF font: simple fixed font. Only the first 256 characters are read.
302          * Those fonts are always 1 byte wide, and 256 or 512 characters long.
303          */
304
305         /* Read font header - two bytes indicate the font properties */
306         if( read( i_file, pi_buffer, 2 ) != 2)
307         {
308             msg_Err( p_vout, "unexpected end of file in '%s'", psz_name );
309             free( p_font );
310             close( i_file );
311             return( NULL );
312         }
313
314         /* Copy font properties */
315         p_font->i_type =                VOUT_FIXED_FONT;
316         p_font->i_width =               8;
317         p_font->i_height =              pi_buffer[1];
318         p_font->i_interspacing =        8;
319         p_font->i_bytes_per_line =      1;
320         p_font->i_bytes_per_char =      pi_buffer[1];
321         p_font->i_first =               0;
322         p_font->i_last =                255;
323
324         /* Allocate font space */
325         p_font->p_data = malloc( 2 * 256 * pi_buffer[1] );
326         if( p_font->p_data == NULL )
327         {
328             msg_Err( p_vout, "out of memory" );
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             msg_Err( p_vout, "unexpected end of file in '%s'", psz_name );
338             free( p_font->p_data );
339             free( p_font );
340             close( i_file );
341             return( NULL );
342         }
343
344         /* Compute border masks - remember that masks have the same matrix as
345          * characters, so an empty character border is required to have a
346          * complete 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         msg_Err( p_vout, "file '%s' has an unknown format", psz_name );
364         free( p_font );
365         close( i_file );
366         return( NULL );
367         break;
368     }
369
370     msg_Err( p_vout, "loaded %s, type %d, %d-%dx%d", psz_name, p_font->i_type,
371              p_font->i_width, p_font->i_interspacing, p_font->i_height );
372
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     free( p_font->p_data );
390     free( p_font );
391 }
392
393 /*****************************************************************************
394  * vout_TextSize: return the dimensions of a text
395  *****************************************************************************
396  * This function is used to align text. It returns the width and height of a
397  * given text.
398  *****************************************************************************/
399 void vout_TextSize( vout_font_t *p_font, int i_style, const char *psz_text, int *pi_width, int *pi_height )
400 {
401     /* If no font was loaded, do nothing */
402     if( p_font == NULL )
403     {
404         *pi_width = *pi_height = 0;
405         return;
406     }
407
408     switch( p_font->i_type )
409     {
410     case VOUT_FIXED_FONT:
411         *pi_width  = ((i_style & WIDE_TEXT) ? p_font->i_interspacing * 2 : p_font->i_interspacing) *
412             (strlen( psz_text ) - 1) + p_font->i_width;
413         *pi_height = p_font->i_height;
414         if( i_style & ITALIC_TEXT )
415         {
416             *pi_width = *pi_height / 3;
417         }
418         break;
419     }
420 }
421
422 /*****************************************************************************
423  * vout_Print: low level printing function
424  *****************************************************************************
425  * This function prints a text, without clipping, in a buffer using a
426  * previously loaded bitmap font.
427  *****************************************************************************/
428 void vout_Print( vout_font_t *p_font, byte_t *p_pic, int i_bytes_per_pixel, int i_bytes_per_line,
429                  u32 i_char_color, u32 i_border_color, u32 i_bg_color, int i_style, const char *psz_text, int i_percent)
430 {
431     byte_t      *p_char, *p_border;        /* character and border mask data */
432     int         i_char_mask, i_border_mask, i_bg_mask;              /* masks */
433     int         i_line;                         /* current line in character */
434     int         i_byte;                         /* current byte in character */
435     int         i_interspacing;                  /* offset between two chars */
436     int         i_font_bytes_per_line, i_font_height;     /* font properties */
437     int         i_position, i_end;                      /* current position  */
438     vout_put_byte_t *p_PutByte;                          /* PutByte function */
439
440     /* If no font was loaded, do nothing */
441     if( p_font == NULL )
442     {
443         return;
444     }
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) / (s64)100);
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             }
530         }
531     }
532 }
533
534 /* following functions are local */
535
536 /*****************************************************************************
537  * PutByte8: print a fixed width font character byte in 1 Bpp
538  *****************************************************************************/
539 static void PutByte8( u8 *p_pic, int i_byte, int i_char, int i_border,
540                        int i_bg, u32 i_char_color, u32 i_border_color,
541                        u32 i_bg_color )
542 {
543     /* Computes position offset and background mask */
544     p_pic += 8 * i_byte;
545     i_bg &= ~(i_char | i_border);
546
547     /* Put character bits */
548     PUT_BYTE_MASK(i_char, i_char_color);
549     PUT_BYTE_MASK(i_border, i_border_color);
550     PUT_BYTE_MASK(i_bg, i_bg_color);
551 }
552
553 /*****************************************************************************
554  * PutByte16: print a fixed width font character byte in 2 Bpp
555  *****************************************************************************/
556 static void PutByte16( u16 *p_pic, int i_byte, int i_char, int i_border,
557                        int i_bg, u32 i_char_color, u32 i_border_color,
558                        u32 i_bg_color )
559 {
560     /* Computes position offset and background mask */
561     p_pic += 8 * i_byte;
562     i_bg &= ~(i_char | i_border);
563
564     /* Put character bits */
565     PUT_BYTE_MASK(i_char, i_char_color);
566     PUT_BYTE_MASK(i_border, i_border_color);
567     PUT_BYTE_MASK(i_bg, i_bg_color);
568 }
569
570 /*****************************************************************************
571  * PutByte24: print a fixed width font character byte in 3 Bpp
572  *****************************************************************************/
573 static void PutByte24( void *p_pic, int i_byte, byte_t i_char, byte_t i_border, byte_t i_bg,
574                        u32 i_char_color, u32 i_border_color, u32 i_bg_color )
575 {
576     /* XXX?? */
577 }
578
579 /*****************************************************************************
580  * PutByte32: print a fixed width font character byte in 4 Bpp
581  *****************************************************************************/
582 static void PutByte32( u32 *p_pic, int i_byte, byte_t i_char, byte_t i_border, byte_t i_bg,
583                        u32 i_char_color, u32 i_border_color, u32 i_bg_color )
584 {
585     /* Computes position offset and background mask */
586     p_pic += 8 * i_byte;
587     i_bg &= ~(i_char | i_border);
588
589     /* Put character bits */
590     PUT_BYTE_MASK(i_char, i_char_color);
591     PUT_BYTE_MASK(i_border, i_border_color);
592     PUT_BYTE_MASK(i_bg, i_bg_color);
593 }
594