]> git.sesse.net Git - vlc/blob - modules/misc/freetype.c
* src/video_output/vout_subpictures.c, include/vlc_video.h:
[vlc] / modules / misc / freetype.c
1 /*****************************************************************************
2  * freetype.c : Put text on the video, using freetype2
3  *****************************************************************************
4  * Copyright (C) 2002, 2003 VideoLAN
5  * $Id$
6  *
7  * Authors: Sigmund Augdal <sigmunau@idi.ntnu.no>
8  *          Gildas Bazin <gbazin@videolan.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 <stdlib.h>                                      /* malloc(), free() */
29 #include <string.h>
30
31 #ifdef HAVE_LINUX_LIMITS_H
32 #   include <linux/limits.h>
33 #endif
34
35 #include <vlc/vlc.h>
36 #include <vlc/vout.h>
37 #include "osd.h"
38 #include "vlc_block.h"
39 #include "vlc_filter.h"
40
41 #include <math.h>
42
43 #ifdef HAVE_ERRNO_H
44 #   include <errno.h>
45 #endif
46
47 #include <ft2build.h>
48 #include FT_FREETYPE_H
49 #include FT_GLYPH_H
50
51 #ifdef SYS_DARWIN
52 #define DEFAULT_FONT "/System/Library/Fonts/LucidaGrande.dfont"
53 #elif defined( SYS_BEOS )
54 #define DEFAULT_FONT "/boot/beos/etc/fonts/ttfonts/Swiss721.ttf"
55 #elif defined( WIN32 )
56 #define DEFAULT_FONT "" /* Default font found at run-time */
57 #else
58 #define DEFAULT_FONT "/usr/share/fonts/truetype/freefont/FreeSerifBold.ttf"
59 #endif
60
61 #if defined(HAVE_ICONV)
62 #include <iconv.h>
63 #endif
64 #if defined(HAVE_FRIBIDI)
65 #include <fribidi/fribidi.h>
66 #endif
67
68 typedef struct line_desc_t line_desc_t;
69
70 /*****************************************************************************
71  * Local prototypes
72  *****************************************************************************/
73 static int  Create ( vlc_object_t * );
74 static void Destroy( vlc_object_t * );
75
76 static subpicture_t *RenderText( filter_t *, block_t * );
77
78 #if !defined(HAVE_ICONV)
79 static int  GetUnicodeCharFromUTF8( byte_t ** );
80 #endif
81
82 static line_desc_t *NewLine( byte_t * );
83
84 /*****************************************************************************
85  * Module descriptor
86  *****************************************************************************/
87 #define FONT_TEXT N_("Font")
88 #define FONT_LONGTEXT N_("Font filename")
89 #define FONTSIZE_TEXT N_("Font size in pixels")
90 #define FONTSIZE_LONGTEXT N_("The size of the fonts used by the osd module. " \
91     "If set to something different than 0 this option will override the " \
92     "relative font size " )
93 #define FONTSIZER_TEXT N_("Font size")
94 #define FONTSIZER_LONGTEXT N_("The size of the fonts used by the osd module" )
95
96 static int   pi_sizes[] = { 20, 18, 16, 12, 6 };
97 static char *ppsz_sizes_text[] = { N_("Smaller"), N_("Small"), N_("Normal"),
98                                    N_("Large"), N_("Larger") };
99
100 vlc_module_begin();
101     set_description( _("freetype2 font renderer") );
102
103     add_file( "freetype-font", DEFAULT_FONT, NULL, FONT_TEXT, FONT_LONGTEXT,
104               VLC_FALSE );
105     add_integer( "freetype-fontsize", 0, NULL, FONTSIZE_TEXT,
106                  FONTSIZE_LONGTEXT, VLC_TRUE );
107     add_integer( "freetype-rel-fontsize", 16, NULL, FONTSIZER_TEXT,
108                  FONTSIZER_LONGTEXT, VLC_FALSE );
109         change_integer_list( pi_sizes, ppsz_sizes_text, 0 );
110
111     set_capability( "text renderer", 100 );
112     add_shortcut( "text" );
113     set_callbacks( Create, Destroy );
114 vlc_module_end();
115
116 /**
117  * Private data in a subpicture. Describes a string.
118  */
119 typedef struct subpicture_data_t
120 {
121     int            i_width;
122     int            i_height;
123     /** The string associated with this subpicture */
124     byte_t        *psz_text;
125     line_desc_t   *p_lines;
126
127 } subpicture_data_t;
128
129 struct line_desc_t
130 {
131     /** NULL-terminated list of glyphs making the string */
132     FT_BitmapGlyph *pp_glyphs;
133     /** list of relative positions for the glyphs */
134     FT_Vector      *p_glyph_pos;
135     int             i_height;
136     int             i_width;
137     line_desc_t    *p_next;
138 };
139
140 static void Render    ( filter_t *, subpicture_t *, subpicture_data_t * );
141 static void FreeString( subpicture_data_t * );
142
143 /*****************************************************************************
144  * filter_sys_t: freetype local data
145  *****************************************************************************
146  * This structure is part of the video output thread descriptor.
147  * It describes the freetype specific properties of an output thread.
148  *****************************************************************************/
149 struct filter_sys_t
150 {
151     FT_Library     p_library;   /* handle to library     */
152     FT_Face        p_face;      /* handle to face object */
153     vlc_bool_t     i_use_kerning;
154     uint8_t        pi_gamma[256];
155 };
156
157 /*****************************************************************************
158  * Create: allocates osd-text video thread output method
159  *****************************************************************************
160  * This function allocates and initializes a Clone vout method.
161  *****************************************************************************/
162 #define gamma_value 2.0
163 static int Create( vlc_object_t *p_this )
164 {
165     filter_t *p_filter = (filter_t *)p_this;
166     filter_sys_t *p_sys;
167     char *psz_fontfile = NULL;
168     int i, i_error;
169     int i_fontsize = 0;
170     double gamma_inv = 1.0f / gamma_value;
171     vlc_value_t val;
172
173     /* Allocate structure */
174     p_sys = malloc( sizeof( filter_sys_t ) );
175     if( !p_sys )
176     {
177         msg_Err( p_filter, "out of memory" );
178         return VLC_ENOMEM;
179     }
180     p_sys->p_face = 0;
181     p_sys->p_library = 0;
182
183     for( i = 0; i < 256; i++ )
184     {
185         p_sys->pi_gamma[i] =
186             (uint8_t)( pow( (double)i / 255.0f, gamma_inv) * 255.0f );
187     }
188
189     var_Create( p_filter, "freetype-font", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
190     var_Create( p_filter, "freetype-fontsize",
191                 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
192     var_Create( p_filter, "freetype-rel-fontsize",
193                 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
194
195     /* Look what method was requested */
196     var_Get( p_filter, "freetype-font", &val );
197     psz_fontfile = val.psz_string;
198     if( !psz_fontfile || !*psz_fontfile )
199     {
200         if( psz_fontfile ) free( psz_fontfile );
201         psz_fontfile = (char *)malloc( PATH_MAX + 1 );
202 #ifdef WIN32
203         GetWindowsDirectory( psz_fontfile, PATH_MAX + 1 );
204         strcat( psz_fontfile, "\\fonts\\arial.ttf" );
205 #elif SYS_DARWIN
206         strcpy( psz_fontfile, DEFAULT_FONT );
207 #else
208         msg_Err( p_filter, "user didn't specify a font" );
209         goto error;
210 #endif
211     }
212
213     i_error = FT_Init_FreeType( &p_sys->p_library );
214     if( i_error )
215     {
216         msg_Err( p_filter, "couldn't initialize freetype" );
217         goto error;
218     }
219
220     i_error = FT_New_Face( p_sys->p_library, psz_fontfile ? psz_fontfile : "",
221                            0, &p_sys->p_face );
222     if( i_error == FT_Err_Unknown_File_Format )
223     {
224         msg_Err( p_filter, "file %s have unknown format", psz_fontfile );
225         goto error;
226     }
227     else if( i_error )
228     {
229         msg_Err( p_filter, "failed to load font file %s", psz_fontfile );
230         goto error;
231     }
232
233     i_error = FT_Select_Charmap( p_sys->p_face, ft_encoding_unicode );
234     if( i_error )
235     {
236         msg_Err( p_filter, "Font has no unicode translation table" );
237         goto error;
238     }
239
240     p_sys->i_use_kerning = FT_HAS_KERNING( p_sys->p_face );
241
242     var_Get( p_filter, "freetype-fontsize", &val );
243     if( val.i_int )
244     {
245         i_fontsize = val.i_int;
246     }
247     else
248     {
249         var_Get( p_filter, "freetype-rel-fontsize", &val );
250         i_fontsize = (int)p_filter->fmt_out.video.i_height / val.i_int;
251     }
252     msg_Dbg( p_filter, "Using fontsize: %i", i_fontsize);
253
254     i_error = FT_Set_Pixel_Sizes( p_sys->p_face, 0, i_fontsize );
255     if( i_error )
256     {
257         msg_Err( p_filter, "couldn't set font size to %d", i_fontsize );
258         goto error;
259     }
260
261     if( psz_fontfile ) free( psz_fontfile );
262     p_filter->pf_render_string = RenderText;
263     p_filter->p_sys = p_sys;
264     return VLC_SUCCESS;
265
266  error:
267     if( p_sys->p_face ) FT_Done_Face( p_sys->p_face );
268     if( p_sys->p_library ) FT_Done_FreeType( p_sys->p_library );
269     if( psz_fontfile ) free( psz_fontfile );
270     free( p_sys );
271     return VLC_EGENERIC;
272 }
273
274 /*****************************************************************************
275  * Destroy: destroy Clone video thread output method
276  *****************************************************************************
277  * Clean up all data and library connections
278  *****************************************************************************/
279 static void Destroy( vlc_object_t *p_this )
280 {
281     filter_t *p_filter = (filter_t *)p_this;
282     filter_sys_t *p_sys = p_filter->p_sys;
283     FT_Done_Face( p_sys->p_face );
284     FT_Done_FreeType( p_sys->p_library );
285     free( p_sys );
286 }
287
288 /*****************************************************************************
289  * Render: place string in picture
290  *****************************************************************************
291  * This function merges the previously rendered freetype glyphs into a picture
292  *****************************************************************************/
293 static void Render( filter_t *p_filter, subpicture_t *p_spu,
294                     subpicture_data_t *p_string )
295 {
296     filter_sys_t *p_sys = p_filter->p_sys;
297     line_desc_t *p_line;
298     uint8_t *p_y, *p_u, *p_v, *p_a;
299     video_format_t fmt;
300     int i, x, y, i_pitch;
301
302     /* Create a new subpicture region */
303     memset( &fmt, 0, sizeof(video_format_t) );
304     fmt.i_chroma = VLC_FOURCC('Y','U','V','A');
305     fmt.i_aspect = VOUT_ASPECT_FACTOR;
306     fmt.i_width = fmt.i_visible_width = p_string->i_width + 2;
307     fmt.i_height = fmt.i_visible_height = p_string->i_height + 2;
308     fmt.i_x_offset = fmt.i_y_offset = 0;
309     p_spu->p_region = p_spu->pf_create_region( VLC_OBJECT(p_filter), &fmt );
310     if( !p_spu->p_region )
311     {
312         msg_Err( p_filter, "cannot allocate SPU region" );
313         return;
314     }
315
316     p_spu->p_region->i_x = p_spu->p_region->i_y = 0;
317     p_y = p_spu->p_region->picture.Y_PIXELS;
318     p_u = p_spu->p_region->picture.U_PIXELS;
319     p_v = p_spu->p_region->picture.V_PIXELS;
320     p_a = p_spu->p_region->picture.A_PIXELS;
321     i_pitch = p_spu->p_region->picture.Y_PITCH;
322
323     /* Initialize the region pixels (only the alpha will be changed later) */
324     memset( p_y, 0x00, i_pitch * p_spu->p_region->fmt.i_height );
325     memset( p_u, 0x80, i_pitch * p_spu->p_region->fmt.i_height );
326     memset( p_v, 0x80, i_pitch * p_spu->p_region->fmt.i_height );
327     memset( p_a, 0x00, i_pitch * p_spu->p_region->fmt.i_height );
328
329 #define pi_gamma p_sys->pi_gamma
330
331     for( p_line = p_string->p_lines; p_line != NULL; p_line = p_line->p_next )
332     {
333         int i_glyph_tmax = 0;
334         int i_bitmap_offset, i_offset;
335         for( i = 0; p_line->pp_glyphs[i] != NULL; i++ )
336         {
337             FT_BitmapGlyph p_glyph = p_line->pp_glyphs[ i ];
338             i_glyph_tmax = __MAX( i_glyph_tmax, p_glyph->top );
339         }
340
341         for( i = 0; p_line->pp_glyphs[i] != NULL; i++ )
342         {
343             FT_BitmapGlyph p_glyph = p_line->pp_glyphs[ i ];
344
345             i_offset = ( p_line->p_glyph_pos[ i ].y +
346                 i_glyph_tmax - p_glyph->top + 1 ) *
347                 i_pitch + p_line->p_glyph_pos[ i ].x + p_glyph->left + 1;
348
349             for( y = 0, i_bitmap_offset = 0; y < p_glyph->bitmap.rows; y++ )
350             {
351                 for( x = 0; x < p_glyph->bitmap.width; x++, i_bitmap_offset++ )
352                 {
353                     if( !pi_gamma[p_glyph->bitmap.buffer[i_bitmap_offset]] )
354                         continue;
355
356                     i_offset -= i_pitch;
357                     p_a[i_offset + x] = ((uint16_t)p_a[i_offset + x] +
358                       pi_gamma[p_glyph->bitmap.buffer[i_bitmap_offset]])/2;
359                     i_offset += i_pitch; x--;
360                     p_a[i_offset + x] = ((uint16_t)p_a[i_offset + x] +
361                       pi_gamma[p_glyph->bitmap.buffer[i_bitmap_offset]])/2;
362                     x += 2;
363                     p_a[i_offset + x] = ((uint16_t)p_a[i_offset + x] +
364                       pi_gamma[p_glyph->bitmap.buffer[i_bitmap_offset]])/2;
365                     i_offset += i_pitch; x--;
366                     p_a[i_offset + x] = ((uint16_t)p_a[i_offset + x] +
367                       pi_gamma[p_glyph->bitmap.buffer[i_bitmap_offset]])/2;
368                     i_offset -= i_pitch;
369                 }
370                 i_offset += i_pitch;
371             }
372
373             i_offset = ( p_line->p_glyph_pos[ i ].y +
374                 i_glyph_tmax - p_glyph->top ) *
375                 i_pitch + p_line->p_glyph_pos[ i ].x + p_glyph->left + 1;
376
377             for( y = 0, i_bitmap_offset = 0; y < p_glyph->bitmap.rows; y++ )
378             {
379                for( x = 0; x < p_glyph->bitmap.width; x++, i_bitmap_offset++ )
380                {
381                    p_y[i_offset + x] =
382                        pi_gamma[p_glyph->bitmap.buffer[i_bitmap_offset]];
383                }
384                i_offset += i_pitch;
385             }
386
387 #undef pi_gamma
388         }
389     }
390 }
391
392 /**
393  * This function receives a string and creates a subpicture for it. It
394  * also calculates the size needed for this string, and renders the
395  * needed glyphs into memory. It is used as pf_add_string callback in
396  * the vout method by this module
397  */
398 static subpicture_t *RenderText( filter_t *p_filter, block_t *p_block )
399 {
400     filter_sys_t *p_sys = p_filter->p_sys;
401     subpicture_t *p_subpic = 0;
402     subpicture_data_t *p_string = 0;
403     line_desc_t  *p_line = 0, *p_next = 0;
404     int i, i_pen_y, i_pen_x, i_error, i_glyph_index, i_previous;
405     uint32_t *p_unicode_string, i_char;
406     int i_string_length;
407     char *psz_string;
408
409 #if defined(HAVE_ICONV)
410     iconv_t iconv_handle;
411 #endif
412
413     FT_BBox line;
414     FT_BBox glyph_size;
415     FT_Vector result;
416     FT_Glyph tmp_glyph;
417
418     /* Sanity check */
419     if( !p_block ) return NULL;
420     psz_string = p_block->p_buffer;
421     if( !psz_string || !*psz_string ) goto error;
422
423     result.x = 0;
424     result.y = 0;
425     line.xMin = 0;
426     line.xMax = 0;
427     line.yMin = 0;
428     line.yMax = 0;
429
430     /* Create and initialize a subpicture */
431     p_subpic = p_filter->pf_spu_buffer_new( p_filter );
432     if( !p_subpic ) goto error;
433
434     p_subpic->i_start = p_block->i_pts;
435     p_subpic->i_stop = p_block->i_pts + p_block->i_length;
436     p_subpic->b_ephemer = (p_block->i_length == 0);
437     p_subpic->b_absolute = VLC_FALSE;
438
439     /* Create and initialize private data for the subpicture */
440     p_string = malloc( sizeof(subpicture_data_t) );
441     if( !p_string )
442     {
443         msg_Err( p_filter, "Out of memory" );
444         goto error;
445     }
446     p_string->p_lines = 0;
447     p_string->psz_text = strdup( psz_string );
448
449 #if defined(HAVE_ICONV)
450     p_unicode_string = malloc( ( strlen(psz_string) + 1 ) * sizeof(uint32_t) );
451     if( p_unicode_string == NULL )
452     {
453         msg_Err( p_filter, "Out of memory" );
454         goto error;
455     }
456 #if defined(WORDS_BIGENDIAN)
457     iconv_handle = iconv_open( "UCS-4BE", "UTF-8" );
458 #else
459     iconv_handle = iconv_open( "UCS-4LE", "UTF-8" );
460 #endif
461     if( iconv_handle == (iconv_t)-1 )
462     {
463         msg_Warn( p_filter, "Unable to do convertion" );
464         goto error;
465     }
466
467     {
468         char *p_in_buffer, *p_out_buffer;
469         size_t i_in_bytes, i_out_bytes, i_out_bytes_left, i_ret;
470         i_in_bytes = strlen( psz_string );
471         i_out_bytes = i_in_bytes * sizeof( uint32_t );
472         i_out_bytes_left = i_out_bytes;
473         p_in_buffer = psz_string;
474         p_out_buffer = (char *)p_unicode_string;
475         i_ret = iconv( iconv_handle, &p_in_buffer, &i_in_bytes,
476                        &p_out_buffer, &i_out_bytes_left );
477         if( i_in_bytes )
478         {
479             msg_Warn( p_filter, "Failed to convert string to unicode (%s), "
480                       "bytes left %d", strerror(errno), i_in_bytes );
481             goto error;
482         }
483         *(uint32_t*)p_out_buffer = 0;
484         i_string_length = (i_out_bytes - i_out_bytes_left) / sizeof(uint32_t);
485     }
486
487 #if defined(HAVE_FRIBIDI)
488     {
489         uint32_t *p_fribidi_string;
490         FriBidiCharType base_dir = FRIBIDI_TYPE_ON;
491         p_fribidi_string = malloc( (i_string_length + 1) * sizeof(uint32_t) );
492         fribidi_log2vis( (FriBidiChar*)p_unicode_string, i_string_length,
493                          &base_dir, (FriBidiChar*)p_fribidi_string, 0, 0, 0 );
494         free( p_unicode_string );
495         p_unicode_string = p_fribidi_string;
496         p_fribidi_string[ i_string_length ] = 0;
497     }
498 #endif
499 #endif
500
501     /* Calculate relative glyph positions and a bounding box for the
502      * entire string */
503     p_line = NewLine( psz_string );
504     if( p_line == NULL )
505     {
506         msg_Err( p_filter, "Out of memory" );
507         goto error;
508     }
509     p_string->p_lines = p_line;
510     i_pen_x = 0;
511     i_pen_y = 0;
512     i_previous = 0;
513     i = 0;
514
515 #define face p_sys->p_face
516 #define glyph face->glyph
517
518     while( *p_unicode_string )
519     {
520         i_char = *p_unicode_string++;
521         if( i_char == '\r' ) /* ignore CR chars wherever they may be */
522         {
523             continue;
524         }
525
526         if( i_char == '\n' )
527         {
528             p_next = NewLine( psz_string );
529             if( p_next == NULL )
530             {
531                 msg_Err( p_filter, "Out of memory" );
532                 goto error;
533             }
534             p_line->p_next = p_next;
535             p_line->i_width = line.xMax;
536             p_line->i_height = face->size->metrics.height >> 6;
537             p_line->pp_glyphs[ i ] = NULL;
538             p_line = p_next;
539             result.x = __MAX( result.x, line.xMax );
540             result.y += face->size->metrics.height >> 6;
541             i_pen_x = 0;
542             line.xMin = 0;
543             line.xMax = 0;
544             line.yMin = 0;
545             line.yMax = 0;
546             i_pen_y += face->size->metrics.height >> 6;
547 #if 0
548             msg_Dbg( p_filter, "Creating new line, i is %d", i );
549 #endif
550             i = 0;
551             continue;
552         }
553
554         i_glyph_index = FT_Get_Char_Index( face, i_char );
555         if( p_sys->i_use_kerning && i_glyph_index
556             && i_previous )
557         {
558             FT_Vector delta;
559             FT_Get_Kerning( face, i_previous, i_glyph_index,
560                             ft_kerning_default, &delta );
561             i_pen_x += delta.x >> 6;
562
563         }
564         p_line->p_glyph_pos[ i ].x = i_pen_x;
565         p_line->p_glyph_pos[ i ].y = i_pen_y;
566         i_error = FT_Load_Glyph( face, i_glyph_index, FT_LOAD_DEFAULT );
567         if( i_error )
568         {
569             msg_Err( p_filter, "FT_Load_Glyph returned %d", i_error );
570             goto error;
571         }
572         i_error = FT_Get_Glyph( glyph, &tmp_glyph );
573         if( i_error )
574         {
575             msg_Err( p_filter, "FT_Get_Glyph returned %d", i_error );
576             goto error;
577         }
578         FT_Glyph_Get_CBox( tmp_glyph, ft_glyph_bbox_pixels, &glyph_size );
579         i_error = FT_Glyph_To_Bitmap( &tmp_glyph, ft_render_mode_normal,
580                                       NULL, 1 );
581         if( i_error ) continue;
582         p_line->pp_glyphs[ i ] = (FT_BitmapGlyph)tmp_glyph;
583
584         /* Do rest */
585         line.xMax = p_line->p_glyph_pos[i].x + glyph_size.xMax - glyph_size.xMin;
586         line.yMax = __MAX( line.yMax, glyph_size.yMax );
587         line.yMin = __MIN( line.yMin, glyph_size.yMin );
588
589         i_previous = i_glyph_index;
590         i_pen_x += glyph->advance.x >> 6;
591         i++;
592     }
593
594     p_line->i_width = line.xMax;
595     p_line->i_height = face->size->metrics.height >> 6;
596     p_line->pp_glyphs[ i ] = NULL;
597     result.x = __MAX( result.x, line.xMax );
598     result.y += line.yMax - line.yMin;
599     p_string->i_height = result.y;
600     p_string->i_width = result.x;
601
602 #undef face
603 #undef glyph
604
605     Render( p_filter, p_subpic, p_string );
606     FreeString( p_string );
607     block_Release( p_block );
608     return p_subpic;
609
610  error:
611     FreeString( p_string );
612     if( p_subpic ) p_filter->pf_spu_buffer_del( p_filter, p_subpic );
613     block_Release( p_block );
614     return NULL;
615 }
616
617 static void FreeString( subpicture_data_t *p_string )
618 {
619     unsigned int i;
620     line_desc_t *p_line, *p_next;
621
622     if( !p_string ) return;
623
624     for( p_line = p_string->p_lines; p_line != NULL; p_line = p_next )
625     {
626         p_next = p_line->p_next;
627         for( i = 0; p_line->pp_glyphs[ i ] != NULL; i++ )
628         {
629             FT_Done_Glyph( (FT_Glyph)p_line->pp_glyphs[ i ] );
630         }
631         free( p_line->pp_glyphs );
632         free( p_line->p_glyph_pos );
633         free( p_line );
634     }
635
636     free( p_string->psz_text );
637     free( p_string );
638 }
639
640 #if !defined( HAVE_ICONV )
641 /* convert one or more utf8 bytes into a unicode character */
642 static int GetUnicodeCharFromUTF8( byte_t **ppsz_utf8_string )
643 {
644     int i_remaining_bytes, i_char = 0;
645     if( ( **ppsz_utf8_string & 0xFC ) == 0xFC )
646     {
647         i_char = **ppsz_utf8_string & 1;
648         i_remaining_bytes = 5;
649     }
650     else if( ( **ppsz_utf8_string & 0xF8 ) == 0xF8 )
651     {
652         i_char = **ppsz_utf8_string & 3;
653         i_remaining_bytes = 4;
654     }
655     else if( ( **ppsz_utf8_string & 0xF0 ) == 0xF0 )
656     {
657         i_char = **ppsz_utf8_string & 7;
658         i_remaining_bytes = 3;
659     }
660     else if( ( **ppsz_utf8_string & 0xE0 ) == 0xE0 )
661     {
662         i_char = **ppsz_utf8_string & 15;
663         i_remaining_bytes = 2;
664     }
665     else if( ( **ppsz_utf8_string & 0xC0 ) == 0xC0 )
666     {
667         i_char = **ppsz_utf8_string & 31;
668         i_remaining_bytes = 1;
669     }
670     else
671     {
672         i_char = **ppsz_utf8_string;
673         i_remaining_bytes = 0;
674     }
675     while( i_remaining_bytes )
676     {
677         (*ppsz_utf8_string)++;
678         i_remaining_bytes--;
679         i_char = ( i_char << 6 ) + ( **ppsz_utf8_string & 0x3F );
680     }
681     (*ppsz_utf8_string)++;
682     return i_char;
683 }
684 #endif
685
686 static line_desc_t *NewLine( byte_t *psz_string )
687 {
688     int i_count;
689     line_desc_t *p_line = malloc( sizeof(line_desc_t) );
690     if( !p_line )
691     {
692         return NULL;
693     }
694     p_line->i_height = 0;
695     p_line->i_width = 0;
696     p_line->p_next = NULL;
697
698     /* We don't use CountUtf8Characters() here because we are not acutally
699      * sure the string is utf8. Better be safe than sorry. */
700     i_count = strlen( psz_string );
701
702     p_line->pp_glyphs = malloc( sizeof(FT_BitmapGlyph)
703                                 * ( i_count + 1 ) );
704     if( p_line->pp_glyphs == NULL )
705     {
706         free( p_line );
707         return NULL;
708     }
709     p_line->p_glyph_pos = malloc( sizeof( FT_Vector )
710                                   * i_count + 1 );
711     if( p_line->p_glyph_pos == NULL )
712     {
713         free( p_line->pp_glyphs );
714         free( p_line );
715         return NULL;
716     }
717
718     return p_line;
719 }