From fbfb066f5c519204f68eae628d052b220f9749ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?shouchih=5Fchen=20=E9=99=B3=E5=AE=88=E5=BF=97?= Date: Wed, 16 Feb 2022 12:23:44 +0800 Subject: [PATCH] read local font file. --- NameCreater/NameCreater.pro.user | 305 +++++++++--------- NameCreater/NameCreater.vcxproj | 2 +- .../{resource => _exp}/DFYuanStd-W8.otf | Bin NameCreater/main.cpp | 46 ++- 4 files changed, 188 insertions(+), 165 deletions(-) rename NameCreater/{resource => _exp}/DFYuanStd-W8.otf (100%) diff --git a/NameCreater/NameCreater.pro.user b/NameCreater/NameCreater.pro.user index a0b8d15..3ccbea6 100644 --- a/NameCreater/NameCreater.pro.user +++ b/NameCreater/NameCreater.pro.user @@ -1,6 +1,6 @@ - + EnvironmentId @@ -89,6 +89,157 @@ ProjectExplorer.Project.Target.0 + + WebAssemblyDeviceType + Qt 5.15.2 WebAssembly + Qt 5.15.2 WebAssembly + qt.qt5.5152.wasm_32_kit + 1 + -1 + 0 + + 0 + D:\_0E\_Private\_Software\namecreater\build-NameCreater-Qt_5_15_2_WebAssembly-Debug + D:/_0E/_Private/_Software/namecreater/build-NameCreater-Qt_5_15_2_WebAssembly-Debug + + + true + QtProjectManager.QMakeBuildStep + false + + + + true + Qt4ProjectManager.MakeStep + + 2 + Build + Build + ProjectExplorer.BuildSteps.Build + + + + true + Qt4ProjectManager.MakeStep + clean + + 1 + Clean + Clean + ProjectExplorer.BuildSteps.Clean + + 2 + false + + false + + Debug + Qt4ProjectManager.Qt4BuildConfiguration + 2 + + + D:\_0E\_Private\_Software\namecreater\build-NameCreater-Qt_5_15_2_WebAssembly-Release + D:/_0E/_Private/_Software/namecreater/build-NameCreater-Qt_5_15_2_WebAssembly-Release + + + true + QtProjectManager.QMakeBuildStep + false + + + + true + Qt4ProjectManager.MakeStep + + 2 + Build + Build + ProjectExplorer.BuildSteps.Build + + + + true + Qt4ProjectManager.MakeStep + clean + + 1 + Clean + Clean + ProjectExplorer.BuildSteps.Clean + + 2 + false + + false + + Release + Qt4ProjectManager.Qt4BuildConfiguration + 0 + 0 + + + 0 + D:\_0E\_Private\_Software\namecreater\build-NameCreater-Qt_5_15_2_WebAssembly-Profile + D:/_0E/_Private/_Software/namecreater/build-NameCreater-Qt_5_15_2_WebAssembly-Profile + + + true + QtProjectManager.QMakeBuildStep + false + + + + true + Qt4ProjectManager.MakeStep + + 2 + Build + Build + ProjectExplorer.BuildSteps.Build + + + + true + Qt4ProjectManager.MakeStep + clean + + 1 + Clean + Clean + ProjectExplorer.BuildSteps.Clean + + 2 + false + + false + + Profile + Qt4ProjectManager.Qt4BuildConfiguration + 0 + 0 + 0 + + 3 + 0 + + true + true + true + + Launch with emrun + WebAssembly.RunConfiguration.Emrun + + false + true + false + true + chrome + + 1 + + + + ProjectExplorer.Project.Target.1 Desktop Desktop Qt 5.15.2 MinGW 32-bit @@ -247,163 +398,11 @@ true false true - D:/_0E/_Private/_Software/namecreater/build-NameCreater-Desktop_Qt_5_15_2_MinGW_32_bit-Release/release D:/_0E/_Private/_Software/namecreater/build-NameCreater-Desktop_Qt_5_15_2_MinGW_32_bit-Release 1 - - ProjectExplorer.Project.Target.1 - - WebAssemblyDeviceType - Qt 5.15.2 WebAssembly - Qt 5.15.2 WebAssembly - qt.qt5.5152.wasm_32_kit - 1 - -1 - 0 - - 0 - D:\_0E\_Private\_Software\namecreater\build-NameCreater-Qt_5_15_2_WebAssembly-Debug - D:/_0E/_Private/_Software/namecreater/build-NameCreater-Qt_5_15_2_WebAssembly-Debug - - - true - QtProjectManager.QMakeBuildStep - false - - - - true - Qt4ProjectManager.MakeStep - - 2 - Build - Build - ProjectExplorer.BuildSteps.Build - - - - true - Qt4ProjectManager.MakeStep - clean - - 1 - Clean - Clean - ProjectExplorer.BuildSteps.Clean - - 2 - false - - false - - Debug - Qt4ProjectManager.Qt4BuildConfiguration - 2 - - - D:\_0E\_Private\_Software\namecreater\build-NameCreater-Qt_5_15_2_WebAssembly-Release - D:/_0E/_Private/_Software/namecreater/build-NameCreater-Qt_5_15_2_WebAssembly-Release - - - true - QtProjectManager.QMakeBuildStep - false - - - - true - Qt4ProjectManager.MakeStep - - 2 - Build - Build - ProjectExplorer.BuildSteps.Build - - - - true - Qt4ProjectManager.MakeStep - clean - - 1 - Clean - Clean - ProjectExplorer.BuildSteps.Clean - - 2 - false - - false - - Release - Qt4ProjectManager.Qt4BuildConfiguration - 0 - 0 - - - 0 - D:\_0E\_Private\_Software\namecreater\build-NameCreater-Qt_5_15_2_WebAssembly-Profile - D:/_0E/_Private/_Software/namecreater/build-NameCreater-Qt_5_15_2_WebAssembly-Profile - - - true - QtProjectManager.QMakeBuildStep - false - - - - true - Qt4ProjectManager.MakeStep - - 2 - Build - Build - ProjectExplorer.BuildSteps.Build - - - - true - Qt4ProjectManager.MakeStep - clean - - 1 - Clean - Clean - ProjectExplorer.BuildSteps.Clean - - 2 - false - - false - - Profile - Qt4ProjectManager.Qt4BuildConfiguration - 0 - 0 - 0 - - 3 - 0 - - true - true - true - - Launch with emrun - WebAssembly.RunConfiguration.Emrun - - false - true - false - true - chrome - - 1 - - ProjectExplorer.Project.TargetCount 2 diff --git a/NameCreater/NameCreater.vcxproj b/NameCreater/NameCreater.vcxproj index 8989da9..55f471d 100644 --- a/NameCreater/NameCreater.vcxproj +++ b/NameCreater/NameCreater.vcxproj @@ -64,7 +64,7 @@ - xcopy /y "$(ProjectDir)_exp\*.csv" "$(TargetDir)" + xcopy /y "$(ProjectDir)_exp\*" "$(TargetDir)" diff --git a/NameCreater/resource/DFYuanStd-W8.otf b/NameCreater/_exp/DFYuanStd-W8.otf similarity index 100% rename from NameCreater/resource/DFYuanStd-W8.otf rename to NameCreater/_exp/DFYuanStd-W8.otf diff --git a/NameCreater/main.cpp b/NameCreater/main.cpp index 8415f72..c039d43 100644 --- a/NameCreater/main.cpp +++ b/NameCreater/main.cpp @@ -1,6 +1,4 @@ #include "NameCreater.h" -#include - #include #include #include @@ -12,11 +10,13 @@ #include #include #include +#include +#include #define BG_MIX_PATH ":/NameCreater/resource/background.jpg" #define BG_BOY_PATH ":/NameCreater/resource/background_boy.jpg" #define BG_GIRL_PATH ":/NameCreater/resource/background_girl.jpg" -#define FONT_NAME QStringLiteral("DFYuan Std W8") +#define FONT_NAME "DFYuanStd-W8.otf" #define FONT_SIZE 120 //#define FONT_SIZE 74 @@ -52,6 +52,7 @@ static QPointF GIRL_POS[] = { static int GIRL_POS_COUNT = sizeof(GIRL_POS) / sizeof(GIRL_POS[0]); + void saveToImage(const QString& filename, QGraphicsScene* scene) { scene->clearSelection(); // Selections would also render to the file @@ -66,9 +67,13 @@ void saveToImage(const QString& filename, QGraphicsScene* scene) { 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 - QString folder_name = "export_" + QDateTime::currentDateTime().toString("yyyyMMdd"); - + qDebug() << "folder_name :" << folder_name; if (!QDir(folder_name).exists()) { QDir().mkdir(folder_name); } @@ -79,23 +84,34 @@ void saveToImage(const QString& filename, QGraphicsScene* scene) { QList getNameByFile(const QString& filename) { QList nameList; - QFile file(filename); + +#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)) { qDebug() << "Load file failed!"; return nameList; } int row = 0; + QTextCodec *tc = QTextCodec::codecForName("Big5"); while (!file.atEnd()) { NameInfo_t info; QByteArray line = file.readLine(); - QByteArrayList csvList = line.split(','); + QString name_tc = tc->toUnicode(line); + QStringList csvList = name_tc.split(','); - QString name1 = QString::fromLocal8Bit(csvList.at(0)); - QString name2 = QString::fromLocal8Bit(csvList.at(1)); - QString type_str = QString::fromLocal8Bit(csvList.last()); + QString name1 = csvList.at(0); //QString::fromLocal8Bit(csvList.at(0)); + QString name2 = csvList.at(1);//QString::fromLocal8Bit(csvList.at(1)); + QString type_str = csvList.last();//QString::fromLocal8Bit(csvList.last()); qDebug() << "row :" << row << " " << name1 << " " << name2; if (name1.compare("name_1") == 0 || name2.compare("name_2") == 0) { @@ -123,8 +139,16 @@ int main(int argc, char* argv[]) QApplication a(argc, argv); // NameCreater w; // w.show(); +#ifdef Q_OS_MAC + QString root_path = QCoreApplication::applicationDirPath() + "/../../../"; +#else + QString root_path = QCoreApplication::applicationDirPath() + "/"; +#endif - QFont font(FONT_NAME); + QString font_path = root_path.append(FONT_NAME); + int id = QFontDatabase::addApplicationFont(font_path); + QStringList family_list = QFontDatabase::applicationFontFamilies(id); + QFont font = QFont(family_list.at(0)); font.setPixelSize(FONT_SIZE); QColor font_color(0, 0, 0);