]> git.sesse.net Git - vlc/blob - src/video_output/video_text.c
(previous commit foired)
[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: cannot allocate vout_font_t (%s)",
232                      strerror(ENOMEM) );
233         close( i_file );
234         return( NULL );
235     }
236
237     /* Read file */
238     switch( ((u16)pi_buffer[0] << 8) | pi_buffer[1] )
239     {
240     case 0x3604:                                              /* .psf file */
241         /*
242          * PSF font: simple fixed font. Only the first 256 characters are read.
243          * Those fonts are always 1 byte wide, and 256 or 512 characters long.
244          */
245
246         /* Read font header - two bytes indicate the font properties */
247         if( read( i_file, pi_buffer, 2 ) != 2)
248         {
249             intf_ErrMsg( "vout error: unexpected end of file '%s'", psz_name );
250             free( p_font );
251             close( i_file );
252             return( NULL );
253         }
254
255         /* Copy font properties */
256         p_font->i_type =                VOUT_FIXED_FONT;
257         p_font->i_width =               8;
258         p_font->i_height =              pi_buffer[1];
259         p_font->i_interspacing =        8;
260         p_font->i_bytes_per_line =      1;
261         p_font->i_bytes_per_char =      pi_buffer[1];
262         p_font->i_first =               0;
263         p_font->i_last =                255;
264
265         /* Allocate font space */
266         p_font->p_data = malloc( 2 * 256 * pi_buffer[1] );
267         if( p_font->p_data == NULL )
268         {
269             intf_ErrMsg( "vout error: cannot allocate font space (%s)",
270                          strerror(ENOMEM) );
271             free( p_font );
272             close( i_file );
273             return( NULL );
274         }
275
276         /* Copy raw data */
277         if( read( i_file, p_font->p_data, 256 * pi_buffer[1] ) != 256 * pi_buffer[1] )
278         {
279             intf_ErrMsg("vout error: unexpected end of file '%s'", psz_name );
280             free( p_font->p_data );
281             free( p_font );
282             close( i_file );
283             return( NULL );
284         }
285
286         /* Computes border masks - remember that masks have the same matrix as
287          * characters, so an empty character border is required to have a complete
288          * border mask. */
289         for( i_char = 0; i_char <= 255; i_char++ )
290         {
291             for( i_line = 0; i_line < pi_buffer[1]; i_line++ )
292             {
293
294                 p_font->p_data[ (i_char + 256) * pi_buffer[1] + i_line ] =
295                     ((p_font->p_data[ i_char * pi_buffer[1] + i_line ] << 1) |
296                      (p_font->p_data[ i_char * pi_buffer[1] + i_line ] >> 1) |
297                      (i_line > 0 ? p_font->p_data[ i_char * pi_buffer[1] + i_line - 1]: 0) |
298                      (i_line < pi_buffer[1] - 1 ? p_font->p_data[ i_char * pi_buffer[1] + i_line + 1]: 0))
299                     & ~p_font->p_data[ i_char * pi_buffer[1] + i_line ];
300             }
301         }
302
303         break;
304     default:
305         intf_ErrMsg("vout error: file '%s' has an unknown format", psz_name );
306         free( p_font );
307         close( i_file );
308         return( NULL );
309         break;
310     }
311
312
313     intf_DbgMsg( "loaded %s: type %d, %d-%dx%d", psz_name, p_font->i_type,
314                  p_font->i_width, p_font->i_interspacing, p_font->i_height );
315     return( p_font );
316 }
317
318 /*****************************************************************************
319  * vout_UnloadFont: unload a font
320  *****************************************************************************
321  * This function free the resources allocated by vout_LoadFont
322  *****************************************************************************/
323 void vout_UnloadFont( vout_font_t *p_font )
324 {
325     intf_DbgMsg( "vout: unloading font %p", p_font );
326     free( p_font->p_data );
327     free( p_font );
328 }
329
330 /*****************************************************************************
331  * vout_TextSize: return the dimensions of a text
332  *****************************************************************************
333  * This function is used to align text. It returns the width and height of a
334  * given text.
335  *****************************************************************************/
336 void vout_TextSize( vout_font_t *p_font, int i_style, const char *psz_text, int *pi_width, int *pi_height )
337 {
338     switch( p_font->i_type )
339     {
340     case VOUT_FIXED_FONT:
341         *pi_width  = ((i_style & WIDE_TEXT) ? p_font->i_interspacing * 2 : p_font->i_interspacing) *
342             (strlen( psz_text ) - 1) + p_font->i_width;
343         *pi_height = p_font->i_height;
344         if( i_style & ITALIC_TEXT )
345         {
346             *pi_width = *pi_height / 3;
347         }
348         break;
349 #ifdef DEBUG
350     default:
351         intf_DbgMsg("error: unknown font type %d", p_font->i_type );
352         break;
353 #endif
354     }
355 }
356
357 /*****************************************************************************
358  * vout_Print: low level printing function
359  *****************************************************************************
360  * This function prints a text, without clipping, in a buffer using a
361  * previously loaded bitmap font.
362  *****************************************************************************/
363 void vout_Print( vout_font_t *p_font, byte_t *p_pic, int i_bytes_per_pixel, int i_bytes_per_line,
364                  u32 i_char_color, u32 i_border_color, u32 i_bg_color, int i_style, const char *psz_text, int i_percent)
365 {
366     byte_t      *p_char, *p_border;        /* character and border mask data */
367     int         i_char_mask, i_border_mask, i_bg_mask;              /* masks */
368     int         i_line;                         /* current line in character */
369     int         i_byte;                         /* current byte in character */
370     int         i_interspacing;                  /* offset between two chars */
371     int         i_font_bytes_per_line, i_font_height;     /* font properties */
372     int         i_position, i_end;                      /* current position  */
373     vout_put_byte_t *p_PutByte;                          /* PutByte function */
374
375     /* FIXME: background: can be something else that whole byte ?? */
376
377     /* Select output function */
378     switch( i_bytes_per_pixel )
379     {
380     case 1:
381         p_PutByte = (vout_put_byte_t *) PutByte8;
382         break;
383     case 2:
384         p_PutByte = (vout_put_byte_t *) PutByte16;
385         break;
386     case 3:
387         p_PutByte = (vout_put_byte_t *) PutByte24;
388         break;
389     case 4:
390     default:
391         p_PutByte = (vout_put_byte_t *) PutByte32;
392         break;
393     }
394
395     /* Choose masks and copy font data to local variables */
396     i_char_mask =               (i_style & VOID_TEXT) ?         0 : 0xff;
397     i_border_mask =             (i_style & OUTLINED_TEXT) ?     0xff : 0;
398     i_bg_mask =                 (i_style & OPAQUE_TEXT) ?       0xff : 0;
399
400     i_font_bytes_per_line =     p_font->i_bytes_per_line;
401     i_font_height =             p_font->i_height;
402     i_interspacing =            i_bytes_per_pixel * ((i_style & WIDE_TEXT) ?
403                                                      p_font->i_interspacing * 2 :
404                                                      p_font->i_interspacing);
405
406     /* compute where to stop... */
407     i_end = (int) (i_percent * strlen(psz_text) / 100LL);
408     if(i_end > strlen(psz_text))
409         i_end = strlen(psz_text);
410     
411     
412     /* Print text */
413     for( i_position = 0; i_position < i_end; i_position++ ,psz_text++ )
414     {
415         /* Check that the character is valid */
416         if( (*psz_text >= p_font->i_first) && (*psz_text <= p_font->i_last) )
417         {
418             /* Select character - bytes per char is always valid, event for
419              * non fixed fonts */
420             p_char =    p_font->p_data + (*psz_text - p_font->i_first) * p_font->i_bytes_per_char;
421             p_border =  p_char + (p_font->i_last - p_font->i_first + 1) * p_font->i_bytes_per_char;
422
423             /* Select base address for output */
424             switch( p_font->i_type )
425             {
426             case VOUT_FIXED_FONT:
427                 /*
428                  * Simple fixed width font
429                  */
430
431                 /* Italic text: shift picture start right */
432                 if( i_style & ITALIC_TEXT )
433                 {
434                     p_pic += i_bytes_per_pixel * (p_font->i_height / 3);
435                 }
436
437                 /* Print character */
438                 for( i_line = 0; i_line < i_font_height; i_line ++ )
439                 {
440                     for( i_byte = 0; i_byte < i_font_bytes_per_line; i_byte++, p_char++, p_border++)
441                     {
442                         /* Put pixels */
443                         p_PutByte( p_pic + i_bytes_per_line * i_line, i_byte,
444                                    *p_char & i_char_mask, *p_border & i_border_mask, i_bg_mask,
445                                    i_char_color, i_border_color, i_bg_color );
446                     }
447
448                     /* Italic text: shift picture start left */
449                     if( (i_style & ITALIC_TEXT) && !(i_line % 3) )
450                     {
451                         p_pic -= i_bytes_per_pixel;
452                     }
453                 }
454
455                 /* Jump to next character */
456                 p_pic += i_interspacing;
457                 break;
458 #ifdef DEBUG
459             default:
460                 intf_DbgMsg("error: unknown font type %d", p_font->i_type );
461                 break;
462 #endif
463             }
464         }
465     
466     }
467 }
468
469 /* following functions are local */
470
471 /*****************************************************************************
472  * PutByte8: print a fixed width font character byte in 1 Bpp
473  *****************************************************************************/
474 static void PutByte8( u8 *p_pic, int i_byte, int i_char, int i_border,
475                        int i_bg, u32 i_char_color, u32 i_border_color,
476                        u32 i_bg_color )
477 {
478     /* Computes position offset and background mask */
479     p_pic += 8 * i_byte;
480     i_bg &= ~(i_char | i_border);
481
482     /* Put character bits */
483     PUT_BYTE_MASK(i_char, i_char_color);
484     PUT_BYTE_MASK(i_border, i_border_color);
485     PUT_BYTE_MASK(i_bg, i_bg_color);
486 }
487
488 /*****************************************************************************
489  * PutByte16: print a fixed width font character byte in 2 Bpp
490  *****************************************************************************/
491 static void PutByte16( u16 *p_pic, int i_byte, int i_char, int i_border,
492                        int i_bg, u32 i_char_color, u32 i_border_color,
493                        u32 i_bg_color )
494 {
495     /* Computes position offset and background mask */
496     p_pic += 8 * i_byte;
497     i_bg &= ~(i_char | i_border);
498
499     /* Put character bits */
500     PUT_BYTE_MASK(i_char, i_char_color);
501     PUT_BYTE_MASK(i_border, i_border_color);
502     PUT_BYTE_MASK(i_bg, i_bg_color);
503 }
504
505 /*****************************************************************************
506  * PutByte24: print a fixed width font character byte in 3 Bpp
507  *****************************************************************************/
508 static void PutByte24( void *p_pic, int i_byte, byte_t i_char, byte_t i_border, byte_t i_bg,
509                        u32 i_char_color, u32 i_border_color, u32 i_bg_color )
510 {
511     /* XXX?? */
512 }
513
514 /*****************************************************************************
515  * PutByte32: print a fixed width font character byte in 4 Bpp
516  *****************************************************************************/
517 static void PutByte32( u32 *p_pic, int i_byte, byte_t i_char, byte_t i_border, byte_t i_bg,
518                        u32 i_char_color, u32 i_border_color, u32 i_bg_color )
519 {
520     /* Computes position offset and background mask */
521     p_pic += 8 * i_byte;
522     i_bg &= ~(i_char | i_border);
523
524     /* Put character bits */
525     PUT_BYTE_MASK(i_char, i_char_color);
526     PUT_BYTE_MASK(i_border, i_border_color);
527     PUT_BYTE_MASK(i_bg, i_bg_color);
528 }
529