Add support for multiple font paths

This commit is contained in:
David Capello 2015-10-16 17:00:10 -03:00
parent 60f0c5bcf7
commit 5e239a0768
2 changed files with 36 additions and 26 deletions

View File

@ -45,9 +45,13 @@ using namespace ui;
class FontItem : public ListItem { class FontItem : public ListItem {
public: public:
FontItem(std::string& fontsDir, const std::string& fn) FontItem(const std::string& fn)
: ListItem(fn) : ListItem(base::get_file_title(fn))
, m_fontsDir(fontsDir) { , m_filename(fn) {
}
const std::string& filename() const {
return m_filename;
} }
private: private:
@ -88,17 +92,13 @@ private:
if (!getParent()) if (!getParent())
return; return;
ASSERT(getParent());
ASSERT(getParent()->type() == kListBoxWidget);
std::string filename = base::join_path(m_fontsDir, getText());
app::skin::SkinTheme* theme = app::skin::SkinTheme::instance(); app::skin::SkinTheme* theme = app::skin::SkinTheme::instance();
gfx::Color color = theme->colors.text(); gfx::Color color = theme->colors.text();
try { try {
m_image.reset( m_image.reset(
render_text( render_text(
filename, 16, m_filename, 16,
getText(), getText(),
doc::rgba(gfx::getr(color), doc::rgba(gfx::getr(color),
gfx::getg(color), gfx::getg(color),
@ -114,8 +114,8 @@ private:
} }
private: private:
std::string& m_fontsDir;
base::UniquePtr<doc::Image> m_image; base::UniquePtr<doc::Image> m_image;
std::string m_filename;
}; };
FontPopup::FontPopup() FontPopup::FontPopup()
@ -133,28 +133,44 @@ FontPopup::FontPopup()
m_popup->view()->attachToView(&m_listBox); m_popup->view()->attachToView(&m_listBox);
std::vector<std::string> fontDirs;
#if _WIN32 #if _WIN32
{ {
std::vector<wchar_t> buf(MAX_PATH); std::vector<wchar_t> buf(MAX_PATH);
HRESULT hr = SHGetFolderPath(NULL, CSIDL_FONTS, NULL, HRESULT hr = SHGetFolderPath(NULL, CSIDL_FONTS, NULL,
SHGFP_TYPE_DEFAULT, &buf[0]); SHGFP_TYPE_DEFAULT, &buf[0]);
if (hr == S_OK) { if (hr == S_OK) {
m_fontsDir = base::to_utf8(&buf[0]); fontDirs.push_back(base::to_utf8(&buf[0]));
} }
} }
#elif __APPLE__ #elif __APPLE__
{ {
m_fontsDir = "/Library/Fonts"; fontDirs.push_back("/System/Library/Fonts/");
fontDirs.push_back("/Library/Fonts");
fontDirs.push_back("~/Library/Fonts");
} }
#endif #endif
if (!m_fontsDir.empty()) { // Create a list of fullpaths to every font found in all font
auto files = base::list_files(m_fontsDir); // directories (fontDirs)
std::sort(files.begin(), files.end()); std::vector<std::string> files;
for (const auto& fontDir : fontDirs) {
auto fontDirFiles = base::list_files(fontDir);
for (const auto& file : fontDirFiles)
files.push_back(base::join_path(fontDir, file));
}
// Sort all files by "file title"
std::sort(
files.begin(), files.end(),
[](const std::string& a, const std::string& b){
return base::utf8_icmp(base::get_file_title(a), base::get_file_title(b)) < 0;
});
// Create one FontItem for each font
for (auto& file : files) { for (auto& file : files) {
if (base::string_to_lower(base::get_file_extension(file)) == "ttf") if (base::string_to_lower(base::get_file_extension(file)) == "ttf")
m_listBox.addChild(new FontItem(m_fontsDir, file)); m_listBox.addChild(new FontItem(file));
}
} }
if (m_listBox.getChildren().empty()) if (m_listBox.getChildren().empty())
@ -181,12 +197,11 @@ void FontPopup::onChangeFont()
void FontPopup::onLoadFont() void FontPopup::onLoadFont()
{ {
Widget* child = m_listBox.getSelectedChild(); FontItem* child = dynamic_cast<FontItem*>(m_listBox.getSelectedChild());
if (!child) if (!child)
return; return;
std::string filename = base::join_path(m_fontsDir, std::string filename = child->filename();
child->getText());
if (base::is_file(filename)) if (base::is_file(filename))
Load(filename); // Fire Load signal Load(filename); // Fire Load signal

View File

@ -29,10 +29,6 @@ namespace app {
void showPopup(const gfx::Rect& bounds); void showPopup(const gfx::Rect& bounds);
const std::string& fontsDir() const {
return m_fontsDir;
}
Signal1<void, const std::string&> Load; Signal1<void, const std::string&> Load;
protected: protected:
@ -42,7 +38,6 @@ namespace app {
private: private:
gen::FontPopup* m_popup; gen::FontPopup* m_popup;
ui::ListBox m_listBox; ui::ListBox m_listBox;
std::string m_fontsDir;
}; };
} // namespace app } // namespace app