最近在做文字绘制,需要用到绘制文字轮廓,发现QT里面有自己的解决方案,分享出来;
具体贴代码
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QPainter>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
//************************************
// 名 称 : GetMaximumOuterContour
// 创建日期 :2023/01/10
// 参 数 : const QPainterPath &path_ 目标Path
// 返 回 值 :QPainterPath 返回Path
//************************************
QPainterPath GetMaximumOuterContour(const QPainterPath &path_)
{
QPainterPath path;
QPainterPath* ppath = new QPainterPath();
QPainterPath path_new;
QVector<QPainterPath*> arrPath;
QVector<QPointF> list;
QPointF op;
QPointF sp, c1, c2, ep;
int cCount = 0;
bool first = true;
bool bfirstadd = true;
size_t count = path_.elementCount();
for (int i = 0; i < count; ++i) {
QPainterPath::ElementType type = path_.elementAt(i).type;
qreal x = path_.elementAt(i).x;
qreal y = path_.elementAt(i).y;
QPointF pt = QPointF(x, y);
if (first)
{
op = pt;
first = false;
}
if (type == QPainterPath::CurveToElement) {
sp = op;
c1 = pt;
cCount = 0;
}
else if (type == QPainterPath::CurveToDataElement) {
cCount++;
if (cCount == 1)
{
c2 = pt;
}
else if (cCount == 2)
{
ep = pt;
cCount = 0;
ppath->cubicTo(c1, c2, ep);
}
}
else if (type == QPainterPath::MoveToElement) {
// path_new = path_new.united(path);
// if(!bfirstadd)
{
// arrPath.push_back(ppath);
}
// ppath = new QPainterPath();
ppath->moveTo(x, y);
bfirstadd = false;
}
else if (type == QPainterPath::LineToElement) {
ppath->lineTo(x, y);
}
op = pt;
}
// path_new = path_new.united(path);
arrPath.push_back(ppath);
// auto itr = arrPath.begin();
// auto itr_end = arrPath.end();
// for(auto itr_i = itr;itr_i != itr_end;itr_i++)
// {
// path_new = path_new.united(**itr);
// }
path_new = path_new.united(*arrPath[0]);
QPainterPath path_new2 = path_new;
// path_new2 = path_new.united(*arrPath[1]);
return path_new2;
}
void MainWindow::paintEvent(QPaintEvent*)
{
QPainter painter(this);
// 创建一个QPainterPath对象
QPainterPath path;
// 设置字体
QFont font;
font.setPointSize(100);
font.setFamily("Arial");
// 设置字符
QString text = "A武汉市江夏区";
// 在QPainterPath中添加字符路径
path.addText(0, 0, font, text);
// 创建一个QPainterPathStroker对象
QPainterPathStroker stroker;
stroker.setWidth(20);
// 使用QPainterPathStroker获取字符的外轮廓
QPainterPath outline = stroker.createStroke(path);
painter.setRenderHint(QPainter::Antialiasing);
painter.setPen(Qt::black);
painter.drawPath(outline);
}
void MainWindow::on_actionAddText_triggered()
{
// 创建一个QPainterPath对象
QPainterPath path;
// 设置字体
QFont font;
font.setPointSize(300);
font.setFamily("Arial");
// 设置字符
QString text = "A武汉市犄角旮旯饕餮道魑魅魍魉\nABCDEGFHIJKLMNOPQRSTUVWXYZ 123456789~!@#$%^&*()_+|[]?';.<>";
// QString text = "A";
// 在QPainterPath中添加字符路径
path.addText(200,500, font, text);
// 创建一个QPainterPathStroker对象
QPainterPathStroker stroker;
stroker.setWidth(2);
// 使用QPainterPathStroker获取字符的外轮廓
QPainterPath outline = stroker.createStroke(path);
// 创建一个QImage对象,并设置背景为白色
QImage image(30000, 1000, QImage::Format_ARGB32);
image.fill(Qt::white);
// 在QImage上绘制外轮廓
QPainter painter(&image);
painter.setRenderHint(QPainter::Antialiasing);
painter.setPen(Qt::black);
painter.drawPath(outline);
// 保存图片
image.save("outline.png");
QPainterPath newoutline = GetMaximumOuterContour(path);
QImage image1(30000, 1000, QImage::Format_ARGB32);
image1.fill(Qt::white);
// 在QImage上绘制外轮廓
QPainter painter1(&image1);
painter1.setRenderHint(QPainter::Antialiasing);
painter1.setPen(Qt::red);
painter1.drawPath(newoutline);
// 保存图片
image1.save("newoutline.png");
}