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