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