From 939f00c90dfaf604a7a512024fd122835fab2927 Mon Sep 17 00:00:00 2001 From: LordMZTE Date: Sat, 27 Mar 2021 14:35:06 +0100 Subject: [PATCH] rainbow now works with unicode! --- src/Utils.cpp | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/src/Utils.cpp b/src/Utils.cpp index 1f0936f5..06df59df 100644 --- a/src/Utils.cpp +++ b/src/Utils.cpp @@ -492,7 +492,10 @@ utils::markdownToHtml(const QString &text, bool rainbowify) if (cmark_node_get_type(cur) != CMARK_NODE_TEXT) continue; // count up by length of current node's text - textLen += strlen(cmark_node_get_literal(cur)); + QTextBoundaryFinder tbf(QTextBoundaryFinder::BoundaryType::Grapheme, + QString(cmark_node_get_literal(cur))); + while (tbf.toNextBoundary() != -1) + textLen++; } // create new iter to start over @@ -510,14 +513,19 @@ utils::markdownToHtml(const QString &text, bool rainbowify) // get text in current node const char *tmp_buf = cmark_node_get_literal(cur); std::string nodeText(tmp_buf); + auto qNodeText = QString::fromStdString(nodeText); // create buffer to append rainbow text to std::string buf; int boundaryStart = 0; - int boundaryEnd = 0; - QTextBoundaryFinder tbf(QTextBoundaryFinder::BoundaryType::Grapheme, QString::fromStdString(nodeText)); + int boundaryEnd = 0; + // use QTextBoundaryFinder to iterate ofer graphemes + QTextBoundaryFinder tbf(QTextBoundaryFinder::BoundaryType::Grapheme, + qNodeText); while ((boundaryEnd = tbf.toNextBoundary()) != -1) { - nhlog::ui()->info("from {} to {}", boundaryStart, boundaryEnd); - auto curChar = nodeText.substr(boundaryStart, boundaryEnd - boundaryEnd + 1); + // Split text to get current char + auto curChar = + qNodeText.mid(boundaryStart, boundaryEnd - boundaryStart) + .toStdString(); boundaryStart = boundaryEnd; // Don't rainbowify spaces if (curChar == " ") { @@ -531,8 +539,8 @@ utils::markdownToHtml(const QString &text, bool rainbowify) auto colorString = color.name(QColor::NameFormat::HexRgb); // create HTML element for current char auto curCharColored = fmt::format("{}", - colorString.toStdString(), - curChar); + colorString.toStdString(), + curChar); // append colored HTML element to buffer buf.append(curCharColored);