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