Commit 6a91698d authored by Jean-Baptiste Kempf's avatar Jean-Baptiste Kempf

Freetype: privatize LoadFont() and document SelectAndLoadFace

parent b5d615ed
......@@ -1361,132 +1361,3 @@ static void Destroy( vlc_object_t *p_this )
free( p_sys );
}
/* Face loading */
int ConvertToLiveSize( filter_t *p_filter, const text_style_t *p_style )
{
filter_sys_t *p_sys = p_filter->p_sys;
int i_font_size = STYLE_DEFAULT_FONT_SIZE;
if( p_style->i_font_size )
{
i_font_size = p_style->i_font_size;
}
else if ( p_style->f_font_relsize )
{
i_font_size = (int) p_filter->fmt_out.video.i_height * p_style->f_font_relsize;
}
if( p_sys->i_scale != 100 )
i_font_size = i_font_size * p_sys->i_scale / 100;
return i_font_size;
}
FT_Face LoadFace( filter_t *p_filter, const char *psz_fontfile, int i_idx,
const text_style_t *p_style )
{
filter_sys_t *p_sys = p_filter->p_sys;
char *psz_key = NULL;
int i_font_size = ConvertToLiveSize( p_filter, p_style );
int i_font_width = p_style->i_style_flags & STYLE_HALFWIDTH ?
i_font_size / 2 : i_font_size;
if( asprintf( &psz_key, "%s - %d - %d - %d",
psz_fontfile, i_idx,
i_font_size, i_font_width ) < 0 )
return NULL;
FT_Face p_face = vlc_dictionary_value_for_key( &p_sys->face_map, psz_key );
if( p_face != kVLCDictionaryNotFound )
goto done;
if( psz_fontfile[0] == ':' && psz_fontfile[1] == '/' )
{
int i_attach = atoi( psz_fontfile + 2 );
if( i_attach < 0 || i_attach >= p_sys->i_font_attachments )
{
msg_Err( p_filter, "LoadFace: Invalid font attachment index" );
p_face = NULL;
}
else
{
input_attachment_t *p_attach = p_sys->pp_font_attachments[ i_attach ];
if( FT_New_Memory_Face( p_sys->p_library, p_attach->p_data,
p_attach->i_data, i_idx, &p_face ) )
p_face = NULL;
}
}
else
if( FT_New_Face( p_sys->p_library, psz_fontfile, i_idx, &p_face ) )
{
msg_Err( p_filter, "LoadFace: Error creating face for %s", psz_key );
p_face = NULL;
}
if( !p_face )
goto done;
if( FT_Select_Charmap( p_face, ft_encoding_unicode ) )
{
/* We've loaded a font face which is unhelpful for actually
* rendering text - fallback to the default one.
*/
msg_Err( p_filter, "LoadFace: Error selecting charmap for %s", psz_key );
FT_Done_Face( p_face );
p_face = NULL;
goto done;
}
if( FT_Set_Pixel_Sizes( p_face, i_font_width, i_font_size ) )
{
msg_Err( p_filter,
"LoadFace: Failed to set font size for %s", psz_key );
FT_Done_Face( p_face );
p_face = NULL;
goto done;
}
vlc_dictionary_insert( &p_sys->face_map, psz_key, p_face );
done:
free( psz_key );
return p_face;
}
FT_Face SelectAndLoadFace( filter_t *p_filter, const text_style_t *p_style,
uni_char_t codepoint )
{
filter_sys_t *p_sys = p_filter->p_sys;
const char *psz_fontname = (p_style->i_style_flags & STYLE_MONOSPACED)
? p_style->psz_monofontname : p_style->psz_fontname;
bool b_bold = p_style->i_style_flags & STYLE_BOLD;
bool b_italic = p_style->i_style_flags & STYLE_ITALIC;
FT_Face p_face = NULL;
int i_idx = 0;
char *psz_fontfile = NULL;
if( p_sys->pf_select )
psz_fontfile = p_sys->pf_select( p_filter, psz_fontname, b_bold, b_italic,
&i_idx, codepoint );
else
psz_fontfile = NULL;
if( !psz_fontfile || *psz_fontfile == '\0' )
{
msg_Warn( p_filter,
"SelectAndLoadFace: no font found for family: %s, codepoint: 0x%x",
psz_fontname, codepoint );
free( psz_fontfile );
return NULL;
}
p_face = LoadFace( p_filter, psz_fontfile, i_idx, p_style );
free( psz_fontfile );
return p_face;
}
......@@ -130,12 +130,14 @@ struct filter_sys_t
uni_char_t codepoint );
};
FT_Face LoadFace( filter_t *p_filter, const char *psz_fontfile, int i_idx,
const text_style_t *p_style );
/**
* Selects and loads the right font
*
* \param p_filter the Freetype module [IN]
* \param p_style the requested style (fonts can be different for italic or bold) [IN]
* \param codepoint the codepoint needed [IN]
*/
FT_Face SelectAndLoadFace( filter_t *p_filter, const text_style_t *p_style,
uni_char_t codepoint );
int ConvertToLiveSize( filter_t *p_filter, const text_style_t *p_style );
#endif
......@@ -37,6 +37,7 @@
#include <vlc_common.h>
#include <vlc_filter.h> /* filter_sys_t */
#include <vlc_text_style.h> /* text_style_t*/
#include <vlc_input.h> /* vlc_input_attachment_* */
#include <ctype.h>
/* apple stuff */
......@@ -70,6 +71,78 @@
#include "platform_fonts.h"
#include "freetype.h"
static FT_Face LoadFace( filter_t *p_filter, const char *psz_fontfile, int i_idx,
const text_style_t *p_style )
{
filter_sys_t *p_sys = p_filter->p_sys;
char *psz_key = NULL;
int i_font_size = ConvertToLiveSize( p_filter, p_style );
int i_font_width = p_style->i_style_flags & STYLE_HALFWIDTH ?
i_font_size / 2 : i_font_size;
if( asprintf( &psz_key, "%s - %d - %d - %d",
psz_fontfile, i_idx,
i_font_size, i_font_width ) < 0 )
return NULL;
FT_Face p_face = vlc_dictionary_value_for_key( &p_sys->face_map, psz_key );
if( p_face != kVLCDictionaryNotFound )
goto done;
if( psz_fontfile[0] == ':' && psz_fontfile[1] == '/' )
{
int i_attach = atoi( psz_fontfile + 2 );
if( i_attach < 0 || i_attach >= p_sys->i_font_attachments )
{
msg_Err( p_filter, "LoadFace: Invalid font attachment index" );
p_face = NULL;
}
else
{
input_attachment_t *p_attach = p_sys->pp_font_attachments[ i_attach ];
if( FT_New_Memory_Face( p_sys->p_library, p_attach->p_data,
p_attach->i_data, i_idx, &p_face ) )
p_face = NULL;
}
}
else
if( FT_New_Face( p_sys->p_library, psz_fontfile, i_idx, &p_face ) )
{
msg_Err( p_filter, "LoadFace: Error creating face for %s", psz_key );
p_face = NULL;
}
if( !p_face )
goto done;
if( FT_Select_Charmap( p_face, ft_encoding_unicode ) )
{
/* We've loaded a font face which is unhelpful for actually
* rendering text - fallback to the default one.
*/
msg_Err( p_filter, "LoadFace: Error selecting charmap for %s", psz_key );
FT_Done_Face( p_face );
p_face = NULL;
goto done;
}
if( FT_Set_Pixel_Sizes( p_face, i_font_width, i_font_size ) )
{
msg_Err( p_filter,
"LoadFace: Failed to set font size for %s", psz_key );
FT_Done_Face( p_face );
p_face = NULL;
goto done;
}
vlc_dictionary_insert( &p_sys->face_map, psz_key, p_face );
done:
free( psz_key );
return p_face;
}
static FT_Face GetFace( filter_t *p_filter, vlc_font_t *p_font )
{
filter_sys_t *p_sys = p_filter->p_sys;
......@@ -371,6 +444,63 @@ char* ToLower( const char *psz_src )
return psz_buffer;
}
/* Face loading */
int ConvertToLiveSize( filter_t *p_filter, const text_style_t *p_style )
{
filter_sys_t *p_sys = p_filter->p_sys;
int i_font_size = STYLE_DEFAULT_FONT_SIZE;
if( p_style->i_font_size )
{
i_font_size = p_style->i_font_size;
}
else if ( p_style->f_font_relsize )
{
i_font_size = (int) p_filter->fmt_out.video.i_height * p_style->f_font_relsize;
}
if( p_sys->i_scale != 100 )
i_font_size = i_font_size * p_sys->i_scale / 100;
return i_font_size;
}
FT_Face SelectAndLoadFace( filter_t *p_filter, const text_style_t *p_style,
uni_char_t codepoint )
{
filter_sys_t *p_sys = p_filter->p_sys;
const char *psz_fontname = (p_style->i_style_flags & STYLE_MONOSPACED)
? p_style->psz_monofontname : p_style->psz_fontname;
bool b_bold = p_style->i_style_flags & STYLE_BOLD;
bool b_italic = p_style->i_style_flags & STYLE_ITALIC;
FT_Face p_face = NULL;
int i_idx = 0;
char *psz_fontfile = NULL;
if( p_sys->pf_select )
psz_fontfile = p_sys->pf_select( p_filter, psz_fontname, b_bold, b_italic,
&i_idx, codepoint );
else
psz_fontfile = NULL;
if( !psz_fontfile || *psz_fontfile == '\0' )
{
msg_Warn( p_filter,
"SelectAndLoadFace: no font found for family: %s, codepoint: 0x%x",
psz_fontname, codepoint );
free( psz_fontfile );
return NULL;
}
p_face = LoadFace( p_filter, psz_fontfile, i_idx, p_style );
free( psz_fontfile );
return p_face;
}
char* Generic_Select( filter_t *p_filter, const char* psz_family,
bool b_bold, bool b_italic,
int *i_idx, uni_char_t codepoint )
......
......@@ -243,4 +243,7 @@ void DumpDictionary( filter_t *p_filter, const vlc_dictionary_t *p_dict,
/* String helpers */
char* ToLower( const char *psz_src );
int ConvertToLiveSize( filter_t *p_filter, const text_style_t *p_style );
#endif //PLATFORM_FONTS_H
......@@ -55,8 +55,9 @@
# include <hb-ft.h>
#endif
#include "text_layout.h"
#include "freetype.h"
#include "text_layout.h"
#include "platform_fonts.h"
/* Win32 */
#ifdef _WIN32
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment