Skip to content

Commit

Permalink
[*] 诗词
Browse files Browse the repository at this point in the history
  • Loading branch information
acgist committed Mar 5, 2024
1 parent 8712547 commit 8aed4de
Show file tree
Hide file tree
Showing 10 changed files with 285 additions and 8 deletions.
8 changes: 6 additions & 2 deletions boot/main/src/source/application/ImageGCWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,12 @@ void lifuren::ImageGCWindow::drawElement() {
previewBoxPtr = new Fl_Box(this->w() / 2 + 200, this->h() / 2 - 150, 400, 300, "预览图片");
previewBoxPtr->box(FL_FLAT_BOX);
// 设置:(10 + 110) * n + 40
LFR_CHOICE_BUTTON(40, 130, fasePtr, "头部", "发色", "默认");
LFR_CHOICE_BUTTON(160, 130, faxingPtr, "头部", "发型", "默认");
LFR_CHOICE_BUTTON(40, 130, fasePtr, "头部", "发色", "默认");
LFR_CHOICE_BUTTON(160, 130, faxingPtr, "头部", "发型", "默认");
LFR_CHOICE_BUTTON(280, 130, meimaoPtr, "头部", "眉毛", "默认");
LFR_CHOICE_BUTTON(400, 130, yanjingPtr, "头部", "眼睛", "默认");
LFR_CHOICE_BUTTON(520, 130, biziPtr, "头部", "鼻子", "默认");
LFR_CHOICE_BUTTON(640, 130, yachiPtr, "头部", "牙齿", "默认");
}

