Improved (but still far from perfect) Fonts Collector performance

Originally committed to SVN as r1714.
This commit is contained in:
Rodrigo Braz Monteiro 2008-01-14 05:54:58 +00:00
parent a69fe9e71e
commit 3acc7b35a8
4 changed files with 77 additions and 27 deletions

View file

@ -40,6 +40,7 @@
#include <wx/filename.h>
#include <wx/wfstream.h>
#include <wx/zipstrm.h>
#include <wx/fontenum.h>
#include "ass_override.h"
#include "ass_file.h"
#include "ass_dialogue.h"
@ -94,12 +95,17 @@ DialogFontsCollector::DialogFontsCollector(wxWindow *parent)
// Action radio box
wxArrayString choices;
choices.Add(_T("Check fonts for availability"));
choices.Add(_T("Copy fonts to folder"));
choices.Add(_T("Copy fonts to zipped archive"));
choices.Add(_T("Attach fonts to current subtitles"));
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
CollectAction = new wxRadioBox(this,RADIO_BOX,_T("Action"),wxDefaultPosition,wxDefaultSize,choices,1);
CollectAction->SetSelection(Options.AsInt(_T("Fonts Collector Action")));
size_t lastAction = Options.AsInt(_T("Fonts Collector Action"));
if (lastAction >= choices.GetCount()) lastAction = 0;
CollectAction->SetSelection(lastAction);
// Log box
LogBox = new wxStyledTextCtrl(this,-1,wxDefaultPosition,wxSize(300,210),0,_T(""));
@ -384,27 +390,35 @@ void FontsCollectorThread::Collect() {
wxMutexGuiLeave();
// Scan file
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,true);
}
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);
}
// Collect from dialogue
else {
curDiag = AssEntry::GetAsDialogue(*cur);
if (curDiag) {
curLine++;
curDiag->ParseASSTags();
curDiag->ProcessParameters(GetFonts);
curDiag->ClearBlocks();
// Collect from dialogue
else {
curDiag = AssEntry::GetAsDialogue(*cur);
if (curDiag) {
curLine++;
curDiag->ParseASSTags();
curDiag->ProcessParameters(GetFonts);
curDiag->ClearBlocks();
}
}
}
}
// For maitenance, gather all on system
else {
wxArrayString fonts = wxFontEnumerator::GetFacenames();
for (size_t i=0;i<fonts.Count();i++) AddFont(fonts[i],2);
}
// Copy fonts
wxMutexGuiEnter();
AppendText(wxString(_("Done.")) + _T("\n\n"));
@ -484,7 +498,7 @@ bool FontsCollectorThread::ProcessFont(wxString name) {
}
// Just checking, found
else if (action == 0) {
else if (action == 0 || action == 4) {
AppendText(_("Found.\n"),1);
return true;
}
@ -565,14 +579,14 @@ bool FontsCollectorThread::AttachFont(wxString filename) {
// Get fonts from ass overrides
void FontsCollectorThread::GetFonts (wxString tagName,int par_n,AssOverrideParameter *param,void *usr) {
if (tagName == _T("\\fn")) {
instance->AddFont(param->AsText(),false);
instance->AddFont(param->AsText(),1);
}
}
///////////////
// Adds a font
void FontsCollectorThread::AddFont(wxString fontname,bool isStyle) {
void FontsCollectorThread::AddFont(wxString fontname,int mode) {
// @-fonts (CJK vertical layout variations) should be listed as the non-@ name
if (fontname.StartsWith(_T("@"), 0))
fontname.Remove(0, 1);
@ -580,8 +594,9 @@ void FontsCollectorThread::AddFont(wxString fontname,bool isStyle) {
if (fonts.Index(fontname) == wxNOT_FOUND) {
fonts.Add(fontname);
if (isStyle) AppendText(wxString::Format(_("\"%s\" found on style \"%s\".\n"), fontname.c_str(), curStyle->name.c_str()));
if (!isStyle) AppendText(wxString::Format(_("\"%s\" found on dialogue line \"%d\".\n"), fontname.c_str(), curLine));
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()));
}
}

View file

@ -74,7 +74,7 @@ private:
bool AttachFont(wxString filename);
void Collect();
void AddFont(wxString fontname,bool isStyle);
void AddFont(wxString fontname,int mode);
void CollectFontData();
void AppendText(wxString text,int colour=0);

View file

@ -115,6 +115,11 @@ void FontFileLister::ClearCache() {
////////////
// Add font
void FontFileLister::AddFont(wxString filename,wxString facename) {
// See if it's a valid facename
facename.Trim(true).Trim(false);
if (facename.IsEmpty()) return;
if (facename.Lower().StartsWith(_T("copyright "))) return;
// Add filename to general list
if (fontFiles.Index(filename) == wxNOT_FOUND) {
fontFiles.Add(filename);

View file

@ -64,6 +64,21 @@ FreetypeFontFileLister::~FreetypeFontFileLister() {
}
//////////////////////
// Get name from face
wxString GetName(FT_Face &face,int i) {
// Get name
FT_SfntName name;
FT_Get_Sfnt_Name(face,i,&name);
char *str = new char[name.string_len+1];
memcpy(str,name.string,name.string_len);
str[name.string_len] = 0;
wxString final(str, wxConvLocal);
delete [] str;
return final;
}
///////////////////////////
// Gather data from system
void FreetypeFontFileLister::DoInitialize() {
@ -104,8 +119,23 @@ void FreetypeFontFileLister::DoInitialize() {
fterr = FT_New_Face(ft2lib, fontfiles[i].mb_str(*wxConvFileName), facenum, &face);
if (fterr) break;
// Special names for TTF and OTF
int nameCount = 0;
wxString ext = fontfiles[i].Right(4).Lower();
if (ext == _T(".otf") || ext == _T(".ttf") || ext == _T(".ttc_")) nameCount = FT_Get_Sfnt_Name_Count(face);
if (nameCount >= 5) {
wxString family = GetName(face,1);
wxString subFamily = GetName(face,2);
wxString fullName = GetName(face,4);
//AddFont(fontfiles[i],family);
AddFont(fontfiles[i],family + _T(" ") + subFamily);;
AddFont(fontfiles[i],fullName);
AddFont(fontfiles[i],GetName(face,11));
}
// Add font
AddFont(fontfiles[i],wxString(face->family_name, wxConvLocal));
if (face->style_name) AddFont(fontfiles[i],wxString(face->family_name, wxConvLocal) + _T(" ") + wxString(face->style_name, wxConvLocal));
FT_Done_Face(face);
}
}