1
/*****************************************************************************
2
* freetype.c : Put text on the video, using freetype2
3
*****************************************************************************
4
* Copyright (C) 2002, 2003 VideoLAN
5
* $Id: freetype.c 6961 2004-03-05 17:34:23Z sam $
7
* Authors: Sigmund Augdal <sigmunau@idi.ntnu.no>
9
* This program is free software; you can redistribute it and/or modify
10
* it under the terms of the GNU General Public License as published by
11
* the Free Software Foundation; either version 2 of the License, or
12
* (at your option) any later version.
14
* This program is distributed in the hope that it will be useful,
15
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
* GNU General Public License for more details.
19
* You should have received a copy of the GNU General Public License
20
* along with this program; if not, write to the Free Software
21
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22
*****************************************************************************/
24
/*****************************************************************************
26
*****************************************************************************/
27
#include <stdlib.h> /* malloc(), free() */
30
#ifdef HAVE_LINUX_LIMITS_H
31
# include <linux/limits.h>
44
#include FT_FREETYPE_H
48
#define DEFAULT_FONT "/System/Library/Fonts/LucidaGrande.dfont"
49
#elif defined( SYS_BEOS )
50
#define DEFAULT_FONT "/boot/beos/etc/fonts/ttfonts/Swiss721.ttf"
51
#elif defined( WIN32 )
52
#define DEFAULT_FONT "" /* Default font found at run-time */
54
#define DEFAULT_FONT "/usr/share/fonts/truetype/freefont/FreeSerifBold.ttf"
57
#if defined(HAVE_ICONV)
60
#if defined(HAVE_FRIBIDI)
61
#include <fribidi/fribidi.h>
64
typedef struct line_desc_t line_desc_t;
66
/*****************************************************************************
68
*****************************************************************************/
69
static int Create ( vlc_object_t * );
70
static void Destroy ( vlc_object_t * );
72
static void Render ( vout_thread_t *, picture_t *,
73
const subpicture_t * );
74
static void RenderI420( vout_thread_t *, picture_t *,
75
const subpicture_t * );
76
static void RenderYUY2( vout_thread_t *, picture_t *,
77
const subpicture_t * );
78
static void RenderRV32( vout_thread_t *, picture_t *,
79
const subpicture_t * );
80
static subpicture_t *AddText ( vout_thread_t *, char *, text_style_t *, int,
81
int, int, mtime_t, mtime_t );
83
static void FreeString( subpicture_t * );
85
#if !defined(HAVE_ICONV)
86
static int GetUnicodeCharFromUTF8( byte_t ** );
89
static line_desc_t *NewLine( byte_t * );
91
/*****************************************************************************
93
*****************************************************************************/
94
#define FONT_TEXT N_("Font")
95
#define FONT_LONGTEXT N_("Font filename")
96
#define FONTSIZE_TEXT N_("Font size in pixels")
97
#define FONTSIZE_LONGTEXT N_("The size of the fonts used by the osd module. " \
98
"If set to something different than 0 this option will override the " \
99
"relative font size " )
100
#define FONTSIZER_TEXT N_("Font size")
101
#define FONTSIZER_LONGTEXT N_("The size of the fonts used by the osd module" )
103
static int pi_sizes[] = { 20, 18, 16, 12, 6 };
104
static char *ppsz_sizes_text[] = { N_("Smaller"), N_("Small"), N_("Normal"),
105
N_("Large"), N_("Larger") };
108
set_description( _("freetype2 font renderer") );
110
add_file( "freetype-font", DEFAULT_FONT, NULL, FONT_TEXT, FONT_LONGTEXT,
112
add_integer( "freetype-fontsize", 0, NULL, FONTSIZE_TEXT,
113
FONTSIZE_LONGTEXT, VLC_TRUE );
114
add_integer( "freetype-rel-fontsize", 16, NULL, FONTSIZER_TEXT,
115
FONTSIZER_LONGTEXT, VLC_FALSE );
116
change_integer_list( pi_sizes, ppsz_sizes_text, 0 );
118
set_capability( "text renderer", 100 );
119
add_shortcut( "text" );
120
set_callbacks( Create, Destroy );
124
* Private data in a subpicture. Describes a string.
126
struct subpicture_sys_t
133
/** The string associated with this subpicture */
135
line_desc_t *p_lines;
140
/** NULL-terminated list of glyphs making the string */
141
FT_BitmapGlyph *pp_glyphs;
142
/** list of relative positions for the glyphs */
143
FT_Vector *p_glyph_pos;
149
/*****************************************************************************
150
* text_renderer_sys_t: freetype local data
151
*****************************************************************************
152
* This structure is part of the video output thread descriptor.
153
* It describes the freetype specific properties of an output thread.
154
*****************************************************************************/
155
struct text_renderer_sys_t
157
FT_Library p_library; /* handle to library */
158
FT_Face p_face; /* handle to face object */
160
vlc_bool_t i_use_kerning;
161
uint8_t pi_gamma[256];
164
/*****************************************************************************
165
* Create: allocates osd-text video thread output method
166
*****************************************************************************
167
* This function allocates and initializes a Clone vout method.
168
*****************************************************************************/
169
#define gamma_value 2.0
170
static int Create( vlc_object_t *p_this )
172
vout_thread_t *p_vout = (vout_thread_t *)p_this;
176
double gamma_inv = 1.0f / gamma_value;
179
/* Allocate structure */
180
p_vout->p_text_renderer_data = malloc( sizeof( text_renderer_sys_t ) );
181
if( p_vout->p_text_renderer_data == NULL )
183
msg_Err( p_vout, "out of memory" );
187
for (i = 0; i < 256; i++) {
188
p_vout->p_text_renderer_data->pi_gamma[i] =
189
(uint8_t)( pow( (double)i / 255.0f, gamma_inv) * 255.0f );
192
var_Create( p_vout, "freetype-font", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
193
var_Create( p_vout, "freetype-fontsize",
194
VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
195
var_Create( p_vout, "freetype-rel-fontsize",
196
VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
198
/* Look what method was requested */
199
var_Get( p_vout, "freetype-font", &val );
200
psz_fontfile = val.psz_string;
202
if( !psz_fontfile || !*psz_fontfile )
204
if( psz_fontfile ) free( psz_fontfile );
205
psz_fontfile = (char *)malloc( PATH_MAX + 1 );
207
GetWindowsDirectory( psz_fontfile, PATH_MAX + 1 );
208
strcat( psz_fontfile, "\\fonts\\arial.ttf" );
210
strcpy( psz_fontfile, DEFAULT_FONT );
212
msg_Err( p_vout, "user didn't specify a font" );
213
free( p_vout->p_text_renderer_data );
218
i_error = FT_Init_FreeType( &p_vout->p_text_renderer_data->p_library );
221
msg_Err( p_vout, "couldn't initialize freetype" );
222
free( p_vout->p_text_renderer_data );
226
i_error = FT_New_Face( p_vout->p_text_renderer_data->p_library,
227
psz_fontfile ? psz_fontfile : "", 0,
228
&p_vout->p_text_renderer_data->p_face );
229
if( i_error == FT_Err_Unknown_File_Format )
231
msg_Err( p_vout, "file %s have unknown format", psz_fontfile );
232
FT_Done_FreeType( p_vout->p_text_renderer_data->p_library );
233
free( p_vout->p_text_renderer_data );
234
if( psz_fontfile ) free( psz_fontfile );
239
msg_Err( p_vout, "failed to load font file %s", psz_fontfile );
240
FT_Done_FreeType( p_vout->p_text_renderer_data->p_library );
241
free( p_vout->p_text_renderer_data );
242
if( psz_fontfile ) free( psz_fontfile );
245
if( psz_fontfile ) free( psz_fontfile );
247
i_error = FT_Select_Charmap( p_vout->p_text_renderer_data->p_face,
248
ft_encoding_unicode );
251
msg_Err( p_vout, "Font has no unicode translation table" );
252
FT_Done_Face( p_vout->p_text_renderer_data->p_face );
253
FT_Done_FreeType( p_vout->p_text_renderer_data->p_library );
254
free( p_vout->p_text_renderer_data );
258
p_vout->p_text_renderer_data->i_use_kerning =
259
FT_HAS_KERNING(p_vout->p_text_renderer_data->p_face);
261
var_Get( p_vout, "freetype-fontsize", &val );
265
i_fontsize = val.i_int;
269
var_Get( p_vout, "freetype-rel-fontsize", &val );
270
i_fontsize = (int) p_vout->render.i_height / val.i_int;
272
msg_Dbg( p_vout, "Using fontsize: %i", i_fontsize);
274
i_error = FT_Set_Pixel_Sizes( p_vout->p_text_renderer_data->p_face, 0,
278
msg_Err( p_vout, "couldn't set font size to %d", i_fontsize );
279
free( p_vout->p_text_renderer_data );
282
p_vout->pf_add_string = AddText;
286
/*****************************************************************************
287
* Destroy: destroy Clone video thread output method
288
*****************************************************************************
289
* Clean up all data and library connections
290
*****************************************************************************/
291
static void Destroy( vlc_object_t *p_this )
293
vout_thread_t *p_vout = (vout_thread_t *)p_this;
294
FT_Done_Face( p_vout->p_text_renderer_data->p_face );
295
FT_Done_FreeType( p_vout->p_text_renderer_data->p_library );
296
free( p_vout->p_text_renderer_data );
299
/*****************************************************************************
300
* Render: place string in picture
301
*****************************************************************************
302
* This function merges the previously rendered freetype glyphs into a picture
303
*****************************************************************************/
304
static void Render( vout_thread_t *p_vout, picture_t *p_pic,
305
const subpicture_t *p_subpic )
307
switch( p_vout->output.i_chroma )
309
/* I420 target, no scaling */
310
case VLC_FOURCC('I','4','2','0'):
311
case VLC_FOURCC('I','Y','U','V'):
312
case VLC_FOURCC('Y','V','1','2'):
313
RenderI420( p_vout, p_pic, p_subpic );
316
/* RV16 target, scaling */
317
case VLC_FOURCC('R','V','1','6'):
318
RenderRV16( p_vout, p_pic, p_subpic );
321
/* RV32 target, scaling */
322
case VLC_FOURCC('R','V','2','4'):
323
case VLC_FOURCC('R','V','3','2'):
324
RenderRV32( p_vout, p_pic, p_subpic );
326
/* NVidia or BeOS overlay, no scaling */
327
case VLC_FOURCC('Y','U','Y','2'):
328
RenderYUY2( p_vout, p_pic, p_subpic );
332
msg_Err( p_vout, "unknown chroma, can't render SPU" );
338
* Draw a string on a i420 (or similar) picture
340
static void RenderI420( vout_thread_t *p_vout, picture_t *p_pic,
341
const subpicture_t *p_subpic )
343
subpicture_sys_t *p_string = p_subpic->p_sys;
344
int i_plane, x, y, pen_x, pen_y;
348
for( p_line = p_subpic->p_sys->p_lines; p_line != NULL; p_line = p_line->p_next )
350
for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
353
int i_pic_pitch = p_pic->p[ i_plane ].i_pitch;
354
int i_pic_width = p_pic->p[ i_plane ].i_visible_pitch;
356
p_in = p_pic->p[ i_plane ].p_pixels;
360
if ( p_string->i_flags & OSD_ALIGN_BOTTOM )
362
pen_y = p_pic->p[ i_plane ].i_lines - p_string->i_height -
363
p_string->i_y_margin;
367
pen_y = p_string->i_y_margin;
369
pen_y += p_vout->p_text_renderer_data->p_face->size->metrics.ascender >> 6;
370
if ( p_string->i_flags & OSD_ALIGN_RIGHT )
372
pen_x = i_pic_width - p_line->i_width
373
- p_string->i_x_margin;
375
else if ( p_string->i_flags & OSD_ALIGN_LEFT )
377
pen_x = p_string->i_x_margin;
381
pen_x = i_pic_width / 2 - p_line->i_width / 2
382
+ p_string->i_x_margin;
385
for( i = 0; p_line->pp_glyphs[i] != NULL; i++ )
387
FT_BitmapGlyph p_glyph = p_line->pp_glyphs[ i ];
388
#define alpha p_vout->p_text_renderer_data->pi_gamma[ p_glyph->bitmap.buffer[ x + y * p_glyph->bitmap.width ] ]
389
#define pixel p_in[ ( p_line->p_glyph_pos[ i ].y + pen_y + y - p_glyph->top ) * i_pic_pitch + x + pen_x + p_line->p_glyph_pos[ i ].x + p_glyph->left ]
390
for(y = 0; y < p_glyph->bitmap.rows; y++ )
392
for( x = 0; x < p_glyph->bitmap.width; x++ )
395
pixel = ( ( pixel * ( 255 - alpha ) ) >> 8 );
397
pixel = ( ( pixel * ( 255 - alpha ) ) >> 8 );
399
pixel = ( ( pixel * ( 255 - alpha ) ) >> 8 );
401
pixel = ( ( pixel * ( 255 - alpha ) ) >> 8 );
405
for(y = 0; y < p_glyph->bitmap.rows; y++ )
407
for( x = 0; x < p_glyph->bitmap.width; x++ )
409
pixel = ( ( pixel * ( 255 - alpha ) ) >> 8 ) +
410
( 255 * alpha >> 8 );
419
if ( p_string->i_flags & OSD_ALIGN_BOTTOM )
421
pen_y = p_pic->p[i_plane].i_lines - ( p_string->i_height>>1) -
422
(p_string->i_y_margin>>1);
426
pen_y = p_string->i_y_margin >> 1;
428
pen_y += p_vout->p_text_renderer_data->p_face->size->metrics.ascender >> 7;
429
if ( p_string->i_flags & OSD_ALIGN_RIGHT )
431
pen_x = i_pic_width - ( p_line->i_width >> 1 )
432
- ( p_string->i_x_margin >> 1 );
434
else if ( p_string->i_flags & OSD_ALIGN_LEFT )
436
pen_x = p_string->i_x_margin >> 1;
440
pen_x = i_pic_width / 2 - p_line->i_width / 4
441
+ p_string->i_x_margin / 2;
444
for( i = 0; p_line->pp_glyphs[i] != NULL; i++ )
446
FT_BitmapGlyph p_glyph = p_line->pp_glyphs[ i ];
447
#define alpha p_vout->p_text_renderer_data->pi_gamma[ p_glyph->bitmap.buffer[ ( x + y * p_glyph->bitmap.width ) ] ]
448
#define pixel p_in[ ( ( p_line->p_glyph_pos[ i ].y >> 1 ) + pen_y + ( y >> 1 ) - ( p_glyph->top >> 1 ) ) * i_pic_pitch + ( x >> 1 ) + pen_x + ( p_line->p_glyph_pos[ i ].x >> 1 ) + ( p_glyph->left >>1) ]
449
for( y = 0; y < p_glyph->bitmap.rows; y+=2 )
451
for( x = 0; x < p_glyph->bitmap.width; x+=2 )
453
pixel = ( ( pixel * ( 0xFF - alpha ) ) >> 8 ) +
454
( 0x80 * alpha >> 8 );
466
* Draw a string on a YUY2 picture
468
static void RenderYUY2( vout_thread_t *p_vout, picture_t *p_pic,
469
const subpicture_t *p_subpic )
471
subpicture_sys_t *p_string = p_subpic->p_sys;
472
int x, y, pen_x, pen_y;
476
for( p_line = p_subpic->p_sys->p_lines; p_line != NULL;
477
p_line = p_line->p_next )
480
int i_pic_pitch = p_pic->p[0].i_pitch;
481
int i_pic_width = p_pic->p[0].i_visible_pitch;
483
p_in = p_pic->p[0].p_pixels;
485
if ( p_string->i_flags & OSD_ALIGN_BOTTOM )
487
pen_y = p_pic->p[0].i_lines - p_string->i_height -
488
p_string->i_y_margin;
492
pen_y = p_string->i_y_margin;
494
pen_y += p_vout->p_text_renderer_data->p_face->size->metrics.ascender >> 6;
495
if ( p_string->i_flags & OSD_ALIGN_RIGHT )
497
pen_x = i_pic_width - p_line->i_width
498
- p_string->i_x_margin;
500
else if ( p_string->i_flags & OSD_ALIGN_LEFT )
502
pen_x = p_string->i_x_margin;
506
pen_x = i_pic_width / 2 /2 - p_line->i_width / 2 + p_string->i_x_margin;
509
for( i = 0; p_line->pp_glyphs[i] != NULL; i++ )
511
FT_BitmapGlyph p_glyph = p_line->pp_glyphs[ i ];
512
#define alpha p_vout->p_text_renderer_data->pi_gamma[ p_glyph->bitmap.buffer[ x + y * p_glyph->bitmap.width ] ]
513
#define pixel p_in[ ( p_line->p_glyph_pos[ i ].y + pen_y + y - p_glyph->top ) * i_pic_pitch + 2 * ( x + pen_x + p_line->p_glyph_pos[ i ].x + p_glyph->left ) ]
514
for(y = 0; y < p_glyph->bitmap.rows; y++ )
516
for( x = 0; x < p_glyph->bitmap.width; x++ )
519
pixel = ( ( pixel * ( 255 - alpha ) ) >> 8 );
521
pixel = ( ( pixel * ( 255 - alpha ) ) >> 8 );
523
pixel = ( ( pixel * ( 255 - alpha ) ) >> 8 );
525
pixel = ( ( pixel * ( 255 - alpha ) ) >> 8 );
529
for(y = 0; y < p_glyph->bitmap.rows; y++ )
531
for( x = 0; x < p_glyph->bitmap.width; x++ )
533
pixel = ( ( pixel * ( 255 - alpha ) ) >> 8 ) +
534
( 255 * alpha >> 8 );
544
* Draw a string on a RV32 picture
546
static void RenderRV32( vout_thread_t *p_vout, picture_t *p_pic,
547
const subpicture_t *p_subpic )
549
subpicture_sys_t *p_string = p_subpic->p_sys;
550
int i_plane, x, y, pen_x, pen_y;
556
for( p_line = p_subpic->p_sys->p_lines; p_line != NULL; p_line = p_line->p_next )
559
int i_pic_pitch = p_pic->p[ i_plane ].i_pitch;
560
int i_pic_width = p_pic->p[ i_plane ].i_visible_pitch;
562
p_in = p_pic->p[ i_plane ].p_pixels;
564
if ( p_string->i_flags & OSD_ALIGN_BOTTOM )
566
pen_y = p_pic->p[ i_plane ].i_lines - p_string->i_height -
567
p_string->i_y_margin;
571
pen_y = p_string->i_y_margin;
573
pen_y += p_vout->p_text_renderer_data->p_face->size->metrics.ascender >> 6;
574
if ( p_string->i_flags & OSD_ALIGN_RIGHT )
576
pen_x = i_pic_width - p_line->i_width
577
- p_string->i_x_margin;
579
else if ( p_string->i_flags & OSD_ALIGN_LEFT )
581
pen_x = p_string->i_x_margin;
585
pen_x = i_pic_width / 2 / 4 - p_line->i_width / 2
586
+ p_string->i_x_margin;
589
for( i = 0; p_line->pp_glyphs[i] != NULL; i++ )
591
FT_BitmapGlyph p_glyph = p_line->pp_glyphs[ i ];
592
#define alpha p_vout->p_text_renderer_data->pi_gamma[ p_glyph->bitmap.buffer[ x + y * p_glyph->bitmap.width ] ]
593
#define pixel( c ) p_in[ ( p_line->p_glyph_pos[ i ].y + pen_y + y - p_glyph->top ) * i_pic_pitch + ( x + pen_x + p_line->p_glyph_pos[ i ].x + p_glyph->left ) * 4 + c ]
594
for(y = 0; y < p_glyph->bitmap.rows; y++ )
596
for( x = 0; x < p_glyph->bitmap.width; x++ )
599
pixel( 0 ) = ( ( pixel( 0 ) * ( 255 - alpha ) ) >> 8 );
600
pixel( 1 ) = ( ( pixel( 1 ) * ( 255 - alpha ) ) >> 8 );
601
pixel( 2 ) = ( ( pixel( 2 ) * ( 255 - alpha ) ) >> 8 );
603
pixel( 0 ) = ( ( pixel( 0 ) * ( 255 - alpha ) ) >> 8 );
604
pixel( 1 ) = ( ( pixel( 1 ) * ( 255 - alpha ) ) >> 8 );
605
pixel( 2 ) = ( ( pixel( 2 ) * ( 255 - alpha ) ) >> 8 );
607
pixel( 0 ) = ( ( pixel( 0 ) * ( 255 - alpha ) ) >> 8 );
608
pixel( 1 ) = ( ( pixel( 1 ) * ( 255 - alpha ) ) >> 8 );
609
pixel( 2 ) = ( ( pixel( 2 ) * ( 255 - alpha ) ) >> 8 );
611
pixel( 0 ) = ( ( pixel( 0 ) * ( 255 - alpha ) ) >> 8 );
612
pixel( 1 ) = ( ( pixel( 1 ) * ( 255 - alpha ) ) >> 8 );
613
pixel( 2 ) = ( ( pixel( 2 ) * ( 255 - alpha ) ) >> 8 );
617
for(y = 0; y < p_glyph->bitmap.rows; y++ )
619
for( x = 0; x < p_glyph->bitmap.width; x++ )
621
pixel( 0 ) = ( ( pixel( 0 ) * ( 255 - alpha ) ) >> 8 ) +
622
( 255 * alpha >> 8 );
623
pixel( 1 ) = ( ( pixel( 1 ) * ( 255 - alpha ) ) >> 8 ) +
624
( 255 * alpha >> 8 );
625
pixel( 2 ) = ( ( pixel( 2 ) * ( 255 - alpha ) ) >> 8 ) +
626
( 255 * alpha >> 8 );
636
* This function receives a string and creates a subpicture for it. It
637
* also calculates the size needed for this string, and renders the
638
* needed glyphs into memory. It is used as pf_add_string callback in
639
* the vout method by this module
641
static subpicture_t *AddText ( vout_thread_t *p_vout, char *psz_string,
642
text_style_t *p_style, int i_flags, int i_hmargin,
643
int i_vmargin, mtime_t i_start, mtime_t i_stop )
645
subpicture_sys_t *p_string;
646
int i, i_pen_y, i_pen_x, i_error, i_glyph_index, i_previous;
647
subpicture_t *p_subpic;
648
line_desc_t *p_line, *p_next;
649
uint32_t *p_unicode_string, i_char;
652
#if defined(HAVE_ICONV)
653
iconv_t iconv_handle;
662
if ( !psz_string || !*psz_string )
678
/* Create and initialize a subpicture */
679
p_subpic = vout_CreateSubPicture( p_vout, MEMORY_SUBPICTURE );
680
if ( p_subpic == NULL )
685
p_subpic->pf_render = Render;
686
p_subpic->pf_destroy = FreeString;
687
p_subpic->i_start = i_start;
688
p_subpic->i_stop = i_stop;
691
p_subpic->b_ephemer = VLC_TRUE;
695
p_subpic->b_ephemer = VLC_FALSE;
698
/* Create and initialize private data for the subpicture */
699
p_string = malloc( sizeof(subpicture_sys_t) );
700
if ( p_string == NULL )
702
msg_Err( p_vout, "Out of memory" );
705
p_subpic->p_sys = p_string;
706
p_string->i_flags = i_flags;
707
p_string->i_x_margin = i_hmargin;
708
p_string->i_y_margin = i_vmargin;
709
p_string->p_lines = 0;
710
p_string->psz_text = strdup( psz_string );
712
#if defined(HAVE_ICONV)
713
p_unicode_string = malloc( ( strlen(psz_string) + 1 ) * sizeof(uint32_t) );
714
if( p_unicode_string == NULL )
716
msg_Err( p_vout, "Out of memory" );
719
#if defined(WORDS_BIGENDIAN)
720
iconv_handle = iconv_open( "UCS-4BE", "UTF-8" );
722
iconv_handle = iconv_open( "UCS-4LE", "UTF-8" );
724
if( iconv_handle == (iconv_t)-1 )
726
msg_Warn( p_vout, "Unable to do convertion" );
731
char *p_in_buffer, *p_out_buffer;
732
size_t i_in_bytes, i_out_bytes, i_out_bytes_left, i_ret;
733
i_in_bytes = strlen( psz_string );
734
i_out_bytes = i_in_bytes * sizeof( uint32_t );
735
i_out_bytes_left = i_out_bytes;
736
p_in_buffer = psz_string;
737
p_out_buffer = (char *)p_unicode_string;
738
i_ret = iconv( iconv_handle, &p_in_buffer, &i_in_bytes, &p_out_buffer, &i_out_bytes_left );
741
msg_Warn( p_vout, "Failed to convert string to unicode (%s), bytes left %d", strerror(errno), i_in_bytes );
744
*(uint32_t*)p_out_buffer = 0;
745
i_string_length = ( i_out_bytes - i_out_bytes_left ) / sizeof(uint32_t);
748
#if defined(HAVE_FRIBIDI)
750
uint32_t *p_fribidi_string;
751
FriBidiCharType base_dir = FRIBIDI_TYPE_ON;
752
p_fribidi_string = malloc( ( i_string_length + 1 ) * sizeof(uint32_t) );
753
fribidi_log2vis( (FriBidiChar*)p_unicode_string, i_string_length,
754
&base_dir, (FriBidiChar*)p_fribidi_string, NULL, NULL,
756
free( p_unicode_string );
757
p_unicode_string = p_fribidi_string;
758
p_fribidi_string[ i_string_length ] = 0;
763
/* Calculate relative glyph positions and a bounding box for the
765
p_line = NewLine( psz_string );
768
msg_Err( p_vout, "Out of memory" );
771
p_string->p_lines = p_line;
777
#define face p_vout->p_text_renderer_data->p_face
778
#define glyph face->glyph
780
while( *p_unicode_string )
782
i_char = *p_unicode_string++;
783
if ( i_char == '\r' ) /* ignore CR chars wherever they may be */
788
if ( i_char == '\n' )
790
p_next = NewLine( psz_string );
793
msg_Err( p_vout, "Out of memory" );
796
p_line->p_next = p_next;
797
p_line->i_width = line.xMax;
798
p_line->i_height = face->size->metrics.height >> 6;
799
p_line->pp_glyphs[ i ] = NULL;
801
result.x = __MAX( result.x, line.xMax );
802
result.y += face->size->metrics.height >> 6;
808
i_pen_y += face->size->metrics.height >> 6;
809
msg_Dbg( p_vout, "Creating new line, i is %d", i );
814
i_glyph_index = FT_Get_Char_Index( face, i_char );
815
if ( p_vout->p_text_renderer_data->i_use_kerning && i_glyph_index
819
FT_Get_Kerning( face, i_previous, i_glyph_index,
820
ft_kerning_default, &delta );
821
i_pen_x += delta.x >> 6;
824
p_line->p_glyph_pos[ i ].x = i_pen_x;
825
p_line->p_glyph_pos[ i ].y = i_pen_y;
826
i_error = FT_Load_Glyph( face, i_glyph_index, FT_LOAD_DEFAULT );
829
msg_Err( p_vout, "FT_Load_Glyph returned %d", i_error );
832
i_error = FT_Get_Glyph( glyph, &tmp_glyph );
835
msg_Err( p_vout, "FT_Get_Glyph returned %d", i_error );
838
FT_Glyph_Get_CBox( tmp_glyph, ft_glyph_bbox_pixels, &glyph_size );
839
i_error = FT_Glyph_To_Bitmap( &tmp_glyph, ft_render_mode_normal,
841
if ( i_error ) continue;
842
p_line->pp_glyphs[ i ] = (FT_BitmapGlyph)tmp_glyph;
845
line.xMax = p_line->p_glyph_pos[i].x + glyph_size.xMax - glyph_size.xMin;
846
line.yMax = __MAX( line.yMax, glyph_size.yMax );
847
line.yMin = __MIN( line.yMin, glyph_size.yMin );
849
i_previous = i_glyph_index;
850
i_pen_x += glyph->advance.x >> 6;
853
p_line->i_width = line.xMax;
854
p_line->i_height = face->size->metrics.height >> 6;
855
p_line->pp_glyphs[ i ] = NULL;
856
result.x = __MAX( result.x, line.xMax );
857
result.y += line.yMax - line.yMin;
858
p_string->i_height = result.y;
859
p_string->i_width = result.x;
860
vout_DisplaySubPicture( p_vout, p_subpic );
867
FreeString( p_subpic );
868
vout_DestroySubPicture( p_vout, p_subpic );
872
static void FreeString( subpicture_t *p_subpic )
875
subpicture_sys_t *p_string = p_subpic->p_sys;
876
line_desc_t *p_line, *p_next;
878
if( p_subpic->p_sys == NULL ) return;
880
for( p_line = p_string->p_lines; p_line != NULL; p_line = p_next )
882
p_next = p_line->p_next;
883
for( i = 0; p_line->pp_glyphs[ i ] != NULL; i++ )
885
FT_Done_Glyph( (FT_Glyph)p_line->pp_glyphs[ i ] );
887
free( p_line->pp_glyphs );
888
free( p_line->p_glyph_pos );
892
free( p_string->psz_text );
896
#if !defined( HAVE_ICONV )
897
/* convert one or more utf8 bytes into a unicode character */
898
static int GetUnicodeCharFromUTF8( byte_t **ppsz_utf8_string )
900
int i_remaining_bytes, i_char = 0;
901
if( ( **ppsz_utf8_string & 0xFC ) == 0xFC )
903
i_char = **ppsz_utf8_string & 1;
904
i_remaining_bytes = 5;
906
else if( ( **ppsz_utf8_string & 0xF8 ) == 0xF8 )
908
i_char = **ppsz_utf8_string & 3;
909
i_remaining_bytes = 4;
911
else if( ( **ppsz_utf8_string & 0xF0 ) == 0xF0 )
913
i_char = **ppsz_utf8_string & 7;
914
i_remaining_bytes = 3;
916
else if( ( **ppsz_utf8_string & 0xE0 ) == 0xE0 )
918
i_char = **ppsz_utf8_string & 15;
919
i_remaining_bytes = 2;
921
else if( ( **ppsz_utf8_string & 0xC0 ) == 0xC0 )
923
i_char = **ppsz_utf8_string & 31;
924
i_remaining_bytes = 1;
928
i_char = **ppsz_utf8_string;
929
i_remaining_bytes = 0;
931
while( i_remaining_bytes )
933
(*ppsz_utf8_string)++;
935
i_char = ( i_char << 6 ) + ( **ppsz_utf8_string & 0x3F );
937
(*ppsz_utf8_string)++;
942
static line_desc_t *NewLine( byte_t *psz_string )
945
line_desc_t *p_line = malloc( sizeof(line_desc_t) );
950
p_line->i_height = 0;
952
p_line->p_next = NULL;
954
/* We don't use CountUtf8Characters() here because we are not acutally
955
* sure the string is utf8. Better be safe than sorry. */
956
i_count = strlen( psz_string );
958
p_line->pp_glyphs = malloc( sizeof(FT_BitmapGlyph)
960
if( p_line->pp_glyphs == NULL )
965
p_line->p_glyph_pos = malloc( sizeof( FT_Vector )
967
if( p_line->p_glyph_pos == NULL )
969
free( p_line->pp_glyphs );