2022-02-20 07:53:55 +08:00

298 lines
7.9 KiB
C++

#include "NameCreater.h"
#include <QtWidgets/QApplication>
#include <QTextCodec>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QGraphicsPixmapItem>
#include <QGraphicsTextItem>
#include <QFile>
#include <QDebug>
#include <QMessageBox>
#include <QDir>
#include <QDateTime>
#include <QFontDatabase>
#define BG_SAMPLE_PATH ":/NameCreater/resource/background_sample.jpg"
#define BG_PATH_FORMAT ":/NameCreater/resource/background_%1.jpg"
#define FONT_NAME "DFYuanStd-W8.otf"
#define FONT_SIZE 120
//#define FONT_SIZE 74
#define BG_MIX_TYPE_STR "mix"
#define BG_BOY_TYPE_STR "boy"
#define BG_GIRL_TYPE_STR "girl"
struct NameInfo_t {
QString name1;
QString name2;
QString bg_path;
};
static QPointF BG_POS[] = {
QPointF(159, 61), QPointF(482, 61), QPointF(802, 61),
QPointF(1122, 61), QPointF(1449, 61), QPointF(1772, 61),
QPointF(2095, 61), QPointF(2414, 61), QPointF(2738, 61),
QPointF(3060, 61), QPointF(900, 2094), QPointF(900, 2420),
};
static int BG_POS_COUNT = sizeof(BG_POS) / sizeof(BG_POS[0]);
static QPointF NAME_POS[] = {
QPointF(240, 430), QPointF(372, 1402),
QPointF(564, 430), QPointF(694, 1402),
QPointF(883, 430), QPointF(1014, 1402),
QPointF(1205, 430), QPointF(1337, 1402),
QPointF(1532, 430), QPointF(1663, 1402),
QPointF(1854, 430), QPointF(1987, 1402),
QPointF(2176, 430), QPointF(2309, 1402),
QPointF(2498, 430), QPointF(2629, 1402),
QPointF(2821, 430), QPointF(2952, 1402),
QPointF(3145, 430), QPointF(3276, 1402),
QPointF(1268, 2012), QPointF(2240, 1880),
QPointF(1268, 2334), QPointF(2240, 2202),
};
static int NAME_POS_COUNT = sizeof(NAME_POS) / sizeof(NAME_POS[0]);
void saveToImage(const QString& filename, QGraphicsScene* scene) {
scene->clearSelection(); // Selections would also render to the file
scene->setSceneRect(scene->itemsBoundingRect()); // Re-shrink the scene to it's bounding contents
QImage image(scene->sceneRect().size().toSize(), QImage::Format_ARGB32); // Create the image with the exact size of the shrunk scene
image.fill(Qt::transparent); // Start all pixels transparent
int dpm = 300 / 0.0254; // ~300 DPI
image.setDotsPerMeterX(dpm);
image.setDotsPerMeterY(dpm);
QPainter painter(&image);
scene->render(&painter);
#ifdef Q_OS_MAC
QString folder_name = QCoreApplication::applicationDirPath() + "/../../../export_" + QDateTime::currentDateTime().toString("yyyyMMdd");
#else
QString folder_name = QCoreApplication::applicationDirPath() + "/export_" + QDateTime::currentDateTime().toString("yyyyMMdd");
#endif
qDebug() << "folder_name :" << folder_name;
if (!QDir(folder_name).exists()) {
QDir().mkdir(folder_name);
}
image.save(QString(folder_name).append("/").append(filename), "JPG", 100);
}
QList<NameInfo_t> getNameByFile(const QString& filename) {
QList<NameInfo_t> nameList;
#ifdef Q_OS_MAC
QString root_path = QCoreApplication::applicationDirPath() + "/../../../";
#else
QString root_path = QCoreApplication::applicationDirPath() + "/";
#endif
QFile file(root_path.append(filename));
if (!file.open(QIODevice::ReadOnly)) {
QMessageBox msgBox;
msgBox.setText("Load file failed!");
msgBox.exec();
file.close();
return nameList;
}
int row = 0;
QTextCodec* tc = QTextCodec::codecForName("Big5");
while (!file.atEnd())
{
NameInfo_t info;
QByteArray line = file.readLine();
QString name_tc = tc->toUnicode(line);
QStringList csvList = name_tc.split(',');
QString name1 = csvList.at(0);
QString type_str = csvList.last();//QString::fromLocal8Bit(csvList.last());
qDebug() << "row :" << row << " " << name1;
#ifdef Q_OS_MAC
type_str = type_str.remove(QRegExp("\n"));
#else
type_str = type_str.remove(QRegExp("\r\n"));
#endif
if (name1.compare("name_1") == 0) {
continue;
}
info.name1 = name1;
info.bg_path = QString(BG_PATH_FORMAT).arg(type_str);
nameList.append(info);
row++;
}
file.close();
return nameList;
}
int main(int argc, char* argv[])
{
int export_times = 0;
QApplication a(argc, argv);
// NameCreater w;
// w.show();
#ifdef Q_OS_MAC
QString root_path = QCoreApplication::applicationDirPath() + "/../../../";
#else
QString root_path = QCoreApplication::applicationDirPath() + "/";
#endif
QString font_path = root_path.append(FONT_NAME);
QFile font_res(font_path);
if (!font_res.open(QIODevice::ReadOnly)) {
QMessageBox msgBox;
msgBox.setText("Can not load font file!" + font_path);
msgBox.exec();
font_res.close();
return 0;
}
int id = QFontDatabase::addApplicationFontFromData(font_res.readAll());
QStringList family_list = QFontDatabase::applicationFontFamilies(id);
QFont font = QFont(family_list.at(0));
font.setPixelSize(FONT_SIZE);
QColor font_color(0, 0, 0);
QList<NameInfo_t> nameList = getNameByFile("name.csv");
if (nameList.isEmpty()) {
QMessageBox msgBox;
msgBox.setText(("Name is empty !"));
msgBox.exec();
}
QGraphicsScene scene;
scene.setBackgroundBrush(QBrush(QColor(255, 255, 255)));
QGraphicsView view(&scene);
//QImage image(BG_SAMPLE_PATH);
//QGraphicsPixmapItem* bgItem = new QGraphicsPixmapItem(QPixmap::fromImage(image));
//scene.addItem(bgItem);
for (int n = 0; n < nameList.length(); n++) {
int idx = n % BG_POS_COUNT;
NameInfo_t name_info = nameList.at(n);
QImage name_image(name_info.bg_path);
if (idx == 0) {
QGraphicsRectItem* bg_item = new QGraphicsRectItem(0, 0, 3508, 2482);
bg_item->setBrush(QBrush(QColor(255, 255, 255)));
scene.addItem(bg_item);
}
QGraphicsPixmapItem* name_bgItem = new QGraphicsPixmapItem(QPixmap::fromImage(name_image));
bool rot = (idx == 10 || idx == 11);
name_bgItem->setRotation(rot ? -90 : 0);
name_bgItem->setPos(BG_POS[idx]);
scene.addItem(name_bgItem);
if (name_info.name1.isEmpty()) {
QMessageBox msgBox;
msgBox.setText(QString().asprintf("Name is Empty at %d", export_times + 1));
msgBox.exec();
}
QString name = name_info.name1;
for (int j = 0; j < 2; j++) {
int pos_idx = idx * 2 + j;
int xscale, yscale;
xscale = yscale = (j == 1) ? -1 : 1;
QPointF pos = NAME_POS[pos_idx];
QGraphicsTextItem* textItem1 = new QGraphicsTextItem(name.mid(0, 1));
textItem1->setFont(font);
textItem1->setDefaultTextColor(font_color);
if (!rot) {
textItem1->setPos(pos);
textItem1->setTransform(QTransform::fromScale(xscale, yscale));
}
else {
textItem1->setPos(pos);
textItem1->setRotation(-90);
textItem1->setTransform(QTransform::fromScale(xscale, yscale));
}
scene.addItem(textItem1);
QGraphicsTextItem* textItem2 = new QGraphicsTextItem(name.mid(1, 1));
textItem2->setFont(font);
textItem2->setDefaultTextColor(font_color);
if (!rot) {
textItem2->setPos(pos.x(), pos.y() + 145 * xscale);
textItem2->setTransform(QTransform::fromScale(xscale, yscale));
}
else {
textItem2->setPos(pos.x() + 145 * xscale, pos.y());
textItem2->setRotation(-90);
textItem2->setTransform(QTransform::fromScale(xscale, yscale));
}
scene.addItem(textItem2);
QGraphicsTextItem* textItem3 = new QGraphicsTextItem(name.mid(2, 1));
textItem3->setFont(font);
textItem3->setDefaultTextColor(font_color);
if (!rot) {
textItem3->setPos(pos.x(), pos.y() + 290 * xscale);
textItem3->setTransform(QTransform::fromScale(xscale, yscale));
}
else {
textItem3->setPos(pos.x() + 290 * xscale, pos.y());
textItem3->setRotation(-90);
textItem3->setTransform(QTransform::fromScale(xscale, yscale));
}
scene.addItem(textItem3);
}
bool is_export_page = ((idx + 1) % BG_POS_COUNT == 0);
if (is_export_page || n == nameList.count()-1) {
QString file_name = QString("%1_.jpg").arg(QString::number(export_times + 1));
saveToImage(file_name, &scene);
export_times++;
scene.clear();
}
}
QMessageBox msgBox;
msgBox.setText(QString().asprintf("Export Finshed. Count:%d", export_times));
msgBox.exec();
font_res.close();
return 0;
}