Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added base hex color parser, and md statement #440

Merged
merged 5 commits into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions doc/en/text-styles.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,34 @@ Styles can be combined. Example:
Output:

***<ins>Message</ins>*** using *~~combed~~ combined* styles<ins>~~.~~</ins>

# Colors

Text color can be set using a color code: [#RRGGBB]


| Component | Purpose |
| --------- | --------------------------------- |
| R | Represents the intensity of red |
| G | Represents the intensity of green |
| B | Represents the intensity of blue |

### Example:

<span style="color: #ff0000">
<span style="color:rgb(105, 105, 105)">
[#ff0000]
</span>Red Text
</span>

<span style="color: #00ff00">
<span style="color:rgb(105, 105, 105)">
[#00ff00]
</span>Green Text
</span>

<span style="color: #0000ff">
<span style="color:rgb(105, 105, 105)">
[#0000ff]
</span>Blue Text
</span>
30 changes: 30 additions & 0 deletions doc/ru/text-styles.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,33 @@
Вывод:

***<ins>Сообщение</ins>***, демонстрирующее *~~обедненные~~ объединенные* стили<ins>~~.~~</ins>

## Цвета

Цвет текста задается при помощи цветового кода: [#RRGGBB]

| Компонент | Назначение |
| ------------ | ------------------------- |
| R | Используется для интенсивности красного |
| G | Используется для интенсивности зеленого |
| B | Используется для интенсивности синего |

### Например:

<span style="color: #ff0000">
<span style="color:rgb(105, 105, 105)">
[#ff0000]
</span>Красный Текст
</span>

<span style="color: #00ff00">
<span style="color:rgb(105, 105, 105)">
[#00ff00]
</span>Зеленый Текст
</span>

<span style="color: #0000ff">
<span style="color:rgb(105, 105, 105)">
[#0000ff]
</span>Синий Текст
</span>
83 changes: 71 additions & 12 deletions src/graphics/ui/markdown.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "markdown.hpp"

#include "coders/commons.hpp"
#include "graphics/core/Font.hpp"

using namespace markdown;
Expand All @@ -9,7 +10,7 @@ static inline void emit(
CharT c, FontStylesScheme& styles, std::basic_stringstream<CharT>& ss
) {
ss << c;
styles.map.emplace_back(styles.palette.size()-1);
styles.map.emplace_back(styles.palette.size() - 1);
}

template <typename CharT>
Expand All @@ -20,6 +21,38 @@ static inline void emit_md(
styles.map.emplace_back(0);
}

template <typename CharT>
static glm::vec4 parse_color(const std::basic_string_view<CharT>& color_code) {
if (color_code.size() != 8 || color_code[0] != '#') {
return glm::vec4(1, 1, 1, 1); // default to white
}

auto hex_to_float = [](char high, char low) {
int high_val = hexchar2int(high);
int low_val = hexchar2int(low);
if (high_val == -1 || low_val == -1) {
return 1.0f; // default to max value on error
}
return (high_val * 16 + low_val) / 255.0f;
};

return glm::vec4(
hex_to_float(color_code[1], color_code[2]),
hex_to_float(color_code[3], color_code[4]),
hex_to_float(color_code[5], color_code[6]),
1
);
}

template <typename CharT>
static inline void apply_color(
const std::basic_string_view<CharT>& color_code, FontStylesScheme& styles
) {
FontStyle style = styles.palette.back();
style.color = parse_color(color_code);
styles.palette.push_back(style);
}

template <typename CharT>
static inline void restyle(
CharT c,
Expand All @@ -43,13 +76,14 @@ Result<CharT> process_markdown(
std::basic_stringstream<CharT> ss;
FontStylesScheme styles {
// markdown default
{{false, false, false, false, glm::vec4(1,1,1,0.5f)}, {}},
{{false, false, false, false, glm::vec4(1, 1, 1, 0.5f)}, {}},
{}
};
FontStyle style;
int pos = 0;
while (pos < source.size()) {
CharT first = source[pos];

if (first == '\\') {
if (pos + 1 < source.size()) {
CharT second = source[++pos];
Expand All @@ -63,32 +97,55 @@ Result<CharT> process_markdown(
emit(second, styles, ss);
pos++;
continue;
case '[':
if (pos + 9 < source.size() && source[pos + 1] == '#' &&
source[pos + 8] == ']') {
if (!eraseMarkdown) {
emit_md(source[pos - 1], styles, ss);
}
for (int i = 0; i < 10; ++i) {

emit(source[pos + i], styles, ss);
}

pos += 10;
continue;
}
}
pos--;
}
} else if (first == '[' && pos + 9 <= source.size() && source[pos + 1] == '#' && source[pos + 8] == ']') {
std::basic_string_view<CharT> color_code = source.substr(pos + 1, 8);
apply_color(color_code, styles);
if (!eraseMarkdown) {
for (int i = 0; i < 9; ++i) {
emit_md(source[pos + i], styles, ss);
}
}
pos += 9; // Skip past the color code
continue;
} else if (first == '*') {
if (pos + 1 < source.size() && source[pos+1] == '*') {
if (pos + 1 < source.size() && source[pos + 1] == '*') {
pos++;
if (!eraseMarkdown)
emit_md(first, styles, ss);
if (!eraseMarkdown) emit_md(first, styles, ss);
style.bold = !style.bold;
restyle(first, style, styles, ss, pos, eraseMarkdown);
continue;
}
style.italic = !style.italic;
restyle(first, style, styles, ss, pos, eraseMarkdown);
continue;
} else if (first == '_' && pos + 1 < source.size() && source[pos+1] == '_') {
} else if (first == '_' && pos + 1 < source.size() &&
source[pos + 1] == '_') {
pos++;
if (!eraseMarkdown)
emit_md(first, styles, ss);
if (!eraseMarkdown) emit_md(first, styles, ss);
style.underline = !style.underline;
restyle(first, style, styles, ss, pos, eraseMarkdown);
continue;
} else if (first == '~' && pos + 1 < source.size() && source[pos+1] == '~') {
} else if (first == '~' && pos + 1 < source.size() &&
source[pos + 1] == '~') {
pos++;
if (!eraseMarkdown)
emit_md(first, styles, ss);
if (!eraseMarkdown) emit_md(first, styles, ss);
style.strikethrough = !style.strikethrough;
restyle(first, style, styles, ss, pos, eraseMarkdown);
continue;
Expand All @@ -106,6 +163,8 @@ Result<char> markdown::process(std::string_view source, bool eraseMarkdown) {
return process_markdown(source, eraseMarkdown);
}

Result<wchar_t> markdown::process(std::wstring_view source, bool eraseMarkdown) {
Result<wchar_t> markdown::process(
std::wstring_view source, bool eraseMarkdown
) {
return process_markdown(source, eraseMarkdown);
}
Loading