Skip to content

Commit

Permalink
Support combining characters.
Browse files Browse the repository at this point in the history
Modify the ftxui::Pixel. Instead of storing a wchar, store a
std::wstring. Now a single pixel can store multiple codepoints.
If a codepoint is of size <=0, it will be appended to the previous
pixel.

Only ftxui::text() is supported. ftxui::vtext support still needs to be
added.

This causes the following CPU and memory regression:
- Memory: Pixel size increases by 200% (16 byte => 48byte).
- CPU:    Draw/Second decrease by 62.5% (16k draw/s => 6k draw/s on 80x80)

Both regressions are acceptable. There are still two orders of magnitude
(100x) before the levels where performance/memory concerns begins.

This fixes: #109
  • Loading branch information
ArthurSonzogni committed Jun 25, 2021
1 parent 93922f1 commit 4a5cbf0
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 14 deletions.
9 changes: 8 additions & 1 deletion include/ftxui/screen/screen.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,16 @@ using Element = std::shared_ptr<Node>;
/// @brief A unicode character and its associated style.
/// @ingroup screen
struct Pixel {
// The graphemes stored into the pixel. To support combining characters,
// like: a⃦, this can potentially contains multiple codepoitns.
// Required: character.size() >= 1;
std::wstring character = L" ";

// Colors:
Color background_color = Color::Default;
Color foreground_color = Color::Default;
wchar_t character = U' ';

// A bit field representing the style:
bool blink : 1;
bool bold : 1;
bool dim : 1;
Expand Down
13 changes: 9 additions & 4 deletions src/ftxui/dom/text.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,15 @@ class Text : public Node {
if (y > box_.y_max)
return;
for (wchar_t c : text_) {
if (x > box_.x_max)
return;
screen.at(x, y) = c;
x += wchar_width(c);
const int width = wchar_width(c);
if (width >= 1) {
if (x > box_.x_max)
return;
screen.PixelAt(x, y).character = c;
} else {
screen.PixelAt(x - 1, y).character += c;
}
x += std::max(width, 0);
}
}

Expand Down
18 changes: 18 additions & 0 deletions src/ftxui/dom/text_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "ftxui/dom/node.hpp" // for Render
#include "ftxui/screen/box.hpp" // for ftxui
#include "ftxui/screen/screen.hpp" // for Screen
#include "ftxui/screen/string.hpp" // for to_string
#include "gtest/gtest_pred_impl.h" // for Test, EXPECT_EQ, TEST

using namespace ftxui;
Expand Down Expand Up @@ -86,6 +87,23 @@ TEST(TextTest, CJK_3) {
screen.ToString());
}

TEST(TextTest, CombiningCharacters) {
const std::wstring t =
// Combining above:
L"ā à á â ã ā a̅ ă ȧ ä ả å a̋ ǎ a̍ a̎ ȁ a̐ ȃ a̒ a̔ a̕ a̚ a̛ a̽ a̾ a̿ à á a͂ a͆ a͊ a͋ a͌ a͐ "
L"a͑ a͒ a͗ a͘ a͛ a͝ a͞ a͠ a͡ aͣ aͤ aͥ aͦ aͧ aͨ aͩ aͪ aͫ aͬ aͭ aͮ aͯ a᷀ a᷁ a᷃ a᷄ a᷅ a᷆ a᷇ a᷈ a᷉ a᷾ a⃐ a⃑ a⃔ "
L"a⃕ a⃖ a⃗ a⃛ a⃜ a⃡ a⃩ a⃰ a︠ a︡ a︢ a︣"
// Combining middle:
L"a̴ a̵ a̶ a̷ a̸ a⃒ a⃓ a⃘ a⃙ a⃚ a⃝ a⃞ a⃟ a⃥ a⃦"
// Combining below:
L"a̗ a̘ a̙ a̜ a̝ a̞ a̟ a̠ a̡ a̢ ạ ḁ a̦ a̧ ą a̩ a̪ a̫ a̬ a̭ a̮ a̯ a̰ a̱ a̲ a̳ a̹ a̺ a̻ a̼ aͅ a͇ a͈ a͉ a͍ "
L"a͎ a͓ a͔ a͕ a͖ a͙ a͚ a͜ a͟ a͢ a᷂ a᷊ a᷿ a⃨";
auto element = text(t);
Screen screen(290, 1);
Render(screen, element);
EXPECT_EQ(to_string(t), screen.ToString());
}

// Copyright 2020 Arthur Sonzogni. All rights reserved.
// Use of this source code is governed by the MIT license that can be found in
// the LICENSE file.
15 changes: 6 additions & 9 deletions src/ftxui/screen/screen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,17 +164,14 @@ std::string Screen::ToString() {
}
for (int x = 0; x < dimx_;) {
auto& pixel = pixels_[y][x];
wchar_t c = pixel.character;
UpdatePixelStyle(ss, previous_pixel, pixel);

auto width = wchar_width(c);
if (width <= 0) {
// Avoid an infinite loop for non-printable characters
c = L' ';
width = 1;
int x_inc = 0;
for (auto& c : pixel.character) {
ss << c;
x_inc += wchar_width(c);
}
ss << c;
x += width;
x += std::max(x_inc, 1);
}
}

Expand All @@ -191,7 +188,7 @@ void Screen::Print() {
/// @param x The character position along the x-axis.
/// @param y The character position along the y-axis.
wchar_t& Screen::at(int x, int y) {
return PixelAt(x, y).character;
return PixelAt(x, y).character[0];
}

/// @brief Access a Pixel at a given position.
Expand Down

0 comments on commit 4a5cbf0

Please sign in to comment.