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

View File

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