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