2012-01-12 23:32:09 +01:00
// Copyright (c) 2012, Thomas Goyne <plorkyeran@aegisub.org>
2007-07-29 03:21:28 +02:00
//
2012-01-12 23:32:09 +01:00
// Permission to use, copy, modify, and distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
2007-07-29 03:21:28 +02:00
//
2012-01-12 23:32:09 +01:00
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
2007-07-29 03:21:28 +02:00
//
2009-07-29 07:43:02 +02:00
// Aegisub Project http://www.aegisub.org/
2007-07-29 03:21:28 +02:00
//
2009-07-29 07:43:02 +02:00
// $Id$
/// @file font_file_lister_freetype.cpp
/// @brief FreeType based font collector
/// @ingroup font_collector
///
2007-07-29 03:21:28 +02:00
2009-01-04 07:31:48 +01:00
# include "config.h"
2007-12-31 07:46:22 +01:00
2009-01-04 07:31:48 +01:00
# ifdef WITH_FREETYPE2
2012-01-12 23:32:09 +01:00
# include "font_file_lister_freetype.h"
2009-09-10 15:06:40 +02:00
2007-07-29 03:21:28 +02:00
# include <ft2build.h>
# include FT_FREETYPE_H
# include FT_GLYPH_H
# include FT_SFNT_NAMES_H
2012-01-12 23:32:09 +01:00
# include FT_TRUETYPE_IDS_H
2007-07-29 03:21:28 +02:00
2009-09-11 20:55:33 +02:00
# include <shlobj.h>
2009-09-10 15:06:40 +02:00
# include "charset_conv.h"
2012-01-12 23:32:09 +01:00
# include "standard_paths.h"
# include "text_file_reader.h"
# include "text_file_writer.h"
namespace {
typedef std : : map < wxString , std : : set < wxString > > FontMap ;
wxString get_font_folder ( ) {
wchar_t szPath [ MAX_PATH ] ;
if ( SUCCEEDED ( SHGetFolderPath ( NULL , CSIDL_FONTS , NULL , 0 , szPath ) ) )
return wxString ( szPath ) + " \\ " ;
else
return wxGetOSDirectory ( ) + " \\ fonts \\ " ;
}
Note: This was done using a script! it's far from perfect but 95% of the work has been done already formatting-wise.
Document all functions, class, struct, union, enum, macro, variable, typedefs. This isn't the actual document in itself but empty documentation using any old documentation if it was there.
This was done using exuberant ctags to get tag info, then a TCL script to parse/remove old comments and convert them into Doxygen-style.
Some notes:
* Anything labeled 'DOCME' needs to be documented, @param and @return have been left blank as it would be annoying to delete the 'DOCME' from every one of those.
* Some multiline comments may have been munged into single line comments
* Leave the /// comments above global variables with a space, if they're harder to read then we'll be less likey to use them.
* Enum comments can go after the enumeration itself '[value] /// comment'
* include/aegisub/*.h haven't been converted yet, this will be done in a later commit
* Some documentation blocks are in the wrong place, in the .h when it should be in the .cpp, or vice versa.
See http://devel.aegisub.org/wiki/Doxygen for some details on Doxygen and a 'style guide'.
Originally committed to SVN as r3312.
2009-07-30 00:59:22 +02:00
2012-01-12 23:32:09 +01:00
FT_UInt get_name_count ( wxString const & filename , FT_Face face ) {
wxString ext = filename . Right ( 4 ) . Lower ( ) ;
if ( ext = = " .otf " | | ext = = " .ttf " | | ext = = " .ttc_ " )
return FT_Get_Sfnt_Name_Count ( face ) ;
return 0 ;
}
2007-07-29 03:21:28 +02:00
2012-01-12 23:32:09 +01:00
std : : map < FT_UShort , std : : vector < wxString > > get_names ( FT_Face face ) {
std : : map < FT_UShort , std : : vector < wxString > > final ;
FT_UInt count = FT_Get_Sfnt_Name_Count ( face ) ;
for ( FT_UInt i = 0 ; i < count ; + + i ) {
FT_SfntName name ;
FT_Get_Sfnt_Name ( face , i , & name ) ;
// truetype string encoding is an absolute nightmare, so just try
// UTF-16BE and local charsets
if ( name . string_len ) {
if ( ( name . platform_id = = TT_PLATFORM_MICROSOFT & & name . encoding_id = = TT_MS_ID_UNICODE_CS ) | |
name . platform_id = = TT_PLATFORM_APPLE_UNICODE | |
name . string [ 0 ] )
{
final [ name . name_id ] . push_back ( wxString ( name . string , wxMBConvUTF16BE ( ) , name . string_len ) ) ;
}
else
final [ name . name_id ] . push_back ( wxString ( name . string , name . string_len ) ) ;
}
}
2007-07-29 03:21:28 +02:00
2012-01-12 23:32:09 +01:00
return final ;
}
Note: This was done using a script! it's far from perfect but 95% of the work has been done already formatting-wise.
Document all functions, class, struct, union, enum, macro, variable, typedefs. This isn't the actual document in itself but empty documentation using any old documentation if it was there.
This was done using exuberant ctags to get tag info, then a TCL script to parse/remove old comments and convert them into Doxygen-style.
Some notes:
* Anything labeled 'DOCME' needs to be documented, @param and @return have been left blank as it would be annoying to delete the 'DOCME' from every one of those.
* Some multiline comments may have been munged into single line comments
* Leave the /// comments above global variables with a space, if they're harder to read then we'll be less likey to use them.
* Enum comments can go after the enumeration itself '[value] /// comment'
* include/aegisub/*.h haven't been converted yet, this will be done in a later commit
* Some documentation blocks are in the wrong place, in the .h when it should be in the .cpp, or vice versa.
See http://devel.aegisub.org/wiki/Doxygen for some details on Doxygen and a 'style guide'.
Originally committed to SVN as r3312.
2009-07-30 00:59:22 +02:00
2012-01-12 23:32:09 +01:00
void load_cache ( FontMap & font_files , std : : set < wxString > & indexed_files ) {
try {
TextFileReader file ( StandardPaths : : DecodePath ( " ?local/freetype_collector_index.dat " ) , " utf-16le " ) ;
while ( file . HasMoreLines ( ) ) {
wxString face = file . ReadLineFromFile ( ) ;
std : : set < wxString > & files = font_files [ face ] ;
while ( file . HasMoreLines ( ) ) {
wxString filename = file . ReadLineFromFile ( ) ;
if ( filename . empty ( ) ) break ;
if ( wxFileExists ( filename ) ) {
files . insert ( filename ) ;
indexed_files . insert ( filename ) ;
}
}
2008-01-14 08:06:43 +01:00
}
2008-01-14 07:38:33 +01:00
}
2012-01-12 23:32:09 +01:00
catch ( agi : : FileNotAccessibleError const & ) { }
2008-01-14 07:38:33 +01:00
}
2012-01-12 23:32:09 +01:00
void save_cache ( FontMap & font_files ) {
TextFileWriter file ( StandardPaths : : DecodePath ( " ?local/freetype_collector_index.dat " ) , " utf-16le " ) ;
for ( FontMap : : iterator face_it = font_files . begin ( ) ; face_it ! = font_files . end ( ) ; + + face_it ) {
file . WriteLineToFile ( face_it - > first ) ;
for_each ( face_it - > second . begin ( ) , face_it - > second . end ( ) ,
bind ( & TextFileWriter : : WriteLineToFile , & file , std : : tr1 : : placeholders : : _1 , true ) ) ;
file . WriteLineToFile ( " " ) ;
}
}
2008-01-14 06:54:58 +01:00
}
2012-01-12 23:32:09 +01:00
FreetypeFontFileLister : : FreetypeFontFileLister ( FontCollectorStatusCallback AppendText ) {
AppendText ( _ ( " Collecting font data from system. This might take a while, depending on the number of fonts installed. Results are cached and subsequent executions will be faster... \n " ) , 0 ) ;
load_cache ( font_files , indexed_files ) ;
2008-01-14 06:54:58 +01:00
2012-01-12 23:32:09 +01:00
FT_Library ft2lib ;
FT_Init_FreeType ( & ft2lib ) ;
Note: This was done using a script! it's far from perfect but 95% of the work has been done already formatting-wise.
Document all functions, class, struct, union, enum, macro, variable, typedefs. This isn't the actual document in itself but empty documentation using any old documentation if it was there.
This was done using exuberant ctags to get tag info, then a TCL script to parse/remove old comments and convert them into Doxygen-style.
Some notes:
* Anything labeled 'DOCME' needs to be documented, @param and @return have been left blank as it would be annoying to delete the 'DOCME' from every one of those.
* Some multiline comments may have been munged into single line comments
* Leave the /// comments above global variables with a space, if they're harder to read then we'll be less likey to use them.
* Enum comments can go after the enumeration itself '[value] /// comment'
* include/aegisub/*.h haven't been converted yet, this will be done in a later commit
* Some documentation blocks are in the wrong place, in the .h when it should be in the .cpp, or vice versa.
See http://devel.aegisub.org/wiki/Doxygen for some details on Doxygen and a 'style guide'.
Originally committed to SVN as r3312.
2009-07-30 00:59:22 +02:00
2007-07-29 03:21:28 +02:00
wxArrayString fontfiles ;
2012-01-12 23:32:09 +01:00
wxDir : : GetAllFiles ( get_font_folder ( ) , & fontfiles , " " , wxDIR_FILES ) ;
2012-03-13 00:34:16 +01:00
for ( size_t ff = 0 ; ff < fontfiles . size ( ) ; + + ff ) {
wxString const & fontfile = fontfiles [ ff ] ;
if ( indexed_files . count ( fontfile ) ) continue ;
2012-01-12 23:32:09 +01:00
FT_Face face ;
2012-03-13 00:34:16 +01:00
for ( FT_Long i = 0 ; FT_New_Face ( ft2lib , fontfile . mb_str ( * wxConvFileName ) , i , & face ) = = 0 ; + + i ) {
if ( get_name_count ( fontfile , face ) > 0 ) {
2012-01-12 23:32:09 +01:00
std : : map < FT_UShort , std : : vector < wxString > > names = get_names ( face ) ;
std : : vector < wxString > & family = names [ 1 ] ;
std : : vector < wxString > & style = names [ 2 ] ;
std : : vector < wxString > & full_name = names [ 4 ] ;
for ( size_t j = 0 ; j < family . size ( ) & & j < style . size ( ) ; + + j ) {
if ( style [ j ] ! = " Regular " )
2012-03-13 00:34:16 +01:00
AddFont ( fontfile , family [ j ] , style [ j ] ) ;
2012-01-12 23:32:09 +01:00
else
2012-03-13 00:34:16 +01:00
AddFont ( fontfile , family [ j ] ) ;
2008-01-14 19:36:10 +01:00
}
2012-01-12 23:32:09 +01:00
for ( size_t j = 0 ; j < full_name . size ( ) ; + + j )
2012-03-13 00:34:16 +01:00
AddFont ( fontfile , full_name [ j ] ) ;
2008-01-14 06:54:58 +01:00
}
2008-01-14 19:36:10 +01:00
else {
2012-01-12 23:32:09 +01:00
if ( face - > style_name )
2012-03-13 00:34:16 +01:00
AddFont ( fontfile , face - > family_name , face - > style_name ) ;
2012-01-12 23:32:09 +01:00
else
2012-03-13 00:34:16 +01:00
AddFont ( fontfile , wxString ( face - > family_name ) ) ;
2008-01-14 19:36:10 +01:00
}
2007-07-29 03:21:28 +02:00
FT_Done_Face ( face ) ;
}
}
2012-01-12 23:32:09 +01:00
FT_Done_FreeType ( ft2lib ) ;
AppendText ( _ ( " Done collecting font data. \n " ) , 0 ) ;
save_cache ( font_files ) ;
2007-07-29 03:21:28 +02:00
}
2007-12-31 07:46:22 +01:00
2012-01-12 23:32:09 +01:00
void FreetypeFontFileLister : : AddFont ( wxString const & filename , wxString facename ) {
facename . Trim ( true ) . Trim ( false ) ;
if ( facename . size ( ) & & ! facename . Lower ( ) . StartsWith ( " copyright " ) )
font_files [ facename ] . insert ( filename ) ;
}
2009-07-29 07:43:02 +02:00
2012-01-12 23:32:09 +01:00
void FreetypeFontFileLister : : AddFont ( wxString const & filename , wxString const & family , wxString const & style ) {
AddFont ( filename , family + " " + style ) ;
AddFont ( filename , " * " + family ) ;
}
Note: This was done using a script! it's far from perfect but 95% of the work has been done already formatting-wise.
Document all functions, class, struct, union, enum, macro, variable, typedefs. This isn't the actual document in itself but empty documentation using any old documentation if it was there.
This was done using exuberant ctags to get tag info, then a TCL script to parse/remove old comments and convert them into Doxygen-style.
Some notes:
* Anything labeled 'DOCME' needs to be documented, @param and @return have been left blank as it would be annoying to delete the 'DOCME' from every one of those.
* Some multiline comments may have been munged into single line comments
* Leave the /// comments above global variables with a space, if they're harder to read then we'll be less likey to use them.
* Enum comments can go after the enumeration itself '[value] /// comment'
* include/aegisub/*.h haven't been converted yet, this will be done in a later commit
* Some documentation blocks are in the wrong place, in the .h when it should be in the .cpp, or vice versa.
See http://devel.aegisub.org/wiki/Doxygen for some details on Doxygen and a 'style guide'.
Originally committed to SVN as r3312.
2009-07-30 00:59:22 +02:00
2012-02-02 20:18:40 +01:00
FontFileLister : : CollectionResult FreetypeFontFileLister : : GetFontPaths ( wxString const & facename , int , bool , std : : set < wxUniChar > const & ) {
CollectionResult ret ;
ret . paths . insert ( ret . paths . end ( ) , font_files [ facename ] . begin ( ) , font_files [ facename ] . end ( ) ) ;
if ( ret . paths . empty ( ) )
ret . paths . insert ( ret . paths . end ( ) , font_files [ " * " + facename ] . begin ( ) , font_files [ " * " + facename ] . end ( ) ) ;
2012-01-12 23:32:09 +01:00
return ret ;
}
# endif WITH_FREETYPE2