static void prevImage(Fl_Widget* widgetPtr, void* voidPtr) {
Expand Down
14 changes: 14 additions & 0 deletions boot/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,20 @@ target_link_libraries(
lifurencore
)

add_executable(testPoetry src/TestPoetry.cpp)
add_test(NAME testPoetry COMMAND testPoetry)
target_link_libraries(
testPoetry PUBLIC
lifurencore
)

add_executable(testCollections src/TestCollections.cpp)
add_test(NAME testCollections COMMAND testCollections)
target_link_libraries(
testCollections PUBLIC
lifurencore
)

add_executable(testFltk src/TestFltk.cpp src/source/fltk/Fltk.cpp)
add_test(NAME testFltk COMMAND testFltk)
target_link_libraries(
Expand Down
58 changes: 58 additions & 0 deletions boot/test/src/TestCollections.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#include <regex>
#include <string>
#include <vector>

#include "Logger.hpp"
#include "Collections.hpp"

int main(const int argc, const char * const argv[]) {
lifuren::logger::init();
SPDLOG_DEBUG("测试");
std::vector<std::string> vector;
SPDLOG_DEBUG("join:{}", lifuren::collections::join(vector, ","));
vector.push_back("1");
vector.push_back("2");
vector.push_back("3");
SPDLOG_DEBUG("join:{}", lifuren::collections::join(vector, ","));
std::vector<std::string> split = lifuren::collections::split("", ",");
assert(split.size() == 1);
split = lifuren::collections::split("1", ",");
assert(split.size() == 1);
split = lifuren::collections::split(",,", ",");
assert(split.size() == 3);
split = lifuren::collections::split("1,", ",");
assert(split.size() == 2);
split = lifuren::collections::split(",1", ",");
assert(split.size() == 2);
split = lifuren::collections::split(",1,", ",");
assert(split.size() == 3);
split = lifuren::collections::split("1,2", ",");
assert(split.size() == 2);
split = lifuren::collections::split(",1,2", ",");
assert(split.size() == 3);
split = lifuren::collections::split("1,2,", ",");
assert(split.size() == 3);
split = lifuren::collections::split(",1,2,", ",");
assert(split.size() == 4);
split = lifuren::collections::split("1,2", "");
assert(split.size() == 2);
split = lifuren::collections::split("", std::vector<std::string>{ "", "" });
assert(split.size() == 1);
split = lifuren::collections::split("1", std::vector<std::string>{ "", "" });
assert(split.size() == 1);
split = lifuren::collections::split(",1", std::vector<std::string>{ "", "" });
assert(split.size() == 2);
split = lifuren::collections::split("1,", std::vector<std::string>{ "", "" });
assert(split.size() == 2);
split = lifuren::collections::split(",1,", std::vector<std::string>{ "", "" });
assert(split.size() == 3);
split = lifuren::collections::split("1,2。3", std::vector<std::string>{ "", "" });
assert(split.size() == 3);
split = lifuren::collections::split("。1,2。3。", std::vector<std::string>{ "", "" });
assert(split.size() == 5);
SPDLOG_DEBUG("完成");
lifuren::logger::shutdown();
return 0;
}


47 changes: 47 additions & 0 deletions boot/test/src/TestPoetry.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include <regex>
#include <string>
#include <vector>
#include <algorithm>

#include "Logger.hpp"
#include "Strings.hpp"
#include "Collections.hpp"

#include "nlohmann/json.hpp"

int main(const int argc, const char * const argv[]) {
lifuren::logger::init();
SPDLOG_DEBUG("测试");
const std::string content = lifuren::strings::trim(R"(
庭院深深深几许,杨柳堆烟,帘幕无重数。玉勒雕鞍游冶处,楼高不见章台路。
雨横风狂三月暮,门掩黄昏,无计留春住。泪眼问花花不语,乱红飞过秋千去。
)");
int fontSize = 0;
int segmentSize = 0;
std::vector<int> segmentFontSize;
nlohmann::json json;
std::vector<std::string> vector = lifuren::collections::split(content, std::vector<std::string>{ "", "", "", "" });
std::for_each(vector.begin(), vector.end(), [&fontSize, &segmentSize, &segmentFontSize](auto& segment) {
segment = lifuren::strings::trim(segment);
if(segment.empty()) {
return;
}
SPDLOG_DEBUG("诗句:{}", segment);
int length = lifuren::strings::length(segment.c_str());
fontSize += length;
segmentSize++;
segmentFontSize.push_back(length);
});
SPDLOG_DEBUG("诗句字数:{}", fontSize);
SPDLOG_DEBUG("诗句段数:{}", segmentSize);
SPDLOG_DEBUG("逐句字数:{}", lifuren::collections::join(segmentFontSize, ","));
json["example"] = content;
json["fontSize"] = fontSize;
json["segmentSize"] = segmentSize;
json["segmentRule"] = segmentFontSize;
json["participleRule"] = std::vector<int>{ };
SPDLOG_DEBUG("配置规则:{}", json.dump(2));
SPDLOG_DEBUG("完成");
lifuren::logger::shutdown();
return 0;
}
1 change: 1 addition & 0 deletions core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ set(
${HEADER_DIR}/Utils.hpp
${HEADER_DIR}/Logger.hpp
${HEADER_DIR}/Strings.hpp
${HEADER_DIR}/Collections.hpp
# 配置
${HEADER_DIR}/Mark.hpp
${HEADER_DIR}/Label.hpp
Expand Down
24 changes: 19 additions & 5 deletions core/src/config/poetry.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
{
"诗词标题": {
"fontSize": 28,
"segmentSize": 4,
"segmentRule": [ 7, 7, 7, 7 ],
"participleRule": [ 7, 7, 7, 7 ]
"虞美人·春花秋月何时了": {
"example": "春花秋月何时了,往事知多少?小楼昨夜又东风,故国不堪回首月明中。\n雕栏玉砌应犹在,只是朱颜改。问君能有几多愁?恰似一江春水向东流。",
"fontSize": 56,
"segmentRule": [ 7, 5, 7, 9, 7, 5, 7, 9 ],
"segmentSize": 8,
"participleRule": [
2, 2, 3, 2, 3, 2, 2, 3, 2, 2, 2, 3,
2, 2, 3, 2, 3, 2, 2, 3, 2, 2, 2, 3
]
},
"蝶恋花·庭院深深深几许": {
"example": "庭院深深深几许,杨柳堆烟,帘幕无重数。玉勒雕鞍游冶处,楼高不见章台路。\n雨横风狂三月暮,门掩黄昏,无计留春住。泪眼问花花不语,乱红飞过秋千去。",
"fontSize": 60,
"segmentRule": [ 7, 4, 5, 7, 7, 7, 4, 5, 7, 7 ],
"segmentSize": 10,
"participleRule": [
2, 2, 3, 2, 2, 2, 3, 2, 2, 3, 2, 2, 3,
2, 2, 3, 2, 2, 2, 3, 2, 2, 3, 2, 2, 3
]
}
}
57 changes: 57 additions & 0 deletions core/src/header/Collections.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/**
* 集合
*
* @author acgist
*/
#pragma once

#include <map>
#include <list>
#include <vector>
#include <string>

namespace lifuren {
namespace collections {

/**
* @param collection 集合
* @param delim 分隔符号
*
* @return 拼接内容
*/
template <typename T>
std::string join(T& collection, const std::string& delim) {
std::string ret;
if(collection.empty()) {
return ret;
}
T::iterator iter = collection.begin();
const T::const_iterator end = collection.end();
const T::const_iterator last = collection.end() - 1;
for (; iter != end; ++iter) {
ret += std::to_string(*iter);
if (iter != last) {
ret += delim;
}
}
return ret;
}

/**
* @param content 文本内容
* @param delim 分隔符号
*
* @return 分割列表
*/
extern std::vector<std::string> split(const std::string& content, const std::string& delim);

/**
* @param content 文本内容
* @param multi 分隔符号
*
* @return 分割列表
*/
extern std::vector<std::string> split(const std::string& content, const std::vector<std::string>& multi);

}
}
4 changes: 3 additions & 1 deletion core/src/header/Label.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ class LabelFile : public Label {
class LabelText : public Label {

public:
// 示例
std::string example;
// 字数
int fontSize = 0;
// 段数
Expand All @@ -98,7 +100,7 @@ class LabelText : public Label {
// 分词规则
std::vector<int> participleRule;
// JSON序列化
NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_DEFAULT(LabelText, name, fontSize, segmentSize, segmentRule, participleRule);
NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_DEFAULT(LabelText, name, example, fontSize, segmentSize, segmentRule, participleRule);

public:
/**
Expand Down
34 changes: 34 additions & 0 deletions core/src/header/Strings.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
namespace lifuren {
namespace strings {

// 空白符号
const char* const EMPTY_CHARS = " \t\r\n";

/**
* 转为小写
*
Expand All @@ -25,5 +28,36 @@ extern void toLower(std::string& value);
*/
extern void toUpper(std::string& value);

/**
* @param value 字符串
*
* @return 去掉空格后的字符串
*/
inline std::string trim(const std::string& value) {
std::size_t index = value.find_first_not_of(EMPTY_CHARS);
if(index == std::string::npos) {
return std::string();
}
std::size_t jndex = value.find_last_not_of(EMPTY_CHARS);
return value.substr(index, jndex + 1 - index);
}

/**
* @param value 字符串
*
* @return 字符串长度
*/
inline size_t length(const char* value) {
size_t index = 0;
size_t jndex = 0;
while (value[index]) {
if ((value[index] & 0xC0) != 0x80) {
++jndex;
};
++index;
}
return jndex;
}

}
}
46 changes: 46 additions & 0 deletions core/src/source/Collections.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include "Collections.hpp"

std::vector<std::string> lifuren::collections::split(const std::string& content, const std::string& delim) {
std::vector<std::string> vector;
size_t pos = 0;
size_t index = 0;
while(true) {
pos = content.find(delim, index);
if(pos == std::string::npos) {
break;
}
vector.push_back(content.substr(index, pos - index));
index = pos + delim.length();
}
if(pos != index && index <= content.length()) {
vector.push_back(content.substr(index, content.length() - index));
}
return vector;
}

std::vector<std::string> lifuren::collections::split(const std::string& content, const std::vector<std::string>& multi) {
std::vector<std::string> vector;
size_t pos = 0;
size_t index = 0;
std::string delim;
while(true) {
size_t min = std::string::npos;
for(auto& value : multi) {
pos = content.find(value, index);
if(pos != std::string::npos && pos < min) {
min = pos;
delim = value;
}
}
pos = min;
if(pos == std::string::npos) {
break;
}
vector.push_back(content.substr(index, pos - index));
index = pos + delim.length();
}
if(pos != index && index <= content.length()) {
vector.push_back(content.substr(index, content.length() - index));
}
return vector;
}

0 comments on commit 8aed4de

Please sign in to comment.