增加复杂公式convert base64功能
This commit is contained in:
parent
1174f3ff61
commit
c6534e2cf4
4
.vscode/settings.json
vendored
4
.vscode/settings.json
vendored
@ -102,7 +102,9 @@
|
|||||||
"qtimer": "cpp",
|
"qtimer": "cpp",
|
||||||
"qstring": "cpp",
|
"qstring": "cpp",
|
||||||
"qabstracttextdocumentlayout": "cpp",
|
"qabstracttextdocumentlayout": "cpp",
|
||||||
"qframe": "cpp"
|
"qframe": "cpp",
|
||||||
|
"qpixmap": "cpp",
|
||||||
|
"qpainter": "cpp"
|
||||||
},
|
},
|
||||||
"editor.gotoLocation.alternativeDeclarationCommand": "editor.action.revealDefinition",
|
"editor.gotoLocation.alternativeDeclarationCommand": "editor.action.revealDefinition",
|
||||||
"editor.gotoLocation.alternativeDefinitionCommand": "editor.action.revealDefinition",
|
"editor.gotoLocation.alternativeDefinitionCommand": "editor.action.revealDefinition",
|
||||||
|
|||||||
2
3rdparty/MicroTeX
vendored
2
3rdparty/MicroTeX
vendored
@ -1 +1 @@
|
|||||||
Subproject commit 086f4eb740270b28bd0c61a0a359aea9300d61ae
|
Subproject commit 0e3707f6dafebb121d98b53c64364d16fefe481d
|
||||||
@ -62,13 +62,13 @@ include_directories(${JSON}/include)
|
|||||||
find_package(CURL REQUIRED)
|
find_package(CURL REQUIRED)
|
||||||
|
|
||||||
#cmark-gfm
|
#cmark-gfm
|
||||||
add_subdirectory(${PROJECT_SOURCE_DIR}/3rdparty/cmark-gfm)
|
#add_subdirectory(${PROJECT_SOURCE_DIR}/3rdparty/cmark-gfm)
|
||||||
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/cmark-gfm/src)
|
#include_directories(${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/cmark-gfm/src)
|
||||||
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/cmark-gfm/extensions)
|
#include_directories(${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/cmark-gfm/extensions)
|
||||||
|
|
||||||
#MicroTeX
|
#MicroTeX
|
||||||
add_subdirectory(${PROJECT_SOURCE_DIR}/3rdparty/MicroTeX)
|
add_subdirectory(${PROJECT_SOURCE_DIR}/3rdparty/MicroTeX)
|
||||||
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/MicroTeX/lib)
|
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/MicroTeX/src)
|
||||||
|
|
||||||
#查找QT模块
|
#查找QT模块
|
||||||
FIND_PACKAGE(Qt6 REQUIRED Core Gui Widgets)
|
FIND_PACKAGE(Qt6 REQUIRED Core Gui Widgets)
|
||||||
@ -148,9 +148,9 @@ target_link_libraries(
|
|||||||
Qt6::Gui
|
Qt6::Gui
|
||||||
Qt6::Widgets
|
Qt6::Widgets
|
||||||
CURL::libcurl
|
CURL::libcurl
|
||||||
libcmark-gfm-extensions_static
|
#libcmark-gfm-extensions_static
|
||||||
libcmark-gfm_static
|
#libcmark-gfm_static
|
||||||
microtex
|
LaTeX
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -91,6 +91,7 @@ void ctai_history_textedit::init_layout(msg_type msg_type_mode)
|
|||||||
// 5. 历史信息QTextEdit
|
// 5. 历史信息QTextEdit
|
||||||
historyLayout = new QVBoxLayout();
|
historyLayout = new QVBoxLayout();
|
||||||
m_msg_history = new QTextEdit();
|
m_msg_history = new QTextEdit();
|
||||||
|
m_math_convert=new ctai_math_convert();
|
||||||
m_msg_history->setUndoRedoEnabled(false); // 关闭撤销历史以节省内存
|
m_msg_history->setUndoRedoEnabled(false); // 关闭撤销历史以节省内存
|
||||||
m_msg_history->setAcceptRichText(true);
|
m_msg_history->setAcceptRichText(true);
|
||||||
m_msg_history->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
m_msg_history->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
||||||
@ -259,6 +260,7 @@ void ctai_history_textedit::add_user_message(const model_data &message)
|
|||||||
disp_data = QSL(message.send_user_data);
|
disp_data = QSL(message.send_user_data);
|
||||||
m_msg_sned_id = QSL(message.send_user_id);
|
m_msg_sned_id = QSL(message.send_user_id);
|
||||||
m_msg_history->setMarkdown(disp_data);
|
m_msg_history->setMarkdown(disp_data);
|
||||||
|
m_math_convert->math_convert_svg(disp_data);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ctai_history_textedit::add_system_message(const model_data &message)
|
void ctai_history_textedit::add_system_message(const model_data &message)
|
||||||
|
|||||||
@ -71,7 +71,7 @@ signals:
|
|||||||
private:
|
private:
|
||||||
void connect_signals(msg_type msg_type_mode);// 连接信号和槽
|
void connect_signals(msg_type msg_type_mode);// 连接信号和槽
|
||||||
private:
|
private:
|
||||||
ctai_math_convert* m_cmark_gfm;
|
ctai_math_convert* m_math_convert;
|
||||||
QFrame *hLine;
|
QFrame *hLine;
|
||||||
QSpacerItem *sparcer_item = new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Fixed);
|
QSpacerItem *sparcer_item = new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Fixed);
|
||||||
QVBoxLayout *mainLayout={};
|
QVBoxLayout *mainLayout={};
|
||||||
|
|||||||
@ -2,15 +2,48 @@
|
|||||||
|
|
||||||
ctai_math_convert::ctai_math_convert()
|
ctai_math_convert::ctai_math_convert()
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ctai_math_convert::~ctai_math_convert() {
|
ctai_math_convert::~ctai_math_convert()
|
||||||
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
void ctai_math_convert::math_convert_svg(const QString& text) {
|
void ctai_math_convert::set_convert_opts()
|
||||||
|
{
|
||||||
}
|
}
|
||||||
void ctai_math_convert::svg_convert_base64() {
|
void ctai_math_convert::save_svg(QPixmap &img, const QString &svg)
|
||||||
|
{
|
||||||
|
QString uid = QUuid::createUuid().toString(QUuid::WithoutBraces);
|
||||||
|
QString svgName = QDir::currentPath() + "/svg/" + uid + ".png";
|
||||||
|
bool ok = img.save(svgName);
|
||||||
|
}
|
||||||
|
QByteArray ctai_math_convert::svg_to_base64(QPixmap &pix)
|
||||||
|
{
|
||||||
|
|
||||||
|
QByteArray data;
|
||||||
|
QBuffer buffer(&data);
|
||||||
|
QImage img = pix.toImage();
|
||||||
|
|
||||||
|
img.save(&buffer, "PNG");
|
||||||
|
data = data.toBase64();
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
void ctai_math_convert::math_convert_svg(const QString &latex)
|
||||||
|
{
|
||||||
|
auto render = tex::LaTeX::parse(latex.toStdWString(), m_width, m_text_size, m_lineSpace, m_color);
|
||||||
|
qDebug() << render->getWidth() << render->getHeight();
|
||||||
|
QPixmap pix(render->getWidth(), render->getHeight());
|
||||||
|
pix.fill(Qt::white);
|
||||||
|
QPainter painter(&pix);
|
||||||
|
painter.setRenderHint(QPainter::Antialiasing, true);
|
||||||
|
tex::Graphics2D_qt g2(&painter);
|
||||||
|
render->draw(g2, 0, 0);
|
||||||
|
if (save_mode)
|
||||||
|
{
|
||||||
|
save_svg(pix, latex);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
qDebug() << "svg_to_base64:" << QString::fromUtf8(svg_to_base64(pix));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@ -1,21 +1,48 @@
|
|||||||
#ifndef CTAI_MATH_CONVERT_H
|
#ifndef CTAI_MATH_CONVERT_H
|
||||||
#define CTAI_MATH_CONVERT_H
|
#define CTAI_MATH_CONVERT_H
|
||||||
|
|
||||||
#include "microtex.h"
|
#include "latex.h"
|
||||||
|
#include "platform/qt/graphic_qt.h"
|
||||||
#include <QDebug>
|
#include <QDebug>
|
||||||
#include <QPainter>
|
#include <QPainter>
|
||||||
#include <QPixmap>
|
#include <QPixmap>
|
||||||
#include <QTimer>
|
#include <QDir>
|
||||||
|
#include <QBuffer>
|
||||||
|
#include <QUuid>
|
||||||
|
class TexGuard {
|
||||||
|
public:
|
||||||
|
TexGuard() {
|
||||||
|
tex::LaTeX::init();
|
||||||
|
}
|
||||||
|
|
||||||
class ctai_math_convert {
|
~TexGuard() {
|
||||||
|
tex::LaTeX::release();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
class ctai_math_convert :public QObject
|
||||||
|
{
|
||||||
|
Q_OBJECT
|
||||||
public:
|
public:
|
||||||
explicit ctai_math_convert();
|
explicit ctai_math_convert();
|
||||||
~ctai_math_convert();
|
~ctai_math_convert();
|
||||||
// 追加内容并处理Markdown
|
// 追加内容并处理Markdown
|
||||||
void math_convert_svg(const QString& text);
|
void math_convert_svg(const QString& text);
|
||||||
void svg_convert_base64();
|
void set_convert_opts();
|
||||||
|
void save_svg(QPixmap& img,const QString& svg);
|
||||||
|
QByteArray svg_to_base64(QPixmap& pix);
|
||||||
private:
|
private:
|
||||||
|
TexGuard texGuard;
|
||||||
|
//宽度
|
||||||
|
int m_width=600;
|
||||||
|
//文本大小
|
||||||
|
int m_text_size=20;
|
||||||
|
//行距
|
||||||
|
float m_lineSpace=20 / 3.f;
|
||||||
|
//颜色
|
||||||
|
tex::color m_color=0xff424242;
|
||||||
|
//处理后保存模式
|
||||||
|
bool save_mode=false;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
Loading…
Reference in New Issue
Block a user