2007-06-20 07:18:47 +02:00
// Copyright (c) 2007, Rodrigo Braz Monteiro
2006-01-16 22:02:54 +01:00
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
// * Neither the name of the Aegisub Group nor the names of its contributors
// may be used to endorse or promote products derived from this software
// without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//
// -----------------------------------------------------------------------------
//
// AEGISUB
//
// Website: http://aegisub.cellosoft.com
// Contact: mailto:zeratul@cellosoft.com
//
////////////
// Includes
2009-01-04 07:31:48 +01:00
# include "config.h"
2006-01-16 22:02:54 +01:00
# include <wx/config.h>
# include <wx/filename.h>
2006-12-18 23:24:25 +01:00
# include <wx/wfstream.h>
# include <wx/zipstrm.h>
2008-01-14 06:54:58 +01:00
# include <wx/fontenum.h>
2006-01-16 22:02:54 +01:00
# include "ass_override.h"
# include "ass_file.h"
# include "ass_dialogue.h"
# include "ass_style.h"
2006-12-24 22:52:54 +01:00
# include "dialog_fonts_collector.h"
2006-01-16 22:02:54 +01:00
# include "utils.h"
# include "options.h"
2006-07-09 01:07:24 +02:00
# include "frame_main.h"
# include "subs_grid.h"
2006-12-30 21:58:24 +01:00
# include "main.h"
2007-06-20 07:18:47 +02:00
# include "font_file_lister.h"
# include "utils.h"
2008-01-13 23:25:26 +01:00
# include "help_button.h"
2008-11-09 02:18:15 +01:00
# include "scintilla_text_ctrl.h"
2007-06-20 07:18:47 +02:00
///////
// IDs
enum IDs {
START_BUTTON = 1150 ,
BROWSE_BUTTON ,
RADIO_BOX
} ;
2006-01-16 22:02:54 +01:00
2008-01-20 07:46:10 +01:00
/////////
// Event
DECLARE_EVENT_TYPE ( EVT_ADD_TEXT , - 1 )
DEFINE_EVENT_TYPE ( EVT_ADD_TEXT )
2006-01-16 22:02:54 +01:00
///////////////
// Constructor
DialogFontsCollector : : DialogFontsCollector ( wxWindow * parent )
: wxDialog ( parent , - 1 , _ ( " Fonts Collector " ) , wxDefaultPosition , wxDefaultSize , wxDEFAULT_DIALOG_STYLE )
{
2007-07-05 01:09:40 +02:00
// Set icon
SetIcon ( BitmapToIcon ( wxBITMAP ( font_collector_button ) ) ) ;
2006-07-09 01:07:24 +02:00
// Parent
main = ( FrameMain * ) parent ;
2006-01-16 22:02:54 +01:00
// Destination box
2006-02-20 23:06:38 +01:00
wxString dest = Options . AsText ( _T ( " Fonts Collector Destination " ) ) ;
if ( dest = = _T ( " ?script " ) ) {
wxFileName filename ( AssFile : : top - > filename ) ;
dest = filename . GetPath ( ) ;
}
2008-01-11 04:38:06 +01:00
while ( dest . Right ( 1 ) = = _T ( " / " ) ) dest = dest . Left ( dest . Length ( ) - 1 ) ;
2006-02-20 23:06:38 +01:00
DestBox = new wxTextCtrl ( this , - 1 , dest , wxDefaultPosition , wxSize ( 250 , 20 ) , 0 ) ;
2006-01-16 22:02:54 +01:00
BrowseButton = new wxButton ( this , BROWSE_BUTTON , _ ( " &Browse... " ) ) ;
wxSizer * DestBottomSizer = new wxBoxSizer ( wxHORIZONTAL ) ;
2006-12-18 23:24:25 +01:00
DestLabel = new wxStaticText ( this , - 1 , _ ( " Choose the folder where the fonts will be collected to. \n It will be created if it doesn't exist. " ) ) ;
2006-01-16 22:02:54 +01:00
DestBottomSizer - > Add ( DestBox , 1 , wxEXPAND | wxRIGHT , 5 ) ;
DestBottomSizer - > Add ( BrowseButton , 0 , 0 , 0 ) ;
wxSizer * DestSizer = new wxStaticBoxSizer ( wxVERTICAL , this , _ ( " Destination " ) ) ;
DestSizer - > Add ( DestLabel , 0 , wxEXPAND | wxBOTTOM , 5 ) ;
DestSizer - > Add ( DestBottomSizer , 0 , wxEXPAND , 0 ) ;
2007-06-20 07:18:47 +02:00
// Action radio box
wxArrayString choices ;
2008-01-14 06:54:58 +01:00
choices . Add ( _ ( " Check fonts for availability " ) ) ;
choices . Add ( _ ( " Copy fonts to folder " ) ) ;
choices . Add ( _ ( " Copy fonts to zipped archive " ) ) ;
choices . Add ( _ ( " Attach fonts to current subtitles " ) ) ;
# ifdef __WXDEBUG__
choices . Add ( _ ( " DEBUG: Verify all fonts in system " ) ) ;
# endif
2007-06-20 07:18:47 +02:00
CollectAction = new wxRadioBox ( this , RADIO_BOX , _T ( " Action " ) , wxDefaultPosition , wxDefaultSize , choices , 1 ) ;
2008-01-14 06:54:58 +01:00
size_t lastAction = Options . AsInt ( _T ( " Fonts Collector Action " ) ) ;
if ( lastAction > = choices . GetCount ( ) ) lastAction = 0 ;
CollectAction - > SetSelection ( lastAction ) ;
2006-01-16 22:02:54 +01:00
// Log box
2008-11-09 02:18:15 +01:00
LogBox = new ScintillaTextCtrl ( this , - 1 , _T ( " " ) , wxDefaultPosition , wxSize ( 300 , 210 ) ) ;
2007-06-20 07:18:47 +02:00
LogBox - > SetWrapMode ( wxSTC_WRAP_WORD ) ;
LogBox - > SetMarginWidth ( 1 , 0 ) ;
LogBox - > SetReadOnly ( true ) ;
LogBox - > StyleSetForeground ( 1 , wxColour ( 0 , 200 , 0 ) ) ;
LogBox - > StyleSetForeground ( 2 , wxColour ( 200 , 0 , 0 ) ) ;
LogBox - > StyleSetForeground ( 3 , wxColour ( 200 , 100 , 0 ) ) ;
2006-01-16 22:02:54 +01:00
wxSizer * LogSizer = new wxStaticBoxSizer ( wxVERTICAL , this , _ ( " Log " ) ) ;
LogSizer - > Add ( LogBox , 1 , wxEXPAND , 0 ) ;
// Buttons sizer
StartButton = new wxButton ( this , START_BUTTON , _ ( " &Start! " ) ) ;
2008-01-13 23:25:26 +01:00
CloseButton = new wxButton ( this , wxID_CANCEL ) ;
2006-01-16 22:02:54 +01:00
StartButton - > SetDefault ( ) ;
2008-01-13 23:25:26 +01:00
wxStdDialogButtonSizer * ButtonSizer = new wxStdDialogButtonSizer ( ) ;
ButtonSizer - > AddButton ( StartButton ) ;
ButtonSizer - > AddButton ( CloseButton ) ;
ButtonSizer - > AddButton ( new HelpButton ( this , _T ( " Fonts Collector " ) ) ) ;
ButtonSizer - > SetAffirmativeButton ( StartButton ) ;
ButtonSizer - > Realize ( ) ;
2006-01-16 22:02:54 +01:00
// Main sizer
wxSizer * MainSizer = new wxBoxSizer ( wxVERTICAL ) ;
2007-06-20 07:18:47 +02:00
MainSizer - > Add ( CollectAction , 0 , wxEXPAND | wxLEFT | wxRIGHT | wxBOTTOM , 5 ) ;
2006-01-16 22:02:54 +01:00
MainSizer - > Add ( DestSizer , 0 , wxEXPAND | wxLEFT | wxRIGHT | wxBOTTOM , 5 ) ;
MainSizer - > Add ( LogSizer , 0 , wxEXPAND | wxLEFT | wxRIGHT | wxBOTTOM , 5 ) ;
MainSizer - > Add ( ButtonSizer , 0 , wxEXPAND | wxLEFT | wxRIGHT | wxBOTTOM , 5 ) ;
// Set sizer
SetSizer ( MainSizer ) ;
MainSizer - > SetSizeHints ( this ) ;
2007-06-20 07:18:47 +02:00
CenterOnParent ( ) ;
2006-12-30 21:58:24 +01:00
// Run dummy event to update label
Update ( ) ;
2006-01-16 22:02:54 +01:00
}
//////////////
// Destructor
DialogFontsCollector : : ~ DialogFontsCollector ( ) {
2007-06-20 07:18:47 +02:00
FontFileLister : : ClearData ( ) ;
2006-01-16 22:02:54 +01:00
}
///////////////
// Event table
BEGIN_EVENT_TABLE ( DialogFontsCollector , wxDialog )
EVT_BUTTON ( START_BUTTON , DialogFontsCollector : : OnStart )
EVT_BUTTON ( BROWSE_BUTTON , DialogFontsCollector : : OnBrowse )
EVT_BUTTON ( wxID_CLOSE , DialogFontsCollector : : OnClose )
2007-06-20 07:18:47 +02:00
EVT_RADIOBOX ( RADIO_BOX , DialogFontsCollector : : OnRadio )
2008-01-20 07:46:10 +01:00
EVT_COMMAND ( 0 , EVT_ADD_TEXT , DialogFontsCollector : : OnAddText )
2006-01-16 22:02:54 +01:00
END_EVENT_TABLE ( )
////////////////////
// Start processing
void DialogFontsCollector : : OnStart ( wxCommandEvent & event ) {
2008-01-20 07:46:10 +01:00
// Clear
LogBox - > SetReadOnly ( false ) ;
LogBox - > ClearAll ( ) ;
LogBox - > SetReadOnly ( true ) ;
2007-06-20 09:10:41 +02:00
// Action being done
int action = CollectAction - > GetSelection ( ) ;
2006-01-16 22:02:54 +01:00
// Check if it's OK to do it
wxString foldername = DestBox - > GetValue ( ) ;
2007-06-20 09:10:41 +02:00
if ( action = = 1 ) foldername + = _T ( " // " ) ;
2006-01-16 22:02:54 +01:00
wxFileName folder ( foldername ) ;
2007-06-20 09:10:41 +02:00
bool isFolder = folder . IsDir ( ) ;
// Check if it's a folder
if ( action = = 1 & & ! isFolder ) {
wxMessageBox ( _ ( " Invalid destination. " ) , _ ( " Error " ) , wxICON_EXCLAMATION | wxOK ) ;
return ;
}
2006-01-16 22:02:54 +01:00
// Make folder if it doesn't exist
2007-06-20 09:10:41 +02:00
if ( action = = 1 | | action = = 2 ) {
2007-06-20 07:18:47 +02:00
if ( ! folder . DirExists ( ) ) {
2007-06-20 09:10:41 +02:00
folder . Mkdir ( 0777 , wxPATH_MKDIR_FULL ) ;
if ( ! folder . DirExists ( ) ) {
wxMessageBox ( _ ( " Could not create destination folder. " ) , _ ( " Error " ) , wxICON_EXCLAMATION | wxOK ) ;
return ;
}
}
}
// Check if we have a valid archive name
if ( action = = 2 ) {
if ( isFolder | | folder . GetName ( ) . IsEmpty ( ) | | folder . GetExt ( ) ! = _T ( " zip " ) ) {
wxMessageBox ( _ ( " Invalid path for .zip file. " ) , _ ( " Error " ) , wxICON_EXCLAMATION | wxOK ) ;
2007-06-20 07:18:47 +02:00
return ;
}
}
2006-01-16 22:02:54 +01:00
2007-06-20 07:18:47 +02:00
// Start thread
wxThread * worker = new FontsCollectorThread ( AssFile : : top , foldername , this ) ;
worker - > Create ( ) ;
worker - > Run ( ) ;
2006-01-16 22:02:54 +01:00
2007-06-20 07:18:47 +02:00
// Set options
if ( action = = 1 | | action = = 2 ) {
2006-02-20 23:06:38 +01:00
wxString dest = foldername ;
wxFileName filename ( AssFile : : top - > filename ) ;
if ( filename . GetPath ( ) = = dest ) {
dest = _T ( " ?script " ) ;
}
Options . SetText ( _T ( " Fonts Collector Destination " ) , dest ) ;
2006-01-16 22:02:54 +01:00
}
2007-06-20 08:29:20 +02:00
Options . SetInt ( _T ( " Fonts Collector Action " ) , action ) ;
Options . Save ( ) ;
2006-01-16 22:02:54 +01:00
2007-06-20 07:18:47 +02:00
// Set buttons
StartButton - > Enable ( false ) ;
BrowseButton - > Enable ( false ) ;
DestBox - > Enable ( false ) ;
CloseButton - > Enable ( false ) ;
CollectAction - > Enable ( false ) ;
DestLabel - > Enable ( false ) ;
if ( ! worker - > IsDetached ( ) ) worker - > Wait ( ) ;
2006-01-16 22:02:54 +01:00
}
////////////////
// Close dialog
void DialogFontsCollector : : OnClose ( wxCommandEvent & event ) {
EndModal ( 0 ) ;
}
///////////////////
// Browse location
void DialogFontsCollector : : OnBrowse ( wxCommandEvent & event ) {
2006-12-18 23:24:25 +01:00
// Chose file name
2007-06-20 07:18:47 +02:00
if ( CollectAction - > GetSelection ( ) = = 2 ) {
2006-12-18 23:24:25 +01:00
wxFileName fname ( DestBox - > GetValue ( ) ) ;
2007-07-05 17:23:31 +02:00
wxString dest = wxFileSelector ( _ ( " Select archive file name " ) , DestBox - > GetValue ( ) , fname . GetFullName ( ) , _T ( " .zip " ) , _ ( " Zip Archives (*.zip)|*.zip " ) , wxFD_SAVE | wxFD_OVERWRITE_PROMPT ) ;
2006-12-18 23:24:25 +01:00
if ( ! dest . empty ( ) ) {
DestBox - > SetValue ( dest ) ;
}
}
// Choose folder
else {
wxString dest = wxDirSelector ( _ ( " Select folder to save fonts on " ) , DestBox - > GetValue ( ) , 0 ) ;
if ( ! dest . empty ( ) ) {
DestBox - > SetValue ( dest ) ;
}
2006-01-16 22:02:54 +01:00
}
}
2007-06-20 07:18:47 +02:00
/////////////////////
// Radio box changed
void DialogFontsCollector : : OnRadio ( wxCommandEvent & event ) {
Update ( event . GetInt ( ) ) ;
2006-12-30 21:58:24 +01:00
}
///////////////////
// Update controls
2007-06-20 07:18:47 +02:00
void DialogFontsCollector : : Update ( int value ) {
2007-06-20 08:29:20 +02:00
// Enable buttons
CloseButton - > Enable ( true ) ;
StartButton - > Enable ( true ) ;
CollectAction - > Enable ( true ) ;
2007-06-20 09:10:41 +02:00
wxString dst = DestBox - > GetValue ( ) ;
2007-06-20 08:29:20 +02:00
2007-06-20 07:18:47 +02:00
// Get value if -1
if ( value = = - 1 ) {
value = CollectAction - > GetSelection ( ) ;
2006-12-18 23:24:25 +01:00
}
2007-06-20 07:18:47 +02:00
2007-06-20 08:29:20 +02:00
// Check or attach
if ( value = = 0 | | value = = 3 ) {
2007-06-20 07:18:47 +02:00
DestBox - > Enable ( false ) ;
BrowseButton - > Enable ( false ) ;
DestLabel - > Enable ( false ) ;
2007-06-20 08:29:20 +02:00
DestLabel - > SetLabel ( _T ( " N/A \n " ) ) ;
2007-06-20 07:18:47 +02:00
}
// Collect to folder
else if ( value = = 1 ) {
DestBox - > Enable ( true ) ;
BrowseButton - > Enable ( true ) ;
DestLabel - > Enable ( true ) ;
2006-12-30 21:58:24 +01:00
DestLabel - > SetLabel ( _ ( " Choose the folder where the fonts will be collected to. \n It will be created if it doesn't exist. " ) ) ;
2007-06-20 09:10:41 +02:00
// Remove filename from browse box
if ( dst . Right ( 4 ) = = _T ( " .zip " ) ) {
wxFileName fn ( dst ) ;
DestBox - > SetValue ( fn . GetPath ( ) ) ;
2006-12-30 21:58:24 +01:00
}
}
2006-01-16 22:02:54 +01:00
2007-06-20 07:18:47 +02:00
// Collect to zip
else if ( value = = 2 ) {
DestBox - > Enable ( true ) ;
BrowseButton - > Enable ( true ) ;
DestLabel - > Enable ( true ) ;
DestLabel - > SetLabel ( _ ( " Enter the name of the destination zip file to collect the fonts to. \n If a folder is entered, a default name will be used. " ) ) ;
2007-06-20 09:10:41 +02:00
// Add filename to browse box
if ( dst . Right ( 4 ) ! = _T ( " .zip " ) ) {
wxFileName fn ( dst + _T ( " // " ) ) ;
fn . SetFullName ( _T ( " fonts.zip " ) ) ;
DestBox - > SetValue ( fn . GetFullPath ( ) ) ;
}
2006-01-16 22:02:54 +01:00
}
}
2008-01-20 07:46:10 +01:00
////////////
// Add text
void DialogFontsCollector : : OnAddText ( wxCommandEvent & event ) {
ColourString * str = ( ColourString * ) event . GetClientData ( ) ;
LogBox - > SetReadOnly ( false ) ;
2008-11-09 02:18:15 +01:00
int pos = LogBox - > GetReverseUnicodePosition ( LogBox - > GetLength ( ) ) ;
2008-01-20 07:46:10 +01:00
LogBox - > AppendText ( str - > text ) ;
if ( str - > colour ) {
2008-11-09 02:18:15 +01:00
LogBox - > StartUnicodeStyling ( pos , 31 ) ;
LogBox - > SetUnicodeStyling ( pos , str - > text . Length ( ) , str - > colour ) ;
2008-01-20 07:46:10 +01:00
}
delete str ;
LogBox - > GotoPos ( pos ) ;
LogBox - > SetReadOnly ( true ) ;
}
2006-01-16 22:02:54 +01:00
///////////////////////
// Collect font files
void FontsCollectorThread : : CollectFontData ( ) {
2007-07-29 03:21:28 +02:00
FontFileLister : : Initialize ( ) ;
2006-01-16 22:02:54 +01:00
}
////////////////////
// Collector thread
FontsCollectorThread : : FontsCollectorThread ( AssFile * _subs , wxString _destination , DialogFontsCollector * _collector )
: wxThread ( wxTHREAD_DETACHED )
{
subs = _subs ;
destination = _destination ;
collector = _collector ;
instance = this ;
}
////////////////
// Thread entry
wxThread : : ExitCode FontsCollectorThread : : Entry ( ) {
// Collect
Collect ( ) ;
2007-06-20 08:29:20 +02:00
// After done, restore status
collector - > Update ( ) ;
2006-01-16 22:02:54 +01:00
// Return
2009-05-18 07:24:07 +02:00
if ( IsDetached ( ) & & TestDestroy ( ) ) Delete ( ) ;
2006-01-16 22:02:54 +01:00
return 0 ;
}
///////////
// Collect
void FontsCollectorThread : : Collect ( ) {
2007-06-20 07:18:47 +02:00
// Set destination folder
int oper = collector - > CollectAction - > GetSelection ( ) ;
destFolder = collector - > DestBox - > GetValue ( ) ;
if ( oper = = 1 & & ! wxFileName : : DirExists ( destFolder ) ) {
AppendText ( _ ( " Invalid destination directory. " ) , 1 ) ;
return ;
2006-12-18 23:24:25 +01:00
}
2006-01-16 22:02:54 +01:00
2007-06-20 09:10:41 +02:00
// Open zip stream if saving to compressed archive
wxFFileOutputStream * out = NULL ;
zip = NULL ;
if ( oper = = 2 ) {
out = new wxFFileOutputStream ( destFolder ) ;
zip = new wxZipOutputStream ( * out ) ;
}
2007-06-20 07:18:47 +02:00
// Collect font data
2007-07-05 17:23:31 +02:00
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 " ) ) ;
2007-06-20 07:18:47 +02:00
CollectFontData ( ) ;
2007-07-05 17:23:31 +02:00
AppendText ( _ ( " Done collecting font data. " ) ) ;
AppendText ( _ ( " Scanning file for fonts... " ) ) ;
2006-01-16 22:02:54 +01:00
2007-06-20 07:18:47 +02:00
// Scan file
2008-01-14 06:54:58 +01:00
if ( collector - > CollectAction - > GetSelection ( ) ! = 4 ) {
AssDialogue * curDiag ;
curLine = 0 ;
for ( std : : list < AssEntry * > : : iterator cur = subs - > Line . begin ( ) ; cur ! = subs - > Line . end ( ) ; cur + + ) {
// Collect from style
curStyle = AssEntry : : GetAsStyle ( * cur ) ;
if ( curStyle ) {
AddFont ( curStyle - > font , 0 ) ;
}
2006-01-16 22:02:54 +01:00
2008-01-14 06:54:58 +01:00
// Collect from dialogue
else {
curDiag = AssEntry : : GetAsDialogue ( * cur ) ;
if ( curDiag ) {
curLine + + ;
curDiag - > ParseASSTags ( ) ;
curDiag - > ProcessParameters ( GetFonts ) ;
curDiag - > ClearBlocks ( ) ;
}
2006-01-16 22:02:54 +01:00
}
}
}
2008-01-14 06:54:58 +01:00
// For maitenance, gather all on system
else {
wxArrayString fonts = wxFontEnumerator : : GetFacenames ( ) ;
for ( size_t i = 0 ; i < fonts . Count ( ) ; i + + ) AddFont ( fonts [ i ] , 2 ) ;
}
2007-06-20 07:18:47 +02:00
// Copy fonts
2007-07-05 17:23:31 +02:00
AppendText ( wxString ( _ ( " Done. " ) ) + _T ( " \n \n " ) ) ;
2007-06-20 07:18:47 +02:00
switch ( oper ) {
case 0 : AppendText ( _ ( " Checking fonts... \n " ) ) ; break ;
case 1 : AppendText ( _ ( " Copying fonts to folder... \n " ) ) ; break ;
case 2 : AppendText ( _ ( " Copying fonts to archive... \n " ) ) ; break ;
case 3 : AppendText ( _ ( " Attaching fonts to file... \n " ) ) ; break ;
}
bool ok = true ;
2007-06-20 08:29:20 +02:00
bool someOk = false ;
2007-06-20 07:18:47 +02:00
for ( size_t i = 0 ; i < fonts . Count ( ) ; i + + ) {
2007-06-20 08:29:20 +02:00
bool result = ProcessFont ( fonts [ i ] ) ;
if ( result ) someOk = true ;
if ( ! result ) ok = false ;
2007-06-20 07:18:47 +02:00
}
2007-06-20 09:10:41 +02:00
// Close ZIP archive
if ( oper = = 2 ) {
zip - > Close ( ) ;
delete zip ;
delete out ;
AppendText ( wxString : : Format ( _ ( " \n Finished writing to %s. \n " ) , destination . c_str ( ) ) , 1 ) ;
}
2007-06-20 07:18:47 +02:00
// Final result
if ( ok ) {
2008-01-11 05:36:29 +01:00
if ( oper = = 0 ) {
AppendText ( _ ( " Done. All fonts found. " ) , 1 ) ;
}
2007-06-20 08:29:20 +02:00
else {
AppendText ( _ ( " Done. All fonts copied. " ) , 1 ) ;
// Modify file if it was attaching
if ( oper = = 3 & & someOk ) {
wxMutexGuiEnter ( ) ;
subs - > FlagAsModified ( _ ( " font attachment " ) ) ;
collector - > main - > SubsBox - > CommitChanges ( ) ;
wxMutexGuiLeave ( ) ;
}
}
2007-06-20 07:18:47 +02:00
}
else {
if ( oper = = 0 ) AppendText ( _ ( " Done. Some fonts could not be found. " ) , 2 ) ;
else AppendText ( _ ( " Done. Some fonts could not be copied. " ) , 2 ) ;
}
}
2006-01-16 22:02:54 +01:00
2007-06-20 07:18:47 +02:00
////////////////
// Process font
bool FontsCollectorThread : : ProcessFont ( wxString name ) {
// Action
int action = collector - > CollectAction - > GetSelection ( ) ;
// Font name
AppendText ( wxString : : Format ( _T ( " \" %s \" ... " ) , name . c_str ( ) ) ) ;
// Get font list
wxArrayString files = FontFileLister : : GetFilesWithFace ( name ) ;
bool result = files . Count ( ) ! = 0 ;
// No files found
if ( ! result ) {
AppendText ( _ ( " Not found. \n " ) , 2 ) ;
return false ;
2006-01-16 22:02:54 +01:00
}
2007-06-20 07:18:47 +02:00
// Just checking, found
2008-01-14 06:54:58 +01:00
else if ( action = = 0 | | action = = 4 ) {
2007-06-20 07:18:47 +02:00
AppendText ( _ ( " Found. \n " ) , 1 ) ;
return true ;
2006-12-18 23:24:25 +01:00
}
2007-06-20 07:18:47 +02:00
// Copy font
AppendText ( _T ( " \n " ) ) ;
for ( size_t i = 0 ; i < files . Count ( ) ; i + + ) {
int tempResult = 0 ;
switch ( action ) {
case 1 : tempResult = CopyFont ( files [ i ] ) ; break ;
case 2 : tempResult = ArchiveFont ( files [ i ] ) ? 1 : 0 ; break ;
case 3 : tempResult = AttachFont ( files [ i ] ) ? 1 : 0 ; break ;
2006-01-16 22:02:54 +01:00
}
2007-06-20 07:18:47 +02:00
if ( tempResult = = 1 ) {
AppendText ( wxString : : Format ( _ ( " * Copied %s. \n " ) , files [ i ] . c_str ( ) ) , 1 ) ;
}
else if ( tempResult = = 2 ) {
wxFileName fn ( files [ i ] ) ;
AppendText ( wxString : : Format ( _ ( " * %s already exists on destination. \n " ) , fn . GetFullName ( ) . c_str ( ) ) , 3 ) ;
2006-01-16 22:02:54 +01:00
}
2007-06-20 07:18:47 +02:00
else {
AppendText ( wxString : : Format ( _ ( " * Failed to copy %s. \n " ) , files [ i ] . c_str ( ) ) , 2 ) ;
result = false ;
}
}
// Done
return result ;
}
/////////////
// Copy font
int FontsCollectorThread : : CopyFont ( wxString filename ) {
wxFileName fn ( filename ) ;
wxString dstName = destFolder + _T ( " // " ) + fn . GetFullName ( ) ;
if ( wxFileName : : FileExists ( dstName ) ) return 2 ;
2009-07-14 04:20:40 +02:00
return wxCopyFile ( filename , dstName , true ) ? 1 : 0 ;
2007-06-20 07:18:47 +02:00
}
////////////////
// Archive font
bool FontsCollectorThread : : ArchiveFont ( wxString filename ) {
2007-06-20 09:10:41 +02:00
// Open file
wxFFileInputStream in ( filename ) ;
if ( ! in . IsOk ( ) ) return false ;
// Write to archive
try {
wxFileName fn ( filename ) ;
zip - > PutNextEntry ( fn . GetFullName ( ) ) ;
zip - > Write ( in ) ;
}
catch ( . . . ) {
return false ;
}
return true ;
2007-06-20 07:18:47 +02:00
}
///////////////
// Attach font
bool FontsCollectorThread : : AttachFont ( wxString filename ) {
2007-06-20 08:29:20 +02:00
try {
subs - > InsertAttachment ( filename ) ;
}
catch ( . . . ) {
return false ;
}
return true ;
2007-06-20 07:18:47 +02:00
}
////////////////////////////////
// Get fonts from ass overrides
void FontsCollectorThread : : GetFonts ( wxString tagName , int par_n , AssOverrideParameter * param , void * usr ) {
if ( tagName = = _T ( " \\ fn " ) ) {
2008-01-14 06:54:58 +01:00
instance - > AddFont ( param - > AsText ( ) , 1 ) ;
2006-01-16 22:02:54 +01:00
}
2007-06-20 07:18:47 +02:00
}
2006-12-18 23:24:25 +01:00
2007-06-20 07:18:47 +02:00
///////////////
// Adds a font
2008-01-14 06:54:58 +01:00
void FontsCollectorThread : : AddFont ( wxString fontname , int mode ) {
2007-08-20 20:10:27 +02:00
// @-fonts (CJK vertical layout variations) should be listed as the non-@ name
if ( fontname . StartsWith ( _T ( " @ " ) , 0 ) )
fontname . Remove ( 0 , 1 ) ;
2007-06-20 07:18:47 +02:00
if ( fonts . Index ( fontname ) = = wxNOT_FOUND ) {
fonts . Add ( fontname ) ;
2007-01-04 04:53:56 +01:00
2008-01-14 06:54:58 +01:00
if ( mode = = 0 ) AppendText ( wxString : : Format ( _ ( " \" %s \" found on style \" %s \" . \n " ) , fontname . c_str ( ) , curStyle - > name . c_str ( ) ) ) ;
else if ( mode = = 1 ) AppendText ( wxString : : Format ( _ ( " \" %s \" found on dialogue line \" %d \" . \n " ) , fontname . c_str ( ) , curLine ) ) ;
else AppendText ( wxString : : Format ( _ ( " \" %s \" found. \n " ) , fontname . c_str ( ) ) ) ;
2006-12-18 23:24:25 +01:00
}
2007-06-20 07:18:47 +02:00
}
2006-07-09 01:07:24 +02:00
2007-06-20 07:18:47 +02:00
///////////////
// Append text
void FontsCollectorThread : : AppendText ( wxString text , int colour ) {
2008-01-20 07:46:10 +01:00
ColourString * str = new ColourString ;
str - > text = text ;
str - > colour = colour ;
wxCommandEvent event ( EVT_ADD_TEXT , 0 ) ;
event . SetClientData ( str ) ;
collector - > AddPendingEvent ( event ) ;
2006-01-16 22:02:54 +01:00
}
2007-06-20 07:18:47 +02:00
///////////////////
// Static instance
FontsCollectorThread * FontsCollectorThread : : instance ;