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