Skip to content

Commit e71e620

Browse files
committed
Fix enum conversion inside generateXML
Fix wrapMessage
1 parent ee14e29 commit e71e620

File tree

3 files changed

+26
-29
lines changed

3 files changed

+26
-29
lines changed

example/result.jpg

107 KB
Loading

example/tsoding.ini

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ textEdgeColor = #FEFEFE
2121
;Options: None, HardShadow, Bevel, GlowOutline, SoftShadow
2222
textEdgeType = SoftShadow
2323

24-
;Options: Default, Monospaced, Serif, SansSerif, Casual, Cursive, SmallCaps
25-
fontStyle = SansSerif
24+
;Options: Default, Monospaced, Proportional, MonospacedSans, ProportionalSans, Casual, Cursive, SmallCapitals
25+
fontStyle = MonospacedSans
2626

2727
;0–300 (virtual percent)
2828
fontSizePercent = 0

ytt_generator.h

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,14 @@ enum class HorizontalAlignment {
180180
Left, Center, Right
181181
};
182182
enum class FontStyle {
183-
Default, Monospaced, Serif, SansSerif, Casual, Cursive, SmallCaps
183+
Default,
184+
Monospaced, // Courier New
185+
Proportional, // Times New Roman
186+
MonospacedSans, // Lucida Console
187+
ProportionalSans, // Roboto
188+
Casual, // Comic Sans!
189+
Cursive, // Monotype Corsiva
190+
SmallCapitals // (Arial with font-variant: small-caps)
184191
};
185192
enum class EdgeType {
186193
None, HardShadow, Bevel, GlowOutline, SoftShadow
@@ -212,6 +219,11 @@ E enumFromString(const std::string &s) {
212219
}
213220
}
214221

222+
template<typename E>
223+
std::string enumToIntString(E e) {
224+
return std::to_string(magic_enum::enum_integer(e));
225+
226+
}
215227

216228
template<typename E>
217229
std::string enumOptionsComment() {
@@ -236,7 +248,7 @@ struct ChatParams {
236248
Color textEdgeColor = {254, 254, 254, 254};
237249

238250
EdgeType textEdgeType = EdgeType::SoftShadow;
239-
FontStyle fontStyle = FontStyle::SansSerif;
251+
FontStyle fontStyle = FontStyle::MonospacedSans;
240252
int fontSizePercent = 0;
241253

242254
TextAlignment textAlignment = TextAlignment::Left;
@@ -466,7 +478,10 @@ std::pair<std::string, std::vector<std::string>> wrapMessage(std::string usernam
466478
continue;
467479
}
468480
if (utf8_length(word) < availableSpace) {
469-
if (!firstWord) lines.back() += " ";
481+
if (!firstWord) {
482+
lines.back() += " ";
483+
availableSpace--;
484+
}
470485
lines.back() += word;
471486
availableSpace -= utf8_length(word);
472487
} else {
@@ -476,6 +491,9 @@ std::pair<std::string, std::vector<std::string>> wrapMessage(std::string usernam
476491
}
477492
firstWord = false;
478493
}
494+
// for (const auto& line :lines){
495+
// assert(utf8_length(line)<=maxWidth);
496+
// }
479497
return {username, lines};
480498
}
481499

@@ -536,13 +554,13 @@ std::string generateXML(const std::vector<ChatMessage> &messages, const ChatPara
536554
pen->SetAttribute("bo", std::to_string(params.textBackgroundColor.a).c_str());
537555

538556
// Set edge attributes if provided.
539-
std::string textEdgeType = enumToString(params.textEdgeType);
557+
std::string textEdgeType = enumToIntString(params.textEdgeType);
540558
if (!textEdgeType.empty()) {
541559
pen->SetAttribute("ec", static_cast<std::string>(params.textEdgeColor).c_str());
542560
pen->SetAttribute("et", textEdgeType.c_str());
543561
}
544562

545-
pen->SetAttribute("fs", enumToString(params.fontStyle).c_str());
563+
pen->SetAttribute("fs", enumToIntString(params.fontStyle).c_str());
546564
pen->SetAttribute("sz", std::to_string(params.fontSizePercent).c_str());
547565
head->InsertEndChild(pen);
548566
kv.second = std::to_string(penIndex);
@@ -552,7 +570,7 @@ std::string generateXML(const std::vector<ChatMessage> &messages, const ChatPara
552570
// Create workspace element for positioning.
553571
XMLElement *ws = doc.NewElement("ws");
554572
ws->SetAttribute("id", "1"); // default workspace id
555-
ws->SetAttribute("ju", enumToString(params.textAlignment).c_str());
573+
ws->SetAttribute("ju", enumToIntString(params.textAlignment).c_str());
556574
head->InsertEndChild(ws);
557575

558576
// Create write positioning (wp) elements.
@@ -616,27 +634,6 @@ Color getRandomColor(const std::string &username) {
616634

617635
}
618636

619-
int test() {
620-
// Example inputs.
621-
std::string username = "Alice1234";
622-
std::string separator = ": ";
623-
std::string message = "Hello this is a long message that we will try to wrap correctly even if there are verylongwordswithoutanyspaces";
624-
int maxWidth = 30;
625-
626-
auto [displayName, wrappedLines] = wrapMessage(username, separator, message, maxWidth);
627-
628-
// Print the results.
629-
std::cout << "Username: " << displayName << "\n";
630-
std::cout << "Message:\n";
631-
// Print the full first line as it would appear when concatenated.
632-
std::string fullFirstLine = displayName + wrappedLines[0];
633-
std::cout << fullFirstLine << "|" << utf8_length(fullFirstLine) << "\n";
634-
for (size_t i = 1; i < wrappedLines.size(); ++i) {
635-
std::cout << wrappedLines[i] << "|" << utf8_length(wrappedLines[i]) << "\n";
636-
}
637-
638-
return 0;
639-
}
640637

641638

642639

0 commit comments

Comments
 (0)