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