45uint8_t g_map_braille[2][4][2] = {
47 {0b00000000, 0b00000001},
48 {0b00000000, 0b00000010},
49 {0b00000000, 0b00000100},
50 {0b00000001, 0b00000000},
53 {0b00000000, 0b00001000},
54 {0b00000000, 0b00010000},
55 {0b00000000, 0b00100000},
56 {0b00000010, 0b00000000},
60std::vector<std::string> g_map_block = {
61 " ",
"▘",
"▖",
"▌",
"▝",
"▀",
"▞",
"▛",
62 "▗",
"▚",
"▄",
"▙",
"▐",
"▜",
"▟",
"█",
65const std::map<std::string, uint8_t> g_map_block_inversed = {
66 {
" ", 0b0000}, {
"▘", 0b0001}, {
"▖", 0b0010}, {
"▌", 0b0011},
67 {
"▝", 0b0100}, {
"▀", 0b0101}, {
"▞", 0b0110}, {
"▛", 0b0111},
68 {
"▗", 0b1000}, {
"▚", 0b1001}, {
"▄", 0b1010}, {
"▙", 0b1011},
69 {
"▐", 0b1100}, {
"▜", 0b1101}, {
"▟", 0b1110}, {
"█", 0b1111},
78 : width_(width), height_(height), storage_(width_ * height_ / 8) {}
84 auto it = storage_.find(XY{x, y});
85 return (it == storage_.end()) ?
Pixel{} : it->second.content;
124 Cell& cell = storage_[XY{x / 2, y / 4}];
125 if (cell.type != CellType::kBraille) {
127 cell.type = CellType::kBraille;
130 cell.content.
character[1] |= g_map_braille[x % 2][y % 4][0];
131 cell.content.
character[2] |= g_map_braille[x % 2][y % 4][1];
140 Cell& cell = storage_[XY{x / 2, y / 4}];
141 if (cell.type != CellType::kBraille) {
143 cell.type = CellType::kBraille;
146 cell.content.
character[1] &= ~(g_map_braille[x % 2][y % 4][0]);
147 cell.content.
character[2] &= ~(g_map_braille[x % 2][y % 4][1]);
157 Cell& cell = storage_[XY{x / 2, y / 4}];
158 if (cell.type != CellType::kBraille) {
160 cell.type = CellType::kBraille;
163 cell.content.
character[1] ^= g_map_braille[x % 2][y % 4][0];
164 cell.content.
character[2] ^= g_map_braille[x % 2][y % 4][1];
198 const int dx = std::abs(x2 - x1);
199 const int dy = std::abs(y2 - y1);
200 const int sx = x1 < x2 ? 1 : -1;
201 const int sy = y1 < y2 ? 1 : -1;
202 const int length = std::max(dx, dy);
204 if (!IsIn(x1, y1) && !IsIn(x2, y2))
206 if (dx + dx > width_ * height_)
210 for (
int i = 0; i < length; ++i) {
212 if (2 * error >= -dy) {
216 if (2 * error <= dx) {
322 int dx = (1 + 2 * x) * e2 * e2;
334 err += dx += 2 * r2 * r2;
338 err += dy += 2 * r1 * r1;
386 int dx = (1 + 2 * x) * e2 * e2;
391 for (
int xx = x1 + x; xx <= x1 - x; ++xx) {
398 err += dx += 2 * (long)r2 * r2;
402 err += dy += 2 * (long)r1 * r1;
407 for (
int yy = y1 - y; yy <= y1 + y; ++yy) {
450 Cell& cell = storage_[XY{x / 2, y / 2}];
451 if (cell.type != CellType::kBlock) {
453 cell.type = CellType::kBlock;
456 int bit = (x % 2) * 2 + y % 2;
457 uint8_t value = g_map_block_inversed.at(cell.content.
character);
459 cell.content.
character = g_map_block[value];
468 Cell& cell = storage_[XY{x / 2, y / 4}];
469 if (cell.type != CellType::kBlock) {
471 cell.type = CellType::kBlock;
475 int bit = (y % 2) * 2 + x % 2;
476 uint8_t value = g_map_block_inversed.at(cell.content.
character);
477 value &= ~(1 << bit);
478 cell.content.
character = g_map_block[value];
488 Cell& cell = storage_[XY{x / 2, y / 4}];
489 if (cell.type != CellType::kBlock) {
491 cell.type = CellType::kBlock;
495 int bit = (y % 2) * 2 + x % 2;
496 uint8_t value = g_map_block_inversed.at(cell.content.
character);
498 cell.content.
character = g_map_block[value];
535 const int dx = std::abs(x2 - x1);
536 const int dy = std::abs(y2 - y1);
537 const int sx = x1 < x2 ? 1 : -1;
538 const int sy = y1 < y2 ? 1 : -1;
539 const int length = std::max(dx, dy);
541 if (!IsIn(x1, y1) && !IsIn(x2, y2))
543 if (dx + dx > width_ * height_)
547 for (
int i = 0; i < length; ++i) {
549 if (2 * error >= -dy) {
553 if (2 * error <= dx) {
661 int dx = (1 + 2 * x) * e2 * e2;
666 DrawBlock(x1 - x, 2 * (y1 + y),
true, s);
667 DrawBlock(x1 + x, 2 * (y1 + y),
true, s);
668 DrawBlock(x1 + x, 2 * (y1 - y),
true, s);
669 DrawBlock(x1 - x, 2 * (y1 - y),
true, s);
673 err += dx += 2 * r2 * r2;
677 err += dy += 2 * r1 * r1;
727 int dx = (1 + 2 * x) * e2 * e2;
732 for (
int xx = x1 + x; xx <= x1 - x; ++xx) {
739 err += dx += 2 * r2 * r2;
743 err += dy += 2 * r1 * r1;
748 for (
int yy = y1 + y; yy <= y1 - y; ++yy) {
769 const std::string& value,
781 const std::string& value,
786 Cell& cell = storage_[XY{x / 2, y / 4}];
787 cell.type = CellType::kText;
798 style(storage_[XY{x / 2, y / 4}].content);
803class CanvasNodeBase :
public Node {
807 void Render(Screen& screen)
override {
808 const Canvas& c =
canvas();
809 int y_max = std::min(c.height() / 4, box_.y_max - box_.y_min + 1);
810 int x_max = std::min(c.width() / 2, box_.x_max - box_.x_min + 1);
811 for (
int y = 0; y < y_max; ++y) {
812 for (
int x = 0; x < x_max; ++x) {
813 screen.PixelAt(box_.x_min + x, box_.y_min + y) = c.GetPixel(x, y);
818 virtual const Canvas&
canvas() = 0;
825 class Impl :
public CanvasNodeBase {
828 requirement_.min_x = (canvas_->width() + 1) / 2;
829 requirement_.min_y = (canvas_->height() + 3) / 4;
834 return std::make_shared<Impl>(std::move(
canvas));
842 class Impl :
public CanvasNodeBase {
844 Impl(
int width,
int height, std::function<
void(
Canvas&)> fn)
845 : width_(width), height_(height), fn_(std::move(fn)) {}
847 void ComputeRequirement()
final {
848 requirement_.min_x = (width_ + 1) / 2;
849 requirement_.min_y = (height_ + 3) / 4;
853 int width = (box_.y_max - box_.y_min + 1) * 2;
854 int height = (box_.x_max - box_.x_min + 1) * 4;
855 canvas_ =
Canvas(width, height);
857 CanvasNodeBase::Render(screen);
864 std::function<void(
Canvas&)> fn_;
866 return std::make_shared<Impl>(width, height, std::move(fn));
872 return canvas(12, 12, std::move(fn));
A class representing terminal colors.
An adapter. Own or reference an immutable object.
A rectangular grid of Pixel.
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 &node)
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.