2015-10-16 05:16:16 +08:00
|
|
|
// Aseprite
|
2024-03-06 05:50:24 +08:00
|
|
|
// Copyright (C) 2020-2024 Igara Studio S.A.
|
2018-02-21 21:39:30 +08:00
|
|
|
// Copyright (C) 2001-2018 David Capello
|
2015-10-16 05:16:16 +08:00
|
|
|
//
|
2016-08-27 04:02:58 +08:00
|
|
|
// This program is distributed under the terms of
|
|
|
|
// the End-User License Agreement for Aseprite.
|
2015-10-16 05:16:16 +08:00
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "app/ui/font_popup.h"
|
|
|
|
|
2024-03-25 22:59:25 +08:00
|
|
|
#include "app/file_selector.h"
|
|
|
|
#include "app/font_info.h"
|
2017-02-25 04:56:57 +08:00
|
|
|
#include "app/font_path.h"
|
2022-01-07 17:02:38 +08:00
|
|
|
#include "app/i18n/strings.h"
|
2017-03-16 01:24:42 +08:00
|
|
|
#include "app/match_words.h"
|
2024-03-25 22:59:25 +08:00
|
|
|
#include "app/ui/separator_in_view.h"
|
|
|
|
#include "app/ui/skin/font_data.h"
|
2015-10-16 05:16:16 +08:00
|
|
|
#include "app/ui/skin/skin_theme.h"
|
2020-06-10 06:56:25 +08:00
|
|
|
#include "app/util/conversion_to_surface.h"
|
2024-03-25 22:59:25 +08:00
|
|
|
#include "app/util/render_text.h"
|
2015-10-16 05:16:16 +08:00
|
|
|
#include "base/fs.h"
|
|
|
|
#include "base/string.h"
|
|
|
|
#include "doc/image.h"
|
2015-11-04 23:17:30 +08:00
|
|
|
#include "doc/image_ref.h"
|
2018-08-09 23:58:43 +08:00
|
|
|
#include "os/surface.h"
|
|
|
|
#include "os/system.h"
|
2024-03-25 22:59:25 +08:00
|
|
|
#include "text/text.h"
|
2015-10-16 05:16:16 +08:00
|
|
|
#include "ui/box.h"
|
|
|
|
#include "ui/button.h"
|
2021-03-20 05:57:56 +08:00
|
|
|
#include "ui/fit_bounds.h"
|
2018-06-09 02:52:10 +08:00
|
|
|
#include "ui/graphics.h"
|
|
|
|
#include "ui/listitem.h"
|
2024-03-25 22:59:25 +08:00
|
|
|
#include "ui/message.h"
|
2018-06-09 02:52:10 +08:00
|
|
|
#include "ui/paint_event.h"
|
2015-12-04 08:50:05 +08:00
|
|
|
#include "ui/size_hint_event.h"
|
2015-10-16 05:16:16 +08:00
|
|
|
#include "ui/theme.h"
|
|
|
|
#include "ui/view.h"
|
|
|
|
|
|
|
|
#include "font_popup.xml.h"
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
2020-04-08 23:03:32 +08:00
|
|
|
#include <shlobj.h>
|
|
|
|
#include <windows.h>
|
|
|
|
#undef max
|
2015-10-16 05:16:16 +08:00
|
|
|
#endif
|
|
|
|
|
2020-04-08 23:03:32 +08:00
|
|
|
#include <algorithm>
|
2015-11-04 23:17:30 +08:00
|
|
|
#include <map>
|
2015-10-20 03:41:14 +08:00
|
|
|
|
2015-10-16 05:16:16 +08:00
|
|
|
namespace app {
|
|
|
|
|
|
|
|
using namespace ui;
|
|
|
|
|
2024-03-25 22:59:25 +08:00
|
|
|
static std::map<std::string, os::SurfaceRef> g_thumbnails;
|
2015-11-04 23:17:30 +08:00
|
|
|
|
2015-10-16 05:16:16 +08:00
|
|
|
class FontItem : public ListItem {
|
|
|
|
public:
|
2024-03-25 22:59:25 +08:00
|
|
|
struct ByName { };
|
|
|
|
|
|
|
|
FontItem(const std::string& name, ByName)
|
|
|
|
: ListItem(name)
|
|
|
|
, m_fontInfo(FontInfo::Type::Name, name,
|
2024-04-16 05:52:36 +08:00
|
|
|
FontInfo::kDefaultSize,
|
|
|
|
text::FontStyle(), true) {
|
2024-03-25 22:59:25 +08:00
|
|
|
getCachedThumbnail();
|
|
|
|
}
|
|
|
|
|
2015-10-17 04:00:10 +08:00
|
|
|
FontItem(const std::string& fn)
|
|
|
|
: ListItem(base::get_file_title(fn))
|
2024-03-25 22:59:25 +08:00
|
|
|
, m_fontInfo(FontInfo::Type::File, fn,
|
2024-04-16 05:52:36 +08:00
|
|
|
FontInfo::kDefaultSize,
|
|
|
|
text::FontStyle(), true) {
|
2024-03-25 22:59:25 +08:00
|
|
|
getCachedThumbnail();
|
|
|
|
}
|
|
|
|
|
|
|
|
FontItem(const std::string& name,
|
2024-04-16 05:52:36 +08:00
|
|
|
const text::FontStyle& style,
|
2024-04-16 06:02:07 +08:00
|
|
|
const text::FontStyleSetRef& set)
|
2024-03-25 22:59:25 +08:00
|
|
|
: ListItem(name)
|
|
|
|
, m_fontInfo(FontInfo::Type::System, name,
|
2024-04-16 06:02:07 +08:00
|
|
|
FontInfo::kDefaultSize, style, true)
|
2024-04-16 05:52:36 +08:00
|
|
|
, m_set(set) {
|
2024-03-25 22:59:25 +08:00
|
|
|
getCachedThumbnail();
|
2015-10-17 04:00:10 +08:00
|
|
|
}
|
|
|
|
|
2024-03-25 22:59:25 +08:00
|
|
|
FontInfo fontInfo() const {
|
|
|
|
return m_fontInfo;
|
2015-10-16 05:16:16 +08:00
|
|
|
}
|
|
|
|
|
2024-03-25 22:59:25 +08:00
|
|
|
obs::signal<void()> ThumbnailGenerated;
|
|
|
|
|
2015-10-16 05:16:16 +08:00
|
|
|
private:
|
2024-03-25 22:59:25 +08:00
|
|
|
void getCachedThumbnail() {
|
|
|
|
m_thumbnail = g_thumbnails[m_fontInfo.thumbnailId()];
|
|
|
|
}
|
|
|
|
|
2015-10-16 05:16:16 +08:00
|
|
|
void onPaint(PaintEvent& ev) override {
|
2015-10-17 05:52:52 +08:00
|
|
|
ListItem::onPaint(ev);
|
|
|
|
|
2024-03-25 22:59:25 +08:00
|
|
|
generateThumbnail();
|
2015-10-16 05:16:16 +08:00
|
|
|
|
2024-03-25 22:59:25 +08:00
|
|
|
if (m_thumbnail) {
|
|
|
|
Graphics* g = ev.graphics();
|
|
|
|
g->drawRgbaSurface(m_thumbnail.get(), textWidth()+4, 0);
|
2015-10-16 05:16:16 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-04 08:50:05 +08:00
|
|
|
void onSizeHint(SizeHintEvent& ev) override {
|
|
|
|
ListItem::onSizeHint(ev);
|
2024-03-25 22:59:25 +08:00
|
|
|
if (m_thumbnail) {
|
2015-12-04 08:50:05 +08:00
|
|
|
gfx::Size sz = ev.sizeHint();
|
|
|
|
ev.setSizeHint(
|
2024-03-25 22:59:25 +08:00
|
|
|
sz.w + 4 + m_thumbnail->width(),
|
|
|
|
std::max(sz.h, m_thumbnail->height()));
|
2015-10-17 05:52:52 +08:00
|
|
|
}
|
2015-10-16 05:16:16 +08:00
|
|
|
}
|
|
|
|
|
2024-03-25 22:59:25 +08:00
|
|
|
void generateThumbnail() {
|
|
|
|
if (m_thumbnail)
|
2015-10-16 05:16:16 +08:00
|
|
|
return;
|
|
|
|
|
2024-03-25 22:59:25 +08:00
|
|
|
const auto* theme = app::skin::SkinTheme::get(this);
|
2015-10-16 05:16:16 +08:00
|
|
|
|
|
|
|
try {
|
2024-03-25 22:59:25 +08:00
|
|
|
const text::FontMgrRef fontMgr = theme->fontMgr();
|
|
|
|
const gfx::Color color = theme->colors.text();
|
|
|
|
doc::ImageRef image =
|
|
|
|
render_text(fontMgr, m_fontInfo, text(), color);
|
|
|
|
if (!image)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Convert the doc::Image into a os::Surface
|
|
|
|
m_thumbnail = os::System::instance()
|
|
|
|
->makeRgbaSurface(image->width(),
|
|
|
|
image->height());
|
|
|
|
convert_image_to_surface(
|
|
|
|
image.get(), nullptr, m_thumbnail.get(),
|
|
|
|
0, 0, 0, 0, image->width(), image->height());
|
2015-11-04 23:17:30 +08:00
|
|
|
|
|
|
|
// Save the thumbnail for future FontPopups
|
2024-03-25 22:59:25 +08:00
|
|
|
g_thumbnails[m_fontInfo.thumbnailId()] = m_thumbnail;
|
|
|
|
|
|
|
|
ThumbnailGenerated();
|
2015-10-16 05:16:16 +08:00
|
|
|
}
|
2015-10-30 03:18:51 +08:00
|
|
|
catch (const std::exception&) {
|
2015-10-17 05:52:52 +08:00
|
|
|
// Ignore errors
|
2015-10-16 05:16:16 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-25 22:59:25 +08:00
|
|
|
void onSelect(bool selected) override {
|
|
|
|
if (!selected || m_thumbnail)
|
|
|
|
return;
|
|
|
|
|
|
|
|
ListBox* listbox = static_cast<ListBox*>(parent());
|
|
|
|
if (!listbox)
|
|
|
|
return;
|
|
|
|
|
|
|
|
generateThumbnail();
|
|
|
|
listbox->makeChildVisible(this);
|
|
|
|
}
|
|
|
|
|
2015-10-16 05:16:16 +08:00
|
|
|
private:
|
2024-03-25 22:59:25 +08:00
|
|
|
os::SurfaceRef m_thumbnail;
|
|
|
|
FontInfo m_fontInfo;
|
2024-04-16 05:52:36 +08:00
|
|
|
text::FontStyleSetRef m_set;
|
2015-10-16 05:16:16 +08:00
|
|
|
};
|
|
|
|
|
2024-03-25 22:59:25 +08:00
|
|
|
bool FontPopup::FontListBox::onProcessMessage(ui::Message* msg)
|
|
|
|
{
|
|
|
|
const bool result = ui::ListBox::onProcessMessage(msg);
|
|
|
|
|
|
|
|
// When we release the mouse button we close the popup, i.e. like
|
|
|
|
// selecting an item from a combo box.
|
|
|
|
if (msg->type() == ui::kMouseUpMessage) {
|
|
|
|
if (auto* win = this->window()) {
|
|
|
|
win->closeWindow(nullptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
FontPopup::FontPopup(const FontInfo& fontInfo)
|
|
|
|
: PopupWindow(std::string(),
|
2015-12-06 02:56:32 +08:00
|
|
|
ClickBehavior::CloseOnClickInOtherWindow,
|
|
|
|
EnterBehavior::DoNothingOnEnter)
|
2015-10-16 05:16:16 +08:00
|
|
|
, m_popup(new gen::FontPopup())
|
2024-03-25 22:59:25 +08:00
|
|
|
, m_timer(100)
|
2015-10-16 05:16:16 +08:00
|
|
|
{
|
|
|
|
setAutoRemap(false);
|
|
|
|
setBorder(gfx::Border(4*guiscale()));
|
|
|
|
|
|
|
|
addChild(m_popup);
|
|
|
|
|
2024-03-25 22:59:25 +08:00
|
|
|
m_timer.Tick.connect([this]{ onTickRelayout(); });
|
2020-07-04 08:51:46 +08:00
|
|
|
m_popup->loadFont()->Click.connect([this]{ onLoadFont(); });
|
2015-10-20 02:32:44 +08:00
|
|
|
m_listBox.setFocusMagnet(true);
|
2024-03-25 22:59:25 +08:00
|
|
|
m_listBox.Change.connect([this]{
|
|
|
|
if (m_listBox.hasFocus() ||
|
|
|
|
m_listBox.hasCapture()) {
|
|
|
|
onFontChange();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
m_listBox.DoubleClickItem.connect([this]{
|
|
|
|
onFontChange();
|
|
|
|
});
|
2015-10-16 05:16:16 +08:00
|
|
|
|
|
|
|
m_popup->view()->attachToView(&m_listBox);
|
|
|
|
|
2024-03-25 22:59:25 +08:00
|
|
|
// Default fonts
|
|
|
|
bool firstThemeFont = true;
|
|
|
|
for (auto kv : skin::SkinTheme::get(this)->getWellKnownFonts()) {
|
|
|
|
if (!kv.second->filename().empty()) {
|
|
|
|
if (firstThemeFont) {
|
|
|
|
m_listBox.addChild(new SeparatorInView(Strings::font_popup_theme_fonts()));
|
|
|
|
firstThemeFont = false;
|
|
|
|
}
|
|
|
|
m_listBox.addChild(new FontItem(kv.first, FontItem::ByName()));
|
2015-10-20 03:41:14 +08:00
|
|
|
}
|
2015-10-17 04:00:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create one FontItem for each font
|
2024-03-25 22:59:25 +08:00
|
|
|
m_listBox.addChild(new SeparatorInView(Strings::font_popup_system_fonts()));
|
|
|
|
bool empty = true;
|
|
|
|
|
|
|
|
// Get system fonts from laf-text module
|
|
|
|
const text::FontMgrRef fontMgr = theme()->fontMgr();
|
|
|
|
const int n = fontMgr->countFamilies();
|
|
|
|
if (n > 0) {
|
|
|
|
for (int i=0; i<n; ++i) {
|
|
|
|
std::string name = fontMgr->familyName(i);
|
|
|
|
text::FontStyleSetRef set = fontMgr->familyStyleSet(i);
|
|
|
|
if (set && set->count() > 0) {
|
2024-04-16 05:52:36 +08:00
|
|
|
// Match the typeface with the default FontStyle (Normal
|
|
|
|
// weight, Upright slant, etc.)
|
|
|
|
auto typeface = set->matchStyle(text::FontStyle());
|
|
|
|
if (typeface) {
|
2024-04-16 06:02:07 +08:00
|
|
|
auto* item = new FontItem(name, typeface->fontStyle(), set);
|
2024-04-16 05:52:36 +08:00
|
|
|
item->ThumbnailGenerated.connect([this]{ onThumbnailGenerated(); });
|
|
|
|
m_listBox.addChild(item);
|
|
|
|
empty = false;
|
2024-03-27 03:00:39 +08:00
|
|
|
}
|
2024-03-25 22:59:25 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Get fonts listing .ttf files TODO we should be able to remove
|
|
|
|
// this code in the future (probably after DirectWrite API is always
|
|
|
|
// available).
|
|
|
|
else {
|
|
|
|
base::paths fontDirs;
|
|
|
|
get_font_dirs(fontDirs);
|
|
|
|
|
|
|
|
// Create a list of fullpaths to every font found in all font
|
|
|
|
// directories (fontDirs)
|
|
|
|
base::paths files;
|
|
|
|
for (const auto& fontDir : fontDirs) {
|
|
|
|
for (const auto& file : base::list_files(fontDir)) {
|
|
|
|
std::string fullpath = base::join_path(fontDir, file);
|
|
|
|
if (base::is_file(fullpath))
|
|
|
|
files.push_back(fullpath);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
});
|
|
|
|
|
|
|
|
for (auto& file : files) {
|
|
|
|
std::string ext = base::string_to_lower(base::get_file_extension(file));
|
|
|
|
if (ext == "ttf" || ext == "ttc" ||
|
|
|
|
ext == "otf" || ext == "dfont") {
|
|
|
|
m_listBox.addChild(new FontItem(file));
|
|
|
|
empty = false;
|
|
|
|
}
|
|
|
|
}
|
2015-10-16 05:16:16 +08:00
|
|
|
}
|
|
|
|
|
2024-03-25 22:59:25 +08:00
|
|
|
if (empty)
|
2022-01-07 17:02:38 +08:00
|
|
|
m_listBox.addChild(new ListItem(Strings::font_popup_empty_fonts()));
|
2024-03-25 22:59:25 +08:00
|
|
|
|
|
|
|
for (auto* child : m_listBox.children()) {
|
|
|
|
if (auto* childItem = dynamic_cast<FontItem*>(child)) {
|
|
|
|
if (childItem->fontInfo().title() == childItem->text()) {
|
|
|
|
m_listBox.selectChild(childItem);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
FontPopup::~FontPopup()
|
|
|
|
{
|
|
|
|
m_timer.stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
void FontPopup::focusListBox()
|
|
|
|
{
|
|
|
|
m_listBox.requestFocus();
|
|
|
|
}
|
|
|
|
|
|
|
|
void FontPopup::setSearchText(const std::string& searchText)
|
|
|
|
{
|
|
|
|
FontItem* firstItem = nullptr;
|
|
|
|
|
|
|
|
const MatchWords match(searchText);
|
|
|
|
for (auto* child : m_listBox.children()) {
|
|
|
|
auto* childItem = dynamic_cast<FontItem*>(child);
|
|
|
|
if (!childItem)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
const bool visible = match(childItem->text());
|
|
|
|
if (visible && !firstItem)
|
|
|
|
firstItem = childItem;
|
|
|
|
childItem->setVisible(visible);
|
|
|
|
}
|
|
|
|
|
|
|
|
m_listBox.selectChild(firstItem);
|
|
|
|
layout();
|
2015-10-16 05:16:16 +08:00
|
|
|
}
|
|
|
|
|
2021-03-20 05:57:56 +08:00
|
|
|
void FontPopup::showPopup(Display* display,
|
|
|
|
const gfx::Rect& buttonBounds)
|
2015-10-16 05:16:16 +08:00
|
|
|
{
|
2024-03-25 22:59:25 +08:00
|
|
|
m_listBox.selectChild(nullptr);
|
2015-10-16 05:16:16 +08:00
|
|
|
|
2021-03-20 05:57:56 +08:00
|
|
|
ui::fit_bounds(display, this,
|
|
|
|
gfx::Rect(buttonBounds.x, buttonBounds.y2(), 32, 32),
|
|
|
|
[](const gfx::Rect& workarea,
|
|
|
|
gfx::Rect& bounds,
|
|
|
|
std::function<gfx::Rect(Widget*)> getWidgetBounds) {
|
|
|
|
bounds.w = workarea.w / 2;
|
|
|
|
bounds.h = workarea.h / 2;
|
|
|
|
});
|
2015-10-16 05:16:16 +08:00
|
|
|
|
|
|
|
openWindow();
|
|
|
|
}
|
|
|
|
|
2024-03-25 22:59:25 +08:00
|
|
|
FontInfo FontPopup::selectedFont()
|
2017-03-16 01:24:42 +08:00
|
|
|
{
|
2024-03-25 22:59:25 +08:00
|
|
|
const FontItem* child = dynamic_cast<FontItem*>(m_listBox.getSelectedChild());
|
|
|
|
if (child)
|
|
|
|
return child->fontInfo();
|
|
|
|
return FontInfo();
|
2017-03-16 01:24:42 +08:00
|
|
|
}
|
|
|
|
|
2024-03-25 22:59:25 +08:00
|
|
|
void FontPopup::onFontChange()
|
2015-10-16 05:16:16 +08:00
|
|
|
{
|
2024-03-25 22:59:25 +08:00
|
|
|
const FontInfo fontInfo = selectedFont();
|
|
|
|
if (fontInfo.isValid())
|
|
|
|
ChangeFont(fontInfo);
|
2015-10-16 05:16:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void FontPopup::onLoadFont()
|
|
|
|
{
|
2024-03-25 22:59:25 +08:00
|
|
|
std::string currentFile;
|
|
|
|
const FontInfo fontInfo = selectedFont();
|
|
|
|
if (fontInfo.isValid() && fontInfo.type() == FontInfo::Type::File)
|
|
|
|
currentFile = fontInfo.name();
|
|
|
|
|
|
|
|
base::paths exts = { "ttf", "ttc", "otf", "dfont" };
|
|
|
|
base::paths face;
|
|
|
|
if (!show_file_selector(
|
|
|
|
Strings::font_popup_select_truetype_fonts(),
|
|
|
|
currentFile, exts,
|
|
|
|
FileSelectorType::Open, face))
|
2015-10-16 05:16:16 +08:00
|
|
|
return;
|
|
|
|
|
2024-03-25 22:59:25 +08:00
|
|
|
ASSERT(!face.empty());
|
|
|
|
ChangeFont(FontInfo(FontInfo::Type::File,
|
|
|
|
face.front()));
|
|
|
|
}
|
2015-10-16 05:16:16 +08:00
|
|
|
|
2024-03-25 22:59:25 +08:00
|
|
|
void FontPopup::onThumbnailGenerated()
|
|
|
|
{
|
|
|
|
m_timer.start();
|
|
|
|
}
|
|
|
|
|
|
|
|
void FontPopup::onTickRelayout()
|
|
|
|
{
|
|
|
|
m_popup->view()->updateView();
|
|
|
|
m_timer.stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FontPopup::onProcessMessage(ui::Message* msg)
|
|
|
|
{
|
|
|
|
switch (msg->type()) {
|
|
|
|
|
|
|
|
case kKeyDownMessage: {
|
|
|
|
const auto* keymsg = static_cast<const KeyMessage*>(msg);
|
|
|
|
|
|
|
|
// Pressing Esc or Enter will just close the popup.
|
|
|
|
if (keymsg->scancode() == kKeyEsc ||
|
|
|
|
keymsg->scancode() == kKeyEnter ||
|
|
|
|
keymsg->scancode() == kKeyEnterPad) {
|
|
|
|
EscKey();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
return ui::PopupWindow::onProcessMessage(msg);
|
2015-10-16 05:16:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace app
|