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