Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New text tool #4383

Draft
wants to merge 7 commits into
base: beta
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -382,6 +382,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 @@ -629,6 +630,7 @@ target_sources(app-lib PRIVATE
file_system.cpp
filename_formatter.cpp
flatten.cpp
font_info.cpp
font_path.cpp
gui_xml.cpp
i18n/strings.cpp
Expand Down Expand Up @@ -678,7 +680,6 @@ target_sources(app-lib PRIVATE
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 @@ -688,6 +689,7 @@ target_sources(app-lib PRIVATE
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 All @@ -712,7 +714,6 @@ target_link_libraries(app-lib
tga-lib
laf-gfx
render-lib
laf-ft
laf-os
laf-text
ui-lib
Expand Down
135 changes: 40 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 {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: method 'fontInfo' can be made static [readability-convert-member-functions-to-static]

Suggested change
FontInfo fontInfo() const {
static FontInfo fontInfo() {

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like a bug in clang-tidy.

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,22 @@ 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(),
text::FontStyle(),
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 +100,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