]> git.sesse.net Git - vlc/blob - src/video_output/video_text.c
ee34bd7615759bf347f69a6b73925be2b5c46953
[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.34 2002/02/19 00:50:20 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_ErrMsg( "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     intf_ErrMsg( "loaded %s: type %d, %d-%dx%d", psz_name, p_font->i_type,
364                  p_font->i_width, p_font->i_interspacing, p_font->i_height );
365
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     free( p_font->p_data );
383     free( p_font );
384 }
385
386 /*****************************************************************************
387  * vout_TextSize: return the dimensions of a text
388  *****************************************************************************
389  * This function is used to align text. It returns the width and height of a
390  * given text.
391  *****************************************************************************/
392 void vout_TextSize( vout_font_t *p_font, int i_style, const char *psz_text, int *pi_width, int *pi_height )
393 {
394     /* If no font was loaded, do nothing */
395     if( p_font == NULL )
396     {
397         *pi_width = *pi_height = 0;
398         return;
399     }
400
401     switch( p_font->i_type )
402     {
403     case VOUT_FIXED_FONT:
404         *pi_width  = ((i_style & WIDE_TEXT) ? p_font->i_interspacing * 2 : p_font->i_interspacing) *
405             (strlen( psz_text ) - 1) + p_font->i_width;
406         *pi_height = p_font->i_height;
407         if( i_style & ITALIC_TEXT )
408         {
409             *pi_width = *pi_height / 3;
410         }
411         break;
412 #ifdef DEBUG
413     default:
414         intf_ErrMsg("error: unknown font type %d", p_font->i_type );
415         break;
416 #endif
417     }
418 }
419
420 /*****************************************************************************
421  * vout_Print: low level printing function
422  *****************************************************************************
423  * This function prints a text, without clipping, in a buffer using a
424  * previously loaded bitmap font.
425  *****************************************************************************/
426 void vout_Print( vout_font_t *p_font, byte_t *p_pic, int i_bytes_per_pixel, int i_bytes_per_line,
427                  u32 i_char_color, u32 i_border_color, u32 i_bg_color, int i_style, const char *psz_text, int i_percent)
428 {
429     byte_t      *p_char, *p_border;        /* character and border mask data */
430     int         i_char_mask, i_border_mask, i_bg_mask;              /* masks */
431     int         i_line;                         /* current line in character */
432     int         i_byte;                         /* current byte in character */
433     int         i_interspacing;                  /* offset between two chars */
434     int         i_font_bytes_per_line, i_font_height;     /* font properties */
435     int         i_position, i_end;                      /* current position  */
436     vout_put_byte_t *p_PutByte;                          /* PutByte function */
437
438     /* If no font was loaded, do nothing */
439     if( p_font == NULL )
440     {
441         return;
442     }
443
444     /* FIXME: background: can be something else that whole byte ?? */
445
446     /* Select output function */
447     switch( i_bytes_per_pixel )
448     {
449     case 1:
450         p_PutByte = (vout_put_byte_t *) PutByte8;
451         break;
452     case 2:
453         p_PutByte = (vout_put_byte_t *) PutByte16;
454         break;
455     case 3:
456         p_PutByte = (vout_put_byte_t *) PutByte24;
457         break;
458     case 4:
459     default:
460         p_PutByte = (vout_put_byte_t *) PutByte32;
461         break;
462     }
463
464     /* Choose masks and copy font data to local variables */
465     i_char_mask =               (i_style & VOID_TEXT) ?         0 : 0xff;
466     i_border_mask =             (i_style & OUTLINED_TEXT) ?     0xff : 0;
467     i_bg_mask =                 (i_style & OPAQUE_TEXT) ?       0xff : 0;
468
469     i_font_bytes_per_line =     p_font->i_bytes_per_line;
470     i_font_height =             p_font->i_height;
471     i_interspacing =            i_bytes_per_pixel * ((i_style & WIDE_TEXT) ?
472                                                      p_font->i_interspacing * 2 :
473                                                      p_font->i_interspacing);
474
475     /* compute where to stop... */
476     i_end = (int) (i_percent * strlen(psz_text) / I64C(100));
477     if(i_end > strlen(psz_text))
478         i_end = strlen(psz_text);
479     
480     
481     /* Print text */
482     for( i_position = 0; i_position < i_end; i_position++ ,psz_text++ )
483     {
484         /* Check that the character is valid */
485         if( (*psz_text >= p_font->i_first) && (*psz_text <= p_font->i_last) )
486         {
487             /* Select character - bytes per char is always valid, event for
488              * non fixed fonts */
489             p_char =    p_font->p_data + (*psz_text - p_font->i_first) * p_font->i_bytes_per_char;
490             p_border =  p_char + (p_font->i_last - p_font->i_first + 1) * p_font->i_bytes_per_char;
491
492             /* Select base address for output */
493             switch( p_font->i_type )
494             {
495             case VOUT_FIXED_FONT:
496                 /*
497                  * Simple fixed width font
498                  */
499
500                 /* Italic text: shift picture start right */
501                 if( i_style & ITALIC_TEXT )
502                 {
503                     p_pic += i_bytes_per_pixel * (p_font->i_height / 3);
504                 }
505
506                 /* Print character */
507                 for( i_line = 0; i_line < i_font_height; i_line ++ )
508                 {
509                     for( i_byte = 0; i_byte < i_font_bytes_per_line; i_byte++, p_char++, p_border++)
510                     {
511                         /* Put pixels */
512                         p_PutByte( p_pic + i_bytes_per_line * i_line, i_byte,
513                                    *p_char & i_char_mask, *p_border & i_border_mask, i_bg_mask,
514                                    i_char_color, i_border_color, i_bg_color );
515                     }
516
517                     /* Italic text: shift picture start left */
518                     if( (i_style & ITALIC_TEXT) && !(i_line % 3) )
519                     {
520                         p_pic -= i_bytes_per_pixel;
521                     }
522                 }
523
524                 /* Jump to next character */
525                 p_pic += i_interspacing;
526                 break;
527 #ifdef DEBUG
528             default:
529                 intf_ErrMsg("error: unknown font type %d", p_font->i_type );
530                 break;
531 #endif
532             }
533         }
534     
535     }
536 }
537
538 /* following functions are local */
539
540 /*****************************************************************************
541  * PutByte8: print a fixed width font character byte in 1 Bpp
542  *****************************************************************************/
543 static void PutByte8( u8 *p_pic, int i_byte, int i_char, int i_border,
544                        int i_bg, u32 i_char_color, u32 i_border_color,
545                        u32 i_bg_color )
546 {
547     /* Computes position offset and background mask */
548     p_pic += 8 * i_byte;
549     i_bg &= ~(i_char | i_border);
550
551     /* Put character bits */
552     PUT_BYTE_MASK(i_char, i_char_color);
553     PUT_BYTE_MASK(i_border, i_border_color);
554     PUT_BYTE_MASK(i_bg, i_bg_color);
555 }
556
557 /*****************************************************************************
558  * PutByte16: print a fixed width font character byte in 2 Bpp
559  *****************************************************************************/
560 static void PutByte16( u16 *p_pic, int i_byte, int i_char, int i_border,
561                        int i_bg, u32 i_char_color, u32 i_border_color,
562                        u32 i_bg_color )
563 {
564     /* Computes position offset and background mask */
565     p_pic += 8 * i_byte;
566     i_bg &= ~(i_char | i_border);
567
568     /* Put character bits */
569     PUT_BYTE_MASK(i_char, i_char_color);
570     PUT_BYTE_MASK(i_border, i_border_color);
571     PUT_BYTE_MASK(i_bg, i_bg_color);
572 }
573
574 /*****************************************************************************
575  * PutByte24: print a fixed width font character byte in 3 Bpp
576  *****************************************************************************/
577 static void PutByte24( void *p_pic, int i_byte, byte_t i_char, byte_t i_border, byte_t i_bg,
578                        u32 i_char_color, u32 i_border_color, u32 i_bg_color )
579 {
580     /* XXX?? */
581 }
582
583 /*****************************************************************************
584  * PutByte32: print a fixed width font character byte in 4 Bpp
585  *****************************************************************************/
586 static void PutByte32( u32 *p_pic, int i_byte, byte_t i_char, byte_t i_border, byte_t i_bg,
587                        u32 i_char_color, u32 i_border_color, u32 i_bg_color )
588 {
589     /* Computes position offset and background mask */
590     p_pic += 8 * i_byte;
591     i_bg &= ~(i_char | i_border);
592
593     /* Put character bits */
594     PUT_BYTE_MASK(i_char, i_char_color);
595     PUT_BYTE_MASK(i_border, i_border_color);
596     PUT_BYTE_MASK(i_bg, i_bg_color);
597 }
598