51uint8_t g_map_braille[2][4][2] = {
53 {0b00000000, 0b00000001},
54 {0b00000000, 0b00000010},
55 {0b00000000, 0b00000100},
56 {0b00000001, 0b00000000},
59 {0b00000000, 0b00001000},
60 {0b00000000, 0b00010000},
61 {0b00000000, 0b00100000},
62 {0b00000010, 0b00000000},
67std::vector<std::string> g_map_block = {
68 " ",
"▘",
"▖",
"▌",
"▝",
"▀",
"▞",
"▛",
69 "▗",
"▚",
"▄",
"▙",
"▐",
"▜",
"▟",
"█",
73const std::map<std::string, uint8_t> g_map_block_inversed = {
74 {
" ", 0b0000}, {
"▘", 0b0001}, {
"▖", 0b0010}, {
"▌", 0b0011},
75 {
"▝", 0b0100}, {
"▀", 0b0101}, {
"▞", 0b0110}, {
"▛", 0b0111},
76 {
"▗", 0b1000}, {
"▚", 0b1001}, {
"▄", 0b1010}, {
"▙", 0b1011},
77 {
"▐", 0b1100}, {
"▜", 0b1101}, {
"▟", 0b1110}, {
"█", 0b1111},
80constexpr auto nostyle = [](Pixel& ) {};
90 storage_(width_ * height_ / 8 ) {}
96 auto it = storage_.find(XY{x, y});
97 return (it == storage_.end()) ?
Pixel() : it->second.content;
138 Cell& cell = storage_[XY{x / 2, y / 4}];
139 if (cell.type != CellType::kBraille) {
140 cell.content.character =
"⠀";
141 cell.type = CellType::kBraille;
144 cell.content.character[1] |= g_map_braille[x % 2][y % 4][0];
145 cell.content.character[2] |= g_map_braille[x % 2][y % 4][1];
155 Cell& cell = storage_[XY{x / 2, y / 4}];
156 if (cell.type != CellType::kBraille) {
157 cell.content.character =
"⠀";
158 cell.type = CellType::kBraille;
161 cell.content.character[1] &= ~(g_map_braille[x % 2][y % 4][0]);
162 cell.content.character[2] &= ~(g_map_braille[x % 2][y % 4][1]);
173 Cell& cell = storage_[XY{x / 2, y / 4}];
174 if (cell.type != CellType::kBraille) {
175 cell.content.character =
"⠀";
176 cell.type = CellType::kBraille;
179 cell.content.character[1] ^= g_map_braille[x % 2][y % 4][0];
180 cell.content.character[2] ^= g_map_braille[x % 2][y % 4][1];
214 const int dx = std::abs(x2 - x1);
215 const int dy = std::abs(y2 - y1);
216 const int sx = x1 < x2 ? 1 : -1;
217 const int sy = y1 < y2 ? 1 : -1;
218 const int length = std::max(dx, dy);
220 if (!IsIn(x1, y1) && !IsIn(x2, y2)) {
223 if (dx + dx > width_ * height_) {
228 for (
int i = 0; i < length; ++i) {
230 if (2 * error >= -dy) {
234 if (2 * error <= dx) {
340 int dx = (1 + 2 * x) * e2 * e2;
352 err += dx += 2 * r2 * r2;
356 err += dy += 2 * r1 * r1;
404 int dx = (1 + 2 * x) * e2 * e2;
409 for (
int xx = x1 + x; xx <= x1 - x; ++xx) {
416 err += dx += 2 * r2 * r2;
420 err += dy += 2 * r1 * r1;
425 for (
int yy = y1 - y; yy <= y1 + y; ++yy) {
470 Cell& cell = storage_[XY{x / 2, y / 2}];
471 if (cell.type != CellType::kBlock) {
472 cell.content.character =
" ";
473 cell.type = CellType::kBlock;
476 const uint8_t bit = (x % 2) * 2 + y % 2;
477 uint8_t value = g_map_block_inversed.at(cell.content.character);
479 cell.content.character = g_map_block[value];
489 Cell& cell = storage_[XY{x / 2, y / 4}];
490 if (cell.type != CellType::kBlock) {
491 cell.content.character =
" ";
492 cell.type = CellType::kBlock;
496 const uint8_t bit = (y % 2) * 2 + x % 2;
497 uint8_t value = g_map_block_inversed.at(cell.content.character);
498 value &= ~(1U << bit);
499 cell.content.character = g_map_block[value];
510 Cell& cell = storage_[XY{x / 2, y / 4}];
511 if (cell.type != CellType::kBlock) {
512 cell.content.character =
" ";
513 cell.type = CellType::kBlock;
517 const uint8_t bit = (y % 2) * 2 + x % 2;
518 uint8_t value = g_map_block_inversed.at(cell.content.character);
520 cell.content.character = g_map_block[value];
557 const int dx = std::abs(x2 - x1);
558 const int dy = std::abs(y2 - y1);
559 const int sx = x1 < x2 ? 1 : -1;
560 const int sy = y1 < y2 ? 1 : -1;
561 const int length = std::max(dx, dy);
563 if (!IsIn(x1, y1) && !IsIn(x2, y2)) {
566 if (dx + dx > width_ * height_) {
571 for (
int i = 0; i < length; ++i) {
573 if (2 * error >= -dy) {
577 if (2 * error <= dx) {
685 int dx = (1 + 2 * x) * e2 * e2;
690 DrawBlock(x1 - x, 2 * (y1 + y),
true, s);
691 DrawBlock(x1 + x, 2 * (y1 + y),
true, s);
692 DrawBlock(x1 + x, 2 * (y1 - y),
true, s);
693 DrawBlock(x1 - x, 2 * (y1 - y),
true, s);
697 err += dx += 2 * r2 * r2;
701 err += dy += 2 * r1 * r1;
751 int dx = (1 + 2 * x) * e2 * e2;
756 for (
int xx = x1 + x; xx <= x1 - x; ++xx) {
763 err += dx += 2 * r2 * r2;
767 err += dy += 2 * r1 * r1;
772 for (
int yy = y1 + y; yy <= y1 - y; ++yy) {
793 const std::string& value,
805 const std::string& value,
812 Cell& cell = storage_[XY{x / 2, y / 4}];
813 cell.type = CellType::kText;
814 cell.content.character = it;
824 style(storage_[XY{x / 2, y / 4}].content);
830class CanvasNodeBase :
public Node {
832 CanvasNodeBase() =
default;
836 const int y_max = std::min(c.
height() / 4, box_.y_max - box_.y_min + 1);
837 const int x_max = std::min(c.
width() / 2, box_.x_max - box_.x_min + 1);
838 for (
int y = 0; y < y_max; ++y) {
839 for (
int x = 0; x < x_max; ++x) {
845 virtual const Canvas&
canvas() = 0;
853 class Impl :
public CanvasNodeBase {
856 requirement_.min_x = (canvas_->width() + 1) / 2;
857 requirement_.min_y = (canvas_->height() + 3) / 4;
862 return std::make_shared<Impl>(
canvas);
870 class Impl :
public CanvasNodeBase {
872 Impl(
int width,
int height, std::function<
void(
Canvas&)> fn)
873 : width_(width), height_(height), fn_(std::move(fn)) {}
875 void ComputeRequirement()
final {
876 requirement_.min_x = (width_ + 1) / 2;
877 requirement_.min_y = (height_ + 3) / 4;
881 const int width = (box_.x_max - box_.x_min + 1) * 2;
882 const int height = (box_.y_max - box_.y_min + 1) * 4;
883 canvas_ =
Canvas(width, height);
892 std::function<void(
Canvas&)> fn_;
894 return std::make_shared<Impl>(width, height, std::move(fn));
900 const int default_dim = 12;
901 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 cell (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.