Skip to content

OpenToonz - An open-source full-featured 2D animation creation software

C

Add Editable Text History to Type Tool (#7190)

Rodneybc64c12b2d3a
diff --git a/toonz/sources/tnztools/CMakeLists.txt b/toonz/sources/tnztools/CMakeLists.txt
index 14e3b4ef7..2b398959e 100644
--- a/toonz/sources/tnztools/CMakeLists.txt
+++ b/toonz/sources/tnztools/CMakeLists.txt
@@ -22,6 +22,7 @@ set(MOC_HEADERS
set(HEADERS
${MOC_HEADERS}
autofill.h
+ typetooltext.h
bluredbrush.h
hookselection.h
mypainttoonzbrush.h
@@ -113,6 +114,7 @@ set(SOURCES
stylepickertool.cpp
trackertool.cpp
typetool.cpp
+ typetooltext.cpp
tooloptionscontrols.cpp
tooloptions.cpp
vectorerasertool.cpp
diff --git a/toonz/sources/tnztools/tests/CMakeLists.txt b/toonz/sources/tnztools/tests/CMakeLists.txt
new file mode 100644
index 000000000..dfbcf598a
--- /dev/null
+++ b/toonz/sources/tnztools/tests/CMakeLists.txt
@@ -0,0 +1,14 @@
+cmake_minimum_required(VERSION 3.16)
+project(TypeToolTextTests LANGUAGES CXX)
+
+set(CMAKE_CXX_STANDARD 17)
+set(CMAKE_CXX_STANDARD_REQUIRED ON)
+set(CMAKE_POSITION_INDEPENDENT_CODE ON)
+find_package(Qt5 5.14 REQUIRED COMPONENTS Core Gui)
+
+enable_testing()
+add_executable(typetooltext_test typetooltext_test.cpp ../typetooltext.cpp)
+target_include_directories(typetooltext_test PRIVATE ..)
+target_link_libraries(typetooltext_test PRIVATE Qt5::Core Qt5::Gui)
+add_test(NAME typetooltext COMMAND typetooltext_test)
+set_tests_properties(typetooltext PROPERTIES ENVIRONMENT "QT_QPA_PLATFORM=offscreen")
diff --git a/toonz/sources/tnztools/tests/typetooltext_test.cpp b/toonz/sources/tnztools/tests/typetooltext_test.cpp
new file mode 100644
index 000000000..38322aaf9
--- /dev/null
+++ b/toonz/sources/tnztools/tests/typetooltext_test.cpp
@@ -0,0 +1,180 @@
+#include "typetooltext.h"
+
+#include <QGuiApplication>
+#include <QFile>
+#include <QTemporaryDir>
+#include <QJsonArray>
+#include <QJsonDocument>
+#include <QJsonObject>
+
+#include <cmath>
+#include <cstdlib>
+#include <iostream>
+
+using namespace TypeToolText;
+
+namespace {
+void require(bool condition, const char *message) {
+ if (!condition) {
+ std::cerr << message << std::endl;
+ std::exit(1);
+ }
+}
+
+TypeToolTextEntry markdown(const QString &source) {
+ TypeToolTextEntry entry;
+ entry.source = source;
+ entry.format = TypeToolTextFormat::Markdown;
+ entry.font = {QStringLiteral("DejaVu Sans"), QStringLiteral("Book"),
+ QStringLiteral("24")};
+ return entry;
+}
+} // namespace
+
+int main(int argc, char **argv) {
+ QGuiApplication application(argc, argv);
+ TypeToolTextEntry entry = markdown(QStringLiteral(
+ "# Title\n\nA **bold** and *italic* paragraph.\n\n- First\n- Second"));
+ const OutlineLayout formatted = buildOutlineLayout(entry);
+ require(formatted.supported && !formatted.paths.isEmpty(),
+ "Markdown produced no outlines");
+ require(formatted.plainText.contains(
+ QStringLiteral("A bold and italic paragraph.")) &&
+ !formatted.plainText.contains(QStringLiteral("#")) &&
+ !formatted.plainText.contains(QStringLiteral("**")),
+ "Markdown markup leaked into formatted text");
+ require(formatted.plainText.contains(QStringLiteral("First")) &&
+ formatted.plainText.contains(QStringLiteral("Second")),
+ "List contents were lost");
+
+ const OutlineLayout list =
+ buildOutlineLayout(markdown(QStringLiteral("- First\n- Second")));
+ require(list.paths.size() >= 4, "List markers were omitted from the artwork");
+
+ entry = markdown(QStringLiteral("# Title"));
+ const OutlineLayout heading = buildOutlineLayout(entry);
+ entry.format = TypeToolTextFormat::PlainText;
+ entry.source = QStringLiteral("Title");
+ const OutlineLayout body = buildOutlineLayout(entry);
+ require(heading.bounds.height() > body.bounds.height() * 1.3,
+ "Heading size was not applied");
+ entry.font.size = QStringLiteral("48");
+ require(buildOutlineLayout(entry).bounds.width() > body.bounds.width() * 1.7,
+ "Changing the font size did not update the outlines");
+
+ entry = markdown(QStringLiteral("first line\nsecond line"));
+ const OutlineLayout hardBreak = buildOutlineLayout(entry);
+ entry.preserveSourceLineBreaks = false;
+ const OutlineLayout softBreak = buildOutlineLayout(entry);
+ require(hardBreak.bounds.height() > softBreak.bounds.height() * 1.5,
+ "The Markdown line-break option has no effect");
+
+ entry = markdown(QStringLiteral("```\n**literal**\nnext\n```"));
+ const QString preservedCode = buildOutlineLayout(entry).plainText;
+ entry.preserveSourceLineBreaks = false;
+ require(preservedCode == buildOutlineLayout(entry).plainText &&
+ preservedCode.contains(QStringLiteral("**literal**\nnext")),
+ "Preserving line breaks changed fenced code contents");
+
+ entry = markdown(QString::fromUtf8("**Caf\xc3\xa9** and \xce\xa9"));
+ const OutlineLayout horizontal = buildOutlineLayout(entry);
+ const OutlineLayout vertical = buildOutlineLayout(entry, true);
+ require(
+ std::abs(horizontal.bounds.width() - vertical.bounds.height()) < 0.01 &&
+ std::abs(horizontal.bounds.height() - vertical.bounds.width()) < 0.01,
+ "Vertical preview and placement do not share the same rotation");
+
+ QRect bounds;
+ const QImage image = rasterizeOutlineLayout(formatted, bounds);
+ require(!image.isNull() && image.size() == bounds.size(),
+ "Formatted text could not be rasterized");
+ int inkPixels = 0;
+ for (int y = 0; y < image.height(); ++y)
+ for (int x = 0; x < image.width(); ++x)
+ if (image.constScanLine(y)[x] < 255) ++inkPixels;
+ require(inkPixels > 0 && inkPixels < image.width() * image.height() / 2,
+ "Rasterization lost the glyphs or filled the document background");
+
+ OutlineLayout oversized;
+ QPainterPath path;
+ path.addRect(0, 0, 40000, 100);
+ oversized.paths.append(path);
+ oversized.bounds = path.boundingRect();
+ require(rasterizeOutlineLayout(oversized, bounds).isNull(),
+ "An oversized raster allocation was accepted");
+
+ QTemporaryDir directory;
+ require(directory.isValid(), "Could not create a temporary test directory");
+ const QString source =
+ QString::fromUtf8("# Caf\xc3\xa9\n**Bold** and \xce\xa9\n");
+ const QString fileName = directory.filePath(QStringLiteral("source.md"));
+ QString error;
+ require(saveTextSource(fileName, source, error), "Text source export failed");
+ QFile saved(fileName);
+ require(saved.open(QIODevice::ReadOnly) && saved.readAll() == source.toUtf8(),
+ "Text export changed Markdown, Unicode, or line breaks");
+ require(
+ !saveTextSource(directory.filePath(QStringLiteral("missing/source.md")),
+ source, error) &&
+ !error.isEmpty(),
+ "A failed text export was reported as successful");
+
+ TypeToolTextEntry first = markdown(source);
+ first.font.style = QStringLiteral("Bold Italic");
+ first.preserveSourceLineBreaks = false;
+ TypeToolTextEntry second = first;
+ second.format = TypeToolTextFormat::PlainText;
+ second.font.size = QStringLiteral("86");
+ const QList<TypeToolTextEntry> original = {first, second};
+ QList<TypeToolTextEntry> loaded;
+ const QByteArray archive = serializeHistory(original);
+ require(
+ deserializeHistory(archive, loaded, error) && loaded.size() == 2 &&
+ sameTextEntry(loaded[0], first) && sameTextEntry(loaded[1], second),
+ "History round trip changed source, formatting, order, or font settings");
+
+ const QList<TypeToolTextEntry> merged = mergeHistory({first}, original);
+ require(merged.size() == 2 && sameTextEntry(merged[1], second),
+ "History merge lost a different appearance or duplicated an entry");
+ QList<TypeToolTextEntry> fullHistory;
+ for (int i = 0; i < cTextHistoryLimit; ++i) {
+ TypeToolTextEntry item = first;
+ item.source = QStringLiteral("Existing %1").arg(i);
+ fullHistory.append(item);
+ }
+ const QList<TypeToolTextEntry> capped = mergeHistory(original, fullHistory);
+ require(
+ capped.size() == cTextHistoryLimit && sameTextEntry(capped[0], first) &&
+ sameTextEntry(capped[1], second) &&
+ sameTextEntry(capped.last(), fullHistory[cTextHistoryLimit - 3]),
+ "History merge changed loaded order or exceeded the recent-entry limit");
+
+ QJsonObject invalid = QJsonDocument::fromJson(archive).object();
+ invalid.insert(QStringLiteral("version"), 2);
+ loaded = original;
+ require(!deserializeHistory(QJsonDocument(invalid).toJson(), loaded, error) &&
+ !error.isEmpty() && sameTextEntry(loaded[0], first),
+ "An unsupported history version modified existing entries");
+ invalid = QJsonDocument::fromJson(archive).object();
+ QJsonArray invalidEntries =
+ invalid.value(QStringLiteral("entries")).toArray();
+ invalidEntries.append(QJsonObject());
+ invalid.insert(QStringLiteral("entries"), invalidEntries);
+ require(
+ !deserializeHistory(QJsonDocument(invalid).toJson(), loaded, error) &&
+ loaded.size() == original.size() && sameTextEntry(loaded[1], second),
+ "Partially valid history was applied before all entries were validated");
+ QByteArray invalidUtf8 = archive;
+ invalidUtf8.insert(invalidUtf8.indexOf("Caf") + 3, char(0xff));
+ require(!deserializeHistory(invalidUtf8, loaded, error),
+ "History accepted invalid UTF-8 data");
+ fullHistory.append(first);
+ require(!deserializeHistory(serializeHistory(fullHistory), loaded, error),
+ "An oversized history was silently truncated");
+ require(deserializeHistory(serializeHistory({}), loaded, error) &&
+ loaded.isEmpty(),
+ "An empty history could not be restored");
+
+ std::cout << "Type Tool text, export, and history tests passed" << std::endl;
+ return 0;
+}
diff --git a/toonz/sources/tnztools/typetool.cpp b/toonz/sources/tnztools/typetool.cpp
index a2a3b727a..8cb6d2e08 100644
--- a/toonz/sources/tnztools/typetool.cpp
+++ b/toonz/sources/tnztools/typetool.cpp
@@ -7,6 +7,7 @@
#include "tools/toolhandle.h"
#include "tstroke.h"
+#include "tcurves.h"
#include "tmathutil.h"
#include "tproperty.h"
#include "tenv.h"
@@ -23,21 +24,50 @@
#include "tregion.h"
#include "tvectorrenderdata.h"
#include "toonz/tpalettehandle.h"
+#include "toonz/toonzfolders.h"
#include "toonzqt/selection.h"
#include "toonzqt/imageutils.h"
+#include "toonzqt/dvdialog.h"
#include "trop.h"
#include "toonz/ttileset.h"
#include "toonz/glrasterpainter.h"
#include "toonz/stage.h"
#include "tfont.h"
+#include "typetooltext.h"
// For Qt translation support
#include <QCoreApplication>
#include <QClipboard>
#include <QApplication>
+#include <QCheckBox>
+#include <QComboBox>
+#include <QFile>
+#include <QFileDialog>
+#include <QFileInfo>
+#include <QIntValidator>
#include <QMimeData>
+#include <QHBoxLayout>
+#include <QLabel>
+#include <QLineEdit>
+#include <QListWidget>
+#include <QMessageBox>
+#include <QPlainTextEdit>
+#include <QPainter>
+#include <QScrollArea>
+#include <QTabWidget>
+#include <QPushButton>
+#include <QSettings>
+#include <QSignalBlocker>
+#include <QTimer>
+#include <QTextCodec>
+#include <QVBoxLayout>
+
+#include <algorithm>
+#include <cmath>
+#include <functional>
+#include <utility>
//#include "tw/message.h"
@@ -47,6 +77,8 @@ using namespace ToolUtils;
namespace {
+using namespace TypeToolText;
+
//---------------------------------------------------------
//
// helper functions
@@ -78,6 +110,80 @@ void paintChar(const TVectorImageP &image, int styleId) {
for (j = 0; j < regNum; j++) paintRegion(image->getRegion(j), styleId, true);
}
+void paintOutlineRegion(TRegion *region, const QList<QPainterPath> &paths,
+ int styleId) {
+ TPointD point;
+ bool filled = false;
+ if (region->getInternalPoint(point)) {
+ for (const QPainterPath &path : paths) {
+ if (path.contains(QPointF(point.x, point.y))) {
+ filled = true;
+ break;
+ }
+ }
+ }
+ region->setStyle(filled ? styleId : 0);
+ for (UINT i = 0; i < region->getSubregionCount(); ++i)
+ paintOutlineRegion(region->getSubregion(i), paths, styleId);
+}
+
+TVectorImageP vectorizeOutlineLayout(const OutlineLayout &layout, double scale,
+ int styleId) {
+ TVectorImageP image = new TVectorImage;
+ QList<QPainterPath> paths;
+ const QTransform transform(scale, 0, 0, -scale, 0, 0);
+ for (const QPainterPath &outline : layout.paths) {
+ const QPainterPath path = transform.map(outline);
+ paths.append(path);
+ std::vector<TThickPoint> points;
+ auto finishContour = [&]() {
+ if (points.size() >= 3) {
+ if (points.back() != points.front()) {
+ points.push_back((points.back() + points.front()) * 0.5);
+ points.push_back(points.front());
+ }
+ TStroke *stroke = new TStroke(points);
+ stroke->setSelfLoop(true);
+ stroke->setStyle(styleId);
+ image->addStroke(stroke);
+ }
+ points.clear();
+ };
+ for (int i = 0; i < path.elementCount(); ++i) {
+ const QPainterPath::Element element = path.elementAt(i);
+ const TThickPoint point(element.x, element.y, 0);
+ if (element.type == QPainterPath::MoveToElement) {
+ finishContour();
+ points.push_back(point);
+ } else if (element.type == QPainterPath::LineToElement &&
+ !points.empty()) {
+ points.push_back((points.back() + point) * 0.5);
+ points.push_back(point);
+ } else if (element.type == QPainterPath::CurveToElement &&
+ !points.empty() && i + 2 < path.elementCount()) {
+ const QPainterPath::Element control = path.elementAt(++i);
+ const QPainterPath::Element end = path.elementAt(++i);
+ std::vector<TThickQuadratic *> quadratics;
+ computeQuadraticsFromCubic(
+ points.back(), point, TThickPoint(control.x, control.y, 0),
+ TThickPoint(end.x, end.y, 0), 0.09, quadratics);
+ for (TThickQuadratic *quadratic : quadratics) {
+ points.push_back(quadratic->getP1());
+ points.push_back(quadratic->getP2());
+ delete quadratic;
+ }
+ }
+ }
+ finishContour();
+ }
+ if (image->getStrokeCount() > 1) image->group(0, image->getStrokeCount());
+ image->enableRegionComputing(true, true);
+ image->findRegions();
+ for (UINT i = 0; i < image->getRegionCount(); ++i)
+ paintOutlineRegion(image->getRegion(i), paths, styleId);
+ return image;
+}
+
//---------------------------------------------------------
// Type, vars, ecc.
//
@@ -85,6 +191,174 @@ void paintChar(const TVectorImageP &image, int styleId) {
TEnv::StringVar EnvCurrentFont("CurrentFont", "MS UI Gothic");
+constexpr qint64 cTextImportWarningSize = 1024 * 1024;
+
+QString typeToolSettingsPath() {
+ return toQString(ToonzFolder::getMyModuleDir() + TFilePath("typetool.ini"));
+}
+
+class TypeToolTextHistory final {
+ bool m_loaded;
+ bool m_enabled;
+ TypeToolTextEntry m_draft;
+ QList<TypeToolTextEntry> m_entries;
+
+ void configureSettings(QSettings &settings) const {
+#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
+ settings.setIniCodec("UTF-8");
+#endif
+ }
+
+ void saveHistory() const {
+ QSettings values(typeToolSettingsPath(), QSettings::IniFormat);
+ configureSettings(values);
+ values.beginGroup("General");
+ values.setValue("Version", 3);
+ values.setValue("HistoryLimit", cTextHistoryLimit);
+ values.endGroup();
+ values.beginWriteArray("History", m_entries.size());
+ for (int i = 0; i < m_entries.size(); ++i) {
+ values.setArrayIndex(i);
+ const TypeToolTextEntry &entry = m_entries.at(i);
+ values.remove("Text");
+ values.setValue("Source", entry.source);
+ values.setValue("Format", textFormatName(entry.format));
+ values.setValue("PreserveLineBreaks", entry.preserveSourceLineBreaks);
+ values.setValue("FontFamily", entry.font.family);
+ values.setValue("FontStyle", entry.font.style);
+ values.setValue("FontSize", entry.font.size);
+ }
+ values.endArray();
+ values.sync();
+ }
+
+public:
+ TypeToolTextHistory() : m_loaded(false), m_enabled(false) {}
+
+ void load() {
+ if (m_loaded) return;
+ m_loaded = true;
+ QSettings values(typeToolSettingsPath(), QSettings::IniFormat);
+ configureSettings(values);
+ values.beginGroup("General");
+ m_enabled = values.value("EditorEnabled", false).toBool();
+ values.endGroup();
+
+ values.beginGroup("Draft");
+ m_draft.source =
+ normalizedText(values.value("Source", values.value("Text")).toString());
+ m_draft.format = textFormatFromName(
+ values.value("Format", QStringLiteral("PlainText")).toString());
+ m_draft.preserveSourceLineBreaks =
+ values.value("PreserveLineBreaks", true).toBool();
+ m_draft.font.family = values.value("FontFamily").toString();
+ m_draft.font.style = values.value("FontStyle").toString();
+ m_draft.font.size = values.value("FontSize").toString();
+ values.endGroup();
+
+ int count = values.beginReadArray("History");
+ for (int i = 0; i < count && m_entries.size() < cTextHistoryLimit; ++i) {
+ values.setArrayIndex(i);
+ TypeToolTextEntry entry;
+ entry.source = normalizedText(
+ values.value("Source", values.value("Text")).toString());
+ entry.format = textFormatFromName(
+ values.value("Format", QStringLiteral("PlainText")).toString());
+ entry.preserveSourceLineBreaks =
+ values.value("PreserveLineBreaks", true).toBool();
+ entry.font.family = values.value("FontFamily").toString();
+ entry.font.style = values.value("FontStyle").toString();
+ entry.font.size = values.value("FontSize").toString();
+ bool duplicate = false;
+ for (const TypeToolTextEntry &savedEntry : m_entries) {
+ if (sameTextEntry(savedEntry, entry)) {
+ duplicate = true;
+ break;
+ }
+ }
+ if (!entry.source.isEmpty() && !duplicate) m_entries.append(entry);
+ }
+ values.endArray();
+ }
+
+ bool isEnabled() const { return m_enabled; }
+ const TypeToolTextEntry &draft() const { return m_draft; }
+ const QList<TypeToolTextEntry> &entries() const { return m_entries; }
+
+ void setEnabled(bool enabled) {
+ load();
+ if (m_enabled == enabled) return;
+ m_enabled = enabled;
+ QSettings values(typeToolSettingsPath(), QSettings::IniFormat);
+ configureSettings(values);
+ values.beginGroup("General");
+ values.setValue("Version", 3);
+ values.setValue("HistoryLimit", cTextHistoryLimit);
+ values.setValue("EditorEnabled", m_enabled);
+ values.endGroup();
+ values.sync();
+ }
+
+ void setDraftSource(const QString &text) {
+ m_draft.source = normalizedText(text);
+ }
+
+ void setDraftFont(const TypeToolFontSettings &font) { m_draft.font = font; }
+
+ void setDraft(const TypeToolTextEntry &entry) {
+ m_draft = entry;
+ m_draft.source = normalizedText(m_draft.source);
+ }
+
+ void saveDraft() const {
+ QSettings values(typeToolSettingsPath(), QSettings::IniFormat);
+ configureSettings(values);
+ values.beginGroup("General");
+ values.setValue("Version", 3);
+ values.setValue("HistoryLimit", cTextHistoryLimit);
+ values.endGroup();
+ values.beginGroup("Draft");
+ values.remove("Text");
+ values.setValue("Source", m_draft.source);
+ values.setValue("Format", textFormatName(m_draft.format));
+ values.setValue("PreserveLineBreaks", m_draft.preserveSourceLineBreaks);
+ values.setValue("FontFamily", m_draft.font.family);
+ values.setValue("FontStyle", m_draft.font.style);
+ values.setValue("FontSize", m_draft.font.size);
+ values.endGroup();
+ values.sync();
+ }
+
+ void addEntry(const QString &sourceText) {
+ TypeToolTextEntry entry = m_draft;
+ entry.source = normalizedText(sourceText);
+ if (entry.source.isEmpty()) return;
+ for (int i = m_entries.size() - 1; i >= 0; --i) {
+ if (sameTextEntry(m_entries.at(i), entry)) m_entries.removeAt(i);
+ }
+ m_entries.prepend(entry);
+ while (m_entries.size() > cTextHistoryLimit) m_entries.removeLast();
+ saveHistory();
+ }
+
+ void removeEntry(int index) {
+ if (index < 0 || index >= m_entries.size()) return;
+ m_entries.removeAt(index);
+ saveHistory();
+ }
+
+ void clear() {
+ m_entries.clear();
+ saveHistory();
+ }
+
+ void setEntries(const QList<TypeToolTextEntry> &entries) {
+ m_entries = entries;
+ while (m_entries.size() > cTextHistoryLimit) m_entries.removeLast();
+ saveHistory();
+ }
+};
+
//! numero di pixel attorno al testo
const double cBorderSize = 15;
@@ -290,6 +564,8 @@ public:
//
//---------------------------------------------------------
+class TypeToolTextHistoryPopup;
+
class TypeTool final : public TTool {
Q_DECLARE_TR_FUNCTIONS(TypeTool)
@@ -297,6 +573,7 @@ class TypeTool final : public TTool {
TEnumProperty m_fontFamilyMenu;
TEnumProperty m_typeFaceMenu;
TBoolProperty m_vertical;
+ TBoolProperty m_textHistoryEnabled;
TEnumProperty m_size;
TPropertyGroup m_prop[2];
@@ -330,6 +607,18 @@ class TypeTool final : public TTool {
bool m_isVertical; // text orientation
TUndo *m_undo;
+ TypeToolTextHistory m_textHistory;
+ TypeToolTextHistoryPopup *m_textHistoryPopup;
+ TypeToolTextEntry m_formattedEntry;
+ OutlineLayout m_formattedLayout;
+ TVectorImageP m_formattedVector;
+ TToonzImageP m_formattedRaster;
+ TPointD m_formattedRasterOffset;
+ bool m_formattedCacheValid = false;
+ bool m_formattedVertical = false;
+ bool m_formattedIsVector = false;
+ int m_formattedStyle = -1;
+ QString m_formattedError;
public:
TypeTool();
@@ -347,6 +636,11 @@ public:
void setTypeface(std::wstring typeface);
void setSize(std::wstring size);
void setVertical(bool vertical);
+ TypeToolFontSettings currentFontSettings() const;
+ QStringList availableFontFamilies() const;
+ QStringList availableFontStyles() const;
+ QStringList availableFontSizes() const;
+ void applyFontSettings(const TypeToolFontSettings &font);
void draw() override;
void updateMouseCursor(const TPointD &pos);
@@ -382,6 +676,18 @@ public:
std::vector<const TVectorImage *> &images);
void addTextToToonzImage(const TToonzImageP &currentImage);
void stopEditing();
+ QString currentText() const;
+ void setTextFromHistory(const QString &text);
+ void syncTextHistoryDraft();
+ void syncTextHistoryFontSettings();
+ void showTextHistoryPopup();
+ void hideTextHistoryPopup();
+ bool verticalOrientation() const { return m_isVertical; }
+ bool isFormattedText() const;
+ void textHistoryEntryChanged();
+ bool updateFormattedText();
+ void drawFormattedText();
+ void addFormattedTextToToonzImage(const TToonzImageP &image);
void reset() override;
@@ -402,8 +708,520 @@ public:
}
int getColorClass() const { return 1; }
+};
+
+//---------------------------------------------------------
+
+class TypeToolTextPreview final : public QWidget {
+ Q_DECLARE_TR_FUNCTIONS(TypeToolTextPreview)
+ OutlineLayout m_layout;
+
+public:
+ explicit TypeToolTextPreview(QWidget *parent) : QWidget(parent) {}
+
+ void setEntry(const TypeToolTextEntry &entry, bool vertical) {
+ m_layout = buildOutlineLayout(entry, vertical);
+ const int width = std::min(
+ 16777215.0, std::max(200.0, std::ceil(m_layout.bounds.width()) + 24));
+ const int height = std::min(
+ 16777215.0, std::max(100.0, std::ceil(m_layout.bounds.height()) + 24));
+ setMinimumSize(width, height);
+ resize(width, height);
+ update();
+ }
+
+protected:
+ void paintEvent(QPaintEvent *) override {
+ QPainter painter(this);
+ painter.fillRect(rect(), Qt::white);
+ painter.setPen(Qt::black);
+ if (!m_layout.supported) {
+ painter.drawText(rect().adjusted(12, 12, -12, -12), Qt::TextWordWrap,
+ tr("Formatted Markdown is unavailable in this build."));
+ return;
+ }
+ painter.setRenderHint(QPainter::Antialiasing);
+ painter.setPen(Qt::NoPen);
+ painter.setBrush(Qt::black);
+ painter.translate(12 - m_layout.bounds.left(), 12 - m_layout.bounds.top());
+ for (const QPainterPath &path : m_layout.paths) painter.drawPath(path);
+ }
+};
+
+class TypeToolTextHistoryPopup final : public DVGui::Dialog {
+ Q_DECLARE_TR_FUNCTIONS(TypeToolTextHistoryPopup)
+
+ TypeTool *m_tool;
+ TypeToolTextHistory &m_history;
+ QListWidget *m_historyList;
+ QPlainTextEdit *m_editor;
+ QTabWidget *m_textTabs;
+ TypeToolTextPreview *m_preview;
+ QComboBox *m_fontFamilyCombo;
+ QComboBox *m_fontStyleCombo;
+ QComboBox *m_fontSizeCombo;
+ QComboBox *m_formatCombo;
+ QCheckBox *m_preserveLineBreaks;
+ QPushButton *m_saveEntryButton;
+ QTimer *m_saveTimer;
+ std::function<void(const QString &)> m_textChanged;
+
+ TypeToolTextEntry editorEntry() const {
+ TypeToolTextEntry entry;
+ entry.source = m_editor->toPlainText();
+ entry.format = m_formatCombo->currentIndex() == 1
+ ? TypeToolTextFormat::Markdown
+ : TypeToolTextFormat::PlainText;
+ entry.preserveSourceLineBreaks = m_preserveLineBreaks->isChecked();
+ entry.font.family = m_fontFamilyCombo->currentText();
+ entry.font.style = m_fontStyleCombo->currentText();
+ entry.font.size = m_fontSizeCombo->currentText();
+ return entry;
+ }
+
+ void refreshTextPreview() {
+ m_preview->setEntry(editorEntry(), m_tool->verticalOrientation());
+ }
+
+ void refreshFontControls() {
+ TypeToolFontSettings font = m_tool->currentFontSettings();
+ QSignalBlocker familyBlocker(m_fontFamilyCombo);
+ QSignalBlocker styleBlocker(m_fontStyleCombo);
+ QSignalBlocker sizeBlocker(m_fontSizeCombo);
+
+ m_fontFamilyCombo->clear();
+ m_fontFamilyCombo->addItems(m_tool->availableFontFamilies());
+ m_fontFamilyCombo->setCurrentText(font.family);
+
+ m_fontStyleCombo->clear();
+ m_fontStyleCombo->addItems(m_tool->availableFontStyles());
+ m_fontStyleCombo->setCurrentText(font.style);
+
+ m_fontSizeCombo->clear();
+ m_fontSizeCombo->addItems(m_tool->availableFontSizes());
+ m_fontSizeCombo->setCurrentText(font.size);
+ refreshTextPreview();
+ }
+
+ void applyFontControls(bool familyChanged = false) {
+ TypeToolFontSettings font;
+ font.family = m_fontFamilyCombo->currentText();
+ if (!familyChanged) font.style = m_fontStyleCombo->currentText();
+ font.size = m_fontSizeCombo->currentText();
+ m_tool->applyFontSettings(font);
+ refreshFontControls();
+ scheduleDraftSave();
+ }
+
+ void setEditorEntry(const TypeToolTextEntry &entry, bool notify = true) {
+ TypeToolTextEntry resolvedEntry = entry;
+ TypeToolFontSettings currentFont = m_tool->currentFontSettings();
+ if (resolvedEntry.font.family.isEmpty())
+ resolvedEntry.font.family = currentFont.family;
+ if (resolvedEntry.font.style.isEmpty())
+ resolvedEntry.font.style = currentFont.style;
+ if (resolvedEntry.font.size.isEmpty())
+ resolvedEntry.font.size = currentFont.size;
+ m_tool->applyFontSettings(resolvedEntry.font);
+
+ QSignalBlocker editorBlocker(m_editor);
+ QSignalBlocker formatBlocker(m_formatCombo);
+ QSignalBlocker lineBreakBlocker(m_preserveLineBreaks);
+ m_editor->setPlainText(resolvedEntry.source);
+ m_saveEntryButton->setEnabled(!resolvedEntry.source.isEmpty());
+ m_formatCombo->setCurrentIndex(
+ resolvedEntry.format == TypeToolTextFormat::Markdown ? 1 : 0);
+ m_preserveLineBreaks->setChecked(resolvedEntry.preserveSourceLineBreaks);
+ m_preserveLineBreaks->setEnabled(resolvedEntry.format ==
+ TypeToolTextFormat::Markdown);
+ refreshFontControls();
+ resolvedEntry.font = m_tool->currentFontSettings();
+ m_history.setDraft(resolvedEntry);
+ m_textTabs->setCurrentIndex(
+ resolvedEntry.format == TypeToolTextFormat::Markdown ? 1 : 0);
+ if (notify) {
+ m_saveTimer->start();
+ if (m_textChanged) m_textChanged(resolvedEntry.source);
+ }
+ }
+
+ void saveTextDocument() {
+ const bool markdown = editorEntry().format == TypeToolTextFormat::Markdown;
+ const QString plainFilter = tr("Plain text (*.txt)");
+ const QString markdownFilter = tr("Markdown (*.md *.markdown)");
+ QFileDialog dialog(this, tr("Save Text Source"));
+ dialog.setAcceptMode(QFileDialog::AcceptSave);
+ dialog.setFileMode(QFileDialog::AnyFile);
+ dialog.setNameFilters(QStringList() << plainFilter << markdownFilter);
+ dialog.selectNameFilter(markdown ? markdownFilter : plainFilter);
+ dialog.setDefaultSuffix(markdown ? QStringLiteral("md")
+ : QStringLiteral("txt"));
+ dialog.selectFile(markdown ? QStringLiteral("text.md")
+ : QStringLiteral("text.txt"));
+ connect(&dialog, &QFileDialog::filterSelected, &dialog,
+ [&dialog, markdownFilter](const QString &filter) {
+ dialog.setDefaultSuffix(filter == markdownFilter
+ ? QStringLiteral("md")
+ : QStringLiteral("txt"));
+ });
+ if (dialog.exec() != QDialog::Accepted || dialog.selectedFiles().isEmpty())
+ return;
+ QString error;
+ if (!saveTextSource(dialog.selectedFiles().first(), m_editor->toPlainText(),
+ error))
+ QMessageBox::warning(this, tr("Save Text Source"),
+ tr("The text could not be saved.\n%1").arg(error));
+ }
+
+ void saveCurrentTextToHistory() {
+ const TypeToolTextEntry entry = editorEntry();
+ if (entry.source.isEmpty()) return;
+ m_history.setDraft(entry);
+ m_history.addEntry(entry.source);
+ m_history.saveDraft();
+ rebuildHistoryList();
+ QSignalBlocker blocker(m_historyList);
+ m_historyList->setCurrentRow(0);
+ }
+
+ void saveTextHistory() {
+ QFileDialog dialog(this, tr("Save Text History"));
+ dialog.setAcceptMode(QFileDialog::AcceptSave);
+ dialog.setFileMode(QFileDialog::AnyFile);
+ dialog.setNameFilter(tr("Type Tool history (*.json)"));
+ dialog.setDefaultSuffix(QStringLiteral("json"));
+ dialog.selectFile(QStringLiteral("type-tool-history.json"));
+ if (dialog.exec() != QDialog::Accepted || dialog.selectedFiles().isEmpty())
+ return;
+ QString error;
+ const QString json =
+ QString::fromUtf8(serializeHistory(m_history.entries()));
+ if (!saveTextSource(dialog.selectedFiles().first(), json, error))
+ QMessageBox::warning(
+ this, tr("Save Text History"),
+ tr("The history could not be saved.\n%1").arg(error));
+ }
+
+ void loadTextHistory() {
+ const QString fileName = QFileDialog::getOpenFileName(
+ this, tr("Load Text History"), QString(),
+ tr("Type Tool history (*.json);;All files (*)"));
+ if (fileName.isEmpty()) return;
+ QFile file(fileName);
+ if (!file.open(QIODevice::ReadOnly)) {
+ QMessageBox::warning(this, tr("Load Text History"),
+ tr("The history file could not be opened.\n%1")
+ .arg(file.errorString()));
+ return;
+ }
+ QList<TypeToolTextEntry> loaded;
+ QString error;
+ if (!deserializeHistory(file.readAll(), loaded, error)) {
+ QMessageBox::warning(this, tr("Load Text History"), error);
+ return;
+ }
+ if (!m_history.entries().isEmpty()) {
+ QMessageBox question(this);
+ question.setWindowTitle(tr("Load Text History"));
+ question.setIcon(QMessageBox::Question);
+ question.setText(
+ tr("Merge the loaded entries into Recent Text, or replace the saved "
+ "listing?"));
+ question.setInformativeText(
+ tr("Merge puts loaded entries first and keeps up to %1 entries. Your "
+ "current text is kept.")
+ .arg(cTextHistoryLimit));
+ QPushButton *merge =
+ question.addButton(tr("Merge"), QMessageBox::AcceptRole);
+ QPushButton *replace =
+ question.addButton(tr("Replace"), QMessageBox::DestructiveRole);
+ question.addButton(QMessageBox::Cancel);
+ question.setDefaultButton(merge);
+ question.exec();
+ if (question.clickedButton() == merge)
+ loaded = mergeHistory(loaded, m_history.entries());
+ else if (question.clickedButton() != replace)
+ return;
+ }
+ m_history.setEntries(loaded);
+ // Loading a listing does not replace the draft or the active canvas text.
+ rebuildHistoryList(false);
+ }
+
+ void importTextDocument() {
+ QString fileName = QFileDialog::getOpenFileName(
+ this, tr("Import Text Document"), QString(),
+ tr("Text documents (*.txt *.md *.markdown);;Plain text (*.txt);;"
+ "Markdown (*.md *.markdown)"));
+ if (fileName.isEmpty()) return;
+
+ QFileInfo fileInfo(fileName);
+ if (fileInfo.size() > cTextImportWarningSize &&
+ QMessageBox::question(
+ this, tr("Import Large Text Document"),
+ tr("This file is larger than 1 MB and may be slow to convert to "
+ "Type Tool geometry. Import it anyway?")) != QMessageBox::Yes)
+ return;
+
+ QFile file(fileName);
+ if (!file.open(QIODevice::ReadOnly)) {
+ QMessageBox::warning(this, tr("Import Text Document"),
+ tr("The selected file could not be opened."));
+ return;
+ }
+
+ QByteArray bytes = file.readAll();
+ QTextCodec::ConverterState state;
+ QString source = QTextCodec::codecForName("UTF-8")->toUnicode(
+ bytes.constData(), bytes.size(), &state);
+ if (state.invalidChars > 0 &&
+ QMessageBox::question(
+ this, tr("Invalid UTF-8 Text"),
+ tr("The file contains invalid UTF-8 data. Invalid characters "
+ "will be replaced. Continue?")) != QMessageBox::Yes)
+ return;
+
+ if (!source.isEmpty() && source.at(0) == QChar::ByteOrderMark)
+ source.remove(0, 1);
+
+ TypeToolTextEntry entry;
+ entry.source = normalizedText(source);
+ QString suffix = fileInfo.suffix().toLower();
+ entry.format =
+ suffix == QStringLiteral("md") || suffix == QStringLiteral("markdown")
+ ? TypeToolTextFormat::Markdown
+ : TypeToolTextFormat::PlainText;
+ entry.preserveSourceLineBreaks = true;
+ setEditorEntry(entry);
+ if (entry.format == TypeToolTextFormat::PlainText) m_editor->setFocus();
+ }
+
+ void rebuildHistoryList(bool restoreSelection = true) {
+ int oldRow = m_historyList->currentRow();
+ QSignalBlocker blocker(m_historyList);
+ m_historyList->clear();
+ for (const TypeToolTextEntry &entry : m_history.entries()) {
+ QString label = entry.source;
+ label.replace('\n', QChar(0x21B5));
+ if (label.size() > 80) label = label.left(77) + QStringLiteral("...");
+ if (entry.format == TypeToolTextFormat::Markdown)
+ label.prepend(QStringLiteral("[MD] "));
+ QString fontLabel =
+ QStringLiteral("%1, %2, %3")
+ .arg(entry.font.family, entry.font.style, entry.font.size);
+ if (!entry.font.family.isEmpty())
+ label.prepend(QStringLiteral("[%1] ").arg(fontLabel));
+ QListWidgetItem *item = new QListWidgetItem(label, m_historyList);
+ QString toolTip = entry.source;
+ if (!entry.font.family.isEmpty())
+ toolTip += QStringLiteral("\n\n%1: %2\n%3: %4\n%5: %6")
+ .arg(tr("Font"), entry.font.family, tr("Style"),
+ entry.font.style, tr("Size"), entry.font.size);
+ item->setToolTip(toolTip);
+ }
+ if (restoreSelection && oldRow >= 0 && oldRow < m_historyList->count())
+ m_historyList->setCurrentRow(oldRow);
+ }
+
+ void scheduleDraftSave() {
+ const TypeToolTextEntry entry = editorEntry();
+ m_history.setDraft(entry);
+ refreshTextPreview();
+ m_saveTimer->start();
+ if (m_textChanged) m_textChanged(entry.source);
+ }
+
+public:
+ TypeToolTextHistoryPopup(QWidget *parent, TypeTool *tool,
+ TypeToolTextHistory &history,
+ std::function<void(const QString &)> textChanged)
+ : DVGui::Dialog(parent, false, false, QStringLiteral("TypeToolHistory"))
+ , m_tool(tool)
+ , m_history(history)
+ , m_historyList(new QListWidget(this))
+ , m_editor(new QPlainTextEdit(this))
+ , m_textTabs(new QTabWidget(this))
+ , m_preview(new TypeToolTextPreview(this))
+ , m_fontFamilyCombo(new QComboBox(this))
+ , m_fontStyleCombo(new QComboBox(this))
+ , m_fontSizeCombo(new QComboBox(this))
+ , m_formatCombo(new QComboBox(this))
+ , m_preserveLineBreaks(
+ new QCheckBox(tr("Preserve source line breaks for Markdown"), this))
+ , m_saveEntryButton(new QPushButton(tr("Save to History"), this))
+ , m_saveTimer(new QTimer(this))
+ , m_textChanged(std::move(textChanged)) {
+ setWindowTitle(tr("Type Tool Text History"));
+ setMinimumSize(520, 460);
+
+ m_historyList->setAlternatingRowColors(true);
+ m_historyList->setSelectionMode(QAbstractItemView::SingleSelection);
+ m_historyList->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
+ m_historyList->setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);
+ m_editor->setPlaceholderText(tr("Enter text to place with the Type Tool."));
+ m_fontSizeCombo->setEditable(true);
+ m_fontSizeCombo->lineEdit()->setValidator(
+ new QIntValidator(1, 1000, m_fontSizeCombo));
+ m_formatCombo->addItem(tr("Plain Text"));
+ m_formatCombo->addItem(tr("Markdown"));
+ QScrollArea *previewArea = new QScrollArea(this);
+ previewArea->setWidget(m_preview);
+ previewArea->setWidgetResizable(false);
+ m_textTabs->addTab(m_editor, tr("Source"));
+ m_textTabs->addTab(previewArea, tr("Preview"));
+ setEditorEntry(m_history.draft(), false);
+
+ QVBoxLayout *layout = new QVBoxLayout;
+ layout->addWidget(new QLabel(tr("Editable Text"), this));
+ layout->addWidget(m_textTabs, 2);
+
+ QHBoxLayout *fontLayout = new QHBoxLayout;
+ fontLayout->addWidget(new QLabel(tr("Font:"), this));
+ fontLayout->addWidget(m_fontFamilyCombo, 2);
+ fontLayout->addWidget(new QLabel(tr("Style:"), this));
+ fontLayout->addWidget(m_fontStyleCombo, 1);
+ fontLayout->addWidget(new QLabel(tr("Size:"), this));
+ fontLayout->addWidget(m_fontSizeCombo);
+ layout->addLayout(fontLayout);
+
+ QHBoxLayout *formatLayout = new QHBoxLayout;
+ formatLayout->addWidget(new QLabel(tr("Document Format:"), this));
+ formatLayout->addWidget(m_formatCombo);
+ formatLayout->addWidget(m_preserveLineBreaks);
+ formatLayout->addStretch(1);
+ layout->addLayout(formatLayout);
+ QLabel *markdownNote =
+ new QLabel(tr("Markdown is formatted in Preview and on the canvas. "
+ "Images are not embedded."),
+ this);
+ markdownNote->setWordWrap(true);
+ layout->addWidget(markdownNote);
+ layout->addWidget(
+ new QLabel(tr("Recent Text (last %1)").arg(cTextHistoryLimit), this));
+ layout->addWidget(m_historyList, 1);
+
+ QPushButton *importButton = new QPushButton(tr("Import Text..."), this);
+ QPushButton *saveTextButton = new QPushButton(tr("Save Text..."), this);
+ QPushButton *deleteButton = new QPushButton(tr("Delete Entry"), this);
+ QPushButton *clearButton = new QPushButton(tr("Clear History"), this);
+ QPushButton *closeButton = new QPushButton(tr("Close"), this);
+ QPushButton *loadHistoryButton =
+ new QPushButton(tr("Load History..."), this);
+ QPushButton *saveHistoryButton =
+ new QPushButton(tr("Save History..."), this);
+ QHBoxLayout *buttonLayout = new QHBoxLayout;
+ buttonLayout->addWidget(importButton);
+ buttonLayout->addWidget(saveTextButton);
+ buttonLayout->addWidget(m_saveEntryButton);
+ buttonLayout->addStretch(1);
+ buttonLayout->addWidget(closeButton);
+ layout->addLayout(buttonLayout);
+ QHBoxLayout *historyButtons = new QHBoxLayout;
+ historyButtons->addWidget(loadHistoryButton);
+ historyButtons->addWidget(saveHistoryButton);
+ historyButtons->addWidget(deleteButton);
+ historyButtons->addWidget(clearButton);
+ historyButtons->addStretch(1);
+ layout->addLayout(historyButtons);
+ addLayout(layout);
+
+ m_saveTimer->setSingleShot(true);
+ m_saveTimer->setInterval(300);
+
+ connect(m_saveTimer, &QTimer::timeout, this,
+ [this]() { m_history.saveDraft(); });
+ connect(m_editor, &QPlainTextEdit::textChanged, this, [this]() {
+ m_saveEntryButton->setEnabled(!m_editor->toPlainText().isEmpty());
+ scheduleDraftSave();
+ });
+ connect(
+ m_formatCombo,
+ static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged),
+ this, [this](int index) {
+ m_preserveLineBreaks->setEnabled(index == 1);
+ scheduleDraftSave();
+ });
+ connect(m_preserveLineBreaks, &QCheckBox::toggled, this,
+ [this]() { scheduleDraftSave(); });
+ connect(m_fontFamilyCombo,
+ static_cast<void (QComboBox::*)(int)>(&QComboBox::activated), this,
+ [this](int) { applyFontControls(true); });
+ connect(m_fontStyleCombo,
+ static_cast<void (QComboBox::*)(int)>(&QComboBox::activated), this,
+ [this](int) { applyFontControls(); });
+ connect(m_fontSizeCombo,
+ static_cast<void (QComboBox::*)(int)>(&QComboBox::activated), this,
+ [this](int) { applyFontControls(); });
+ connect(m_fontSizeCombo->lineEdit(), &QLineEdit::editingFinished, this,
+ [this]() { applyFontControls(); });
+ connect(m_historyList, &QListWidget::currentRowChanged, this,
+ [this](int row) {
+ if (row < 0 || row >= m_history.entries().size()) return;
+ setEditorEntry(m_history.entries().at(row));
+ });
+ connect(importButton, &QPushButton::clicked, this,
+ [this]() { importTextDocument(); });
+ connect(saveTextButton, &QPushButton::clicked, this,
+ [this]() { saveTextDocument(); });
+ connect(m_saveEntryButton, &QPushButton::clicked, this,
+ [this]() { saveCurrentTextToHistory(); });
+ connect(loadHistoryButton, &QPushButton::clicked, this,
+ [this]() { loadTextHistory(); });
+ connect(saveHistoryButton, &QPushButton::clicked, this,
+ [this]() { saveTextHistory(); });
+ connect(deleteButton, &QPushButton::clicked, this, [this]() {
+ int row = m_historyList->currentRow();
+ if (row < 0) return;
+ m_history.removeEntry(row);
+ rebuildHistoryList(false);
+ });
+ connect(clearButton, &QPushButton::clicked, this, [this]() {
+ if (QMessageBox::question(
+ this, tr("Clear Text History"),
+ tr("Remove all saved Type Tool text entries?")) !=
+ QMessageBox::Yes)
+ return;
+ m_history.clear();
+ rebuildHistoryList(false);
+ });
+ connect(closeButton, &QPushButton::clicked, this, &QWidget::hide);
+ connect(this, &DVGui::Dialog::dialogClosed, this, [this]() {
+ m_saveTimer->stop();
+ m_history.setDraft(editorEntry());
+ m_history.saveDraft();
+ });
+
+ rebuildHistoryList();
+ }
+
+ void setEditorText(const QString &text) {
+ QString normalized = normalizedText(text);
+ if (m_editor->toPlainText() == normalized) return;
+ QSignalBlocker blocker(m_editor);
+ m_editor->setPlainText(normalized);
+ m_saveEntryButton->setEnabled(!normalized.isEmpty());
+ m_history.setDraftSource(normalized);
+ refreshTextPreview();
+ m_saveTimer->start();
+ }
+
+ void syncFontControls() {
+ refreshFontControls();
+ m_history.setDraftFont(m_tool->currentFontSettings());
+ m_saveTimer->start();
+ }
+
+ void refreshHistory() { rebuildHistoryList(); }
-} typeTool;
+ void showAndRaise() {
+ refreshHistory();
+ show();
+ raise();
+ activateWindow();
+ }
+};
+
+TypeTool typeTool;
//---------------------------------------------------------
//
@@ -428,8 +1246,10 @@ TypeTool::TypeTool()
, m_fontFamilyMenu("Font:") // W_ToolOptions_FontName
, m_typeFaceMenu("Style:") // W_ToolOptions_TypeFace
, m_vertical("Vertical Orientation", false) // W_ToolOptions_Vertical
- , m_size("Size:") // W_ToolOptions_Size
- , m_undo(0) {
+ , m_textHistoryEnabled("Text History", false)
+ , m_size("Size:") // W_ToolOptions_Size
+ , m_undo(0)
+ , m_textHistoryPopup(nullptr) {
bind(TTool::VectorImage | TTool::ToonzImage | TTool::EmptyTarget);
m_prop[0].bind(m_fontFamilyMenu);
// Su mac non e' visibile il menu dello style perche' e' stato inserito nel
@@ -440,7 +1260,9 @@ TypeTool::TypeTool()
//#endif
m_prop[1].bind(m_size);
m_prop[1].bind(m_vertical);
+ m_prop[1].bind(m_textHistoryEnabled);
m_vertical.setId("Orientation");
+ m_textHistoryEnabled.setId("TextHistory");
m_fontFamilyMenu.setId("TypeFont");
m_typeFaceMenu.setId("TypeStyle");
m_size.setId("TypeSize");
@@ -452,29 +1274,247 @@ TypeTool::~TypeTool() {}
//---------------------------------------------------------
+bool TypeTool::isFormattedText() const {
+ return m_textHistoryEnabled.getValue() &&
+ m_textHistory.draft().format == TypeToolTextFormat::Markdown;
+}
+
+void TypeTool::textHistoryEntryChanged() {
+ m_formattedCacheValid = false;
+ if (m_active) {
+ updateCharPositions();
+ invalidate();
+ }
+}
+
+bool TypeTool::updateFormattedText() {
+ const TImageP target = getImage(false);
+ if (!target) return false;
+ const bool vectorTarget = !!TVectorImageP(target);
+ const double scale = vectorTarget ? Stage::inch / Stage::standardDpi : 1;
+ TypeToolTextEntry entry = m_textHistory.draft();
+ entry.source = currentText();
+ entry.font = currentFontSettings();
+ const int styleId = getApplication()->getCurrentLevelStyleIndex();
+ const bool cached =
+ m_formattedCacheValid && sameTextEntry(entry, m_formattedEntry) &&
+ m_formattedVertical == m_isVertical &&
+ m_formattedIsVector == vectorTarget && m_formattedStyle == styleId;
+ if (!cached) {
+ m_formattedEntry = entry;
+ m_formattedVertical = m_isVertical;
+ m_formattedIsVector = vectorTarget;
+ m_formattedStyle = styleId;
+ m_formattedCacheValid = true;
+ m_formattedVector = TVectorImageP();
+ m_formattedRaster = TToonzImageP();
+ m_formattedError.clear();
+ m_formattedLayout = buildOutlineLayout(entry, m_isVertical);
+ if (!m_formattedLayout.supported) {
+ m_formattedError = tr("Formatted Markdown is unavailable in this build.");
+ } else if (!m_formattedLayout.paths.isEmpty()) {
+ if (vectorTarget) {
+ m_formattedVector =
+ vectorizeOutlineLayout(m_formattedLayout, scale, styleId);
+ } else {
+ QRect bounds;
+ const QImage image = rasterizeOutlineLayout(m_formattedLayout, bounds);
+ if (image.isNull()) {
+ m_formattedError =
+ tr("The formatted text is too large for a Toonz Raster image. "
+ "Reduce its size or use a vector level.");
+ } else {
+ TRasterCM32P raster(image.width(), image.height());
+ raster->lock();
+ for (int y = 0; y < image.height(); ++y) {
+ const uchar *source = image.constScanLine(image.height() - 1 - y);
+ TPixelCM32 *pixel = raster->pixels(y);
+ for (int x = 0; x < image.width(); ++x)
+ pixel[x] = source[x] == 255 ? TPixelCM32(0, 0, 255)
+ : TPixelCM32(styleId, 0, source[x]);
+ }
+ raster->unlock();
+ m_formattedRaster = new TToonzImage(raster, raster->getBounds());
+ m_formattedRasterOffset =
+ TPointD(bounds.left(), -bounds.top() - bounds.height());
+ }
+ }
+ }
+ }
+ const QRectF box = m_formattedLayout.bounds;
+ m_textBox = TRectD(m_startPoint.x + scale * box.left(),
+ m_startPoint.y - scale * box.bottom(),
+ m_startPoint.x + scale * box.right(),
+ m_startPoint.y - scale * box.top())
+ .enlarge(cBorderSize * m_pixelSize);
+ return m_formattedError.isEmpty();
+}
+
+void TypeTool::drawFormattedText() {
+ m_pixelSize = std::sqrt(tglGetPixelSize2());
+ if (!updateFormattedText()) return;
+ const TImageP image = getImage(false);
+ TPalette *palette = image->getPalette();
+ if (!palette) return;
+ if (m_formattedVector) {
+ const TVectorRenderData data(TTranslation(m_startPoint), TRect(), palette,
+ 0, false);
+ tglDraw(data, m_formattedVector.getPointer());
+ } else if (m_formattedRaster) {
+ m_formattedRaster->setPalette(palette);
+ const TDimension size = m_formattedRaster->getSize();
+ const TTranslation position(m_startPoint + m_formattedRasterOffset +
+ TPointD(size.lx / 2, size.ly / 2));
+ GLRasterPainter::drawRaster(position, m_formattedRaster, false);
+ }
+ ToolUtils::drawRect(m_textBox, TPixel32::Black, 0x5555);
+}
+
+void TypeTool::addFormattedTextToToonzImage(const TToonzImageP &image) {
+ const TRasterCM32P target = image->getRaster();
+ const TRasterCM32P source = m_formattedRaster->getRaster();
+ const TPointD position = m_startPoint + m_formattedRasterOffset;
+ const TRectD worldBounds(position.x, position.y, position.x + source->getLx(),
+ position.y + source->getLy());
+ const TRect changedArea =
+ ToonzImageUtils::convertWorldToRaster(worldBounds, image) *
+ target->getBounds();
+ if (changedArea.isEmpty()) return;
+ TTileSetCM32 *before = new TTileSetCM32(target->getSize());
+ before->add(target, changedArea);
+ TRop::over(target, source,
+ TTranslation(position + convert(target->getCenter())));
+ TTileSetCM32 *after = new TTileSetCM32(target->getSize());
+ after->add(target, changedArea);
+ TXshSimpleLevel *level =
+ getApplication()->getCurrentLevel()->getSimpleLevel();
+ TUndoManager::manager()->add(
+ new RasterUndoTypeTool(before, after, level, getCurrentFid(),
+ m_isFrameCreated, m_isLevelCreated));
+ delete m_undo;
+ m_undo = nullptr;
+ ToolUtils::updateSaveBox();
+}
+
+//---------------------------------------------------------
+
+QString TypeTool::currentText() const {
+ QString text;
+ for (const StrokeChar &character : m_string) {
+ if (character.isReturn())
+ text.append('\n');
+ else
+ text.append(QString::fromStdWString(
+ std::wstring(1, static_cast<wchar_t>(character.m_key))));
+ }
+ return text;
+}
+
+//---------------------------------------------------------
+
+void TypeTool::setTextFromHistory(const QString &sourceText) {
+ if (!m_active || !m_validFonts || !getImage(false)) return;
+
+ QString text = normalizedText(sourceText);
+ if (currentText() == text) {
+ textHistoryEntryChanged();
+ return;
+ }
+
+ QString typeToolText = text;
+ typeToolText.replace('\n', '\r');
+ replaceText(typeToolText.toStdWString(), 0,
+ static_cast<int>(m_string.size()));
+ m_cursorIndex = static_cast<int>(m_string.size());
+ m_preeditRange = std::make_pair(m_cursorIndex, m_cursorIndex);
+ textHistoryEntryChanged();
+}
+
+//---------------------------------------------------------
+
+void TypeTool::syncTextHistoryDraft() {
+ if (!m_textHistoryEnabled.getValue()) return;
+ QString text = currentText();
+ m_textHistory.setDraftSource(text);
+ if (m_textHistoryPopup)
+ m_textHistoryPopup->setEditorText(text);
+ else
+ m_textHistory.saveDraft();
+}
+
+//---------------------------------------------------------
+
+void TypeTool::syncTextHistoryFontSettings() {
+ if (!m_textHistoryEnabled.getValue()) return;
+ m_textHistory.setDraftFont(currentFontSettings());
+ if (m_textHistoryPopup)
+ m_textHistoryPopup->syncFontControls();
+ else
+ m_textHistory.saveDraft();
+ textHistoryEntryChanged();
+}
+
+//---------------------------------------------------------
+
+void TypeTool::showTextHistoryPopup() {
+ if (!m_textHistoryEnabled.getValue()) return;
+ if (!m_textHistoryPopup) {
+ m_textHistoryPopup = new TypeToolTextHistoryPopup(
+ QApplication::activeWindow(), this, m_textHistory,
+ [this](const QString &text) { setTextFromHistory(text); });
+ QObject::connect(m_textHistoryPopup, &QObject::destroyed,
+ [this]() { m_textHistoryPopup = nullptr; });
+ }
+ m_textHistoryPopup->showAndRaise();
+}
+
+//---------------------------------------------------------
+
+void TypeTool::hideTextHistoryPopup() {
+ if (m_textHistoryPopup) m_textHistoryPopup->hide();
+}
+
+//---------------------------------------------------------
+
void TypeTool::updateTranslation() {
m_fontFamilyMenu.setQStringName(tr("Font:"));
m_typeFaceMenu.setQStringName(tr("Style:"));
m_vertical.setQStringName(tr("Vertical Orientation"));
+ m_textHistoryEnabled.setQStringName(tr("Text History"));
m_size.setQStringName(tr("Size:"));
}
//---------------------------------------------------------
bool TypeTool::onPropertyChanged(std::string propertyName) {
+ if (propertyName == m_textHistoryEnabled.getName()) {
+ m_textHistory.load();
+ bool enabled = m_textHistoryEnabled.getValue();
+ m_textHistory.setEnabled(enabled);
+ if (enabled)
+ showTextHistoryPopup();
+ else
+ hideTextHistoryPopup();
+ return true;
+ }
+
if (!m_validFonts) return false;
if (propertyName == m_fontFamilyMenu.getName()) {
setFont(m_fontFamilyMenu.getValue());
+ syncTextHistoryFontSettings();
return true;
} else if (propertyName == m_typeFaceMenu.getName()) {
setTypeface(m_typeFaceMenu.getValue());
+ syncTextHistoryFontSettings();
return true;
} else if (propertyName == m_size.getName()) {
setSize(m_size.getValue());
+ syncTextHistoryFontSettings();
return true;
} else if (propertyName == m_vertical.getName()) {
setVertical(m_vertical.getValue());
+ syncTextHistoryFontSettings();
return true;
}
return false;
@@ -486,6 +1526,9 @@ void TypeTool::init() {
if (m_initialized) return;
m_initialized = true;
+ m_textHistory.load();
+ m_textHistoryEnabled.setValue(m_textHistory.isEnabled());
+
loadFonts();
if (!m_validFonts) return;
@@ -649,6 +1692,85 @@ void TypeTool::setSize(std::wstring strSize) {
//---------------------------------------------------------
+TypeToolFontSettings TypeTool::currentFontSettings() const {
+ TypeToolFontSettings font;
+ font.family = QString::fromStdWString(m_fontFamilyMenu.getValue());
+ font.style = QString::fromStdWString(m_typeFaceMenu.getValue());
+ font.size = QString::fromStdWString(m_size.getValue());
+ return font;
+}
+
+//---------------------------------------------------------
+
+QStringList TypeTool::availableFontFamilies() const {
+ QStringList families;
+ for (const std::wstring &family : m_fontFamilyMenu.getRange())
+ families.append(QString::fromStdWString(family));
+ return families;
+}
+
+//---------------------------------------------------------
+
+QStringList TypeTool::availableFontStyles() const {
+ QStringList styles;
+ for (const std::wstring &style : m_typeFaceMenu.getRange())
+ styles.append(QString::fromStdWString(style));
+ return styles;
+}
+
+//---------------------------------------------------------
+
+QStringList TypeTool::availableFontSizes() const {
+ QStringList sizes;
+ for (const std::wstring &size : m_size.getRange())
+ sizes.append(QString::fromStdWString(size));
+ return sizes;
+}
+
+//---------------------------------------------------------
+
+void TypeTool::applyFontSettings(const TypeToolFontSettings &font) {
+ bool changed = false;
+
+ std::wstring family = font.family.toStdWString();
+ bool familyAvailable = family.empty() || m_fontFamilyMenu.isValue(family);
+ if (!family.empty() && familyAvailable) {
+ if (m_fontFamilyMenu.getValue() != family) {
+ m_fontFamilyMenu.setValue(family);
+ setFont(family);
+ changed = true;
+ }
+ }
+
+ std::wstring style = font.style.toStdWString();
+ if (familyAvailable && !style.empty() && m_typeFaceMenu.isValue(style) &&
+ m_typeFaceMenu.getValue() != style) {
+ m_typeFaceMenu.setValue(style);
+ setTypeface(style);
+ changed = true;
+ }
+
+ bool validSize = false;
+ int size = font.size.toInt(&validSize);
+ if (validSize && size > 0 && size <= 1000) {
+ std::wstring sizeValue = QString::number(size).toStdWString();
+ if (!m_size.isValue(sizeValue)) {
+ m_size.addValue(sizeValue);
+ TTool::getApplication()->getCurrentTool()->notifyToolComboBoxListChanged(
+ m_size.getName());
+ }
+ if (m_size.getValue() != sizeValue) {
+ m_size.setValue(sizeValue);
+ setSize(sizeValue);
+ changed = true;
+ }
+ }
+
+ if (changed) TTool::getApplication()->getCurrentTool()->notifyToolChanged();
+}
+
+//---------------------------------------------------------
+
void TypeTool::setVertical(bool vertical) {
if (vertical == m_isVertical) return;
@@ -667,6 +1789,9 @@ void TypeTool::setVertical(bool vertical) {
void TypeTool::stopEditing() {
if (m_active == false) return;
m_active = false;
+ m_formattedCacheValid = false;
+ m_formattedVector = TVectorImageP();
+ m_formattedRaster = TToonzImageP();
m_string.clear();
m_cursorIndex = 0;
m_textBox = TRectD();
@@ -704,6 +1829,10 @@ void TypeTool::updateStrokeChar() {
//---------------------------------------------------------
void TypeTool::updateCharPositions(int updateFrom) {
+ if (isFormattedText()) {
+ updateTextBox();
+ return;
+ }
if (updateFrom < 0) updateFrom = 0;
UINT size = m_string.size();
TPointD currentOffset;
@@ -817,6 +1946,10 @@ void TypeTool::updateCursorPoint() {
//---------------------------------------------------------
void TypeTool::updateTextBox() {
+ if (isFormattedText() && m_active) {
+ updateFormattedText();
+ return;
+ }
UINT size = m_string.size();
UINT returnNumber = 0;
double currentLineLength = 0;
@@ -886,6 +2019,10 @@ void TypeTool::updateMouseCursor(const TPointD &pos) {
void TypeTool::draw() {
if (!m_active || !getImage(false)) return;
+ if (isFormattedText()) {
+ drawFormattedText();
+ return;
+ }
TFontManager *instance = TFontManager::instance();
@@ -1078,6 +2215,7 @@ void TypeTool::addTextToImage() {
UINT size = m_string.size();
if (size == 0) return;
+ QString committedText = currentText();
TImageP img = getImage(true);
TVectorImageP vi = img;
@@ -1085,7 +2223,21 @@ void TypeTool::addTextToImage() {
if (!vi && !ti) return;
- if (vi) {
+ if (isFormattedText()) {
+ if (!updateFormattedText()) {
+ QMessageBox::warning(QApplication::activeWindow(), tr("Type Tool"),
+ m_formattedError);
+ return;
+ }
+ if (vi && m_formattedVector && m_formattedVector->getStrokeCount()) {
+ TVectorImageP placed = m_formattedVector->cloneImage();
+ placed->transform(TTranslation(m_startPoint));
+ std::vector<const TVectorImage *> images(1, placed.getPointer());
+ addTextToVectorImage(vi, images);
+ } else if (ti && m_formattedRaster) {
+ addFormattedTextToToonzImage(ti);
+ }
+ } else if (vi) {
QMutexLocker lock(vi->getMutex());
std::vector<const TVectorImage *> images;
@@ -1109,6 +2261,17 @@ void TypeTool::addTextToImage() {
notifyImageChanged();
// getApplication()->notifyImageChanges();
+ if (m_textHistoryEnabled.getValue()) {
+ m_textHistory.setDraftFont(currentFontSettings());
+ m_textHistory.addEntry(committedText);
+ m_textHistory.setDraftSource(committedText);
+ m_textHistory.saveDraft();
+ if (m_textHistoryPopup) {
+ m_textHistoryPopup->setEditorText(committedText);
+ m_textHistoryPopup->refreshHistory();
+ }
+ }
+
m_string.clear();
m_cursorIndex = 0;
m_textBox = TRectD();
@@ -1250,6 +2413,10 @@ void TypeTool::leftButtonDown(const TPointD &pos, const TMouseEvent &) {
? TRotation(m_startPoint, 90) * pos
: pos;
if (m_textBox.contains(clickPoint)) {
+ if (isFormattedText()) {
+ showTextHistoryPopup();
+ return;
+ }
setCursorIndexFromPoint(pos);
updateCursorPoint();
invalidate();
@@ -1263,6 +2430,12 @@ void TypeTool::leftButtonDown(const TPointD &pos, const TMouseEvent &) {
m_startPoint = pos;
updateTextBox();
updateCursorPoint();
+ if (m_textHistoryEnabled.getValue()) {
+ // Committing the previous block can deactivate editing via onImageChanged.
+ m_active = true;
+ showTextHistoryPopup();
+ setTextFromHistory(m_textHistory.draft().source);
+ }
updateMouseCursor(pos);
// enableShortcuts(false);
invalidate();
@@ -1557,6 +2730,8 @@ bool TypeTool::keyDown(QKeyEvent *event) {
if (!m_validFonts || !m_active) return true;
+ bool textChanged = false;
+
switch (event->key()) {
case Qt::Key_Insert:
case Qt::Key_CapsLock:
@@ -1635,6 +2810,7 @@ bool TypeTool::keyDown(QKeyEvent *event) {
case Qt::Key_Delete:
deleteKey();
+ textChanged = true;
break;
case Qt::Key_Backspace:
@@ -1642,12 +2818,14 @@ bool TypeTool::keyDown(QKeyEvent *event) {
m_cursorIndex--;
m_preeditRange = std::make_pair(m_cursorIndex, m_cursorIndex);
deleteKey();
+ textChanged = true;
}
break;
case Qt::Key_Return:
case Qt::Key_Enter:
addReturn();
+ textChanged = true;
break;
default:
@@ -1657,16 +2835,19 @@ bool TypeTool::keyDown(QKeyEvent *event) {
m_cursorIndex += unicodeChar.size();
m_preeditRange = std::make_pair(startIndex, m_cursorIndex);
updateCharPositions(startIndex - 1);
+ textChanged = true;
}
+ if (textChanged) syncTextHistoryDraft();
invalidate();
return true;
}
//-----------------------------------------------------------------------------
-void TypeTool::onInputText(const std::wstring &preedit, const std::wstring &commit,
- int replacementStart, int replacementLen) {
+void TypeTool::onInputText(const std::wstring &preedit,
+ const std::wstring &commit, int replacementStart,
+ int replacementLen) {
// butto la vecchia preedit string
m_preeditRange.first = std::max(0, m_preeditRange.first);
m_preeditRange.second = std::min((int)m_string.size(), m_preeditRange.second);
@@ -1690,6 +2871,7 @@ void TypeTool::onInputText(const std::wstring &preedit, const std::wstring &comm
// aggiorno la posizione del cursore
m_cursorIndex = m_preeditRange.second;
updateCharPositions(a);
+ syncTextHistoryDraft();
invalidate();
}
@@ -1701,6 +2883,7 @@ void TypeTool::onActivate() {
m_string.clear();
m_textBox = TRectD(0, 0, 0, 0);
m_cursorIndex = 0;
+ if (m_textHistoryEnabled.getValue()) showTextHistoryPopup();
}
//---------------------------------------------------------
@@ -1711,11 +2894,15 @@ void TypeTool::onDeactivate() {
addTextToImage(); // call internally stopEditing()
else
stopEditing();
+ hideTextHistoryPopup();
}
//---------------------------------------------------------
void TypeTool::reset() {
+ m_formattedCacheValid = false;
+ m_formattedVector = TVectorImageP();
+ m_formattedRaster = TToonzImageP();
m_string.clear();
m_textBox = TRectD(0, 0, 0, 0);
m_cursorIndex = 0;
diff --git a/toonz/sources/tnztools/typetooltext.cpp b/toonz/sources/tnztools/typetooltext.cpp
new file mode 100644
index 000000000..223f725a1
--- /dev/null
+++ b/toonz/sources/tnztools/typetooltext.cpp
@@ -0,0 +1,396 @@
+#include "typetooltext.h"
+
+#include <QAbstractTextDocumentLayout>
+#include <QFontDatabase>
+#include <QCoreApplication>
+#include <QJsonArray>
+#include <QJsonDocument>
+#include <QJsonObject>
+#include <QTextCodec>
+#include <QPaintEngine>
+#include <QPainter>
+#include <QPainterPathStroker>
+#include <QRegularExpression>
+#include <QSaveFile>
+#include <QTextBlock>
+#include <QTextCursor>
+#include <QTextDocument>
+#include <QTextLayout>
+
+#include <cmath>
+
+namespace TypeToolText {
+namespace {
+
+class OutlinePaintEngine final : public QPaintEngine {
+public:
+ QList<QPainterPath> paths;
+
+ OutlinePaintEngine() : QPaintEngine(QPaintEngine::AllFeatures) {}
+ bool begin(QPaintDevice *) override {
+ setActive(true);
+ return true;
+ }
+ bool end() override {
+ setActive(false);
+ return true;
+ }
+ Type type() const override { return User; }
+ void updateState(const QPaintEngineState &) override {}
+
+ void drawPath(const QPainterPath &path) override {
+ const QBrush brush = painter()->brush();
+ const QPen pen = painter()->pen();
+ const QTransform transform = painter()->worldTransform();
+ if (brush.style() != Qt::NoBrush && brush.color() == QColor(Qt::black))
+ paths.append(transform.map(path));
+ if (pen.style() != Qt::NoPen && pen.color() == QColor(Qt::black)) {
+ QPainterPathStroker stroker;
+ stroker.setWidth(pen.widthF() > 0 ? pen.widthF() : 1);
+ stroker.setCapStyle(pen.capStyle());
+ stroker.setJoinStyle(pen.joinStyle());
+ if (pen.style() != Qt::SolidLine)
+ stroker.setDashPattern(pen.dashPattern());
+ paths.append(transform.map(stroker.createStroke(path)));
+ }
+ }
+
+ void drawPolygon(const QPointF *points, int count,
+ PolygonDrawMode mode) override {
+ if (count < 1) return;
+ QPainterPath path(points[0]);
+ for (int i = 1; i < count; ++i) path.lineTo(points[i]);
+ if (mode != PolylineMode) path.closeSubpath();
+ path.setFillRule(mode == WindingMode ? Qt::WindingFill : Qt::OddEvenFill);
+ drawPath(path);
+ }
+
+ // QTextDocument's default text painting becomes glyph outlines through
+ // QPaintEngine::drawTextItem, including shaping and per-run font styles.
+ void drawPixmap(const QRectF &, const QPixmap &, const QRectF &) override {}
+};
+
+class OutlinePaintDevice final : public QPaintDevice {
+ mutable OutlinePaintEngine m_engine;
+
+public:
+ QPaintEngine *paintEngine() const override { return &m_engine; }
+ const QList<QPainterPath> &paths() const { return m_engine.paths; }
+
+protected:
+ int metric(PaintDeviceMetric metric) const override {
+ switch (metric) {
+ case PdmWidth:
+ case PdmHeight:
+ return 32768;
+ case PdmWidthMM:
+ case PdmHeightMM:
+ return 8670;
+ case PdmDpiX:
+ case PdmDpiY:
+ case PdmPhysicalDpiX:
+ case PdmPhysicalDpiY:
+ return 96;
+ case PdmDepth:
+ return 32;
+ case PdmNumColors:
+ return 0;
+ case PdmDevicePixelRatio:
+ return 1;
+#if QT_VERSION >= QT_VERSION_CHECK(5, 6, 0)
+ case PdmDevicePixelRatioScaled:
+ return QPaintDevice::devicePixelRatioFScale();
+#endif
+ }
+ return 0;
+ }
+};
+
+class TextDocument final : public QTextDocument {
+protected:
+ // Text placement never embeds image resources from imported documents.
+ QVariant loadResource(int, const QUrl &) override { return QVariant(); }
+};
+
+QString preserveMarkdownLineBreaks(const QString &source) {
+ QStringList lines = source.split('\n');
+ QChar fence;
+ int fenceLength = 0;
+ const QRegularExpression fencePattern(QStringLiteral("^(`{3,}|~{3,})"));
+ for (int i = 0; i + 1 < lines.size(); ++i) {
+ const QString trimmed = lines[i].trimmed();
+ const QRegularExpressionMatch match = fencePattern.match(trimmed);
+ if (match.hasMatch()) {
+ const QString marker = match.captured(1);
+ if (fence.isNull()) {
+ fence = marker[0];
+ fenceLength = marker.size();
+ } else if (marker[0] == fence && marker.size() >= fenceLength &&
+ trimmed.mid(marker.size()).trimmed().isEmpty()) {
+ fence = QChar();
+ }
+ continue;
+ }
+ if (!fence.isNull() || trimmed.isEmpty() ||
+ lines[i].startsWith(QStringLiteral(" ")) ||
+ lines[i].startsWith('\t') || lines[i].endsWith(QStringLiteral(" ")) ||
+ trimmed.endsWith('\\') || lines[i + 1].trimmed().isEmpty())
+ continue;
+ lines[i] += QStringLiteral(" ");
+ }
+ return lines.join('\n');
+}
+
+} // namespace
+
+QString textFormatName(TypeToolTextFormat format) {
+ return format == TypeToolTextFormat::Markdown ? QStringLiteral("Markdown")
+ : QStringLiteral("PlainText");
+}
+
+TypeToolTextFormat textFormatFromName(const QString &name) {
+ return name.compare(QStringLiteral("Markdown"), Qt::CaseInsensitive) == 0
+ ? TypeToolTextFormat::Markdown
+ : TypeToolTextFormat::PlainText;
+}
+
+QString normalizedText(QString text) {
+ text.replace("\r\n", "\n");
+ text.replace('\r', '\n');
+ return text;
+}
+
+bool sameTextEntry(const TypeToolTextEntry &first,
+ const TypeToolTextEntry &second) {
+ return first.source == second.source && first.format == second.format &&
+ first.preserveSourceLineBreaks == second.preserveSourceLineBreaks &&
+ first.font.family == second.font.family &&
+ first.font.style == second.font.style &&
+ first.font.size == second.font.size;
+}
+
+OutlineLayout buildOutlineLayout(const TypeToolTextEntry &entry,
+ bool vertical) {
+ OutlineLayout result;
+ TextDocument document;
+ bool validSize = false;
+ int size = entry.font.size.toInt(&validSize);
+ if (!validSize || size < 1 || size > 1000) size = 70;
+ const QFont font =
+ QFontDatabase().font(entry.font.family, entry.font.style, size);
+ document.setDefaultFont(font);
+ document.setDocumentMargin(0);
+ OutlinePaintDevice device;
+ document.documentLayout()->setPaintDevice(&device);
+ document.setUseDesignMetrics(true);
+ if (entry.format == TypeToolTextFormat::Markdown) {
+#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
+ const QString source = entry.preserveSourceLineBreaks
+ ? preserveMarkdownLineBreaks(entry.source)
+ : entry.source;
+ document.setMarkdown(source, QTextDocument::MarkdownFeatures(
+ QTextDocument::MarkdownDialectGitHub) |
+ QTextDocument::MarkdownNoHTML);
+#else
+ result.supported = false;
+ return result;
+#endif
+ } else {
+ document.setPlainText(entry.source);
+ }
+
+ // All text uses the selected palette style. Preserve font/paragraph
+ // formatting, but omit document backgrounds and link-specific colors.
+ QTextCursor cursor(&document);
+ cursor.select(QTextCursor::Document);
+ QTextCharFormat characterFormat;
+ characterFormat.setForeground(Qt::black);
+ characterFormat.setBackground(Qt::NoBrush);
+ cursor.mergeCharFormat(characterFormat);
+ QTextBlockFormat blockFormat;
+ blockFormat.setBackground(Qt::NoBrush);
+ cursor.mergeBlockFormat(blockFormat);
+ document.setTextWidth(-1);
+ document.size(); // Lay out before reading the first line's baseline.
+ const QTextBlock firstBlock = document.begin();
+ const QTextLayout *firstLayout = firstBlock.layout();
+ qreal baseline = 0;
+ if (firstLayout && firstLayout->lineCount()) {
+ const QTextLine line = firstLayout->lineAt(0);
+ baseline = document.documentLayout()->blockBoundingRect(firstBlock).top() +
+ line.y() + line.ascent();
+ }
+
+ QPainter painter(&device);
+ painter.setRenderHint(QPainter::TextAntialiasing);
+ document.drawContents(&painter);
+ painter.end();
+ QTransform transform;
+ if (vertical) transform.rotate(90);
+ transform.translate(0, -baseline);
+ for (const QPainterPath &path : device.paths()) {
+ if (path.isEmpty()) continue;
+ const QPainterPath outline = transform.map(path);
+ result.paths.append(outline);
+ result.bounds = result.bounds.united(outline.boundingRect());
+ }
+ result.plainText = document.toPlainText();
+ return result;
+}
+
+QImage rasterizeOutlineLayout(const OutlineLayout &layout, QRect &bounds) {
+ bounds = QRect();
+ if (layout.paths.isEmpty()) return QImage();
+ const QRectF box = layout.bounds.adjusted(-1, -1, 1, 1);
+ if (!std::isfinite(box.width()) || !std::isfinite(box.height()) ||
+ box.width() > 32768 || box.height() > 32768 ||
+ std::abs(box.left()) > 100000000 || std::abs(box.top()) > 100000000)
+ return QImage();
+ const QRect rasterBounds = box.toAlignedRect();
+ if (qint64(rasterBounds.width()) * rasterBounds.height() > 16 * 1024 * 1024)
+ return QImage();
+ QImage image(rasterBounds.size(), QImage::Format_Grayscale8);
+ if (image.isNull()) return image;
+ image.fill(255);
+ QPainter painter(&image);
+ painter.setRenderHint(QPainter::Antialiasing);
+ painter.setPen(Qt::NoPen);
+ painter.setBrush(Qt::black);
+ painter.translate(-rasterBounds.left(), -rasterBounds.top());
+ for (const QPainterPath &path : layout.paths) painter.drawPath(path);
+ painter.end();
+ bounds = rasterBounds;
+ return image;
+}
+
+bool saveTextSource(const QString &fileName, const QString &source,
+ QString &error) {
+ QSaveFile file(fileName);
+ const QByteArray bytes = source.toUtf8();
+ if (!file.open(QIODevice::WriteOnly) || file.write(bytes) != bytes.size() ||
+ !file.commit()) {
+ error = file.errorString();
+ return false;
+ }
+ error.clear();
+ return true;
+}
+
+QByteArray serializeHistory(const QList<TypeToolTextEntry> &entries) {
+ QJsonArray array;
+ for (const TypeToolTextEntry &entry : entries) {
+ QJsonObject font;
+ font.insert(QStringLiteral("family"), entry.font.family);
+ font.insert(QStringLiteral("style"), entry.font.style);
+ font.insert(QStringLiteral("size"), entry.font.size);
+ QJsonObject item;
+ item.insert(QStringLiteral("source"), entry.source);
+ item.insert(QStringLiteral("format"), textFormatName(entry.format));
+ item.insert(QStringLiteral("preserveLineBreaks"),
+ entry.preserveSourceLineBreaks);
+ item.insert(QStringLiteral("font"), font);
+ array.append(item);
+ }
+ QJsonObject object;
+ object.insert(QStringLiteral("format"),
+ QStringLiteral("OpenToonzTypeToolHistory"));
+ object.insert(QStringLiteral("version"), 1);
+ object.insert(QStringLiteral("entries"), array);
+ return QJsonDocument(object).toJson(QJsonDocument::Indented);
+}
+
+bool deserializeHistory(const QByteArray &bytes,
+ QList<TypeToolTextEntry> &entries, QString &error) {
+ auto invalid = [&error](const char *message) {
+ error = QCoreApplication::translate("TypeToolTextHistoryPopup", message);
+ return false;
+ };
+ QTextCodec::ConverterState state;
+ QTextCodec::codecForName("UTF-8")->toUnicode(bytes.constData(), bytes.size(),
+ &state);
+ if (state.invalidChars || state.remainingChars)
+ return invalid(
+ QT_TRANSLATE_NOOP("TypeToolTextHistoryPopup",
+ "The history file contains invalid UTF-8 text."));
+ QJsonParseError parseError;
+ const QJsonDocument document = QJsonDocument::fromJson(bytes, &parseError);
+ if (parseError.error != QJsonParseError::NoError || !document.isObject())
+ return invalid(
+ QT_TRANSLATE_NOOP("TypeToolTextHistoryPopup",
+ "The history file is not a valid JSON object."));
+ const QJsonObject object = document.object();
+ if (object.value(QStringLiteral("format")).toString() !=
+ QStringLiteral("OpenToonzTypeToolHistory") ||
+ !object.value(QStringLiteral("version")).isDouble() ||
+ object.value(QStringLiteral("version")).toDouble() != 1 ||
+ !object.value(QStringLiteral("entries")).isArray())
+ return invalid(
+ QT_TRANSLATE_NOOP("TypeToolTextHistoryPopup",
+ "The file is not a supported Type Tool history."));
+ const QJsonArray array = object.value(QStringLiteral("entries")).toArray();
+ if (array.size() > cTextHistoryLimit)
+ return invalid(QT_TRANSLATE_NOOP(
+ "TypeToolTextHistoryPopup",
+ "The history file exceeds the Recent Text entry limit."));
+ QList<TypeToolTextEntry> loaded;
+ for (const QJsonValue &value : array) {
+ if (!value.isObject())
+ return invalid(
+ QT_TRANSLATE_NOOP("TypeToolTextHistoryPopup",
+ "The history file contains an invalid entry."));
+ const QJsonObject item = value.toObject();
+ const QJsonObject font = item.value(QStringLiteral("font")).toObject();
+ const QString format = item.value(QStringLiteral("format")).toString();
+ if (!item.value(QStringLiteral("source")).isString() ||
+ item.value(QStringLiteral("source")).toString().isEmpty() ||
+ (format != QStringLiteral("PlainText") &&
+ format != QStringLiteral("Markdown")) ||
+ !item.value(QStringLiteral("preserveLineBreaks")).isBool() ||
+ !font.value(QStringLiteral("family")).isString() ||
+ !font.value(QStringLiteral("style")).isString() ||
+ !font.value(QStringLiteral("size")).isString())
+ return invalid(
+ QT_TRANSLATE_NOOP("TypeToolTextHistoryPopup",
+ "The history file contains an invalid entry."));
+ TypeToolTextEntry entry;
+ entry.source =
+ normalizedText(item.value(QStringLiteral("source")).toString());
+ entry.format = textFormatFromName(format);
+ entry.preserveSourceLineBreaks =
+ item.value(QStringLiteral("preserveLineBreaks")).toBool();
+ entry.font.family = font.value(QStringLiteral("family")).toString();
+ entry.font.style = font.value(QStringLiteral("style")).toString();
+ entry.font.size = font.value(QStringLiteral("size")).toString();
+ bool validSize = false;
+ const int size = entry.font.size.toInt(&validSize);
+ if (!entry.font.size.isEmpty() && (!validSize || size < 1 || size > 1000))
+ return invalid(
+ QT_TRANSLATE_NOOP("TypeToolTextHistoryPopup",
+ "The history file contains an invalid font size."));
+ loaded.append(entry);
+ }
+ // Publish only after every entry has passed validation.
+ entries = mergeHistory(loaded, QList<TypeToolTextEntry>());
+ error.clear();
+ return true;
+}
+
+QList<TypeToolTextEntry> mergeHistory(const QList<TypeToolTextEntry> &loaded,
+ const QList<TypeToolTextEntry> &current) {
+ QList<TypeToolTextEntry> result;
+ const QList<TypeToolTextEntry> candidates = loaded + current;
+ for (const TypeToolTextEntry &entry : candidates) {
+ bool duplicate = false;
+ for (const TypeToolTextEntry &saved : result) {
+ if (sameTextEntry(entry, saved)) {
+ duplicate = true;
+ break;
+ }
+ }
+ if (!duplicate) result.append(entry);
+ if (result.size() == cTextHistoryLimit) break;
+ }
+ return result;
+}
+
+} // namespace TypeToolText
diff --git a/toonz/sources/tnztools/typetooltext.h b/toonz/sources/tnztools/typetooltext.h
new file mode 100644
index 000000000..c5e155554
--- /dev/null
+++ b/toonz/sources/tnztools/typetooltext.h
@@ -0,0 +1,53 @@
+#pragma once
+
+#include <QImage>
+#include <QList>
+#include <QPainterPath>
+#include <QString>
+
+namespace TypeToolText {
+
+constexpr int cTextHistoryLimit = 20;
+
+enum class TypeToolTextFormat { PlainText, Markdown };
+
+struct TypeToolFontSettings final {
+ QString family;
+ QString style;
+ QString size;
+};
+
+struct TypeToolTextEntry final {
+ QString source;
+ TypeToolTextFormat format = TypeToolTextFormat::PlainText;
+ bool preserveSourceLineBreaks = true;
+ TypeToolFontSettings font;
+};
+
+QString textFormatName(TypeToolTextFormat format);
+TypeToolTextFormat textFormatFromName(const QString &name);
+QString normalizedText(QString text);
+bool sameTextEntry(const TypeToolTextEntry &first,
+ const TypeToolTextEntry &second);
+
+// Baseline-relative outlines in Qt coordinates (positive Y points down).
+// The preview and both drawing targets consume these same paths.
+struct OutlineLayout final {
+ QList<QPainterPath> paths;
+ QRectF bounds;
+ QString plainText;
+ bool supported = true;
+};
+
+OutlineLayout buildOutlineLayout(const TypeToolTextEntry &entry,
+ bool vertical = false);
+QImage rasterizeOutlineLayout(const OutlineLayout &layout, QRect &bounds);
+bool saveTextSource(const QString &fileName, const QString &source,
+ QString &error);
+QByteArray serializeHistory(const QList<TypeToolTextEntry> &entries);
+bool deserializeHistory(const QByteArray &bytes,
+ QList<TypeToolTextEntry> &entries, QString &error);
+QList<TypeToolTextEntry> mergeHistory(const QList<TypeToolTextEntry> &loaded,
+ const QList<TypeToolTextEntry> &current);
+
+} // namespace TypeToolText