Skip to content

Commit

Permalink
New font selector (fix #4363)
Browse files Browse the repository at this point in the history
This new font selector list installed fonts with its proper name. It
needs some extra work yet to select font set styles (regular, bold,
italic, etc.)
  • Loading branch information
dacap committed Mar 25, 2024
1 parent f2a3daa commit 047bebe
Show file tree
Hide file tree
Showing 28 changed files with 1,070 additions and 369 deletions.
4 changes: 2 additions & 2 deletions INSTALL.md
Expand Up @@ -51,7 +51,7 @@ To compile Aseprite you will need:

* The latest version of [CMake](https://cmake.org) (3.16 or greater)
* [Ninja](https://ninja-build.org) build system
* And a compiled version of the `aseprite-m102` branch of
* And a compiled version of the `aseprite-m124` branch of
the [Skia library](https://github.com/aseprite/skia#readme).
There are [pre-built packages available](https://github.com/aseprite/skia/releases).
You can get some extra information in
Expand Down Expand Up @@ -190,7 +190,7 @@ but it could be different in your Mac.
### Apple Silicon

If you running macOS on an ARM64/AArch64/Apple Silicon Mac (e.g. M1),
you can compile a native ARM64 version of Aseprite following similar
you can compile a native ARM64 version of Aseprite following similar
steps as above but when we call `cmake`, we have some differences:

cd aseprite
Expand Down
1 change: 1 addition & 0 deletions data/pref.xml
Expand Up @@ -347,6 +347,7 @@
<option id="font_face" type="std::string" />
<option id="font_size" type="int" default="12" />
<option id="antialias" type="bool" default="false" />
<option id="font_info" type="std::string" />
</section>
<section id="symmetry_mode">
<option id="enabled" type="bool" default="false" />
Expand Down
6 changes: 4 additions & 2 deletions data/strings/en.ini
Expand Up @@ -764,8 +764,10 @@ preview = &Preview
tiled = &Tiled
[font_popup]
title = Fonts
load = Load
theme_fonts = Theme Fonts
system_fonts = System Fonts
load = Load External Font
select_truetype_fonts = Select a Font File
empty_fonts = No system fonts were found
[frame_combo]
Expand Down
4 changes: 2 additions & 2 deletions data/widgets/font_popup.xml
@@ -1,12 +1,12 @@
<!-- Aseprite -->
<!-- Copyright (C) 2024 by Igara Studio S.A. -->
<!-- Copyright (C) 2015-2017 by David Capello -->
<gui>
<vbox id="font_popup">
<search id="search" magnet="true" />
<view id="view" expansive="true" />
<hbox>
<boxfiller />
<button id="load_font" text="@.load" magnet="true" minwidth="80" />
<button id="load_font" text="@.load" minwidth="80" />
</hbox>
</vbox>
</gui>
17 changes: 6 additions & 11 deletions data/widgets/paste_text.xml
@@ -1,24 +1,19 @@
<!-- Aseprite -->
<!-- Copyright (C) 2024 by Igara Studio S.A. -->
<!-- Copyright (C) 2015-2018 by David Capello -->
<gui>
<window id="paste_text" text="@.title">
<grid columns="2">
<label text="@.text" />
<entry expansive="true" maxsize="256" id="user_text" magnet="true" cell_align="horizontal" />

<label text="@.font_size" />
<expr id="font_size" text="32" cell_align="horizontal" />
<entry expansive="true" maxsize="256" minwidth="256" id="user_text" magnet="true" cell_align="horizontal" />

<label text="@.font" />
<dropdownbutton minwidth="60" id="font_face" text="@.select_font" cell_align="horizontal" />
<font id="font_face" cell_align="horizontal" />

<label text="@.color" />
<colorpicker id="font_color" cell_align="horizontal" />

<hbox />
<check id="antialias"
text="@.antialias" cell_align="horizontal"
tooltip="@.antialias_tooltip" />
<hbox>
<colorpicker id="font_color" />
</hbox>

<separator horizontal="true" cell_hspan="2" />

Expand Down
5 changes: 3 additions & 2 deletions src/app/CMakeLists.txt
Expand Up @@ -388,6 +388,7 @@ if(ENABLE_UI)
ui/file_list_view.cpp
ui/file_selector.cpp
ui/filename_field.cpp
ui/font_entry.cpp
ui/font_popup.cpp
ui/hex_color_entry.cpp
ui/home_view.cpp
Expand Down Expand Up @@ -639,6 +640,7 @@ add_library(app-lib
file_system.cpp
filename_formatter.cpp
flatten.cpp
font_info.cpp
font_path.cpp
gui_xml.cpp
i18n/strings.cpp
Expand Down Expand Up @@ -688,7 +690,6 @@ add_library(app-lib
util/conversion_to_surface.cpp
util/expand_cel_canvas.cpp
util/filetoks.cpp
util/freetype_utils.cpp
util/layer_boundaries.cpp
util/layer_utils.cpp
util/msk_file.cpp
Expand All @@ -698,6 +699,7 @@ add_library(app-lib
util/pixel_ratio.cpp
util/range_utils.cpp
util/readable_time.cpp
util/render_text.cpp
util/resize_image.cpp
util/shader_helpers.cpp
util/tile_flags_utils.cpp
Expand Down Expand Up @@ -728,7 +730,6 @@ target_link_libraries(app-lib
tga-lib
laf-gfx
render-lib
laf-ft
laf-os
laf-text
ui-lib
Expand Down
134 changes: 39 additions & 95 deletions src/app/commands/cmd_paste_text.cpp
@@ -1,5 +1,5 @@
// Aseprite
// Copyright (C) 2019-2022 Igara Studio S.A.
// Copyright (C) 2019-2024 Igara Studio S.A.
// Copyright (C) 2001-2018 David Capello
//
// This program is distributed under the terms of
Expand All @@ -13,13 +13,12 @@
#include "app/commands/command.h"
#include "app/console.h"
#include "app/context.h"
#include "app/file_selector.h"
#include "app/pref/preferences.h"
#include "app/ui/drop_down_button.h"
#include "app/ui/editor/editor.h"
#include "app/ui/font_popup.h"
#include "app/ui/skin/skin_theme.h"
#include "app/ui/timeline/timeline.h"
#include "app/util/freetype_utils.h"
#include "app/util/render_text.h"
#include "base/fs.h"
#include "base/string.h"
#include "doc/image.h"
Expand Down Expand Up @@ -57,84 +56,16 @@ bool PasteTextCommand::onEnabled(Context* ctx)

class PasteTextWindow : public app::gen::PasteText {
public:
PasteTextWindow(const std::string& face, int size,
bool antialias,
const app::Color& color)
: m_face(face) {
ok()->setEnabled(!m_face.empty());
if (!m_face.empty())
updateFontFaceButton();

fontSize()->setTextf("%d", size);
fontFace()->Click.connect([this]{ onSelectFontFile(); });
fontFace()->DropDownClick.connect([this]{ onSelectSystemFont(); });
PasteTextWindow(const FontInfo& fontInfo,
const app::Color& color) {
fontFace()->setInfo(fontInfo);
fontColor()->setColor(color);
this->antialias()->setSelected(antialias);
}

std::string faceValue() const {
return m_face;
FontInfo fontInfo() const {
return fontFace()->info();
}

int sizeValue() const {
int size = fontSize()->textInt();
size = std::clamp(size, 1, 5000);
return size;
}

private:
void updateFontFaceButton() {
fontFace()->mainButton()
->setTextf("Select Font: %s",
base::get_file_title(m_face).c_str());
}

void onSelectFontFile() {
base::paths exts = { "ttf", "ttc", "otf", "dfont" };
base::paths face;
if (!show_file_selector(
"Select a TrueType Font",
m_face, exts,
FileSelectorType::Open, face))
return;

ASSERT(!face.empty());
setFontFace(face.front());
}

void setFontFace(const std::string& face) {
m_face = face;
ok()->setEnabled(true);
updateFontFaceButton();
}

void onSelectSystemFont() {
if (!m_fontPopup) {
try {
m_fontPopup.reset(new FontPopup());
m_fontPopup->Load.connect(&PasteTextWindow::setFontFace, this);
m_fontPopup->Close.connect([this]{ onCloseFontPopup(); });
}
catch (const std::exception& ex) {
Console::showException(ex);
return;
}
}

if (!m_fontPopup->isVisible()) {
m_fontPopup->showPopup(display(), fontFace()->bounds());
}
else {
m_fontPopup->closeWindow(NULL);
}
}

void onCloseFontPopup() {
fontFace()->dropDown()->requestFocus();
}

std::string m_face;
std::unique_ptr<FontPopup> m_fontPopup;
};

void PasteTextCommand::onExecute(Context* ctx)
Expand All @@ -144,10 +75,21 @@ void PasteTextCommand::onExecute(Context* ctx)
return;

Preferences& pref = Preferences::instance();
PasteTextWindow window(pref.textTool.fontFace(),
pref.textTool.fontSize(),
pref.textTool.antialias(),
pref.colorBar.fgColor());
FontInfo fontInfo;

// Old configuration
if (!pref.textTool.fontFace().empty()) {
fontInfo = FontInfo(FontInfo::Type::File,
pref.textTool.fontFace(),
pref.textTool.fontSize(),
pref.textTool.antialias());
}
// New configuration
if (!pref.textTool.fontInfo().empty()) {
fontInfo = base::convert_to<FontInfo>(pref.textTool.fontInfo());
}

PasteTextWindow window(fontInfo, pref.colorBar.fgColor());

window.userText()->setText(last_text_used);

Expand All @@ -157,23 +99,25 @@ void PasteTextCommand::onExecute(Context* ctx)

last_text_used = window.userText()->text();

bool antialias = window.antialias()->isSelected();
std::string faceName = window.faceValue();
int size = window.sizeValue();
size = std::clamp(size, 1, 999);
pref.textTool.fontFace(faceName);
pref.textTool.fontSize(size);
pref.textTool.antialias(antialias);
fontInfo = window.fontInfo();
pref.textTool.fontInfo(base::convert_to<std::string>(fontInfo));
if (!pref.textTool.fontFace().empty()) {
pref.textTool.fontFace.clearValue();
pref.textTool.fontSize.clearValue();
pref.textTool.antialias.clearValue();
}

try {
auto* theme = skin::SkinTheme::instance();
std::string text = window.userText()->text();
app::Color appColor = window.fontColor()->getColor();
doc::color_t color = doc::rgba(appColor.getRed(),
appColor.getGreen(),
appColor.getBlue(),
appColor.getAlpha());

doc::ImageRef image(render_text(faceName, size, text, color, antialias));
app::Color color = window.fontColor()->getColor();

doc::ImageRef image = render_text(
theme->fontMgr(), fontInfo, text,
gfx::rgba(color.getRed(),
color.getGreen(),
color.getBlue(),
color.getAlpha()));
if (image) {
Sprite* sprite = editor->sprite();
if (image->pixelFormat() != sprite->pixelFormat()) {
Expand Down

0 comments on commit 047bebe

Please sign in to comment.