]> git.sesse.net Git - vlc/blob - modules/misc/freetype.c
d0ebd4c4e800426416c24ae76c4fa4bed15c7550
[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         msg_Err( p_filter, "freetype-rel-font currenlty broken, FIXME" );
251         i_fontsize = (int) /*p_filter->render.i_height*/ 400 / val.i_int;
252     }
253     msg_Dbg( p_filter, "Using fontsize: %i", i_fontsize);
254
255     i_error = FT_Set_Pixel_Sizes( p_sys->p_face, 0, i_fontsize );
256     if( i_error )
257     {
258         msg_Err( p_filter, "couldn't set font size to %d", i_fontsize );
259         goto error;
260     }
261
262     if( psz_fontfile ) free( psz_fontfile );
263     p_filter->pf_render_string = RenderText;
264     p_filter->p_sys = p_sys;
265     return VLC_SUCCESS;
266
267  error:
268     if( p_sys->p_face ) FT_Done_Face( p_sys->p_face );
269     if( p_sys->p_library ) FT_Done_FreeType( p_sys->p_library );
270     if( psz_fontfile ) free( psz_fontfile );
271     free( p_sys );
272     return VLC_EGENERIC;
273 }
274
275 /*****************************************************************************
276  * Destroy: destroy Clone video thread output method
277  *****************************************************************************
278  * Clean up all data and library connections
279  *****************************************************************************/
280 static void Destroy( vlc_object_t *p_this )
281 {
282     filter_t *p_filter = (filter_t *)p_this;
283     filter_sys_t *p_sys = p_filter->p_sys;
284     FT_Done_Face( p_sys->p_face );
285     FT_Done_FreeType( p_sys->p_library );
286     free( p_sys );
287 }
288
289 /*****************************************************************************
290  * Render: place string in picture
291  *****************************************************************************
292  * This function merges the previously rendered freetype glyphs into a picture
293  *****************************************************************************/
294 static void Render( filter_t *p_filter, subpicture_t *p_spu,
295                     subpicture_data_t *p_string )
296 {
297     filter_sys_t *p_sys = p_filter->p_sys;
298     line_desc_t *p_line;
299     uint8_t *p_y, *p_u, *p_v, *p_a;
300     video_format_t fmt;
301     int i, x, y, i_pitch;
302
303     /* Create a new subpicture region */
304     memset( &fmt, 0, sizeof(video_format_t) );
305     fmt.i_chroma = VLC_FOURCC('Y','U','V','A');
306     fmt.i_aspect = VOUT_ASPECT_FACTOR;
307     fmt.i_width = fmt.i_visible_width = p_string->i_width + 2;
308     fmt.i_height = fmt.i_visible_height = p_string->i_height + 2;
309     fmt.i_x_offset = fmt.i_y_offset = 0;
310     p_spu->p_region = p_spu->pf_create_region( VLC_OBJECT(p_filter), &fmt );
311     if( !p_spu->p_region )
312     {
313         msg_Err( p_filter, "cannot allocate SPU region" );
314         return;
315     }
316
317     p_spu->p_region->i_x = p_spu->p_region->i_y = 0;
318     p_y = p_spu->p_region->picture.Y_PIXELS;
319     p_u = p_spu->p_region->picture.U_PIXELS;
320     p_v = p_spu->p_region->picture.V_PIXELS;
321     p_a = p_spu->p_region->picture.A_PIXELS;
322     i_pitch = p_spu->p_region->picture.Y_PITCH;
323
324     /* Initialize the region pixels (only the alpha will be changed later) */
325     memset( p_y, 0x00, i_pitch * p_spu->p_region->fmt.i_height );
326     memset( p_u, 0x80, i_pitch * p_spu->p_region->fmt.i_height );
327     memset( p_v, 0x80, i_pitch * p_spu->p_region->fmt.i_height );
328     memset( p_a, 0x00, i_pitch * p_spu->p_region->fmt.i_height );
329
330 #define pi_gamma p_sys->pi_gamma
331
332     for( p_line = p_string->p_lines; p_line != NULL; p_line = p_line->p_next )
333     {
334         int i_glyph_tmax = 0;
335         int i_bitmap_offset, i_offset;
336         for( i = 0; p_line->pp_glyphs[i] != NULL; i++ )
337         {
338             FT_BitmapGlyph p_glyph = p_line->pp_glyphs[ i ];
339             i_glyph_tmax = __MAX( i_glyph_tmax, p_glyph->top );
340         }
341
342         for( i = 0; p_line->pp_glyphs[i] != NULL; i++ )
343         {
344             FT_BitmapGlyph p_glyph = p_line->pp_glyphs[ i ];
345
346             i_offset = ( p_line->p_glyph_pos[ i ].y +
347                 i_glyph_tmax - p_glyph->top + 1 ) *
348                 i_pitch + p_line->p_glyph_pos[ i ].x + p_glyph->left + 1;
349
350             for( y = 0, i_bitmap_offset = 0; y < p_glyph->bitmap.rows; y++ )
351             {
352                 for( x = 0; x < p_glyph->bitmap.width; x++, i_bitmap_offset++ )
353                 {
354                     if( !pi_gamma[p_glyph->bitmap.buffer[i_bitmap_offset]] )
355                         continue;
356
357                     i_offset -= i_pitch;
358                     p_a[i_offset + x] = ((uint16_t)p_a[i_offset + x] +
359                       pi_gamma[p_glyph->bitmap.buffer[i_bitmap_offset]])/2;
360                     i_offset += i_pitch; x--;
361                     p_a[i_offset + x] = ((uint16_t)p_a[i_offset + x] +
362                       pi_gamma[p_glyph->bitmap.buffer[i_bitmap_offset]])/2;
363                     x += 2;
364                     p_a[i_offset + x] = ((uint16_t)p_a[i_offset + x] +
365                       pi_gamma[p_glyph->bitmap.buffer[i_bitmap_offset]])/2;
366                     i_offset += i_pitch; x--;
367                     p_a[i_offset + x] = ((uint16_t)p_a[i_offset + x] +
368                       pi_gamma[p_glyph->bitmap.buffer[i_bitmap_offset]])/2;
369                     i_offset -= i_pitch;
370                 }
371                 i_offset += i_pitch;
372             }
373
374             i_offset = ( p_line->p_glyph_pos[ i ].y +
375                 i_glyph_tmax - p_glyph->top ) *
376                 i_pitch + p_line->p_glyph_pos[ i ].x + p_glyph->left + 1;
377
378             for( y = 0, i_bitmap_offset = 0; y < p_glyph->bitmap.rows; y++ )
379             {
380                for( x = 0; x < p_glyph->bitmap.width; x++, i_bitmap_offset++ )
381                {
382                    p_y[i_offset + x] =
383                        pi_gamma[p_glyph->bitmap.buffer[i_bitmap_offset]];
384                }
385                i_offset += i_pitch;
386             }
387
388 #undef pi_gamma
389         }
390     }
391 }
392
393 /**
394  * This function receives a string and creates a subpicture for it. It
395  * also calculates the size needed for this string, and renders the
396  * needed glyphs into memory. It is used as pf_add_string callback in
397  * the vout method by this module
398  */
399 static subpicture_t *RenderText( filter_t *p_filter, block_t *p_block )
400 {
401     filter_sys_t *p_sys = p_filter->p_sys;
402     subpicture_t *p_subpic = 0;
403     subpicture_data_t *p_string = 0;
404     line_desc_t  *p_line = 0, *p_next = 0;
405     int i, i_pen_y, i_pen_x, i_error, i_glyph_index, i_previous;
406     uint32_t *p_unicode_string, i_char;
407     int i_string_length;
408     char *psz_string;
409
410 #if defined(HAVE_ICONV)
411     iconv_t iconv_handle;
412 #endif
413
414     FT_BBox line;
415     FT_BBox glyph_size;
416     FT_Vector result;
417     FT_Glyph tmp_glyph;
418
419     /* Sanity check */
420     if( !p_block ) return NULL;
421     psz_string = p_block->p_buffer;
422     if( !psz_string || !*psz_string ) goto error;
423
424     result.x = 0;
425     result.y = 0;
426     line.xMin = 0;
427     line.xMax = 0;
428     line.yMin = 0;
429     line.yMax = 0;
430
431     /* Create and initialize a subpicture */
432     p_subpic = p_filter->pf_spu_buffer_new( p_filter );
433     if( !p_subpic ) goto error;
434
435     p_subpic->i_start = p_block->i_pts;
436     p_subpic->i_stop = p_block->i_pts + p_block->i_length;
437     p_subpic->b_ephemer = (p_block->i_length == 0);
438     p_subpic->b_absolute = VLC_FALSE;
439
440     /* Create and initialize private data for the subpicture */
441     p_string = malloc( sizeof(subpicture_data_t) );
442     if( !p_string )
443     {
444         msg_Err( p_filter, "Out of memory" );
445         goto error;
446     }
447     p_string->p_lines = 0;
448     p_string->psz_text = strdup( psz_string );
449
450 #if defined(HAVE_ICONV)
451     p_unicode_string = malloc( ( strlen(psz_string) + 1 ) * sizeof(uint32_t) );
452     if( p_unicode_string == NULL )
453     {
454         msg_Err( p_filter, "Out of memory" );
455         goto error;
456     }
457 #if defined(WORDS_BIGENDIAN)
458     iconv_handle = iconv_open( "UCS-4BE", "UTF-8" );
459 #else
460     iconv_handle = iconv_open( "UCS-4LE", "UTF-8" );
461 #endif
462     if( iconv_handle == (iconv_t)-1 )
463     {
464         msg_Warn( p_filter, "Unable to do convertion" );
465         goto error;
466     }
467
468     {
469         char *p_in_buffer, *p_out_buffer;
470         size_t i_in_bytes, i_out_bytes, i_out_bytes_left, i_ret;
471         i_in_bytes = strlen( psz_string );
472         i_out_bytes = i_in_bytes * sizeof( uint32_t );
473         i_out_bytes_left = i_out_bytes;
474         p_in_buffer = psz_string;
475         p_out_buffer = (char *)p_unicode_string;
476         i_ret = iconv( iconv_handle, &p_in_buffer, &i_in_bytes,
477                        &p_out_buffer, &i_out_bytes_left );
478         if( i_in_bytes )
479         {
480             msg_Warn( p_filter, "Failed to convert string to unicode (%s), "
481                       "bytes left %d", strerror(errno), i_in_bytes );
482             goto error;
483         }
484         *(uint32_t*)p_out_buffer = 0;
485         i_string_length = (i_out_bytes - i_out_bytes_left) / sizeof(uint32_t);
486     }
487
488 #if defined(HAVE_FRIBIDI)
489     {
490         uint32_t *p_fribidi_string;
491         FriBidiCharType base_dir = FRIBIDI_TYPE_ON;
492         p_fribidi_string = malloc( (i_string_length + 1) * sizeof(uint32_t) );
493         fribidi_log2vis( (FriBidiChar*)p_unicode_string, i_string_length,
494                          &base_dir, (FriBidiChar*)p_fribidi_string, 0, 0, 0 );
495         free( p_unicode_string );
496         p_unicode_string = p_fribidi_string;
497         p_fribidi_string[ i_string_length ] = 0;
498     }
499 #endif
500 #endif
501
502     /* Calculate relative glyph positions and a bounding box for the
503      * entire string */
504     p_line = NewLine( psz_string );
505     if( p_line == NULL )
506     {
507         msg_Err( p_filter, "Out of memory" );
508         goto error;
509     }
510     p_string->p_lines = p_line;
511     i_pen_x = 0;
512     i_pen_y = 0;
513     i_previous = 0;
514     i = 0;
515
516 #define face p_sys->p_face
517 #define glyph face->glyph
518
519     while( *p_unicode_string )
520     {
521         i_char = *p_unicode_string++;
522         if( i_char == '\r' ) /* ignore CR chars wherever they may be */
523         {
524             continue;
525         }
526
527         if( i_char == '\n' )
528         {
529             p_next = NewLine( psz_string );
530             if( p_next == NULL )
531             {
532                 msg_Err( p_filter, "Out of memory" );
533                 goto error;
534             }
535             p_line->p_next = p_next;
536             p_line->i_width = line.xMax;
537             p_line->i_height = face->size->metrics.height >> 6;
538             p_line->pp_glyphs[ i ] = NULL;
539             p_line = p_next;
540             result.x = __MAX( result.x, line.xMax );
541             result.y += face->size->metrics.height >> 6;
542             i_pen_x = 0;
543             line.xMin = 0;
544             line.xMax = 0;
545             line.yMin = 0;
546             line.yMax = 0;
547             i_pen_y += face->size->metrics.height >> 6;
548 #if 0
549             msg_Dbg( p_filter, "Creating new line, i is %d", i );
550 #endif
551             i = 0;
552             continue;
553         }
554
555         i_glyph_index = FT_Get_Char_Index( face, i_char );
556         if( p_sys->i_use_kerning && i_glyph_index
557             && i_previous )
558         {
559             FT_Vector delta;
560             FT_Get_Kerning( face, i_previous, i_glyph_index,
561                             ft_kerning_default, &delta );
562             i_pen_x += delta.x >> 6;
563
564         }
565         p_line->p_glyph_pos[ i ].x = i_pen_x;
566         p_line->p_glyph_pos[ i ].y = i_pen_y;
567         i_error = FT_Load_Glyph( face, i_glyph_index, FT_LOAD_DEFAULT );
568         if( i_error )
569         {
570             msg_Err( p_filter, "FT_Load_Glyph returned %d", i_error );
571             goto error;
572         }
573         i_error = FT_Get_Glyph( glyph, &tmp_glyph );
574         if( i_error )
575         {
576             msg_Err( p_filter, "FT_Get_Glyph returned %d", i_error );
577             goto error;
578         }
579         FT_Glyph_Get_CBox( tmp_glyph, ft_glyph_bbox_pixels, &glyph_size );
580         i_error = FT_Glyph_To_Bitmap( &tmp_glyph, ft_render_mode_normal,
581                                       NULL, 1 );
582         if( i_error ) continue;
583         p_line->pp_glyphs[ i ] = (FT_BitmapGlyph)tmp_glyph;
584
585         /* Do rest */
586         line.xMax = p_line->p_glyph_pos[i].x + glyph_size.xMax - glyph_size.xMin;
587         line.yMax = __MAX( line.yMax, glyph_size.yMax );
588         line.yMin = __MIN( line.yMin, glyph_size.yMin );
589
590         i_previous = i_glyph_index;
591         i_pen_x += glyph->advance.x >> 6;
592         i++;
593     }
594
595     p_line->i_width = line.xMax;
596     p_line->i_height = face->size->metrics.height >> 6;
597     p_line->pp_glyphs[ i ] = NULL;
598     result.x = __MAX( result.x, line.xMax );
599     result.y += line.yMax - line.yMin;
600     p_string->i_height = result.y;
601     p_string->i_width = result.x;
602
603 #undef face
604 #undef glyph
605
606     Render( p_filter, p_subpic, p_string );
607     FreeString( p_string );
608     block_Release( p_block );
609     return p_subpic;
610
611  error:
612     FreeString( p_string );
613     if( p_subpic ) p_filter->pf_spu_buffer_del( p_filter, p_subpic );
614     block_Release( p_block );
615     return NULL;
616 }
617
618 static void FreeString( subpicture_data_t *p_string )
619 {
620     unsigned int i;
621     line_desc_t *p_line, *p_next;
622
623     if( !p_string ) return;
624
625     for( p_line = p_string->p_lines; p_line != NULL; p_line = p_next )
626     {
627         p_next = p_line->p_next;
628         for( i = 0; p_line->pp_glyphs[ i ] != NULL; i++ )
629         {
630             FT_Done_Glyph( (FT_Glyph)p_line->pp_glyphs[ i ] );
631         }
632         free( p_line->pp_glyphs );
633         free( p_line->p_glyph_pos );
634         free( p_line );
635     }
636
637     free( p_string->psz_text );
638     free( p_string );
639 }
640
641 #if !defined( HAVE_ICONV )
642 /* convert one or more utf8 bytes into a unicode character */
643 static int GetUnicodeCharFromUTF8( byte_t **ppsz_utf8_string )
644 {
645     int i_remaining_bytes, i_char = 0;
646     if( ( **ppsz_utf8_string & 0xFC ) == 0xFC )
647     {
648         i_char = **ppsz_utf8_string & 1;
649         i_remaining_bytes = 5;
650     }
651     else if( ( **ppsz_utf8_string & 0xF8 ) == 0xF8 )
652     {
653         i_char = **ppsz_utf8_string & 3;
654         i_remaining_bytes = 4;
655     }
656     else if( ( **ppsz_utf8_string & 0xF0 ) == 0xF0 )
657     {
658         i_char = **ppsz_utf8_string & 7;
659         i_remaining_bytes = 3;
660     }
661     else if( ( **ppsz_utf8_string & 0xE0 ) == 0xE0 )
662     {
663         i_char = **ppsz_utf8_string & 15;
664         i_remaining_bytes = 2;
665     }
666     else if( ( **ppsz_utf8_string & 0xC0 ) == 0xC0 )
667     {
668         i_char = **ppsz_utf8_string & 31;
669         i_remaining_bytes = 1;
670     }
671     else
672     {
673         i_char = **ppsz_utf8_string;
674         i_remaining_bytes = 0;
675     }
676     while( i_remaining_bytes )
677     {
678         (*ppsz_utf8_string)++;
679         i_remaining_bytes--;
680         i_char = ( i_char << 6 ) + ( **ppsz_utf8_string & 0x3F );
681     }
682     (*ppsz_utf8_string)++;
683     return i_char;
684 }
685 #endif
686
687 static line_desc_t *NewLine( byte_t *psz_string )
688 {
689     int i_count;
690     line_desc_t *p_line = malloc( sizeof(line_desc_t) );
691     if( !p_line )
692     {
693         return NULL;
694     }
695     p_line->i_height = 0;
696     p_line->i_width = 0;
697     p_line->p_next = NULL;
698
699     /* We don't use CountUtf8Characters() here because we are not acutally
700      * sure the string is utf8. Better be safe than sorry. */
701     i_count = strlen( psz_string );
702
703     p_line->pp_glyphs = malloc( sizeof(FT_BitmapGlyph)
704                                 * ( i_count + 1 ) );
705     if( p_line->pp_glyphs == NULL )
706     {
707         free( p_line );
708         return NULL;
709     }
710     p_line->p_glyph_pos = malloc( sizeof( FT_Vector )
711                                   * i_count + 1 );
712     if( p_line->p_glyph_pos == NULL )
713     {
714         free( p_line->pp_glyphs );
715         free( p_line );
716         return NULL;
717     }
718
719     return p_line;
720 }