Exemple #1
0
void TtfFontList::LoadAll() {
    if(loaded) return;

    for(const std::string &font : GetFontFiles()) {
        TtfFont tf = {};
        tf.fontFile = font;
        if(tf.LoadFromFile(fontLibrary))
            l.Add(&tf);
    }

    // Sort fonts according to their actual name, not filename.
    std::sort(&l.elem[0], &l.elem[l.n],
        [](const TtfFont &a, const TtfFont &b) { return a.name < b.name; });

    // Filter out fonts with the same family and style name. This is not
    // strictly necessarily the exact same font, but it will almost always be.
    TtfFont *it = std::unique(&l.elem[0], &l.elem[l.n],
        [](const TtfFont &a, const TtfFont &b) { return a.name == b.name; });
    l.RemoveLast(&l.elem[l.n] - it);

    // TODO: identify fonts by their name and not filename, which may change
    // between OSes.

    loaded = true;
}
///////////
// Collect
void FontsCollectorThread::Collect() {
	// Make sure there is a separator at the end
	destination += _T("\\");

	// Reset log box
	wxTextCtrl *LogBox = collector->LogBox;
	wxMutexGuiEnter();
	LogBox->SetValue(_T(""));
	LogBox->SetDefaultStyle(wxTextAttr(wxColour(0,0,180)));
	LogBox->AppendText(_("Searching for fonts in file...\n"));
	LogBox->SetDefaultStyle(wxTextAttr(wxColour(0,0,0)));
	LogBox->Refresh();
	LogBox->Update();
	wxSafeYield();
	wxMutexGuiLeave();

	// Scans file
	bool fileModified = false;
	AssStyle *curStyle;
	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);
			wxMutexGuiEnter();
			LogBox->AppendText(wxString(_T("\"")) + curStyle->font + _("\" found on style \"") + curStyle->name + _T("\".\n"));
			LogBox->Refresh();
			LogBox->Update();
			wxSafeYield();
			wxMutexGuiLeave();
		}

		// Collect from dialogue
		else {
			curDiag = AssEntry::GetAsDialogue(*cur);
			if (curDiag) {
				curLine++;
				curDiag->ParseASSTags();
				curDiag->ProcessParameters(GetFonts);
				curDiag->ClearBlocks();
			}
		}
	}

#ifdef __WINDOWS__
	// Collect font data
	wxMutexGuiEnter();
	LogBox->SetDefaultStyle(wxTextAttr(wxColour(0,0,180)));
	LogBox->AppendText(_("\nReading fonts from registry...\n"));
	LogBox->SetDefaultStyle(wxTextAttr(wxColour(0,0,0)));
	wxSafeYield();
	wxMutexGuiLeave();
	CollectFontData();

	// Get fonts folder
	wxString source;
	TCHAR szPath[MAX_PATH];
	if (SUCCEEDED(SHGetFolderPath(NULL, CSIDL_FONTS,NULL,0,szPath))) {
		source = wxString(szPath);
	}
	else source = wxGetOSDirectory() + _T("\\fonts");
	source += _T("\\");

	// Get font file names
	wxArrayString work;
	wxArrayString copied;
	bool attaching = collector->AttachmentCheck->IsChecked();
	for (size_t i=0;i<fonts.GetCount();i++) {
		try {
			work = GetFontFiles(fonts[i]);
			for (size_t j=0;j<work.GetCount();j++) {
				// Get path to font file
				wxString srcFile,dstFile;
				wxFileName srcFileName(work[j]);
				if (srcFileName.FileExists() && srcFileName.IsAbsolute()) {
					srcFile = work[j];
					dstFile = destination + srcFileName.GetFullName();
				}
				else {
					srcFile = source + work[j];
					dstFile = destination + work[j];
				}

				if (copied.Index(work[j]) == wxNOT_FOUND) {
					copied.Add(work[j]);

					// Check if it exists
					if (!attaching && wxFileName::FileExists(dstFile)) {
						wxMutexGuiEnter();
						LogBox->SetDefaultStyle(wxTextAttr(wxColour(255,128,0)));
						LogBox->AppendText(wxString(_T("\"")) + work[j] + _("\" already exists on destination.\n"));
						LogBox->Refresh();
						LogBox->Update();
						wxSafeYield();
						wxMutexGuiLeave();
					}

					// Copy
					else {
						// Copy font
						bool success;
						if (attaching) {
							try {
								subs->InsertAttachment(srcFile);
								fileModified = true;
								success = true;
							}
							catch (...) { success = false; }
						}
						else success = Copy(srcFile,dstFile);

						// Report
						wxMutexGuiEnter();
						if (success) {
							LogBox->SetDefaultStyle(wxTextAttr(wxColour(0,180,0)));
							LogBox->AppendText(wxString(_T("\"")) + work[j] + _("\" copied.\n"));

						}
						else {
							LogBox->SetDefaultStyle(wxTextAttr(wxColour(220,0,0)));
							LogBox->AppendText(wxString(_("Failed copying \"")) + srcFile + _T("\".\n"));
						}
						LogBox->Refresh();
						LogBox->Update();
						wxSafeYield();
						wxMutexGuiLeave();
					}
				}
			}
		}

		catch (...) {
			wxMutexGuiEnter();
			LogBox->SetDefaultStyle(wxTextAttr(wxColour(220,0,0)));
			LogBox->AppendText(wxString(_("Could not find font ")) + fonts[i] + _T("\n"));
			wxMutexGuiLeave();
		}
	}
#endif

	// Flag file as modified
	if (fileModified) {
		subs->FlagAsModified();
		collector->main->SubsBox->CommitChanges();
	}
}