46uint8_t g_map_braille[2][4][2] = {
48 {0b00000000, 0b00000001},
49 {0b00000000, 0b00000010},
50 {0b00000000, 0b00000100},
51 {0b00000001, 0b00000000},
54 {0b00000000, 0b00001000},
55 {0b00000000, 0b00010000},
56 {0b00000000, 0b00100000},
57 {0b00000010, 0b00000000},
62std::vector<std::string> g_map_block = {
63 " ",
"▘",
"▖",
"▌",
"▝",
"▀",
"▞",
"▛",
64 "▗",
"▚",
"▄",
"▙",
"▐",
"▜",
"▟",
"█",
68const std::map<std::string, uint8_t> g_map_block_inversed = {
69 {
" ", 0b0000}, {
"▘", 0b0001}, {
"▖", 0b0010}, {
"▌", 0b0011},
70 {
"▝", 0b0100}, {
"▀", 0b0101}, {
"▞", 0b0110}, {
"▛", 0b0111},
71 {
"▗", 0b1000}, {
"▚", 0b1001}, {
"▄", 0b1010}, {
"▙", 0b1011},
72 {
"▐", 0b1100}, {
"▜", 0b1101}, {
"▟", 0b1110}, {
"█", 0b1111},
75constexpr auto nostyle = [](Pixel& ) {};
85 storage_(width_ * height_ / 8 ) {}
91 auto it = storage_.find(XY{x, y});
92 return (it == storage_.end()) ?
Pixel{} : it->second.content;
133 Cell& cell = storage_[XY{x / 2, y / 4}];
134 if (cell.type != CellType::kBraille) {
136 cell.type = CellType::kBraille;
139 cell.content.
character[1] |= g_map_braille[x % 2][y % 4][0];
140 cell.content.
character[2] |= g_map_braille[x % 2][y % 4][1];
150 Cell& cell = storage_[XY{x / 2, y / 4}];
151 if (cell.type != CellType::kBraille) {
153 cell.type = CellType::kBraille;
156 cell.content.
character[1] &= ~(g_map_braille[x % 2][y % 4][0]);
157 cell.content.
character[2] &= ~(g_map_braille[x % 2][y % 4][1]);
168 Cell& cell = storage_[XY{x / 2, y / 4}];
169 if (cell.type != CellType::kBraille) {
171 cell.type = CellType::kBraille;
174 cell.content.
character[1] ^= g_map_braille[x % 2][y % 4][0];
175 cell.content.
character[2] ^= g_map_braille[x % 2][y % 4][1];
209 const int dx = std::abs(x2 - x1);
210 const int dy = std::abs(y2 - y1);
211 const int sx = x1 < x2 ? 1 : -1;
212 const int sy = y1 < y2 ? 1 : -1;
213 const int length = std::max(dx, dy);
215 if (!IsIn(x1, y1) && !IsIn(x2, y2)) {
218 if (dx + dx > width_ * height_) {
223 for (
int i = 0; i < length; ++i) {
225 if (2 * error >= -dy) {
229 if (2 * error <= dx) {
335 int dx = (1 + 2 * x) * e2 * e2;
347 err += dx += 2 * r2 * r2;
351 err += dy += 2 * r1 * r1;
399 int dx = (1 + 2 * x) * e2 * e2;
404 for (
int xx = x1 + x; xx <= x1 - x; ++xx) {
411 err += dx += 2 * (long)r2 * r2;
415 err += dy += 2 * (long)r1 * r1;
420 for (
int yy = y1 - y; yy <= y1 + y; ++yy) {
465 Cell& cell = storage_[XY{x / 2, y / 2}];
466 if (cell.type != CellType::kBlock) {
468 cell.type = CellType::kBlock;
471 uint8_t bit = (x % 2) * 2 + y % 2;
472 uint8_t value = g_map_block_inversed.at(cell.content.
character);
474 cell.content.
character = g_map_block[value];
484 Cell& cell = storage_[XY{x / 2, y / 4}];
485 if (cell.type != CellType::kBlock) {
487 cell.type = CellType::kBlock;
491 uint8_t bit = (y % 2) * 2 + x % 2;
492 uint8_t value = g_map_block_inversed.at(cell.content.
character);
493 value &= ~(1U << bit);
494 cell.content.
character = g_map_block[value];
505 Cell& cell = storage_[XY{x / 2, y / 4}];
506 if (cell.type != CellType::kBlock) {
508 cell.type = CellType::kBlock;
512 uint8_t bit = (y % 2) * 2 + x % 2;
513 uint8_t value = g_map_block_inversed.at(cell.content.
character);
515 cell.content.
character = g_map_block[value];
552 const int dx = std::abs(x2 - x1);
553 const int dy = std::abs(y2 - y1);
554 const int sx = x1 < x2 ? 1 : -1;
555 const int sy = y1 < y2 ? 1 : -1;
556 const int length = std::max(dx, dy);
558 if (!IsIn(x1, y1) && !IsIn(x2, y2)) {
561 if (dx + dx > width_ * height_) {
566 for (
int i = 0; i < length; ++i) {
568 if (2 * error >= -dy) {
572 if (2 * error <= dx) {
680 int dx = (1 + 2 * x) * e2 * e2;
685 DrawBlock(x1 - x, 2 * (y1 + y),
true, s);
686 DrawBlock(x1 + x, 2 * (y1 + y),
true, s);
687 DrawBlock(x1 + x, 2 * (y1 - y),
true, s);
688 DrawBlock(x1 - x, 2 * (y1 - y),
true, s);
692 err += dx += 2 * r2 * r2;
696 err += dy += 2 * r1 * r1;
746 int dx = (1 + 2 * x) * e2 * e2;
751 for (
int xx = x1 + x; xx <= x1 - x; ++xx) {
758 err += dx += 2 * r2 * r2;
762 err += dy += 2 * r1 * r1;
767 for (
int yy = y1 + y; yy <= y1 - y; ++yy) {
788 const std::string& value,
800 const std::string& value,
806 Cell& cell = storage_[XY{x / 2, y / 4}];
807 cell.type = CellType::kText;
818 style(storage_[XY{x / 2, y / 4}].content);
824class CanvasNodeBase :
public Node {
826 CanvasNodeBase() =
default;
830 int y_max = std::min(c.
height() / 4, box_.y_max - box_.y_min + 1);
831 int x_max = std::min(c.
width() / 2, box_.x_max - box_.x_min + 1);
832 for (
int y = 0; y < y_max; ++y) {
833 for (
int x = 0; x < x_max; ++x) {
839 virtual const Canvas&
canvas() = 0;
846 class Impl :
public CanvasNodeBase {
849 requirement_.min_x = (canvas_->width() + 1) / 2;
850 requirement_.min_y = (canvas_->height() + 3) / 4;
855 return std::make_shared<Impl>(std::move(
canvas));
863 class Impl :
public CanvasNodeBase {
865 Impl(
int width,
int height, std::function<
void(
Canvas&)> fn)
866 : width_(width), height_(height), fn_(std::move(fn)) {}
868 void ComputeRequirement()
final {
869 requirement_.min_x = (width_ + 1) / 2;
870 requirement_.min_y = (height_ + 3) / 4;
874 int width = (box_.y_max - box_.y_min + 1) * 2;
875 int height = (box_.x_max - box_.x_min + 1) * 4;
876 canvas_ =
Canvas(width, height);
878 CanvasNodeBase::Render(screen);
885 std::function<void(
Canvas&)> fn_;
887 return std::make_shared<Impl>(width, height, std::move(fn));
893 const int default_dim = 12;
894 return canvas(default_dim, default_dim, std::move(fn));
A class representing terminal colors.
An adapter. Own or reference an immutable object.
A rectangular grid of Pixel.
Pixel & PixelAt(int x, int y)
Access a Pixel at a given position.
std::shared_ptr< Node > Element
std::vector< std::string > Utf8ToGlyphs(const std::string &input)
Element canvas(ConstRef< Canvas >)
Produce an element from a Canvas, or a reference to a Canvas.
void Render(Screen &screen, const Element &element)
Display an element on a ftxui::Screen.
Decorator color(Color)
Decorate using a foreground color.
void DrawBlockLine(int x1, int y1, int x2, int y2)
Draw a line made of block characters.
void DrawPointEllipseFilled(int x, int y, int r1, int r2)
Draw a filled ellipse made of braille dots.
void DrawPointLine(int x1, int y1, int x2, int y2)
Draw a line made of braille dots.
void DrawText(int x, int y, const std::string &value)
Draw a piece of text.
std::function< void(Pixel &)> Stylizer
void DrawBlockOn(int x, int y)
Draw a block.
void DrawPointCircleFilled(int x, int y, int radius)
Draw a filled circle made of braille dots.
void DrawPointOn(int x, int y)
Draw a braille dot.
void DrawPointOff(int x, int y)
Erase a braille dot.
Pixel GetPixel(int x, int y) const
Get the content of a cell.
void DrawBlockEllipseFilled(int x1, int y1, int r1, int r2)
Draw a filled ellipse made of block characters.
void DrawPointEllipse(int x, int y, int r1, int r2)
Draw an ellipse made of braille dots.
void DrawPoint(int x, int y, bool value)
Draw a braille dot.
void DrawBlockEllipse(int x1, int y1, int r1, int r2)
Draw an ellipse made of block characters.
void DrawBlockToggle(int x, int y)
Toggle a block. If it is filled, it will be erased. If it is empty, it will be filled.
void DrawBlockCircle(int x1, int y1, int radius)
Draw a circle made of block characters.
void DrawBlockCircleFilled(int x1, int y1, int radius)
Draw a filled circle made of block characters.
void DrawPointCircle(int x, int y, int radius)
Draw a circle made of braille dots.
void DrawBlockOff(int x, int y)
Erase a block.
void DrawBlock(int x, int y, bool value)
Draw a block.
void Style(int x, int y, const Stylizer &style)
Modify a pixel at a given location.
void DrawPointToggle(int x, int y)
Toggle a braille dot. A filled one will be erased, and the other will be drawn.
A unicode character and its associated style.